-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlocations_test.go
140 lines (112 loc) · 3.57 KB
/
locations_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package solus
import (
"context"
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/guregu/null.v4"
)
func TestLocationsService_Create(t *testing.T) {
data := LocationCreateRequest{
Name: "name",
Description: "description",
IconID: null.IntFrom(1),
IsDefault: false,
IsVisible: true,
}
s := startTestServer(t, func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/locations", r.URL.Path)
assert.Equal(t, http.MethodPost, r.Method)
assertRequestBody(t, r, data)
writeResponse(t, w, http.StatusCreated, fakeLocation)
})
defer s.Close()
actual, err := createTestClient(t, s.URL).Locations.Create(context.Background(), data)
require.NoError(t, err)
require.Equal(t, fakeLocation, actual)
}
func TestLocationsService_List(t *testing.T) {
expected := LocationsResponse{
Data: []Location{
fakeLocation,
},
}
s := startTestServer(t, func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/locations", r.URL.Path)
assert.Equal(t, http.MethodGet, r.Method)
assertRequestQuery(t, r, url.Values{
"filter[search]": []string{"name"},
})
writeJSON(t, w, http.StatusOK, expected)
})
defer s.Close()
f := (&FilterLocations{}).ByName("name")
actual, err := createTestClient(t, s.URL).Locations.List(context.Background(), f)
require.NoError(t, err)
actual.service = nil
require.Equal(t, expected, actual)
}
func TestLocationsService_Get(t *testing.T) {
s := startTestServer(t, func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/locations/10", r.URL.Path)
assert.Equal(t, http.MethodGet, r.Method)
writeResponse(t, w, http.StatusOK, fakeLocation)
})
defer s.Close()
actual, err := createTestClient(t, s.URL).Locations.Get(context.Background(), 10)
require.NoError(t, err)
require.Equal(t, fakeLocation, actual)
}
func TestLocationsService_Update(t *testing.T) {
data := LocationCreateRequest{
Name: "name",
Description: "description",
IconID: null.IntFrom(1),
IsDefault: false,
IsVisible: true,
}
s := startTestServer(t, func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/locations/10", r.URL.Path)
assert.Equal(t, http.MethodPut, r.Method)
assertRequestBody(t, r, data)
writeResponse(t, w, http.StatusOK, fakeLocation)
})
defer s.Close()
actual, err := createTestClient(t, s.URL).Locations.Update(context.Background(), 10, data)
require.NoError(t, err)
require.Equal(t, fakeLocation, actual)
}
func TestLocationsService_Patch(t *testing.T) {
data := LocationPatchRequest{
IsDefault: false,
IsVisible: true,
}
s := startTestServer(t, func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/locations/10", r.URL.Path)
assert.Equal(t, http.MethodPatch, r.Method)
assertRequestBody(t, r, data)
response := fakeLocation
response.IsDefault = data.IsDefault
response.IsVisible = data.IsVisible
writeResponse(t, w, http.StatusOK, response)
})
defer s.Close()
actual, err := createTestClient(t, s.URL).Locations.Patch(context.Background(), 10, data)
require.NoError(t, err)
expected := fakeLocation
expected.IsDefault = false
expected.IsVisible = true
require.Equal(t, expected, actual)
}
func TestLocationsService_Delete(t *testing.T) {
s := startTestServer(t, func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/locations/10", r.URL.Path)
assert.Equal(t, http.MethodDelete, r.Method)
w.WriteHeader(http.StatusNoContent)
})
defer s.Close()
err := createTestClient(t, s.URL).Locations.Delete(context.Background(), 10)
require.NoError(t, err)
}