-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider_location_test.go
151 lines (112 loc) · 3.81 KB
/
provider_location_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
141
142
143
144
145
146
147
148
149
150
151
package eygo
import (
"encoding/json"
//"fmt"
"strconv"
"testing"
)
func TestNewProviderLocationService(t *testing.T) {
driver := NewMockDriver()
service := NewProviderLocationService(driver)
t.Run("it is configured with the given driver", func(t *testing.T) {
if service.Driver != driver {
t.Errorf("Expected the service to use the given driver")
}
})
}
func TestProviderLocationService_ForProvider(t *testing.T) {
provider := &Provider{ID: 1}
driver := NewMockDriver()
service := NewProviderLocationService(driver)
t.Run("when there are matching providerLocations", func(t *testing.T) {
pl1 := &ProviderLocation{ID: "1"}
pl2 := &ProviderLocation{ID: "2"}
pl3 := &ProviderLocation{ID: "3"}
stubProviderProviderLocations(driver, provider, pl1, pl2, pl3)
all := service.ForProvider(provider, nil)
t.Run("it contains all matching providerLocations", func(t *testing.T) {
providerLocations := []*ProviderLocation{pl1, pl2, pl3}
if len(all) != len(providerLocations) {
t.Errorf("Expected %d providerLocations, got %d", len(providerLocations), len(all))
}
for _, providerLocation := range providerLocations {
found := false
for _, other := range all {
if providerLocation.ID == other.ID {
found = true
}
}
if !found {
t.Errorf("ProviderLocation %s was not present", providerLocation.ID)
}
}
})
})
t.Run("when there are no matching providerLocations", func(t *testing.T) {
driver.Reset()
t.Run("it is empty", func(t *testing.T) {
all := service.ForProvider(provider, nil)
if len(all) != 0 {
t.Errorf("Expected 0 providerLocations, got")
}
})
})
}
func TestProviderLocationService_Children(t *testing.T) {
parent := &ProviderLocation{ID: "mommy"}
driver := NewMockDriver()
service := NewProviderLocationService(driver)
t.Run("when there are matching providerLocations", func(t *testing.T) {
pl1 := &ProviderLocation{ID: "1"}
pl2 := &ProviderLocation{ID: "2"}
pl3 := &ProviderLocation{ID: "3"}
stubChildren(driver, parent, pl1, pl2, pl3)
all := service.Children(parent, nil)
t.Run("it contains all matching providerLocations", func(t *testing.T) {
providerLocations := []*ProviderLocation{pl1, pl2, pl3}
if len(all) != len(providerLocations) {
t.Errorf("Expected %d providerLocations, got %d", len(providerLocations), len(all))
}
for _, providerLocation := range providerLocations {
found := false
for _, other := range all {
if providerLocation.ID == other.ID {
found = true
}
}
if !found {
t.Errorf("ProviderLocation %s was not present", providerLocation.ID)
}
}
})
})
t.Run("when there are no matching providerLocations", func(t *testing.T) {
driver.Reset()
t.Run("it is empty", func(t *testing.T) {
all := service.Children(parent, nil)
if len(all) != 0 {
t.Errorf("Expected 0 providerLocations, got")
}
})
})
}
func stubProviderProviderLocations(driver *MockDriver, provider *Provider, providerLocations ...*ProviderLocation) {
pages := make([][]byte, 0)
wrapper := struct {
ProviderLocations []*ProviderLocation `json:"provider_locations,omitempty"`
}{ProviderLocations: providerLocations}
if encoded, err := json.Marshal(&wrapper); err == nil {
pages = append(pages, encoded)
driver.AddResponse("get", "providers/"+strconv.Itoa(provider.ID)+"/locations", Response{Pages: pages})
}
}
func stubChildren(driver *MockDriver, parent *ProviderLocation, children ...*ProviderLocation) {
pages := make([][]byte, 0)
wrapper := struct {
ProviderLocations []*ProviderLocation `json:"provider_locations,omitempty"`
}{ProviderLocations: children}
if encoded, err := json.Marshal(&wrapper); err == nil {
pages = append(pages, encoded)
driver.AddResponse("get", "provider-locations/"+parent.ID+"/provider-locations", Response{Pages: pages})
}
}