~kota/metweather

4bcb0564de18fe244385f72f95c28f323c667d55 — Dakota Walsh 4 months ago de059fd
use proper temperature symbol for forecast

I updated the forecast function to use the proper Celsius symbol and
fixed the tests
2 files changed, 23 insertions(+), 18 deletions(-)

M cmd/forecast.go
M cmd/forecast_test.go
M cmd/forecast.go => cmd/forecast.go +6 -4
@@ 23,6 23,7 @@ func init() {
	viper.BindPFlag("location", forecastCmd.PersistentFlags().Lookup("location"))
}

// forecast fetches and prints a forecast based on options provided
func forecast(cmd *cobra.Command, args []string) {
	client := metservice.NewClient()
	ctx := context.Background()


@@ 36,20 37,21 @@ func forecast(cmd *cobra.Command, args []string) {
	}
}

// forecastWeekly fetches and prints a forecast on one line for each day
// forecastWeekly fetches and prints a forecast on one line for each day using
// a provided client, context, and location
func forecastWeekly(client *metservice.Client, ctx context.Context, location string) error {
	forecast, _, err := client.GetForecast(ctx, location)
	f, _, err := client.GetForecast(ctx, location)
	if err != nil {
		return fmt.Errorf("getting forecast: %v", err)
	}
	for _, day := range forecast.Days {
	for _, day := range f.Days {
		fmt.Fprintf(out, "%d-%d-%d ",
			day.Date.Local().Year(),
			day.Date.Local().Month(),
			day.Date.Local().Day())
		fmt.Fprintf(out, "%s ", *day.ForecastWord)
		fmt.Fprintf(out, "%d-", *day.Min)
		fmt.Fprintf(out, "%dc\n", *day.Max)
		fmt.Fprintf(out, "%d°C\n", *day.Max)
	}
	return nil
}

M cmd/forecast_test.go => cmd/forecast_test.go +17 -14
@@ 9,34 9,37 @@ import (
	"testing"
)

func TestForecast(t *testing.T) {
func TestForecastWeekly(t *testing.T) {
	client, mux, teardown := setup()
	defer teardown()

	mux.HandleFunc("/localForecastDunedin", func(w http.ResponseWriter, r *http.Request) {
		reader, err := os.Open("data/localForecastDunedin.json")
		if err != nil {
			t.Errorf("metweather.forecast returned error: %v", err)
			t.Errorf("metweather.forecastWeekly returned error: %v", err)
		}
		io.Copy(w, reader)
	})

	ctx := context.Background()
	out = new(bytes.Buffer) // captured output
	forecastWeekly(client, ctx, "Dunedin")
	err := forecastWeekly(client, ctx, "Dunedin")
	if err != nil {
		t.Errorf("metweather.forecastWeekly returned error: %v", err)
	}
	got := out.(*bytes.Buffer).String()
	want := `2021-7-16 Partly cloudy 7-13c
2021-7-17 Showers 8-11c
2021-7-18 Few showers 7-10c
2021-7-19 Showers 4-11c
2021-7-20 Partly cloudy 6-11c
2021-7-21 Partly cloudy 6-12c
2021-7-22 Few showers 6-11c
2021-7-23 Fine 5-10c
2021-7-24 Fine 5-11c
2021-7-25 Partly cloudy 6-12c
	want := `2021-7-16 Partly cloudy 7-13°C
2021-7-17 Showers 8-11°C
2021-7-18 Few showers 7-10°C
2021-7-19 Showers 4-11°C
2021-7-20 Partly cloudy 6-11°C
2021-7-21 Partly cloudy 6-12°C
2021-7-22 Few showers 6-11°C
2021-7-23 Fine 5-10°C
2021-7-24 Fine 5-11°C
2021-7-25 Partly cloudy 6-12°C
`
	if got != want {
		t.Errorf("got = %q\nwant = %q", got, want)
		t.Errorf("metweather.forecastWeekly\ngot = %q\nwant = %q", got, want)
	}
}