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

156 lines
4.7 KiB
C#
Raw Normal View History

2018-02-11 20:55:29 -05:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PoweredSoft.DynamicLinq.Dal.Pocos;
using PoweredSoft.DynamicLinq.Extensions;
namespace PoweredSoft.DynamicLinq.Test
{
[TestClass]
public class SimpleQueryTests
2018-02-11 20:55:29 -05:00
{
[TestMethod]
public void Equal()
{
// subject.
var authors = new List<Author>()
{
new Author { Id = long.MaxValue, FirstName = "David", LastName = "Lebee" }
};
// the query.
var query = authors.AsQueryable();
// simple where.
var newQuery = query.Where("FirstName", ConditionOperators.Equal, "David");
// must match.
Assert.IsTrue(newQuery.Any(), "Must have at least one author that matches");
}
[TestMethod]
public void Contains()
{
// subject.
var authors = new List<Author>()
{
new Author { Id = long.MaxValue, FirstName = "David", LastName = "Lebee" }
};
// the query.
var query = authors.AsQueryable();
// simple where.
var newQuery = query.Where("FirstName", ConditionOperators.Contains, "Da");
// must match.
Assert.IsTrue(newQuery.Any(), "Must have at least one author that matches");
}
[TestMethod]
public void StartsWith()
{
// subject.
var authors = new List<Author>()
{
new Author { Id = long.MaxValue, FirstName = "David", LastName = "Lebee" }
};
// the query.
var query = authors.AsQueryable();
// simple where.
var newQuery = query.Where("FirstName", ConditionOperators.StartsWith, "Da");
// must match.
Assert.IsTrue(newQuery.Any(), "Must have at least one author that matches");
}
[TestMethod]
public void EndsWith()
{
// subject.
var authors = new List<Author>()
{
new Author { Id = long.MaxValue, FirstName = "David", LastName = "Lebee" }
};
// the query.
var query = authors.AsQueryable();
// simple where.
var newQuery = query.Where("FirstName", ConditionOperators.EndsWith, "Da");
// must match.
Assert.IsFalse(newQuery.Any(), "Not suppose to find any matches");
}
[TestMethod]
public void LessThen()
{
// subject.
2018-02-12 05:18:44 -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 05:18:44 -05:00
var query = posts.AsQueryable();
2018-02-11 20:55:29 -05:00
// simple where.
var newQuery = query.Where("AuthorId", ConditionOperators.LessThan, 2);
// must match.
Assert.AreEqual(2, newQuery.Count());
}
[TestMethod]
public void GreaterThanOrEqual()
{
// subject.
2018-02-12 05:18:44 -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 05:18:44 -05:00
var query = posts.AsQueryable();
2018-02-11 20:55:29 -05:00
// simple where.
var newQuery = query.Where("AuthorId", ConditionOperators.GreaterThanOrEqual, 2);
// must match.
Assert.AreEqual(1, newQuery.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();
query = query.OrderByDescending("AuthorId");
query = query.ThenBy("Id");
var first = query.First();
var second = query.Skip(1).First();
Assert.IsTrue(first.Id == 3);
Assert.IsTrue(second.Id == 1);
}
2018-02-11 20:55:29 -05:00
}
}