|
1 | 1 | package test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "fmt"
|
5 | 6 | "reflect"
|
6 | 7 | "testing"
|
@@ -38,3 +39,89 @@ func TestDatabaseTransaction(t *testing.T) {
|
38 | 39 | })
|
39 | 40 | }
|
40 | 41 | }
|
| 42 | + |
| 43 | +func insertDocument(ctx context.Context, col driver.Collection, t *testing.T) driver.DocumentMeta { |
| 44 | + doc := struct { |
| 45 | + Name string `json:"name,omitempty"` |
| 46 | + }{ |
| 47 | + Name: "Hello World", |
| 48 | + } |
| 49 | + if meta, err := col.CreateDocument(ctx, &doc); err != nil { |
| 50 | + t.Fatalf("Failed to create document: %s", describe(err)) |
| 51 | + } else { |
| 52 | + return meta |
| 53 | + } |
| 54 | + return driver.DocumentMeta{} |
| 55 | +} |
| 56 | + |
| 57 | +func documentExists(ctx context.Context, col driver.Collection, key string, exists bool, t *testing.T) { |
| 58 | + if found, err := col.DocumentExists(ctx, key); err != nil { |
| 59 | + t.Fatalf("DocumentExists failed: %s", describe(err)) |
| 60 | + } else { |
| 61 | + if exists != found { |
| 62 | + t.Errorf("Document status not as expected: expected: %t, actual: %t", exists, found) |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestTransactionCommit(t *testing.T) { |
| 68 | + c := createClientFromEnv(t, true) |
| 69 | + skipBelowVersion(c, "3.5", t) |
| 70 | + colname := "trx_test_col" |
| 71 | + ctx := context.Background() |
| 72 | + db := ensureDatabase(ctx, c, "trx_test", nil, t) |
| 73 | + col := ensureCollection(ctx, db, colname, nil, t) |
| 74 | + |
| 75 | + trxid, err := db.BeginTransaction(ctx, driver.TransactionCollections{Exclusive: []string{colname}}, nil) |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("Failed to begin transaction: %s", describe(err)) |
| 78 | + } |
| 79 | + |
| 80 | + tctx := driver.WithTransactionID(ctx, trxid) |
| 81 | + meta1 := insertDocument(tctx, col, t) |
| 82 | + |
| 83 | + // document should not exist without transaction |
| 84 | + documentExists(ctx, col, meta1.Key, false, t) |
| 85 | + |
| 86 | + // document should exist with transaction |
| 87 | + documentExists(tctx, col, meta1.Key, true, t) |
| 88 | + |
| 89 | + // Now commit the transaction |
| 90 | + if err := db.CommitTransaction(ctx, trxid, nil); err != nil { |
| 91 | + t.Fatalf("Failed to commit transaction: %s", describe(err)) |
| 92 | + } |
| 93 | + |
| 94 | + // document should exist |
| 95 | + documentExists(ctx, col, meta1.Key, true, t) |
| 96 | +} |
| 97 | + |
| 98 | +func TestTransactionAbort(t *testing.T) { |
| 99 | + c := createClientFromEnv(t, true) |
| 100 | + skipBelowVersion(c, "3.5", t) |
| 101 | + colname := "trx_test_col_abort" |
| 102 | + ctx := context.Background() |
| 103 | + db := ensureDatabase(ctx, c, "trx_test", nil, t) |
| 104 | + col := ensureCollection(ctx, db, colname, nil, t) |
| 105 | + |
| 106 | + trxid, err := db.BeginTransaction(ctx, driver.TransactionCollections{Exclusive: []string{colname}}, nil) |
| 107 | + if err != nil { |
| 108 | + t.Fatalf("Failed to begin transaction: %s", describe(err)) |
| 109 | + } |
| 110 | + |
| 111 | + tctx := driver.WithTransactionID(ctx, trxid) |
| 112 | + meta1 := insertDocument(tctx, col, t) |
| 113 | + |
| 114 | + // document should not exist without transaction |
| 115 | + documentExists(ctx, col, meta1.Key, false, t) |
| 116 | + |
| 117 | + // document should exist with transaction |
| 118 | + documentExists(tctx, col, meta1.Key, true, t) |
| 119 | + |
| 120 | + // Now commit the transaction |
| 121 | + if err := db.AbortTransaction(ctx, trxid, nil); err != nil { |
| 122 | + t.Fatalf("Failed to abort transaction: %s", describe(err)) |
| 123 | + } |
| 124 | + |
| 125 | + // document should exist |
| 126 | + documentExists(ctx, col, meta1.Key, false, t) |
| 127 | +} |
0 commit comments