2121
2222from unittest import TestCase
2323
24- from neo4j .v1 .session import GraphDatabase , CypherError
24+ from neo4j .v1 .session import GraphDatabase , CypherError , Record , record
2525from neo4j .v1 .typesystem import Node , Relationship , Path
2626
2727
@@ -36,11 +36,11 @@ def test_can_run_simple_statement(self):
3636 for record in session .run ("RETURN 1 AS n" ):
3737 assert record [0 ] == 1
3838 assert record ["n" ] == 1
39- with self .assertRaises (AttributeError ):
39+ with self .assertRaises (KeyError ):
40+ _ = record ["x" ]
41+ assert record ["n" ] == 1
42+ with self .assertRaises (KeyError ):
4043 _ = record ["x" ]
41- assert record .n == 1
42- with self .assertRaises (AttributeError ):
43- _ = record .x
4444 with self .assertRaises (TypeError ):
4545 _ = record [object ()]
4646 assert repr (record )
@@ -77,7 +77,6 @@ def test_can_run_simple_statement_from_bytes_string(self):
7777 for record in session .run (b"RETURN 1 AS n" ):
7878 assert record [0 ] == 1
7979 assert record ["n" ] == 1
80- assert record .n == 1
8180 assert repr (record )
8281 assert len (record ) == 1
8382 count += 1
@@ -138,12 +137,6 @@ def test_can_handle_cypher_error(self):
138137 with self .assertRaises (CypherError ):
139138 session .run ("X" )
140139
141- def test_record_equality (self ):
142- with GraphDatabase .driver ("bolt://localhost" ).session () as session :
143- result = session .run ("unwind([1, 1]) AS a RETURN a" )
144- assert result [0 ] == result [1 ]
145- assert result [0 ] != "this is not a record"
146-
147140 def test_can_obtain_summary_info (self ):
148141 with GraphDatabase .driver ("bolt://localhost" ).session () as session :
149142 result = session .run ("CREATE (n) RETURN n" )
@@ -211,6 +204,79 @@ def test_can_obtain_notification_info(self):
211204 assert position .column == 1
212205
213206
207+ class RecordTestCase (TestCase ):
208+ def test_record_equality (self ):
209+ record1 = Record (["name" ,"empire" ], ["Nigel" , "The British Empire" ])
210+ record2 = Record (["name" ,"empire" ], ["Nigel" , "The British Empire" ])
211+ record3 = Record (["name" ,"empire" ], ["Stefan" , "Das Deutschland" ])
212+ assert record1 == record2
213+ assert record1 != record3
214+ assert record2 != record3
215+
216+ def test_record_hashing (self ):
217+ record1 = Record (["name" ,"empire" ], ["Nigel" , "The British Empire" ])
218+ record2 = Record (["name" ,"empire" ], ["Nigel" , "The British Empire" ])
219+ record3 = Record (["name" ,"empire" ], ["Stefan" , "Das Deutschland" ])
220+ assert hash (record1 ) == hash (record2 )
221+ assert hash (record1 ) != hash (record3 )
222+ assert hash (record2 ) != hash (record3 )
223+
224+ def test_record_keys (self ):
225+ aRecord = Record (["name" ,"empire" ], ["Nigel" , "The British Empire" ])
226+ assert list (aRecord .keys ()) == ["name" , "empire" ]
227+
228+ def test_record_values (self ):
229+ aRecord = Record (["name" ,"empire" ], ["Nigel" , "The British Empire" ])
230+ assert list (aRecord .values ()) == ["Nigel" , "The British Empire" ]
231+
232+ def test_record_items (self ):
233+ aRecord = Record (["name" ,"empire" ], ["Nigel" , "The British Empire" ])
234+ assert list (aRecord .items ()) == [("name" , "Nigel" ), ("empire" , "The British Empire" )]
235+
236+ def test_record_index (self ):
237+ aRecord = Record (["name" ,"empire" ], ["Nigel" , "The British Empire" ])
238+ assert aRecord .index ("name" ) == 0
239+ assert aRecord .index ("empire" ) == 1
240+ with self .assertRaises (KeyError ):
241+ aRecord .index ("crap" )
242+
243+ def test_record_contains (self ):
244+ aRecord = Record (["name" ,"empire" ], ["Nigel" , "The British Empire" ])
245+ assert "name" in aRecord
246+ assert "empire" in aRecord
247+ assert "Germans" not in aRecord
248+
249+ def test_record_iter (self ):
250+ aRecord = Record (["name" ,"empire" ], ["Nigel" , "The British Empire" ])
251+ assert list (aRecord .__iter__ ()) == ["name" , "empire" ]
252+
253+ def test_record_record (self ):
254+ aRecord = Record (["name" ,"empire" ], ["Nigel" , "The British Empire" ])
255+ assert record (aRecord ) is aRecord
256+
257+ def test_record_copy (self ):
258+ original = Record (["name" ,"empire" ], ["Nigel" , "The British Empire" ])
259+ duplicate = original .copy ()
260+ assert dict (original ) == dict (duplicate )
261+ assert original .keys () == duplicate .keys ()
262+ assert original is not duplicate
263+
264+ def test_record_as_dict (self ):
265+ aRecord = Record (["name" ,"empire" ], ["Nigel" , "The British Empire" ])
266+ assert dict (aRecord ) == { "name" : "Nigel" , "empire" : "The British Empire" }
267+
268+ def test_record_as_list (self ):
269+ aRecord = Record (["name" ,"empire" ], ["Nigel" , "The British Empire" ])
270+ assert list (aRecord ) == ["name" , "empire" ]
271+
272+ def test_record_len (self ):
273+ aRecord = Record (["name" ,"empire" ], ["Nigel" , "The British Empire" ])
274+ assert len (aRecord ) == 2
275+
276+ def test_record_repr (self ):
277+ aRecord = Record (["name" ,"empire" ], ["Nigel" , "The British Empire" ])
278+ assert repr (aRecord ) == "<Record name='Nigel' empire='The British Empire'>"
279+
214280class TransactionTestCase (TestCase ):
215281 def test_can_commit_transaction (self ):
216282 with GraphDatabase .driver ("bolt://localhost" ).session () as session :
0 commit comments