~kota/metweather

ref: v0.1.0 metweather/cmd/observation_test.go -rw-r--r-- 1.5 KiB
bd4ec2c4Dakota Walsh add installation instructions to readme 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package cmd

import (
	"bytes"
	"context"
	"io"
	"net/http"
	"os"
	"testing"
)

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

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

	ctx := context.Background()
	out = new(bytes.Buffer) // captured output
	err := observationThreeHour(client, ctx, "Dunedin")
	if err != nil {
		t.Errorf("metweather.observationThreeHour returned error: %v", err)
	}
	got := out.(*bytes.Buffer).String()
	want := "9°C 2km/h W - 77%(10.01°C)\n"
	if got != want {
		t.Errorf("metweather.observationThreeHour\ngot = %q\nwant = %q", got, want)
	}
}

func TestWindChill(t *testing.T) {
	var tests = []struct {
		temp  int
		speed int
		want  float64
	}{
		{12, 5, 12.042135509662362},
		{12, 10, 11.04098839261837},
		{12, 25, 9.535110852166257},
		{12, 50, 8.239921987968058},
		{9, 5, 8.640714167661585},
		{9, 10, 7.459305944972208},
		{9, 25, 5.682288283565697},
		{9, 50, 4.153894755731934},
		{0, 5, -1.5635498583407408},
		{0, 10, -3.2857413979662784},
		{0, 25, -5.87617942223598},
		{0, 50, -8.104186940976438},
	}
	for _, test := range tests {
		if got := windChill(test.temp, test.speed); got != test.want {
			t.Errorf("metweather.windChill\ntemp = %d\nspeed = %d\nwant = %f\n got = %f\n",
				test.temp,
				test.speed,
				test.want,
				got)
		}
	}
}