Skip to content

Commit

Permalink
chore: cleanup, update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
vankongv committed Sep 10, 2024
1 parent 49b9295 commit 512b0f1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 74 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
98 changes: 46 additions & 52 deletions tests/delete_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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))
}
})
}
Expand Down
21 changes: 1 addition & 20 deletions tests/seedTable.go → tests/seed_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"

"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/oolio-group/dynago"
)

Expand All @@ -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{
Expand All @@ -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
}
3 changes: 2 additions & 1 deletion tests/transact_items_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit 512b0f1

Please sign in to comment.