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