|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "reflect" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" |
| 10 | + "github.com/oolio-group/dynago" |
| 11 | +) |
| 12 | + |
| 13 | +type Terminal struct { |
| 14 | + Id string |
| 15 | + Pk string |
| 16 | + Sk string |
| 17 | +} |
| 18 | + |
| 19 | +func TestTransactItems(t *testing.T) { |
| 20 | + endoint, purge := startLocalDatabase(t) |
| 21 | + defer purge() |
| 22 | + |
| 23 | + table := prepareTable(t, endoint, "transcact_item_test") |
| 24 | + |
| 25 | + testCases := []struct { |
| 26 | + title string |
| 27 | + condition string |
| 28 | + keys map[string]types.AttributeValue |
| 29 | + opts []dynago.QueryOptions |
| 30 | + //items to be added |
| 31 | + newItems []Terminal |
| 32 | + operations []types.TransactWriteItem |
| 33 | + //items expected to exist in table after transaction operation |
| 34 | + expected []Terminal |
| 35 | + expectedErr error |
| 36 | + }{{ |
| 37 | + title: "assign terminal - only add a terminal", |
| 38 | + condition: "pk = :pk", |
| 39 | + keys: map[string]types.AttributeValue{ |
| 40 | + ":pk": &types.AttributeValueMemberS{Value: "terminal1"}, |
| 41 | + }, |
| 42 | + newItems: []Terminal{}, |
| 43 | + operations: []types.TransactWriteItem{ |
| 44 | + table.WithPutItem("terminal1", "merchant1", Terminal{ |
| 45 | + Id: "1", |
| 46 | + Pk: "terminal1", |
| 47 | + Sk: "merchant1", |
| 48 | + }), |
| 49 | + }, |
| 50 | + expected: []Terminal{ |
| 51 | + { |
| 52 | + Id: "1", |
| 53 | + Pk: "terminal1", |
| 54 | + Sk: "merchant1", |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + { |
| 59 | + title: "assign terminal - delete existing and update with new", |
| 60 | + condition: "pk = :pk", |
| 61 | + keys: map[string]types.AttributeValue{ |
| 62 | + ":pk": &types.AttributeValueMemberS{Value: "terminal1"}, |
| 63 | + }, |
| 64 | + newItems: []Terminal{{ |
| 65 | + Id: "1", |
| 66 | + Pk: "terminal1", |
| 67 | + Sk: "merchant2", |
| 68 | + }}, |
| 69 | + operations: []types.TransactWriteItem{ |
| 70 | + table.WithDeleteItem("terminal1", "merchant1"), |
| 71 | + table.WithPutItem("terminal1", "merchant2", Terminal{ |
| 72 | + Id: "1", |
| 73 | + Pk: "terminal1", |
| 74 | + Sk: "merchant2", |
| 75 | + }), |
| 76 | + }, |
| 77 | + expected: []Terminal{ |
| 78 | + { |
| 79 | + Id: "1", |
| 80 | + Pk: "terminal1", |
| 81 | + Sk: "merchant2", |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + } |
| 86 | + for _, tc := range testCases { |
| 87 | + t.Run(tc.title, func(t *testing.T) { |
| 88 | + t.Helper() |
| 89 | + ctx := context.TODO() |
| 90 | + // Create Item |
| 91 | + if len(tc.newItems) > 0 { |
| 92 | + items := make([]*dynago.TransactPutItemsInput, 0, len(tc.newItems)) |
| 93 | + for _, item := range tc.newItems { |
| 94 | + items = append(items, &dynago.TransactPutItemsInput{ |
| 95 | + dynago.StringValue(item.Pk), dynago.StringValue(item.Sk), item, |
| 96 | + }) |
| 97 | + } |
| 98 | + err := table.TransactPutItems(ctx, items) |
| 99 | + if err != nil { |
| 100 | + t.Fatalf("transaction put items failed; got %s", err) |
| 101 | + } |
| 102 | + } |
| 103 | + //perform operations |
| 104 | + if len(tc.operations) > 0 { |
| 105 | + err := table.TransactItems(ctx, tc.operations) |
| 106 | + if err != nil { |
| 107 | + t.Fatalf("error occurred %s", err) |
| 108 | + } |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + var out []Terminal |
| 113 | + _, err := table.Query(ctx, tc.condition, tc.keys, &out) |
| 114 | + if tc.expectedErr != nil { |
| 115 | + if err == nil { |
| 116 | + t.Fatalf("expected query to fail with %s", tc.expectedErr) |
| 117 | + } |
| 118 | + if !strings.Contains(err.Error(), tc.expectedErr.Error()) { |
| 119 | + t.Fatalf("expected query to fail with %s; got %s", tc.expectedErr, err) |
| 120 | + } |
| 121 | + return |
| 122 | + } |
| 123 | + if err != nil { |
| 124 | + t.Fatalf("expected query to succeed; got %s", err) |
| 125 | + } |
| 126 | + if !reflect.DeepEqual(tc.expected, out) { |
| 127 | + t.Errorf("expected query to return %v; got %v", tc.expected, out) |
| 128 | + } |
| 129 | + |
| 130 | + }) |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments