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

178 lines
5.7 KiB
C#
Raw Normal View History

2018-02-11 20:55:29 -05:00
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PoweredSoft.DynamicLinq.Dal.Pocos;
2018-02-12 04:18:42 -05:00
using PoweredSoft.DynamicLinq.Fluent;
using PoweredSoft.DynamicLinq.Helpers;
2018-02-11 20:55:29 -05:00
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;
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 QueryBuilder<Post>(query);
// 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()
{
var authors = new List<Author>()
{
new Author
{
Id = 1,
FirstName = "David",
LastName = "Lebee",
Posts = new List<Post>
{
new Post
{
Id = 1,
AuthorId = 1,
Title = "Match",
Content = "ABC",
Comments = new List<Comment>()
{
new Comment()
{
Id = 1,
DisplayName = "John Doe",
CommentText = "!@#$!@#!@#",
Email = "john.doe@me.com"
}
}
},
new Post
{
Id = 2,
AuthorId = 1,
Title = "Match",
Content = "ABC"
}
}
},
new Author
{
Id = 2,
FirstName = "Chuck",
LastName = "Norris",
Posts = new List<Post>
{
new Post
{
Id = 3,
AuthorId = 2,
Title = "Match",
Content = "ASD"
},
new Post
{
Id = 4,
AuthorId = 2,
Title = "DontMatch",
Content = "ASD"
}
}
}
};
// 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);
});
Assert.AreEqual(1, query.Count());
}
2018-02-11 20:55:29 -05:00
}
}