Skip to content

Commit 56c1f9f

Browse files
author
Adrian Hall
committed
(#199) Change the nextlink comparison to use QueryHelpers
1 parent 80b1ce8 commit 56c1f9f

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

tests/CommunityToolkit.Datasync.Server.Test/Helpers/LiveControllerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using CommunityToolkit.Datasync.TestCommon;
56
using Microsoft.AspNetCore.Mvc;
67
using System.Net;
78

@@ -107,8 +108,7 @@ private async Task MovieQueryTest(string pathAndQuery, int itemCount, string nex
107108

108109
if (nextLinkQuery is not null)
109110
{
110-
result.NextLink.Should().NotBeNull();
111-
Uri.UnescapeDataString(result.NextLink).Should().Be(nextLinkQuery);
111+
result.NextLink.Should().NotBeNull().And.MatchQueryString(nextLinkQuery);
112112
}
113113
else
114114
{

tests/CommunityToolkit.Datasync.Server.Test/Service/Query_Tests.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3935,13 +3935,12 @@ private async Task MovieQueryTest(string pathAndQuery, int itemCount, string nex
39353935
PageOfItems<ClientMovie> result = JsonSerializer.Deserialize<PageOfItems<ClientMovie>>(content, this.serializerOptions);
39363936

39373937
// Payload has the right content
3938-
Assert.Equal(itemCount, result!.Items!.Length);
3939-
Assert.Equal(nextLinkQuery, result.NextLink == null ? null : Uri.UnescapeDataString(result.NextLink));
3940-
Assert.Equal(totalCount, result.Count);
3938+
result?.Items?.Length.Should().Be(itemCount);
3939+
result.NextLink.Should().MatchQueryString(nextLinkQuery);
3940+
result.Count.Should().Be(totalCount);
39413941

39423942
// The first n items must match what is expected
3943-
Assert.True(result.Items.Length >= firstItems.Length);
3944-
Assert.Equal(firstItems, result.Items.Take(firstItems.Length).Select(m => m.Id).ToArray());
3943+
result.Items.Take(firstItems.Length).Select(m => m.Id).ToList().Should().ContainInConsecutiveOrder(firstItems);
39453944
for (int idx = 0; idx < firstItems.Length; idx++)
39463945
{
39473946
InMemoryMovie expected = this.factory.GetServerEntityById<InMemoryMovie>(firstItems[idx])!;

tests/CommunityToolkit.Datasync.TestCommon/FluentExtensions/StringAssertions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using FluentAssertions.Execution;
66
using FluentAssertions.Primitives;
77
using FluentAssertions;
8+
using Microsoft.AspNetCore.WebUtilities;
9+
using Microsoft.Extensions.Primitives;
810

911
namespace CommunityToolkit.Datasync.TestCommon;
1012

@@ -22,4 +24,18 @@ public static AndConstraint<StringAssertions> BeAGuid(this StringAssertions curr
2224
.FailWith("Expected object to be a Guid, but found {0}", current.Subject);
2325
return new AndConstraint<StringAssertions>(current);
2426
}
27+
28+
public static AndConstraint<StringAssertions> MatchQueryString(this StringAssertions current, string queryString, string because = "", params object[] becauseArgs)
29+
{
30+
Dictionary<string, StringValues> q1 = QueryHelpers.ParseNullableQuery(queryString) ?? [];
31+
Dictionary<string, StringValues> q2 = QueryHelpers.ParseNullableQuery(current.Subject) ?? [];
32+
bool isEquivalent = q1.Count == q2.Count && !q1.Except(q2).Any();
33+
34+
Execute.Assertion
35+
.BecauseOf(because, becauseArgs)
36+
.ForCondition(isEquivalent)
37+
.FailWith("Expected query string to match '{0}', but found '{1}'", queryString, current.Subject);
38+
return new AndConstraint<StringAssertions>(current);
39+
40+
}
2541
}

0 commit comments

Comments
 (0)