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

feat: add deleteitem function to write interface #16

Merged
merged 11 commits into from
Sep 10, 2024
12 changes: 6 additions & 6 deletions delete_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ import (
* @param sk the sort key of the record
* @return true if the record was deleted, false otherwise
*/
func (t *Client) DeleteItem(ctx context.Context, pk string, sk string) error {
func (t *Client) DeleteItem(ctx context.Context, pk Attribute, sk Attribute) error {

//delete item from dynamodb
vankongv marked this conversation as resolved.
Show resolved Hide resolved
input := &dynamodb.DeleteItemInput{
TableName: &t.TableName,
Key: map[string]types.AttributeValue{
"pk": &types.AttributeValueMemberS{Value: pk},
"sk": &types.AttributeValueMemberS{Value: sk},
"pk": pk,
"sk": sk,
},
}
resp, err := t.client.DeleteItem(ctx, input)
_, err := t.client.DeleteItem(ctx, input)

vankongv marked this conversation as resolved.
Show resolved Hide resolved
if err != nil && resp == nil {
log.Println("failed to delete record into database. Error:" + err.Error())
if err != nil {
log.Println("failed to delete record from database. Error:" + err.Error())
return err
}

Expand Down
1 change: 1 addition & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type WriteAPI interface {
// Create or update given item in DynamoDB. Must implemenmt DynamoRecord interface.
// DynamoRecord.GetKeys will be called to get values for parition and sort keys.
PutItem(ctx context.Context, pk Attribute, sk Attribute, item interface{}) error
DeleteItem(ctx context.Context, pk Attribute, sk Attribute) error
}

type TransactionAPI interface {
Expand Down
83 changes: 83 additions & 0 deletions tests/delete_item_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package tests
vankongv marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"
"testing"

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

type user struct {
Pk string `dynamodbav:"pk"`
Sk string `dynamodbav:"sk"`
}

func TestDeleteItem(t *testing.T) {
table := prepareTable(t, dynamoEndpoint, "delete_test")

ctx := context.TODO()

items := []user{
{Pk: "users#org1", Sk: "user#1"},
{Pk: "users#org1", Sk: "user#2"},
{Pk: "users#org1", Sk: "user#3"},
}

for _, item := range items {
err := table.PutItem(ctx, dynago.StringValue(item.Pk), dynago.StringValue(item.Sk), item)
if err != nil {
t.Fatalf("failed to insert item %v; got %s", item, err)
}
}

//query all items to check they exist
for _, item := range items {
var output []user
_, err := table.Query(ctx, "pk = :pk and sk = :sk", map[string]types.AttributeValue{
vankongv marked this conversation as resolved.
Show resolved Hide resolved
":pk": &types.AttributeValueMemberS{Value: item.Pk},
":sk": &types.AttributeValueMemberS{Value: item.Sk},
}, &output)
if err != nil {
t.Fatalf("expected query to succeed for item %v; got %s", item, err)
}
if len(output) == 0 {
t.Fatalf("expected item %v to be found, but found none", item)
}
}

// delete 1st item
err := table.DeleteItem(ctx, dynago.StringValue(items[0].Pk), dynago.StringValue(items[0].Sk))
if err != nil {
t.Fatalf("expected delete to succeed for item %v; got %s", items[0], err)
}

// query first item to confirm it is deleted
var deleteOutput []user
_, err = table.Query(ctx, "pk = :pk and sk = :sk", map[string]types.AttributeValue{
":pk": &types.AttributeValueMemberS{Value: items[0].Pk},
":sk": &types.AttributeValueMemberS{Value: items[0].Sk},
}, &deleteOutput)

if err != nil {
t.Fatalf("expected query to succeed for item %v after deletion; got %s", items[0], err)
}
if len(deleteOutput) != 0 {
t.Fatalf("expected item %v to be deleted; found %v", items[0], deleteOutput)
}

// query other items to confirm they still exist
for i := 1; i < len(items); i++ {
var output []user
_, err := table.Query(ctx, "pk = :pk and sk = :sk", map[string]types.AttributeValue{
":pk": &types.AttributeValueMemberS{Value: items[i].Pk},
":sk": &types.AttributeValueMemberS{Value: items[i].Sk},
}, &output)
if err != nil {
t.Fatalf("expected query to succeed for item %v; got %s", items[i], err)
}
if len(output) == 0 {
t.Fatalf("expected item %v to be found, but found none", items[i])
}
}
}
Loading