Skip to content

Commit bee9dea

Browse files
committed
tst
1 parent 812e0ce commit bee9dea

File tree

2 files changed

+70
-59
lines changed

2 files changed

+70
-59
lines changed

test/System.Linq.Dynamic.Core.NewtonsoftJson.Tests/NewtonsoftJsonTests.cs

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq.Dynamic.Core.NewtonsoftJson.Config;
1+
using System.Linq.Dynamic.Core.Exceptions;
2+
using System.Linq.Dynamic.Core.NewtonsoftJson.Config;
23
using FluentAssertions;
34
using Newtonsoft.Json.Linq;
45
using Xunit;
@@ -508,32 +509,6 @@ public void Where_With_Select()
508509
first.Value<string>().Should().Be("Doe");
509510
}
510511

511-
[Fact]
512-
public void Where_OptionalProperty()
513-
{
514-
// Arrange
515-
var array =
516-
"""
517-
[
518-
{
519-
"Name": "John",
520-
"Age": 30
521-
},
522-
{
523-
"Name": "Doe"
524-
}
525-
]
526-
""";
527-
528-
// Act
529-
var result = JArray.Parse(array).Where("Age >= 30").Select("Name");
530-
531-
// Assert
532-
result.Should().HaveCount(1);
533-
var first = result.First();
534-
first.Value<string>().Should().Be("John");
535-
}
536-
537512
[Theory]
538513
[InlineData("notExisting == true")]
539514
[InlineData("notExisting == \"true\"")]
@@ -561,4 +536,37 @@ public void Where_NonExistingMember_EmptyResult(string predicate)
561536
// Assert
562537
result.Should().BeEmpty();
563538
}
539+
540+
[Theory]
541+
[InlineData("""[ { "Name": "John", "Age": 30 }, { "Name": "Doe" }, { } ]""")]
542+
[InlineData("""[ { "Name": "Doe" }, { "Name": "John", "Age": 30 }, { } ]""")]
543+
public void NormalizeArray(string array)
544+
{
545+
// Act
546+
var result = JArray.Parse(array)
547+
.Where("Age >= 30")
548+
.Select("Name");
549+
550+
// Assert
551+
result.Should().HaveCount(1);
552+
var first = result.First();
553+
first.Value<string>().Should().Be("John");
554+
}
555+
556+
[Fact]
557+
public void NormalizeArray_When_NormalizeIsFalse_ShouldThrow()
558+
{
559+
// Arrange
560+
var config = new NewtonsoftJsonParsingConfig
561+
{
562+
Normalize = false
563+
};
564+
var array = """[ { "Name": "Doe" }, { "Name": "John", "Age": 30 }, { } ]""";
565+
566+
// Act
567+
Action act = () => JArray.Parse(array).Where(config, "Age >= 30");
568+
569+
// Assert
570+
act.Should().Throw<InvalidOperationException>().WithMessage("The binary operator GreaterThanOrEqual is not defined for the types 'System.Object' and 'System.Int32'.");
571+
}
564572
}

test/System.Linq.Dynamic.Core.SystemTextJson.Tests/SystemTextJsonTests.cs

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Linq.Dynamic.Core.SystemTextJson.Config;
22
using System.Text.Json;
33
using FluentAssertions;
4-
using Newtonsoft.Json.Linq;
54
using Xunit;
65

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

541-
[Fact]
542-
public void Where_OptionalProperty()
543-
{
544-
// Arrange
545-
var data =
546-
"""
547-
[
548-
{
549-
"Name": "John",
550-
"Age": 30
551-
},
552-
{
553-
"Name": "Doe"
554-
}
555-
]
556-
""";
557-
var source = JsonDocument.Parse(data);
558-
559-
// Act
560-
var result = source.Where("Age >= 30").Select("Name");
561-
562-
// Assert
563-
var array = result.RootElement.EnumerateArray();
564-
array.Should().HaveCount(1);
565-
var first = result.First();
566-
array.First().GetString().Should().Be("John");
567-
}
568-
569540
[Theory]
570541
[InlineData("notExisting == true")]
571542
[InlineData("notExisting == \"true\"")]
@@ -580,17 +551,49 @@ public void Where_OptionalProperty()
580551
[InlineData("\"something\" == notExisting")]
581552
[InlineData("1 < notExisting")]
582553
public void Where_NonExistingMember_EmptyResult(string predicate)
554+
{
555+
// Act
556+
var result = _source.Where(predicate).RootElement.EnumerateArray();
557+
558+
// Assert
559+
result.Should().BeEmpty();
560+
}
561+
562+
[Theory]
563+
[InlineData("""[ { "Name": "John", "Age": 30 }, { "Name": "Doe" }, { } ]""")]
564+
[InlineData("""[ { "Name": "Doe" }, { "Name": "John", "Age": 30 }, { } ]""")]
565+
public void NormalizeArray(string data)
566+
{
567+
// Arrange
568+
var source = JsonDocument.Parse(data);
569+
570+
// Act
571+
var result = source
572+
.Where("Age >= 30")
573+
.Select("Name");
574+
575+
// Assert
576+
var array = result.RootElement.EnumerateArray();
577+
array.Should().HaveCount(1);
578+
var first = result.First();
579+
array.First().GetString().Should().Be("John");
580+
}
581+
582+
[Fact]
583+
public void NormalizeArray_When_NormalizeIsFalse_ShouldThrow()
583584
{
584585
// Arrange
585586
var config = new SystemTextJsonParsingConfig
586587
{
587-
ConvertObjectToSupportComparison = true
588+
Normalize = false
588589
};
590+
var data = """[ { "Name": "Doe" }, { "Name": "John", "Age": 30 }, { } ]""";
591+
var array = JsonDocument.Parse(data);
589592

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

593596
// Assert
594-
result.Should().BeEmpty();
597+
act.Should().Throw<InvalidOperationException>().WithMessage("Unable to find property 'Age' on type '<>f__AnonymousType*");
595598
}
596599
}

0 commit comments

Comments
 (0)