-
-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New feature: GroupJoin #78
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
test/System.Linq.Dynamic.Core.Tests/QueryableTests.GroupJoin.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
using System.Collections.Generic; | ||
using System.Linq.Dynamic.Core.Exceptions; | ||
using System.Linq.Dynamic.Core.Tests.Helpers.Models; | ||
using NFluent; | ||
using Xunit; | ||
|
||
namespace System.Linq.Dynamic.Core.Tests | ||
{ | ||
public partial class QueryableTests | ||
{ | ||
[Fact] | ||
public void GroupJoin() | ||
{ | ||
//Arrange | ||
Person magnus = new Person { Name = "Hedlund, Magnus" }; | ||
Person terry = new Person { Name = "Adams, Terry" }; | ||
Person charlotte = new Person { Name = "Weiss, Charlotte" }; | ||
|
||
Pet barley = new Pet { Name = "Barley", Owner = terry }; | ||
Pet boots = new Pet { Name = "Boots", Owner = terry }; | ||
Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte }; | ||
Pet daisy = new Pet { Name = "Daisy", Owner = magnus }; | ||
|
||
var people = new List<Person> { magnus, terry, charlotte }; | ||
var petsList = new List<Pet> { barley, boots, whiskers, daisy }; | ||
|
||
//Act | ||
var realQuery = people.AsQueryable().GroupJoin( | ||
petsList, | ||
person => person, | ||
pet => pet.Owner, | ||
(person, pets) => new { OwnerName = person.Name, Pets = pets }); | ||
|
||
var dynamicQuery = people.AsQueryable().GroupJoin( | ||
petsList, | ||
"it", | ||
"Owner", | ||
"new(outer.Name as OwnerName, inner as Pets)"); | ||
|
||
//Assert | ||
var realResult = realQuery.ToArray(); | ||
|
||
#if NETSTANDARD | ||
var dynamicResult = dynamicQuery.ToDynamicArray<DynamicClass>(); | ||
|
||
Assert.Equal(realResult.Length, dynamicResult.Length); | ||
for (int i = 0; i < realResult.Length; i++) | ||
{ | ||
Assert.Equal(realResult[i].OwnerName, dynamicResult[i].GetDynamicPropertyValue<string>("OwnerName")); | ||
for (int j = 0; j < realResult[i].Pets.Count(); j++) | ||
{ | ||
Assert.Equal(realResult[i].Pets.ElementAt(j).Name, dynamicResult[i].GetDynamicPropertyValue<IEnumerable<Pet>>("Pets").ElementAt(j).Name); | ||
} | ||
} | ||
#else | ||
var dynamicResult = dynamicQuery.ToDynamicArray(); | ||
|
||
Assert.Equal(realResult.Length, dynamicResult.Length); | ||
for (int i = 0; i < realResult.Length; i++) | ||
{ | ||
Assert.Equal(realResult[i].OwnerName, ((dynamic) dynamicResult[i]).OwnerName); | ||
for (int j = 0; j < realResult[i].Pets.Count(); j++) | ||
{ | ||
Assert.Equal(realResult[i].Pets.ElementAt(j).Name, (((IEnumerable<Pet>)((dynamic)dynamicResult[i]).Pets)).ElementAt(j).Name); | ||
} | ||
} | ||
#endif | ||
} | ||
|
||
[Fact] | ||
public void GroupJoinOnNullableType_RightNullable() | ||
{ | ||
//Arrange | ||
Person magnus = new Person { Id = 1, Name = "Hedlund, Magnus" }; | ||
Person terry = new Person { Id = 2, Name = "Adams, Terry" }; | ||
Person charlotte = new Person { Id = 3, Name = "Weiss, Charlotte" }; | ||
|
||
Pet barley = new Pet { Name = "Barley", NullableOwnerId = terry.Id }; | ||
Pet boots = new Pet { Name = "Boots", NullableOwnerId = terry.Id }; | ||
Pet whiskers = new Pet { Name = "Whiskers", NullableOwnerId = charlotte.Id }; | ||
Pet daisy = new Pet { Name = "Daisy", NullableOwnerId = magnus.Id }; | ||
|
||
var people = new List<Person> { magnus, terry, charlotte }; | ||
var petsList = new List<Pet> { barley, boots, whiskers, daisy }; | ||
|
||
//Act | ||
var realQuery = people.AsQueryable().GroupJoin( | ||
petsList, | ||
person => person.Id, | ||
pet => pet.NullableOwnerId, | ||
(person, pets) => new { OwnerName = person.Name, Pets = pets }); | ||
|
||
var dynamicQuery = people.AsQueryable().GroupJoin( | ||
petsList, | ||
"it.Id", | ||
"NullableOwnerId", | ||
"new(outer.Name as OwnerName, inner as Pets)"); | ||
|
||
//Assert | ||
var realResult = realQuery.ToArray(); | ||
var dynamicResult = dynamicQuery.ToDynamicArray<DynamicClass>(); | ||
|
||
Assert.Equal(realResult.Length, dynamicResult.Length); | ||
for (int i = 0; i < realResult.Length; i++) | ||
{ | ||
Assert.Equal(realResult[i].OwnerName, dynamicResult[i].GetDynamicPropertyValue<string>("OwnerName")); | ||
for (int j = 0; j < realResult[i].Pets.Count(); j++) | ||
{ | ||
Assert.Equal(realResult[i].Pets.ElementAt(j).Name, dynamicResult[i].GetDynamicPropertyValue<IEnumerable<Pet>>("Pets").ElementAt(j).Name); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public void GroupJoinOnNullableType_LeftNullable() | ||
{ | ||
//Arrange | ||
Person magnus = new Person { NullableId = 1, Name = "Hedlund, Magnus" }; | ||
Person terry = new Person { NullableId = 2, Name = "Adams, Terry" }; | ||
Person charlotte = new Person { NullableId = 3, Name = "Weiss, Charlotte" }; | ||
|
||
Pet barley = new Pet { Name = "Barley", OwnerId = terry.Id }; | ||
Pet boots = new Pet { Name = "Boots", OwnerId = terry.Id }; | ||
Pet whiskers = new Pet { Name = "Whiskers", OwnerId = charlotte.Id }; | ||
Pet daisy = new Pet { Name = "Daisy", OwnerId = magnus.Id }; | ||
|
||
var people = new List<Person> { magnus, terry, charlotte }; | ||
var petsList = new List<Pet> { barley, boots, whiskers, daisy }; | ||
|
||
//Act | ||
var realQuery = people.AsQueryable().GroupJoin( | ||
petsList, | ||
person => person.NullableId, | ||
pet => pet.OwnerId, | ||
(person, pets) => new { OwnerName = person.Name, Pets = pets }); | ||
|
||
var dynamicQuery = people.AsQueryable().GroupJoin( | ||
petsList, | ||
"it.NullableId", | ||
"OwnerId", | ||
"new(outer.Name as OwnerName, inner as Pets)"); | ||
|
||
//Assert | ||
var realResult = realQuery.ToArray(); | ||
var dynamicResult = dynamicQuery.ToDynamicArray<DynamicClass>(); | ||
|
||
Assert.Equal(realResult.Length, dynamicResult.Length); | ||
for (int i = 0; i < realResult.Length; i++) | ||
{ | ||
Assert.Equal(realResult[i].OwnerName, dynamicResult[i].GetDynamicPropertyValue<string>("OwnerName")); | ||
for (int j = 0; j < realResult[i].Pets.Count(); j++) | ||
{ | ||
Assert.Equal(realResult[i].Pets.ElementAt(j).Name, dynamicResult[i].GetDynamicPropertyValue<IEnumerable<Pet>>("Pets").ElementAt(j).Name); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public void GroupJoinOnNullableType_NotSameTypesThrowsException() | ||
{ | ||
var person = new Person { Id = 1, Name = "Hedlund, Magnus" }; | ||
var people = new List<Person> { person }; | ||
var pets = new List<Pet> { new Pet { Name = "Daisy", OwnerId = person.Id } }; | ||
|
||
Check.ThatCode(() => | ||
people.AsQueryable() | ||
.GroupJoin( | ||
pets, | ||
"it.Id", | ||
"Name", // This is wrong | ||
"new(outer.Name as OwnerName, inner as Pets)")).Throws<ParseException>(); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also add a negative test (throws exception) like https://github.com/StefH/System.Linq.Dynamic.Core/blob/master/test/System.Linq.Dynamic.Core.Tests/QueryableTests.Join.cs#L153
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test added.