5da93bbf35c1d6566aa284198d32cfe5e8fdccda
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
29 client
.cypher
(new CypherQuery.from_string
("MATCH (n) WHERE n.key = {key} OPTIONAL MATCH n-[r]-() DELETE r, n"))
31 var andres
= new NeoNode
32 andres
.labels
.add_all
(["PERSON", "MALE"])
33 andres
["name"] = "Andres"
35 andres
["status"] = true
36 andres
["groups"] = new JsonArray.from
([1, 2, 3])
40 client
.save_node
(andres
)
41 assert andres
.is_linked
42 var andres_url
= andres
.url
.to_s
45 var res1
= client
.load_node
(andres_url
)
47 print res1
["name"].to_s
48 print res1
["age"].to_s
49 print res1
["status"].to_s
50 print res1
["groups"].to_json
51 print res1
.labels
.join
(" ")
52 assert res1
.out_edges
.is_empty
54 # Create a second node
55 var kate
= new NeoNode
56 kate
.labels
.add_all
(["PERSON", "FEMALE"])
59 kate
["status"] = false
60 client
.save_node
(kate
)
62 var kate_url
= kate
.url
.to_s
63 var res2
= client
.load_node
(kate_url
)
66 var loves
= new NeoEdge(andres
, "LOVES", kate
)
68 client
.save_edge
(loves
)
69 assert loves
.is_linked
70 var loves_url
= loves
.url
.to_s
73 assert loves
.from
== andres
74 assert loves
.from
== res1
75 assert loves
.to
== kate
76 assert loves
.to
== res2
79 var res3
= client
.load_edge
(loves_url
)
81 print res3
.rel_type
.to_s
82 print res3
["since"].to_s
85 print
"{andres["name"].to_s} LOVES {andres.out_nodes("LOVES").first["name"].to_s}"
86 print
"{kate["name"].to_s} IS LOVED BY {kate.in_nodes("LOVES").first["name"].to_s}"
88 print
"\n# Test lazy\n"
90 client
= new Neo4jClient("http://localhost:7474/")
94 var res4
= client
.load_node
(andres_url
)
96 print res4
["name"].to_s
97 print res4
["age"].to_s
98 print res4
["status"].to_s
99 print res4
["groups"].to_json
100 print res4
.labels
.join
(" ")
101 assert res4
.in_edges
.is_empty
102 assert not res4
.out_edges
.is_empty
105 var res5
= client
.load_node
(kate_url
)
106 assert res5
.is_linked
107 print res5
["name"].to_s
108 print res5
["age"].to_s
109 print res5
["status"].to_s
110 print res5
.labels
.join
(" ")
111 assert not res5
.in_edges
.is_empty
112 assert res5
.out_edges
.is_empty
115 var res6
= client
.load_edge
(loves_url
)
116 assert res6
.is_linked
117 print res6
.rel_type
.to_s
118 print res6
["since"].to_s
119 print
"{res4["name"].to_s} LOVES {res4.out_nodes("LOVES").first["name"].to_s}"
120 print
"{res5["name"].to_s} IS LOVED BY {res5.in_nodes("LOVES").first["name"].to_s}"
123 var query
= (new CypherQuery).
124 nmatch
("(n: MALE)-[r: LOVES]->(m)").
125 nwhere
("n.name = 'Andres'").
126 nand
("n.key = {key}").
128 var res7
= client
.cypher
(query
)
129 assert res7
.as(JsonObject)["data"].as(JsonArray).length
== 1