github.com/fastly/go-fastly@v1.18.0/fastly/dictionary_item_batch_test.go (about) 1 package fastly 2 3 import ( 4 "testing" 5 ) 6 7 func TestClient_BatchModifyDictionaryItems_Create(t *testing.T) { 8 9 fixtureBase := "dictionary_items_batch/create/" 10 nameSuffix := "BatchModifyDictionaryItems_Create" 11 12 // Given: a test service with a dictionary and a batch of create operations, 13 testService := createTestService(t, fixtureBase+"create_service", nameSuffix) 14 defer deleteTestService(t, fixtureBase+"delete_service", testService.ID) 15 16 testVersion := createTestVersion(t, fixtureBase+"create_version", testService.ID) 17 18 testDictionary := createTestDictionary(t, fixtureBase+"create_dictionary", testService.ID, testVersion.Number, nameSuffix) 19 defer deleteTestDictionary(t, testDictionary, fixtureBase+"delete_dictionary") 20 21 batchCreateOperations := &BatchModifyDictionaryItemsInput{ 22 Service: testService.ID, 23 Dictionary: testDictionary.ID, 24 Items: []*BatchDictionaryItem{ 25 { 26 Operation: CreateBatchOperation, 27 ItemKey: "key1", 28 ItemValue: "val1", 29 }, 30 { 31 Operation: CreateBatchOperation, 32 ItemKey: "key2", 33 ItemValue: "val2", 34 }, 35 }, 36 } 37 38 // When: I execute the batch create operations against the Fastly API, 39 var err error 40 record(t, fixtureBase+"create_dictionary_items", func(c *Client) { 41 42 err = c.BatchModifyDictionaryItems(batchCreateOperations) 43 }) 44 if err != nil { 45 t.Fatal(err) 46 } 47 48 // Then: I expect to be able to list all of the created dictionary items. 49 var actualDictionaryItems []*DictionaryItem 50 record(t, fixtureBase+"list_after_create", func(c *Client) { 51 actualDictionaryItems, err = c.ListDictionaryItems(&ListDictionaryItemsInput{ 52 Service: testService.ID, 53 Dictionary: testDictionary.ID, 54 }) 55 }) 56 if err != nil { 57 t.Fatal(err) 58 } 59 60 actualNumberOfDictItems := len(actualDictionaryItems) 61 expectedNumberOfDictItems := len(batchCreateOperations.Items) 62 if actualNumberOfDictItems != expectedNumberOfDictItems { 63 t.Errorf("Incorrect number of dictionary items returned, expected: %d, got %d", expectedNumberOfDictItems, actualNumberOfDictItems) 64 } 65 66 for i, item := range actualDictionaryItems { 67 68 actualItemKey := item.ItemKey 69 expectedItemKey := batchCreateOperations.Items[i].ItemKey 70 if actualItemKey != expectedItemKey { 71 t.Errorf("First ItemKey did not match, expected %s, got %s", expectedItemKey, actualItemKey) 72 } 73 74 actualItemValue := item.ItemValue 75 expectedItemValue := batchCreateOperations.Items[i].ItemValue 76 if actualItemValue != expectedItemValue { 77 t.Errorf("First ItemValue did not match, expected %s, got %s", expectedItemValue, actualItemValue) 78 } 79 80 } 81 82 } 83 84 func TestClient_BatchModifyDictionaryItems_Delete(t *testing.T) { 85 86 fixtureBase := "dictionary_items_batch/delete/" 87 nameSuffix := "BatchModifyDictionaryItems_Delete" 88 89 // Given: a test service with a dictionary and dictionary items, 90 testService := createTestService(t, fixtureBase+"create_service", nameSuffix) 91 defer deleteTestService(t, fixtureBase+"delete_service", testService.ID) 92 93 testVersion := createTestVersion(t, fixtureBase+"create_version", testService.ID) 94 95 testDictionary := createTestDictionary(t, fixtureBase+"create_dictionary", testService.ID, testVersion.Number, nameSuffix) 96 defer deleteTestDictionary(t, testDictionary, fixtureBase+"delete_dictionary") 97 98 batchCreateOperations := &BatchModifyDictionaryItemsInput{ 99 Service: testService.ID, 100 Dictionary: testDictionary.ID, 101 Items: []*BatchDictionaryItem{ 102 { 103 Operation: CreateBatchOperation, 104 ItemKey: "key1", 105 ItemValue: "val1", 106 }, 107 { 108 Operation: CreateBatchOperation, 109 ItemKey: "key2", 110 ItemValue: "val2", 111 }, 112 }, 113 } 114 115 var err error 116 record(t, fixtureBase+"create_dictionary_items", func(c *Client) { 117 118 err = c.BatchModifyDictionaryItems(batchCreateOperations) 119 }) 120 if err != nil { 121 t.Fatal(err) 122 } 123 124 // When: I execute the batch delete operations against the Fastly API, 125 batchDeleteOperations := &BatchModifyDictionaryItemsInput{ 126 Service: testService.ID, 127 Dictionary: testDictionary.ID, 128 Items: []*BatchDictionaryItem{ 129 { 130 Operation: DeleteBatchOperation, 131 ItemKey: "key2", 132 ItemValue: "val2", 133 }, 134 }, 135 } 136 137 record(t, fixtureBase+"delete_dictionary_items", func(c *Client) { 138 139 err = c.BatchModifyDictionaryItems(batchDeleteOperations) 140 }) 141 if err != nil { 142 t.Fatal(err) 143 } 144 145 // Then: I expect to be able to list a single dictionary item. 146 var actualDictionaryItems []*DictionaryItem 147 record(t, fixtureBase+"list_after_delete", func(client *Client) { 148 actualDictionaryItems, err = client.ListDictionaryItems(&ListDictionaryItemsInput{ 149 Service: testService.ID, 150 Dictionary: testDictionary.ID, 151 }) 152 }) 153 if err != nil { 154 t.Fatal(err) 155 } 156 157 actualNumberOfDictItems := len(actualDictionaryItems) 158 expectedNumberOfDictItems := len(batchDeleteOperations.Items) 159 if actualNumberOfDictItems != expectedNumberOfDictItems { 160 t.Errorf("Incorrect number of dictionary items returned, expected: %d, got %d", expectedNumberOfDictItems, actualNumberOfDictItems) 161 } 162 } 163 164 func TestClient_BatchModifyDictionaryItems_Update(t *testing.T) { 165 166 fixtureBase := "dictionary_items_batch/update/" 167 nameSuffix := "BatchModifyDictionaryItems_Update" 168 169 // Given: a test service with a dictionary and dictionary items, 170 testService := createTestService(t, fixtureBase+"create_service", nameSuffix) 171 defer deleteTestService(t, fixtureBase+"delete_service", testService.ID) 172 173 testVersion := createTestVersion(t, fixtureBase+"create_version", testService.ID) 174 175 testDictionary := createTestDictionary(t, fixtureBase+"create_dictionary", testService.ID, testVersion.Number, nameSuffix) 176 defer deleteTestDictionary(t, testDictionary, fixtureBase+"delete_dictionary") 177 178 batchCreateOperations := &BatchModifyDictionaryItemsInput{ 179 Service: testService.ID, 180 Dictionary: testDictionary.ID, 181 Items: []*BatchDictionaryItem{ 182 { 183 Operation: CreateBatchOperation, 184 ItemKey: "key1", 185 ItemValue: "val1", 186 }, 187 { 188 Operation: CreateBatchOperation, 189 ItemKey: "key2", 190 ItemValue: "val2", 191 }, 192 }, 193 } 194 195 var err error 196 record(t, fixtureBase+"create_dictionary_items", func(c *Client) { 197 198 err = c.BatchModifyDictionaryItems(batchCreateOperations) 199 }) 200 if err != nil { 201 t.Fatal(err) 202 } 203 204 // When: I execute the batch update operations against the Fastly API, 205 batchUpdateOperations := &BatchModifyDictionaryItemsInput{ 206 Service: testService.ID, 207 Dictionary: testDictionary.ID, 208 Items: []*BatchDictionaryItem{ 209 { 210 Operation: UpdateBatchOperation, 211 ItemKey: "key2", 212 ItemValue: "val2Updated", 213 }, 214 }, 215 } 216 217 record(t, fixtureBase+"update_dictionary_items", func(c *Client) { 218 219 err = c.BatchModifyDictionaryItems(batchUpdateOperations) 220 }) 221 if err != nil { 222 t.Fatal(err) 223 } 224 225 // Then: I expect to be able to list all of the dictionary items with modifications applied to a single item. 226 var actualDictionaryItems []*DictionaryItem 227 record(t, fixtureBase+"list_after_update", func(c *Client) { 228 actualDictionaryItems, err = c.ListDictionaryItems(&ListDictionaryItemsInput{ 229 Service: testService.ID, 230 Dictionary: testDictionary.ID, 231 }) 232 }) 233 if err != nil { 234 t.Fatal(err) 235 } 236 237 actualNumberOfDictItems := len(actualDictionaryItems) 238 expectedNumberOfDictItems := len(batchCreateOperations.Items) 239 if actualNumberOfDictItems != expectedNumberOfDictItems { 240 t.Errorf("Incorrect number of dictionary items returned, expected: %d, got %d", expectedNumberOfDictItems, actualNumberOfDictItems) 241 } 242 243 actualItemKey := actualDictionaryItems[0].ItemKey 244 expectedItemKey := batchCreateOperations.Items[0].ItemKey 245 246 if actualItemKey != expectedItemKey { 247 t.Errorf("First ItemKey did not match, expected %s, got %s", expectedItemKey, actualItemKey) 248 } 249 250 actualItemValue := actualDictionaryItems[0].ItemValue 251 expectedItemValue := batchCreateOperations.Items[0].ItemValue 252 253 // Confirm the second dictionary item contains the modifications. 254 if actualItemValue != expectedItemValue { 255 t.Errorf("First ItemValue did not match, expected %s, got %s", expectedItemValue, actualItemValue) 256 } 257 258 actualItemKey = actualDictionaryItems[1].ItemKey 259 expectedItemKey = batchUpdateOperations.Items[0].ItemKey 260 261 if actualItemKey != expectedItemKey { 262 t.Errorf("Second ItemKey did not match, expected %s, got %s", expectedItemKey, actualItemKey) 263 } 264 265 actualItemValue = actualDictionaryItems[1].ItemValue 266 expectedItemValue = batchUpdateOperations.Items[0].ItemValue 267 268 if actualItemValue != expectedItemValue { 269 t.Errorf("Second ItemValue did not match, expected %s, got %s", expectedItemValue, actualItemValue) 270 } 271 272 } 273 274 func TestClient_BatchModifyDictionaryItems_Upsert(t *testing.T) { 275 276 fixtureBase := "dictionary_items_batch/upsert/" 277 nameSuffix := "BatchModifyDictionaryItems_Upsert" 278 279 // Given: a test service with a dictionary and dictionary items, 280 testService := createTestService(t, fixtureBase+"create_service", nameSuffix) 281 defer deleteTestService(t, fixtureBase+"delete_service", testService.ID) 282 283 testVersion := createTestVersion(t, fixtureBase+"create_version", testService.ID) 284 285 testDictionary := createTestDictionary(t, fixtureBase+"create_dictionary", testService.ID, testVersion.Number, nameSuffix) 286 defer deleteTestDictionary(t, testDictionary, fixtureBase+"delete_dictionary") 287 288 batchCreateOperations := &BatchModifyDictionaryItemsInput{ 289 Service: testService.ID, 290 Dictionary: testDictionary.ID, 291 Items: []*BatchDictionaryItem{ 292 { 293 Operation: CreateBatchOperation, 294 ItemKey: "key1", 295 ItemValue: "val1", 296 }, 297 }, 298 } 299 300 var err error 301 record(t, fixtureBase+"create_dictionary_items", func(c *Client) { 302 303 err = c.BatchModifyDictionaryItems(batchCreateOperations) 304 }) 305 if err != nil { 306 t.Fatal(err) 307 } 308 309 // When: I execute the batch upsert operations against the Fastly API 310 batchUpsertOperations := &BatchModifyDictionaryItemsInput{ 311 Service: testService.ID, 312 Dictionary: testDictionary.ID, 313 Items: []*BatchDictionaryItem{ 314 { 315 Operation: UpsertBatchOperation, 316 ItemKey: "key1", 317 ItemValue: "val1Updated", 318 }, 319 { 320 Operation: UpsertBatchOperation, 321 ItemKey: "key2", 322 ItemValue: "val2", 323 }, 324 }, 325 } 326 327 record(t, fixtureBase+"upsert_dictionary_items", func(c *Client) { 328 329 err = c.BatchModifyDictionaryItems(batchUpsertOperations) 330 }) 331 if err != nil { 332 t.Fatal(err) 333 } 334 335 // Then: I expect to be able to list all of the dictionary items with the modification present. 336 var actualDictionaryItems []*DictionaryItem 337 record(t, fixtureBase+"list_after_upsert", func(c *Client) { 338 actualDictionaryItems, err = c.ListDictionaryItems(&ListDictionaryItemsInput{ 339 Service: testService.ID, 340 Dictionary: testDictionary.ID, 341 }) 342 }) 343 if err != nil { 344 t.Fatal(err) 345 } 346 347 actualNumberOfDictItems := len(actualDictionaryItems) 348 expectedNumberOfDictItems := len(batchUpsertOperations.Items) 349 if actualNumberOfDictItems != expectedNumberOfDictItems { 350 t.Errorf("Incorrect number of dictionary items returned, expected: %d, got %d", expectedNumberOfDictItems, actualNumberOfDictItems) 351 } 352 353 for i, item := range actualDictionaryItems { 354 355 actualItemKey := item.ItemKey 356 expectedItemKey := batchUpsertOperations.Items[i].ItemKey 357 if actualItemKey != expectedItemKey { 358 t.Errorf("First ItemKey did not match, expected %s, got %s", expectedItemKey, actualItemKey) 359 } 360 361 actualItemValue := item.ItemValue 362 expectedItemValue := batchUpsertOperations.Items[i].ItemValue 363 if actualItemValue != expectedItemValue { 364 t.Errorf("First ItemValue did not match, expected %s, got %s", expectedItemValue, actualItemValue) 365 } 366 367 } 368 369 }