Rename REAMDE to README.md
[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 var srv = new Neo4jServer
18 srv.start_quiet
19
20 var key = get_time
21
22 var andres = new NeoNode
23 andres.labels.add_all(["PERSON", "MALE"])
24 andres["name"] = "Andres"
25 andres["age"] = 24
26 andres["status"] = true
27 andres["groups"] = new JsonArray.from([1, 2, 3])
28 andres["key"] = key
29
30 var kate = new NeoNode
31 kate.labels.add_all(["PERSON", "FEMALE"])
32 kate["name"] = "Kate"
33 kate["age"] = 25
34 kate["status"] = false
35
36 var loves = new NeoEdge(andres, "LOVES", kate)
37 loves["since"] = 1999
38
39 var client = new Neo4jClient("http://localhost:7474")
40 assert client.is_ok
41
42 print "# Save batch\n"
43
44 var batch = new NeoBatch(client)
45 batch.save_node(andres)
46 batch.save_node(kate)
47 batch.save_edge(loves)
48 var errors = batch.execute
49
50 assert errors.is_empty
51 assert andres.is_linked
52 assert kate.is_linked
53 assert loves.is_linked
54
55 var andres_url = andres.url.to_s
56 var kate_url = kate.url.to_s
57 var loves_url = loves.url.to_s
58
59 client = new Neo4jClient("http://localhost:7474")
60 assert client.is_ok
61
62 # Read Andres
63 var res4 = client.load_node(andres_url)
64 assert res4.is_linked
65 print res4["name"].to_s
66 print res4["age"].to_s
67 print res4["status"].to_s
68 print res4["groups"].to_json
69 print res4.labels.join(" ")
70 assert res4.in_edges.is_empty
71 assert not res4.out_edges.is_empty
72
73 # Read Kate
74 var res5 = client.load_node(kate_url)
75 assert res5.is_linked
76 print res5["name"].to_s
77 print res5["age"].to_s
78 print res5["status"].to_s
79 print res5.labels.join(" ")
80 assert not res5.in_edges.is_empty
81 assert res5.out_edges.is_empty
82
83 # Read LOVES
84 var res6 = client.load_edge(loves_url)
85 assert res6.is_linked
86 print res6.rel_type.to_s
87 print res6["since"].to_s
88 print "{res4["name"].to_s} LOVES {res4.out_nodes("LOVES").first["name"].to_s}"
89 print "{res5["name"].to_s} IS LOVED BY {res5.in_nodes("LOVES").first["name"].to_s}"
90
91 # Test Cypher
92 var query = (new CypherQuery).
93 nmatch("(n: MALE)-[r: LOVES]->(m)").
94 nwhere("n.name = 'Andres'").
95 nand("n.key = {key}").
96 nreturn("n, r, m")
97 var res7 = client.cypher(query)
98 assert res7.as(JsonObject)["data"].as(JsonArray).length == 1
99