advancing well in unit testing :)

This commit is contained in:
David Lebee 2018-10-22 21:05:23 -05:00
parent 640782ed9e
commit 11074bc946
7 changed files with 197 additions and 28 deletions

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PoweredSoft.DynamicQuery.Test.Mock; using PoweredSoft.DynamicQuery.Test.Mock;
@ -11,22 +12,7 @@ namespace PoweredSoft.DynamicQuery.Test
[Fact] [Fact]
public void TestEmptyCriteria() public void TestEmptyCriteria()
{ {
var options = new DbContextOptionsBuilder<MockContext>().UseInMemoryDatabase(databaseName: "TestEmptyCriteria").Options; MockContextFactory.SeedAndTestContextFor("CriteriaTests_TestEmptyCriteria", TestSeeders.SimpleSeedScenario, ctx =>
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 resultShouldMatch = ctx.Items.ToList();
var queryable = ctx.Items.AsQueryable(); var queryable = ctx.Items.AsQueryable();
@ -35,8 +21,8 @@ namespace PoweredSoft.DynamicQuery.Test
var criteria = new QueryCriteria(); var criteria = new QueryCriteria();
var queryHandler = new QueryHandler(); var queryHandler = new QueryHandler();
var result = queryHandler.Execute(queryable, criteria); var result = queryHandler.Execute(queryable, criteria);
Assert.All(resultShouldMatch, t => result.Data.Contains(t)); Assert.Equal(resultShouldMatch, result.Data);
} });
} }
} }
} }

View File

@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PoweredSoft.DynamicQuery.Core;
using PoweredSoft.DynamicQuery.Test.Mock;
using Xunit;
namespace PoweredSoft.DynamicQuery.Test
{
public class FilterTests
{
[Fact]
public void SimpleFilter()
{
MockContextFactory.SeedAndTestContextFor("FilterTests_SimpleFilter", TestSeeders.SimpleSeedScenario, ctx =>
{
var resultShouldMatch = ctx.Items.Where(t => t.Name.EndsWith("Cables")).ToList();
// query handler that is empty should be the same as running to list.
var criteria = new QueryCriteria()
{
Filters = new List<IFilter>
{
new SimpleFilter
{
Path = "Name",
Type = FilterType.EndsWith,
Value = "Cables"
}
}
};
var queryHandler = new QueryHandler();
var result = queryHandler.Execute(ctx.Items, criteria);
Assert.Equal(resultShouldMatch, result.Data);
});
}
private class MockIsChuckFilter : ISimpleFilter
{
public bool? And { get; set; } = false;
public FilterType Type { get; set; } = FilterType.Equal;
public string Path { get; set; } = "FirstName";
public object Value { get; set; } = "Chuck";
}
[Fact]
public void TestInversionOfControl()
{
MockContextFactory.SeedAndTestContextFor("FilterTests_SimpleFilter", TestSeeders.SimpleSeedScenario, ctx =>
{
var resultShouldMatch = ctx.Customers.Where(t => t.FirstName == "Chuck").ToList();
// query handler that is empty should be the same as running to list.
var criteria = new QueryCriteria()
{
Filters = new List<IFilter> { new MockIsChuckFilter() }
};
var queryHandler = new QueryHandler();
var result = queryHandler.Execute(ctx.Customers, criteria);
Assert.Equal(resultShouldMatch, result.Data);
});
}
}
}

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
namespace PoweredSoft.DynamicQuery.Test.Mock namespace PoweredSoft.DynamicQuery.Test.Mock
{ {
@ -6,7 +7,7 @@ namespace PoweredSoft.DynamicQuery.Test.Mock
{ {
public long Id { get; set; } public long Id { get; set; }
public long OrderNum { get; set; } public long OrderNum { get; set; }
public long Date { get; set; } public DateTime Date { get; set; }
public long CustomerId { get; set; } public long CustomerId { get; set; }
public virtual Customer Customer { get; set; } public virtual Customer Customer { get; set; }
@ -18,11 +19,6 @@ namespace PoweredSoft.DynamicQuery.Test.Mock
public long Id { get; set; } public long Id { get; set; }
public string FirstName { get; set; } public string FirstName { get; set; }
public string LastName { 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<Order> Orders { get; set; } = new HashSet<Order>(); public ICollection<Order> Orders { get; set; } = new HashSet<Order>();
@ -43,6 +39,7 @@ namespace PoweredSoft.DynamicQuery.Test.Mock
public long Quantity { get; set; } public long Quantity { get; set; }
public decimal PriceAtTheTime { get; set; } public decimal PriceAtTheTime { get; set; }
public long ItemId { get; set; } public long ItemId { get; set; }
public long OrderId { get; set; }
public virtual Item Item { get; set; } public virtual Item Item { get; set; }
public virtual Order Order { get; set; } public virtual Order Order { get; set; }

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EntityFrameworkCore;
namespace PoweredSoft.DynamicQuery.Test.Mock
{
public static class MockContextFactory
{
public static void TestContextFor(string testName, Action<MockContext> action)
{
var options = new DbContextOptionsBuilder<MockContext>().UseInMemoryDatabase(databaseName: testName).Options;
using (var ctx = new MockContext(options))
action(ctx);
}
public static void SeedAndTestContextFor(string testName, Action<string> seedAction, Action<MockContext> action)
{
seedAction(testName);
TestContextFor(testName, action);
}
}
}

View File

@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
namespace PoweredSoft.DynamicQuery.Test.Mock
{
public static class TestSeeders
{
public static void SimpleSeedScenario(string testName)
{
MockContextFactory.TestContextFor(testName, ctx =>
{
ctx.AddRange(new Customer[]
{
new Customer() { Id = 1, FirstName = "David", LastName = "Lebee" },
new Customer() { Id = 2, FirstName = "John", LastName = "Doe" },
new Customer() { Id = 3, FirstName = "Chuck", LastName = "Norris" },
new Customer() { Id = 4, FirstName = "Nelson", LastName = "Mendela" },
new Customer() { Id = 5, FirstName = "Jimi", LastName = "Hendrix" },
new Customer() { Id = 6, FirstName = "Axel", LastName = "Rose" },
new Customer() { Id = 7, FirstName = "John", LastName = "Frusciante" },
new Customer() { Id = 8, FirstName = "Michael", LastName = "Jackson" },
new Customer() { Id = 9, FirstName = "Anita", LastName = "Franklin" },
});
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 },
new Item { Id = 6, Name = "HDMI Cables", Price = 20M },
new Item { Id = 7, Name = "Power Cables", Price = 5.99M }
});
ctx.Orders.AddRange(new Order[]
{
new Order()
{
Id = 1,
OrderNum = 1000,
CustomerId = 1,
Date = new DateTime(2018, 1, 1),
OrderItems = new List<OrderItem>()
{
new OrderItem() { Id = 1, ItemId = 1, PriceAtTheTime = 1000M, Quantity = 1 },
new OrderItem() { Id = 2, ItemId = 2, PriceAtTheTime = 30M, Quantity = 1 },
new OrderItem() { Id = 3, ItemId = 4, PriceAtTheTime = 399.99M, Quantity = 2 },
new OrderItem() { Id = 4, ItemId = 6, PriceAtTheTime = 20, Quantity = 2 },
new OrderItem() { Id = 8, ItemId = 6, PriceAtTheTime = 3.99M, Quantity = 3 }
}
},
new Order()
{
Id = 2,
OrderNum = 1001,
CustomerId = 2,
Date = new DateTime(2018, 2, 1),
OrderItems = new List<OrderItem>()
{
new OrderItem() { Id = 9, ItemId = 6, PriceAtTheTime = 20, Quantity = 2 },
new OrderItem() { Id = 10, ItemId = 6, PriceAtTheTime = 3.99M, Quantity = 3 }
}
},
new Order()
{
Id = 3,
OrderNum = 1002,
CustomerId = 3,
Date = new DateTime(2018, 2, 1),
OrderItems = new List<OrderItem>()
{
new OrderItem() { Id = 11, ItemId = 5, PriceAtTheTime = 499.99M, Quantity = 1 },
new OrderItem() { Id = 12, ItemId = 6, PriceAtTheTime = 20, Quantity = 1 },
new OrderItem() { Id = 13, ItemId = 7, PriceAtTheTime = 3.99M, Quantity = 1 }
}
},
new Order()
{
Id = 4,
OrderNum = 1003,
CustomerId = 1,
Date = new DateTime(2018, 3, 1),
OrderItems = new List<OrderItem>()
{
new OrderItem() { Id = 14, ItemId = 2, PriceAtTheTime = 50M, Quantity = 1 },
new OrderItem() { Id = 15, ItemId = 3, PriceAtTheTime = 75.50M, Quantity = 1 },
}
}
});
ctx.SaveChanges();
});
}
}
}

View File

@ -20,7 +20,7 @@ Criteria must implement the following interfaces
Object | Interface | Implementation | Example | Description 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/CriteriaTests.cs) | 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 Filter | [interface](../master/PoweredSoft.DynamicQuery.Core/IFilter.cs) | [default implementation](../master/PoweredSoft.DynamicQuery/Filter.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/FilterTests.cs) | 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 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 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
Sort | [interface](../master/PoweredSoft.DynamicQuery.Core/ISort.cs) | [default implementation](../master/PoweredSoft.DynamicQuery/Sort.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/TBT.md) | Represent a sort to be executed Sort | [interface](../master/PoweredSoft.DynamicQuery.Core/ISort.cs) | [default implementation](../master/PoweredSoft.DynamicQuery/Sort.cs) | [test](../master/PoweredSoft.DynamicQuery.Test/TBT.md) | Represent a sort to be executed