|
| 1 | +// Copyright 2026 Democratized Data Foundation |
| 2 | +// |
| 3 | +// This file is part of the DefraDB test suite. |
| 4 | +// |
| 5 | +// The DefraDB test suite is licensed under either: |
| 6 | +// |
| 7 | +// (1) GNU Affero General Public License v3 |
| 8 | +// (2) Business Source License 1.1 |
| 9 | +// |
| 10 | +// See tests/LICENSE for details. |
| 11 | + |
| 12 | +package txn_testing |
| 13 | + |
| 14 | +import ( |
| 15 | + "testing" |
| 16 | + |
| 17 | + "github.com/sourcenetwork/immutable" |
| 18 | + |
| 19 | + "github.com/sourcenetwork/defradb/tests/action" |
| 20 | + testUtils "github.com/sourcenetwork/defradb/tests/integration" |
| 21 | + "github.com/sourcenetwork/defradb/tests/state" |
| 22 | +) |
| 23 | + |
| 24 | +// SDL definition order determines CollectionID: |
| 25 | +// |
| 26 | +// CollectionID 0 = Company (first type in SDL) |
| 27 | +// CollectionID 1 = Employee (second type in SDL) |
| 28 | +const companyEmployeeSDL = ` |
| 29 | + type Company { |
| 30 | + name: String |
| 31 | + } |
| 32 | + type Employee { |
| 33 | + name: String |
| 34 | + company: Company |
| 35 | + } |
| 36 | +` |
| 37 | + |
| 38 | +// TestTxnRelation_CreateTargetAndLinkInSameTxn_NoError asserts that a document |
| 39 | +// and its relation target can both be created within a single transaction: the |
| 40 | +// relation validator can read the target from the same transaction's buffer. |
| 41 | +func TestTxnRelation_CreateTargetAndLinkInSameTxn_NoError(t *testing.T) { |
| 42 | + test := testUtils.TestCase{ |
| 43 | + Actions: []any{ |
| 44 | + &action.AddCollection{ |
| 45 | + SDL: companyEmployeeSDL, |
| 46 | + }, |
| 47 | + // Create Company in Txn1 (not yet committed). |
| 48 | + &action.AddDoc{ |
| 49 | + CollectionID: 0, |
| 50 | + TransactionID: immutable.Some(1), |
| 51 | + Doc: `{"name": "Acme"}`, |
| 52 | + }, |
| 53 | + // Create Employee in the same Txn1 — validateRelationDocIDs reads from |
| 54 | + // Txn1's buffer and sees the Company even though it isn't committed yet. |
| 55 | + &action.AddDoc{ |
| 56 | + CollectionID: 1, |
| 57 | + TransactionID: immutable.Some(1), |
| 58 | + DocMap: map[string]any{ |
| 59 | + "name": "Alice", |
| 60 | + "company": testUtils.NewDocIndex(0, 0), |
| 61 | + }, |
| 62 | + }, |
| 63 | + &action.CommitTransaction{ |
| 64 | + TransactionID: 1, |
| 65 | + }, |
| 66 | + }, |
| 67 | + } |
| 68 | + testUtils.ExecuteTestCase(t, test) |
| 69 | +} |
| 70 | + |
| 71 | +// TestTxnRelation_LinkInTxnBeforeTargetCommitted_Error asserts that a transaction |
| 72 | +// cannot link to a document that was created in a different, uncommitted transaction: |
| 73 | +// the relation validator does not see the other transaction's uncommitted data. |
| 74 | +func TestTxnRelation_LinkInTxnBeforeTargetCommitted_Error(t *testing.T) { |
| 75 | + test := testUtils.TestCase{ |
| 76 | + // LevelDB does not support concurrent transactions. |
| 77 | + SupportedDatabaseTypes: immutable.Some([]state.DatabaseType{ |
| 78 | + testUtils.BadgerFileType, |
| 79 | + testUtils.BadgerIMType, |
| 80 | + testUtils.DefraIMType, |
| 81 | + }), |
| 82 | + Actions: []any{ |
| 83 | + &action.AddCollection{ |
| 84 | + SDL: companyEmployeeSDL, |
| 85 | + }, |
| 86 | + // Create Company in Txn1 (NOT committed). |
| 87 | + &action.AddDoc{ |
| 88 | + CollectionID: 0, |
| 89 | + TransactionID: immutable.Some(1), |
| 90 | + Doc: `{"name": "Acme"}`, |
| 91 | + }, |
| 92 | + // Create Employee in Txn2 — Txn2 cannot see Txn1's uncommitted Company. |
| 93 | + &action.AddDoc{ |
| 94 | + CollectionID: 1, |
| 95 | + TransactionID: immutable.Some(2), |
| 96 | + DocMap: map[string]any{ |
| 97 | + "name": "Alice", |
| 98 | + "company": testUtils.NewDocIndex(0, 0), |
| 99 | + }, |
| 100 | + ExpectedError: "relation target document not found", |
| 101 | + }, |
| 102 | + }, |
| 103 | + } |
| 104 | + testUtils.ExecuteTestCase(t, test) |
| 105 | +} |
| 106 | + |
| 107 | +// TestTxnRelation_DeleteTargetThenLink_Error asserts that when a relation target is |
| 108 | +// soft-deleted within a transaction, a subsequent AddDoc in the same transaction that |
| 109 | +// references the deleted target is rejected by the relation validator. |
| 110 | +func TestTxnRelation_DeleteTargetThenLink_Error(t *testing.T) { |
| 111 | + test := testUtils.TestCase{ |
| 112 | + Actions: []any{ |
| 113 | + &action.AddCollection{ |
| 114 | + SDL: companyEmployeeSDL, |
| 115 | + }, |
| 116 | + // Create and commit Company outside any transaction. |
| 117 | + &action.AddDoc{ |
| 118 | + CollectionID: 0, |
| 119 | + Doc: `{"name": "Acme"}`, |
| 120 | + }, |
| 121 | + // In Txn1: soft-delete the Company. |
| 122 | + testUtils.DeleteDoc{ |
| 123 | + CollectionID: 0, |
| 124 | + DocID: 0, |
| 125 | + TransactionID: immutable.Some(1), |
| 126 | + }, |
| 127 | + // In Txn1: attempt to create Employee linking to the now-deleted Company. |
| 128 | + &action.AddDoc{ |
| 129 | + CollectionID: 1, |
| 130 | + TransactionID: immutable.Some(1), |
| 131 | + DocMap: map[string]any{ |
| 132 | + "name": "Alice", |
| 133 | + "company": testUtils.NewDocIndex(0, 0), |
| 134 | + }, |
| 135 | + ExpectedError: "relation target document not found", |
| 136 | + }, |
| 137 | + }, |
| 138 | + } |
| 139 | + testUtils.ExecuteTestCase(t, test) |
| 140 | +} |
0 commit comments