Skip to content

Commit 11108d6

Browse files
committed
Add tests for custom specs.
1 parent 6774939 commit 11108d6

2 files changed

Lines changed: 203 additions & 0 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
namespace Tests.Evaluators;
2+
3+
public class WhereEvaluatorCustomSpecTests
4+
{
5+
private static readonly WhereEvaluator _evaluator = WhereEvaluator.Instance;
6+
7+
public record Customer(int Id);
8+
9+
[Fact]
10+
public void Filters_GivenSingleWhereExpression()
11+
{
12+
List<Customer> input = [new(1), new(2), new(3), new(4), new(5)];
13+
List<Customer> expected = [new(4), new(5)];
14+
15+
var spec = new CustomSpecification<Customer>();
16+
spec.Where.Add(new WhereExpressionInfo<Customer>(x => x.Id > 3));
17+
18+
Assert(spec, input, expected);
19+
}
20+
21+
[Fact]
22+
public void Filters_GivenMultipleWhereExpressions()
23+
{
24+
List<Customer> input = [new(1), new(2), new(3), new(4), new(5)];
25+
List<Customer> expected = [new(4)];
26+
27+
var spec = new CustomSpecification<Customer>();
28+
spec.Where.Add(new WhereExpressionInfo<Customer>(x => x.Id > 3));
29+
spec.Where.Add(new WhereExpressionInfo<Customer>(x => x.Id < 5));
30+
31+
Assert(spec, input, expected);
32+
}
33+
34+
[Fact]
35+
public void DoesNotFilter_GivenNoWhereExpression()
36+
{
37+
List<Customer> input = [new(1), new(2), new(3), new(4), new(5)];
38+
List<Customer> expected = [new(1), new(2), new(3), new(4), new(5)];
39+
40+
var spec = new CustomSpecification<Customer>();
41+
42+
Assert(spec, input, expected);
43+
}
44+
45+
private static void Assert<T>(ISpecification<T> spec, List<T> input, List<T> expected) where T : class
46+
{
47+
var actualForIEnumerable = _evaluator.Evaluate(input, spec);
48+
actualForIEnumerable.Should().NotBeNull();
49+
actualForIEnumerable.Should().Equal(expected);
50+
51+
var actualForIQueryable = _evaluator.GetQuery(input.AsQueryable(), spec);
52+
actualForIQueryable.Should().NotBeNull();
53+
actualForIQueryable.Should().Equal(expected);
54+
}
55+
56+
public class CustomSpecification<T> : ISpecification<T>
57+
{
58+
public List<WhereExpressionInfo<T>> Where { get; set; } = new();
59+
public List<SearchExpressionInfo<T>> Search { get; set; } = new();
60+
public IEnumerable<SearchExpressionInfo<T>> SearchCriterias => Search;
61+
public IEnumerable<WhereExpressionInfo<T>> WhereExpressions => Where;
62+
63+
public ISpecificationBuilder<T> Query => throw new NotImplementedException();
64+
public IEnumerable<OrderExpressionInfo<T>> OrderExpressions => throw new NotImplementedException();
65+
public IEnumerable<IncludeExpressionInfo> IncludeExpressions => throw new NotImplementedException();
66+
public IEnumerable<string> IncludeStrings => throw new NotImplementedException();
67+
public Dictionary<string, object> Items => throw new NotImplementedException();
68+
public int Take => throw new NotImplementedException();
69+
public int Skip => throw new NotImplementedException();
70+
public Func<IEnumerable<T>, IEnumerable<T>>? PostProcessingAction => throw new NotImplementedException();
71+
public IEnumerable<string> QueryTags => throw new NotImplementedException();
72+
public bool CacheEnabled => throw new NotImplementedException();
73+
public string? CacheKey => throw new NotImplementedException();
74+
public bool AsTracking => throw new NotImplementedException();
75+
public bool AsNoTracking => throw new NotImplementedException();
76+
public bool AsSplitQuery => throw new NotImplementedException();
77+
public bool AsNoTrackingWithIdentityResolution => throw new NotImplementedException();
78+
public bool IgnoreQueryFilters => throw new NotImplementedException();
79+
public bool IgnoreAutoIncludes => throw new NotImplementedException();
80+
public IEnumerable<T> Evaluate(IEnumerable<T> entities)
81+
=> throw new NotImplementedException();
82+
public bool IsSatisfiedBy(T entity)
83+
=> throw new NotImplementedException();
84+
}
85+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
namespace Tests.Validators;
2+
3+
public class WhereValidatorCustomSpecTests
4+
{
5+
private static readonly WhereValidator _validator = WhereValidator.Instance;
6+
7+
public record Customer(int Id, string Name);
8+
9+
[Fact]
10+
public void ReturnsTrue_GivenEmptySpec()
11+
{
12+
var customer = new Customer(1, "Customer1");
13+
14+
var spec = new CustomSpecification<Customer>();
15+
16+
var result = _validator.IsValid(customer, spec);
17+
18+
result.Should().BeTrue();
19+
}
20+
21+
[Fact]
22+
public void ReturnsTrue_GivenSpecWithSingleWhere_WithValidEntity()
23+
{
24+
var customer = new Customer(1, "Customer1");
25+
26+
var spec = new CustomSpecification<Customer>();
27+
spec.Where.Add(new WhereExpressionInfo<Customer>(x => x.Id == 1));
28+
29+
var result = _validator.IsValid(customer, spec);
30+
31+
result.Should().BeTrue();
32+
}
33+
34+
[Fact]
35+
public void ReturnsFalse_GivenSpecWithSingleWhere_WithInvalidEntity()
36+
{
37+
var customer = new Customer(1, "Customer1");
38+
39+
var spec = new CustomSpecification<Customer>();
40+
spec.Where.Add(new WhereExpressionInfo<Customer>(x => x.Id == 2));
41+
42+
var result = _validator.IsValid(customer, spec);
43+
44+
result.Should().BeFalse();
45+
}
46+
47+
[Fact]
48+
public void ReturnsTrue_GivenSpecWithMultipleWhere_WithValidEntity()
49+
{
50+
var customer = new Customer(1, "Customer1");
51+
52+
var spec = new CustomSpecification<Customer>();
53+
spec.Where.Add(new WhereExpressionInfo<Customer>(x => x.Id == 1));
54+
spec.Where.Add(new WhereExpressionInfo<Customer>(x => x.Name == "Customer1"));
55+
56+
var result = _validator.IsValid(customer, spec);
57+
58+
result.Should().BeTrue();
59+
}
60+
61+
[Fact]
62+
public void ReturnsFalse_GivenSpecWithMultipleWhere_WithSingleInvalidValue()
63+
{
64+
var customer = new Customer(1, "Customer1");
65+
66+
var spec = new CustomSpecification<Customer>();
67+
spec.Where.Add(new WhereExpressionInfo<Customer>(x => x.Id == 2));
68+
spec.Where.Add(new WhereExpressionInfo<Customer>(x => x.Name == "Customer1"));
69+
70+
var result = _validator.IsValid(customer, spec);
71+
72+
result.Should().BeFalse();
73+
}
74+
75+
[Fact]
76+
public void ReturnsFalse_GivenSpecWithMultipleWhere_WithAllInvalidValues()
77+
{
78+
var customer = new Customer(1, "Customer1");
79+
80+
var spec = new CustomSpecification<Customer>();
81+
spec.Where.Add(new WhereExpressionInfo<Customer>(x => x.Id == 1));
82+
spec.Where.Add(new WhereExpressionInfo<Customer>(x => x.Name == "Customer2"));
83+
84+
var result = _validator.IsValid(customer, spec);
85+
86+
result.Should().BeFalse();
87+
}
88+
89+
public class CustomSpecification<T> : ISpecification<T>
90+
{
91+
public List<WhereExpressionInfo<T>> Where { get; set; } = new();
92+
public List<SearchExpressionInfo<T>> Search { get; set; } = new();
93+
public IEnumerable<SearchExpressionInfo<T>> SearchCriterias => Search;
94+
public IEnumerable<WhereExpressionInfo<T>> WhereExpressions => Where;
95+
96+
public ISpecificationBuilder<T> Query => throw new NotImplementedException();
97+
public IEnumerable<OrderExpressionInfo<T>> OrderExpressions => throw new NotImplementedException();
98+
public IEnumerable<IncludeExpressionInfo> IncludeExpressions => throw new NotImplementedException();
99+
public IEnumerable<string> IncludeStrings => throw new NotImplementedException();
100+
public Dictionary<string, object> Items => throw new NotImplementedException();
101+
public int Take => throw new NotImplementedException();
102+
public int Skip => throw new NotImplementedException();
103+
public Func<IEnumerable<T>, IEnumerable<T>>? PostProcessingAction => throw new NotImplementedException();
104+
public IEnumerable<string> QueryTags => throw new NotImplementedException();
105+
public bool CacheEnabled => throw new NotImplementedException();
106+
public string? CacheKey => throw new NotImplementedException();
107+
public bool AsTracking => throw new NotImplementedException();
108+
public bool AsNoTracking => throw new NotImplementedException();
109+
public bool AsSplitQuery => throw new NotImplementedException();
110+
public bool AsNoTrackingWithIdentityResolution => throw new NotImplementedException();
111+
public bool IgnoreQueryFilters => throw new NotImplementedException();
112+
public bool IgnoreAutoIncludes => throw new NotImplementedException();
113+
public IEnumerable<T> Evaluate(IEnumerable<T> entities)
114+
=> throw new NotImplementedException();
115+
public bool IsSatisfiedBy(T entity)
116+
=> throw new NotImplementedException();
117+
}
118+
}

0 commit comments

Comments
 (0)