Skip to content

Commit

Permalink
where-select tests
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Apr 9, 2024
1 parent 08b5274 commit b3c597a
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static JsonDocument Select(this JsonDocument source, string selector, par
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="selector">A projection string expression to apply to each element.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>An <see cref="JsonElement"/> whose elements are the result of invoking a projection string on each element of source.</returns>
public static JsonDocument Select(this JsonDocument source, SystemTextJsonParsingConfig config, string selector, params object?[] args)
{
Expand Down Expand Up @@ -191,7 +191,17 @@ private static JsonDocument ToJsonDocumentArray(Func<IQueryable> func)
var array = new List<object>();
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>());
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<string>().Should().Be("John");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Text.Json;
using FluentAssertions;
using Newtonsoft.Json.Linq;
using Xunit;

namespace System.Linq.Dynamic.Core.SystemTextJson.Tests;
Expand Down Expand Up @@ -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");
}
}

0 comments on commit b3c597a

Please sign in to comment.