neo4j: Prevent accidental code injections in the tests
[nit.git] / tests / test_neo4j.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 srv = new Neo4jServer
21 srv.start_quiet
22
23 print "# Test local\n"
24
25 var client = new Neo4jClient("http://localhost:7474")
26 assert client.is_ok
27
28 # Clear the previous objects, if any
29 client.cypher(
30 new CypherQuery.from_string(
31 "MATCH (n) WHERE n.key = \{key\} OPTIONAL MATCH n-[r]-() DELETE r, n"
32 ).set("key", key)
33 )
34
35 var andres = new NeoNode
36 andres.labels.add_all(["PERSON", "MALE"])
37 andres["name"] = "Andres"
38 andres["age"] = 24
39 andres["status"] = true
40 andres["groups"] = new JsonArray.from([1, 2, 3])
41 andres["key"] = key
42
43 # Create node
44 client.save_node(andres)
45 assert andres.is_linked
46 var andres_url = andres.url.to_s
47
48 # Read Node
49 var res1 = client.load_node(andres_url)
50 assert res1.is_linked
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
57
58 # Create a second node
59 var kate = new NeoNode
60 kate.labels.add_all(["PERSON", "FEMALE"])
61 kate["name"] = "Kate"
62 kate["age"] = 25
63 kate["status"] = false
64 client.save_node(kate)
65 assert kate.is_linked
66 var kate_url = kate.url.to_s
67 var res2 = client.load_node(kate_url)
68
69 # Create an edge
70 var loves = new NeoEdge(andres, "LOVES", kate)
71 loves["since"] = 1999
72 client.save_edge(loves)
73 assert loves.is_linked
74 var loves_url = loves.url.to_s
75
76 # Check edge
77 assert loves.from == andres
78 assert loves.from == res1
79 assert loves.to == kate
80 assert loves.to == res2
81
82 # Read edge
83 var res3 = client.load_edge(loves_url)
84 assert res3.is_linked
85 print res3.rel_type.to_s
86 print res3["since"].to_s
87
88 # Follow edge
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}"
91
92 print "\n# Test lazy\n"
93
94 client = new Neo4jClient("http://localhost:7474/")
95 assert client.is_ok
96
97 # Read Andres
98 var res4 = client.load_node(andres_url)
99 assert res4.is_linked
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
107
108 # Read Kate
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
117
118 # Read LOVES
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}"
125
126 # Test Cypher
127 var query = (new CypherQuery).
128 nmatch("(n: MALE)-[r: LOVES]->(m)").
129 nwhere("n.name = 'Andres'").
130 nand("n.key = \{key\}").
131 nreturn("n, r, m").
132 set("key", key)
133 var res7 = client.cypher(query)
134 assert res7.as(JsonObject)["data"].as(JsonArray).length == 1
135