using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using PoweredSoft.DynamicLinq.Dal; using PoweredSoft.DynamicLinq.Dal.Pocos; using PoweredSoft.DynamicLinq.EntityFramework; using PoweredSoft.DynamicLinq; namespace PoweredSoft.DynamicLinq.Test { [TestClass] public class EntityFrameworkTests { public static string testConnectionString => @"Server=(localdb)\mssqllocaldb;Database=EFProviders.InMemory;Trusted_Connection=True;"; //"data source=(local); initial catalog=blogtests;persist security info=True; Integrated Security=SSPI;"; public static void SeedForTests(BlogContext context) { context.Authors.Add(new Author { FirstName = "David", LastName = "Lebee", Posts = new List() { new Post() { CreateTime = DateTimeOffset.Now, PublishTime = DateTimeOffset.Now, Title = "New project", Content = "Lots of good things coming", Comments = new List() { new Comment() { DisplayName = "John Doe", Email = "john.doe@me.com", CommentText = "Very interesting", }, new Comment() { DisplayName = "Nice Guy", Email = "nice.guy@lol.com", CommentText = "Best of luck!" } } }, new Post() { CreateTime = DateTimeOffset.Now, PublishTime = null, Title = "The future!", Content = "Is Near" } } }); context.Authors.Add(new Author { FirstName = "Some", LastName = "Dude", Posts = new List() { new Post() { CreateTime = DateTimeOffset.Now, PublishTime = DateTimeOffset.Now, Title = "The One", Content = "And Only" }, new Post() { CreateTime = DateTimeOffset.Now, PublishTime = DateTimeOffset.Now, Title = "The Two", Content = "And Second" } } }); context.SaveChanges(); } [TestMethod] public void TestSimpleWhere() { var context = new BlogContext(testConnectionString); SeedForTests(context); var query = context.Authors.AsQueryable(); query = query.Where("FirstName", ConditionOperators.Equal, "David"); var author = query.FirstOrDefault(); Assert.IsNotNull(author); } [TestMethod] public void TestWhereAnd() { var context = new BlogContext(testConnectionString); SeedForTests(context); var query = context.Authors.AsQueryable(); query = query.Query(q => q .Compare("FirstName", ConditionOperators.Equal, "David") .And("LastName", ConditionOperators.Equal, "Lebee") ); var author = query.FirstOrDefault(); Assert.IsNotNull(author); } [TestMethod] public void TestWhereOr() { var context = new BlogContext(testConnectionString); SeedForTests(context); var query = context.Authors.AsQueryable(); query = query.Query(q => q .Compare("FirstName", ConditionOperators.Equal, "David") .Or("FirstName", ConditionOperators.Equal, "Some") ); var author = query.FirstOrDefault(); Assert.IsNotNull(author); } [TestMethod] public void TestGoingThroughSimpleNav() { var context = new BlogContext(testConnectionString); SeedForTests(context); var query = context.Posts.AsQueryable(); query = query.Include("Author").Where("Author.FirstName", ConditionOperators.Contains, "David"); var post = query.FirstOrDefault(); Assert.AreEqual("David", post?.Author?.FirstName); } [TestMethod] public void TestGoingThroughCollectionNav() { var context = new BlogContext(testConnectionString); SeedForTests(context); var query = context.Authors.AsQueryable(); query = query.Where("Posts.Title", ConditionOperators.Contains, "New"); var author = query.FirstOrDefault(); Assert.AreEqual(author?.FirstName, "David"); } [TestMethod] public void TestGoingThrough2CollectionNav() { var context = new BlogContext(testConnectionString); SeedForTests(context); var query = context.Authors.AsQueryable(); query = query.Where("Posts.Comments.Email", ConditionOperators.Contains, "@me.com"); var author = query.FirstOrDefault(); Assert.AreEqual(author?.FirstName, "David"); } [TestMethod] public void TestSort() { var context = new BlogContext(testConnectionString); SeedForTests(context); var query = context.Posts.AsQueryable(); var dq = query.OrderBy("Title").ThenByDescending("Content").ToList(); var sq = query.OrderBy(t => t.Title).ThenByDescending(t => t.Content).ToList(); Assert.AreEqual(dq.Count, sq.Count); for (var i = 0; i < dq.Count; i++) Assert.AreEqual(dq[i].Id, sq[i].Id); } [TestMethod] public void TestContextTypeLessHelper() { var context = new BlogContext(testConnectionString); SeedForTests(context); var queryable = context.Query(typeof(Author), q => q.Compare("FirstName", ConditionOperators.Equal, "David")); var result = queryable.ToListAsync().Result; var first = result.FirstOrDefault() as Author; Assert.AreEqual(first?.FirstName, "David"); } } }