Skip to content

Commit

Permalink
Correctly handle errors in transaction tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Feb 18, 2022
1 parent 2351fae commit 67a1f02
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions db/tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,16 @@ func ExecuteQueryTestCase(t *testing.T, schema string, collectionNames []string,

// Create the transactions before executing and queries
transactions := make([]core.Txn, 0, len(test.TransactionalQueries))
for _, tq := range test.TransactionalQueries {
erroredQueries := make([]bool, len(test.TransactionalQueries))
for i, tq := range test.TransactionalQueries {
if len(transactions) < tq.TransactionId {
continue
}

txn, err := db.NewTxn(ctx, false)
if err != nil {
if assertError(t, test.Description, err, tq.ExpectedError) {
return
erroredQueries[i] = true
}
}
defer txn.Discard(ctx)
Expand All @@ -266,28 +267,34 @@ func ExecuteQueryTestCase(t *testing.T, schema string, collectionNames []string,
transactions[tq.TransactionId] = txn
}

for _, tq := range test.TransactionalQueries {
for i, tq := range test.TransactionalQueries {
if erroredQueries[i] {
continue
}
result := db.ExecTransactionalQuery(ctx, tq.Query, transactions[tq.TransactionId])
if assertQueryResults(t, test.Description, result, tq.Results, tq.ExpectedError) {
return
erroredQueries[i] = true
}
}

txnIndexesCommited := map[int]struct{}{}
for _, tq := range test.TransactionalQueries {
for i, tq := range test.TransactionalQueries {
if erroredQueries[i] {
continue
}
if _, alreadyCommited := txnIndexesCommited[tq.TransactionId]; alreadyCommited {
continue
}
txnIndexesCommited[tq.TransactionId] = struct{}{}

err := transactions[tq.TransactionId].Commit(ctx)
if assertError(t, test.Description, err, tq.ExpectedError) {
return
erroredQueries[i] = true
}
}

for _, tq := range test.TransactionalQueries {
if tq.ExpectedError != "" {
for i, tq := range test.TransactionalQueries {
if tq.ExpectedError != "" && !erroredQueries[i] {
assert.Fail(t, "Expected an error however none was raised.", test.Description)
}
}
Expand Down

0 comments on commit 67a1f02

Please sign in to comment.