-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddon_test.go
96 lines (72 loc) · 2.1 KB
/
addon_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
package eygo
import (
"encoding/json"
"testing"
)
func TestNewAddonService(t *testing.T) {
driver := NewMockDriver()
service := NewAddonService(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 TestAddonService_ForAccount(t *testing.T) {
account := &Account{ID: "1", Name: "Account 1"}
driver := NewMockDriver()
service := NewAddonService(driver)
t.Run("when there are matching addons", func(t *testing.T) {
addon1 := &Addon{ID: 1}
addon2 := &Addon{ID: 2}
addon3 := &Addon{ID: 3}
stubAccountAddons(driver, account, addon1, addon2, addon3)
all := service.ForAccount(account, nil)
t.Run("it contains all matching addons", func(t *testing.T) {
addons := []*Addon{addon1, addon2, addon3}
if len(all) != len(addons) {
t.Errorf("Expected %d addons, got %d", len(addons), len(all))
}
for _, addon := range addons {
found := false
for _, other := range all {
if addon.ID == other.ID {
found = true
}
}
if !found {
t.Errorf("Addon %d was not present", addon.ID)
}
}
})
})
t.Run("when there are no matching addons", func(t *testing.T) {
driver.Reset()
t.Run("it is empty", func(t *testing.T) {
all := service.ForAccount(account, nil)
if len(all) != 0 {
t.Errorf("Expected 0 addons, got")
}
})
})
}
func stubAddons(driver *MockDriver, addons ...*Addon) {
pages := make([][]byte, 0)
wrapper := struct {
Addons []*Addon `json:"addons,omitempty"`
}{Addons: addons}
if encoded, err := json.Marshal(&wrapper); err == nil {
pages = append(pages, encoded)
driver.AddResponse("get", "addons", Response{Pages: pages})
}
}
func stubAccountAddons(driver *MockDriver, account *Account, addons ...*Addon) {
pages := make([][]byte, 0)
wrapper := struct {
Addons []*Addon `json:"addons,omitempty"`
}{Addons: addons}
if encoded, err := json.Marshal(&wrapper); err == nil {
pages = append(pages, encoded)
driver.AddResponse("get", "accounts/"+account.ID+"/addons", Response{Pages: pages})
}
}