-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
LabelsAcceptanceTest.scala
150 lines (123 loc) · 5.27 KB
/
LabelsAcceptanceTest.scala
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
/*
* Copyright (c) 2002-2016 "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.internal.cypher.acceptance
import org.neo4j.cypher.internal.compiler.v3_1.helpers.CollectionSupport
import org.neo4j.cypher.{CypherException, ExecutionEngineFunSuite, QueryStatisticsTestSupport}
import org.neo4j.graphdb.Node
import org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine
import org.neo4j.kernel.impl.store.NeoStores
import org.scalatest.Assertions
import org.scalautils.LegacyTripleEquals
class LabelsAcceptanceTest extends ExecutionEngineFunSuite
with QueryStatisticsTestSupport with Assertions with CollectionSupport with LegacyTripleEquals {
test("Adding_single_literal_label") {
assertThat("create (n {}) set n:FOO", List("FOO"))
assertThat("create (n {}) set n :FOO", List("FOO"))
}
test("Adding_multiple_literal_labels") {
assertThat("create (n {}) set n:FOO:BAR", List("FOO", "BAR"))
assertThat("create (n {}) set n :FOO :BAR", List("FOO", "BAR"))
assertThat("create (n {}) set n :FOO:BAR", List("FOO", "BAR"))
}
test("Creating_nodes_with_literal_labels") {
assertDoesNotWork("CREATE node :FOO:BAR {name: 'Stefan'}")
assertDoesNotWork("CREATE node :FOO:BAR")
assertDoesNotWork("CREATE node")
assertThat("CREATE (node)", List())
assertThat("CREATE (node :FOO:BAR)", List("FOO", "BAR"))
assertThat("CREATE (node:FOO:BAR {name: 'Mattias'})", List("FOO", "BAR"))
assertThat("CREATE (n:Person)-[:OWNS]->(x:Dog) RETURN n AS node", List("Person"))
}
test("Recreating_and_labelling_the_same_node_twice_differently_is_forbidden") {
assertDoesNotWork("CREATE (n: FOO)-[:test]->(b), (n: BAR)-[:test2]->(c)")
assertDoesNotWork("CREATE (c)<-[:test2]-(n: FOO), (n: BAR)<-[:test]-(b)")
assertDoesNotWork("CREATE (n :Foo) CREATE (n :Bar)-[:OWNS]->(x:Dog)")
assertDoesNotWork("CREATE (n {}) CREATE (n :Bar)-[:OWNS]->(x:Dog)")
assertDoesNotWork("CREATE (n :Foo) CREATE (n {})-[:OWNS]->(x:Dog)")
assertDoesNotWork("CREATE (n :Foo) CREATE (n {})-[:OWNS]->(x:Dog)")
}
test("Add_labels_to_nodes_in_a_foreach") {
assertThat("CREATE (a),(b),(c) WITH [a,b,c] as nodes FOREACH(n in nodes | SET n :FOO:BAR)", List("FOO", "BAR"))
}
test("Using_labels_in_RETURN_clauses") {
createNode()
assertThat("match (n) where id(n) = 0 RETURN labels(n)", List())
createNode()
assertThat("match (n) where id(n) = 0 SET n :FOO RETURN labels(n)", List("FOO"))
}
test("Removing_labels") {
usingLabels("FOO", "BAR").
assertThat("START n=node({node}) REMOVE n:FOO RETURN n").
returnsLabels("BAR")
usingLabels("FOO").
assertThat("START n=node({node}) REMOVE n:BAR RETURN n").
returnsLabels("FOO")
}
test("should not create labels id when trying to delete non-existing labels") {
createNode()
val resultRule = execute("Cypher planner=rule MATCH (n) REMOVE n:BAR RETURN id(n) as id").toList
resultRule should equal(List(Map("id" -> 0)))
val resultCost = execute("MATCH (n) REMOVE n:BAR RETURN id(n) as id").toList
resultCost should equal(List(Map("id" -> 0)))
graph.inTx {
graph.getDependencyResolver.resolveDependency(classOf[RecordStorageEngine]).testAccessNeoStores().getLabelTokenStore.getHighId should equal(0)
}
}
private class AssertThat(labels: Seq[String], query:String) {
def returnsLabels(expected:String*):AssertThat = {
val node = createLabeledNode(labels:_*)
val result = executeScalar[Node](query, "node"->node)
assertInTx(result.labels === expected.toList)
this
}
}
private class UsingLabels(labels:Seq[String]) {
def assertThat(q:String):AssertThat = new AssertThat(labels, q)
}
private def usingLabels(labels:String*):UsingLabels = new UsingLabels(labels)
private def assertThat(q: String, expectedLabels: List[String]) {
val result = execute(q).toList
graph.inTx {
if (result.isEmpty) {
val n = graph.getNodeById(0)
assert(n.labels === expectedLabels)
} else {
result.foreach {
map => map.get("node") match {
case None =>
assert(makeTraversable(map.head._2).toList === expectedLabels)
case Some(n: Node) =>
assert(n.labels === expectedLabels)
case _ =>
throw new AssertionError("assertThat used with result that is not a node")
}
}
}
}
insertNewCleanDatabase()
}
private def insertNewCleanDatabase() {
stopTest()
initTest()
}
def assertDoesNotWork(s: String) {
intercept[CypherException](assertThat(s, List.empty))
}
}