-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstruments_test.go
125 lines (118 loc) · 2.88 KB
/
instruments_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package openaq
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"testing"
)
const instruments = `{"meta":{"name":"openaq-api","website":"/","page":1,"limit":100,"found":1},"results":[{"id":1,"name":"OpenAQ admin","locationsCount":7061}]}`
func TestInstrumentsQueryParams(t *testing.T) {
instrumentArgs := InstrumentArgs{
BaseArgs: BaseArgs{
Limit: 100,
Page: 42,
},
}
queryString, err := instrumentArgs.QueryParams()
ok(t, err)
expected := url.Values{"limit": []string{"100"}, "page": []string{"42"}}
equals(t, expected, queryString)
}
func TestGetInstruments(t *testing.T) {
client := NewTestClient(func(req *http.Request) *http.Response {
equals(t, req.URL.String(), "https://api.openaq.org/v3/instruments?limit=100&page=1")
return &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(manufacturers)),
Header: make(http.Header),
}
})
config := &Config{
Client: client,
}
openAQClient, err := NewClient(*config)
if err != nil {
fmt.Println("Failed to create new client")
}
args := &InstrumentArgs{}
ctx := context.Background()
body, err := openAQClient.GetInstruments(ctx, *args)
expected := InstrumentsResponse{
Meta: Meta{
Name: "openaq-api",
Website: "/",
Limit: 100,
Page: 1,
Found: float64(1),
},
Results: []Instrument{
{
ID: 1,
Name: "OpenAQ admin",
},
},
}
ok(t, err)
equals(t, &expected, body)
}
func TestGetInstrument(t *testing.T) {
client := NewTestClient(func(req *http.Request) *http.Response {
// Test request parameters
equals(t, req.URL.String(), "https://api.openaq.org/v3/instruments/1")
return &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(owners)),
Header: make(http.Header),
}
})
config := &Config{
Client: client,
}
openAQClient, err := NewClient(*config)
if err != nil {
fmt.Println("")
}
ctx := context.Background()
body, err := openAQClient.GetInstrument(ctx, 1)
ok(t, err)
equals(t, body.Results[len(body.Results)-1].ID, int64(1))
}
func TestGetManufacturerInstruments(t *testing.T) {
client := NewTestClient(func(req *http.Request) *http.Response {
equals(t, req.URL.String(), "https://api.openaq.org/v3/manufacturers/42/instruments")
return &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(manufacturers)),
Header: make(http.Header),
}
})
config := &Config{
Client: client,
}
openAQClient, err := NewClient(*config)
if err != nil {
fmt.Println("Failed to create new client")
}
ctx := context.Background()
body, err := openAQClient.GetManufacturerInstruments(ctx, 42)
expected := InstrumentsResponse{
Meta: Meta{
Name: "openaq-api",
Website: "/",
Limit: 100,
Page: 1,
Found: float64(1),
},
Results: []Instrument{
{
ID: 1,
Name: "OpenAQ admin",
},
},
}
ok(t, err)
equals(t, &expected, body)
}