-
Notifications
You must be signed in to change notification settings - Fork 0
/
manufacturers_test.go
89 lines (82 loc) · 2.06 KB
/
manufacturers_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
package openaq
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"testing"
)
const manufacturers = `{"meta":{"name":"openaq-api","website":"/","page":1,"limit":100,"found":1},"results":[{"id":1,"name":"OpenAQ admin","locationsCount":7061}]}`
func TestManufacturersQueryParams(t *testing.T) {
manufacturerArgs := ManufacturerArgs{
BaseArgs: BaseArgs{
Limit: 100,
Page: 42,
},
}
queryString, err := manufacturerArgs.QueryParams()
ok(t, err)
expected := url.Values{"limit": []string{"100"}, "page": []string{"42"}}
equals(t, expected, queryString)
}
func TestGetManufacturers(t *testing.T) {
client := NewTestClient(func(req *http.Request) *http.Response {
equals(t, req.URL.String(), "https://api.openaq.org/v3/manufacturers?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 := &ManufacturerArgs{}
ctx := context.Background()
body, err := openAQClient.GetManufacturers(ctx, *args)
expected := ManufacturersResponse{
Meta: Meta{
Name: "openaq-api",
Website: "/",
Limit: 100,
Page: 1,
Found: float64(1),
},
Results: []Manufacturer{
{
ID: 1,
Name: "OpenAQ admin",
},
},
}
ok(t, err)
equals(t, &expected, body)
}
func TestGetManufacturer(t *testing.T) {
client := NewTestClient(func(req *http.Request) *http.Response {
// Test request parameters
equals(t, req.URL.String(), "https://api.openaq.org/v3/manufacturers/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.GetManufacturer(ctx, 1)
ok(t, err)
equals(t, body.Results[len(body.Results)-1].ID, int64(1))
}