<s> from py2neo import Node , Path , Relationship , Graph , order , size <EOL> from test . util import GraphTestCase <EOL> class PathTestCase ( GraphTestCase ) : <EOL> def test_can_construct_simple_path ( self ) : <EOL> alice = Node ( name = "<STR_LIT>" ) <EOL> bob = Node ( name = "<STR_LIT>" ) <EOL> path = Path ( alice , "<STR_LIT>" , bob ) <EOL> assert order ( path ) == <NUM_LIT:2> <EOL> assert size ( path ) == <NUM_LIT:1> <EOL> assert len ( path ) == <NUM_LIT:1> <EOL> def test_can_construct_path_with_none_node ( self ) : <EOL> alice = Node ( name = "<STR_LIT>" ) <EOL> path = Path ( alice , "<STR_LIT>" , None ) <EOL> assert order ( path ) == <NUM_LIT:2> <EOL> assert size ( path ) == <NUM_LIT:1> <EOL> assert len ( path ) == <NUM_LIT:1> <EOL> def test_can_create_path ( self ) : <EOL> path = Path ( { "<STR_LIT:name>" : "<STR_LIT>" } , "<STR_LIT>" , { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> nodes = path . nodes ( ) <EOL> assert len ( path ) == <NUM_LIT:1> <EOL> assert nodes [ <NUM_LIT:0> ] [ "<STR_LIT:name>" ] == "<STR_LIT>" <EOL> assert path [ <NUM_LIT:0> ] . type ( ) == "<STR_LIT>" <EOL> assert nodes [ - <NUM_LIT:1> ] [ "<STR_LIT:name>" ] == "<STR_LIT>" <EOL> path = Path ( path , "<STR_LIT>" , { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> nodes = path . nodes ( ) <EOL> assert len ( path ) == <NUM_LIT:2> <EOL> assert nodes [ <NUM_LIT:0> ] [ "<STR_LIT:name>" ] == "<STR_LIT>" <EOL> assert path [ <NUM_LIT:0> ] . type ( ) == "<STR_LIT>" <EOL> assert nodes [ <NUM_LIT:1> ] [ "<STR_LIT:name>" ] == "<STR_LIT>" <EOL> path = Path ( { "<STR_LIT:name>" : "<STR_LIT>" } , "<STR_LIT>" , path ) <EOL> nodes = path . nodes ( ) <EOL> assert len ( path ) == <NUM_LIT:3> <EOL> assert nodes [ <NUM_LIT:0> ] [ "<STR_LIT:name>" ] == "<STR_LIT>" <EOL> assert path [ <NUM_LIT:0> ] . type ( ) == "<STR_LIT>" <EOL> assert nodes [ <NUM_LIT:1> ] [ "<STR_LIT:name>" ] == "<STR_LIT>" <EOL> assert path [ <NUM_LIT:1> ] . type ( ) == "<STR_LIT>" <EOL> assert nodes [ <NUM_LIT:2> ] [ "<STR_LIT:name>" ] == "<STR_LIT>" <EOL> def test_can_slice_path ( self ) : <EOL> a = Node ( name = "<STR_LIT>" ) <EOL> b = Node ( name = "<STR_LIT>" ) <EOL> c = Node ( name = "<STR_LIT>" ) <EOL> d = Node ( name = "<STR_LIT>" ) <EOL> e = Node ( name = "<STR_LIT>" ) <EOL> f = Node ( name = "<STR_LIT>" ) <EOL> path = Path ( a , "<STR_LIT>" , b , "<STR_LIT>" , c , "<STR_LIT>" , d , "<STR_LIT>" , e , "<STR_LIT>" , f ) <EOL> assert len ( path ) == <NUM_LIT:5> <EOL> assert path [ <NUM_LIT:0> ] == Relationship ( a , "<STR_LIT>" , b ) <EOL> assert path [ <NUM_LIT:1> ] == Relationship ( b , "<STR_LIT>" , c ) <EOL> assert path [ <NUM_LIT:2> ] == Relationship ( c , "<STR_LIT>" , d ) <EOL> assert path [ - <NUM_LIT:1> ] == Relationship ( e , "<STR_LIT>" , f ) <EOL> assert path [ <NUM_LIT:0> : <NUM_LIT:2> ] == Path ( a , "<STR_LIT>" , b , "<STR_LIT>" , c ) <EOL> assert path [ <NUM_LIT:3> : <NUM_LIT:5> ] == Path ( d , "<STR_LIT>" , e , "<STR_LIT>" , f ) <EOL> assert path [ : ] == Path ( a , "<STR_LIT>" , b , "<STR_LIT>" , c , "<STR_LIT>" , d , "<STR_LIT>" , e , "<STR_LIT>" , f ) <EOL> def test_can_iterate_path ( self ) : <EOL> a = Node ( name = "<STR_LIT>" ) <EOL> b = Node ( name = "<STR_LIT>" ) <EOL> c = Node ( name = "<STR_LIT>" ) <EOL> d = Node ( name = "<STR_LIT>" ) <EOL> e = Node ( name = "<STR_LIT>" ) <EOL> f = Node ( name = "<STR_LIT>" ) <EOL> path = Path ( a , "<STR_LIT>" , b , "<STR_LIT>" , c , "<STR_LIT>" , d , "<STR_LIT>" , e , "<STR_LIT>" , f ) <EOL> assert list ( path ) == [ <EOL> Relationship ( a , '<STR_LIT>' , b ) , <EOL> Relationship ( b , '<STR_LIT>' , c ) , <EOL> Relationship ( c , '<STR_LIT>' , d ) , <EOL> Relationship ( d , '<STR_LIT>' , e ) , <EOL> Relationship ( e , '<STR_LIT>' , f ) , <EOL> ] <EOL> assert list ( enumerate ( path ) ) == [ <EOL> ( <NUM_LIT:0> , Relationship ( a , '<STR_LIT>' , b ) ) , <EOL> ( <NUM_LIT:1> , Relationship ( b , '<STR_LIT>' , c ) ) , <EOL> ( <NUM_LIT:2> , Relationship ( c , '<STR_LIT>' , d ) ) , <EOL> ( <NUM_LIT:3> , Relationship ( d , '<STR_LIT>' , e ) ) , <EOL> ( <NUM_LIT:4> , Relationship ( e , '<STR_LIT>' , f ) ) <EOL> ] <EOL> def test_can_join_paths ( self ) : <EOL> a = Node ( name = "<STR_LIT>" ) <EOL> b = Node ( name = "<STR_LIT>" ) <EOL> c = Node ( name = "<STR_LIT>" ) <EOL> d = Node ( name = "<STR_LIT>" ) <EOL> path1 = Path ( a , "<STR_LIT>" , b ) <EOL> path2 = Path ( c , "<STR_LIT>" , d ) <EOL> path = Path ( path1 , "<STR_LIT>" , path2 ) <EOL> assert list ( path ) == [ <EOL> Relationship ( a , '<STR_LIT>' , b ) , <EOL> Relationship ( b , '<STR_LIT>' , c ) , <EOL> Relationship ( c , '<STR_LIT>' , d ) , <EOL> ] <EOL> def test_path_equality ( self ) : <EOL> alice = Node ( name = "<STR_LIT>" ) <EOL> bob = Node ( name = "<STR_LIT>" ) <EOL> carol = Node ( name = "<STR_LIT>" ) <EOL> dave = Node ( name = "<STR_LIT>" ) <EOL> path_1 = Path ( alice , "<STR_LIT>" , bob , Relationship ( carol , "<STR_LIT>" , bob ) , carol , "<STR_LIT>" , dave ) <EOL> path_2 = Path ( alice , "<STR_LIT>" , bob , Relationship ( carol , "<STR_LIT>" , bob ) , carol , "<STR_LIT>" , dave ) <EOL> assert path_1 == path_2 <EOL> def test_path_inequality ( self ) : <EOL> alice = Node ( name = "<STR_LIT>" ) <EOL> bob = Node ( name = "<STR_LIT>" ) <EOL> carol = Node ( name = "<STR_LIT>" ) <EOL> dave = Node ( name = "<STR_LIT>" ) <EOL> path_1 = Path ( alice , "<STR_LIT>" , bob , Relationship ( carol , "<STR_LIT>" , bob ) , carol , "<STR_LIT>" , dave ) <EOL> path_2 = Path ( alice , "<STR_LIT>" , carol , Relationship ( dave , "<STR_LIT>" , carol ) , dave ) <EOL> assert path_1 != path_2 <EOL> assert path_1 != "<STR_LIT>" <EOL> def test_path_in_several_ways ( self ) : <EOL> alice = Node ( name = "<STR_LIT>" ) <EOL> bob = Node ( name = "<STR_LIT>" ) <EOL> carol = Node ( name = "<STR_LIT>" ) <EOL> dave = Node ( name = "<STR_LIT>" ) <EOL> path = Path ( alice , "<STR_LIT>" , bob , Relationship ( carol , "<STR_LIT>" , bob ) , carol , "<STR_LIT>" , dave ) <EOL> assert path . __bool__ ( ) <EOL> assert path . __nonzero__ ( ) <EOL> assert path [ <NUM_LIT:0> ] == Relationship ( alice , "<STR_LIT>" , bob ) <EOL> assert path [ <NUM_LIT:1> ] == Relationship ( carol , "<STR_LIT>" , bob ) <EOL> assert path [ <NUM_LIT:2> ] == Relationship ( carol , "<STR_LIT>" , dave ) <EOL> assert path [ - <NUM_LIT:1> ] == Relationship ( carol , "<STR_LIT>" , dave ) <EOL> assert path [ <NUM_LIT:0> : <NUM_LIT:1> ] == Path ( alice , "<STR_LIT>" , bob ) <EOL> assert path [ <NUM_LIT:0> : <NUM_LIT:2> ] == Path ( alice , "<STR_LIT>" , bob , Relationship ( carol , "<STR_LIT>" , bob ) , carol ) <EOL> try : <EOL> _ = path [ <NUM_LIT:7> ] <EOL> except IndexError : <EOL> assert True <EOL> else : <EOL> assert False <EOL> def test_path_direction ( self ) : <EOL> cypher = """<STR_LIT>""" <EOL> path = self . graph . evaluate ( cypher ) <EOL> assert path [ <NUM_LIT:0> ] . start_node ( ) [ "<STR_LIT:name>" ] == "<STR_LIT>" <EOL> assert path [ <NUM_LIT:0> ] . end_node ( ) [ "<STR_LIT:name>" ] == "<STR_LIT>" <EOL> assert path [ <NUM_LIT:1> ] . start_node ( ) [ "<STR_LIT:name>" ] == "<STR_LIT>" <EOL> assert path [ <NUM_LIT:1> ] . end_node ( ) [ "<STR_LIT:name>" ] == "<STR_LIT>" <EOL> assert path [ <NUM_LIT:2> ] . start_node ( ) [ "<STR_LIT:name>" ] == "<STR_LIT>" <EOL> assert path [ <NUM_LIT:2> ] . end_node ( ) [ "<STR_LIT:name>" ] == "<STR_LIT>" <EOL> class CreatePathTestCase ( GraphTestCase ) : <EOL> def setUp ( self ) : <EOL> Graph . auto_sync_properties = True <EOL> def test_can_create_path ( self ) : <EOL> path = Path ( { "<STR_LIT:name>" : "<STR_LIT>" } , "<STR_LIT>" , { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> nodes = path . nodes ( ) <EOL> assert dict ( nodes [ <NUM_LIT:0> ] ) == { "<STR_LIT:name>" : "<STR_LIT>" } <EOL> assert path [ <NUM_LIT:0> ] . type ( ) == "<STR_LIT>" <EOL> assert dict ( nodes [ <NUM_LIT:1> ] ) == { "<STR_LIT:name>" : "<STR_LIT>" } <EOL> self . graph . create ( path ) <EOL> assert isinstance ( nodes [ <NUM_LIT:0> ] , Node ) <EOL> assert nodes [ <NUM_LIT:0> ] [ "<STR_LIT:name>" ] == "<STR_LIT>" <EOL> assert isinstance ( path [ <NUM_LIT:0> ] , Relationship ) <EOL> assert path [ <NUM_LIT:0> ] . type ( ) == "<STR_LIT>" <EOL> assert isinstance ( nodes [ <NUM_LIT:1> ] , Node ) <EOL> assert nodes [ <NUM_LIT:1> ] [ "<STR_LIT:name>" ] == "<STR_LIT>" <EOL> def test_can_create_path_with_rel_properties ( self ) : <EOL> path = Path ( { "<STR_LIT:name>" : "<STR_LIT>" } , ( "<STR_LIT>" , { "<STR_LIT>" : <NUM_LIT> } ) , { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> nodes = path . nodes ( ) <EOL> assert dict ( nodes [ <NUM_LIT:0> ] ) == { "<STR_LIT:name>" : "<STR_LIT>" } <EOL> assert path [ <NUM_LIT:0> ] . type ( ) == "<STR_LIT>" <EOL> assert dict ( path [ <NUM_LIT:0> ] ) == { "<STR_LIT>" : <NUM_LIT> } <EOL> assert dict ( nodes [ <NUM_LIT:1> ] ) == { "<STR_LIT:name>" : "<STR_LIT>" } <EOL> self . graph . create ( path ) <EOL> assert isinstance ( nodes [ <NUM_LIT:0> ] , Node ) <EOL> assert nodes [ <NUM_LIT:0> ] [ "<STR_LIT:name>" ] == "<STR_LIT>" <EOL> assert isinstance ( path [ <NUM_LIT:0> ] , Relationship ) <EOL> assert path [ <NUM_LIT:0> ] . type ( ) == "<STR_LIT>" <EOL> assert dict ( path [ <NUM_LIT:0> ] ) == { "<STR_LIT>" : <NUM_LIT> } <EOL> assert isinstance ( nodes [ <NUM_LIT:1> ] , Node ) <EOL> assert nodes [ <NUM_LIT:1> ] [ "<STR_LIT:name>" ] == "<STR_LIT>" <EOL> class PathIterationAndReversalTestCase ( GraphTestCase ) : <EOL> def setUp ( self ) : <EOL> self . alice = Node ( "<STR_LIT>" , name = "<STR_LIT>" , age = <NUM_LIT> ) <EOL> self . bob = Node ( "<STR_LIT>" , name = "<STR_LIT>" , age = <NUM_LIT> ) <EOL> self . carol = Node ( "<STR_LIT>" , name = "<STR_LIT>" , age = <NUM_LIT> ) <EOL> self . dave = Node ( "<STR_LIT>" , name = "<STR_LIT>" , age = <NUM_LIT> ) <EOL> def test_can_iterate_path_relationships ( self ) : <EOL> path = Path ( self . alice , "<STR_LIT>" , self . bob , Relationship ( self . carol , "<STR_LIT>" , self . bob ) , <EOL> self . carol , "<STR_LIT>" , self . dave ) <EOL> rels = list ( path ) <EOL> assert rels == [ <EOL> Relationship ( self . alice , "<STR_LIT>" , self . bob ) , <EOL> Relationship ( self . carol , "<STR_LIT>" , self . bob ) , <EOL> Relationship ( self . carol , "<STR_LIT>" , self . dave ) , <EOL> ] <EOL> def test_can_make_new_path_from_relationships ( self ) : <EOL> path = Path ( self . alice , "<STR_LIT>" , self . bob , Relationship ( self . carol , "<STR_LIT>" , self . bob ) , <EOL> self . carol , "<STR_LIT>" , self . dave ) <EOL> rels = list ( path ) <EOL> new_path = Path ( * rels ) <EOL> new_rels = list ( new_path ) <EOL> assert new_rels == [ <EOL> Relationship ( self . alice , "<STR_LIT>" , self . bob ) , <EOL> Relationship ( self . carol , "<STR_LIT>" , self . bob ) , <EOL> Relationship ( self . carol , "<STR_LIT>" , self . dave ) , <EOL> ] <EOL> def test_can_make_new_path_from_path ( self ) : <EOL> path = Path ( self . alice , "<STR_LIT>" , self . bob , Relationship ( self . carol , "<STR_LIT>" , self . bob ) , <EOL> self . carol , "<STR_LIT>" , self . dave ) <EOL> new_path = Path ( path ) <EOL> new_rels = list ( new_path ) <EOL> assert new_rels == [ <EOL> Relationship ( self . alice , "<STR_LIT>" , self . bob ) , <EOL> Relationship ( self . carol , "<STR_LIT>" , self . bob ) , <EOL> Relationship ( self . carol , "<STR_LIT>" , self . dave ) , <EOL> ] <EOL> def test_can_reverse_iterate_path_relationships ( self ) : <EOL> path = Path ( self . alice , "<STR_LIT>" , self . bob , Relationship ( self . carol , "<STR_LIT>" , self . bob ) , <EOL> self . carol , "<STR_LIT>" , self . dave ) <EOL> rels = list ( reversed ( path ) ) <EOL> assert rels == [ <EOL> Relationship ( self . carol , "<STR_LIT>" , self . dave ) , <EOL> Relationship ( self . carol , "<STR_LIT>" , self . bob ) , <EOL> Relationship ( self . alice , "<STR_LIT>" , self . bob ) , <EOL> ] </s>
