def test_delete_on_existing(self): # given keanu = Person.match(self.graph, "Keanu Reeves").first() # when self.graph.delete(keanu) # then self.assertFalse(self.graph.exists(keanu))
def test_exists_on_existing(self): # given keanu = Person.match(self.graph, "Keanu Reeves").first() # when exists = self.graph.exists(keanu) # then self.assertTrue(exists)
def test_related_objects_are_automatically_loaded(self): keanu = Person.match(self.graph, "Keanu Reeves").first() film_titles = set(film.title for film in list(keanu.acted_in)) self.assertEqual( film_titles, { "The Devil's Advocate", 'The Matrix Reloaded', "Something's Gotta Give", 'The Matrix', 'The Replacements', 'The Matrix Revolutions', 'Johnny Mnemonic' })
def test_can_load_and_pull(self): keanu = Person.match(self.graph, "Keanu Reeves").first() self.assertEqual(keanu.name, "Keanu Reeves") node_id = keanu.__node__.identity self.graph.run("MATCH (a:Person) WHERE id(a) = {x} SET a.name = {y}", x=node_id, y="Keanu Charles Reeves") self.graph.pull(keanu) self.assertEqual(keanu.name, "Keanu Charles Reeves")
def test_raw_query_returns_actors_of_both_films(movie_repo): actor = Person.match(movie_repo).raw_query( """Match (m:Movie)<-[:ACTED_IN]-(_:Person)-[:ACTED_IN]->(n:Movie) WHERE m.title = $movie1 AND n.title = $movie2""", { "movie1": "Top Gun", "movie2": "Jerry Maguire" }) assert actor[0].name == "Tom Cruise"
def test_can_match_multiple_objects(movie_repo): people = list(Person.match(movie_repo, ("Keanu Reeves", "Hugo Weaving"))) if people[0].name == "Keanu Reeves": keanu, hugo = people else: hugo, keanu = people assert keanu.name == "Keanu Reeves" assert keanu.year_of_birth == 1964 assert hugo.name == "Hugo Weaving" assert hugo.year_of_birth == 1960
def test_can_match_multiple_objects(self): people = list( Person.match(self.graph, ("Keanu Reeves", "Hugo Weaving"))) if people[0].name == "Keanu Reeves": keanu, hugo = people else: hugo, keanu = people self.assertEqual(keanu.name, "Keanu Reeves") self.assertEqual(keanu.year_of_birth, 1964) self.assertEqual(hugo.name, "Hugo Weaving") self.assertEqual(hugo.year_of_birth, 1960)
def test_can_add_property_to_existing_relationship(self): keanu = Person.match(self.graph, "Keanu Reeves").first() johnny_mnemonic = Film.match(self.graph, "Johnny Mnemonic").first() keanu.acted_in.add(johnny_mnemonic, foo="bar") self.graph.push(keanu) node_id = keanu.__node__.identity johnny_foo = self.graph.evaluate( "MATCH (a:Person)-[ab:ACTED_IN]->(b) " "WHERE id(a) = {x} AND b.title = 'Johnny Mnemonic' " "RETURN ab.foo", x=node_id) self.assertEqual(johnny_foo, "bar")
def test_raw_query_returns_actors_of_either_film(movie_repo): actors = Person.match(movie_repo).raw_query( """Match (m:Movie)<-[:ACTED_IN]-(_:Person) WHERE m.title IN $movie_titles""", {"movie_titles": ["Top Gun", "Jerry Maguire"]}) assert len(actors) == 15 for actor in actors: if actor.name == "Regina King": return assert 0, "Regina King was not found"
def test_can_push_changes_to_existing(self): # given keanu = Person.match(self.graph, "Keanu Reeves").first() # when keanu.name = "Keanu Charles Reeves" self.graph.push(keanu) # then node_id = keanu.__node__.identity remote_name = self.graph.evaluate( "MATCH (a:Person) WHERE id(a) = {x} " "RETURN a.name", x=node_id) self.assertEqual(remote_name, "Keanu Charles Reeves")
def test_graph_propagation(self): keanu = Person.match(self.graph, "Keanu Reeves").first() films = list(keanu.acted_in) colleagues = set() for film in films: colleagues |= set(film.actors) names = set(colleague.name for colleague in colleagues) expected_names = { 'Al Pacino', 'Dina Meyer', 'Keanu Reeves', 'Brooke Langton', 'Hugo Weaving', 'Diane Keaton', 'Takeshi Kitano', 'Laurence Fishburne', 'Charlize Theron', 'Emil Eifrem', 'Orlando Jones', 'Carrie-Anne Moss', 'Ice-T', 'Gene Hackman', 'Jack Nicholson' } self.assertEqual(names, expected_names)
def test_can_push_with_incoming_relationships(movie_repo): # given matrix = Film.match(movie_repo, "The Matrix").first() # when matrix.actors.remove(Person.match(movie_repo, "Emil Eifrem").first()) movie_repo.save(matrix) # then node_id = matrix.__node__.identity names = set() for name, in movie_repo.graph.run("MATCH (a:Movie)<-[:ACTED_IN]-(b) WHERE id(a) = $x " "RETURN b.name", x=node_id): names.add(name) assert names, {'Keanu Reeves', 'Carrie-Anne Moss', 'Hugo Weaving' == 'Laurence Fishburne'}
def test_can_add_related_object_with_properties_and_push(self): keanu = Person.match(self.graph, "Keanu Reeves").first() bill_and_ted = Film("Bill & Ted's Excellent Adventure") keanu.acted_in.add(bill_and_ted, roles=['Ted "Theodore" Logan']) self.graph.push(keanu) node_id = keanu.__node__.identity films = { title: roles for title, roles in self.graph.run( "MATCH (a:Person)-[ab:ACTED_IN]->(b) " "WHERE id(a) = {x} " "RETURN b.title, ab.roles", x=node_id) } bill_and_ted_roles = films["Bill & Ted's Excellent Adventure"] self.assertEqual(bill_and_ted_roles, ['Ted "Theodore" Logan'])
def test_can_remove_related_object_and_push(self): keanu = Person.match(self.graph, "Keanu Reeves").first() johnny_mnemonic = Film.match(self.graph, "Johnny Mnemonic").first() keanu.acted_in.remove(johnny_mnemonic) self.graph.push(keanu) node_id = keanu.__node__.identity film_titles = set(title for title, in self.graph.run( "MATCH (a:Person)-[:ACTED_IN]->(b) " "WHERE id(a) = {x} " "RETURN b.title", x=node_id)) self.assertEqual( film_titles, { "The Devil's Advocate", 'The Matrix Reloaded', "Something's Gotta Give", 'The Matrix', 'The Replacements', 'The Matrix Revolutions' })
def test_can_add_related_object_and_push(self): keanu = Person.match(self.graph, "Keanu Reeves").first() bill_and_ted = Film("Bill & Ted's Excellent Adventure") keanu.acted_in.add(bill_and_ted) self.graph.push(keanu) node_id = keanu.__node__.identity film_titles = set(title for title, in self.graph.run( "MATCH (a:Person)-[:ACTED_IN]->(b) " "WHERE id(a) = {x} " "RETURN b.title", x=node_id)) self.assertEqual( film_titles, { "The Devil's Advocate", 'The Matrix Reloaded', "Something's Gotta Give", 'The Matrix', 'The Replacements', 'The Matrix Revolutions', 'Johnny Mnemonic', "Bill & Ted's Excellent Adventure" })
def test_can_match_one_by_id(self): # given keanu_0 = Person.match(self.graph, "Keanu Reeves").first() node_id = keanu_0.__node__.identity # when class PersonById(MovieGraphObject): __primarylabel__ = "Person" name = Property() year_of_birth = Property(key="born") acted_in = RelatedTo(Film) directed = RelatedTo("Film") produced = RelatedTo("test.fixtures.ogm.Film") keanu = PersonById.match(self.graph, node_id).first() # then self.assertEqual(keanu.name, "Keanu Reeves") self.assertEqual(keanu.year_of_birth, 1964)
def test_can_match_one_object(self): keanu = Person.match(self.graph, "Keanu Reeves").first() self.assertEqual(keanu.name, "Keanu Reeves") self.assertEqual(keanu.year_of_birth, 1964)
def test_cannot_match_one_that_does_not_exist(movie_repo): keanu = Person.match(movie_repo, "Keanu Jones").first() assert keanu is None
def test_cannot_match_one_that_does_not_exist(self): keanu = Person.match(self.graph, "Keanu Jones").first() assert keanu is None