-
Notifications
You must be signed in to change notification settings - Fork 2
feat: implement UpdateItem operation and support for TransactWriteItems #23
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
Open
devin-ai-integration
wants to merge
15
commits into
main
Choose a base branch
from
devin/1746585260-implement-updateitem
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a148f37
Implement UpdateItem operation and support for TransactWriteItems (fi…
devin-ai-integration[bot] 289900f
feat: implement UpdateItem operation and support for TransactWriteItems
devin-ai-integration[bot] db01e31
fix: limit retries in optimistic lock concurrency test
devin-ai-integration[bot] 2eda1c2
fix: skip table creation in CI environment
devin-ai-integration[bot] 9a84415
fix: revert CI skip for TestNewClientLocalEndpoint
devin-ai-integration[bot] 8117b1e
fix: add retry logic for table creation in TestNewClientLocalEndpoint
devin-ai-integration[bot] cd7f6fe
fix: remove silent error handling in WithUpdateItem
devin-ai-integration[bot] 521045b
fix: implement panic for option errors in WithUpdateItem
devin-ai-integration[bot] 8fa755d
refactor: move TestTransactWithUpdateItem to existing table-driven te…
devin-ai-integration[bot] 680e0e4
fix: remove unused import in updateitem_test.go
devin-ai-integration[bot] 9227c78
fix: improve optimistic lock concurrency test with better retry handling
devin-ai-integration[bot] 2f00d0f
fix: replace IN operator with OR in query condition
devin-ai-integration[bot] dcdf70c
fix: update test to use proper DynamoDB query pattern
devin-ai-integration[bot] d4fe429
fix: increase retry count and delay in optimistic lock test
devin-ai-integration[bot] 1b5063a
Merge branch 'main' into devin/1746585260-implement-updateitem
shidil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,269 @@ | ||
| package tests | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "reflect" | ||
| "strings" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" | ||
| "github.com/oolio-group/dynago" | ||
| ) | ||
|
|
||
| type Account struct { | ||
| ID string | ||
| Balance int | ||
| Version uint | ||
| Status string | ||
|
|
||
| Pk string | ||
| Sk string | ||
| } | ||
|
|
||
| func TestUpdateItem(t *testing.T) { | ||
| table := prepareTable(t, dynamoEndpoint, "update_test") | ||
| testCases := []struct { | ||
| title string | ||
| item Account | ||
| updates map[string]dynago.Attribute | ||
| options []dynago.UpdateOption | ||
| expected Account | ||
| expectedErr error | ||
| }{ | ||
| { | ||
| title: "update fields success", | ||
| item: Account{ | ||
| ID: "1", | ||
| Balance: 100, | ||
| Status: "active", | ||
| Pk: "account_1", | ||
| Sk: "account_1", | ||
| }, | ||
| updates: map[string]dynago.Attribute{ | ||
| "Balance": dynago.NumberValue(200), | ||
| "Status": dynago.StringValue("inactive"), | ||
| }, | ||
| options: []dynago.UpdateOption{}, | ||
| expected: Account{ | ||
| ID: "1", | ||
| Balance: 200, | ||
| Status: "inactive", | ||
| Pk: "account_1", | ||
| Sk: "account_1", | ||
| }, | ||
| }, | ||
| { | ||
| title: "optimistic lock success", | ||
| item: Account{ | ||
| ID: "2", | ||
| Balance: 100, | ||
| Version: 1, | ||
| Pk: "account_2", | ||
| Sk: "account_2", | ||
| }, | ||
| updates: map[string]dynago.Attribute{ | ||
| "Balance": dynago.NumberValue(300), | ||
| }, | ||
| options: []dynago.UpdateOption{ | ||
| dynago.WithOptimisticLockForUpdate("Version", 1), | ||
| }, | ||
| expected: Account{ | ||
| ID: "2", | ||
| Balance: 300, | ||
| Version: 2, | ||
| Pk: "account_2", | ||
| Sk: "account_2", | ||
| }, | ||
| }, | ||
| { | ||
| title: "conditional update success", | ||
| item: Account{ | ||
| ID: "3", | ||
| Balance: 100, | ||
| Status: "active", | ||
| Pk: "account_3", | ||
| Sk: "account_3", | ||
| }, | ||
| updates: map[string]dynago.Attribute{ | ||
| "Status": dynago.StringValue("inactive"), | ||
| }, | ||
| options: []dynago.UpdateOption{ | ||
| dynago.WithConditionalUpdate("attribute_exists(Balance)"), | ||
| }, | ||
| expected: Account{ | ||
| ID: "3", | ||
| Balance: 100, | ||
| Status: "inactive", | ||
| Pk: "account_3", | ||
| Sk: "account_3", | ||
| }, | ||
| }, | ||
| { | ||
| title: "conditional update failure", | ||
| item: Account{ | ||
| ID: "4", | ||
| Balance: 100, | ||
| Pk: "account_4", | ||
| Sk: "account_4", | ||
| }, | ||
| updates: map[string]dynago.Attribute{ | ||
| "Status": dynago.StringValue("inactive"), | ||
| }, | ||
| options: []dynago.UpdateOption{ | ||
| dynago.WithConditionalUpdate("attribute_exists(NonExistentField)"), | ||
| }, | ||
| expectedErr: fmt.Errorf("ConditionalCheckFailedException"), | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.title, func(t *testing.T) { | ||
| t.Helper() | ||
| ctx := context.TODO() | ||
|
|
||
| pk := dynago.StringValue(tc.item.Pk) | ||
| sk := dynago.StringValue(tc.item.Sk) | ||
| err := table.PutItem(ctx, pk, sk, &tc.item) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error on initial put: %s", err) | ||
| } | ||
|
|
||
| err = table.UpdateItem(ctx, pk, sk, tc.updates, tc.options...) | ||
| if err != nil { | ||
| if tc.expectedErr == nil { | ||
| t.Fatalf("unexpected error: %s", err) | ||
| } | ||
| if !strings.Contains(err.Error(), tc.expectedErr.Error()) { | ||
| t.Fatalf("expected op to fail with %s; got %s", tc.expectedErr, err) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| var out Account | ||
| err, found := table.GetItem(ctx, pk, sk, &out) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error on get: %s", err) | ||
| } | ||
| if !found { | ||
| t.Errorf("expected to find item with pk %s and sk %s", tc.item.Pk, tc.item.Sk) | ||
| } | ||
| if !reflect.DeepEqual(tc.expected, out) { | ||
| t.Errorf("expected query to return %v; got %v", tc.expected, out) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestUpdateItemOptimisticLockConcurrency(t *testing.T) { | ||
| table := prepareTable(t, dynamoEndpoint, "update_optimistic_test") | ||
| account := Account{ID: "123", Balance: 0, Version: 0, Pk: "123", Sk: "123"} | ||
| ctx := context.Background() | ||
| pk := dynago.StringValue("123") | ||
| err := table.PutItem(ctx, pk, pk, account) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error %s", err) | ||
| return | ||
| } | ||
|
|
||
| update := func() error { | ||
| var acc Account | ||
| err, _ := table.GetItem(ctx, pk, pk, &acc) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| updates := map[string]dynago.Attribute{ | ||
| "Balance": dynago.NumberValue(int64(acc.Balance + 100)), | ||
| } | ||
|
|
||
| return table.UpdateItem(ctx, pk, pk, updates, dynago.WithOptimisticLockForUpdate("Version", acc.Version)) | ||
| } | ||
|
|
||
| var wg sync.WaitGroup | ||
| for range 10 { | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| maxRetries := 10 | ||
| for i := 0; i < maxRetries; i++ { | ||
| err := update() | ||
| if err == nil { | ||
| return | ||
| } | ||
| if i%3 == 0 { | ||
| t.Logf("Retry %d: %v", i, err) | ||
| } | ||
| } | ||
| t.Logf("Max retries reached, continuing") | ||
| }() | ||
| } | ||
| wg.Wait() | ||
|
|
||
| var acc Account | ||
| err, _ = table.GetItem(ctx, pk, pk, &acc) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error %s", err) | ||
| return | ||
| } | ||
| if acc.Balance != 1000 { | ||
| t.Errorf("expected account balance to be 1000 after 10 increments of 100; got %d", acc.Balance) | ||
| } | ||
| if acc.Version != 10 { | ||
| t.Errorf("expected account version to be 10 after 10 updates; got %d", acc.Version) | ||
| } | ||
| } | ||
|
|
||
| func TestTransactWithUpdateItem(t *testing.T) { | ||
shidil marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| table := prepareTable(t, dynamoEndpoint, "transact_update_test") | ||
|
|
||
| ctx := context.TODO() | ||
| account1 := Account{ID: "101", Balance: 100, Status: "active", Pk: "account_101", Sk: "account_101"} | ||
| account2 := Account{ID: "102", Balance: 200, Status: "active", Pk: "account_102", Sk: "account_102"} | ||
|
|
||
| items := []*dynago.TransactPutItemsInput{ | ||
| {dynago.StringValue(account1.Pk), dynago.StringValue(account1.Sk), account1}, | ||
| {dynago.StringValue(account2.Pk), dynago.StringValue(account2.Sk), account2}, | ||
| } | ||
|
|
||
| err := table.TransactPutItems(ctx, items) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error on initial put: %s", err) | ||
| } | ||
|
|
||
| operations := []types.TransactWriteItem{ | ||
| table.WithUpdateItem("account_101", "account_101", map[string]dynago.Attribute{ | ||
| "Balance": dynago.NumberValue(150), | ||
| "Status": dynago.StringValue("pending"), | ||
| }), | ||
| table.WithUpdateItem("account_102", "account_102", map[string]dynago.Attribute{ | ||
| "Balance": dynago.NumberValue(250), | ||
| "Status": dynago.StringValue("pending"), | ||
| }), | ||
| } | ||
|
|
||
| err = table.TransactItems(ctx, operations...) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error on transaction: %s", err) | ||
| } | ||
|
|
||
| var acc1, acc2 Account | ||
| err, _ = table.GetItem(ctx, dynago.StringValue("account_101"), dynago.StringValue("account_101"), &acc1) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error on get: %s", err) | ||
| } | ||
|
|
||
| err, _ = table.GetItem(ctx, dynago.StringValue("account_102"), dynago.StringValue("account_102"), &acc2) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error on get: %s", err) | ||
| } | ||
|
|
||
| if acc1.Balance != 150 || acc1.Status != "pending" { | ||
| t.Errorf("account1 not updated correctly: %+v", acc1) | ||
| } | ||
|
|
||
| if acc2.Balance != 250 || acc2.Status != "pending" { | ||
| t.Errorf("account2 not updated correctly: %+v", acc2) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.