Skip to content

Fix shape serialisation tests #4177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public static ConnectionSettings ApplyDomainSettings(this ConnectionSettings set
.DefaultMappingFor<Metric>(map => map
.IndexName("server-metrics")
)
.DefaultMappingFor<GeoShape>(map => map
.IndexName("geoshapes")
)
.DefaultMappingFor<Shape>(map => map
.IndexName("shapes")
);
Expand Down
106 changes: 106 additions & 0 deletions src/Tests/Tests.Domain/GeoShape.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.Threading;
using Bogus;
using Nest;
using Tests.Configuration;

namespace Tests.Domain
{
public class GeoShape
{
private static int _idState;
public ICircleGeoShape Circle { get; set; }
public IEnvelopeGeoShape Envelope { get; set; }

public static Faker<GeoShape> Generator { get; } =
new Faker<GeoShape>()
.UseSeed(TestConfiguration.Instance.Seed)
.RuleFor(p => p.Id, p => Interlocked.Increment(ref _idState))
.RuleFor(p => p.GeometryCollection, p =>
new GeometryCollection(new List<IGeoShape>
{
GenerateRandomPoint(p),
GenerateRandomMultiPoint(p),
GenerateLineString(p),
GenerateMultiLineString(p),
GeneratePolygon(p),
GenerateMultiPolygon(p)
})
)
.RuleFor(p => p.Envelope, p => new EnvelopeGeoShape(new[]
{
new GeoCoordinate(45, 0),
new GeoCoordinate(0, 45)
}))
.RuleFor(p => p.Circle, p => new CircleGeoShape(GenerateGeoCoordinate(p), $"{p.Random.Int(1, 100)}km"));

public IGeometryCollection GeometryCollection { get; set; }

public int Id { get; set; }

public static IList<GeoShape> Shapes { get; } = Generator.Clone().Generate(10);

private static IPointGeoShape GenerateRandomPoint(Faker p) =>
new PointGeoShape(GenerateGeoCoordinate(p));

private static IMultiPointGeoShape GenerateRandomMultiPoint(Faker p) =>
new MultiPointGeoShape(GenerateGeoCoordinates(p, p.Random.Int(1, 5)));

private static ILineStringGeoShape GenerateLineString(Faker p) =>
new LineStringGeoShape(GenerateGeoCoordinates(p, 3));

private static IMultiLineStringGeoShape GenerateMultiLineString(Faker p)
{
var coordinates = new List<IEnumerable<GeoCoordinate>>();
for (var i = 0; i < p.Random.Int(1, 5); i++)
coordinates.Add(GenerateGeoCoordinates(p, 3));

return new MultiLineStringGeoShape(coordinates);
}

private static IPolygonGeoShape GeneratePolygon(Faker p) => new PolygonGeoShape(new List<IEnumerable<GeoCoordinate>>
{
GeneratePolygonCoordinates(p, GenerateGeoCoordinate(p))
});

private static IMultiPolygonGeoShape GenerateMultiPolygon(Faker p) => new MultiPolygonGeoShape(
new List<IEnumerable<IEnumerable<GeoCoordinate>>>
{
new[] { GeneratePolygonCoordinates(p, GenerateGeoCoordinate(p)) }
});

private static GeoCoordinate GenerateGeoCoordinate(Faker p) =>
new GeoCoordinate(p.Address.Latitude(), p.Address.Longitude());

private static IEnumerable<GeoCoordinate> GenerateGeoCoordinates(Faker p, int count)
{
var points = new List<GeoCoordinate>();

for (var i = 0; i < count; i++)
points.Add(GenerateGeoCoordinate(p));

return points;
}

// adapted from https://gis.stackexchange.com/a/103465/30046
private static IEnumerable<GeoCoordinate> GeneratePolygonCoordinates(Faker p, GeoCoordinate centroid, double maxDistance = 0.0002)
{
const int maxPoints = 20;
var points = new List<GeoCoordinate>(maxPoints);
double startingAngle = (int)(p.Random.Double() * (1d / 3) * Math.PI);
var angle = startingAngle;
for (var i = 0; i < maxPoints; i++)
{
var distance = p.Random.Double() * maxDistance;
points.Add(new GeoCoordinate(centroid.Latitude + Math.Sin(angle) * distance, centroid.Longitude + Math.Cos(angle) * distance));
angle = angle + p.Random.Double() * (2d / 3) * Math.PI;
if (angle > 2 * Math.PI) break;
}

// close the polygon
points.Add(points[0]);
return points;
}
}
}
4 changes: 1 addition & 3 deletions src/Tests/Tests.Domain/Shape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace Tests.Domain
public class Shape
{
private static int _idState;
public ICircleGeoShape Circle { get; set; }
public IEnvelopeGeoShape Envelope { get; set; }

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

public IGeometryCollection GeometryCollection { get; set; }

Expand Down
30 changes: 15 additions & 15 deletions src/Tests/Tests/QueryDsl/Geo/GeoShape/GeoShapeSerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ namespace Tests.QueryDsl.Geo.GeoShape
{
public abstract class GeoShapeSerializationTestsBase
: ApiIntegrationTestBase<IntrusiveOperationCluster,
ISearchResponse<Domain.Shape>,
ISearchResponse<Domain.GeoShape>,
ISearchRequest,
SearchDescriptor<Domain.Shape>,
SearchRequest<Domain.Shape>>
SearchDescriptor<Domain.GeoShape>,
SearchRequest<Domain.GeoShape>>
{
private readonly IEnumerable<GeoCoordinate> _coordinates =
Domain.Shape.Shapes.First().Envelope.Coordinates;
Domain.GeoShape.Shapes.First().Envelope.Coordinates;

protected GeoShapeSerializationTestsBase(IntrusiveOperationCluster cluster, EndpointUsage usage)
: base(cluster, usage) { }
Expand Down Expand Up @@ -53,7 +53,7 @@ protected GeoShapeSerializationTestsBase(IntrusiveOperationCluster cluster, Endp

protected override int ExpectStatusCode => 200;

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

protected abstract string Index { get; }

protected override SearchRequest<Domain.Shape> Initializer => new SearchRequest<Domain.Shape>(Index)
protected override SearchRequest<Domain.GeoShape> Initializer => new SearchRequest<Domain.GeoShape>(Index)
{
Query = new GeoShapeQuery
{
Name = "named_query",
Boost = 1.1,
Field = Infer.Field<Domain.Shape>(p => p.Envelope),
Field = Infer.Field<Domain.GeoShape>(p => p.Envelope),
Shape = new EnvelopeGeoShape(_coordinates),
Relation = GeoShapeRelation.Intersects,
IgnoreUnmapped = true,
Expand All @@ -90,11 +90,11 @@ protected GeoShapeSerializationTestsBase(IntrusiveOperationCluster cluster, Endp
protected override LazyResponses ClientUsage() => Calls(
(client, f) => client.Search(f),
(client, f) => client.SearchAsync(f),
(client, r) => client.Search<Domain.Shape>(r),
(client, r) => client.SearchAsync<Domain.Shape>(r)
(client, r) => client.Search<Domain.GeoShape>(r),
(client, r) => client.SearchAsync<Domain.GeoShape>(r)
);

protected override void ExpectResponse(ISearchResponse<Domain.Shape> response)
protected override void ExpectResponse(ISearchResponse<Domain.GeoShape> response)
{
response.IsValid.Should().BeTrue();
response.Documents.Count.Should().Be(10);
Expand All @@ -118,7 +118,7 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues
.NumberOfShards(1)
.NumberOfReplicas(0)
)
.Map<Domain.Shape>(mm => mm
.Map<Domain.GeoShape>(mm => mm
.AutoMap()
.Properties(p => p
.GeoShape(g => g
Expand All @@ -140,7 +140,7 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues

var bulkResponse = Client.Bulk(b => b
.Index(Index)
.IndexMany(Domain.Shape.Shapes)
.IndexMany(Domain.GeoShape.Shapes)
.Refresh(Refresh.WaitFor)
);

Expand All @@ -167,7 +167,7 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues
.NumberOfShards(1)
.NumberOfReplicas(0)
)
.Map<Domain.Shape>(mm => mm
.Map<Domain.GeoShape>(mm => mm
.AutoMap()
.Properties(p => p
.GeoShape(g => g
Expand All @@ -189,7 +189,7 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues

var bulk = new List<object>();

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

protected override void ExpectResponse(ISearchResponse<Domain.Shape> response)
protected override void ExpectResponse(ISearchResponse<Domain.GeoShape> response)
{
base.ExpectResponse(response);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public class ShapeSerializationTests : ShapeSerializationTestsBase
public ShapeSerializationTests(IntrusiveOperationCluster cluster, EndpointUsage usage)
: base(cluster, usage) { }

protected override string Index => "geoshapes";
protected override string Index => "shapes";

protected override void IntegrationSetup(IElasticClient client, CallUniqueValues values)
{
Expand All @@ -123,16 +123,12 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues
.Map<Domain.Shape>(mm => mm
.AutoMap()
.Properties(p => p
.GeoShape(g => g
.Shape(g => g
.Name(n => n.GeometryCollection)
)
.GeoShape(g => g
.Shape(g => g
.Name(n => n.Envelope)
)
.GeoShape(g => g
.Name(n => n.Circle)
.Strategy(GeoStrategy.Recursive)
)
)
)
);
Expand All @@ -157,7 +153,7 @@ public class ShapeGeoWKTSerializationTests : ShapeSerializationTestsBase
public ShapeGeoWKTSerializationTests(IntrusiveOperationCluster cluster, EndpointUsage usage)
: base(cluster, usage) { }

protected override string Index => "wkt-geoshapes";
protected override string Index => "wkt-shapes";

protected override void IntegrationSetup(IElasticClient client, CallUniqueValues values)
{
Expand All @@ -172,16 +168,12 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues
.Map<Domain.Shape>(mm => mm
.AutoMap()
.Properties(p => p
.GeoShape(g => g
.Shape(g => g
.Name(n => n.GeometryCollection)
)
.GeoShape(g => g
.Shape(g => g
.Name(n => n.Envelope)
)
.GeoShape(g => g
.Name(n => n.Circle)
.Strategy(GeoStrategy.Recursive)
)
)
)
);
Expand All @@ -198,8 +190,7 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues
{
id = shape.Id,
geometryCollection = GeoWKTWriter.Write(shape.GeometryCollection),
envelope = GeoWKTWriter.Write(shape.Envelope),
circle = shape.Circle
envelope = GeoWKTWriter.Write(shape.Envelope)
});
}

Expand Down Expand Up @@ -245,7 +236,6 @@ protected override void ExpectResponse(ISearchResponse<Domain.Shape> response)
jValue.Value.Should().BeOfType<string>();
jValue = (JValue)jObject["envelope"];
jValue.Value.Should().BeOfType<string>();
jObject["circle"].Should().BeOfType<JObject>();
}
}
}
Expand Down