Merge: doc: fixed some typos and other misc. corrections
[nit.git] / tests / test_neo4j_batch.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import neo4j
16
17 # key used to loosely assume unicity and prevent conflicting db accesses
18 var key = "NIT_TESTING_ID".environ.to_i
19
20 var andres = new NeoNode
21 andres.labels.add_all(["PERSON", "MALE"])
22 andres["name"] = "Andres"
23 andres["age"] = 24
24 andres["status"] = true
25 andres["groups"] = new JsonArray.from([1, 2, 3])
26 andres["key"] = key
27
28 var kate = new NeoNode
29 kate.labels.add_all(["PERSON", "FEMALE"])
30 kate["name"] = "Kate"
31 kate["age"] = 25
32 kate["status"] = false
33
34 var loves = new NeoEdge(andres, "LOVES", kate)
35 loves["since"] = 1999
36
37 var client = new Neo4jClient("http://neo4j:7474")
38 assert client.is_ok
39
40 # Clear the previous objects, if any
41 client.cypher(
42 new CypherQuery.from_string(
43 "MATCH (n) WHERE n.key = \{key\} OPTIONAL MATCH n-[r]-() DELETE r, n"
44 ).set("key", key)
45 )
46
47 print "# Save batch\n"
48
49 var batch = new NeoBatch(client)
50 batch.save_node(andres)
51 batch.save_node(kate)
52 batch.save_edge(loves)
53 var errors = batch.execute
54
55 assert errors.is_empty
56 assert andres.is_linked
57 assert kate.is_linked
58 assert loves.is_linked
59
60 var andres_url = andres.url.to_s
61 var kate_url = kate.url.to_s
62 var loves_url = loves.url.to_s
63
64 client = new Neo4jClient("http://neo4j:7474")
65 assert client.is_ok
66
67 # Read Andres
68 var res4 = client.load_node(andres_url)
69 assert res4.is_linked
70 print res4["name"].to_s
71 print res4["age"].to_s
72 print res4["status"].to_s
73 print res4["groups"].to_json
74 print res4.labels.join(" ")
75 assert res4.in_edges.is_empty
76 assert not res4.out_edges.is_empty
77
78 # Read Kate
79 var res5 = client.load_node(kate_url)
80 assert res5.is_linked
81 print res5["name"].to_s
82 print res5["age"].to_s
83 print res5["status"].to_s
84 print res5.labels.join(" ")
85 assert not res5.in_edges.is_empty
86 assert res5.out_edges.is_empty
87
88 # Read LOVES
89 var res6 = client.load_edge(loves_url)
90 assert res6.is_linked
91 print res6.rel_type.to_s
92 print res6["since"].to_s
93 print "{res4["name"].to_s} LOVES {res4.out_nodes("LOVES").first["name"].to_s}"
94 print "{res5["name"].to_s} IS LOVED BY {res5.in_nodes("LOVES").first["name"].to_s}"
95
96 # Test Cypher
97 var query = (new CypherQuery).
98 nmatch("(n: MALE)-[r: LOVES]->(m)").
99 nwhere("n.name = 'Andres'").
100 nand("n.key = \{key\}").
101 nreturn("n, r, m").
102 set("key", key)
103 var res7 = client.cypher(query)
104 assert res7.as(JsonObject)["data"].as(JsonArray).length == 1
105