Skip to content

Commit

Permalink
Updated the stream transactions test, added few more tests.
Browse files Browse the repository at this point in the history
For Arango 3.4, filter and exclude the StreamTransaction tests using Xunit Trait.

fix #322
  • Loading branch information
DiscJockeyDJ committed Jun 16, 2021
1 parent d7021f7 commit e5ac8eb
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
- run:
name: Test
command:
dotnet test -c Release
dotnet test -c Release --filter Transactions!=Stream

"test-arangodb-3_5":
working_directory: ~/arangodb-net-standard
Expand Down
248 changes: 174 additions & 74 deletions arangodb-net-standard.Test/TransactionApi/TransactionApiClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,81 +32,101 @@ public TransactionApiClientTest(TransactionApiClientTestFixture fixture)
/// Tests that the stream transaction gets aborted successfully.
/// </summary>
[Fact]
[Trait("Transactions", "Stream")]
public async Task AbortTransaction_ShouldSucceed()
{
// Begin a new transaction.
var beginTransaction = await _adb.Transaction.BeginTransaction<StreamTransactionResult>(
new StreamTransactionBody
{
Collections = new PostTransactionRequestCollections
{
Write = new[] { "TestCollection2" },
Write = new[] { "TestCollection1" },
},
});

var dummyCollectionUpdate = await _adb.Document.PostDocumentAsync(
"TestCollection2",
new
{
_key = "names",
value = new[] { "world", "love" },
});

// Abort the transaction.
var result =
var abortTransaction =
await _adb.Transaction.AbortTransaction<StreamTransactionResult>(beginTransaction.Result.Id);

// Check for the correct transaction status.
Assert.Equal(StreamTransactionStatus.Aborted, result.Result.Status);
Assert.Equal(StreamTransactionStatus.Aborted, abortTransaction.Result.Status);
}

/// <summary>
/// Test that an exception is thrown when trying to abort a transaction that has already been committed.
/// </summary>
/// <exception cref="ApiErrorException">With ErrorNum 1653 if the transaction cannot be aborted.</exception>
[Fact]
[Trait("Transactions", "Stream")]
public async Task AbortTransaction_ShouldThrowException_WhenTheTransactionCannotBeAborted()
{
// Begin a new transaction to create a document.
var beginTransaction = await _adb.Transaction.BeginTransaction<StreamTransactionResult>(
new StreamTransactionBody
{
Collections = new PostTransactionRequestCollections
{
Write = new[] { "TestCollection1" },
},
});

// Check the collection is not updated yet.
// Commit the transaction.
await _adb.Transaction.CommitTransaction<StreamTransactionResult>(beginTransaction.Result.Id);
var ex = await Assert.ThrowsAsync<ApiErrorException>(async () =>
{
// Abort the transaction with the same transaction Id.
await _adb.Transaction.AbortTransaction<object>(beginTransaction.Result.Id);
});

// Check for the correct error number.
Assert.Equal(1653, ex.ApiError.ErrorNum);
}

/// <summary>
/// Test that an exception is thrown when trying to abort a transaction that does not exist.
/// </summary>
/// <exception cref="ApiErrorException">With ErrorNum 10 if the transaction is not found.</exception>
[Fact]
[Trait("Transactions", "Stream")]
public async Task AbortTransaction_ShouldThrowException_WhenTheTransactionIsNotFound()
{
var ex = await Assert.ThrowsAsync<ApiErrorException>(async () =>
{
await _adb.Document.GetDocumentAsync<object>(dummyCollectionUpdate._id);
await _adb.Transaction.AbortTransaction<object>("Some Bogus Transaction Id");
});

Assert.Equal(1202, ex.ApiError.ErrorNum);
// Check for the correct error number.
Assert.Equal(10, ex.ApiError.ErrorNum);
}

/// <summary>
/// Tests that when the existing collections are used to begin a transaction, the stream transaction is running.
/// </summary>
[Fact]
[Trait("Transactions", "Stream")]
public async Task BeginTransaction_ShouldSucceed()
{
var result = await _adb.Transaction.BeginTransaction<StreamTransactionResult>(
// Begin a transaction.
var beginTransaction = await _adb.Transaction.BeginTransaction<StreamTransactionResult>(
new StreamTransactionBody
{
Collections = new PostTransactionRequestCollections
{
Write = new[] { "TestCollection2" },
Write = new[] { "TestCollection1" },
},
});

// Check for the correct transaction status.
Assert.Equal(StreamTransactionStatus.Running, result.Result.Status);

var dummyCollectionUpdate = await _adb.Document.PostDocumentAsync(
"TestCollection2",
new
{
_key = "names",
value = new[] { "world", "love" },
});

// Check the collection is not updated yet.
var ex = await Assert.ThrowsAsync<ApiErrorException>(async () =>
{
await _adb.Document.GetDocumentAsync<object>(dummyCollectionUpdate._id);
});

Assert.Equal(1202, ex.ApiError.ErrorNum);
Assert.Equal(StreamTransactionStatus.Running, beginTransaction.Result.Status);
}

/// <summary>
/// Tests that when referring to a non existing collection to transact, the stream transaction does not begin.
/// </summary>
/// <exception cref="ApiErrorException">With ErrorNum 1203 if the collection is not found.</exception>
[Fact]
[Trait("Transactions", "Stream")]
public async Task BeginTransaction_ShouldThrowException_WhenReferring_To_A_NonExisting_Collection()
{
var ex = await Assert.ThrowsAsync<ApiErrorException>(async () =>
Expand All @@ -115,81 +135,127 @@ await _adb.Transaction.BeginTransaction<object>(new StreamTransactionBody
Collections = new PostTransactionRequestCollections
{
Read = new[] { "SomeCollection" },
Write = new[] { "TestCollection2" },
Write = new[] { "TestCollection1" },
},
}));

// Check for the correct error number.
Assert.Equal(1203, ex.ApiError.ErrorNum);
}

/// <summary>
/// Tests that when there is no transaction body, the stream transaction does not begin.
/// </summary>
/// <exception cref="ApiErrorException">With ErrorNum 10 if the transaction body is missing.</exception>
[Fact]
[Trait("Transactions", "Stream")]
public async Task BeginTransaction_ShouldThrowException_WhenTransactionBodyIsMissing()
{
var ex = await Assert.ThrowsAsync<ApiErrorException>(async () =>
await _adb.Transaction.BeginTransaction<object>(null)
);

// Check for the correct error number.
Assert.Equal(10, ex.ApiError.ErrorNum);
}

/// <summary>
/// Tests that the stream transaction gets committed successfully.
/// </summary>
[Fact]
[Trait("Transactions", "Stream")]
public async Task CommitTransaction_ShouldSucceed()
{
// Begin a new transaction.
var beginTransaction = await _adb.Transaction.BeginTransaction<StreamTransactionResult>(
new StreamTransactionBody
{
Collections = new PostTransactionRequestCollections
{
Write = new[] { "TestCollection2" },
Write = new[] { "TestCollection1" },
},
});

var dummyCollectionUpdate = await _adb.Document.PostDocumentAsync(
"TestCollection2",
new
// Commit the transaction.
var result = await _adb.Transaction.CommitTransaction<StreamTransactionResult>(beginTransaction.Result.Id);

// Check for the correct transaction status.
Assert.Equal(StreamTransactionStatus.Committed, result.Result.Status);
}

/// <summary>
/// Test that an exception is thrown when trying to commit a transaction that has already been aborted.
/// </summary>
/// <exception cref="ApiErrorException">With ErrorNum 1653 if the transaction cannot be committed.</exception>
[Fact]
[Trait("Transactions", "Stream")]
public async Task CommitTransaction_ShouldThrowException_WhenTheTransactionCannotBeCommitted()
{
// Begin a new transaction to create a document.
var beginTransaction = await _adb.Transaction.BeginTransaction<StreamTransactionResult>(
new StreamTransactionBody
{
Collections = new PostTransactionRequestCollections
{
_key = "names",
value = new[] { "world", "love" },
});
Write = new[] { "TestCollection1" },
},
});

// Check the collection is not updated yet.
// Abort the transaction.
await _adb.Transaction.AbortTransaction<StreamTransactionResult>(beginTransaction.Result.Id);
var ex = await Assert.ThrowsAsync<ApiErrorException>(async () =>
{
await _adb.Document.GetDocumentAsync<object>(dummyCollectionUpdate._id);
// Commit the transaction with the same transaction Id.
await _adb.Transaction.CommitTransaction<object>(beginTransaction.Result.Id);
});

Assert.Equal(1202, ex.ApiError.ErrorNum);

// Commit a transaction.
var result =
await _adb.Transaction.CommitTransaction<StreamTransactionResult>(beginTransaction.Result.Id);

// Check for the correct transaction status.
Assert.Equal(StreamTransactionStatus.Committed, result.Result.Status);
// Check for the correct error number.
Assert.Equal(1653, ex.ApiError.ErrorNum);
}

// Check that the collection is now updated.
var latestCollectionUpdate = await _adb.Document.GetDocumentAsync<object>(dummyCollectionUpdate._id);
/// <summary>
/// Test that an exception is thrown when trying to commit a transaction that does not exist.
/// </summary>
/// <exception cref="ApiErrorException">With ErrorNum 10 if the transaction is not found.</exception>
[Fact]
[Trait("Transactions", "Stream")]
public async Task CommitTransaction_ShouldThrowException_WhenTheTransactionIsNotFound()
{
var ex = await Assert.ThrowsAsync<ApiErrorException>(async () =>
{
await _adb.Transaction.CommitTransaction<object>("Some Bogus Transaction Id");
});

// Check that the document exists.
Assert.NotNull(latestCollectionUpdate);
// Check for the correct error number.
Assert.Equal(10, ex.ApiError.ErrorNum);
}

/// <summary>
/// Tests that all running transactions are returned successfully.
/// </summary>
[Fact]
[Trait("Transactions", "Stream")]
public async Task GetAllRunningTransactions_ShouldSucceed()
{
// Begin the first transaction.
var firstTransaction = await _adb.Transaction.BeginTransaction<StreamTransactionResult>(
new StreamTransactionBody
new StreamTransactionBody
{
Collections = new PostTransactionRequestCollections
{
Collections = new PostTransactionRequestCollections
{
Write = new[] { "TestCollection1" },
},
});
Write = new[] { "TestCollection1" },
},
});

// Begin the second transaction.
var secondTransaction = await _adb.Transaction.BeginTransaction<StreamTransactionResult>(
new StreamTransactionBody
new StreamTransactionBody
{
Collections = new PostTransactionRequestCollections
{
Collections = new PostTransactionRequestCollections
{
Write = new[] { "TestCollection2" },
},
});
Write = new[] { "TestCollection2" },
},
});

// Get all running transactions.
var result = await _adb.Transaction.GetAllRunningTransactions();
Expand All @@ -199,20 +265,37 @@ public async Task GetAllRunningTransactions_ShouldSucceed()
Assert.Contains(result.Transactions, x => x.State.Equals(StreamTransactionStatus.Running));
}

/// <summary>
/// Tests that when there are no running transactions, an empty list of <see cref="Transaction"/> is returned.
/// </summary>
[Fact]
[Trait("Transactions", "Stream")]
public async Task GetAllRunningTransactions_ShouldSucceed_WhenThereAreNoRunningTransactions()
{
// Get all running transactions.
var result = await _adb.Transaction.GetAllRunningTransactions();

// Check for all the running transactions.
Assert.NotNull(result);
Assert.Empty(result.Transactions);
}

/// <summary>
/// Tests that the status of a transaction is returned successfully.
/// </summary>
[Fact]
[Trait("Transactions", "Stream")]
public async Task GetTransactionStatus_ShouldSucceed()
{
// Begin the transaction.
var firstTransaction = await _adb.Transaction.BeginTransaction<StreamTransactionResult>(
new StreamTransactionBody
{
Collections = new PostTransactionRequestCollections
{
Write = new[] { "TestCollection1" },
},
});
new StreamTransactionBody
{
Collections = new PostTransactionRequestCollections
{
Write = new[] { "TestCollection1" },
},
});

// Get the transaction status.
var transaction =
Expand All @@ -222,6 +305,23 @@ public async Task GetTransactionStatus_ShouldSucceed()
Assert.Equal(firstTransaction.Result.Status, transaction.Result.Status);
}

/// <summary>
/// Test that an exception is thrown when trying to get the status of a transaction that does not exist.
/// </summary>
/// <exception cref="ApiErrorException">With ErrorNum 10 if the transaction is not found.</exception>
[Fact]
[Trait("Transactions", "Stream")]
public async Task GetTransactionStatus_ShouldThrowException_WhenTheTransctionIdIsNotFound()
{
var ex = await Assert.ThrowsAsync<ApiErrorException>(async () =>
{
await _adb.Transaction.GetTransactionStatus<object>("Some Bogus Transaction Id");
});

// Check for the correct error number.
Assert.Equal(10, ex.ApiError.ErrorNum);
}

/// <summary>
/// Tests that a post JS transaction succeeds.
/// </summary>
Expand Down
Loading

0 comments on commit e5ac8eb

Please sign in to comment.