diff --git a/docs/preview/02-Features/04-Storage/02-cosmos.mdx b/docs/preview/02-Features/04-Storage/02-cosmos.mdx index 95c54933..8ebde5ff 100644 --- a/docs/preview/02-Features/04-Storage/02-cosmos.mdx +++ b/docs/preview/02-Features/04-Storage/02-cosmos.mdx @@ -177,7 +177,7 @@ await TemporaryNoSqlContainer.CreateIfNotExistsAsync(..., options => // Delete all NoSql items that matches any of the configured filters, // upon the test fixture creation, when there was already a NoSql container available. options.OnSetup.CleanMatchingItems( - NoSqlItemFilter.ItemEqual((Shipment s) => s.BoatName == "The Alice"), + NoSqlItemFilter.Where(ship => ship["BoatName"] == "The Alice"), NoSqlItemFilter.ItemIdEqual("123")); // Options related to when the test fixture is teared down. @@ -194,8 +194,8 @@ await TemporaryNoSqlContainer.CreateIfNotExistsAsync(..., options => // upon the test fixture disposal, even if the test fixture didn't inserted them. // ⚠️ NoSql items inserted by the test fixture will always be deleted, regardless of the configured filters. options.OnTeardown.CleanMatchingItems( - NoSqlItemFilter.ItemEqual((Shipment s) => s.BoatName == "The Alice"), - NoSqlItemFilter.ItemIdEqual("123")); + NoSqlItemFilter.Where((Shipment s) => s.BoatName == "The Alice"), + NoSqlItemFilter.IdEqual("123")); }); // `OnTeardown` is also still available after the temporary container is created: diff --git a/src/Arcus.Testing.Storage.Cosmos/TemporaryNoSqlContainer.cs b/src/Arcus.Testing.Storage.Cosmos/TemporaryNoSqlContainer.cs index 9b07ada7..14e1c532 100644 --- a/src/Arcus.Testing.Storage.Cosmos/TemporaryNoSqlContainer.cs +++ b/src/Arcus.Testing.Storage.Cosmos/TemporaryNoSqlContainer.cs @@ -48,7 +48,7 @@ private NoSqlItemFilter(Func /// /// The unique required 'id' value. /// Thrown when the is blank. - public static NoSqlItemFilter ItemIdEqual(string itemId) + public static NoSqlItemFilter IdEqual(string itemId) { if (string.IsNullOrWhiteSpace(itemId)) { @@ -64,7 +64,7 @@ public static NoSqlItemFilter ItemIdEqual(string itemId) /// The unique required 'id' value. /// The value that specifies how the strings will be compared. /// Thrown when the is blank. - public static NoSqlItemFilter ItemIdEqual(string itemId, StringComparison comparisonType) + public static NoSqlItemFilter IdEqual(string itemId, StringComparison comparisonType) { if (string.IsNullOrWhiteSpace(itemId)) { @@ -86,11 +86,11 @@ public static NoSqlItemFilter PartitionKeyEqual(PartitionKey partitionKey) /// /// Creates a filter to match a NoSql item based on its contents. /// - /// The custom type + /// The custom type of the NoSql item. /// The custom filter to match against the contents of the NoSql item. /// Thrown when the is null. /// Thrown when the used client has no serializer configured. - public static NoSqlItemFilter ItemEqual(Func itemFilter) + public static NoSqlItemFilter Where(Func itemFilter) { ArgumentNullException.ThrowIfNull(itemFilter); @@ -115,6 +115,23 @@ public static NoSqlItemFilter ItemEqual(Func itemFilter) }); } + /// + /// Creates a filter to match a NoSql item based on its contents. + /// + /// The custom filter to match against the contents of the NoSql item. + /// Thrown when the is null. + /// Thrown when the used client has no serializer configured. + public static NoSqlItemFilter Where(Func, bool> itemFilter) + { + ArgumentNullException.ThrowIfNull(itemFilter); + + return new NoSqlItemFilter((_, _, json, _) => + { + var dic = json.ToObject>(); + return itemFilter(dic); + }); + } + /// /// Match the current NoSql item with the user configured filter. /// diff --git a/src/Arcus.Testing.Tests.Integration/Storage/TemporaryNoSqlContainerTests.cs b/src/Arcus.Testing.Tests.Integration/Storage/TemporaryNoSqlContainerTests.cs index 71f011b8..ada3cd5f 100644 --- a/src/Arcus.Testing.Tests.Integration/Storage/TemporaryNoSqlContainerTests.cs +++ b/src/Arcus.Testing.Tests.Integration/Storage/TemporaryNoSqlContainerTests.cs @@ -4,7 +4,6 @@ using Arcus.Testing.Tests.Integration.Storage.Fixture; using Bogus; using Microsoft.Azure.Cosmos; -using Microsoft.Extensions.Options; using Newtonsoft.Json; using Xunit; using Xunit.Abstractions; @@ -164,12 +163,13 @@ public async Task CreateTempNoSqlContainerWithCleanMatchingOnTeardown_WhenExisti private static NoSqlItemFilter CreateMatchingFilter(Ship item) { - return Bogus.Random.Int(1, 4) switch + return Bogus.Random.Int(1, 5) switch { - 1 => NoSqlItemFilter.ItemIdEqual(item.Id), - 2 => NoSqlItemFilter.ItemIdEqual(item.Id, StringComparison.OrdinalIgnoreCase), + 1 => NoSqlItemFilter.IdEqual(item.Id), + 2 => NoSqlItemFilter.IdEqual(item.Id, StringComparison.OrdinalIgnoreCase), 3 => NoSqlItemFilter.PartitionKeyEqual(item.GetPartitionKey()), - 4 => NoSqlItemFilter.ItemEqual(x => x.BoatName == item.BoatName), + 4 => NoSqlItemFilter.Where(x => x.BoatName == item.BoatName), + 5 => NoSqlItemFilter.Where(x => x["BoatName"].ToString() == item.BoatName), _ => throw new ArgumentOutOfRangeException(nameof(item), "Unknown filter type") }; } diff --git a/src/Arcus.Testing.Tests.Unit/Storage/NoSqlItemFilterTests.cs b/src/Arcus.Testing.Tests.Unit/Storage/NoSqlItemFilterTests.cs index 6fa307d9..8a453ff3 100644 --- a/src/Arcus.Testing.Tests.Unit/Storage/NoSqlItemFilterTests.cs +++ b/src/Arcus.Testing.Tests.Unit/Storage/NoSqlItemFilterTests.cs @@ -13,7 +13,7 @@ public class NoSqlItemFilterTests [ClassData(typeof(Blanks))] public void CreateFilter_WithoutItemId_Fails(string itemId) { - Assert.ThrowsAny(() => NoSqlItemFilter.ItemIdEqual(itemId)); + Assert.ThrowsAny(() => NoSqlItemFilter.IdEqual(itemId)); } [Theory] @@ -21,13 +21,19 @@ public void CreateFilter_WithoutItemId_Fails(string itemId) public void CreateFilter_WithComparisonWithoutItemId_Fails(string itemId) { Assert.ThrowsAny( - () => NoSqlItemFilter.ItemIdEqual(itemId, Bogus.PickRandom())); + () => NoSqlItemFilter.IdEqual(itemId, Bogus.PickRandom())); + } + + [Fact] + public void CreateFilter_WithoutItemTFilter_Fails() + { + Assert.ThrowsAny(() => NoSqlItemFilter.Where(null)); } [Fact] public void CreateFilter_WithoutItemFilter_Fails() { - Assert.ThrowsAny(() => NoSqlItemFilter.ItemEqual(null)); + Assert.ThrowsAny(() => NoSqlItemFilter.Where(null)); } } } diff --git a/src/Arcus.Testing.Tests.Unit/Storage/TemporaryNoSqlContainerOptionsTests.cs b/src/Arcus.Testing.Tests.Unit/Storage/TemporaryNoSqlContainerOptionsTests.cs index 0948946a..ff27d7a0 100644 --- a/src/Arcus.Testing.Tests.Unit/Storage/TemporaryNoSqlContainerOptionsTests.cs +++ b/src/Arcus.Testing.Tests.Unit/Storage/TemporaryNoSqlContainerOptionsTests.cs @@ -13,7 +13,7 @@ public void OnSetup_WithNullFilter_Fails() // Act / Assert Assert.ThrowsAny(() => options.OnSetup.CleanMatchingItems(null)); - Assert.ThrowsAny(() => options.OnSetup.CleanMatchingItems(NoSqlItemFilter.ItemIdEqual("some-id"), null)); + Assert.ThrowsAny(() => options.OnSetup.CleanMatchingItems(NoSqlItemFilter.IdEqual("some-id"), null)); } [Fact] @@ -24,7 +24,7 @@ public void OnTeardown_WithNullFilter_Fails() // Act / Assert Assert.ThrowsAny(() => options.OnTeardown.CleanMatchingItems(null)); - Assert.ThrowsAny(() => options.OnTeardown.CleanMatchingItems(NoSqlItemFilter.ItemIdEqual("some-id"), null)); + Assert.ThrowsAny(() => options.OnTeardown.CleanMatchingItems(NoSqlItemFilter.IdEqual("some-id"), null)); } } }