Skip to content

Commit 0b690d4

Browse files
committed
More tests for #2086 compound conditionless logic fix
1 parent 3ff7f57 commit 0b690d4

File tree

6 files changed

+111
-1
lines changed

6 files changed

+111
-1
lines changed

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/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/QueryDslUsageTestsBase.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,15 @@ private void AssertIsNotConditionless(IQueryContainer c)
5151
Query = this.QueryInitializer
5252
};
5353

54+
protected virtual NotConditionlessWhen NotConditionlessWhen => null;
5455
protected virtual ConditionlessWhen ConditionlessWhen => null;
5556

5657
protected readonly QueryContainer ConditionlessQuery = new QueryContainer(new TermQuery { });
5758

58-
[U] public void SeenByVisitor()
59+
protected QueryContainer VerbatimQuery = new QueryContainer(new TermQuery { IsVerbatim = true });
60+
61+
[U]
62+
public void SeenByVisitor()
5963
{
6064
var visitor = new DslPrettyPrintVisitor(TestClient.CreateSettings());
6165
var query = this.QueryFluent(new QueryContainerDescriptor<Project>());
@@ -79,6 +83,45 @@ [U] public void ConditionlessWhenExpectedToBe()
7983
((IQueryContainer)this.QueryInitializer).IsConditionless.Should().BeFalse();
8084
}
8185

86+
[U]
87+
public void NotConditionlessWhenExpectedToBe()
88+
{
89+
if (NotConditionlessWhen == null) return;
90+
foreach (var when in NotConditionlessWhen)
91+
{
92+
var query = this.QueryFluent(new QueryContainerDescriptor<Project>());
93+
when(query);
94+
query = this.QueryInitializer;
95+
when(query);
96+
}
97+
}
98+
8299
private void IsConditionless(IQueryContainer q, bool be) => q.IsConditionless.Should().Be(be);
83100
}
101+
102+
public abstract class NotConditionlessWhen : List<Action<QueryContainer>>
103+
{
104+
}
105+
public class NotConditionlessWhen<TQuery> : NotConditionlessWhen where TQuery : IQuery
106+
{
107+
private readonly Func<IQueryContainer, TQuery> _dispatch;
108+
109+
public NotConditionlessWhen(Func<IQueryContainer, TQuery> dispatch)
110+
{
111+
_dispatch = dispatch;
112+
}
113+
114+
public void Add(Action<TQuery> when)
115+
{
116+
this.Add(q => Assert(q, when));
117+
}
118+
119+
private void Assert(IQueryContainer c, Action<TQuery> when)
120+
{
121+
TQuery q = this._dispatch(c);
122+
when(q);
123+
q.Conditionless.Should().BeFalse();
124+
c.IsConditionless.Should().BeFalse();
125+
}
126+
}
84127
}

0 commit comments

Comments
 (0)