-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExpressionVisitorSpecificationTests.cs
More file actions
183 lines (140 loc) · 6.38 KB
/
Copy pathExpressionVisitorSpecificationTests.cs
File metadata and controls
183 lines (140 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
namespace BuildingBlock.Specification.Tests
{
using BuildingBlock.Specification;
using BuildingBlock.Specification.Extensions;
using BuildingBlock.Specification.Tests.Models;
using BuildingBlock.Specification.Tests.Specifications;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
[TestClass]
public class EFExpressionVisitorSpecificationTests
{
[TestMethod]
public void SimpleEFImplementation()
{
//assign
var repo = new ProductRepository();
var spec = new PriceGreaterThen(10);
//act
var result = repo.GetProducts(spec).ToList();
Assert.IsTrue(result.All(p => spec.IsSatisfiedBy(p)), $"Not all products were matched category or price by the specification");
}
[TestMethod]
public void TestAndImplementation()
{
//assign
var repo = new ProductRepository();
var spec = new PriceGreaterThen(10)
.And(new PriceLesserThen(20));
//act
var result = repo.GetProducts(spec).ToList();
Assert.IsTrue(result.All(p => spec.IsSatisfiedBy(p)), $"Not all products were matched category or price by the specification");
}
[TestMethod]
public void TestDeepEFImplementation()
{
//assign
var repo = new ProductRepository();
var spec = new ProductOfTag("Test-1");
//act
var result = repo.GetProducts(spec).ToList();
Assert.IsTrue(result.All(p => spec.IsSatisfiedBy(p)), $"Not all products were matched category or price by the specification");
}
[TestMethod]
public void TestDeepOrEFImplementation()
{
//assign
var repo = new ProductRepository();
var spec = new ProductOfTag("Test-1")
.Or(new ProductOfTag("Test-2"));
//act
var result = repo.GetProducts(spec).ToList();
Assert.IsTrue(result.All(p => spec.IsSatisfiedBy(p)), $"Not all products were matched category or price by the specification");
}
[TestMethod]
public void TestEFHelperImplementation()
{
//assign
var repo = new ProductRepository();
// ISpecification< Product, IProductSpecificationVisitor> spec = null;/* new PriceGreaterThen(10)
// .And(new PriceLesserThen(20))
//.Or(new ProductOfCategory("Electronics"));*/
var helper = new Helpers.EnumerableSpecificationHelper<Product, IProductSpecificationVisitor>();
helper.Include(new ProductOfTag("Test-1"));
helper.Include(new ProductOfTag("Test-2"));
helper.Apply();
helper.Include(new ProductOfCategory("Electronics"));
helper.Apply();
var spec = helper.GetSpecification();
//act
var result = repo.GetProducts(spec).ToList();
Assert.IsTrue(result.All(p => spec.IsSatisfiedBy(p)), $"Not all products were matched category or price by the specification");
}
[TestMethod]
public void TestEFImplementation()
{
//assign
var repo = new ProductRepository();
var spec = new PriceGreaterThen(10)
.And(new PriceLesserThen(20))
.Or(new ProductOfCategory("Electronics"));
//act
var result = repo.GetProducts(spec).ToList();
Assert.IsTrue(result.All(p => spec.IsSatisfiedBy(p)), $"Not all products were matched category or price by the specification");
}
[TestMethod]
public void TestNotImplementation()
{
//assign
var repo = new ProductRepository();
var spec = new PriceGreaterThen(10).Not();
//act
var result = repo.GetProducts(spec).ToList();
Assert.IsTrue(result.All(p => spec.IsSatisfiedBy(p)), $"Not all products were matched category or price by the specification");
}
[TestMethod]
public void TestOrImplementation()
{
//assign
var repo = new ProductRepository();
var spec = new PriceGreaterThen(10)
.Or(new PriceLesserThen(20));
//act
var result = repo.GetProducts(spec).ToList();
Assert.IsTrue(result.All(p => spec.IsSatisfiedBy(p)), $"Not all products were matched category or price by the specification");
}
}
public class ProductRepository
{
static ProductRepository()
{
Repo = new List<EFProduct>();
Repo.Add(new EFProduct(Guid.NewGuid(), 5, "Electronics", new List<string>() { "Test-1", "Test-2", "Test-3" }));
Repo.Add(new EFProduct(Guid.NewGuid(), 500, "Electronics", new List<string>() { "Test-1" }));
Repo.Add(new EFProduct(Guid.NewGuid(), 10, "Electronics", new List<string>() { "Test-1", "Test-2", "Test-3" }));
Repo.Add(new EFProduct(Guid.NewGuid(), 15, "Electronics", new List<string>() { "Test-1", "Test-2", "Test-3" }));
Repo.Add(new EFProduct(Guid.NewGuid(), 20, "Electronics", new List<string>() { "Test-1", "Test-2", "Test-3" }));
Repo.Add(new EFProduct(Guid.NewGuid(), 10, "Games", new List<string>() { "Test-1", "Test-2", "Test-3" }));
Repo.Add(new EFProduct(Guid.NewGuid(), 15, "Games", new List<string>() { "Test-1", "Test-2", "Test-3" }));
Repo.Add(new EFProduct(Guid.NewGuid(), 20, "Games", new List<string>() { "Test-1", "Test-2", "Test-3" }));
}
public static List<EFProduct> Repo { get; set; }
public IEnumerable<Product> GetProducts(ISpecification<Product, IProductSpecificationVisitor> spec)
{
var visitor = new ExpressionQueryProductSpecVisitor();
spec.Accept(visitor);
var expression = visitor.Expr;
foreach (var efProduct in Repo.AsQueryable().Where(expression).ToList())
{
var product = ConvertPersistenceToDomain(efProduct);
yield return product;
}
}
private Product ConvertPersistenceToDomain(EFProduct entity)
{
return new Product(entity.Price, entity.Category, entity.Tags.Select(dl => dl.Name).ToList());
}
}
}