|
| 1 | +package eygo |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestNewAddonService(t *testing.T) { |
| 9 | + driver := NewMockDriver() |
| 10 | + service := NewAddonService(driver) |
| 11 | + |
| 12 | + t.Run("it is configured with the given driver", func(t *testing.T) { |
| 13 | + if service.Driver != driver { |
| 14 | + t.Errorf("Expected the service to use the given driver") |
| 15 | + } |
| 16 | + }) |
| 17 | +} |
| 18 | + |
| 19 | +func TestAddonService_ForAccount(t *testing.T) { |
| 20 | + account := &Account{ID: "1", Name: "Account 1"} |
| 21 | + driver := NewMockDriver() |
| 22 | + service := NewAddonService(driver) |
| 23 | + |
| 24 | + t.Run("when there are matching addons", func(t *testing.T) { |
| 25 | + addon1 := &Addon{ID: 1} |
| 26 | + addon2 := &Addon{ID: 2} |
| 27 | + addon3 := &Addon{ID: 3} |
| 28 | + |
| 29 | + stubAccountAddons(driver, account, addon1, addon2, addon3) |
| 30 | + |
| 31 | + all := service.ForAccount(account, nil) |
| 32 | + |
| 33 | + t.Run("it contains all matching addons", func(t *testing.T) { |
| 34 | + addons := []*Addon{addon1, addon2, addon3} |
| 35 | + |
| 36 | + if len(all) != len(addons) { |
| 37 | + t.Errorf("Expected %d addons, got %d", len(addons), len(all)) |
| 38 | + } |
| 39 | + |
| 40 | + for _, addon := range addons { |
| 41 | + found := false |
| 42 | + |
| 43 | + for _, other := range all { |
| 44 | + if addon.ID == other.ID { |
| 45 | + found = true |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if !found { |
| 50 | + t.Errorf("Addon %d was not present", addon.ID) |
| 51 | + } |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + }) |
| 56 | + |
| 57 | + t.Run("when there are no matching addons", func(t *testing.T) { |
| 58 | + driver.Reset() |
| 59 | + |
| 60 | + t.Run("it is empty", func(t *testing.T) { |
| 61 | + all := service.ForAccount(account, nil) |
| 62 | + |
| 63 | + if len(all) != 0 { |
| 64 | + t.Errorf("Expected 0 addons, got") |
| 65 | + } |
| 66 | + }) |
| 67 | + |
| 68 | + }) |
| 69 | + |
| 70 | +} |
| 71 | + |
| 72 | +func stubAddons(driver *MockDriver, addons ...*Addon) { |
| 73 | + pages := make([][]byte, 0) |
| 74 | + |
| 75 | + wrapper := struct { |
| 76 | + Addons []*Addon `json:"addons,omitempty"` |
| 77 | + }{Addons: addons} |
| 78 | + |
| 79 | + if encoded, err := json.Marshal(&wrapper); err == nil { |
| 80 | + pages = append(pages, encoded) |
| 81 | + driver.AddResponse("get", "addons", Response{Pages: pages}) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func stubAccountAddons(driver *MockDriver, account *Account, addons ...*Addon) { |
| 86 | + pages := make([][]byte, 0) |
| 87 | + |
| 88 | + wrapper := struct { |
| 89 | + Addons []*Addon `json:"addons,omitempty"` |
| 90 | + }{Addons: addons} |
| 91 | + |
| 92 | + if encoded, err := json.Marshal(&wrapper); err == nil { |
| 93 | + pages = append(pages, encoded) |
| 94 | + driver.AddResponse("get", "accounts/"+account.ID+"/addons", Response{Pages: pages}) |
| 95 | + } |
| 96 | +} |
0 commit comments