Skip to content

Commit 1a38675

Browse files
committed
Add query tag tests for custom specs.
1 parent 3a105ba commit 1a38675

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
namespace Tests.Evaluators;
2+
3+
[Collection("SharedCollection")]
4+
public class QueryTagEvaluatorCustomTests(TestFactory factory) : IntegrationTest(factory)
5+
{
6+
private static readonly QueryTagEvaluator _evaluator = QueryTagEvaluator.Instance;
7+
8+
[Fact]
9+
public void QueriesMatch_GivenTag()
10+
{
11+
var tag = "asd";
12+
13+
var spec = new CustomSpecification<Country>();
14+
spec.Tags.Add(tag);
15+
16+
var actual = _evaluator.GetQuery(DbContext.Countries, spec)
17+
.ToQueryString();
18+
19+
var expected = DbContext.Countries
20+
.TagWith(tag)
21+
.ToQueryString();
22+
23+
actual.Should().Be(expected);
24+
}
25+
26+
[Fact]
27+
public void QueriesMatch_GivenMultipleTags()
28+
{
29+
var tag1 = "asd";
30+
var tag2 = "qwe";
31+
32+
var spec = new CustomSpecification<Country>();
33+
spec.Tags.Add(tag1);
34+
spec.Tags.Add(tag2);
35+
36+
var actual = _evaluator.GetQuery(DbContext.Countries, spec)
37+
.ToQueryString();
38+
39+
var expected = DbContext.Countries
40+
.TagWith(tag1)
41+
.TagWith(tag2)
42+
.ToQueryString();
43+
44+
actual.Should().Be(expected);
45+
}
46+
47+
48+
[Fact]
49+
public void DoesNothing_GivenNoTag()
50+
{
51+
var spec = new CustomSpecification<Country>();
52+
53+
var actual = _evaluator.GetQuery(DbContext.Countries, spec)
54+
.Expression
55+
.ToString();
56+
57+
var expected = DbContext.Countries
58+
.AsQueryable()
59+
.Expression
60+
.ToString();
61+
62+
actual.Should().Be(expected);
63+
}
64+
65+
[Fact]
66+
public void Applies_GivenSingleTag()
67+
{
68+
var tag = "asd";
69+
70+
var spec = new CustomSpecification<Country>();
71+
spec.Tags.Add(tag);
72+
73+
var actual = _evaluator.GetQuery(DbContext.Countries, spec)
74+
.Expression
75+
.ToString();
76+
77+
var expected = DbContext.Countries
78+
.TagWith(tag)
79+
.Expression
80+
.ToString();
81+
82+
actual.Should().Be(expected);
83+
}
84+
85+
[Fact]
86+
public void Applies_GivenTwoTags()
87+
{
88+
var tag1 = "asd";
89+
var tag2 = "qwe";
90+
91+
var spec = new CustomSpecification<Country>();
92+
spec.Tags.Add(tag1);
93+
spec.Tags.Add(tag2);
94+
95+
var actual = _evaluator.GetQuery(DbContext.Countries, spec)
96+
.Expression
97+
.ToString();
98+
99+
var expected = DbContext.Countries
100+
.TagWith(tag1)
101+
.TagWith(tag2)
102+
.Expression
103+
.ToString();
104+
105+
actual.Should().Be(expected);
106+
}
107+
108+
[Fact]
109+
public void Applies_GivenMultipleTags()
110+
{
111+
var tag1 = "asd";
112+
var tag2 = "qwe";
113+
var tag3 = "zxc";
114+
115+
var spec = new CustomSpecification<Country>();
116+
spec.Tags.Add(tag1);
117+
spec.Tags.Add(tag2);
118+
spec.Tags.Add(tag3);
119+
120+
var actual = _evaluator.GetQuery(DbContext.Countries, spec)
121+
.Expression
122+
.ToString();
123+
124+
var expected = DbContext.Countries
125+
.TagWith(tag1)
126+
.TagWith(tag2)
127+
.TagWith(tag3)
128+
.Expression
129+
.ToString();
130+
131+
actual.Should().Be(expected);
132+
}
133+
134+
public class CustomSpecification<T> : ISpecification<T>
135+
{
136+
public List<string> Tags { get; set; } = new();
137+
public List<WhereExpressionInfo<T>> Where { get; set; } = new();
138+
public List<SearchExpressionInfo<T>> Search { get; set; } = new();
139+
public IEnumerable<string> QueryTags => Tags;
140+
public IEnumerable<SearchExpressionInfo<T>> SearchCriterias => Search;
141+
public IEnumerable<WhereExpressionInfo<T>> WhereExpressions => Where;
142+
143+
public ISpecificationBuilder<T> Query => throw new NotImplementedException();
144+
public IEnumerable<OrderExpressionInfo<T>> OrderExpressions => throw new NotImplementedException();
145+
public IEnumerable<IncludeExpressionInfo> IncludeExpressions => throw new NotImplementedException();
146+
public IEnumerable<string> IncludeStrings => throw new NotImplementedException();
147+
public Dictionary<string, object> Items => throw new NotImplementedException();
148+
public int Take => throw new NotImplementedException();
149+
public int Skip => throw new NotImplementedException();
150+
public Func<IEnumerable<T>, IEnumerable<T>>? PostProcessingAction => throw new NotImplementedException();
151+
public bool CacheEnabled => throw new NotImplementedException();
152+
public string? CacheKey => throw new NotImplementedException();
153+
public bool AsTracking => throw new NotImplementedException();
154+
public bool AsNoTracking => throw new NotImplementedException();
155+
public bool AsSplitQuery => throw new NotImplementedException();
156+
public bool AsNoTrackingWithIdentityResolution => throw new NotImplementedException();
157+
public bool IgnoreQueryFilters => throw new NotImplementedException();
158+
public bool IgnoreAutoIncludes => throw new NotImplementedException();
159+
public IEnumerable<T> Evaluate(IEnumerable<T> entities)
160+
=> throw new NotImplementedException();
161+
public bool IsSatisfiedBy(T entity)
162+
=> throw new NotImplementedException();
163+
}
164+
}

0 commit comments

Comments
 (0)