1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 # key used to loosely assume unicity and prevent conflicting db accesses
18 var key
= "NIT_TESTING_ID".environ
.to_i
20 var srv
= new Neo4jServer
23 print
"# Test local\n"
25 var client
= new Neo4jClient("http://localhost:7474")
28 # Clear the previous objects, if any
30 new CypherQuery.from_string
(
31 "MATCH (n) WHERE n.key = \{key\} OPTIONAL MATCH n-[r]-() DELETE r, n"
35 var andres
= new NeoNode
36 andres
.labels
.add_all
(["PERSON", "MALE"])
37 andres
["name"] = "Andres"
39 andres
["status"] = true
40 andres
["groups"] = new JsonArray.from
([1, 2, 3])
44 client
.save_node
(andres
)
45 assert andres
.is_linked
46 var andres_url
= andres
.url
.to_s
49 var res1
= client
.load_node
(andres_url
)
51 print res1
["name"].to_s
52 print res1
["age"].to_s
53 print res1
["status"].to_s
54 print res1
["groups"].to_json
55 print res1
.labels
.join
(" ")
56 assert res1
.out_edges
.is_empty
58 # Create a second node
59 var kate
= new NeoNode
60 kate
.labels
.add_all
(["PERSON", "FEMALE"])
63 kate
["status"] = false
64 client
.save_node
(kate
)
66 var kate_url
= kate
.url
.to_s
67 var res2
= client
.load_node
(kate_url
)
70 var loves
= new NeoEdge(andres
, "LOVES", kate
)
72 client
.save_edge
(loves
)
73 assert loves
.is_linked
74 var loves_url
= loves
.url
.to_s
77 assert loves
.from
== andres
78 assert loves
.from
== res1
79 assert loves
.to
== kate
80 assert loves
.to
== res2
83 var res3
= client
.load_edge
(loves_url
)
85 print res3
.rel_type
.to_s
86 print res3
["since"].to_s
89 print
"{andres["name"].to_s} LOVES {andres.out_nodes("LOVES").first["name"].to_s}"
90 print
"{kate["name"].to_s} IS LOVED BY {kate.in_nodes("LOVES").first["name"].to_s}"
92 print
"\n# Test lazy\n"
94 client
= new Neo4jClient("http://localhost:7474/")
98 var res4
= client
.load_node
(andres_url
)
100 print res4
["name"].to_s
101 print res4
["age"].to_s
102 print res4
["status"].to_s
103 print res4
["groups"].to_json
104 print res4
.labels
.join
(" ")
105 assert res4
.in_edges
.is_empty
106 assert not res4
.out_edges
.is_empty
109 var res5
= client
.load_node
(kate_url
)
110 assert res5
.is_linked
111 print res5
["name"].to_s
112 print res5
["age"].to_s
113 print res5
["status"].to_s
114 print res5
.labels
.join
(" ")
115 assert not res5
.in_edges
.is_empty
116 assert res5
.out_edges
.is_empty
119 var res6
= client
.load_edge
(loves_url
)
120 assert res6
.is_linked
121 print res6
.rel_type
.to_s
122 print res6
["since"].to_s
123 print
"{res4["name"].to_s} LOVES {res4.out_nodes("LOVES").first["name"].to_s}"
124 print
"{res5["name"].to_s} IS LOVED BY {res5.in_nodes("LOVES").first["name"].to_s}"
127 var query
= (new CypherQuery).
128 nmatch
("(n: MALE)-[r: LOVES]->(m)").
129 nwhere
("n.name = 'Andres'").
130 nand
("n.key = \{key\}").
133 var res7
= client
.cypher
(query
)
134 assert res7
.as(JsonObject)["data"].as(JsonArray).length
== 1