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_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_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_custom_relationship(self): class MyRelationship(Relationship): pass mapper = Mapper(TC) rel = mapper.create(entity=MyRelationship) self.assertIsInstance(rel, MyRelationship)
def test_can_create_custom_node(self): class MyNode(Node): pass mapper = Mapper(TC) node = mapper.create(entity=MyNode) self.assertIsInstance(node, MyNode)
def test_can_create_custom_node_with_undefined_properties(self): class MyNodeXXX(Node): pass class MyNodeXXXMapper(EntityMapper): entity = MyNodeXXX __ALLOW_UNDEFINED__ = True __PROPERTIES__ = { 'name': String(), } mapper = Mapper(TC) name = 'name{}'.format(random()) p = {'name': name} node = mapper.create(entity=MyNodeXXX, properties=p) data = node.data self.assertIsInstance(node, MyNodeXXX) self.assertEqual(1, len(data)) self.assertEqual(name, data['name'])
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])
pass class TweetMapper(EntityNodeMapper): entity = Tweet __PROPERTIES__ = { 'created': DateTime(default=datetime.now), 'text': String(), } # 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)
def test_can_create_generic_relationship(self): mapper = Mapper(TC) relationship = mapper.create(entity_type='relationship') self.assertIsInstance(relationship, Relationship)
def test_can_create_generic_node(self): mapper = Mapper(TC) node = mapper.create() self.assertIsInstance(node, Node)