Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement batch operations for dictionary items #125

Merged
merged 10 commits into from
Jul 22, 2019
Prev Previous commit
Next Next commit
Updates the unit tests work independently.
  • Loading branch information
Trent Rosenbaum committed Jul 16, 2019
commit 6719ab5f94acd01a4ff649e218cbb9f809b42ac2
20 changes: 12 additions & 8 deletions fastly/dictionary_item_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
func TestClient_BatchModifyDictionaryItems_Create(t *testing.T) {

fixtureBase := "dictionary_items_batch/create/"
nameSuffix := "BatchModifyDictionaryItems_Create"

// Given: a test service with a dictionary and a batch of create operations,
testService := createTestService(t, fixtureBase + "create_service")
testService := createTestService(t, fixtureBase + "create_service", nameSuffix)
defer deleteTestService(t, fixtureBase +"delete_service", testService.ID)

testVersion := createTestVersion(t,fixtureBase + "create_version", testService.ID)

testDictionary := createTestDictionary(t, fixtureBase + "create_dictionary", testService.ID, testVersion.Number)
testDictionary := createTestDictionary(t, fixtureBase + "create_dictionary", testService.ID, testVersion.Number, nameSuffix)
defer deleteTestDictionary(t, testDictionary, fixtureBase + "delete_dictionary")

batchCreateOperations := &BatchModifyDictionaryItemsInput {
Expand Down Expand Up @@ -85,14 +86,15 @@ func TestClient_BatchModifyDictionaryItems_Create(t *testing.T) {
func TestClient_BatchModifyDictionaryItems_Delete(t *testing.T) {

fixtureBase := "dictionary_items_batch/delete/"
nameSuffix := "BatchModifyDictionaryItems_Delete"

// Given: a test service with a dictionary and dictionary items,
testService := createTestService(t, fixtureBase + "create_service")
testService := createTestService(t, fixtureBase + "create_service", nameSuffix)
defer deleteTestService(t, fixtureBase + "delete_service", testService.ID)

testVersion := createTestVersion(t,fixtureBase + "create_version", testService.ID)

testDictionary := createTestDictionary(t, fixtureBase + "create_dictionary", testService.ID, testVersion.Number)
testDictionary := createTestDictionary(t, fixtureBase + "create_dictionary", testService.ID, testVersion.Number, nameSuffix)
defer deleteTestDictionary(t, testDictionary, fixtureBase + "delete_dictionary")

batchCreateOperations := &BatchModifyDictionaryItemsInput {
Expand Down Expand Up @@ -164,14 +166,15 @@ func TestClient_BatchModifyDictionaryItems_Delete(t *testing.T) {
func TestClient_BatchModifyDictionaryItems_Update(t *testing.T) {

fixtureBase := "dictionary_items_batch/update/"
nameSuffix := "BatchModifyDictionaryItems_Update"

// Given: a test service with a dictionary and dictionary items,
testService := createTestService(t, fixtureBase + "create_service")
testService := createTestService(t, fixtureBase + "create_service", nameSuffix)
defer deleteTestService(t, fixtureBase + "delete_service", testService.ID)

testVersion := createTestVersion(t,fixtureBase + "create_version", testService.ID)

testDictionary := createTestDictionary(t, fixtureBase + "create_dictionary", testService.ID, testVersion.Number)
testDictionary := createTestDictionary(t, fixtureBase + "create_dictionary", testService.ID, testVersion.Number, nameSuffix)
defer deleteTestDictionary(t, testDictionary, fixtureBase + "delete_dictionary")

batchCreateOperations := &BatchModifyDictionaryItemsInput {
Expand Down Expand Up @@ -275,14 +278,15 @@ func TestClient_BatchModifyDictionaryItems_Update(t *testing.T) {
func TestClient_BatchModifyDictionaryItems_Upsert(t *testing.T) {

fixtureBase := "dictionary_items_batch/upsert/"
nameSuffix := "BatchModifyDictionaryItems_Upsert"

// Given: a test service with a dictionary and dictionary items,
testService := createTestService(t, fixtureBase + "create_service")
testService := createTestService(t, fixtureBase + "create_service", nameSuffix)
defer deleteTestService(t, fixtureBase + "delete_service", testService.ID)

testVersion := createTestVersion(t,fixtureBase + "create_version", testService.ID)

testDictionary := createTestDictionary(t, fixtureBase + "create_dictionary", testService.ID, testVersion.Number)
testDictionary := createTestDictionary(t, fixtureBase + "create_dictionary", testService.ID, testVersion.Number, nameSuffix)
defer deleteTestDictionary(t, testDictionary, fixtureBase + "delete_dictionary")

batchCreateOperations := &BatchModifyDictionaryItemsInput {
Expand Down
90 changes: 48 additions & 42 deletions fastly/dictionary_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ package fastly

import "testing"


func TestClient_DictionaryItems(t *testing.T) {
tv := createTestVersion(t,"dictionary_items/version", testServiceID)

td := createTestDictionary(t, "dictionary_items/dictionary", testServiceID, tv.Number)
defer deleteTestDictionary(t, td, "dictionary_items/delete_dictionary")
fixtureBase := "dictionary_items/"
nameSuffix := "DictionaryItems"

testService := createTestService(t, fixtureBase + "create_service", nameSuffix)
defer deleteTestService(t, fixtureBase +"delete_service", testService.ID)

testVersion := createTestVersion(t, fixtureBase+"version", testService.ID)

testDictionary := createTestDictionary(t, fixtureBase+"dictionary", testService.ID, testVersion.Number, nameSuffix)
defer deleteTestDictionary(t, testDictionary, fixtureBase+"delete_dictionary")

// Create
var err error
var d *DictionaryItem
record(t, "dictionary_items/create", func(c *Client) {
d, err = c.CreateDictionaryItem(&CreateDictionaryItemInput{
Service: testServiceID,
Dictionary: td.ID,
var createDictionaryItem *DictionaryItem
record(t, fixtureBase+"create", func(c *Client) {
createDictionaryItem, err = c.CreateDictionaryItem(&CreateDictionaryItemInput{
Service: testService.ID,
Dictionary: testDictionary.ID,
ItemKey: "test-dictionary-item",
ItemValue: "value",
})
Expand All @@ -26,78 +32,78 @@ func TestClient_DictionaryItems(t *testing.T) {

// Ensure deleted
defer func() {
record(t, "dictionary_items/cleanup", func(c *Client) {
record(t, fixtureBase+"cleanup", func(c *Client) {
c.DeleteDictionaryItem(&DeleteDictionaryItemInput{
Service: testServiceID,
Dictionary: td.ID,
Service: testService.ID,
Dictionary: testDictionary.ID,
ItemKey: "test-dictionary-item",
})
})
}()

if d.ItemKey != "test-dictionary-item" {
t.Errorf("bad item_key: %q", d.ItemKey)
if createDictionaryItem.ItemKey != "test-dictionary-item" {
t.Errorf("bad item_key: %q", createDictionaryItem.ItemKey)
}
if d.ItemValue != "value" {
t.Errorf("bad item_value: %q", d.ItemValue)
if createDictionaryItem.ItemValue != "value" {
t.Errorf("bad item_value: %q", createDictionaryItem.ItemValue)
}

// List
var ds []*DictionaryItem
record(t, "dictionary_items/list", func(c *Client) {
ds, err = c.ListDictionaryItems(&ListDictionaryItemsInput{
Service: testServiceID,
Dictionary: td.ID,
var dictionaryItems []*DictionaryItem
record(t, fixtureBase+"list", func(c *Client) {
dictionaryItems, err = c.ListDictionaryItems(&ListDictionaryItemsInput{
Service: testService.ID,
Dictionary: testDictionary.ID,
})
})
if err != nil {
t.Fatal(err)
}
if len(ds) < 1 {
t.Errorf("bad dictionary items: %v", ds)
if len(dictionaryItems) < 1 {
t.Errorf("bad dictionary items: %v", dictionaryItems)
}

// Get
var nd *DictionaryItem
record(t, "dictionary_items/get", func(c *Client) {
nd, err = c.GetDictionaryItem(&GetDictionaryItemInput{
Service: testServiceID,
Dictionary: td.ID,
var retrievedDictionaryItem *DictionaryItem
record(t, fixtureBase+"get", func(c *Client) {
retrievedDictionaryItem, err = c.GetDictionaryItem(&GetDictionaryItemInput{
Service: testService.ID,
Dictionary: testDictionary.ID,
ItemKey: "test-dictionary-item",
})
})
if err != nil {
t.Fatal(err)
}
if nd.ItemKey != "test-dictionary-item" {
t.Errorf("bad item_key: %q", nd.ItemKey)
if retrievedDictionaryItem.ItemKey != "test-dictionary-item" {
t.Errorf("bad item_key: %q", retrievedDictionaryItem.ItemKey)
}
if nd.ItemValue != "value" {
t.Errorf("bad item_value: %q", nd.ItemValue)
if retrievedDictionaryItem.ItemValue != "value" {
t.Errorf("bad item_value: %q", retrievedDictionaryItem.ItemValue)
}

// Update
var ud *DictionaryItem
record(t, "dictionary_items/update", func(c *Client) {
ud, err = c.UpdateDictionaryItem(&UpdateDictionaryItemInput{
Service: testServiceID,
Dictionary: td.ID,
var updatedDictionaryItem *DictionaryItem
record(t, fixtureBase+"update", func(c *Client) {
updatedDictionaryItem, err = c.UpdateDictionaryItem(&UpdateDictionaryItemInput{
Service: testService.ID,
Dictionary: testDictionary.ID,
ItemKey: "test-dictionary-item",
ItemValue: "new-value",
})
})
if err != nil {
t.Fatal(err)
}
if ud.ItemValue != "new-value" {
t.Errorf("bad item_value: %q", ud.ItemValue)
if updatedDictionaryItem.ItemValue != "new-value" {
t.Errorf("bad item_value: %q", updatedDictionaryItem.ItemValue)
}

// Delete
record(t, "dictionary_items/delete", func(c *Client) {
record(t, fixtureBase+"delete", func(c *Client) {
err = c.DeleteDictionaryItem(&DeleteDictionaryItemInput{
Service: testServiceID,
Dictionary: td.ID,
Service: testService.ID,
Dictionary: testDictionary.ID,
ItemKey: "test-dictionary-item",
})
})
Expand Down
35 changes: 17 additions & 18 deletions fastly/dictionary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import "testing"
func TestClient_Dictionaries(t *testing.T) {
t.Parallel()

var err error
var tv *Version
record(t, "dictionaries/version", func(c *Client) {
tv = testVersion(t, c)
})
fixtureBase := "dictionaries/"

testVersion := createTestVersion(t, fixtureBase+"version", testServiceID)

// Create
var err error
var d *Dictionary
record(t, "dictionaries/create", func(c *Client) {
record(t, fixtureBase+"create", func(c *Client) {
d, err = c.CreateDictionary(&CreateDictionaryInput{
Service: testServiceID,
Version: tv.Number,
Version: testVersion.Number,
Name: "test_dictionary",
})
})
Expand All @@ -26,16 +25,16 @@ func TestClient_Dictionaries(t *testing.T) {

// Ensure deleted
defer func() {
record(t, "dictionaries/cleanup", func(c *Client) {
record(t, fixtureBase+"cleanup", func(c *Client) {
c.DeleteDictionary(&DeleteDictionaryInput{
Service: testServiceID,
Version: tv.Number,
Version: testVersion.Number,
Name: "test_dictionary",
})

c.DeleteDictionary(&DeleteDictionaryInput{
Service: testServiceID,
Version: tv.Number,
Version: testVersion.Number,
Name: "new_test_dictionary",
})
})
Expand All @@ -47,10 +46,10 @@ func TestClient_Dictionaries(t *testing.T) {

// List
var ds []*Dictionary
record(t, "dictionaries/list", func(c *Client) {
record(t, fixtureBase+"list", func(c *Client) {
ds, err = c.ListDictionaries(&ListDictionariesInput{
Service: testServiceID,
Version: tv.Number,
Version: testVersion.Number,
})
})
if err != nil {
Expand All @@ -62,10 +61,10 @@ func TestClient_Dictionaries(t *testing.T) {

// Get
var nd *Dictionary
record(t, "dictionaries/get", func(c *Client) {
record(t, fixtureBase+"get", func(c *Client) {
nd, err = c.GetDictionary(&GetDictionaryInput{
Service: testServiceID,
Version: tv.Number,
Version: testVersion.Number,
Name: "test_dictionary",
})
})
Expand All @@ -78,10 +77,10 @@ func TestClient_Dictionaries(t *testing.T) {

// Update
var ud *Dictionary
record(t, "dictionaries/update", func(c *Client) {
record(t, fixtureBase+"update", func(c *Client) {
ud, err = c.UpdateDictionary(&UpdateDictionaryInput{
Service: testServiceID,
Version: tv.Number,
Version: testVersion.Number,
Name: "test_dictionary",
NewName: "new_test_dictionary",
})
Expand All @@ -94,10 +93,10 @@ func TestClient_Dictionaries(t *testing.T) {
}

// Delete
record(t, "dictionaries/delete", func(c *Client) {
record(t, fixtureBase+"delete", func(c *Client) {
err = c.DeleteDictionary(&DeleteDictionaryInput{
Service: testServiceID,
Version: tv.Number,
Version: testVersion.Number,
Name: "new_test_dictionary",
})
})
Expand Down
12 changes: 5 additions & 7 deletions fastly/fastly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package fastly

import (
"fmt"
"github.com/dnaeon/go-vcr/recorder"
"sync"
"testing"
"time"

"github.com/dnaeon/go-vcr/recorder"
)

// testClient is the test client.
Expand Down Expand Up @@ -56,14 +54,14 @@ func recordRealtimeStats(t *testing.T, fixture string, f func(*RTSClient)) {
f(client)
}

func createTestService(t *testing.T, serviceFixture string) *Service {
func createTestService(t *testing.T, serviceFixture string, serviceNameSuffix string) *Service {

var err error
var service *Service

record(t, serviceFixture, func(client *Client) {
service, err = client.CreateService(&CreateServiceInput{
Name: fmt.Sprintf("test_service_%d", time.Now().Unix()),
Name: fmt.Sprintf("test_service_%s", serviceNameSuffix),
Comment: "go-fastly client test",
})
})
Expand Down Expand Up @@ -108,7 +106,7 @@ func createTestVersion(t *testing.T, versionFixture string, serviceId string) *V
return version
}

func createTestDictionary(t *testing.T, dictionaryFixture string, serviceId string, version int) *Dictionary {
func createTestDictionary(t *testing.T, dictionaryFixture string, serviceId string, version int, dictionaryNameSuffix string) *Dictionary {

var err error
var dictionary *Dictionary
Expand All @@ -117,7 +115,7 @@ func createTestDictionary(t *testing.T, dictionaryFixture string, serviceId stri
dictionary, err = client.CreateDictionary(&CreateDictionaryInput{
Service: serviceId,
Version: version,
Name: fmt.Sprintf("test_dictionary_%d", time.Now().Unix()),
Name: fmt.Sprintf("test_dictionary_%s", dictionaryNameSuffix),
})
})
if err != nil {
Expand Down
Loading