Skip to content

Commit 975e64a

Browse files
committed
More tests for #2086 compound conditionless logic fix
1 parent 97f2085 commit 975e64a

File tree

10 files changed

+158
-0
lines changed

10 files changed

+158
-0
lines changed

src/Tests/QueryDsl/Compound/And/AndQueryUsageTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,12 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
5555
{
5656
{ q=>q.Filters = null }, { q=> q.Filters = Enumerable.Empty<QueryContainer>() }, { q=>q.Filters = new [] { ConditionlessQuery } }
5757
};
58+
59+
protected override NotConditionlessWhen NotConditionlessWhen => new NotConditionlessWhen<IAndQuery>(a => a.And)
60+
{
61+
q => q.Filters = new [] { VerbatimQuery },
62+
q => q.Filters = new [] { ConditionlessQuery, VerbatimQuery }
63+
};
64+
5865
}
5966
}

src/Tests/QueryDsl/Compound/Bool/BoolQueryUsageTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,34 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
7979
},
8080
};
8181

82+
protected override NotConditionlessWhen NotConditionlessWhen => new NotConditionlessWhen<IBoolQuery>(a => a.Bool)
83+
{
84+
q => {
85+
q.MustNot = new [] { VerbatimQuery };
86+
q.Should = null;
87+
q.Must = null;
88+
q.Filter = null;
89+
},
90+
q => {
91+
q.MustNot = null;
92+
q.Should = new [] { VerbatimQuery };
93+
q.Must = null;
94+
q.Filter = null;
95+
},
96+
q => {
97+
q.MustNot = null;
98+
q.Should = null;
99+
q.Must = new [] { VerbatimQuery };
100+
q.Filter = null;
101+
},
102+
q => {
103+
q.MustNot = null;
104+
q.Should = null;
105+
q.Must = null;
106+
q.Filter = new [] { VerbatimQuery };
107+
},
108+
};
109+
82110
[U]
83111
public void NullQueryDoesNotCauseANullReferenceException()
84112
{

src/Tests/QueryDsl/Compound/Boosting/BoostingQueryUsageTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,22 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
5757
q.PositiveQuery = ConditionlessQuery;
5858
},
5959
};
60+
61+
protected override NotConditionlessWhen NotConditionlessWhen => new NotConditionlessWhen<IBoostingQuery>(a => a.Boosting)
62+
{
63+
q=> {
64+
q.NegativeQuery = VerbatimQuery;
65+
q.PositiveQuery = VerbatimQuery;
66+
},
67+
q => {
68+
q.NegativeQuery = null;
69+
q.PositiveQuery = VerbatimQuery;
70+
},
71+
q => {
72+
q.NegativeQuery = VerbatimQuery;
73+
q.PositiveQuery = null;
74+
}
75+
};
76+
6077
}
6178
}

src/Tests/QueryDsl/Compound/ConstantScore/ConstantScoreQueryUsageTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,9 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
4545
q => q.Filter = null ,
4646
q => q.Filter = ConditionlessQuery,
4747
};
48+
protected override NotConditionlessWhen NotConditionlessWhen => new NotConditionlessWhen<IConstantScoreQuery>(a => a.ConstantScore)
49+
{
50+
q => q.Filter = VerbatimQuery
51+
};
4852
}
4953
}

src/Tests/QueryDsl/Compound/Dismax/DismaxQueryUsageTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
5757
q => q.Queries = new [] { ConditionlessQuery },
5858
};
5959

60+
protected override NotConditionlessWhen NotConditionlessWhen => new NotConditionlessWhen<IDisMaxQuery>(a => a.DisMax)
61+
{
62+
q => q.Queries = new [] { VerbatimQuery },
63+
q => q.Queries = new [] { VerbatimQuery, ConditionlessQuery },
64+
};
65+
6066
[U]
6167
public void NullQueryDoesNotCauseANullReferenceException()
6268
{

src/Tests/QueryDsl/Compound/Filtered/FilteredQueryUsageTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,21 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
5858
q.Query = ConditionlessQuery;
5959
},
6060
};
61+
62+
protected override NotConditionlessWhen NotConditionlessWhen => new NotConditionlessWhen<IFilteredQuery>(a => a.Filtered)
63+
{
64+
q=> {
65+
q.Filter = VerbatimQuery;
66+
q.Query = null;
67+
},
68+
q => {
69+
q.Filter = null;
70+
q.Query = VerbatimQuery;
71+
},
72+
q => {
73+
q.Filter = null;
74+
q.Query = new BoolQuery { Must = new [] { VerbatimQuery } };
75+
},
76+
};
6177
}
6278
}

src/Tests/QueryDsl/Compound/Indices/IndicesNoMatchQueryUsageTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,17 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
4747
q => q.Query = null,
4848
q => q.Query = ConditionlessQuery
4949
};
50+
51+
protected override NotConditionlessWhen NotConditionlessWhen => new NotConditionlessWhen<IIndicesQuery>(p => p.Indices)
52+
{
53+
q => {
54+
q.Query = VerbatimQuery;
55+
q.NoMatchQuery = null;
56+
},
57+
q => {
58+
q.Query = null;
59+
q.NoMatchQuery = VerbatimQuery;
60+
}
61+
};
5062
}
5163
}

src/Tests/QueryDsl/Compound/Not/NotQueryUsageTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,18 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
5555
{
5656
{ q=>q.Filters = null }, { q=> q.Filters = Enumerable.Empty<QueryContainer>() }, { q=>q.Filters = new [] { ConditionlessQuery } }
5757
};
58+
59+
protected override NotConditionlessWhen NotConditionlessWhen => new NotConditionlessWhen<INotQuery>(a => a.Not)
60+
{
61+
q => {
62+
q.Filters = new [] { VerbatimQuery };
63+
},
64+
q => {
65+
q.Filters = new [] { ConditionlessQuery, VerbatimQuery };
66+
},
67+
q => {
68+
q.Filters = new [] { new QueryContainer(new BoolQuery { Filter = new [] { VerbatimQuery } }) };
69+
},
70+
};
5871
}
5972
}

src/Tests/QueryDsl/Compound/Or/OrQueryUsageTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,18 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
5555
{
5656
{ q=>q.Filters = null }, { q=> q.Filters = Enumerable.Empty<QueryContainer>() }, { q=>q.Filters = new [] { ConditionlessQuery } }
5757
};
58+
59+
protected override NotConditionlessWhen NotConditionlessWhen => new NotConditionlessWhen<IOrQuery>(a => a.Or)
60+
{
61+
q => {
62+
q.Filters = new [] { VerbatimQuery };
63+
},
64+
q => {
65+
q.Filters = new [] { ConditionlessQuery, VerbatimQuery };
66+
},
67+
q => {
68+
q.Filters = new [] { new QueryContainer(new BoolQuery { Filter = new [] { VerbatimQuery } }) };
69+
},
70+
};
5871
}
5972
}

src/Tests/QueryDsl/QueryDslUsageTestsBase.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,13 @@ private void AssertIsNotConditionless(IQueryContainer c)
5454
Query = this.QueryInitializer
5555
};
5656

57+
protected virtual NotConditionlessWhen NotConditionlessWhen => null;
5758
protected virtual ConditionlessWhen ConditionlessWhen => null;
5859

5960
protected QueryContainer ConditionlessQuery = new QueryContainer(new TermQuery { });
6061

62+
protected QueryContainer VerbatimQuery = new QueryContainer(new TermQuery { IsVerbatim = true });
63+
6164
[U]
6265
public void SeenByVisitor()
6366
{
@@ -84,6 +87,19 @@ public void ConditionlessWhenExpectedToBe()
8487
((IQueryContainer)this.QueryInitializer).IsConditionless.Should().BeFalse();
8588
}
8689

90+
[U]
91+
public void NotConditionlessWhenExpectedToBe()
92+
{
93+
if (NotConditionlessWhen == null) return;
94+
foreach (var when in NotConditionlessWhen)
95+
{
96+
var query = this.QueryFluent(new QueryContainerDescriptor<Project>());
97+
when(query);
98+
query = this.QueryInitializer;
99+
when(query);
100+
}
101+
}
102+
87103
private void IsConditionless(IQueryContainer q, bool be) => q.IsConditionless.Should().Be(be);
88104
}
89105

@@ -114,4 +130,30 @@ private void Assert(IQueryContainer c, Action<TQuery> when)
114130
c.IsConditionless.Should().BeTrue();
115131
}
116132
}
133+
134+
public abstract class NotConditionlessWhen : List<Action<QueryContainer>>
135+
{
136+
}
137+
public class NotConditionlessWhen<TQuery> : NotConditionlessWhen where TQuery : IQuery
138+
{
139+
private readonly Func<IQueryContainer, TQuery> _dispatch;
140+
141+
public NotConditionlessWhen(Func<IQueryContainer, TQuery> dispatch)
142+
{
143+
_dispatch = dispatch;
144+
}
145+
146+
public void Add(Action<TQuery> when)
147+
{
148+
this.Add(q => Assert(q, when));
149+
}
150+
151+
private void Assert(IQueryContainer c, Action<TQuery> when)
152+
{
153+
TQuery q = this._dispatch(c);
154+
when(q);
155+
q.Conditionless.Should().BeFalse();
156+
c.IsConditionless.Should().BeFalse();
157+
}
158+
}
117159
}

0 commit comments

Comments
 (0)