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