diff --git a/DynamicQuery.sln b/DynamicQuery.sln index b51facc..361e0b1 100644 --- a/DynamicQuery.sln +++ b/DynamicQuery.sln @@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution README.md = README.md EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PoweredSoft.DynamicQuery.Test", "PoweredSoft.DynamicQuery.Test\PoweredSoft.DynamicQuery.Test.csproj", "{3EAD8217-8E10-4261-9055-50444905922C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {E4E954E0-66FA-4D72-979A-FB2EF8356A90}.Debug|Any CPU.Build.0 = Debug|Any CPU {E4E954E0-66FA-4D72-979A-FB2EF8356A90}.Release|Any CPU.ActiveCfg = Release|Any CPU {E4E954E0-66FA-4D72-979A-FB2EF8356A90}.Release|Any CPU.Build.0 = Release|Any CPU + {3EAD8217-8E10-4261-9055-50444905922C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3EAD8217-8E10-4261-9055-50444905922C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3EAD8217-8E10-4261-9055-50444905922C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3EAD8217-8E10-4261-9055-50444905922C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PoweredSoft.DynamicQuery.Test/CriteriaTests.cs b/PoweredSoft.DynamicQuery.Test/CriteriaTests.cs new file mode 100644 index 0000000..5e16fdd --- /dev/null +++ b/PoweredSoft.DynamicQuery.Test/CriteriaTests.cs @@ -0,0 +1,42 @@ +using System; +using System.Linq; +using Microsoft.EntityFrameworkCore; +using PoweredSoft.DynamicQuery.Test.Mock; +using Xunit; + +namespace PoweredSoft.DynamicQuery.Test +{ + public class CriteriaTests + { + [Fact] + public void TestEmptyCriteria() + { + var options = new DbContextOptionsBuilder().UseInMemoryDatabase(databaseName: "TestEmptyCriteria").Options; + using (var ctx = new MockContext(options)) + { + ctx.AddRange(new Item[] + { + new Item { Id = 1, Name = "Computer", Price = 1000M }, + new Item { Id = 2, Name = "Mice", Price = 25.99M }, + new Item { Id = 3, Name = "Keyboard", Price = 100M }, + new Item { Id = 4, Name = "Screen", Price = 499.98M }, + new Item { Id = 5, Name = "Printer", Price = 230.95M }, + }); + + ctx.SaveChanges(); + } + + using (var ctx = new MockContext(options)) + { + var resultShouldMatch = ctx.Items.ToList(); + var queryable = ctx.Items.AsQueryable(); + + // query handler that is empty should be the same as running to list. + var criteria = new QueryCriteria(); + var queryHandler = new QueryHandler(); + var result = queryHandler.Execute(queryable, criteria); + Assert.All(resultShouldMatch, t => result.Data.Contains(t)); + } + } + } +} diff --git a/PoweredSoft.DynamicQuery.Test/Mock/Entities.cs b/PoweredSoft.DynamicQuery.Test/Mock/Entities.cs new file mode 100644 index 0000000..0bffa50 --- /dev/null +++ b/PoweredSoft.DynamicQuery.Test/Mock/Entities.cs @@ -0,0 +1,50 @@ +using System.Collections.Generic; + +namespace PoweredSoft.DynamicQuery.Test.Mock +{ + public class Order + { + public long Id { get; set; } + public long OrderNum { get; set; } + public long Date { get; set; } + public long CustomerId { get; set; } + + public virtual Customer Customer { get; set; } + public ICollection OrderItems { get; set; } = new HashSet(); + } + + public class Customer + { + public long Id { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public string AddressLine1 { get; set; } + public string AddressLine2 { get; set; } + public string City { get; set; } + public string ProvinceOrState { get; set; } + public string PostalCodeOrZip { get; set; } + + + public ICollection Orders { get; set; } = new HashSet(); + } + + public class Item + { + public long Id { get; set; } + public string Name { get; set; } + public decimal Price { get; set; } + + public virtual ICollection OrderItems { get; set; } = new HashSet(); + } + + public class OrderItem + { + public long Id { get; set; } + public long Quantity { get; set; } + public decimal PriceAtTheTime { get; set; } + public long ItemId { get; set; } + + public virtual Item Item { get; set; } + public virtual Order Order { get; set; } + } +} diff --git a/PoweredSoft.DynamicQuery.Test/Mock/MockContext.cs b/PoweredSoft.DynamicQuery.Test/Mock/MockContext.cs new file mode 100644 index 0000000..812c117 --- /dev/null +++ b/PoweredSoft.DynamicQuery.Test/Mock/MockContext.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Microsoft.EntityFrameworkCore; + +namespace PoweredSoft.DynamicQuery.Test.Mock +{ + public class MockContext : DbContext + { + public virtual DbSet Customers { get; set; } + public virtual DbSet Items { get; set; } + public virtual DbSet Orders { get; set; } + public virtual DbSet OrderItems { get; set; } + + public MockContext() + { + + } + + public MockContext(DbContextOptions options) + : base(options) + { + + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + + } + } +} diff --git a/PoweredSoft.DynamicQuery.Test/PoweredSoft.DynamicQuery.Test.csproj b/PoweredSoft.DynamicQuery.Test/PoweredSoft.DynamicQuery.Test.csproj new file mode 100644 index 0000000..84570c8 --- /dev/null +++ b/PoweredSoft.DynamicQuery.Test/PoweredSoft.DynamicQuery.Test.csproj @@ -0,0 +1,21 @@ + + + + netcoreapp2.1 + + false + + + + + + + + + + + + + + + diff --git a/README.md b/README.md index 7c4a026..c3a6886 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Criteria must implement the following interfaces Object | Interface | Implementation | Example | Description -----------------|--------------------------------------------------------------------------|-------------------------------------------------------------------------------|--------------------------------------------------------|-------------------------------------------- -Query Criteria | [interface](../master/PoweredSoft.DynamicQuery.Core/IQueryCriteria.cs) | [default implementation](../master/PoweredSoft.DynamicQuery/QueryCriteria.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/TBT.md) | Wraps the query parameters +Query Criteria | [interface](../master/PoweredSoft.DynamicQuery.Core/IQueryCriteria.cs) | [default implementation](../master/PoweredSoft.DynamicQuery/QueryCriteria.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/CriteriaTests.cs) | Wraps the query parameters Filter | [interface](../master/PoweredSoft.DynamicQuery.Core/IFilter.cs) | [default implementation](../master/PoweredSoft.DynamicQuery/Filter.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/TBT.md) | Represent a filter to be executed Simple Filter | [interface](../master/PoweredSoft.DynamicQuery.Core/ISimpleFilter.cs) | [default implementation](../master/PoweredSoft.DynamicQuery/Filter.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/TBT.md) | Represent a simple filter to be executed Composite Filter | [interface](../master/PoweredSoft.DynamicQuery.Core/ICompositeFilter.cs) | [default implementation](../master/PoweredSoft.DynamicQuery/Filter.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/TBT.md) | Represent a composite filter to be executed