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_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_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_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_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_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"}
def test_can_push_changes_to_existing(self): # given keanu = Person.select(self.graph, "Keanu Reeves").first() # when keanu.name = "Keanu Charles Reeves" self.graph.push(keanu) # then remote_node = remote(keanu.__ogm__.node) remote_name = self.graph.evaluate("MATCH (a:Person) WHERE id(a) = {x} " "RETURN a.name", x=remote_node._id) assert remote_name == "Keanu Charles Reeves"
def test_can_push_with_incoming_relationships(self): # given matrix = Film.select(self.graph, "The Matrix").first() # when matrix.actors.remove(Person.select(self.graph, "Emil Eifrem").first()) self.graph.push(matrix) # then remote_node = remote(matrix.__ogm__.node) names = set() for name, in self.graph.run("MATCH (a:Movie)<-[:ACTED_IN]-(b) WHERE id(a) = {x} " "RETURN b.name", x=remote_node._id): names.add(name) assert names == {'Keanu Reeves', 'Carrie-Anne Moss', 'Hugo Weaving', 'Laurence Fishburne'}
def test_can_find_one_by_id(self): # given keanu_0 = Person.select(self.graph, "Keanu Reeves").first() node_id = remote(keanu_0.__ogm__.node)._id # 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.select(self.graph, node_id).first() # then assert keanu.name == "Keanu Reeves" assert keanu.year_of_birth == 1964
def test_can_find_multiple_objects(self): keanu, hugo = list(Person.select(self.graph, ("Keanu Reeves", "Hugo Weaving"))) assert keanu.name == "Keanu Reeves" assert keanu.year_of_birth == 1964 assert hugo.name == "Hugo Weaving" assert hugo.year_of_birth == 1960
def test_cannot_find_one_that_does_not_exist(self): keanu = Person.select(self.graph, "Keanu Jones").first() assert keanu is None
def test_can_find_one_object(self): keanu = Person.select(self.graph, "Keanu Reeves").first() assert keanu.name == "Keanu Reeves" assert keanu.year_of_birth == 1964
def test_related_objects_are_automatically_loaded(self): keanu = Person.select(self.graph, "Keanu Reeves").first() film_titles = set(film.title for film in list(keanu.acted_in)) assert film_titles == {"The Devil's Advocate", 'The Matrix Reloaded', "Something's Gotta Give", 'The Matrix', 'The Replacements', 'The Matrix Revolutions', 'Johnny Mnemonic'}