From 84cc9dc02e4de3ea509073567b35ca21e1413d01 Mon Sep 17 00:00:00 2001 From: Stuart Cam Date: Wed, 23 Oct 2019 18:29:51 +1100 Subject: [PATCH] Remove circle from Shape tests, change integration test setup to use Shape() instead of GeoShape(), change default index for Shape types. --- .../ConnectionSettingsExtensions.cs | 3 + src/Tests/Tests.Domain/GeoShape.cs | 106 ++++++++++++++++++ src/Tests/Tests.Domain/Shape.cs | 4 +- .../GeoShape/GeoShapeSerializationTests.cs | 30 ++--- .../Shape/ShapeSerializationTests.cs | 24 ++-- 5 files changed, 132 insertions(+), 35 deletions(-) create mode 100644 src/Tests/Tests.Domain/GeoShape.cs diff --git a/src/Tests/Tests.Domain/Extensions/ConnectionSettingsExtensions.cs b/src/Tests/Tests.Domain/Extensions/ConnectionSettingsExtensions.cs index e25d7b3dca1..a4eb9d82174 100644 --- a/src/Tests/Tests.Domain/Extensions/ConnectionSettingsExtensions.cs +++ b/src/Tests/Tests.Domain/Extensions/ConnectionSettingsExtensions.cs @@ -27,6 +27,9 @@ public static ConnectionSettings ApplyDomainSettings(this ConnectionSettings set .DefaultMappingFor(map => map .IndexName("server-metrics") ) + .DefaultMappingFor(map => map + .IndexName("geoshapes") + ) .DefaultMappingFor(map => map .IndexName("shapes") ); diff --git a/src/Tests/Tests.Domain/GeoShape.cs b/src/Tests/Tests.Domain/GeoShape.cs new file mode 100644 index 00000000000..9a44137b7a2 --- /dev/null +++ b/src/Tests/Tests.Domain/GeoShape.cs @@ -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 Generator { get; } = + new Faker() + .UseSeed(TestConfiguration.Instance.Seed) + .RuleFor(p => p.Id, p => Interlocked.Increment(ref _idState)) + .RuleFor(p => p.GeometryCollection, p => + new GeometryCollection(new List + { + 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 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>(); + 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> + { + GeneratePolygonCoordinates(p, GenerateGeoCoordinate(p)) + }); + + private static IMultiPolygonGeoShape GenerateMultiPolygon(Faker p) => new MultiPolygonGeoShape( + new List>> + { + new[] { GeneratePolygonCoordinates(p, GenerateGeoCoordinate(p)) } + }); + + private static GeoCoordinate GenerateGeoCoordinate(Faker p) => + new GeoCoordinate(p.Address.Latitude(), p.Address.Longitude()); + + private static IEnumerable GenerateGeoCoordinates(Faker p, int count) + { + var points = new List(); + + 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 GeneratePolygonCoordinates(Faker p, GeoCoordinate centroid, double maxDistance = 0.0002) + { + const int maxPoints = 20; + var points = new List(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; + } + } +} diff --git a/src/Tests/Tests.Domain/Shape.cs b/src/Tests/Tests.Domain/Shape.cs index 15924f0b031..f6c5749db38 100644 --- a/src/Tests/Tests.Domain/Shape.cs +++ b/src/Tests/Tests.Domain/Shape.cs @@ -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 Generator { get; } = @@ -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; } diff --git a/src/Tests/Tests/QueryDsl/Geo/GeoShape/GeoShapeSerializationTests.cs b/src/Tests/Tests/QueryDsl/Geo/GeoShape/GeoShapeSerializationTests.cs index e035c588f7c..4e161767191 100644 --- a/src/Tests/Tests/QueryDsl/Geo/GeoShape/GeoShapeSerializationTests.cs +++ b/src/Tests/Tests/QueryDsl/Geo/GeoShape/GeoShapeSerializationTests.cs @@ -16,13 +16,13 @@ namespace Tests.QueryDsl.Geo.GeoShape { public abstract class GeoShapeSerializationTestsBase : ApiIntegrationTestBase, + ISearchResponse, ISearchRequest, - SearchDescriptor, - SearchRequest> + SearchDescriptor, + SearchRequest> { private readonly IEnumerable _coordinates = - Domain.Shape.Shapes.First().Envelope.Coordinates; + Domain.GeoShape.Shapes.First().Envelope.Coordinates; protected GeoShapeSerializationTestsBase(IntrusiveOperationCluster cluster, EndpointUsage usage) : base(cluster, usage) { } @@ -53,7 +53,7 @@ protected GeoShapeSerializationTestsBase(IntrusiveOperationCluster cluster, Endp protected override int ExpectStatusCode => 200; - protected override Func, ISearchRequest> Fluent => s => s + protected override Func, ISearchRequest> Fluent => s => s .Index(Index) .Query(q => q .GeoShape(c => c @@ -72,13 +72,13 @@ protected GeoShapeSerializationTestsBase(IntrusiveOperationCluster cluster, Endp protected abstract string Index { get; } - protected override SearchRequest Initializer => new SearchRequest(Index) + protected override SearchRequest Initializer => new SearchRequest(Index) { Query = new GeoShapeQuery { Name = "named_query", Boost = 1.1, - Field = Infer.Field(p => p.Envelope), + Field = Infer.Field(p => p.Envelope), Shape = new EnvelopeGeoShape(_coordinates), Relation = GeoShapeRelation.Intersects, IgnoreUnmapped = true, @@ -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(r), - (client, r) => client.SearchAsync(r) + (client, r) => client.Search(r), + (client, r) => client.SearchAsync(r) ); - protected override void ExpectResponse(ISearchResponse response) + protected override void ExpectResponse(ISearchResponse response) { response.IsValid.Should().BeTrue(); response.Documents.Count.Should().Be(10); @@ -118,7 +118,7 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues .NumberOfShards(1) .NumberOfReplicas(0) ) - .Map(mm => mm + .Map(mm => mm .AutoMap() .Properties(p => p .GeoShape(g => g @@ -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) ); @@ -167,7 +167,7 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues .NumberOfShards(1) .NumberOfReplicas(0) ) - .Map(mm => mm + .Map(mm => mm .AutoMap() .Properties(p => p .GeoShape(g => g @@ -189,7 +189,7 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues var bulk = new List(); - 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 @@ -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 response) + protected override void ExpectResponse(ISearchResponse response) { base.ExpectResponse(response); diff --git a/src/Tests/Tests/QueryDsl/Specialized/Shape/ShapeSerializationTests.cs b/src/Tests/Tests/QueryDsl/Specialized/Shape/ShapeSerializationTests.cs index a7bd45bff63..15c126605fc 100644 --- a/src/Tests/Tests/QueryDsl/Specialized/Shape/ShapeSerializationTests.cs +++ b/src/Tests/Tests/QueryDsl/Specialized/Shape/ShapeSerializationTests.cs @@ -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) { @@ -123,16 +123,12 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues .Map(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) - ) ) ) ); @@ -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) { @@ -172,16 +168,12 @@ protected override void IntegrationSetup(IElasticClient client, CallUniqueValues .Map(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) - ) ) ) ); @@ -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) }); } @@ -245,7 +236,6 @@ protected override void ExpectResponse(ISearchResponse response) jValue.Value.Should().BeOfType(); jValue = (JValue)jObject["envelope"]; jValue.Value.Should().BeOfType(); - jObject["circle"].Should().BeOfType(); } } }