dotnet-dynamic-linq/PoweredSoft.DynamicLinq.Test/ComplexQueriesTests.cs

127 lines
4.0 KiB
C#
Raw Normal View History

2018-02-11 20:55:29 -05:00
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
2018-02-12 05:18:44 -05:00
using System.Data.SqlClient;
2018-02-11 20:55:29 -05:00
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
2018-02-11 20:55:29 -05:00
using System.Text;
using System.Threading.Tasks;
2018-03-09 00:22:12 -05:00
using PoweredSoft.DynamicLinq;
using PoweredSoft.DynamicLinq.Dal.Pocos;
using PoweredSoft.DynamicLinq.Fluent;
2018-02-11 20:55:29 -05:00
namespace PoweredSoft.DynamicLinq.Test
{
[TestClass]
public class ComplexQueriesTests
2018-02-11 20:55:29 -05:00
{
[TestMethod]
public void ComplexQueryBuilder()
{
// subject.
2018-02-12 04:18:42 -05:00
var posts = new List<Post>()
2018-02-11 20:55:29 -05:00
{
new Post { Id = 1, AuthorId = 1, Title = "Hello 1", Content = "World" },
new Post { Id = 2, AuthorId = 1, Title = "Hello 2", Content = "World" },
new Post { Id = 3, AuthorId = 2, Title = "Hello 3", Content = "World" },
};
// the query.
2018-02-12 04:18:42 -05:00
var query = posts.AsQueryable();
2018-02-11 20:55:29 -05:00
2018-02-11 21:11:35 -05:00
query = query.Query(q =>
{
q.Compare("AuthorId", ConditionOperators.Equal, 1);
q.And(sq =>
{
sq.Compare("Content", ConditionOperators.Equal, "World");
sq.Or("Title", ConditionOperators.Contains, 3);
});
});
Assert.AreEqual(2, query.Count());
2018-02-11 20:55:29 -05:00
}
2018-02-12 04:18:42 -05:00
[TestMethod]
public void UsingQueryBuilder()
{
// subject.
var posts = new List<Post>()
{
new Post { Id = 1, AuthorId = 1, Title = "Hello 1", Content = "World" },
new Post { Id = 2, AuthorId = 1, Title = "Hello 2", Content = "World" },
new Post { Id = 3, AuthorId = 2, Title = "Hello 3", Content = "World" },
};
// the query.
var query = posts.AsQueryable();
var queryBuilder = new QueryBuilder<Post>(query);
queryBuilder.Compare("AuthorId", ConditionOperators.Equal, 1);
queryBuilder.And(subQuery =>
{
subQuery.Compare("Content", ConditionOperators.Equal, "World");
subQuery.Or("Title", ConditionOperators.Contains, 3);
});
query = queryBuilder.Build();
Assert.AreEqual(2, query.Count());
}
2018-02-12 05:18:44 -05:00
[TestMethod]
public void TestingSort()
{
// subject.
var posts = new List<Post>()
{
new Post { Id = 1, AuthorId = 1, Title = "Hello 1", Content = "World" },
new Post { Id = 2, AuthorId = 1, Title = "Hello 2", Content = "World" },
new Post { Id = 3, AuthorId = 2, Title = "Hello 3", Content = "World" },
};
// the query.
var query = posts.AsQueryable();
var queryBuilder = new PoweredSoft.DynamicLinq.Fluent.SortBuilder<Post>(query);
2018-02-12 05:18:44 -05:00
// add some sorting.
queryBuilder
.OrderByDescending("AuthorId")
.ThenBy("Id");
query = queryBuilder.Build();
var first = query.First();
var second = query.Skip(1).First();
Assert.IsTrue(first.Id == 3);
Assert.IsTrue(second.Id == 1);
}
[TestMethod]
public void TestAutomaticNullChecking()
{
2018-03-06 23:40:01 -05:00
var authors = TestData.Authors;
// the query.
var query = authors.AsQueryable();
query = query.Query(qb =>
{
qb.NullChecking();
qb.And("Posts.Comments.Email", ConditionOperators.Equal, "john.doe@me.com", collectionHandling: QueryCollectionHandling.Any);
});
var query2 = query.Where(qb =>
{
qb.NullChecking();
qb.And("Posts.Comments.Email", ConditionOperators.Equal, "john.doe@me.com", collectionHandling: QueryCollectionHandling.Any);
});
Assert.AreEqual(1, query.Count());
Assert.AreEqual(1, query2.Count());
}
2018-02-11 20:55:29 -05:00
}
}