Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using EfficientDynamoDb.Extensions;
using EfficientDynamoDb.Exceptions;
using EfficientDynamoDb.Internal.Extensions;
using EfficientDynamoDb.Operations.Shared;
using NUnit.Framework;
Expand Down Expand Up @@ -218,4 +220,48 @@ public async Task SupportLargeBatchGetRequest()

result.ShouldBe(_testUsers, ignoreOrder: true);
}

[Test]
public void ThrowWhenInvalidBatchGetParameters()
{
Should.Throw<ValidationException>(async () =>
{
await _context.BatchGet()
.WithItems(
Batch.GetItem<TestUser>().WithPrimaryKey($"{KeyPrefix}-pk-1", ""),
Batch.GetItem<TestUser>().WithPrimaryKey($"{KeyPrefix}-pk-2", "")
)
.ToListAsync<TestUser>();
});
}

[Test]
public async Task BatchGetItemsWhenSuppressedThrowing()
{
var result = await _context.BatchGet()
.WithItems(
Batch.GetItem<TestUser>().WithPrimaryKey(_testUsers[0].PartitionKey, _testUsers[0].SortKey),
Batch.GetItem<TestUser>().WithPrimaryKey(_testUsers[1].PartitionKey, _testUsers[1].SortKey)
)
.SuppressThrowing()
.ToListAsync<TestUser>();

result.IsSuccess.ShouldBeTrue();
result.Value.ShouldBe(_testUsers[..2], ignoreOrder: true);
}

[Test]
public async Task ReturnErrorWhenInvalidRequestAndSuppressedThrowing()
{
var result = await _context.BatchGet()
.WithItems(
Batch.GetItem<TestUser>().WithPrimaryKey($"{KeyPrefix}-pk-1", ""),
Batch.GetItem<TestUser>().WithPrimaryKey($"{KeyPrefix}-pk-2", "")
)
.SuppressThrowing()
.ToListAsync<TestUser>();

result.IsSuccess.ShouldBeFalse();
result.Exception.ShouldNotBeNull();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using EfficientDynamoDb.Exceptions;
using EfficientDynamoDb.Operations.Shared;
using NUnit.Framework;
using Shouldly;
Expand Down Expand Up @@ -460,4 +461,79 @@ await _context.BatchWrite()
retrievedReplacementUser!.Name.ShouldBe("Replacement Complex User");
retrievedReplacementUser.Age.ShouldBe(35);
}

[Test]
public void ThrowWhenInvalidBatchWriteParameters()
{
var invalidUsers = new[]
{
new TestUser { PartitionKey = $"{KeyPrefix}-pk-1", SortKey = "", Name = "test", Age = 25, Email = "test@example.com" },
new TestUser { PartitionKey = $"{KeyPrefix}-pk-2", SortKey = "", Name = "test2", Age = 30, Email = "test2@example.com" }
};

Should.Throw<ValidationException>(async () =>
{
await _context.BatchWrite()
.WithItems(invalidUsers.Select(Batch.PutItem))
.ExecuteAsync();
});
}

[Test]
public async Task BatchWriteItemsWhenSuppressedThrowing()
{
var testUsers = new[]
{
new TestUser
{
PartitionKey = $"{KeyPrefix}-suppressed-pk-1",
SortKey = $"{KeyPrefix}-suppressed-sk-1",
Name = "Suppressed User 1",
Age = 25,
Email = "suppressed1@example.com"
},
new TestUser
{
PartitionKey = $"{KeyPrefix}-suppressed-pk-2",
SortKey = $"{KeyPrefix}-suppressed-sk-2",
Name = "Suppressed User 2",
Age = 30,
Email = "suppressed2@example.com"
}
};

_testUsersToCleanup.AddRange(testUsers);

var result = await _context.BatchWrite()
.WithItems(testUsers.Select(Batch.PutItem))
.SuppressThrowing()
.ExecuteAsync();

result.IsSuccess.ShouldBeTrue();

// Verify items were created using batch read
var retrievedItems = await _context.BatchGet()
.WithItems(testUsers.Select(user => Batch.GetItem<TestUser>().WithPrimaryKey(user.PartitionKey, user.SortKey)))
.ToListAsync<TestUser>();

retrievedItems.ShouldBe(testUsers, ignoreOrder: true);
}

[Test]
public async Task ReturnErrorWhenInvalidRequestAndSuppressedThrowing()
{
var invalidUsers = new[]
{
new TestUser { PartitionKey = $"{KeyPrefix}-suppressed-error-pk-1", SortKey = "", Name = "test", Age = 25, Email = "test@example.com" },
new TestUser { PartitionKey = $"{KeyPrefix}-suppressed-error-pk-2", SortKey = "", Name = "test2", Age = 30, Email = "test2@example.com" }
};

var result = await _context.BatchWrite()
.WithItems(invalidUsers.Select(Batch.PutItem))
.SuppressThrowing()
.ExecuteAsync();

result.IsSuccess.ShouldBeFalse();
result.Exception.ShouldNotBeNull();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using EfficientDynamoDb.Exceptions;
using EfficientDynamoDb.Internal.Extensions;
using EfficientDynamoDb.Operations.Shared;
using NUnit.Framework;
Expand Down Expand Up @@ -51,6 +52,18 @@ public async Task ReturnItemWhenItemExists()
result.ShouldBe(_testUser);
}

[Test]
public void ThrowsExceptionWhenMissingSortKey()
{
// Pass only partition key when both partition key and sort key are required
// This should trigger a validation error
Should.Throw<ValidationException>(() =>
_context.GetItem<TestUser>()
.WithPrimaryKey(_testUser.PartitionKey)
.ToItemAsync()
);
}

[Test]
public async Task ReturnItemUsingBuilder()
{
Expand Down Expand Up @@ -150,4 +163,32 @@ public async Task ReturnResponseWithMetadata(bool useConsistentRead)
response.ConsumedCapacity.ShouldNotBeNull();
response.ConsumedCapacity.CapacityUnits.ShouldBe(expectedConsumedCapacity);
}

[Test]
public async Task ReturnSuccessfulResultWhenItemExistsAndSuppressedThrowing()
{
var result = await _context.GetItem<TestUser>()
.WithPrimaryKey(_testUser.PartitionKey, _testUser.SortKey)
.SuppressThrowing()
.ToItemAsync();

result.IsSuccess.ShouldBeTrue();
result.Exception.ShouldBeNull();
result.Value.ShouldBe(_testUser);
}

[Test]
public async Task ReturnErrorWhenInvalidRequestAndSuppressedThrowing()
{
// Pass only partition key when both partition key and sort key are required
// This should trigger a validation error
var result = await _context.GetItem<TestUser>()
.WithPrimaryKey(_testUser.PartitionKey)
.SuppressThrowing()
.ToItemAsync();

result.IsSuccess.ShouldBeFalse();
result.Exception.ShouldNotBeNull();
result.Value.ShouldBeNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ public async Task CreateNewItemSuccessfully()
retrievedItem.ShouldBe(testUser);
}

[Test]
public void ThrowWhenMissingSortKey()
{
Should.Throw<ValidationException>(async () =>
{
var testUser = new TestUser { PartitionKey = $"{KeyPrefix}-missing-sort-key-pk", SortKey = "", Name = "test", Age = 25, Email = "test@example.com" };
await _context.PutItem()
.WithItem(testUser)
.ExecuteAsync();
_testPartitionKey = testUser.PartitionKey;
_testSortKey = testUser.SortKey;
});
}

[Test]
public async Task ReplaceExistingItemSuccessfully()
{
Expand Down Expand Up @@ -380,4 +394,47 @@ await _context.PutItem()
.ToItemAsync();
retrievedItem.ShouldBe(testUser);
}

[Test]
public async Task CreateNewItemWhenSuppressedThrowing()
{
_testPartitionKey = $"{KeyPrefix}-suppress_error_success-pk";
_testSortKey = $"{KeyPrefix}-suppress_error_success-sk";
var testUser = new TestUser
{
PartitionKey = _testPartitionKey,
SortKey = _testSortKey,
Name = "Suppress Error User",
Age = 25,
Email = "suppress_error@example.com"
};

var result = await _context.PutItem()
.WithItem(testUser)
.SuppressThrowing()
.ExecuteAsync();

result.IsSuccess.ShouldBeTrue();
}

[Test]
public async Task ReturnErrorWhenInvalidRequestAndSuppressedThrowing()
{
var testUser = new TestUser
{
PartitionKey = $"{KeyPrefix}-suppress_error_fail-pk",
SortKey = "", // Empty sort key - this should trigger a validation error
Name = "Suppress Error User",
Age = 25,
Email = "suppress_error@example.com"
};

var result = await _context.PutItem()
.WithItem(testUser)
.SuppressThrowing()
.ExecuteAsync();

result.IsSuccess.ShouldBeFalse();
result.Exception.ShouldNotBeNull();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using EfficientDynamoDb.Exceptions;
using EfficientDynamoDb.Extensions;
using EfficientDynamoDb.Operations.Shared;
using NUnit.Framework;
using Shouldly;
Expand Down Expand Up @@ -436,4 +438,43 @@ public async Task QueryItemsWithSortKeyLessThanOrEqualToSuccessfully()
string.CompareOrdinal(x.SortKey, sortKeyThreshold) <= 0);
queriedItems.ShouldBe(expectedItems, ignoreOrder: true);
}

[Test]
public void ThrowWhenInvalidQueryParameters()
{
Should.Throw<ResourceNotFoundException>(async () =>
{
await _context.Query<TestUser>()
.WithKeyExpression(x => x.On(y => y.PartitionKey).EqualTo($"{KeyPrefix}-pk-1"))
.WithTableName("non_existent_table")
.ToAsyncEnumerable()
.ToListAsync();
});
}

[Test]
public async Task QueryItemsWhenSuppressedThrowing()
{
var result = await _context.Query<TestUser>()
.WithKeyExpression(x => x.On(y => y.PartitionKey).EqualTo($"{KeyPrefix}-pk-1"))
.SuppressThrowing()
.ToListAsync();

result.Exception.ShouldBeNull();
result.IsSuccess.ShouldBeTrue();
result.Value.ShouldBe(_testUsers.Where(x => x.PartitionKey == $"{KeyPrefix}-pk-1"), ignoreOrder: true);
}

[Test]
public async Task ReturnErrorWhenInvalidRequestAndSuppressedThrowing()
{
var result = await _context.Query<TestUser>()
.WithKeyExpression(x => x.On(y => y.PartitionKey).EqualTo($"{KeyPrefix}-pk-1"))
.WithTableName("non_existent_table")
.SuppressThrowing()
.ToPageAsync();

result.IsSuccess.ShouldBeFalse();
result.Exception.ShouldNotBeNull();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using EfficientDynamoDb.Exceptions;
using EfficientDynamoDb.Extensions;
using EfficientDynamoDb.Operations.Shared;
using NUnit.Framework;
using Shouldly;
Expand Down Expand Up @@ -324,4 +326,43 @@ public async Task ScanWithStringContainsFilterSuccessfully()

scannedItems.ShouldAllBe(x => x.Name.Contains("Alice"));
}

[Test]
public void ThrowWhenInvalidScanParameters()
{
Should.Throw<ResourceNotFoundException>(async () =>
{
await _context.Scan<TestUser>()
.WithFilterExpression(x => x.On(y => y.PartitionKey).BeginsWith($"{KeyPrefix}-pk-"))
.WithTableName("non_existent_table")
.ToAsyncEnumerable()
.ToListAsync();
});
}

[Test]
public async Task ScanItemsWhenSuppressedThrowing()
{
var result = await _context.Scan<TestUser>()
.WithFilterExpression(x => x.On(y => y.PartitionKey).BeginsWith($"{KeyPrefix}-pk-"))
.WithLimit(2)
.SuppressThrowing()
.ToPageAsync();

result.IsSuccess.ShouldBeTrue();
result.Value.Items.Count.ShouldBeLessThanOrEqualTo(2);
}

[Test]
public async Task ReturnErrorWhenInvalidRequestAndSuppressedThrowing()
{
var result = await _context.Scan<TestUser>()
.WithFilterExpression(x => x.On(y => y.PartitionKey).BeginsWith($"{KeyPrefix}-pk-"))
.WithTableName("non_existent_table")
.SuppressThrowing()
.ToPageAsync();

result.IsSuccess.ShouldBeFalse();
result.Exception.ShouldNotBeNull();
}
}
Loading