Skip to content

Commit 07d070c

Browse files
committed
added some boolean integration tests
1 parent abfd768 commit 07d070c

File tree

7 files changed

+174
-0
lines changed

7 files changed

+174
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using System.Reflection;
3+
using FluentAssertions;
4+
using NUnit.Framework;
5+
using Nest.Tests.Integration;
6+
using Nest.Tests.MockData;
7+
using Nest.Tests.MockData.Domain;
8+
9+
namespace Nest.Tests.Integration.Integration.Query
10+
{
11+
[TestFixture]
12+
public class BoolQueryResults : IntegrationTests
13+
{
14+
15+
/// <summary>
16+
/// Document used in test.
17+
/// </summary>
18+
private ElasticSearchProject _LookFor;
19+
20+
[TestFixtureSetUp]
21+
public void Initialize()
22+
{
23+
_LookFor = NestTestData.Session.Single<ElasticSearchProject>().Get();
24+
_LookFor.Name = "one two three four";
25+
var status = this._client.Index(_LookFor, new IndexParameters { Refresh = true }).ConnectionStatus;
26+
Assert.True(status.Success, status.Result);
27+
}
28+
29+
[Test]
30+
public void SingleTerm()
31+
{
32+
var results = _client.Search<BoolTerm>(s => s
33+
.Query(q =>
34+
q.Term(p => p.Name1, "a1")
35+
)
36+
);
37+
this.AssertBoolQueryResults(results, expectedCount: 1);
38+
}
39+
40+
[Test]
41+
public void TwoTermsOfSingleDocument()
42+
{
43+
var results = _client.Search<BoolTerm>(s => s
44+
.Query(q =>
45+
q.Term(p => p.Name1, "a1") && q.Term(p=>p.Name2, "b1")
46+
)
47+
);
48+
this.AssertBoolQueryResults(results, expectedCount: 1);
49+
}
50+
51+
[Test]
52+
public void TwoTermsOfNoDocument()
53+
{
54+
var results = _client.Search<BoolTerm>(s => s
55+
.Query(q =>
56+
q.Term(p => p.Name1, "a1") && q.Term(p=>p.Name2, "b2")
57+
)
58+
);
59+
this.AssertBoolQueryResults(results, expectedCount: 0);
60+
}
61+
62+
[Test]
63+
public void ThreeTermsOfOneDocument()
64+
{
65+
var results = _client.Search<BoolTerm>(s => s
66+
.Query(q =>
67+
q.Term(p => p.Name1, "a1") && (q.Term(p => p.Name2, "b2") || q.Term(p => p.Name2, "b1"))
68+
)
69+
);
70+
this.AssertBoolQueryResults(results, expectedCount: 1);
71+
}
72+
73+
[Test]
74+
public void TwoTermsOfTwoDocuments()
75+
{
76+
var results = _client.Search<BoolTerm>(s => s
77+
.Query(q =>
78+
q.Term(p => p.Name1, "a1") || q.Term(p => p.Name2, "b2")
79+
)
80+
);
81+
this.AssertBoolQueryResults(results, expectedCount: 2);
82+
}
83+
84+
[Test]
85+
public void FourTermsOfTwoDocuments()
86+
{
87+
var results = _client.Search<BoolTerm>(s => s
88+
.Query(q =>
89+
(q.Term(p => p.Name1, "a1") && q.Term(p => p.Name2, "b1"))
90+
|| (q.Term(p => p.Name1, "a2") && q.Term(p => p.Name2, "b2"))
91+
)
92+
);
93+
this.AssertBoolQueryResults(results, expectedCount: 2);
94+
}
95+
96+
private void AssertBoolQueryResults(IQueryResponse<BoolTerm> results, int expectedCount)
97+
{
98+
Assert.True(results.IsValid, results.ConnectionStatus.Result);
99+
Assert.True(results.ConnectionStatus.Success, results.ConnectionStatus.Result);
100+
Assert.GreaterOrEqual(results.Total, expectedCount);
101+
}
102+
}
103+
}

src/Nest.Tests.Integration/IntegrationSetup.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,36 @@ public static void Setup()
1919

2020
var projects = NestTestData.Data;
2121
var people = NestTestData.People;
22+
var boolTerms = NestTestData.BoolTerms;
2223

2324
client.CreateIndex(ElasticsearchConfiguration.DefaultIndex, c => c
2425
.NumberOfReplicas(0)
2526
.NumberOfShards(1)
2627
.AddMapping<ElasticSearchProject>(m => m.MapFromAttributes())
2728
.AddMapping<Person>(m => m.MapFromAttributes())
29+
.AddMapping<BoolTerm>(m => m.Properties(pp=>pp
30+
.String(sm => sm.Name(p => p.Name1).Index(FieldIndexOption.not_analyzed))
31+
.String(sm => sm.Name(p => p.Name2).Index(FieldIndexOption.not_analyzed))
32+
))
2833
);
2934
client.CreateIndex(ElasticsearchConfiguration.DefaultIndex + "_clone", c => c
3035
.NumberOfReplicas(0)
3136
.NumberOfShards(1)
3237
.AddMapping<ElasticSearchProject>(m => m.MapFromAttributes())
3338
.AddMapping<Person>(m => m.MapFromAttributes())
39+
.AddMapping<BoolTerm>(m => m.Properties(pp => pp
40+
.String(sm => sm.Name(p => p.Name1).Index(FieldIndexOption.not_analyzed))
41+
.String(sm => sm.Name(p => p.Name2).Index(FieldIndexOption.not_analyzed))
42+
))
3443
);
3544

3645
var bulk = new BulkDescriptor();
3746
foreach (var p in projects)
3847
bulk.Index<ElasticSearchProject>(i=>i.Object(p));
3948
foreach (var p in people)
4049
bulk.Index<Person>(i => i.Object(p));
50+
foreach (var p in boolTerms)
51+
bulk.Index<BoolTerm>(i => i.Object(p));
4152
client.Bulk(bulk);
4253

4354
client.Refresh(new[] {ElasticsearchConfiguration.DefaultIndex, ElasticsearchConfiguration.DefaultIndex + "_clone"});

src/Nest.Tests.Integration/Nest.Tests.Integration.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
<Compile Include="Integration\Filter\MissingExistsFilterTests.cs" />
102102
<Compile Include="Integration\Filter\PrefixFilterTests.cs" />
103103
<Compile Include="Integration\Filter\RangeFilterTests.cs" />
104+
<Compile Include="Integration\Query\BoolQueryResults.cs" />
104105
<Compile Include="Integration\Query\TermToString.cs" />
105106
<Compile Include="Integration\Query\TextPhraseQueryTests.cs" />
106107
<Compile Include="Integration\Query\TextPhrasePrefixQueryTests.cs" />
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using AutoPoco.Engine;
6+
7+
namespace Nest.Tests.MockData.DataSources
8+
{
9+
/// <summary>
10+
/// Generator of random ints.
11+
/// </summary>
12+
public class IncrementalNameSource : DatasourceBase<string>
13+
{
14+
private int _currentId;
15+
private readonly string _prefix;
16+
17+
public IncrementalNameSource(string prefix = "a")
18+
{
19+
this._prefix = prefix;
20+
}
21+
22+
public override string Next(IGenerationSession session)
23+
{
24+
var name = this._prefix + this._currentId++;
25+
return name;
26+
}
27+
}
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Nest.Tests.MockData.Domain
7+
{
8+
public class BoolTerm
9+
{
10+
public string Name1 { get; set; }
11+
public string Name2 { get; set; }
12+
}
13+
}

src/Nest.Tests.MockData/Nest.Tests.MockData.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
</ItemGroup>
6868
<ItemGroup>
6969
<Compile Include="DataSources\BoolSource.cs" />
70+
<Compile Include="DataSources\IncrementalNameSource.cs" />
7071
<Compile Include="DataSources\FloatArraySource.cs" />
7172
<Compile Include="DataSources\IntSource.cs" />
7273
<Compile Include="DataSources\ElasticSearchProjectDescriptionSource.cs" />
@@ -77,6 +78,7 @@
7778
<Compile Include="DataSources\DoubleSource.cs" />
7879
<Compile Include="DataSources\LongSource.cs" />
7980
<Compile Include="DataSources\FloatSource.cs" />
81+
<Compile Include="Domain\BoolTerm.cs" />
8082
<Compile Include="Domain\GeoShape.cs" />
8183
<Compile Include="Domain\ElasticSearchProject.cs" />
8284
<Compile Include="Domain\GeoLocation.cs" />

src/Nest.Tests.MockData/NestTestData.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace Nest.Tests.MockData
1414
public static class NestTestData
1515
{
1616
private static IEnumerable<Person> _People { get; set; }
17+
private static IEnumerable<BoolTerm> _BoolTerms { get; set; }
1718
private static IEnumerable<ElasticSearchProject> _Data { get; set; }
1819
public static IGenerationSession _Session { get; set; }
1920
public static IGenerationSession Session
@@ -34,6 +35,9 @@ public static IGenerationSession Session
3435
x.Include<GeoLocation>()
3536
.Setup(c => c.lat).Use<FloatSource>()
3637
.Setup(c => c.lon).Use<FloatSource>();
38+
x.Include<BoolTerm>()
39+
.Setup(c => c.Name1).Use<IncrementalNameSource>("a")
40+
.Setup(c => c.Name2).Use<IncrementalNameSource>("b");
3741
x.Include<Person>()
3842
.Setup(c => c.Id).Use<IntegerIdSource>()
3943
.Setup(c => c.Email).Use<EmailAddressSource>()
@@ -74,6 +78,18 @@ public static IEnumerable<Person> People
7478
}
7579
}
7680

81+
public static IEnumerable<BoolTerm> BoolTerms
82+
{
83+
get
84+
{
85+
if (_BoolTerms != null)
86+
return _BoolTerms;
87+
_BoolTerms = Session.List<BoolTerm>(100).Get();
88+
return _BoolTerms;
89+
90+
}
91+
}
92+
7793
public static IEnumerable<ElasticSearchProject> Data
7894
{
7995
get

0 commit comments

Comments
 (0)