def test_does_not_disambiguate(self): create_subject('This') with pytest.raises(ValidationError) as e: disambiguate('_:', {'name': 'That'}, Subject) assert e.value.message == 'Invalid subject: That'
def test_disambiguates(self, change_ids): oldTag = Tag.objects.create(name='This', change_id=change_ids.get()) disTag = disambiguate('_:', {'name': 'This'}, Tag) assert disTag is not None assert disTag.id == oldTag.id assert disTag.name == oldTag.name
def test_disambiguates(self): oldSubject = create_subject('This') disSubject = disambiguate('_:', {'name': 'This'}, Subject) assert disSubject is not None assert disSubject.id == oldSubject.id assert disSubject.name == oldSubject.name
def test_does_not_disambiguate_empty_string(self, change_ids): CreativeWork.objects.create( title='', description='see here is the the thing about emptiness', change_id=change_ids.get()) disWork = disambiguate('_:', {'title': ''}, CreativeWork) assert disWork is None
def test_does_not_disambiguate(self, change_ids): CreativeWork.objects.create( title='all about giraffes', description='see here is the the thing about giraffes', change_id=change_ids.get()) disWork = disambiguate('_:', {'title': 'all about short-necked ungulates'}, CreativeWork) assert disWork is None
def test_disambiguates(self, change_ids): oldWork = CreativeWork.objects.create( title='all about giraffes', description='see here is the the thing about giraffes', change_id=change_ids.get()) disWork = disambiguate('_:', {'title': 'all about giraffes'}, CreativeWork) assert disWork is not None assert disWork.id == oldWork.id assert disWork.title == oldWork.title assert disWork.description == oldWork.description
def _disambiguate(self): if self.is_merge: return None self.__instance = disambiguation.disambiguate(self.id, { **self.attrs, **{k: v['@id'] for k, v in self.relations.items() if not str(v['@id']).startswith('_:')}, **{k: [x['@id'] for x in v if not str(x['@id']).startswith('_:')] for k, v in self.reverse_relations.items() if any(not str(x['@id']).startswith('_:') for x in v)}, }, self.model) if self.__instance: self.id = self.__instance.pk self.type = self.__instance._meta.model_name.lower() self.__refs.append((self.id, self.type))
def _disambiguate(self): if self.is_merge: return None self.__instance = disambiguation.disambiguate( self.id, { **self.attrs, **{ k: v['@id'] for k, v in self.relations.items() if not str(v['@id']).startswith('_:') } }, self.model) if self.__instance: self.id = self.__instance.pk self.__refs.append((self.id, self.type))
def test_links_disambiguate(self, change_ids): cw = CreativeWork.objects.create( title='All about cats', description='see here is the the thing about emptiness', change_id=change_ids.get()) link = Link.objects.create(url='http://share.osf.io/cats', type='provider', change_id=change_ids.get()) ThroughLinks.objects.create( link=link, creative_work=cw, change_id=change_ids.get(), link_version=link.versions.first(), creative_work_version=cw.versions.first(), ) assert disambiguate('_:', {'links': [link.pk]}, CreativeWork) == cw
def _create(self, save=True): inst = self.target_type.model_class()(change=self, **self._resolve_change()) if save: try: with transaction.atomic(): inst.save() except IntegrityError as e: from share.disambiguation import disambiguate logger.info('Handling unique violation error %r', e) self.type = Change.TYPE.update self.target = disambiguate('_:', self.change, self.target_type.model_class()) logger.info('Updating target to %r and type to update', self.target) self.save() return self._update(save=save) return inst
def test_does_not_disambiguate(self, change_ids): Tag.objects.create(name='This', change_id=change_ids.get()) disTag = disambiguate('_:', {'name': 'That'}, Tag) assert disTag is None