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
76 changes: 76 additions & 0 deletions tests/delete_item_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package tests
vankongv marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"
"testing"

"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, found := table.GetItem(ctx, dynago.StringValue(item.Pk), dynago.StringValue(item.Sk), &output)

if err != nil {
t.Fatalf("expected query to succeed for item %v; got %s", item, err)
}
if found == false {
t.Fatalf("expected item %v to be found; got none", item)
}
}

// delete 1st item
err := table.DeleteItem(ctx, dynago.StringValue(items[0].Pk), dynago.StringValue(items[0].Sk))

if err != nil {
t.Fatalf("failed to delete item %v; got %s", items[0], err)
}

// query first item to confirm it is deleted
var deletedItem user
err, found := table.GetItem(ctx, dynago.StringValue(items[0].Pk), dynago.StringValue(items[0].Sk), &deletedItem)

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

// query other items to confirm they still exist
for i := 1; i < len(items); i++ {
var output user
err, found := table.GetItem(ctx, dynago.StringValue(items[i].Pk), dynago.StringValue(items[i].Sk), &output)

if err != nil {
t.Fatalf("expected query to succeed for item %v; got %s", items[i], err)
}
if found == false {
t.Fatalf("expected item %v to be found; got none", items[i])
}
}
}
Loading