This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_test.go
69 lines (66 loc) · 1.49 KB
/
main_test.go
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
66
67
68
69
package main
import (
"context"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestCovidStatusString(t *testing.T) {
tests := []struct {
name string
fields CovidStatus
want string
}{
{
name: "Test format message to show in the notification",
fields: CovidStatus{
Cases: 100,
Deaths: 7,
Recovered: 1,
},
want: "Cases: 100, Deaths: 7, Recovered: 1",
},
}
for _, v := range tests {
v := v
t.Run(v.name, func(t *testing.T) {
t.Parallel()
if got := v.fields.String(); got != v.want {
t.Errorf("CovidStatus.String() = %v, want %v", got, v.want)
}
})
}
}
func TestFetch(t *testing.T) {
tests := []struct {
name string
input io.Reader
want bool
}{
{"ok json", strings.NewReader(`{"data": {"cases": 10, "deaths": 10, "recovered": 10}}`), true},
{"bad json", strings.NewReader(`{"data": "cases": 10, "deaths": 10, "recovered": 10}}`), false},
}
for _, v := range tests {
v := v
t.Run(v.name, func(t *testing.T) {
t.Parallel()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.Copy(w, v.input)
}))
defer ts.Close()
req, err := http.NewRequest("GET", ts.URL, nil)
if err != nil {
panic("misuse of NewRequest")
}
ch := make(chan CovidStatus)
go func() { // drain the fetch output out
_ = <-ch
}()
if err := fetch(context.TODO(), req, ch); (err == nil) != v.want {
t.Errorf("fetch: expected: %v got: %v", v.want, err)
}
})
}
}