-
Notifications
You must be signed in to change notification settings - Fork 0
/
flavor_test.go
97 lines (73 loc) · 2.18 KB
/
flavor_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
package eygo
import (
"encoding/json"
//"fmt"
"testing"
)
func TestNewFlavorService(t *testing.T) {
driver := NewMockDriver()
service := NewFlavorService(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 TestFlavorService_ForAccount(t *testing.T) {
account := &Account{ID: "1", Name: "Account 1"}
driver := NewMockDriver()
service := NewFlavorService(driver)
t.Run("when there are matching flavors", func(t *testing.T) {
flavor1 := &Flavor{ID: "Flavor 1"}
flavor2 := &Flavor{ID: "Flavor 2"}
flavor3 := &Flavor{ID: "Flavor 3"}
stubAccountFlavors(driver, account, flavor1, flavor2, flavor3)
all := service.ForAccount(account, nil)
t.Run("it contains all matching flavors", func(t *testing.T) {
flavors := []*Flavor{flavor1, flavor2, flavor3}
if len(all) != len(flavors) {
t.Errorf("Expected %d flavors, got %d", len(flavors), len(all))
}
for _, flavor := range flavors {
found := false
for _, other := range all {
if flavor.ID == other.ID {
found = true
}
}
if !found {
t.Errorf("Flavor %s was not present", flavor.ID)
}
}
})
})
t.Run("when there are no matching flavors", 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 flavors, got")
}
})
})
}
func stubFlavors(driver *MockDriver, flavors ...*Flavor) {
pages := make([][]byte, 0)
wrapper := struct {
Flavors []*Flavor `json:"flavors,omitempty"`
}{Flavors: flavors}
if encoded, err := json.Marshal(&wrapper); err == nil {
pages = append(pages, encoded)
driver.AddResponse("get", "flavors", Response{Pages: pages})
}
}
func stubAccountFlavors(driver *MockDriver, account *Account, flavors ...*Flavor) {
pages := make([][]byte, 0)
wrapper := struct {
Flavors []*Flavor `json:"flavors,omitempty"`
}{Flavors: flavors}
if encoded, err := json.Marshal(&wrapper); err == nil {
pages = append(pages, encoded)
driver.AddResponse("get", "accounts/"+account.ID+"/flavors", Response{Pages: pages})
}
}