Skip to content

Commit 6ec05ea

Browse files
codebrainrusscam
authored andcommitted
Remove circle from Shape tests, change integration test setup to use Shape() instead of GeoShape(), change default index for Shape types. (#4177)
(cherry picked from commit c1c95cf)
1 parent 06ba5c3 commit 6ec05ea

File tree

5 files changed

+132
-35
lines changed

5 files changed

+132
-35
lines changed

src/Tests/Tests.Domain/Extensions/ConnectionSettingsExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public static ConnectionSettings ApplyDomainSettings(this ConnectionSettings set
2727
.DefaultMappingFor<Metric>(map => map
2828
.IndexName("server-metrics")
2929
)
30+
.DefaultMappingFor<GeoShape>(map => map
31+
.IndexName("geoshapes")
32+
)
3033
.DefaultMappingFor<Shape>(map => map
3134
.IndexName("shapes")
3235
);

src/Tests/Tests.Domain/GeoShape.cs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading;
4+
using Bogus;
5+
using Nest;
6+
using Tests.Configuration;
7+
8+
namespace Tests.Domain
9+
{
10+
public class GeoShape
11+
{
12+
private static int _idState;
13+
public ICircleGeoShape Circle { get; set; }
14+
public IEnvelopeGeoShape Envelope { get; set; }
15+
16+
public static Faker<GeoShape> Generator { get; } =
17+
new Faker<GeoShape>()
18+
.UseSeed(TestConfiguration.Instance.Seed)
19+
.RuleFor(p => p.Id, p => Interlocked.Increment(ref _idState))
20+
.RuleFor(p => p.GeometryCollection, p =>
21+
new GeometryCollection(new List<IGeoShape>
22+
{
23+
GenerateRandomPoint(p),
24+
GenerateRandomMultiPoint(p),
25+
GenerateLineString(p),
26+
GenerateMultiLineString(p),
27+
GeneratePolygon(p),
28+
GenerateMultiPolygon(p)
29+
})
30+
)
31+
.RuleFor(p => p.Envelope, p => new EnvelopeGeoShape(new[]
32+
{
33+
new GeoCoordinate(45, 0),
34+
new GeoCoordinate(0, 45)
35+
}))
36+
.RuleFor(p => p.Circle, p => new CircleGeoShape(GenerateGeoCoordinate(p), $"{p.Random.Int(1, 100)}km"));
37+
38+
public IGeometryCollection GeometryCollection { get; set; }
39+
40+
public int Id { get; set; }
41+
42+
public static IList<GeoShape> Shapes { get; } = Generator.Clone().Generate(10);
43+
44+
private static IPointGeoShape GenerateRandomPoint(Faker p) =>
45+
new PointGeoShape(GenerateGeoCoordinate(p));
46+
47+
private static IMultiPointGeoShape GenerateRandomMultiPoint(Faker p) =>
48+
new MultiPointGeoShape(GenerateGeoCoordinates(p, p.Random.Int(1, 5)));
49+
50+
private static ILineStringGeoShape GenerateLineString(Faker p) =>
51+
new LineStringGeoShape(GenerateGeoCoordinates(p, 3));
52+
53+
private static IMultiLineStringGeoShape GenerateMultiLineString(Faker p)
54+
{
55+
var coordinates = new List<IEnumerable<GeoCoordinate>>();
56+
for (var i = 0; i < p.Random.Int(1, 5); i++)
57+
coordinates.Add(GenerateGeoCoordinates(p, 3));
58+
59+
return new MultiLineStringGeoShape(coordinates);
60+
}
61+
62+
private static IPolygonGeoShape GeneratePolygon(Faker p) => new PolygonGeoShape(new List<IEnumerable<GeoCoordinate>>
63+
{
64+
GeneratePolygonCoordinates(p, GenerateGeoCoordinate(p))
65+
});
66+
67+
private static IMultiPolygonGeoShape GenerateMultiPolygon(Faker p) => new MultiPolygonGeoShape(
68+
new List<IEnumerable<IEnumerable<GeoCoordinate>>>
69+
{
70+
new[] { GeneratePolygonCoordinates(p, GenerateGeoCoordinate(p)) }
71+
});
72+
73+
private static GeoCoordinate GenerateGeoCoordinate(Faker p) =>
74+
new GeoCoordinate(p.Address.Latitude(), p.Address.Longitude());
75+
76+
private static IEnumerable<GeoCoordinate> GenerateGeoCoordinates(Faker p, int count)
77+
{
78+
var points = new List<GeoCoordinate>();
79+
80+
for (var i = 0; i < count; i++)
81+
points.Add(GenerateGeoCoordinate(p));
82+
83+
return points;
84+
}
85+
86+
// adapted from https://gis.stackexchange.com/a/103465/30046
87+
private static IEnumerable<GeoCoordinate> GeneratePolygonCoordinates(Faker p, GeoCoordinate centroid, double maxDistance = 0.0002)
88+
{
89+
const int maxPoints = 20;
90+
var points = new List<GeoCoordinate>(maxPoints);
91+
double startingAngle = (int)(p.Random.Double() * (1d / 3) * Math.PI);
92+
var angle = startingAngle;
93+
for (var i = 0; i < maxPoints; i++)
94+
{
95+
var distance = p.Random.Double() * maxDistance;
96+
points.Add(new GeoCoordinate(centroid.Latitude + Math.Sin(angle) * distance, centroid.Longitude + Math.Cos(angle) * distance));
97+
angle = angle + p.Random.Double() * (2d / 3) * Math.PI;
98+
if (angle > 2 * Math.PI) break;
99+
}
100+
101+
// close the polygon
102+
points.Add(points[0]);
103+
return points;
104+
}
105+
}
106+
}

src/Tests/Tests.Domain/Shape.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace Tests.Domain
1010
public class Shape
1111
{
1212
private static int _idState;
13-
public ICircleGeoShape Circle { get; set; }
1413
public IEnvelopeGeoShape Envelope { get; set; }
1514

1615
public static Faker<Shape> Generator { get; } =
@@ -32,8 +31,7 @@ public class Shape
3231
{
3332
new GeoCoordinate(45, 0),
3433
new GeoCoordinate(0, 45)
35-
}))
36-
.RuleFor(p => p.Circle, p => new CircleGeoShape(GenerateGeoCoordinate(p), $"{p.Random.Int(1, 100)}km"));
34+
}));
3735

3836
public IGeometryCollection GeometryCollection { get; set; }
3937

src/Tests/Tests/QueryDsl/Geo/GeoShape/GeoShapeSerializationTests.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ namespace Tests.QueryDsl.Geo.GeoShape
1616
{
1717
public abstract class GeoShapeSerializationTestsBase
1818
: ApiIntegrationTestBase<IntrusiveOperationCluster,
19-
ISearchResponse<Domain.Shape>,
19+
ISearchResponse<Domain.GeoShape>,
2020
ISearchRequest,
21-
SearchDescriptor<Domain.Shape>,
22-
SearchRequest<Domain.Shape>>
21+
SearchDescriptor<Domain.GeoShape>,
22+
SearchRequest<Domain.GeoShape>>
2323
{
2424
private readonly IEnumerable<GeoCoordinate> _coordinates =
25-
Domain.Shape.Shapes.First().Envelope.Coordinates;
25+
Domain.GeoShape.Shapes.First().Envelope.Coordinates;
2626

2727
protected GeoShapeSerializationTestsBase(IntrusiveOperationCluster cluster, EndpointUsage usage)
2828
: base(cluster, usage) { }
@@ -53,7 +53,7 @@ protected GeoShapeSerializationTestsBase(IntrusiveOperationCluster cluster, Endp
5353

5454
protected override int ExpectStatusCode => 200;
5555

56-
protected override Func<SearchDescriptor<Domain.Shape>, ISearchRequest> Fluent => s => s
56+
protected override Func<SearchDescriptor<Domain.GeoShape>, ISearchRequest> Fluent => s => s
5757
.Index(Index)
5858
.Query(q => q
5959
.GeoShape(c => c
@@ -72,13 +72,13 @@ protected GeoShapeSerializationTestsBase(IntrusiveOperationCluster cluster, Endp
7272

7373
protected abstract string Index { get; }
7474

75-
protected override SearchRequest<Domain.Shape> Initializer => new SearchRequest<Domain.Shape>(Index)
75+
protected override SearchRequest<Domain.GeoShape> Initializer => new SearchRequest<Domain.GeoShape>(Index)
7676
{
7777
Query = new GeoShapeQuery
7878
{
7979
Name = "named_query",
8080
Boost = 1.1,
81-
Field = Infer.Field<Domain.Shape>(p => p.Envelope),
81+
Field = Infer.Field<Domain.GeoShape>(p => p.Envelope),
8282
Shape = new EnvelopeGeoShape(_coordinates),
8383
Relation = GeoShapeRelation.Intersects,
8484
IgnoreUnmapped = true,
@@ -90,11 +90,11 @@ protected GeoShapeSerializationTestsBase(IntrusiveOperationCluster cluster, Endp
9090
protected override LazyResponses ClientUsage() => Calls(
9191
(client, f) => client.Search(f),
9292
(client, f) => client.SearchAsync(f),
93-
(client, r) => client.Search<Domain.Shape>(r),
94-
(client, r) => client.SearchAsync<Domain.Shape>(r)
93+
(client, r) => client.Search<Domain.GeoShape>(r),
94+
(client, r) => client.SearchAsync<Domain.GeoShape>(r)
9595
);
9696

97-
protected override void ExpectResponse(ISearchResponse<Domain.Shape> response)
97+
protected override void ExpectResponse(ISearchResponse<Domain.GeoShape> response)
9898
{
9999
response.IsValid.Should().BeTrue();
100100
response.Documents.Count.Should().Be(10);
@@ -118,7 +118,7 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues
118118
.NumberOfShards(1)
119119
.NumberOfReplicas(0)
120120
)
121-
.Map<Domain.Shape>(mm => mm
121+
.Map<Domain.GeoShape>(mm => mm
122122
.AutoMap()
123123
.Properties(p => p
124124
.GeoShape(g => g
@@ -140,7 +140,7 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues
140140

141141
var bulkResponse = Client.Bulk(b => b
142142
.Index(Index)
143-
.IndexMany(Domain.Shape.Shapes)
143+
.IndexMany(Domain.GeoShape.Shapes)
144144
.Refresh(Refresh.WaitFor)
145145
);
146146

@@ -167,7 +167,7 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues
167167
.NumberOfShards(1)
168168
.NumberOfReplicas(0)
169169
)
170-
.Map<Domain.Shape>(mm => mm
170+
.Map<Domain.GeoShape>(mm => mm
171171
.AutoMap()
172172
.Properties(p => p
173173
.GeoShape(g => g
@@ -189,7 +189,7 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues
189189

190190
var bulk = new List<object>();
191191

192-
foreach (var shape in Domain.Shape.Shapes)
192+
foreach (var shape in Domain.GeoShape.Shapes)
193193
{
194194
bulk.Add(new { index = new { _index = Index, _id = shape.Id } });
195195
bulk.Add(new
@@ -210,7 +210,7 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues
210210
throw new Exception($"Error indexing shapes for integration test: {bulkResponse.DebugInformation}");
211211
}
212212

213-
protected override void ExpectResponse(ISearchResponse<Domain.Shape> response)
213+
protected override void ExpectResponse(ISearchResponse<Domain.GeoShape> response)
214214
{
215215
base.ExpectResponse(response);
216216

src/Tests/Tests/QueryDsl/Specialized/Shape/ShapeSerializationTests.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public class ShapeSerializationTests : ShapeSerializationTestsBase
107107
public ShapeSerializationTests(IntrusiveOperationCluster cluster, EndpointUsage usage)
108108
: base(cluster, usage) { }
109109

110-
protected override string Index => "geoshapes";
110+
protected override string Index => "shapes";
111111

112112
protected override void IntegrationSetup(IElasticClient client, CallUniqueValues values)
113113
{
@@ -122,16 +122,12 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues
122122
.Map<Domain.Shape>(mm => mm
123123
.AutoMap()
124124
.Properties(p => p
125-
.GeoShape(g => g
125+
.Shape(g => g
126126
.Name(n => n.GeometryCollection)
127127
)
128-
.GeoShape(g => g
128+
.Shape(g => g
129129
.Name(n => n.Envelope)
130130
)
131-
.GeoShape(g => g
132-
.Name(n => n.Circle)
133-
.Strategy(GeoStrategy.Recursive)
134-
)
135131
)
136132
)
137133
);
@@ -156,7 +152,7 @@ public class ShapeGeoWKTSerializationTests : ShapeSerializationTestsBase
156152
public ShapeGeoWKTSerializationTests(IntrusiveOperationCluster cluster, EndpointUsage usage)
157153
: base(cluster, usage) { }
158154

159-
protected override string Index => "wkt-geoshapes";
155+
protected override string Index => "wkt-shapes";
160156

161157
protected override void IntegrationSetup(IElasticClient client, CallUniqueValues values)
162158
{
@@ -171,16 +167,12 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues
171167
.Map<Domain.Shape>(mm => mm
172168
.AutoMap()
173169
.Properties(p => p
174-
.GeoShape(g => g
170+
.Shape(g => g
175171
.Name(n => n.GeometryCollection)
176172
)
177-
.GeoShape(g => g
173+
.Shape(g => g
178174
.Name(n => n.Envelope)
179175
)
180-
.GeoShape(g => g
181-
.Name(n => n.Circle)
182-
.Strategy(GeoStrategy.Recursive)
183-
)
184176
)
185177
)
186178
);
@@ -197,8 +189,7 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues
197189
{
198190
id = shape.Id,
199191
geometryCollection = GeoWKTWriter.Write(shape.GeometryCollection),
200-
envelope = GeoWKTWriter.Write(shape.Envelope),
201-
circle = shape.Circle
192+
envelope = GeoWKTWriter.Write(shape.Envelope)
202193
});
203194
}
204195

@@ -244,7 +235,6 @@ protected override void ExpectResponse(ISearchResponse<Domain.Shape> response)
244235
jValue.Value.Should().BeOfType<string>();
245236
jValue = (JValue)jObject["envelope"];
246237
jValue.Value.Should().BeOfType<string>();
247-
jObject["circle"].Should().BeOfType<JObject>();
248238
}
249239
}
250240
}

0 commit comments

Comments
 (0)