Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
tst
  • Loading branch information
StefH committed Nov 15, 2025
commit bee9dea075b9638bad4814df06538980c431881c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq.Dynamic.Core.NewtonsoftJson.Config;
using System.Linq.Dynamic.Core.Exceptions;
using System.Linq.Dynamic.Core.NewtonsoftJson.Config;
using FluentAssertions;
using Newtonsoft.Json.Linq;
using Xunit;
Expand Down Expand Up @@ -508,32 +509,6 @@ public void Where_With_Select()
first.Value<string>().Should().Be("Doe");
}

[Fact]
public void Where_OptionalProperty()
{
// Arrange
var array =
"""
[
{
"Name": "John",
"Age": 30
},
{
"Name": "Doe"
}
]
""";

// Act
var result = JArray.Parse(array).Where("Age >= 30").Select("Name");

// Assert
result.Should().HaveCount(1);
var first = result.First();
first.Value<string>().Should().Be("John");
}

[Theory]
[InlineData("notExisting == true")]
[InlineData("notExisting == \"true\"")]
Expand Down Expand Up @@ -561,4 +536,37 @@ public void Where_NonExistingMember_EmptyResult(string predicate)
// Assert
result.Should().BeEmpty();
}

[Theory]
[InlineData("""[ { "Name": "John", "Age": 30 }, { "Name": "Doe" }, { } ]""")]
[InlineData("""[ { "Name": "Doe" }, { "Name": "John", "Age": 30 }, { } ]""")]
public void NormalizeArray(string array)
{
// Act
var result = JArray.Parse(array)
.Where("Age >= 30")
.Select("Name");

// Assert
result.Should().HaveCount(1);
var first = result.First();
first.Value<string>().Should().Be("John");
}

[Fact]
public void NormalizeArray_When_NormalizeIsFalse_ShouldThrow()
{
// Arrange
var config = new NewtonsoftJsonParsingConfig
{
Normalize = false
};
var array = """[ { "Name": "Doe" }, { "Name": "John", "Age": 30 }, { } ]""";

// Act
Action act = () => JArray.Parse(array).Where(config, "Age >= 30");

// Assert
act.Should().Throw<InvalidOperationException>().WithMessage("The binary operator GreaterThanOrEqual is not defined for the types 'System.Object' and 'System.Int32'.");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Linq.Dynamic.Core.SystemTextJson.Config;
using System.Text.Json;
using FluentAssertions;
using Newtonsoft.Json.Linq;
using Xunit;

namespace System.Linq.Dynamic.Core.SystemTextJson.Tests;
Expand Down Expand Up @@ -538,34 +537,6 @@ public void Where_With_Select()
array.First().GetString().Should().Be("Doe");
}

[Fact]
public void Where_OptionalProperty()
{
// Arrange
var data =
"""
[
{
"Name": "John",
"Age": 30
},
{
"Name": "Doe"
}
]
""";
var source = JsonDocument.Parse(data);

// Act
var result = source.Where("Age >= 30").Select("Name");

// Assert
var array = result.RootElement.EnumerateArray();
array.Should().HaveCount(1);
var first = result.First();
array.First().GetString().Should().Be("John");
}

[Theory]
[InlineData("notExisting == true")]
[InlineData("notExisting == \"true\"")]
Expand All @@ -580,17 +551,49 @@ public void Where_OptionalProperty()
[InlineData("\"something\" == notExisting")]
[InlineData("1 < notExisting")]
public void Where_NonExistingMember_EmptyResult(string predicate)
{
// Act
var result = _source.Where(predicate).RootElement.EnumerateArray();

// Assert
result.Should().BeEmpty();
}

[Theory]
[InlineData("""[ { "Name": "John", "Age": 30 }, { "Name": "Doe" }, { } ]""")]
[InlineData("""[ { "Name": "Doe" }, { "Name": "John", "Age": 30 }, { } ]""")]
public void NormalizeArray(string data)
{
// Arrange
var source = JsonDocument.Parse(data);

// Act
var result = source
.Where("Age >= 30")
.Select("Name");

// Assert
var array = result.RootElement.EnumerateArray();
array.Should().HaveCount(1);
var first = result.First();
Copy link

Copilot AI Nov 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused variable: The variable first is declared but never used. This line should be removed or the variable should be used in an assertion.

Suggested change
var first = result.First();

Copilot uses AI. Check for mistakes.
array.First().GetString().Should().Be("John");
}

[Fact]
public void NormalizeArray_When_NormalizeIsFalse_ShouldThrow()
{
// Arrange
var config = new SystemTextJsonParsingConfig
{
ConvertObjectToSupportComparison = true
Normalize = false
};
var data = """[ { "Name": "Doe" }, { "Name": "John", "Age": 30 }, { } ]""";
var array = JsonDocument.Parse(data);

// Act
var result = _source.Where(config, predicate).RootElement.EnumerateArray();
Action act = () => JsonDocument.Parse(data).Where(config, "Age >= 30");

// Assert
result.Should().BeEmpty();
act.Should().Throw<InvalidOperationException>().WithMessage("Unable to find property 'Age' on type '<>f__AnonymousType*");
Copy link

Copilot AI Nov 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wildcard pattern in the error message assertion ('<>f__AnonymousType*) is fragile. Consider using .WithMessage("*Unable to find property 'Age'*") or a more specific pattern that doesn't rely on the anonymous type name format.

Suggested change
act.Should().Throw<InvalidOperationException>().WithMessage("Unable to find property 'Age' on type '<>f__AnonymousType*");
act.Should().Throw<InvalidOperationException>().WithMessage("*Unable to find property 'Age'*");

Copilot uses AI. Check for mistakes.
}
}
Loading