-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
SchemaWithPECAcceptanceTest.java
163 lines (141 loc) · 6.38 KB
/
SchemaWithPECAcceptanceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j Enterprise Edition. The included source
* code can be redistributed and/or modified under the terms of the
* GNU AFFERO GENERAL PUBLIC LICENSE Version 3
* (http://www.fsf.org/licensing/licenses/agpl-3.0.html) with the
* Commons Clause, as found in the associated LICENSE.txt file.
*
* 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 Affero General Public License for more details.
*
* Neo4j object code can be licensed independently from the source
* under separate terms from the AGPL. Inquiries can be directed to:
* licensing@neo4j.com
*
* More information is also available at:
* https://neo4j.com/licensing/
*/
package org.neo4j.graphdb;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.neo4j.SchemaHelper;
import org.neo4j.graphdb.schema.ConstraintDefinition;
import org.neo4j.graphdb.schema.IndexDefinition;
import org.neo4j.kernel.impl.coreapi.schema.IndexDefinitionImpl;
import org.neo4j.kernel.impl.coreapi.schema.InternalSchemaActions;
import org.neo4j.kernel.impl.coreapi.schema.NodeKeyConstraintDefinition;
import org.neo4j.kernel.impl.coreapi.schema.NodePropertyExistenceConstraintDefinition;
import org.neo4j.kernel.impl.coreapi.schema.RelationshipPropertyExistenceConstraintDefinition;
import org.neo4j.kernel.impl.coreapi.schema.UniquenessConstraintDefinition;
import org.neo4j.test.rule.EnterpriseDatabaseRule;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import static org.neo4j.test.mockito.matcher.Neo4jMatchers.containsOnly;
import static org.neo4j.test.mockito.matcher.Neo4jMatchers.getConstraints;
public class SchemaWithPECAcceptanceTest
{
@Rule
public EnterpriseDatabaseRule dbRule = new EnterpriseDatabaseRule();
private GraphDatabaseService db;
private Label label = Labels.MY_LABEL;
private Label label2 = Labels.MY_OTHER_LABEL;
private String propertyKey = "my_property_key";
private String propertyKey2 = "my_other_property";
private enum Labels implements Label
{
MY_LABEL,
MY_OTHER_LABEL
}
private enum Types implements RelationshipType
{
MY_TYPE,
MY_OTHER_TYPE
}
@Before
public void init()
{
db = dbRule.getGraphDatabaseAPI();
}
@Test
public void shouldCreateNodePropertyExistenceConstraint()
{
// When
ConstraintDefinition constraint = createNodePropertyExistenceConstraint( label, propertyKey );
// Then
assertThat( getConstraints( db ), containsOnly( constraint ) );
}
@Test
public void shouldCreateRelationshipPropertyExistenceConstraint()
{
// When
ConstraintDefinition constraint = createRelationshipPropertyExistenceConstraint( Types.MY_TYPE, propertyKey );
// Then
assertThat( getConstraints( db ), containsOnly( constraint ) );
}
@Test
public void shouldListAddedConstraintsByLabel()
{
// GIVEN
ConstraintDefinition constraint1 = createUniquenessConstraint( label, propertyKey );
ConstraintDefinition constraint2 = createNodePropertyExistenceConstraint( label, propertyKey );
ConstraintDefinition constraint3 = createNodeKeyConstraint( label, propertyKey2 );
createNodeKeyConstraint( label2, propertyKey2 );
createNodePropertyExistenceConstraint( Labels.MY_OTHER_LABEL, propertyKey );
// WHEN THEN
assertThat( getConstraints( db, label ), containsOnly( constraint1, constraint2, constraint3 ) );
}
@Test
public void shouldListAddedConstraintsByRelationshipType()
{
// GIVEN
ConstraintDefinition constraint1 = createRelationshipPropertyExistenceConstraint( Types.MY_TYPE, propertyKey );
createRelationshipPropertyExistenceConstraint( Types.MY_OTHER_TYPE, propertyKey );
// WHEN THEN
assertThat( getConstraints( db, Types.MY_TYPE ), containsOnly( constraint1 ) );
}
@Test
public void shouldListAddedConstraints()
{
// GIVEN
ConstraintDefinition constraint1 = createUniquenessConstraint( label, propertyKey );
ConstraintDefinition constraint2 = createNodePropertyExistenceConstraint( label, propertyKey );
ConstraintDefinition constraint3 = createRelationshipPropertyExistenceConstraint( Types.MY_TYPE, propertyKey );
ConstraintDefinition constraint4 = createNodeKeyConstraint( label, propertyKey2 );
// WHEN THEN
assertThat( getConstraints( db ), containsOnly( constraint1, constraint2, constraint3, constraint4 ) );
}
private ConstraintDefinition createUniquenessConstraint( Label label, String propertyKey )
{
SchemaHelper.createUniquenessConstraint( db, label, propertyKey );
SchemaHelper.awaitIndexes( db );
InternalSchemaActions actions = mock( InternalSchemaActions.class );
IndexDefinition index = new IndexDefinitionImpl( actions, new Label[]{label}, new String[]{propertyKey}, true );
return new UniquenessConstraintDefinition( actions, index );
}
private ConstraintDefinition createNodeKeyConstraint( Label label, String propertyKey )
{
SchemaHelper.createNodeKeyConstraint( db, label, propertyKey );
SchemaHelper.awaitIndexes( db );
InternalSchemaActions actions = mock( InternalSchemaActions.class );
IndexDefinition index = new IndexDefinitionImpl( actions, new Label[]{label}, new String[]{propertyKey}, true );
return new NodeKeyConstraintDefinition( actions, index );
}
private ConstraintDefinition createNodePropertyExistenceConstraint( Label label, String propertyKey )
{
SchemaHelper.createNodePropertyExistenceConstraint( db, label, propertyKey );
return new NodePropertyExistenceConstraintDefinition( mock( InternalSchemaActions.class ), label,
new String[]{propertyKey} );
}
private ConstraintDefinition createRelationshipPropertyExistenceConstraint( Types type, String propertyKey )
{
SchemaHelper.createRelPropertyExistenceConstraint( db, type, propertyKey );
return new RelationshipPropertyExistenceConstraintDefinition( mock( InternalSchemaActions.class ), type,
propertyKey );
}
}