Merge: doc: fixed some typos and other misc. corrections
[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 print "# Test local\n"
21
22 var client = new Neo4jClient("http://neo4j:7474")
23 assert client.is_ok
24
25 # Clear the previous objects, if any
26 client.cypher(
27 new CypherQuery.from_string(
28 "MATCH (n) WHERE n.key = \{key\} OPTIONAL MATCH n-[r]-() DELETE r, n"
29 ).set("key", key)
30 )
31
32 var andres = new NeoNode
33 andres.labels.add_all(["PERSON", "MALE"])
34 andres["name"] = "Andres"
35 andres["age"] = 24
36 andres["status"] = true
37 andres["groups"] = new JsonArray.from([1, 2, 3])
38 andres["key"] = key
39
40 # Create node
41 client.save_node(andres)
42 assert andres.is_linked
43 var andres_url = andres.url.to_s
44
45 # Read Node
46 var res1 = client.load_node(andres_url)
47 assert res1.is_linked
48 print res1["name"].to_s
49 print res1["age"].to_s
50 print res1["status"].to_s
51 print res1["groups"].to_json
52 print res1.labels.join(" ")
53 assert res1.out_edges.is_empty
54
55 # Create a second node
56 var kate = new NeoNode
57 kate.labels.add_all(["PERSON", "FEMALE"])
58 kate["name"] = "Kate"
59 kate["age"] = 25
60 kate["status"] = false
61 client.save_node(kate)
62 assert kate.is_linked
63 var kate_url = kate.url.to_s
64 var res2 = client.load_node(kate_url)
65
66 # Create an edge
67 var loves = new NeoEdge(andres, "LOVES", kate)
68 loves["since"] = 1999
69 client.save_edge(loves)
70 assert loves.is_linked
71 var loves_url = loves.url.to_s
72
73 # Check edge
74 assert loves.from == andres
75 assert loves.from == res1
76 assert loves.to == kate
77 assert loves.to == res2
78
79 # Read edge
80 var res3 = client.load_edge(loves_url)
81 assert res3.is_linked
82 print res3.rel_type.to_s
83 print res3["since"].to_s
84
85 # Follow edge
86 print "{andres["name"].to_s} LOVES {andres.out_nodes("LOVES").first["name"].to_s}"
87 print "{kate["name"].to_s} IS LOVED BY {kate.in_nodes("LOVES").first["name"].to_s}"
88
89 print "\n# Test lazy\n"
90
91 client = new Neo4jClient("http://neo4j:7474/")
92 assert client.is_ok
93
94 # Read Andres
95 var res4 = client.load_node(andres_url)
96 assert res4.is_linked
97 print res4["name"].to_s
98 print res4["age"].to_s
99 print res4["status"].to_s
100 print res4["groups"].to_json
101 print res4.labels.join(" ")
102 assert res4.in_edges.is_empty
103 assert not res4.out_edges.is_empty
104
105 # Read Kate
106 var res5 = client.load_node(kate_url)
107 assert res5.is_linked
108 print res5["name"].to_s
109 print res5["age"].to_s
110 print res5["status"].to_s
111 print res5.labels.join(" ")
112 assert not res5.in_edges.is_empty
113 assert res5.out_edges.is_empty
114
115 # Read LOVES
116 var res6 = client.load_edge(loves_url)
117 assert res6.is_linked
118 print res6.rel_type.to_s
119 print res6["since"].to_s
120 print "{res4["name"].to_s} LOVES {res4.out_nodes("LOVES").first["name"].to_s}"
121 print "{res5["name"].to_s} IS LOVED BY {res5.in_nodes("LOVES").first["name"].to_s}"
122
123 # Test Cypher
124 var query = (new CypherQuery).
125 nmatch("(n: MALE)-[r: LOVES]->(m)").
126 nwhere("n.name = 'Andres'").
127 nand("n.key = \{key\}").
128 nreturn("n, r, m").
129 set("key", key)
130 var res7 = client.cypher(query)
131 assert res7.as(JsonObject)["data"].as(JsonArray).length == 1
132