Exemple #1
0
    def test_add_multiple_sources(self, celery_app):
        source1 = factories.SourceFactory()
        source2 = factories.SourceFactory()

        work = factories.AbstractCreativeWorkFactory(title='All about Canada')
        data = {
            '@id': IDObfuscator.encode(work),
            '@type': 'creativework',
            'title': 'All aboot Canada'
        }

        nd1 = NormalizedData.objects.create(source=source1.user,
                                            data={'@graph': [data]})
        nd2 = NormalizedData.objects.create(source=source2.user,
                                            data={'@graph': [data]})

        assert work.sources.count() == 0

        celery_app.tasks['share.tasks.disambiguate'](nd1.id)

        work.refresh_from_db()
        assert work.title == 'All aboot Canada'
        assert work.sources.count() == 1

        celery_app.tasks['share.tasks.disambiguate'](nd2.id)

        work.refresh_from_db()
        assert work.title == 'All aboot Canada'
        assert work.sources.count() == 2
    def test_canonical(self, Graph, ingest, first_canonical, second_canonical,
                       change):
        first_source = factories.SourceFactory(canonical=first_canonical)
        second_source = factories.SourceFactory(canonical=second_canonical)

        first_graph = Graph(
            Preprint(
                id=1,
                title='The first title',
                identifiers=[WorkIdentifier(1)],
            ))
        cs = ingest(first_graph, user=first_source.user)
        cw = cs.changes.first().target

        assert cw.type == 'share.preprint'
        assert cw.title == 'The first title'

        second_graph = Graph(
            Article(
                id=1,
                title='The Second Title',
                identifiers=[WorkIdentifier(1)],
            ))
        ingest(second_graph, user=second_source.user)

        cw = models.AbstractCreativeWork.objects.get(id=cw.id)

        assert cw.type == change.get('type', 'share.preprint')
        assert cw.title == change.get('title', 'The first title')
Exemple #3
0
def share_user():
    user = ShareUser(username='******', )
    user.save()

    # add source
    factories.SourceFactory(user=user),
    return user
Exemple #4
0
    def test_no_icon(self, schema):
        x = factories.AbstractCreativeWorkFactory()
        source = factories.SourceFactory(icon='')
        x.sources.add(source.user)

        # Have to use % formats because of {}s everywhere
        result = schema.execute('''
            query {
                creativeWork(id: "%s") {
                    id,
                    title,
                    description,
                    sources {
                        title
                    }
                }
            }
        ''' % (IDObfuscator.encode(x), ))

        assert not result.errors

        assert result.data == OrderedDict([
            ('creativeWork', OrderedDict([
                ('id', IDObfuscator.encode(x)),
                ('title', x.title),
                ('description', x.description),
                ('sources', [])
            ]))
        ])
Exemple #5
0
    def __init__(self,
                 elastic,
                 index=False,
                 num_identifiers=1,
                 num_sources=1,
                 date=None):
        self.elastic = elastic

        if date is None:
            self.work = factories.AbstractCreativeWorkFactory()
        else:
            models.AbstractCreativeWork._meta.get_field(
                'date_created').auto_now_add = False
            self.work = factories.AbstractCreativeWorkFactory(
                date_created=date,
                date_modified=date,
            )
            models.AbstractCreativeWork._meta.get_field(
                'date_created').auto_now_add = True

        self.sources = [factories.SourceFactory() for _ in range(num_sources)]
        self.work.sources.add(*[s.user for s in self.sources])

        for i in range(num_identifiers):
            factories.WorkIdentifierFactory(
                uri='http://example.com/{}/{}'.format(self.work.id, i),
                creative_work=self.work)

        if index:
            index_helpers(self)
Exemple #6
0
    def test_index_no_icon(self, elastic):
        source = factories.SourceFactory(icon=None)

        tasks.index_sources()

        with pytest.raises(NotFoundError):
            elastic.es_client.get(index=elastic.es_index,
                                  doc_type='sources',
                                  id=source.name)
Exemple #7
0
    def test_add_multiple_sources(self):
        source1 = factories.SourceFactory()
        source2 = factories.SourceFactory()

        work = factories.AbstractCreativeWorkFactory(title='All about Canada')
        data = [{'@id': IDObfuscator.encode(work), '@type': 'creativework', 'title': 'All aboot Canada'}]

        assert work.sources.count() == 0

        Ingester(data).as_user(source1.user).ingest(index=False)

        work.refresh_from_db()
        assert work.title == 'All aboot Canada'
        assert work.sources.count() == 1

        Ingester(data).as_user(source2.user).ingest(index=False)

        work.refresh_from_db()
        assert work.title == 'All aboot Canada'
        assert work.sources.count() == 2
Exemple #8
0
    def test_index(self, elastic):
        source = factories.SourceFactory()

        tasks.index_sources()

        doc = elastic.es_client.get(index=elastic.es_index,
                                    doc_type='sources',
                                    id=source.name)

        assert doc['_id'] == source.name
        assert doc['_source']['name'] == source.long_title
        assert doc['_source']['short_name'] == source.name
Exemple #9
0
    def test_canonical(self, Graph, first_canonical, second_canonical, change):
        first_source = factories.SourceFactory(canonical=first_canonical)
        second_source = factories.SourceFactory(canonical=second_canonical)

        first_graph = ChangeGraph(Graph(
            Preprint(
                id=1,
                title='The first title',
                identifiers=[WorkIdentifier(1)],
            )),
                                  namespace=first_source.user.username)

        second_graph = ChangeGraph(Graph(
            Article(
                id=1,
                title='The Second Title',
                identifiers=[WorkIdentifier(1)],
            )),
                                   namespace=second_source.user.username)

        first_graph.process()
        (cw, _) = ChangeSet.objects.from_graph(
            first_graph,
            NormalizedDataFactory(source=first_source.user).id).accept()

        assert cw.type == 'share.preprint'
        assert cw.title == 'The first title'

        second_graph.process()
        ChangeSet.objects.from_graph(
            second_graph,
            NormalizedDataFactory(source=second_source.user).id).accept()

        cw = models.AbstractCreativeWork.objects.get(id=cw.id)

        assert second_graph.nodes[0].change == change
        assert cw.type == change.get('type', 'share.preprint')
        assert cw.title == change.get('title', 'The first title')
Exemple #10
0
def make_source_config(context, label, name=None, interval=None, time=None):
    kwargs = {'label': label}

    if name is None:
        kwargs['source'] = factories.SourceFactory()
    else:
        kwargs['source'] = models.Source.objects.get(name=name)

    if interval is not None:
        kwargs['harvest_interval'] = {
            'daily': '1 day',
            'weekly': '1 week',
            'fortnightly': '2 weeks',
            'yearly': '1 year',
            'monthly': '1 month',
        }[interval]

    if time is not None:
        kwargs['harvest_after'] = time

    factories.SourceConfigFactory(**kwargs)
Exemple #11
0
    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]
Exemple #12
0
def make_source(context, name):
    if not hasattr(context, 'source'):
        context.sources = {}
    context.sources[name] = factories.SourceFactory(name=name)
    context.subject = ('sources', context.sources[name])