From 46422bca42e21eb1b09f850ab527e7a56608e98b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Leb=C3=A9e?= Date: Tue, 13 Feb 2018 21:18:58 -0600 Subject: [PATCH] testing with EF no changes required works quite well :) --- .../Configurations/Configurations.cs | 2 - PoweredSoft.DynamicLinq.Dal/Pocos/Comment.cs | 4 - ...lexQueryTest.cs => ComplexQueriesTests.cs} | 80 +------- .../EntityFrameworkTests.cs | 172 ++++++++++++++++++ PoweredSoft.DynamicLinq.Test/HelpersTests.cs | 91 +++++++++ .../PoweredSoft.DynamicLinq.Test.csproj | 13 +- .../{QueryTests.cs => SimpleQueriesTest.cs} | 2 +- PoweredSoft.DynamicLinq.Test/packages.config | 1 + 8 files changed, 278 insertions(+), 87 deletions(-) rename PoweredSoft.DynamicLinq.Test/{ComplexQueryTest.cs => ComplexQueriesTests.cs} (63%) create mode 100644 PoweredSoft.DynamicLinq.Test/EntityFrameworkTests.cs create mode 100644 PoweredSoft.DynamicLinq.Test/HelpersTests.cs rename PoweredSoft.DynamicLinq.Test/{QueryTests.cs => SimpleQueriesTest.cs} (99%) diff --git a/PoweredSoft.DynamicLinq.Dal/Configurations/Configurations.cs b/PoweredSoft.DynamicLinq.Dal/Configurations/Configurations.cs index 0049aaa..f922a20 100644 --- a/PoweredSoft.DynamicLinq.Dal/Configurations/Configurations.cs +++ b/PoweredSoft.DynamicLinq.Dal/Configurations/Configurations.cs @@ -60,14 +60,12 @@ namespace PoweredSoft.DynamicLinq.Dal.Configurations ToTable("Comment", schema); HasKey(t => t.Id); Property(t => t.Id).HasColumnName("Id").HasColumnType("bigint").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); - Property(t => t.ParentCommentId).HasColumnName("ParentCommentId").HasColumnType("bigint").IsOptional(); Property(t => t.PostId).HasColumnName("PostId").HasColumnType("bigint").IsRequired(); Property(t => t.DisplayName).HasColumnName("DisplayName").HasColumnType("nvarchar").HasMaxLength(100).IsRequired(); Property(t => t.Email).HasColumnName("Email").HasColumnType("nvarchar").IsOptional(); Property(t => t.CommentText).HasColumnName("CommentText").HasColumnType("nvarchar").HasMaxLength(255).IsOptional(); HasRequired(t => t.Post).WithMany(t => t.Comments).HasForeignKey(t => t.PostId).WillCascadeOnDelete(false); - HasOptional(t => t.ParentComment).WithMany(t => t.Comments).HasForeignKey(t => t.ParentCommentId).WillCascadeOnDelete(false); } } } diff --git a/PoweredSoft.DynamicLinq.Dal/Pocos/Comment.cs b/PoweredSoft.DynamicLinq.Dal/Pocos/Comment.cs index b0c003f..df79636 100644 --- a/PoweredSoft.DynamicLinq.Dal/Pocos/Comment.cs +++ b/PoweredSoft.DynamicLinq.Dal/Pocos/Comment.cs @@ -9,14 +9,10 @@ namespace PoweredSoft.DynamicLinq.Dal.Pocos public class Comment { public long Id { get; set; } - public long? ParentCommentId { get; set; } public long PostId { get; set; } public string DisplayName { get; set; } public string Email { get; set; } public string CommentText { get; set; } - - public Comment ParentComment { get; set; } - public ICollection Comments { get; set; } public Post Post { get; set; } } } diff --git a/PoweredSoft.DynamicLinq.Test/ComplexQueryTest.cs b/PoweredSoft.DynamicLinq.Test/ComplexQueriesTests.cs similarity index 63% rename from PoweredSoft.DynamicLinq.Test/ComplexQueryTest.cs rename to PoweredSoft.DynamicLinq.Test/ComplexQueriesTests.cs index ead086e..87c7909 100644 --- a/PoweredSoft.DynamicLinq.Test/ComplexQueryTest.cs +++ b/PoweredSoft.DynamicLinq.Test/ComplexQueriesTests.cs @@ -15,7 +15,7 @@ using System.Threading.Tasks; namespace PoweredSoft.DynamicLinq.Test { [TestClass] - public class ComplexQueryTest + public class ComplexQueriesTests { [TestMethod] public void ComplexQueryBuilder() @@ -100,7 +100,7 @@ namespace PoweredSoft.DynamicLinq.Test } [TestMethod] - public void TestCreateFilterExpressionCheckNull() + public void TestAutomaticNullChecking() { var authors = new List() { @@ -174,81 +174,5 @@ namespace PoweredSoft.DynamicLinq.Test Assert.AreEqual(1, query.Count()); } - [TestMethod] - public void TestCreateFilterExpression() - { - var authors = new List() - { - new Author - { - Id = 1, - FirstName = "David", - LastName = "Lebee", - Posts = new List - { - new Post - { - Id = 1, - AuthorId = 1, - Title = "Match", - Content = "ABC", - Comments = new List() - { - new Comment() - { - Id = 1, - DisplayName = "John Doe", - CommentText = "!@#$!@#!@#", - Email = "John.doe@me.com" - } - } - }, - new Post - { - Id = 2, - AuthorId = 1, - Title = "Match", - Content = "ABC", - Comments = new List() - } - } - }, - new Author - { - Id = 2, - FirstName = "Chuck", - LastName = "Norris", - Posts = new List - { - new Post - { - Id = 3, - AuthorId = 2, - Title = "Match", - Content = "ASD", - Comments = new List() - }, - new Post - { - Id = 4, - AuthorId = 2, - Title = "DontMatch", - Content = "ASD", - Comments = new List() - } - } - } - }; - - // the query. - var query = authors.AsQueryable(); - - var allExpression = QueryableHelpers.CreateFilterExpression("Posts.Title", ConditionOperators.Equal, "Match", QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling.All); - var anyExpression = QueryableHelpers.CreateFilterExpression("Posts.Title", ConditionOperators.Equal, "Match", QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling.Any); - var anyExpression2 = QueryableHelpers.CreateFilterExpression("Posts.Comments.Email", ConditionOperators.Equal, "John.doe@me.com", QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling.Any); - Assert.AreEqual(1, query.Count(allExpression)); - Assert.AreEqual(2, query.Count(anyExpression)); - Assert.AreEqual(1, query.Count(anyExpression2)); - } } } diff --git a/PoweredSoft.DynamicLinq.Test/EntityFrameworkTests.cs b/PoweredSoft.DynamicLinq.Test/EntityFrameworkTests.cs new file mode 100644 index 0000000..5d3182d --- /dev/null +++ b/PoweredSoft.DynamicLinq.Test/EntityFrameworkTests.cs @@ -0,0 +1,172 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using PoweredSoft.DynamicLinq.Dal; +using PoweredSoft.DynamicLinq.Dal.Pocos; +using PoweredSoft.DynamicLinq.Extensions; + +namespace PoweredSoft.DynamicLinq.Test +{ + [TestClass] + public class EntityFrameworkTests + { + public static string testConnectionString => + "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!", + //Comments = new List() + //{ + // new Comment() + // { + // DisplayName = "David Lebee", + // Email = "david@poweredsoft.com", + // CommentText = "Thanks!" + // } + //} + } + } + }, + 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.Where("Author.FirstName", ConditionOperators.Contains, "David"); + Assert.AreEqual(2, query.Count()); + } + + [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"); + } + } +} diff --git a/PoweredSoft.DynamicLinq.Test/HelpersTests.cs b/PoweredSoft.DynamicLinq.Test/HelpersTests.cs new file mode 100644 index 0000000..3c7a92a --- /dev/null +++ b/PoweredSoft.DynamicLinq.Test/HelpersTests.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using PoweredSoft.DynamicLinq.Dal.Pocos; +using PoweredSoft.DynamicLinq.Helpers; + +namespace PoweredSoft.DynamicLinq.Test +{ + [TestClass] + public class HelpersTests + { + + [TestMethod] + public void TestCreateFilterExpression() + { + var authors = new List() + { + new Author + { + Id = 1, + FirstName = "David", + LastName = "Lebee", + Posts = new List + { + new Post + { + Id = 1, + AuthorId = 1, + Title = "Match", + Content = "ABC", + Comments = new List() + { + new Comment() + { + Id = 1, + DisplayName = "John Doe", + CommentText = "!@#$!@#!@#", + Email = "John.doe@me.com" + } + } + }, + new Post + { + Id = 2, + AuthorId = 1, + Title = "Match", + Content = "ABC", + Comments = new List() + } + } + }, + new Author + { + Id = 2, + FirstName = "Chuck", + LastName = "Norris", + Posts = new List + { + new Post + { + Id = 3, + AuthorId = 2, + Title = "Match", + Content = "ASD", + Comments = new List() + }, + new Post + { + Id = 4, + AuthorId = 2, + Title = "DontMatch", + Content = "ASD", + Comments = new List() + } + } + } + }; + + // the query. + var query = authors.AsQueryable(); + + var allExpression = QueryableHelpers.CreateFilterExpression("Posts.Title", ConditionOperators.Equal, "Match", QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling.All); + var anyExpression = QueryableHelpers.CreateFilterExpression("Posts.Title", ConditionOperators.Equal, "Match", QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling.Any); + var anyExpression2 = QueryableHelpers.CreateFilterExpression("Posts.Comments.Email", ConditionOperators.Equal, "John.doe@me.com", QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling.Any); + Assert.AreEqual(1, query.Count(allExpression)); + Assert.AreEqual(2, query.Count(anyExpression)); + Assert.AreEqual(1, query.Count(anyExpression2)); + } + } +} diff --git a/PoweredSoft.DynamicLinq.Test/PoweredSoft.DynamicLinq.Test.csproj b/PoweredSoft.DynamicLinq.Test/PoweredSoft.DynamicLinq.Test.csproj index 27bdbd4..209cea0 100644 --- a/PoweredSoft.DynamicLinq.Test/PoweredSoft.DynamicLinq.Test.csproj +++ b/PoweredSoft.DynamicLinq.Test/PoweredSoft.DynamicLinq.Test.csproj @@ -38,6 +38,12 @@ 4 + + ..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.dll + + + ..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.SqlServer.dll + ..\packages\Faker.Data.1.0.7\lib\net45\Faker.dll @@ -48,13 +54,16 @@ ..\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + - - + + + + diff --git a/PoweredSoft.DynamicLinq.Test/QueryTests.cs b/PoweredSoft.DynamicLinq.Test/SimpleQueriesTest.cs similarity index 99% rename from PoweredSoft.DynamicLinq.Test/QueryTests.cs rename to PoweredSoft.DynamicLinq.Test/SimpleQueriesTest.cs index 8702b14..2491f09 100644 --- a/PoweredSoft.DynamicLinq.Test/QueryTests.cs +++ b/PoweredSoft.DynamicLinq.Test/SimpleQueriesTest.cs @@ -9,7 +9,7 @@ using PoweredSoft.DynamicLinq.Extensions; namespace PoweredSoft.DynamicLinq.Test { [TestClass] - public class QueryTests + public class SimpleQueryTests { [TestMethod] public void Equal() diff --git a/PoweredSoft.DynamicLinq.Test/packages.config b/PoweredSoft.DynamicLinq.Test/packages.config index 5dfd80b..f054fb9 100644 --- a/PoweredSoft.DynamicLinq.Test/packages.config +++ b/PoweredSoft.DynamicLinq.Test/packages.config @@ -1,5 +1,6 @@  +