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