Skip to content

Commit

Permalink
Increase test coverage for spatial index
Browse files Browse the repository at this point in the history
One failing test ignored, wil be debugged.
  • Loading branch information
craigtaverner authored and OliviaYtterbrink committed Feb 14, 2018
1 parent 42c5a3a commit e818de7
Show file tree
Hide file tree
Showing 9 changed files with 324 additions and 91 deletions.
Expand Up @@ -27,7 +27,10 @@
import org.neo4j.internal.kernel.api.IndexQuery.StringContainsPredicate;
import org.neo4j.internal.kernel.api.IndexQuery.StringPrefixPredicate;
import org.neo4j.internal.kernel.api.IndexQuery.StringRangePredicate;
import org.neo4j.internal.kernel.api.IndexQuery.GeometryRangePredicate;
import org.neo4j.internal.kernel.api.IndexQuery.StringSuffixPredicate;
import org.neo4j.values.storable.CoordinateReferenceSystem;
import org.neo4j.values.storable.PointValue;
import org.neo4j.values.storable.Values;

import static org.junit.Assert.assertFalse;
Expand All @@ -49,6 +52,7 @@ public void testExists()
assertTrue( test( p, 1.0 ) );
assertTrue( test( p, true ) );
assertTrue( test( p, new long[]{1L} ) );
assertTrue( test( p, Values.pointValue( CoordinateReferenceSystem.WGS84, 12.3, 45.6 ) ) );

assertFalse( test( p, null ) );
}
Expand All @@ -63,6 +67,7 @@ public void testExact()
assertExactPredicate( 1.0 );
assertExactPredicate( true );
assertExactPredicate( new long[]{1L} );
assertExactPredicate( Values.pointValue( CoordinateReferenceSystem.WGS84, 12.3, 45.6 ) );
}

private void assertExactPredicate( Object value )
Expand Down Expand Up @@ -243,6 +248,91 @@ public void testStringRange_LowerUnbounded()
assertFalse( test( p, "bee" ) );
}

// GEOMETRY RANGE

private PointValue gps1 = Values.pointValue( CoordinateReferenceSystem.WGS84, -12.6, -56.7 );
private PointValue gps2 = Values.pointValue( CoordinateReferenceSystem.WGS84, -11.6, -55.7 );
private PointValue gps3 = Values.pointValue( CoordinateReferenceSystem.WGS84, -11.0, -55 );
private PointValue gps4 = Values.pointValue( CoordinateReferenceSystem.WGS84, 0, 0 );
private PointValue gps5 = Values.pointValue( CoordinateReferenceSystem.WGS84, 12.6, 56.7 );
private PointValue gps6 = Values.pointValue( CoordinateReferenceSystem.WGS84, 14.6, 58.7 );
private PointValue gps7 = Values.pointValue( CoordinateReferenceSystem.WGS84, 15.6, 59.7 );

//TODO: Also insert points which can't be compared e.g. Cartesian and (-100, 100)

@Test
public void testGeometryRange_FalseForIrrelevant()
{
GeometryRangePredicate p = IndexQuery.range( propId, gps2, true, gps5, true );

assertFalseForOtherThings( p );
}

@Test
public void testGeometryRange_InclusiveLowerInclusiveUpper()
{
GeometryRangePredicate p = IndexQuery.range( propId, gps2, true, gps5, true );

assertFalse( test( p, gps1 ) );
assertTrue( test( p, gps2 ) );
assertTrue( test( p, gps5 ) );
assertFalse( test( p, gps6 ) );
assertFalse( test( p, gps7 ) );
}

@Test
public void testGeometryRange_ExclusiveLowerInclusiveUpper()
{
GeometryRangePredicate p = IndexQuery.range( propId, gps2, false, gps5, true );

assertFalse( test( p, gps2 ) );
assertTrue( test( p, gps3 ) );
assertTrue( test( p, gps5 ) );
assertFalse( test( p, gps6 ) );
}

@Test
public void testGeometryRange_InclusiveLowerExclusiveUpper()
{
GeometryRangePredicate p = IndexQuery.range( propId, gps2, true, gps5, false );

assertFalse( test( p, gps1 ) );
assertTrue( test( p, gps2 ) );
assertTrue( test( p, gps3 ) );
assertFalse( test( p, gps5 ) );
}

@Test
public void testGeometryRange_ExclusiveLowerExclusiveUpper()
{
GeometryRangePredicate p = IndexQuery.range( propId, gps2, false, gps5, false );

assertFalse( test( p, gps2 ) );
assertTrue( test( p, gps3 ) );
assertTrue( test( p, gps4 ) );
assertFalse( test( p, gps5 ) );
}

@Test
public void testGeometryRange_UpperUnbounded()
{
GeometryRangePredicate p = IndexQuery.range( propId, gps2, false, null, false );

assertFalse( test( p, gps2 ) );
assertTrue( test( p, gps3 ) );
assertTrue( test( p, gps7 ) );
}

@Test
public void testGeometryRange_LowerUnbounded()
{
GeometryRangePredicate p = IndexQuery.range( propId, null, false, gps5, false );

assertTrue( test( p, gps1 ) );
assertTrue( test( p, gps3 ) );
assertFalse( test( p, gps5 ) );
}

// STRING PREFIX

@Test
Expand Down
Expand Up @@ -31,6 +31,7 @@
import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.collection.primitive.PrimitiveLongSet;
import org.neo4j.graphdb.schema.IndexDefinition;
import org.neo4j.graphdb.spatial.Point;
import org.neo4j.helpers.collection.Iterators;
import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor;
import org.neo4j.kernel.api.ReadOperations;
Expand All @@ -44,12 +45,17 @@
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.test.mockito.matcher.Neo4jMatchers;
import org.neo4j.test.rule.ImpermanentDatabaseRule;
import org.neo4j.values.storable.CoordinateReferenceSystem;
import org.neo4j.values.storable.PointValue;
import org.neo4j.values.storable.Values;

import static java.lang.String.format;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.neo4j.graphdb.SpatialMocks.mockCartesian;
import static org.neo4j.graphdb.SpatialMocks.mockWGS84;
import static org.neo4j.helpers.collection.Iterators.asSet;
import static org.neo4j.helpers.collection.Iterators.count;
import static org.neo4j.helpers.collection.MapUtil.map;
Expand Down Expand Up @@ -353,6 +359,10 @@ public void shouldBeAbleToQuerySupportedPropertyTypes()
assertCanCreateAndFind( db, LABEL1, property, 12L );
assertCanCreateAndFind( db, LABEL1, property, (float)12. );
assertCanCreateAndFind( db, LABEL1, property, 12. );
assertCanCreateAndFind( db, LABEL1, property, new SpatialMocks.MockPoint( 12.3, 45.6, mockWGS84() ) );
assertCanCreateAndFind( db, LABEL1, property, new SpatialMocks.MockPoint( 123, 456, mockCartesian() ) );
assertCanCreateAndFind( db, LABEL1, property, Values.pointValue( CoordinateReferenceSystem.WGS84, 12.3, 45.6 ) );
assertCanCreateAndFind( db, LABEL1, property, Values.pointValue( CoordinateReferenceSystem.Cartesian, 123, 456 ) );

assertCanCreateAndFind( db, LABEL1, property, new String[]{"A String"} );
assertCanCreateAndFind( db, LABEL1, property, new boolean[]{true} );
Expand All @@ -371,6 +381,10 @@ public void shouldBeAbleToQuerySupportedPropertyTypes()
assertCanCreateAndFind( db, LABEL1, property, new Float[]{(float)19.} );
assertCanCreateAndFind( db, LABEL1, property, new double[]{20.} );
assertCanCreateAndFind( db, LABEL1, property, new Double[]{21.} );
assertCanCreateAndFind( db, LABEL1, property, new Point[]{new SpatialMocks.MockPoint( 12.3, 45.6, mockWGS84() )} );
assertCanCreateAndFind( db, LABEL1, property, new Point[]{new SpatialMocks.MockPoint( 123, 456, mockCartesian() )} );
assertCanCreateAndFind( db, LABEL1, property, new PointValue[]{Values.pointValue( CoordinateReferenceSystem.WGS84, 12.3, 45.6 )} );
assertCanCreateAndFind( db, LABEL1, property, new PointValue[]{Values.pointValue( CoordinateReferenceSystem.Cartesian, 123, 456 )} );
}

@Test
Expand Down
107 changes: 107 additions & 0 deletions community/kernel/src/test/java/org/neo4j/graphdb/SpatialMocks.java
@@ -0,0 +1,107 @@
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.graphdb;

import java.util.ArrayList;
import java.util.List;

import org.neo4j.graphdb.spatial.CRS;
import org.neo4j.graphdb.spatial.Coordinate;
import org.neo4j.graphdb.spatial.Geometry;
import org.neo4j.graphdb.spatial.Point;

public class SpatialMocks
{

public static CRS mockWGS84()
{
return mockCRS( 4326, "WGS-84", "http://spatialreference.org/ref/epsg/4326/" );
}

public static CRS mockCartesian()
{
return mockCRS( 7203, "cartesian", "http://spatialreference.org/ref/sr-org/7203/" );
}

public static CRS mockCRS( final int code, final String type, final String href )
{
return new CRS()
{
public int getCode()
{
return code;
}

public String getType()
{
return type;
}

public String getHref()
{
return href;
}
};
}

public static class MockPoint extends MockGeometry implements Point
{
private final Coordinate coordinate;

public MockPoint( final double x, final double y, final CRS crs )
{
super( "Point", new ArrayList<>(), crs );
this.coordinate = new Coordinate( x, y );
this.coordinates.add( this.coordinate );
}
}

public static class MockGeometry implements Geometry
{
protected final String geometryType;
protected final CRS crs;
protected final List<Coordinate> coordinates;

public MockGeometry( String geometryType, final List<Coordinate> coordinates, final CRS crs )
{
this.geometryType = geometryType;
this.coordinates = coordinates;
this.crs = crs;
}

@Override
public String getGeometryType()
{
return geometryType;
}

@Override
public List<Coordinate> getCoordinates()
{
return coordinates;
}

@Override
public CRS getCRS()
{
return crs;
}
}
}
Expand Up @@ -25,6 +25,8 @@
import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor;
import org.neo4j.kernel.api.schema.index.IndexDescriptor;
import org.neo4j.kernel.api.schema.index.IndexDescriptorFactory;
import org.neo4j.values.storable.CoordinateReferenceSystem;
import org.neo4j.values.storable.PointValue;
import org.neo4j.values.storable.Values;

import static java.util.Arrays.asList;
Expand Down Expand Up @@ -75,6 +77,23 @@ public void testIndexSeekAndScanByNumber() throws Exception
assertThat( query( exists( 1 ) ), equalTo( asList( 1L, 2L, 3L ) ) );
}

@Test
public void testIndexSeekAndScanByPoint() throws Exception
{
PointValue gps = Values.pointValue( CoordinateReferenceSystem.WGS84, 12.6, 56.7 );
PointValue car = Values.pointValue( CoordinateReferenceSystem.Cartesian, 12.6, 56.7 );

updateAndCommit( asList(
add( 1L, descriptor.schema(), gps, gps ),
add( 2L, descriptor.schema(), car, car ),
add( 3L, descriptor.schema(), gps, car ) ) );

assertThat( query( exact( 0, gps ), exact( 1, gps ) ), equalTo( singletonList( 1L ) ) );
assertThat( query( exact( 0, car ), exact( 1, car ) ), equalTo( singletonList( 2L ) ) );
assertThat( query( exact( 0, gps ), exact( 1, car ) ), equalTo( singletonList( 3L ) ) );
assertThat( query( exists( 1 ) ), equalTo( asList( 1L, 2L, 3L ) ) );
}

// This behaviour is expected by General indexes

@Ignore( "Not a test. This is a compatibility suite" )
Expand Down Expand Up @@ -104,6 +123,17 @@ public void testDuplicatesInIndexSeekByNumber() throws Exception

assertThat( query( exact( 0, 333 ), exact( 1, 333 ) ), equalTo( asList( 1L, 2L ) ) );
}

@Test
public void testDuplicatesInIndexSeekByPoint() throws Exception
{
PointValue gps = Values.pointValue( CoordinateReferenceSystem.WGS84, 12.6, 56.7 );
updateAndCommit( asList(
add( 1L, descriptor.schema(), gps, gps ),
add( 2L, descriptor.schema(), gps, gps ) ) );

assertThat( query( exact( 0, gps ), exact( 1, gps ) ), equalTo( asList( 1L, 2L ) ) );
}
}

// This behaviour is expected by Unique indexes
Expand Down

0 comments on commit e818de7

Please sign in to comment.