def test_not_exists_on_non_existing(movie_repo): # given alice = Person() alice.name = "Alice Smith" alice.year_of_birth = 1970 # when exists = movie_repo.exists(alice) # then assert not exists
def test_not_exists_on_non_existing(self): # given alice = Person() alice.name = "Alice Smith" alice.year_of_birth = 1970 # when exists = self.graph.exists(alice) # then self.assertFalse(exists)
def test_create(self): # given alice = Person() alice.name = "Alice" alice.year_of_birth = 1970 alice.acted_in.add(Film.select(self.graph, "The Matrix").first()) # when self.graph.create(alice) # then node = alice.__ogm__.node remote_node = remote(node) assert remote_node
def test_create(movie_repo): # given alice = Person() alice.name = "Alice" alice.year_of_birth = 1970 alice.acted_in.add(Film.match(movie_repo, "The Matrix").first()) # when movie_repo.save(alice) # then node = alice.__node__ assert node.graph == movie_repo.graph assert node.identity is not None
def test_can_push_new(movie_repo): # given alice = Person() alice.name = "Alice Smith" alice.year_of_birth = 1970 # when movie_repo.save(alice) # then node_id = alice.__node__.identity remote_name = movie_repo.graph.evaluate("MATCH (a:Person) WHERE id(a) = $x " "RETURN a.name", x=node_id) assert remote_name == "Alice Smith"
def test_can_push_new(self): # given alice = Person() alice.name = "Alice Smith" alice.year_of_birth = 1970 # when self.graph.push(alice) # then remote_node = remote(alice.__ogm__.node) remote_name = self.graph.evaluate("MATCH (a:Person) WHERE id(a) = {x} " "RETURN a.name", x=remote_node._id) assert remote_name == "Alice Smith"
def test_create(self): # given alice = Person() alice.name = "Alice" alice.year_of_birth = 1970 alice.acted_in.add(Film.match(self.graph, "The Matrix").first()) # when self.graph.create(alice) # then node = alice.__node__ self.assertEqual(node.graph, self.graph) self.assertIsNotNone(node.identity)
def test_can_push_new(self): # given alice = Person() alice.name = "Alice Smith" alice.year_of_birth = 1970 # when self.graph.push(alice) # then node_id = alice.__node__.identity remote_name = self.graph.evaluate( "MATCH (a:Person) WHERE id(a) = {x} " "RETURN a.name", x=node_id) self.assertEqual(remote_name, "Alice Smith")
def test_can_push_new_that_points_to_new(movie_repo): # given the_dominatrix = Film("The Dominatrix") alice = Person() alice.name = "Alice Smith" alice.year_of_birth = 1970 alice.acted_in.add(the_dominatrix) # when movie_repo.save(alice) # then node_id = alice.__node__.identity film_node = movie_repo.graph.evaluate("MATCH (a:Person)-[:ACTED_IN]->(b) " "WHERE id(a) = $x RETURN b", x=node_id) assert film_node["title"] == "The Dominatrix"
def test_can_push_new_that_points_to_existing(movie_repo): # given alice = Person() alice.name = "Alice Smith" alice.year_of_birth = 1970 alice.acted_in.add(Film.match(movie_repo, "The Matrix").first()) # when movie_repo.save(alice) # then node_id = alice.__node__.identity film_node = movie_repo.graph.evaluate("MATCH (a:Person)-[:ACTED_IN]->(b) " "WHERE id(a) = $x RETURN b", x=node_id) assert film_node["title"] == "The Matrix" assert film_node["tagline"] == "Welcome to the Real World"
def test_can_push_new_that_points_to_existing(self): # given alice = Person() alice.name = "Alice Smith" alice.year_of_birth = 1970 alice.acted_in.add(Film.select(self.graph, "The Matrix").first()) # when self.graph.push(alice) # then remote_node = remote(alice.__ogm__.node) film_node = self.graph.evaluate("MATCH (a:Person)-[:ACTED_IN]->(b) WHERE id(a) = {x} RETURN b", x=remote_node._id) assert film_node["title"] == "The Matrix" assert film_node["tagline"] == "Welcome to the Real World"
def test_can_push_new_that_points_to_new(self): # given the_dominatrix = Film("The Dominatrix") alice = Person() alice.name = "Alice Smith" alice.year_of_birth = 1970 alice.acted_in.add(the_dominatrix) # when self.graph.push(alice) # then remote_node = remote(alice.__ogm__.node) film_node = self.graph.evaluate("MATCH (a:Person)-[:ACTED_IN]->(b) WHERE id(a) = {x} RETURN b", x=remote_node._id) assert film_node["title"] == "The Dominatrix"
def test_can_push_new_that_points_to_new(self): # given the_dominatrix = Film("The Dominatrix") alice = Person() alice.name = "Alice Smith" alice.year_of_birth = 1970 alice.acted_in.add(the_dominatrix) # when self.graph.push(alice) # then node_id = alice.__node__.identity film_node = self.graph.evaluate( "MATCH (a:Person)-[:ACTED_IN]->(b) WHERE id(a) = {x} RETURN b", x=node_id) self.assertEqual(film_node["title"], "The Dominatrix")
def test_can_load_and_pull(self): keanu = Person.select(self.graph, "Keanu Reeves").first() assert keanu.name == "Keanu Reeves" remote_node = remote(keanu.__ogm__.node) self.graph.run("MATCH (a:Person) WHERE id(a) = {x} SET a.name = {y}", x=remote_node._id, y="Keanu Charles Reeves") self.graph.pull(keanu) assert keanu.name == "Keanu Charles Reeves"
def test_can_push_new_that_points_to_existing(self): # given alice = Person() alice.name = "Alice Smith" alice.year_of_birth = 1970 alice.acted_in.add(Film.match(self.graph, "The Matrix").first()) # when self.graph.push(alice) # then node_id = alice.__node__.identity film_node = self.graph.evaluate( "MATCH (a:Person)-[:ACTED_IN]->(b) WHERE id(a) = {x} RETURN b", x=node_id) self.assertEqual(film_node["title"], "The Matrix") self.assertEqual(film_node["tagline"], "Welcome to the Real World")
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_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_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_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_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_add_property_to_existing_relationship(self): keanu = Person.select(self.graph, "Keanu Reeves").first() johnny_mnemonic = Film.select(self.graph, "Johnny Mnemonic").first() keanu.acted_in.add(johnny_mnemonic, foo="bar") self.graph.push(keanu) remote_node = remote(keanu.__ogm__.node) 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=remote_node._id) assert johnny_foo == "bar"
def test_delete_on_existing(self): # given keanu = Person.select(self.graph, "Keanu Reeves").first() node = keanu.__ogm__.node # when self.graph.delete(keanu) # then assert not self.graph.exists(node)
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_graph_propagation(self): keanu = Person.select(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'} assert names == expected_names
def test_can_add_related_object_with_properties_and_push(self): keanu = Person.select(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) remote_node = remote(keanu.__ogm__.node) 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=remote_node._id)} bill_and_ted_roles = films["Bill & Ted's Excellent Adventure"] assert bill_and_ted_roles == ['Ted "Theodore" Logan']
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_remove_related_object_and_push(self): keanu = Person.select(self.graph, "Keanu Reeves").first() johnny_mnemonic = Film.select(self.graph, "Johnny Mnemonic").first() keanu.acted_in.remove(johnny_mnemonic) self.graph.push(keanu) remote_node = remote(keanu.__ogm__.node) film_titles = set(title for title, in self.graph.run("MATCH (a:Person)-[:ACTED_IN]->(b) " "WHERE id(a) = {x} " "RETURN b.title", x=remote_node._id)) assert film_titles == {"The Devil's Advocate", 'The Matrix Reloaded', "Something's Gotta Give", 'The Matrix', 'The Replacements', 'The Matrix Revolutions'}
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_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_can_add_related_object_and_push(self): keanu = Person.select(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) remote_node = remote(keanu.__ogm__.node) film_titles = set(title for title, in self.graph.run("MATCH (a:Person)-[:ACTED_IN]->(b) " "WHERE id(a) = {x} " "RETURN b.title", x=remote_node._id)) assert 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"}