Skip to content

Commit f2df442

Browse files
authored
feat: add nosql item filter dic overload (#222)
* pr-fix: correct merge w/ 'main' * chore: eat your own logging dog food * feat: add nosql item filter dictionary overload
1 parent 56ae7a0 commit f2df442

File tree

5 files changed

+40
-17
lines changed

5 files changed

+40
-17
lines changed

docs/preview/02-Features/04-Storage/02-cosmos.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ await TemporaryNoSqlContainer.CreateIfNotExistsAsync(..., options =>
177177
// Delete all NoSql items that matches any of the configured filters,
178178
// upon the test fixture creation, when there was already a NoSql container available.
179179
options.OnSetup.CleanMatchingItems(
180-
NoSqlItemFilter.ItemEqual((Shipment s) => s.BoatName == "The Alice"),
180+
NoSqlItemFilter.Where(ship => ship["BoatName"] == "The Alice"),
181181
NoSqlItemFilter.ItemIdEqual("123"));
182182

183183
// Options related to when the test fixture is teared down.
@@ -194,8 +194,8 @@ await TemporaryNoSqlContainer.CreateIfNotExistsAsync(..., options =>
194194
// upon the test fixture disposal, even if the test fixture didn't inserted them.
195195
// ⚠️ NoSql items inserted by the test fixture will always be deleted, regardless of the configured filters.
196196
options.OnTeardown.CleanMatchingItems(
197-
NoSqlItemFilter.ItemEqual((Shipment s) => s.BoatName == "The Alice"),
198-
NoSqlItemFilter.ItemIdEqual("123"));
197+
NoSqlItemFilter.Where((Shipment s) => s.BoatName == "The Alice"),
198+
NoSqlItemFilter.IdEqual("123"));
199199
});
200200

201201
// `OnTeardown` is also still available after the temporary container is created:

src/Arcus.Testing.Storage.Cosmos/TemporaryNoSqlContainer.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private NoSqlItemFilter(Func<string, PartitionKey, JObject, CosmosClient, bool>
4848
/// </summary>
4949
/// <param name="itemId">The unique required 'id' value.</param>
5050
/// <exception cref="ArgumentException">Thrown when the <paramref name="itemId"/> is blank.</exception>
51-
public static NoSqlItemFilter ItemIdEqual(string itemId)
51+
public static NoSqlItemFilter IdEqual(string itemId)
5252
{
5353
if (string.IsNullOrWhiteSpace(itemId))
5454
{
@@ -64,7 +64,7 @@ public static NoSqlItemFilter ItemIdEqual(string itemId)
6464
/// <param name="itemId">The unique required 'id' value.</param>
6565
/// <param name="comparisonType">The value that specifies how the strings will be compared.</param>
6666
/// <exception cref="ArgumentException">Thrown when the <paramref name="itemId"/> is blank.</exception>
67-
public static NoSqlItemFilter ItemIdEqual(string itemId, StringComparison comparisonType)
67+
public static NoSqlItemFilter IdEqual(string itemId, StringComparison comparisonType)
6868
{
6969
if (string.IsNullOrWhiteSpace(itemId))
7070
{
@@ -86,11 +86,11 @@ public static NoSqlItemFilter PartitionKeyEqual(PartitionKey partitionKey)
8686
/// <summary>
8787
/// Creates a filter to match a NoSql item based on its contents.
8888
/// </summary>
89-
/// <typeparam name="TItem">The custom type </typeparam>
89+
/// <typeparam name="TItem">The custom type of the NoSql item.</typeparam>
9090
/// <param name="itemFilter">The custom filter to match against the contents of the NoSql item.</param>
9191
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="itemFilter"/> is <c>null</c>.</exception>
9292
/// <exception cref="InvalidOperationException">Thrown when the used client has no serializer configured.</exception>
93-
public static NoSqlItemFilter ItemEqual<TItem>(Func<TItem, bool> itemFilter)
93+
public static NoSqlItemFilter Where<TItem>(Func<TItem, bool> itemFilter)
9494
{
9595
ArgumentNullException.ThrowIfNull(itemFilter);
9696

@@ -115,6 +115,23 @@ public static NoSqlItemFilter ItemEqual<TItem>(Func<TItem, bool> itemFilter)
115115
});
116116
}
117117

118+
/// <summary>
119+
/// Creates a filter to match a NoSql item based on its contents.
120+
/// </summary>
121+
/// <param name="itemFilter">The custom filter to match against the contents of the NoSql item.</param>
122+
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="itemFilter"/> is <c>null</c>.</exception>
123+
/// <exception cref="InvalidOperationException">Thrown when the used client has no serializer configured.</exception>
124+
public static NoSqlItemFilter Where(Func<IDictionary<string, object>, bool> itemFilter)
125+
{
126+
ArgumentNullException.ThrowIfNull(itemFilter);
127+
128+
return new NoSqlItemFilter((_, _, json, _) =>
129+
{
130+
var dic = json.ToObject<Dictionary<string, object>>();
131+
return itemFilter(dic);
132+
});
133+
}
134+
118135
/// <summary>
119136
/// Match the current NoSql item with the user configured filter.
120137
/// </summary>

src/Arcus.Testing.Tests.Integration/Storage/TemporaryNoSqlContainerTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Arcus.Testing.Tests.Integration.Storage.Fixture;
55
using Bogus;
66
using Microsoft.Azure.Cosmos;
7-
using Microsoft.Extensions.Options;
87
using Newtonsoft.Json;
98
using Xunit;
109
using Xunit.Abstractions;
@@ -164,12 +163,13 @@ public async Task CreateTempNoSqlContainerWithCleanMatchingOnTeardown_WhenExisti
164163

165164
private static NoSqlItemFilter CreateMatchingFilter(Ship item)
166165
{
167-
return Bogus.Random.Int(1, 4) switch
166+
return Bogus.Random.Int(1, 5) switch
168167
{
169-
1 => NoSqlItemFilter.ItemIdEqual(item.Id),
170-
2 => NoSqlItemFilter.ItemIdEqual(item.Id, StringComparison.OrdinalIgnoreCase),
168+
1 => NoSqlItemFilter.IdEqual(item.Id),
169+
2 => NoSqlItemFilter.IdEqual(item.Id, StringComparison.OrdinalIgnoreCase),
171170
3 => NoSqlItemFilter.PartitionKeyEqual(item.GetPartitionKey()),
172-
4 => NoSqlItemFilter.ItemEqual<Ship>(x => x.BoatName == item.BoatName),
171+
4 => NoSqlItemFilter.Where<Ship>(x => x.BoatName == item.BoatName),
172+
5 => NoSqlItemFilter.Where(x => x["BoatName"].ToString() == item.BoatName),
173173
_ => throw new ArgumentOutOfRangeException(nameof(item), "Unknown filter type")
174174
};
175175
}

src/Arcus.Testing.Tests.Unit/Storage/NoSqlItemFilterTests.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,27 @@ public class NoSqlItemFilterTests
1313
[ClassData(typeof(Blanks))]
1414
public void CreateFilter_WithoutItemId_Fails(string itemId)
1515
{
16-
Assert.ThrowsAny<ArgumentException>(() => NoSqlItemFilter.ItemIdEqual(itemId));
16+
Assert.ThrowsAny<ArgumentException>(() => NoSqlItemFilter.IdEqual(itemId));
1717
}
1818

1919
[Theory]
2020
[ClassData(typeof(Blanks))]
2121
public void CreateFilter_WithComparisonWithoutItemId_Fails(string itemId)
2222
{
2323
Assert.ThrowsAny<ArgumentException>(
24-
() => NoSqlItemFilter.ItemIdEqual(itemId, Bogus.PickRandom<StringComparison>()));
24+
() => NoSqlItemFilter.IdEqual(itemId, Bogus.PickRandom<StringComparison>()));
25+
}
26+
27+
[Fact]
28+
public void CreateFilter_WithoutItemTFilter_Fails()
29+
{
30+
Assert.ThrowsAny<ArgumentException>(() => NoSqlItemFilter.Where<Shipment>(null));
2531
}
2632

2733
[Fact]
2834
public void CreateFilter_WithoutItemFilter_Fails()
2935
{
30-
Assert.ThrowsAny<ArgumentException>(() => NoSqlItemFilter.ItemEqual<Shipment>(null));
36+
Assert.ThrowsAny<ArgumentException>(() => NoSqlItemFilter.Where(null));
3137
}
3238
}
3339
}

src/Arcus.Testing.Tests.Unit/Storage/TemporaryNoSqlContainerOptionsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public void OnSetup_WithNullFilter_Fails()
1313

1414
// Act / Assert
1515
Assert.ThrowsAny<ArgumentException>(() => options.OnSetup.CleanMatchingItems(null));
16-
Assert.ThrowsAny<ArgumentException>(() => options.OnSetup.CleanMatchingItems(NoSqlItemFilter.ItemIdEqual("some-id"), null));
16+
Assert.ThrowsAny<ArgumentException>(() => options.OnSetup.CleanMatchingItems(NoSqlItemFilter.IdEqual("some-id"), null));
1717
}
1818

1919
[Fact]
@@ -24,7 +24,7 @@ public void OnTeardown_WithNullFilter_Fails()
2424

2525
// Act / Assert
2626
Assert.ThrowsAny<ArgumentException>(() => options.OnTeardown.CleanMatchingItems(null));
27-
Assert.ThrowsAny<ArgumentException>(() => options.OnTeardown.CleanMatchingItems(NoSqlItemFilter.ItemIdEqual("some-id"), null));
27+
Assert.ThrowsAny<ArgumentException>(() => options.OnTeardown.CleanMatchingItems(NoSqlItemFilter.IdEqual("some-id"), null));
2828
}
2929
}
3030
}

0 commit comments

Comments
 (0)