Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for IndexRule and ConstraintRule
- Loading branch information
Showing
5 changed files
with
248 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
community/kernel/src/test/java/org/neo4j/kernel/impl/store/record/ConstraintRuleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright (c) 2002-2017 "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.kernel.impl.store.record; | ||
|
||
import org.junit.Test; | ||
|
||
import org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.Assert.assertThat; | ||
import static org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptorFactory.existsForLabel; | ||
import static org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptorFactory.existsForRelType; | ||
import static org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptorFactory.uniqueForLabel; | ||
import static org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptorFactory.uniqueForRelType; | ||
import static org.neo4j.test.assertion.Assert.assertException; | ||
|
||
public class ConstraintRuleTest extends SchemaRuleTestBase | ||
{ | ||
@Test | ||
public void shouldCreateUniquenessConstraint() throws Exception | ||
{ | ||
// GIVEN | ||
ConstraintDescriptor descriptor = uniqueForLabel( LABEL_ID, PROPERTY_ID_1 ); | ||
ConstraintRule constraintRule = ConstraintRule.constraintRule( RULE_ID, descriptor ); | ||
|
||
// THEN | ||
assertThat( constraintRule.getId(), equalTo( RULE_ID ) ); | ||
assertThat( constraintRule.getSchemaDescriptor(), equalTo( descriptor.schema() ) ); | ||
assertThat( constraintRule.getConstraintDescriptor(), equalTo( descriptor ) ); | ||
assertException( constraintRule::getOwnedIndex, IllegalStateException.class, "" ); | ||
} | ||
|
||
@Test | ||
public void shouldCreateUniquenessConstraintWithOwnedIndex() throws Exception | ||
{ | ||
// GIVEN | ||
ConstraintDescriptor descriptor = uniqueForLabel( LABEL_ID, PROPERTY_ID_1 ); | ||
ConstraintRule constraintRule = ConstraintRule.constraintRule( RULE_ID, descriptor, RULE_ID_2 ); | ||
|
||
// THEN | ||
assertThat( constraintRule.getConstraintDescriptor(), equalTo( descriptor ) ); | ||
assertThat( constraintRule.getOwnedIndex(), equalTo( RULE_ID_2 ) ); | ||
} | ||
|
||
@Test | ||
public void shouldCreateExistenceConstraint() throws Exception | ||
{ | ||
// GIVEN | ||
ConstraintDescriptor descriptor = existsForLabel( LABEL_ID, PROPERTY_ID_1 ); | ||
ConstraintRule constraintRule = ConstraintRule.constraintRule( RULE_ID, descriptor ); | ||
|
||
// THEN | ||
assertThat( constraintRule.getId(), equalTo( RULE_ID ) ); | ||
assertThat( constraintRule.getSchemaDescriptor(), equalTo( descriptor.schema() ) ); | ||
assertThat( constraintRule.getConstraintDescriptor(), equalTo( descriptor ) ); | ||
assertException( constraintRule::getOwnedIndex, IllegalStateException.class, "" ); | ||
} | ||
|
||
@Test | ||
public void indexRulesAreEqualBasedOnConstraintDescriptor() throws Exception | ||
{ | ||
assertEqualityByDescriptor( existsForLabel( LABEL_ID, PROPERTY_ID_1 ) ); | ||
assertEqualityByDescriptor( uniqueForLabel( LABEL_ID, PROPERTY_ID_1 ) ); | ||
assertEqualityByDescriptor( existsForRelType( REL_TYPE_ID, PROPERTY_ID_1 ) ); | ||
assertEqualityByDescriptor( uniqueForRelType( REL_TYPE_ID, PROPERTY_ID_1 ) ); | ||
assertEqualityByDescriptor( existsForLabel( LABEL_ID, PROPERTY_ID_1, PROPERTY_ID_2 ) ); | ||
assertEqualityByDescriptor( uniqueForLabel( LABEL_ID, PROPERTY_ID_1, PROPERTY_ID_2 ) ); | ||
} | ||
|
||
private void assertEqualityByDescriptor( ConstraintDescriptor descriptor ) | ||
{ | ||
ConstraintRule rule1 = ConstraintRule.constraintRule( RULE_ID, descriptor, RULE_ID_2 ); | ||
ConstraintRule rule2 = ConstraintRule.constraintRule( RULE_ID_2, descriptor ); | ||
|
||
assertEquality( rule1, rule2 ); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
community/kernel/src/test/java/org/neo4j/kernel/impl/store/record/IndexRuleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright (c) 2002-2017 "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.kernel.impl.store.record; | ||
|
||
import org.junit.Test; | ||
|
||
import org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.neo4j.kernel.api.schema_new.index.NewIndexDescriptorFactory.forLabel; | ||
import static org.neo4j.kernel.api.schema_new.index.NewIndexDescriptorFactory.uniqueForLabel; | ||
import static org.neo4j.test.assertion.Assert.assertException; | ||
|
||
public class IndexRuleTest extends SchemaRuleTestBase | ||
{ | ||
@Test | ||
public void shouldCreateGeneralIndex() throws Exception | ||
{ | ||
// GIVEN | ||
NewIndexDescriptor descriptor = forLabel( LABEL_ID, PROPERTY_ID_1 ); | ||
IndexRule indexRule = IndexRule.indexRule( RULE_ID, descriptor, PROVIDER_DESCRIPTOR ); | ||
|
||
// THEN | ||
assertThat( indexRule.getId(), equalTo( RULE_ID ) ); | ||
assertFalse( indexRule.canSupportUniqueConstraint() ); | ||
assertThat( indexRule.getSchemaDescriptor(), equalTo( descriptor.schema() ) ); | ||
assertThat( indexRule.getIndexDescriptor(), equalTo( descriptor ) ); | ||
assertThat( indexRule.getProviderDescriptor(), equalTo( PROVIDER_DESCRIPTOR ) ); | ||
assertException( indexRule::getOwningConstraint, IllegalStateException.class, "" ); | ||
assertException( () -> indexRule.withOwningConstraint( RULE_ID_2 ), IllegalStateException.class, "" ); | ||
} | ||
|
||
@Test | ||
public void shouldCreateUniqueIndex() throws Exception | ||
{ | ||
// GIVEN | ||
NewIndexDescriptor descriptor = uniqueForLabel( LABEL_ID, PROPERTY_ID_1 ); | ||
IndexRule indexRule = IndexRule.indexRule( RULE_ID, descriptor, PROVIDER_DESCRIPTOR ); | ||
|
||
// THEN | ||
assertThat( indexRule.getId(), equalTo( RULE_ID ) ); | ||
assertTrue( indexRule.canSupportUniqueConstraint() ); | ||
assertThat( indexRule.getSchemaDescriptor(), equalTo( descriptor.schema() ) ); | ||
assertThat( indexRule.getIndexDescriptor(), equalTo( descriptor ) ); | ||
assertThat( indexRule.getProviderDescriptor(), equalTo( PROVIDER_DESCRIPTOR ) ); | ||
assertThat( indexRule.getOwningConstraint(), equalTo( null ) ); | ||
|
||
IndexRule withConstraint = indexRule.withOwningConstraint( RULE_ID_2 ); | ||
assertThat( withConstraint.getOwningConstraint(), equalTo( RULE_ID_2 ) ); | ||
assertThat( indexRule.getOwningConstraint(), equalTo( null ) ); // this is unchanged | ||
} | ||
|
||
@Test | ||
public void indexRulesAreEqualBasedOnIndexDescriptor() throws Exception | ||
{ | ||
assertEqualityByDescriptor( forLabel( LABEL_ID, PROPERTY_ID_1 ) ); | ||
assertEqualityByDescriptor( uniqueForLabel( LABEL_ID, PROPERTY_ID_1 ) ); | ||
assertEqualityByDescriptor( forLabel( LABEL_ID, PROPERTY_ID_1, PROPERTY_ID_2 ) ); | ||
assertEqualityByDescriptor( uniqueForLabel( LABEL_ID, PROPERTY_ID_1, PROPERTY_ID_2 ) ); | ||
} | ||
|
||
private void assertEqualityByDescriptor( NewIndexDescriptor descriptor ) | ||
{ | ||
IndexRule rule1 = IndexRule.indexRule( RULE_ID, descriptor, PROVIDER_DESCRIPTOR ); | ||
IndexRule rule2 = IndexRule.indexRule( RULE_ID_2, descriptor, PROVIDER_DESCRIPTOR_2 ); | ||
|
||
assertEquality( rule1, rule2 ); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
community/kernel/src/test/java/org/neo4j/kernel/impl/store/record/SchemaRuleTestBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (c) 2002-2017 "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.kernel.impl.store.record; | ||
|
||
import org.neo4j.kernel.api.index.SchemaIndexProvider; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
abstract class SchemaRuleTestBase | ||
{ | ||
protected static final long RULE_ID = 1; | ||
protected static final long RULE_ID_2 = 1; | ||
protected static final int LABEL_ID = 10; | ||
protected static final int REL_TYPE_ID = 20; | ||
protected static final int PROPERTY_ID_1 = 30; | ||
protected static final int PROPERTY_ID_2 = 31; | ||
|
||
protected static final SchemaIndexProvider.Descriptor PROVIDER_DESCRIPTOR = | ||
new SchemaIndexProvider.Descriptor( "index-provider", "1.0" ); | ||
protected static final SchemaIndexProvider.Descriptor PROVIDER_DESCRIPTOR_2 = | ||
new SchemaIndexProvider.Descriptor( "index-provider-2", "2.0" ); | ||
|
||
protected void assertEquality( Object o1, Object o2 ) | ||
{ | ||
assertThat( o1, equalTo( o2 ) ); | ||
assertThat( o2, equalTo( o1 ) ); | ||
assertThat( o1.hashCode(), equalTo( o2.hashCode() ) ); | ||
} | ||
} |