From 512b0f192fab40b7ee5385ad29086c08dc91642c Mon Sep 17 00:00:00 2001 From: vankongv Date: Tue, 10 Sep 2024 15:42:26 +1000 Subject: [PATCH] chore: cleanup, update documentation --- README.md | 2 +- tests/delete_item_test.go | 98 ++++++++++------------ tests/{seedTable.go => seed_table_test.go} | 21 +---- tests/transact_items_test.go | 3 +- 4 files changed, 50 insertions(+), 74 deletions(-) rename tests/{seedTable.go => seed_table_test.go} (55%) diff --git a/README.md b/README.md index 539f6e9..8cea3b3 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ table, err := dynago.NewClient(ctx, dynago.ClientOptions{ ### Run dynago.locally ```sh -docker run -p docker run -p 8000:8000 amazon/dynago.local +docker run -p 8000:8000 amazon/dynamodb-local ``` ```go diff --git a/tests/delete_item_test.go b/tests/delete_item_test.go index a5e7345..74cfad3 100644 --- a/tests/delete_item_test.go +++ b/tests/delete_item_test.go @@ -16,27 +16,15 @@ type user struct { } type userKeys struct { - pk string - sk string + Pk string + Sk string } -func getSeedData(ctx context.Context, table *dynago.Client, seedData []tableRecord) ([]tableRecord, error) { - itemsToGet := make([]map[string]types.AttributeValue, 0, len(seedData)) - for _, v := range seedData { - itemsToGet = append(itemsToGet, map[string]types.AttributeValue{ - "pk": dynago.StringValue(v.Pk), - "sk": dynago.StringValue(v.Sk), - }) - } - - var out []tableRecord - - err := table.BatchGetItems(ctx, itemsToGet, &out) - if err != nil { - return nil, fmt.Errorf("Unable to get seed data: %w", err) - } - - return out, nil +type testCase struct { + title string + itemsToDelete []userKeys + itemsLeft int + seedData []tableRecord } func TestDeleteItem(t *testing.T) { @@ -78,43 +66,36 @@ func TestDeleteItem(t *testing.T) { }, } - type testCase struct { - title string - itemsToDelete []userKeys - itemsLeft int - seedData []tableRecord - } - cases := []testCase{ { title: "Delete User 2", itemsToDelete: []userKeys{ { - pk: "users#org1", - sk: "user#2", + Pk: "users#org1", + Sk: "user#2", }, }, itemsLeft: 3, - seedData: records, + seedData: records, //each test case will have the same seed data }, { title: "Delete all users", itemsToDelete: []userKeys{ { - pk: "users#org1", - sk: "user#1", + Pk: "users#org1", + Sk: "user#1", }, { - pk: "users#org1", - sk: "user#2", + Pk: "users#org1", + Sk: "user#2", }, { - pk: "users#org1", - sk: "user#3", + Pk: "users#org1", + Sk: "user#3", }, { - pk: "users#org2", - sk: "user#4", + Pk: "users#org2", + Sk: "user#4", }, }, itemsLeft: 0, @@ -124,8 +105,8 @@ func TestDeleteItem(t *testing.T) { title: "Delete wrong user", itemsToDelete: []userKeys{ { - pk: "users#org1", - sk: "user#none", + Pk: "users#org1", + Sk: "user#none", }, }, itemsLeft: 4, @@ -135,8 +116,8 @@ func TestDeleteItem(t *testing.T) { title: "Delete user with invalid keys", itemsToDelete: []userKeys{ { - pk: "invalid#org", - sk: "invalid#user", + Pk: "invalid#org", + Sk: "invalid#user", }, }, itemsLeft: 4, @@ -146,39 +127,52 @@ func TestDeleteItem(t *testing.T) { for _, c := range cases { t.Run(c.title, func(t *testing.T) { + //prepare records err := seedRecords(ctx, table, c.seedData) if err != nil { t.Fatalf("failed to prepare records; got %s", err) } + //delete records for _, item := range c.itemsToDelete { - err = table.DeleteItem(ctx, dynago.StringValue(item.pk), dynago.StringValue(item.sk)) + err = table.DeleteItem(ctx, dynago.StringValue(item.Pk), dynago.StringValue(item.Sk)) if err != nil { t.Fatalf("unable to delete records; got %s", err) } } - data, err := getSeedData(ctx, table, c.seedData) + //get original seed records + itemsToGet := make([]map[string]types.AttributeValue, 0, len(c.seedData)) + for _, v := range c.seedData { + itemsToGet = append(itemsToGet, map[string]types.AttributeValue{ + "pk": dynago.StringValue(v.Pk), + "sk": dynago.StringValue(v.Sk), + }) + } + + var out []tableRecord + + err = table.BatchGetItems(ctx, itemsToGet, &out) if err != nil { - t.Fatalf("failed to prepare records; got %s", err) + t.Fatalf("Unable to get seed data: %s", err) } - // see if "itemsToDelete" are the ones deleted. - dataKeys := make([]string, 0, len(data)) - for _, item := range data { + //check if deleted items are not in db + dataKeys := make([]string, 0, len(out)) + for _, item := range out { dataKeys = append(dataKeys, fmt.Sprintf("%s--%s", item.Pk, item.Sk)) } for _, v := range c.itemsToDelete { - recKey := fmt.Sprintf("%s--%s", v.pk, v.sk) + recKey := fmt.Sprintf("%s--%s", v.Pk, v.Sk) if slices.Contains(dataKeys, recKey) { - t.Fatalf("expected items to deleted in db but found it, pk %s; sk %s", v.pk, v.sk) + t.Fatalf("expected items to deleted in db but found it, pk %s; sk %s", v.Pk, v.Sk) } } - // compare items remaining is seed data matches test - if len(data) != c.itemsLeft { - t.Fatalf("expected items in db; %v found; %v", c.itemsLeft, len(data)) + //check if remaining records match expected number of items left + if len(out) != c.itemsLeft { + t.Fatalf("expected items in db; %v found; %v", c.itemsLeft, len(out)) } }) } diff --git a/tests/seedTable.go b/tests/seed_table_test.go similarity index 55% rename from tests/seedTable.go rename to tests/seed_table_test.go index 69d6d45..8b3635c 100644 --- a/tests/seedTable.go +++ b/tests/seed_table_test.go @@ -4,7 +4,6 @@ import ( "context" "fmt" - "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" "github.com/oolio-group/dynago" ) @@ -15,6 +14,7 @@ type tableRecord struct { } func seedRecords(ctx context.Context, table *dynago.Client, input []tableRecord) error { + //insert items items := make([]*dynago.TransactPutItemsInput, 0, len(input)) for _, item := range input { in := dynago.TransactPutItemsInput{ @@ -31,24 +31,5 @@ func seedRecords(ctx context.Context, table *dynago.Client, input []tableRecord) return fmt.Errorf("failed to insert items; got %s", err) } - requests := make([]map[string]types.AttributeValue, 0, len(input)) - for _, item := range input { - req := map[string]types.AttributeValue{ - "pk": dynago.StringValue(item.Pk), - "sk": dynago.StringValue(item.Sk), - } - requests = append(requests, req) - } - - var output []tableRecord - err = table.BatchGetItems(ctx, requests, &output) - if err != nil { - return fmt.Errorf("failed to get items; got %s", err) - } - - if len(input) != len(output) { - return fmt.Errorf("count of items does not match input items; got %s", err) - } - return nil } diff --git a/tests/transact_items_test.go b/tests/transact_items_test.go index 8be681a..21142f7 100644 --- a/tests/transact_items_test.go +++ b/tests/transact_items_test.go @@ -89,7 +89,7 @@ func TestTransactItems(t *testing.T) { items := make([]*dynago.TransactPutItemsInput, 0, len(tc.newItems)) for _, item := range tc.newItems { items = append(items, &dynago.TransactPutItemsInput{ - dynago.StringValue(item.Pk), dynago.StringValue(item.Sk), item, + PartitionKeyValue: dynago.StringValue(item.Pk), SortKeyValue: dynago.StringValue(item.Sk), Item: item, }) } err := table.TransactPutItems(ctx, items) @@ -98,6 +98,7 @@ func TestTransactItems(t *testing.T) { } } //perform operations + if len(tc.operations) > 0 { err := table.TransactItems(ctx, tc.operations...) if err != nil {