2018-03-21 21:00:46 -04:00
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
|
using PoweredSoft.DynamicLinq.Dal.Pocos;
|
2018-03-21 23:27:03 -04:00
|
|
|
|
using PoweredSoft.DynamicLinq.Test.Helpers;
|
2018-03-21 21:00:46 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace PoweredSoft.DynamicLinq.Test
|
|
|
|
|
{
|
2018-03-21 23:27:03 -04:00
|
|
|
|
public class Mock
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
public int ForeignId { get; set; }
|
|
|
|
|
public decimal Total { get; set; }
|
|
|
|
|
|
|
|
|
|
public List<MockB> Bs { get; set; } = new List<MockB>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class MockB
|
|
|
|
|
{
|
|
|
|
|
public List<string> FirstNames { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-21 21:00:46 -04:00
|
|
|
|
[TestClass]
|
|
|
|
|
public class SelectTests
|
|
|
|
|
{
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void TestSelect()
|
|
|
|
|
{
|
2018-03-21 23:27:03 -04:00
|
|
|
|
var list = new List<Mock>()
|
|
|
|
|
{
|
|
|
|
|
new Mock{
|
|
|
|
|
Id = 1,
|
|
|
|
|
ForeignId = 1,
|
|
|
|
|
Total = 100,
|
|
|
|
|
Bs = new List<MockB>() {
|
|
|
|
|
new MockB { FirstNames = new List<string>{"David", "John" } }
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var regularSyntaxA = list
|
2018-03-21 21:00:46 -04:00
|
|
|
|
.AsQueryable()
|
|
|
|
|
.Select(t => new
|
|
|
|
|
{
|
|
|
|
|
Id = t.Id,
|
2018-03-21 23:27:03 -04:00
|
|
|
|
FirstNames = t.Bs.SelectMany(t2 => t2.FirstNames).ToList(),
|
2018-10-25 22:01:44 -04:00
|
|
|
|
FirstNamesLists = t.Bs.Select(t2 => t2.FirstNames).ToList(),
|
|
|
|
|
FirstFirstName = t.Bs.SelectMany(t2 => t2.FirstNames).First(),
|
|
|
|
|
FirstOrDefaultFirstName = t.Bs.SelectMany(t2 => t2.FirstNames).FirstOrDefault(),
|
|
|
|
|
LastFirstName = t.Bs.SelectMany(t2 => t2.FirstNames).Last(),
|
|
|
|
|
LastOrDefaultFirstName = t.Bs.SelectMany(t2 => t2.FirstNames).LastOrDefault(),
|
|
|
|
|
|
|
|
|
|
FirstFirstNameList = t.Bs.Select(t2 => t2.FirstNames).First(),
|
|
|
|
|
FirstOrDefaultFirstNameList = t.Bs.Select(t2 => t2.FirstNames).FirstOrDefault(),
|
|
|
|
|
LastFirstNameList = t.Bs.Select(t2 => t2.FirstNames).Last(),
|
|
|
|
|
LastOrDefaultFirstNameList = t.Bs.Select(t2 => t2.FirstNames).LastOrDefault()
|
2018-03-21 23:15:45 -04:00
|
|
|
|
});
|
|
|
|
|
|
2018-03-21 23:27:03 -04:00
|
|
|
|
var regularSyntax = regularSyntaxA.ToList();
|
2018-03-21 21:00:46 -04:00
|
|
|
|
|
2018-03-21 23:27:03 -04:00
|
|
|
|
var dynamicSyntax = list
|
2018-03-21 21:00:46 -04:00
|
|
|
|
.AsQueryable()
|
|
|
|
|
.Select(t =>
|
2018-03-21 23:15:45 -04:00
|
|
|
|
{
|
|
|
|
|
t.Path("Id");
|
2018-10-25 22:01:44 -04:00
|
|
|
|
t.ToList("Bs.FirstNames", "FirstNames", SelectCollectionHandling.Flatten);
|
|
|
|
|
t.ToList("Bs.FirstNames", "FirstNamesLists", SelectCollectionHandling.LeaveAsIs);
|
|
|
|
|
t.First("Bs.FirstNames", "FirstFirstName", SelectCollectionHandling.Flatten);
|
|
|
|
|
t.FirstOrDefault("Bs.FirstNames", "FirstOrDefaultFirstName", SelectCollectionHandling.Flatten);
|
|
|
|
|
t.Last("Bs.FirstNames", "LastFirstName", SelectCollectionHandling.Flatten);
|
|
|
|
|
t.LastOrDefault("Bs.FirstNames", "LastOrDefaultFirstName", SelectCollectionHandling.Flatten);
|
|
|
|
|
|
|
|
|
|
t.First("Bs.FirstNames", "FirstFirstNameList", SelectCollectionHandling.LeaveAsIs);
|
|
|
|
|
t.FirstOrDefault("Bs.FirstNames", "FirstOrDefaultFirstNameList", SelectCollectionHandling.LeaveAsIs);
|
|
|
|
|
t.Last("Bs.FirstNames", "LastFirstNameList", SelectCollectionHandling.LeaveAsIs);
|
|
|
|
|
t.LastOrDefault("Bs.FirstNames", "LastOrDefaultFirstNameList", SelectCollectionHandling.LeaveAsIs);
|
2018-03-21 23:15:45 -04:00
|
|
|
|
})
|
|
|
|
|
.ToDynamicClassList();
|
2018-03-21 21:00:46 -04:00
|
|
|
|
|
|
|
|
|
Assert.AreEqual(regularSyntax.Count, dynamicSyntax.Count);
|
|
|
|
|
for(var i = 0; i < regularSyntax.Count; i++)
|
|
|
|
|
{
|
2018-03-21 23:27:03 -04:00
|
|
|
|
Assert.AreEqual(regularSyntax[i].Id, dynamicSyntax[i].GetDynamicPropertyValue<int>("Id"));
|
2018-10-25 22:01:44 -04:00
|
|
|
|
Assert.AreEqual(regularSyntax[i].FirstFirstName, dynamicSyntax[i].GetDynamicPropertyValue<string>("FirstFirstName"));
|
|
|
|
|
Assert.AreEqual(regularSyntax[i].FirstOrDefaultFirstName, dynamicSyntax[i].GetDynamicPropertyValue<string>("FirstOrDefaultFirstName"));
|
|
|
|
|
Assert.AreEqual(regularSyntax[i].LastFirstName, dynamicSyntax[i].GetDynamicPropertyValue<string>("LastFirstName"));
|
|
|
|
|
Assert.AreEqual(regularSyntax[i].LastOrDefaultFirstName, dynamicSyntax[i].GetDynamicPropertyValue<string>("LastOrDefaultFirstName"));
|
|
|
|
|
|
|
|
|
|
CollectionAssert.AreEqual(regularSyntax[i].FirstFirstNameList, dynamicSyntax[i].GetDynamicPropertyValue<List<string>>("FirstFirstNameList"));
|
|
|
|
|
CollectionAssert.AreEqual(regularSyntax[i].FirstOrDefaultFirstNameList, dynamicSyntax[i].GetDynamicPropertyValue<List<string>>("FirstOrDefaultFirstNameList"));
|
|
|
|
|
CollectionAssert.AreEqual(regularSyntax[i].LastFirstNameList, dynamicSyntax[i].GetDynamicPropertyValue<List<string>>("LastFirstNameList"));
|
|
|
|
|
CollectionAssert.AreEqual(regularSyntax[i].LastOrDefaultFirstNameList, dynamicSyntax[i].GetDynamicPropertyValue<List<string>>("LastOrDefaultFirstNameList"));
|
|
|
|
|
|
|
|
|
|
|
2018-03-21 23:27:03 -04:00
|
|
|
|
QueryableAssert.AreEqual(regularSyntax[i].FirstNames.AsQueryable(), dynamicSyntax[i].GetDynamicPropertyValue<List<string>>("FirstNames").AsQueryable());
|
|
|
|
|
|
|
|
|
|
|
2018-10-25 22:01:44 -04:00
|
|
|
|
|
|
|
|
|
|
2018-03-21 23:27:03 -04:00
|
|
|
|
var left = regularSyntax[i].FirstNamesLists;
|
|
|
|
|
var right = dynamicSyntax[i].GetDynamicPropertyValue<List<List<string>>>("FirstNamesLists");
|
|
|
|
|
Assert.AreEqual(left.Count, right.Count);
|
|
|
|
|
for(var j = 0; j < left.Count; j++)
|
|
|
|
|
QueryableAssert.AreEqual(left[j].AsQueryable(), right[j].AsQueryable());
|
|
|
|
|
}
|
2018-03-21 23:15:45 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void SelectNullChecking()
|
|
|
|
|
{
|
2018-03-22 19:16:57 -04:00
|
|
|
|
var query = TestData.Authors.AsQueryable();
|
|
|
|
|
|
2018-04-15 00:26:42 -04:00
|
|
|
|
|
2018-03-23 00:34:46 -04:00
|
|
|
|
var qs = query.Select(t => new
|
2018-03-22 19:16:57 -04:00
|
|
|
|
{
|
2018-03-23 00:34:46 -04:00
|
|
|
|
CommentLikes = t.Posts == null ?
|
|
|
|
|
new List<CommentLike>() :
|
|
|
|
|
t.Posts.Where(t2 => t2.Comments != null).SelectMany(t2 => t2.Comments.Where(t3 => t3.CommentLikes != null).SelectMany(t3 => t3.CommentLikes)).ToList()
|
2018-03-22 19:16:57 -04:00
|
|
|
|
});
|
|
|
|
|
|
2018-03-23 00:34:46 -04:00
|
|
|
|
var a = qs.ToList();
|
|
|
|
|
|
2018-03-22 19:16:57 -04:00
|
|
|
|
var querySelect = query.Select(t =>
|
|
|
|
|
{
|
|
|
|
|
t.NullChecking(true);
|
2018-10-25 22:01:44 -04:00
|
|
|
|
t.ToList("Posts.Comments.CommentLikes", selectCollectionHandling: SelectCollectionHandling.Flatten);
|
2018-03-22 19:16:57 -04:00
|
|
|
|
});
|
|
|
|
|
|
2018-04-11 23:04:54 -04:00
|
|
|
|
var b = querySelect.ToDynamicClassList();
|
2018-04-10 21:08:27 -04:00
|
|
|
|
|
2018-04-11 23:04:54 -04:00
|
|
|
|
Assert.AreEqual(a.Count, b.Count);
|
|
|
|
|
for(var i = 0; i < a.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var left = a[i];
|
|
|
|
|
var right = b[i];
|
|
|
|
|
|
|
|
|
|
var leftCommentLikes = left.CommentLikes;
|
|
|
|
|
var rightCommentLikes = right.GetDynamicPropertyValue<List<CommentLike>>("CommentLikes");
|
|
|
|
|
QueryableAssert.AreEqual(leftCommentLikes.AsQueryable(), rightCommentLikes.AsQueryable());
|
|
|
|
|
}
|
2018-03-21 21:00:46 -04:00
|
|
|
|
}
|
2018-04-13 19:20:12 -04:00
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void SelectNullChecking2()
|
|
|
|
|
{
|
|
|
|
|
var query = TestData.Likes.AsQueryable();
|
|
|
|
|
|
|
|
|
|
var qs = query.Select(t => new
|
|
|
|
|
{
|
|
|
|
|
Post = t.Comment == null || t.Comment.Post == null ? null : t.Comment.Post,
|
|
|
|
|
Texts = (t.Comment == null || t.Comment.Post == null || t.Comment.Post.Comments == null ? new List<string>() : t.Comment.Post.Comments.Select(t2 => t2.CommentText)).ToList()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var a = qs.ToList();
|
|
|
|
|
|
|
|
|
|
var querySelect = query.Select(t =>
|
|
|
|
|
{
|
|
|
|
|
t.NullChecking(true);
|
|
|
|
|
// this needs to be fixed.
|
2018-04-15 00:26:42 -04:00
|
|
|
|
t.Path("Comment.CommentText", "CommentText2");
|
|
|
|
|
//t.PathToList("Comment.Post.Comments.CommentText", selectCollectionHandling: SelectCollectionHandling.Flatten);
|
2018-04-13 19:20:12 -04:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var b = querySelect.ToDynamicClassList();
|
|
|
|
|
}
|
2018-03-21 21:00:46 -04:00
|
|
|
|
}
|
|
|
|
|
}
|