Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add nosql item filter dic overload #222

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
c07d3e9
pr-fix: correct merge w/ 'main'
stijnmoreels Aug 2, 2024
6b26d6d
Merge branch 'main' of https://github.com/stijnmoreels/arcus.testing
stijnmoreels Aug 2, 2024
0de9e56
Merge remote-tracking branch 'upstream/main'
stijnmoreels Aug 26, 2024
cbfd3d9
Merge remote-tracking branch 'upstream/main'
stijnmoreels Sep 6, 2024
cc62c4b
Merge remote-tracking branch 'upstream/main'
stijnmoreels Sep 13, 2024
7ad43ef
Merge remote-tracking branch 'upstream/main'
stijnmoreels Sep 23, 2024
483f449
Merge remote-tracking branch 'upstream/main'
stijnmoreels Sep 27, 2024
b496a00
Merge remote-tracking branch 'upstream/main'
stijnmoreels Oct 10, 2024
be1dc25
Merge remote-tracking branch 'upstream/main'
stijnmoreels Oct 18, 2024
6968cb5
Merge remote-tracking branch 'upstream/main'
stijnmoreels Oct 18, 2024
b0b1e6d
Merge remote-tracking branch 'upstream/main'
stijnmoreels Oct 18, 2024
6080add
Merge remote-tracking branch 'upstream/main'
stijnmoreels Oct 23, 2024
42cdd3d
Merge remote-tracking branch 'upstream/main'
stijnmoreels Oct 28, 2024
3c67e9a
Merge remote-tracking branch 'upstream/main'
stijnmoreels Oct 28, 2024
c25ca9c
Merge remote-tracking branch 'upstream/main'
stijnmoreels Oct 29, 2024
b94ed50
chore: eat your own logging dog food
stijnmoreels Nov 1, 2024
f00adf4
Merge remote-tracking branch 'upstream/main'
stijnmoreels Nov 1, 2024
b1b7b47
feat: add nosql item filter dictionary overload
stijnmoreels Nov 6, 2024
ed226a9
Merge branch 'main' into feature/add-nosql-item-filter-dic-overload
stijnmoreels Nov 6, 2024
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
6 changes: 3 additions & 3 deletions docs/preview/02-Features/04-Storage/02-cosmos.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand Down
25 changes: 21 additions & 4 deletions src/Arcus.Testing.Storage.Cosmos/TemporaryNoSqlContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private NoSqlItemFilter(Func<string, PartitionKey, JObject, CosmosClient, bool>
/// </summary>
/// <param name="itemId">The unique required 'id' value.</param>
/// <exception cref="ArgumentException">Thrown when the <paramref name="itemId"/> is blank.</exception>
public static NoSqlItemFilter ItemIdEqual(string itemId)
public static NoSqlItemFilter IdEqual(string itemId)
{
if (string.IsNullOrWhiteSpace(itemId))
{
Expand All @@ -64,7 +64,7 @@ public static NoSqlItemFilter ItemIdEqual(string itemId)
/// <param name="itemId">The unique required 'id' value.</param>
/// <param name="comparisonType">The value that specifies how the strings will be compared.</param>
/// <exception cref="ArgumentException">Thrown when the <paramref name="itemId"/> is blank.</exception>
public static NoSqlItemFilter ItemIdEqual(string itemId, StringComparison comparisonType)
public static NoSqlItemFilter IdEqual(string itemId, StringComparison comparisonType)
{
if (string.IsNullOrWhiteSpace(itemId))
{
Expand All @@ -86,11 +86,11 @@ public static NoSqlItemFilter PartitionKeyEqual(PartitionKey partitionKey)
/// <summary>
/// Creates a filter to match a NoSql item based on its contents.
/// </summary>
/// <typeparam name="TItem">The custom type </typeparam>
/// <typeparam name="TItem">The custom type of the NoSql item.</typeparam>
/// <param name="itemFilter">The custom filter to match against the contents of the NoSql item.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="itemFilter"/> is <c>null</c>.</exception>
/// <exception cref="InvalidOperationException">Thrown when the used client has no serializer configured.</exception>
public static NoSqlItemFilter ItemEqual<TItem>(Func<TItem, bool> itemFilter)
public static NoSqlItemFilter Where<TItem>(Func<TItem, bool> itemFilter)
{
ArgumentNullException.ThrowIfNull(itemFilter);

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

/// <summary>
/// Creates a filter to match a NoSql item based on its contents.
/// </summary>
/// <param name="itemFilter">The custom filter to match against the contents of the NoSql item.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="itemFilter"/> is <c>null</c>.</exception>
/// <exception cref="InvalidOperationException">Thrown when the used client has no serializer configured.</exception>
public static NoSqlItemFilter Where(Func<IDictionary<string, object>, bool> itemFilter)
{
ArgumentNullException.ThrowIfNull(itemFilter);

return new NoSqlItemFilter((_, _, json, _) =>
{
var dic = json.ToObject<Dictionary<string, object>>();
return itemFilter(dic);
});
}

/// <summary>
/// Match the current NoSql item with the user configured filter.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Ship>(x => x.BoatName == item.BoatName),
4 => NoSqlItemFilter.Where<Ship>(x => x.BoatName == item.BoatName),
5 => NoSqlItemFilter.Where(x => x["BoatName"].ToString() == item.BoatName),
_ => throw new ArgumentOutOfRangeException(nameof(item), "Unknown filter type")
};
}
Expand Down
12 changes: 9 additions & 3 deletions src/Arcus.Testing.Tests.Unit/Storage/NoSqlItemFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,27 @@ public class NoSqlItemFilterTests
[ClassData(typeof(Blanks))]
public void CreateFilter_WithoutItemId_Fails(string itemId)
{
Assert.ThrowsAny<ArgumentException>(() => NoSqlItemFilter.ItemIdEqual(itemId));
Assert.ThrowsAny<ArgumentException>(() => NoSqlItemFilter.IdEqual(itemId));
}

[Theory]
[ClassData(typeof(Blanks))]
public void CreateFilter_WithComparisonWithoutItemId_Fails(string itemId)
{
Assert.ThrowsAny<ArgumentException>(
() => NoSqlItemFilter.ItemIdEqual(itemId, Bogus.PickRandom<StringComparison>()));
() => NoSqlItemFilter.IdEqual(itemId, Bogus.PickRandom<StringComparison>()));
}

[Fact]
public void CreateFilter_WithoutItemTFilter_Fails()
{
Assert.ThrowsAny<ArgumentException>(() => NoSqlItemFilter.Where<Shipment>(null));
}

[Fact]
public void CreateFilter_WithoutItemFilter_Fails()
{
Assert.ThrowsAny<ArgumentException>(() => NoSqlItemFilter.ItemEqual<Shipment>(null));
Assert.ThrowsAny<ArgumentException>(() => NoSqlItemFilter.Where(null));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void OnSetup_WithNullFilter_Fails()

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

[Fact]
Expand All @@ -24,7 +24,7 @@ public void OnTeardown_WithNullFilter_Fails()

// Act / Assert
Assert.ThrowsAny<ArgumentException>(() => options.OnTeardown.CleanMatchingItems(null));
Assert.ThrowsAny<ArgumentException>(() => options.OnTeardown.CleanMatchingItems(NoSqlItemFilter.ItemIdEqual("some-id"), null));
Assert.ThrowsAny<ArgumentException>(() => options.OnTeardown.CleanMatchingItems(NoSqlItemFilter.IdEqual("some-id"), null));
}
}
}
Loading