From b3c597a70d61b1a207305e9724f9f0d25e99ae25 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Tue, 9 Apr 2024 11:23:37 +0200 Subject: [PATCH] where-select tests --- .../SystemTextJsonExtensions.cs | 14 ++++- .../NewtonsoftJsonTests.cs | 51 ++++++++++++++++++ .../SystemTextJsonTests.cs | 52 ++++++++++++++++++- 3 files changed, 114 insertions(+), 3 deletions(-) diff --git a/src/System.Linq.Dynamic.Core.SystemTextJson/SystemTextJsonExtensions.cs b/src/System.Linq.Dynamic.Core.SystemTextJson/SystemTextJsonExtensions.cs index 7ff11279..fdd769e3 100644 --- a/src/System.Linq.Dynamic.Core.SystemTextJson/SystemTextJsonExtensions.cs +++ b/src/System.Linq.Dynamic.Core.SystemTextJson/SystemTextJsonExtensions.cs @@ -140,7 +140,7 @@ public static JsonDocument Select(this JsonDocument source, string selector, par /// The source /// The . /// A projection string expression to apply to each element. - /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. + /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// An whose elements are the result of invoking a projection string on each element of source. public static JsonDocument Select(this JsonDocument source, SystemTextJsonParsingConfig config, string selector, params object?[] args) { @@ -191,7 +191,17 @@ private static JsonDocument ToJsonDocumentArray(Func func) var array = new List(); foreach (var dynamicElement in func()) { - var element = dynamicElement is DynamicClass dynamicClass ? JsonElementUtils.FromObject(dynamicClass) : dynamicElement; + object element; + if (dynamicElement is DynamicClass dynamicClass) + { + element = JsonElementUtils.FromObject(dynamicClass); + } + else + { + element = dynamicElement; + } + + //var element = dynamicElement is DynamicClass dynamicClass ? JsonElementUtils.FromObject(dynamicClass) : dynamicElement; array.Add(element); } diff --git a/test/System.Linq.Dynamic.Core.NewtonsoftJson.Tests/NewtonsoftJsonTests.cs b/test/System.Linq.Dynamic.Core.NewtonsoftJson.Tests/NewtonsoftJsonTests.cs index aa5158c5..978f165c 100644 --- a/test/System.Linq.Dynamic.Core.NewtonsoftJson.Tests/NewtonsoftJsonTests.cs +++ b/test/System.Linq.Dynamic.Core.NewtonsoftJson.Tests/NewtonsoftJsonTests.cs @@ -77,4 +77,55 @@ public void Any() // Assert result.Should().BeTrue(); } + + [Fact] + public void Select() + { + // Arrange + var json = @"[ + { + ""Name"": ""John"", + ""Age"": 30 + }, + { + ""Name"": ""Doe"", + ""Age"": 25 + } + ]"; + + var doc = JArray.Parse(json); + + // Act + var result = doc.Select("Name"); + + // Assert + var array = result.Select(x => x.Value()); + array.Should().BeEquivalentTo("John", "Doe"); + } + + [Fact] + public void Where_Select() + { + // Arrange + var json = @"[ + { + ""Name"": ""John"", + ""Age"": 30 + }, + { + ""Name"": ""Doe"", + ""Age"": 25 + } + ]"; + + var doc = JArray.Parse(json); + + // Act + var result = doc.Where("Age > 25").Select("Name"); + + // Assert + result.Should().HaveCount(1); + var first = result.First(); + first.Value().Should().Be("John"); + } } \ No newline at end of file diff --git a/test/System.Linq.Dynamic.Core.SystemTextJson.Tests/SystemTextJsonTests.cs b/test/System.Linq.Dynamic.Core.SystemTextJson.Tests/SystemTextJsonTests.cs index 5a5d80ea..7a7534d0 100644 --- a/test/System.Linq.Dynamic.Core.SystemTextJson.Tests/SystemTextJsonTests.cs +++ b/test/System.Linq.Dynamic.Core.SystemTextJson.Tests/SystemTextJsonTests.cs @@ -1,6 +1,5 @@ using System.Text.Json; using FluentAssertions; -using Newtonsoft.Json.Linq; using Xunit; namespace System.Linq.Dynamic.Core.SystemTextJson.Tests; @@ -78,4 +77,55 @@ public void Any() // Assert result.Should().BeTrue(); } + + [Fact] + public void Select() + { + // Arrange + var json = @"[ + { + ""Name"": ""John"", + ""Age"": 30 + }, + { + ""Name"": ""Doe"", + ""Age"": 25 + } + ]"; + + var doc = JsonDocument.Parse(json); + + // Act + var result = doc.Select("Name"); + + // Assert + var array = result.RootElement.EnumerateArray().Select(x => x.GetString()); + array.Should().BeEquivalentTo("John", "Doe"); + } + + [Fact] + public void Where_Select() + { + // Arrange + var json = @"[ + { + ""Name"": ""John"", + ""Age"": 30 + }, + { + ""Name"": ""Doe"", + ""Age"": 25 + } + ]"; + + var doc = JsonDocument.Parse(json); + + // Act + var result = doc.Where("Age > 25").Select("Name"); + + // Assert + var array = result.RootElement.EnumerateArray(); + array.Should().HaveCount(1); + array.First().GetString().Should().Be("John"); + } } \ No newline at end of file