Skip to content

Commit

Permalink
Added randomized testing for composite 4-property index
Browse files Browse the repository at this point in the history
  • Loading branch information
tinwelint committed Sep 11, 2018
1 parent 8b8e8d4 commit ebc2f23
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.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.kernel.api.index;

import org.junit.Ignore;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.neo4j.internal.kernel.api.schema.SchemaDescriptor;
import org.neo4j.kernel.api.schema.index.TestIndexDescriptorFactory;
import org.neo4j.values.storable.RandomValues;
import org.neo4j.values.storable.ValueTuple;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.neo4j.helpers.collection.Iterables.single;
import static org.neo4j.kernel.api.index.IndexQueryHelper.add;
import static org.neo4j.kernel.api.index.IndexQueryHelper.exact;

@Ignore( "Not a test. This is a compatibility suite that provides test cases for verifying" +
" IndexProvider implementations. Each index provider that is to be tested by this suite" +
" must create their own test class extending IndexProviderCompatibilityTestSuite." +
" The @Ignore annotation doesn't prevent these tests to run, it rather removes some annoying" +
" errors or warnings in some IDEs about test classes needing a public zero-arg constructor." )
public class CompositeRandomizedIndexAccessorCompatibility extends IndexAccessorCompatibility
{
public CompositeRandomizedIndexAccessorCompatibility( IndexProviderCompatibilityTestSuite testSuite )
{
// composite index of 4 properties
super( testSuite, TestIndexDescriptorFactory.forLabel( 1000, 100, 101, 102, 103 ) );
}

@Test
public void testExactMatchOnRandomCompositeValues() throws Exception
{
// given
List<RandomValues.Types> types = testSuite.supportedValueTypes();
List<IndexEntryUpdate<?>> updates = new ArrayList<>();
Set<ValueTuple> duplicateChecker = new HashSet<>();
for ( long id = 0; id < 30_000; id++ )
{
IndexEntryUpdate<SchemaDescriptor> update;
do
{
update = add( id, descriptor.schema(),
random.nextValue( random.among( types ) ),
random.nextValue( random.among( types ) ),
random.nextValue( random.among( types ) ),
random.nextValue( random.among( types ) ) );
}
while ( !duplicateChecker.add( ValueTuple.of( update.values() ) ) );
updates.add( update );
}
updateAndCommit( updates );

// when
for ( IndexEntryUpdate<?> update : updates )
{
// then
List<Long> hits = query(
exact( 100, update.values()[0] ),
exact( 101, update.values()[1] ),
exact( 102, update.values()[2] ),
exact( 103, update.values()[3] ) );
assertEquals( update + " " + hits.toString(), 1, hits.size() );
assertThat( single( hits ), equalTo( update.getEntityId() ) );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
CompositeIndexAccessorCompatibility.General.class,
CompositeIndexAccessorCompatibility.Unique.class,
UniqueConstraintCompatibility.class,
SimpleRandomizedIndexAccessorCompatibility.General.class
SimpleRandomizedIndexAccessorCompatibility.General.class,
CompositeRandomizedIndexAccessorCompatibility.class
} )
public abstract class IndexProviderCompatibilityTestSuite
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -95,11 +94,7 @@ private Map<Value,List<IndexEntryUpdate<?>>> createRandomUpdates( int numberOfUp

private List<RandomValues.Types> listOfRandomValidValueTypes()
{
List<RandomValues.Types> validValueTypes = new ArrayList<>( Arrays.asList( RandomValues.Types.values() ) );
if ( !testSuite.supportsSpatial() )
{
removeSpatialTypes( validValueTypes );
}
List<RandomValues.Types> validValueTypes = testSuite.supportedValueTypes();
Collections.shuffle( validValueTypes, random.random() );
int numberOfGroupsToUse = random.nextInt( 1, validValueTypes.size() - 1 );
while ( validValueTypes.size() > numberOfGroupsToUse )
Expand All @@ -109,14 +104,6 @@ private List<RandomValues.Types> listOfRandomValidValueTypes()
return validValueTypes;
}

private void removeSpatialTypes( List<RandomValues.Types> targetValueTypes )
{
targetValueTypes.remove( RandomValues.Types.CARTESIAN_POINT );
targetValueTypes.remove( RandomValues.Types.CARTESIAN_POINT_3D );
targetValueTypes.remove( RandomValues.Types.GEOGRAPHIC_POINT );
targetValueTypes.remove( RandomValues.Types.GEOGRAPHIC_POINT_3D );
}

private Value randomValueFromValidTypes( List<RandomValues.Types> validValueTypes )
{
int targetValueType = random.nextInt( validValueTypes.size() );
Expand Down

0 comments on commit ebc2f23

Please sign in to comment.