def test_lineage_indexing(self): child = factories.AbstractCreativeWorkFactory() fetched = next(fetchers.CreativeWorkFetcher()([child.id])) assert fetched['lists'].get('lineage') is None actual_lineage = [child] for _ in range(5): new_parent = factories.AbstractCreativeWorkFactory() factories.AbstractWorkRelationFactory(type='share.ispartof', subject=actual_lineage[0], related=new_parent) actual_lineage.insert(0, new_parent) for i, work in enumerate(actual_lineage): expected_lineage = actual_lineage[:i][-3:] fetched = next(fetchers.CreativeWorkFetcher()([work.id])) fetched_lineage = fetched['lists'].get('lineage', []) assert len(fetched_lineage) == len(expected_lineage) for indexed, ancestor in zip(fetched_lineage, expected_lineage): assert indexed['id'] == util.IDObfuscator.encode(ancestor) assert indexed['title'] == ancestor.title assert set(indexed['identifiers']) == set( ancestor.identifiers.values_list('uri'))
def test_subject_indexing(self, bepresses, customs, expected): custom_tax = factories.SubjectTaxonomyFactory( source__long_title='mergik') system_tax = models.SubjectTaxonomy.objects.get( source__user__username=settings.APPLICATION_USERNAME) custom = ['Magic', 'Cool Magic', 'SUPER COOL MAGIC'] bepress = [ 'Engineering', 'Computer Engineering', 'Data Storage Systems' ] for i, name in enumerate(tuple(bepress)): bepress[i] = factories.SubjectFactory( name=name, taxonomy=system_tax, parent=bepress[i - 1] if i > 0 else None, ) for i, name in enumerate(tuple(custom)): custom[i] = factories.SubjectFactory( name=name, taxonomy=custom_tax, central_synonym=bepress[i], parent=custom[i - 1] if i > 0 else None, ) work = factories.AbstractCreativeWorkFactory() for i in bepresses: factories.ThroughSubjectsFactory(subject=bepress[i], creative_work=work) for i in customs: factories.ThroughSubjectsFactory(subject=custom[i], creative_work=work) fetched = next(fetchers.CreativeWorkFetcher()([work.id])) assert {k: v for k, v in fetched.items() if k.startswith('subject')} == expected
def test_creativework_fetcher(self): works = [ factories.AbstractCreativeWorkFactory(), factories.AbstractCreativeWorkFactory(is_deleted=True), factories.AbstractCreativeWorkFactory(), factories.AbstractCreativeWorkFactory(), ] for work in works[:-1]: factories.WorkIdentifierFactory(creative_work=work) factories.WorkIdentifierFactory.create_batch(5, creative_work=works[0]) source = factories.SourceFactory() works[1].sources.add(source.user) # Trim trailing zeros def iso(x): return re.sub(r'(\.\d+?)0*\+', r'\1+', x.isoformat()) fetched = list(fetchers.CreativeWorkFetcher()(work.id for work in works)) # TODO add more variance assert fetched == [{ 'id': util.IDObfuscator.encode(work), 'type': work._meta.verbose_name, 'types': [ cls._meta.verbose_name for cls in type(work).mro() if hasattr(cls, '_meta') and cls._meta.proxy ], 'title': work.title, 'description': work.description, 'date': iso(work.date_published), 'date_created': iso(work.date_created), 'date_modified': iso(work.date_modified), 'date_published': iso(work.date_published), 'date_updated': iso(work.date_updated), 'is_deleted': work.is_deleted or not work.identifiers.exists(), 'justification': getattr(work, 'justification', None), 'language': work.language, 'registration_type': getattr(work, 'registration_type', None), 'retracted': work.outgoing_creative_work_relations.filter( type='share.retracted').exists(), 'withdrawn': getattr(work, 'withdrawn', None), 'identifiers': list(work.identifiers.values_list('uri', flat=True)), 'sources': [user.source.long_title for user in work.sources.all()], 'subjects': [], 'subject_synonyms': [], 'tags': [], 'lists': {}, } for work in works]