def test_can_create_before_and_after_delete_events_custom(self): mapper = Mapper(TC) class MyNode4(Node): pass class MyNodeMapper4(EntityMapper): entity = MyNode4 def on_before_delete(self, entity): self.before_delete = self.deleted_before def on_after_delete(self, entity, response, **kwargs): self.after_delete = self.deleted_after mn = mapper.create(entity=MyNode4, id=999) my_mapper = mapper.get_mapper(mn) my_mapper.after_delete = 'AFTERDELETE' my_mapper.before_delete = 'BEFOEREDELETE' my_mapper.deleted_before = 'UDPATED{}'.format(random()) my_mapper.deleted_after = 'UDPATED{}'.format(random()) work = mapper.delete(mn) query = work.queries() work.send() self.assertEqual(my_mapper.after_delete, my_mapper.deleted_after) self.assertEqual(my_mapper.before_delete, my_mapper.deleted_before) self.assertEqual(1, len(query)) self.assertIn('MATCH', query[0][0])
def test_can_create_before_and_after_save_event_custom(self): mapper = Mapper(TC) class MyNode2(Node): pass class MyNodeMapper2(EntityMapper): entity = MyNode2 def on_before_create(self, entity): self.before_save = self.updated_before def on_after_create(self, entity, response, **kwargs): self.after_save = self.updated_after mn = mapper.create(entity=MyNode2) my_mapper = mapper.get_mapper(mn) my_mapper.after_save = 'AFTERESAVE' my_mapper.before_save = 'BEFOERESAVE' my_mapper.updated_before = 'UDPATED{}'.format(random()) my_mapper.updated_after = 'UDPATED{}'.format(random()) work = mapper.save(mn) query = work.queries() work.send() self.assertEqual(my_mapper.after_save, my_mapper.updated_after) self.assertEqual(my_mapper.before_save, my_mapper.updated_before) self.assertEqual(1, len(query)) self.assertIn('CREATE', query[0][0])
def test_can_create_on_relationship_added_end_mapper_custom_event(self): mapper = Mapper(TC) entities = { 'end': { 'end': None, 'relationship': None, } } updated = { 'end': 'end updated {}'.format(random()), } modified = { 'end': None, } class End_StartCustomNode(Node): pass class End_EndCustomNode(Node): pass class End_RelationshipCustomNode(Relationship): pass class End_RelationshipCustomNodeMapper(EntityRelationshipMapper): entity = End_RelationshipCustomNode class End_StartCustomNodeMapper(EntityMapper): entity = End_StartCustomNode __RELATIONSHIPS__ = { 'Other': RelatedEntity( relationship_entity=End_RelationshipCustomNode), } class End_EndCustomNodeMapper(EntityMapper): entity = End_EndCustomNode __RELATIONSHIPS__ = { 'Other': RelatedEntity( relationship_entity=End_RelationshipCustomNode), } def on_relationship_other_added(self, entity, relationship_entity, response, relationship_end, **kwargs): nonlocal entities, updated, modified modified['end'] = updated['end'] entities['end']['end'] = entity entities['end']['relationship'] = relationship_entity start = mapper.create(entity=End_StartCustomNode) end = mapper.create(entity=End_EndCustomNode) start_mapper = mapper.get_mapper(start) rel, work = start_mapper(start)['Other'].add(end) work.send() self.assertEqual(modified['end'], updated['end']) self.assertEqual(end, entities['end']['end']) self.assertEqual(rel, entities['end']['relationship'])
def test_can_get_mapper_single_instance_from_entity_class(self): mapper = Mapper(TC) m = get_mapper(TestLabeldtNode, mapper) m2 = mapper.get_mapper(TestLabeldtNode) self.assertIsInstance(m, EntityMapper) self.assertIsInstance(m, TestLabeldtNodeMapper) self.assertIsInstance(m2, EntityMapper) self.assertIsInstance(m2, TestLabeldtNodeMapper) self.assertEqual(id(m), id(m2))
def setUp(self): class Start(Node): pass class End(Node): pass class Other(Node): pass class Knows(Relationship): pass self.Knows = Knows() self.Start = Start() self.Other = Other() self.End = End() mapper = Mapper(connection=None) self.start_mapper = mapper.get_mapper(self.Start) self.end_mapper = mapper.get_mapper(self.End)
def test_can_load_correct_mapper_for_entity(self): class MyNodeTest(Node): pass class MyNodeMapperTest(EntityMapper): entity = MyNodeTest mapper = Mapper(TC) my_mapper = mapper.get_mapper(MyNodeTest) self.assertIsInstance(my_mapper, MyNodeMapperTest)
def test_can_create_after_save_event_custom(self): mapper = Mapper(TC) class MyNode1(Node): pass class MyNodeMapper1(EntityMapper): entity = MyNode1 def on_after_create(self, entity, response, **kwargs): self.after_save = self.updated mn = mapper.create(entity=MyNode1) my_mapper = mapper.get_mapper(mn) my_mapper.after_save = 'AFTERESAVE' my_mapper.updated = 'UDPATED{}'.format(random()) work = mapper.save(mn) queries = work.queries() work.send() self.assertEqual(my_mapper.after_save, my_mapper.updated) self.assertEqual(1, len(queries)) self.assertIn('CREATE', queries[0][0])
def test_can_create_before_save_event_custom(self): mapper = Mapper(TC) class MyNode(Node): pass class MyNodeMapper(EntityMapper): entity = MyNode def on_before_create(self, entity): self.before_save = self.updated mn = mapper.create(entity=MyNode) my_mapper = mapper.get_mapper(mn) my_mapper.before_save = 'BEFOERESAVE' my_mapper.updated = 'UDPATED{}'.format(random()) work = mapper.save(mn) queries = work.queries() work.send() self.assertEqual(my_mapper.before_save, my_mapper.updated) self.assertEqual(1, len(queries)) self.assertIn('CREATE', queries[0][0])
} # clear out all of the existing nodes mapper.query(query='MATCH (n) detach delete n') # time to create some entities and save them to the graph mark = mapper.create(entity=User, properties={'username': '******'}) someone = mapper.create(entity=User, properties={'username': '******'}) # lets save our users work = mapper.save(mark, someone) work.send() # Mark should follow someone user_mapper = mapper.get_mapper(User) follow_relationship_entity, work = user_mapper(mark)['Follows'].add(someone) work.send() # let get a list of mark's followers and who he is following followers = user_mapper(mark)['Followers']() print('Mark\'s followers', followers.data) follows = user_mapper(mark)['Follows']() print('Mark follows', follows.data) # do the same for someone followers = user_mapper(someone)['Followers']() print('Someone\'s followers', followers.data) follows = user_mapper(someone)['Follows']()
def test_can_load_generic_mapper_for_entity_without_mapper(self): node = Node() mapper = Mapper(TC) my_mapper = mapper.get_mapper(node) self.assertEqual(my_mapper.__class__.__name__, 'EntityNodeMapper')