Beispiel #1
0
    def test_pagination(self):
        site = SiteFactory()
        page_size = 3
        total = 4
        uri = url_for('site.rdf_catalog', _external=True)
        uri_first = url_for('site.rdf_catalog_format', format='json',
                            page=1, page_size=page_size, _external=True)
        uri_last = url_for('site.rdf_catalog_format', format='json',
                           page=2, page_size=page_size, _external=True)
        VisibleDatasetFactory.create_batch(total)

        # First page
        datasets = Dataset.objects.paginate(1, page_size)
        catalog = build_catalog(site, datasets, format='json')
        graph = catalog.graph

        assert isinstance(catalog, Resource)
        assert catalog.identifier == URIRef(uri)
        types = [o.identifier for o in catalog.objects(RDF.type)]
        assert DCAT.Catalog in types
        assert HYDRA.Collection in types

        assert catalog.value(HYDRA.totalItems) == Literal(total)

        assert len(list(catalog.objects(DCAT.dataset))) == page_size

        paginations = list(graph.subjects(RDF.type,
                                          HYDRA.PartialCollectionView))
        assert len(paginations) == 1
        pagination = graph.resource(paginations[0])
        assert pagination.identifier == URIRef(uri_first)
        assert pagination.value(HYDRA.first).identifier == URIRef(uri_first)
        assert pagination.value(HYDRA.next).identifier == URIRef(uri_last)
        assert pagination.value(HYDRA.last).identifier == URIRef(uri_last)
        assert HYDRA.previous not in pagination

        # Second page
        datasets = Dataset.objects.paginate(2, page_size)
        catalog = build_catalog(site, datasets, format='json')
        graph = catalog.graph

        assert isinstance(catalog, Resource)
        assert catalog.identifier == URIRef(uri)
        types = [o.identifier for o in catalog.objects(RDF.type)]
        assert DCAT.Catalog in types
        assert HYDRA.Collection in types

        assert catalog.value(HYDRA.totalItems) == Literal(total)

        assert len(list(catalog.objects(DCAT.dataset))) == 1

        paginations = list(graph.subjects(RDF.type,
                                          HYDRA.PartialCollectionView))
        assert len(paginations) == 1
        pagination = graph.resource(paginations[0])
        assert pagination.identifier == URIRef(uri_last)
        assert pagination.value(HYDRA.first).identifier == URIRef(uri_first)
        assert pagination.value(HYDRA.previous).identifier == URIRef(uri_first)
        assert pagination.value(HYDRA.last).identifier == URIRef(uri_last)
        assert HYDRA.next not in pagination
Beispiel #2
0
    def test_org_admin_can_accept_transfer(self):
        owner = UserFactory()
        admin = UserFactory()
        org = OrganizationFactory(members=[Member(user=admin, role='admin')])
        subject = VisibleDatasetFactory(owner=owner)
        transfer = TransferFactory(owner=owner,
                                   recipient=org,
                                   subject=subject)

        owner.reload()  # Needs updated metrics
        assert owner.metrics['datasets'] == 1

        org.reload()  # Needs updated metrics
        assert org.metrics['datasets'] == 0

        admin.reload()  # Needs updated metrics
        assert admin.metrics['datasets'] == 0

        login_user(admin)
        transfer = accept_transfer(transfer)

        assert transfer.status == 'accepted'

        subject.reload()
        assert subject.organization == org
        assert subject.owner is None

        org.reload()
        assert org.metrics['datasets'] == 1

        admin.reload()
        assert admin.metrics['datasets'] == 0

        owner.reload()
        assert owner.metrics['datasets'] == 0
Beispiel #3
0
    def test_empty_search_with_filter_and_match(self):
        '''Should match both the topic criteria and the query'''
        with self.autoindex():
            # Match both the topic condition but the queried tag
            match = VisibleDatasetFactory.create_batch(2, tags=[
                'in', 'filtered'
            ])
            # Match the topic condition but not the queried tag
            no_match = VisibleDatasetFactory.create_batch(2, tags=['in'])
            # Excluded because not matching one of the topic tag
            excluded = VisibleDatasetFactory.create_batch(2, tags=[
                'out', 'filtered'
            ])
        topic = TopicFactory(tags=['in', 'no-match'])

        query = topic_search_for(topic, DatasetSearch, tag='filtered')
        result = search.query(query)

        found = [d.id for d in result]

        self.assertEqual(len(found), 2)

        for dataset in match:
            self.assertIn(dataset.id, found)
        for dataset in no_match + excluded:
            self.assertNotIn(dataset.id, found)
Beispiel #4
0
    def test_recipient_user_can_accept_transfer(self):
        owner = UserFactory()
        recipient = UserFactory()
        subject = VisibleDatasetFactory(owner=owner)
        transfer = TransferFactory(owner=owner,
                                   recipient=recipient,
                                   subject=subject)

        owner.reload()  # Needs updated metrics
        assert owner.metrics['datasets'] == 1

        recipient.reload()  # Needs updated metrics
        assert recipient.metrics['datasets'] == 0

        login_user(recipient)
        transfer = accept_transfer(transfer)

        assert transfer.status == 'accepted'

        subject.reload()
        assert subject.owner == recipient

        recipient.reload()
        assert recipient.metrics['datasets'] == 1

        owner.reload()
        assert owner.metrics['datasets'] == 0
Beispiel #5
0
    def test_empty_search_no_match(self):
        '''Should return no result if no data match the tags'''
        with self.autoindex():
            VisibleDatasetFactory.create_batch(2, tags=['whatever'])
        topic = TopicFactory(tags=['no-match'])

        query = topic_search_for(topic, DatasetSearch)
        result = search.query(query)

        self.assertEqual(len(result), 0)
Beispiel #6
0
    def test_minimal(self, app):
        site = SiteFactory()
        home_url = url_for('site.home_redirect', _external=True)
        uri = url_for('site.rdf_catalog', _external=True)
        datasets = VisibleDatasetFactory.create_batch(3)
        catalog = build_catalog(site, datasets)
        graph = catalog.graph

        assert isinstance(catalog, Resource)
        catalogs = graph.subjects(RDF.type, DCAT.Catalog)
        assert len(list(catalogs)) == 1

        assert catalog.value(RDF.type).identifier == DCAT.Catalog

        assert isinstance(catalog.identifier, URIRef)
        assert str(catalog.identifier) == uri
        assert catalog.value(DCT.title) == Literal(site.title)
        lang = app.config['DEFAULT_LANGUAGE']
        assert catalog.value(DCT.language) == Literal(lang)

        assert len(list(catalog.objects(DCAT.dataset))) == len(datasets)

        assert catalog.value(FOAF.homepage).identifier == URIRef(home_url)

        org = catalog.value(DCT.publisher)
        assert org.value(RDF.type).identifier == FOAF.Organization
        assert org.value(FOAF.name) == Literal(app.config['SITE_AUTHOR'])

        graph = catalog.graph
        graph_datasets = graph.subjects(RDF.type, DCAT.Dataset)
        assert len(list(graph_datasets)) == len(datasets)
Beispiel #7
0
    def test_catalog_rdf_paginate(self, client):
        VisibleDatasetFactory.create_batch(4)
        url = url_for('site.rdf_catalog_format', format='n3', page_size=3)
        next_url = url_for('site.rdf_catalog_format', format='n3',
                           page=2, page_size=3, _external=True)

        response = client.get(url)
        assert200(response)

        graph = Graph().parse(data=response.data, format='n3')
        pagination = graph.value(predicate=RDF.type,
                                 object=HYDRA.PartialCollectionView)
        assert pagination is not None
        pagination = graph.resource(pagination)
        assert not pagination.value(HYDRA.previous)
        assert pagination.value(HYDRA.next).identifier == URIRef(next_url)
Beispiel #8
0
    def test_no_duplicate(self):
        site = SiteFactory()
        org = OrganizationFactory()
        user = UserFactory()
        datasets = VisibleDatasetFactory.create_batch(2, owner=user)
        datasets += VisibleDatasetFactory.create_batch(2, organization=org)
        catalog = build_catalog(site, datasets)
        graph = catalog.graph

        orgs = list(graph.subjects(RDF.type, FOAF.Organization))
        assert len(orgs) == 1 + 1  # There is the site publisher
        users = list(graph.subjects(RDF.type, FOAF.Person))
        assert len(users) == 1
        org_names = list(graph.objects(orgs[0], FOAF.name))
        assert len(org_names) == 1
        user_names = list(graph.objects(users[0], FOAF.name))
        assert len(user_names) == 1
Beispiel #9
0
    def test_empty_search_with_match(self):
        '''Should only return data with at least one tag'''
        with self.autoindex():
            included = VisibleDatasetFactory.create_batch(2, tags=['in'])
            excluded = VisibleDatasetFactory.create_batch(2, tags=['out'])
        topic = TopicFactory(tags=['in', 'no-match'])

        query = topic_search_for(topic, DatasetSearch)
        result = search.query(query)

        found = [d.id for d in result]

        self.assertEqual(len(found), 2)

        for dataset in included:
            self.assertIn(dataset.id, found)
        for dataset in excluded:
            self.assertNotIn(dataset.id, found)
Beispiel #10
0
    def test_datasets_within_sitemap(self, sitemap):
        '''It should return a dataset list from the sitemap.'''
        datasets = VisibleDatasetFactory.create_batch(3)

        sitemap.fetch()

        for dataset in datasets:
            url = sitemap.get_by_url('datasets.show_redirect', dataset=dataset)
            assert url is not None
            sitemap.assert_url(url, 0.8, 'weekly')
Beispiel #11
0
    def test_datasets_without_dynamic_region(self, autoindex, client):
        paca, bdr, arles = create_geozones_fixtures()
        with autoindex:
            organization = OrganizationFactory()
            for _ in range(3):
                VisibleDatasetFactory(
                    organization=organization,
                    spatial=SpatialCoverageFactory(zones=[paca.id]))

        response = client.get(url_for('api.zone_datasets', id=paca.id))
        assert200(response)
        assert len(response.json) == 3
Beispiel #12
0
    def test_zone_datasets(self):
        paca, bdr, arles = create_geozones_fixtures()
        with self.autoindex():
            organization = OrganizationFactory()
            for _ in range(3):
                VisibleDatasetFactory(
                    organization=organization,
                    spatial=SpatialCoverageFactory(zones=[paca.id]))

        response = self.get(url_for('api.zone_datasets', id=paca.id))
        self.assert200(response)
        self.assertEqual(len(response.json), 3)
    def test_render_display_with_private_assets_only_member(self):
        '''It should render the organization page without private and empty assets'''
        organization = OrganizationFactory()
        datasets = [
            VisibleDatasetFactory(organization=organization) for _ in range(2)
        ]
        reuses = [
            VisibleReuseFactory(organization=organization) for _ in range(2)
        ]
        for _ in range(2):
            DatasetFactory(organization=organization,
                           resources=[])  # Empty asset
            VisibleDatasetFactory(organization=organization, private=True)
            ReuseFactory(organization=organization, datasets=[])  # Empty asset
            VisibleReuseFactory(organization=organization, private=True)
        response = self.get(url_for('organizations.show', org=organization))

        self.assert200(response)

        rendered_datasets = self.get_context_variable('datasets')
        self.assertEqual(len(rendered_datasets), len(datasets))

        rendered_reuses = self.get_context_variable('reuses')
        self.assertEqual(len(rendered_reuses), len(reuses))

        rendered_private_datasets = [
            dataset for dataset in rendered_datasets if dataset.private
        ]
        self.assertEqual(len(rendered_private_datasets), 0)

        rendered_private_reuses = [
            reuse for reuse in rendered_reuses if reuse.private
        ]
        self.assertEqual(len(rendered_private_reuses), 0)

        total_datasets = self.get_context_variable('total_datasets')
        self.assertEqual(total_datasets, len(datasets))

        total_reuses = self.get_context_variable('total_reuses')
        self.assertEqual(total_reuses, len(reuses))
Beispiel #14
0
 def test_with_region_datasets(self):
     with self.autoindex():
         organization = OrganizationFactory(zone=self.paca.id)
         VisibleDatasetFactory.create_batch(
             3,
             organization=organization,
             spatial=SpatialCoverageFactory(zones=[self.paca.id]))
     response = self.client.get(
         url_for('territories.territory', territory=self.paca))
     self.assert200(response)
     data = response.data.decode('utf-8')
     self.assertIn(self.paca.name, data)
     base_datasets = self.get_context_variable('base_datasets')
     self.assertEqual(len(base_datasets), 0)
     territory_datasets = self.get_context_variable('territory_datasets')
     self.assertEqual(len(territory_datasets), 3)
     for dataset in territory_datasets:
         self.assertIn(
             '<div data-udata-dataset-id="{dataset.id}"'.format(
                 dataset=dataset), data)
     self.assertEqual(self.get_context_variable('other_datasets'), [])
     self.assertNotIn('dataset-item--cta', data)
Beispiel #15
0
 def test_counties_with_other_datasets(self):
     with self.autoindex():
         organization = OrganizationFactory()
         VisibleDatasetFactory.create_batch(
             3,
             organization=organization,
             spatial=SpatialCoverageFactory(zones=[self.bdr.id]))
     response = self.client.get(
         url_for('territories.territory', territory=self.bdr))
     self.assert200(response)
     data = response.data.decode('utf-8')
     self.assertIn(self.bdr.name, data)
     base_datasets = self.get_context_variable('base_datasets')
     self.assertEqual(len(base_datasets), 0)
     other_datasets = self.get_context_variable('other_datasets')
     self.assertEqual(len(other_datasets), 3)
     for dataset in other_datasets:
         self.assertIn(
             '<div data-udata-dataset-id="{dataset.id}"'.format(
                 dataset=dataset), data)
     self.assertEqual(self.get_context_variable('territory_datasets'), [])
     self.assertIn('You want to add your own datasets to that list?', data)
Beispiel #16
0
    def test_my_org_issues(self):
        user = self.login()
        member = Member(user=user, role='editor')
        organization = OrganizationFactory(members=[member])
        reuse = ReuseFactory(owner=user)
        org_reuse = ReuseFactory(organization=organization)
        dataset = VisibleDatasetFactory(owner=user)
        org_dataset = VisibleDatasetFactory(organization=organization)

        sender = UserFactory()
        issues = [
            Issue.objects.create(subject=s, title='', user=sender)
            for s in (dataset, org_dataset, reuse, org_reuse)
        ]

        # Should not be listed
        Issue.objects.create(subject=VisibleDatasetFactory(), title='', user=sender)
        Issue.objects.create(subject=ReuseFactory(), title='', user=sender)

        response = self.get(url_for('api.my_org_issues'))
        self.assert200(response)
        self.assertEqual(len(response.json), len(issues))
Beispiel #17
0
    def test_zone_datasets_with_dynamic_and_setting(self):
        paca, bdr, arles = create_geozones_fixtures()
        organization = OrganizationFactory()
        for _ in range(3):
            VisibleDatasetFactory(
                organization=organization,
                spatial=SpatialCoverageFactory(zones=[paca.id]))

        response = self.get(url_for('api.zone_datasets', id=paca.id),
                            qs={'dynamic': 1})
        self.assert200(response)
        # No dynamic datasets given that they are added by udata-front extension.
        self.assertEqual(len(response.json), 3)
Beispiel #18
0
    def test_render_datasets(self):
        '''It should render a topic datasets page'''
        with self.autoindex():
            [VisibleDatasetFactory(tags=['tag-{0}'.format(i)])
             for i in range(3)]
        topic = TopicFactory(tags=['tag-0', 'tag-2'])

        response = self.get(url_for('topics.datasets', topic=topic))
        self.assert200(response)

        rendered_datasets = self.get_context_variable('datasets')
        self.assertEqual(len(rendered_datasets), 2)
        for dataset in rendered_datasets:
            self.assertIn(dataset.tags[0], ['tag-0', 'tag-2'])
Beispiel #19
0
    def test_dataset_api_delete(self):
        '''It should delete a dataset from the API'''
        user = self.login()
        with self.autoindex():
            dataset = VisibleDatasetFactory(owner=user)
            response = self.delete(url_for('api.dataset', dataset=dataset))

        self.assertStatus(response, 204)
        self.assertEqual(Dataset.objects.count(), 1)
        self.assertIsNotNone(Dataset.objects[0].deleted)

        response = self.get(url_for('api.datasets'))
        self.assert200(response)
        self.assertEqual(len(response.json['data']), 0)
Beispiel #20
0
    def test_zone_datasets_with_dynamic(self):
        paca, bdr, arles = create_geozones_fixtures()
        with self.autoindex():
            organization = OrganizationFactory()
            for _ in range(3):
                VisibleDatasetFactory(
                    organization=organization,
                    spatial=SpatialCoverageFactory(zones=[paca.id]))

        response = self.get(
            url_for('api.zone_datasets', id=paca.id), qs={'dynamic': 1})
        self.assert200(response)
        # No dynamic datasets given that the setting is deactivated by default.
        self.assertEqual(len(response.json), 3)
Beispiel #21
0
 def test_regions_with_other_datasets_and_pertinent_ones(self):
     user = self.login()
     with self.autoindex():
         member = Member(user=user, role='admin')
         organization = OrganizationFactory(members=[member])
         VisibleDatasetFactory.create_batch(
             3,
             organization=organization,
             spatial=SpatialCoverageFactory(zones=[self.paca.id]))
     response = self.client.get(
         url_for('territories.territory', territory=self.paca))
     self.assert200(response)
     data = response.data.decode('utf-8')
     self.assertIn(self.paca.name, data)
     base_datasets = self.get_context_variable('base_datasets')
     self.assertEqual(len(base_datasets), 0)
     other_datasets = self.get_context_variable('other_datasets')
     self.assertEqual(len(other_datasets), 3)
     for dataset in other_datasets:
         self.assertIn(
             '<div data-udata-dataset-id="{dataset.id}"'.format(
                 dataset=dataset), data)
     self.assertEqual(self.get_context_variable('territory_datasets'), [])
Beispiel #22
0
    def test_my_org_discussions(self):
        user = self.login()
        member = Member(user=user, role='editor')
        organization = OrganizationFactory(members=[member])
        reuse = ReuseFactory(owner=user)
        org_reuse = ReuseFactory(organization=organization)
        dataset = VisibleDatasetFactory(owner=user)
        org_dataset = VisibleDatasetFactory(organization=organization)

        discussions = [
            Discussion.objects.create(subject=dataset, title='', user=user),
            Discussion.objects.create(subject=org_dataset, title='', user=user),
            Discussion.objects.create(subject=reuse, title='', user=user),
            Discussion.objects.create(subject=org_reuse, title='', user=user),
        ]

        # Should not be listed
        Discussion.objects.create(subject=VisibleDatasetFactory(), title='', user=user)
        Discussion.objects.create(subject=ReuseFactory(), title='', user=user)

        response = self.get(url_for('api.my_org_discussions'))
        self.assert200(response)
        self.assertEqual(len(response.json), len(discussions))
Beispiel #23
0
    def test_empty_search_with_filter_and_match(self):
        '''Should match both the topic criteria and the query'''
        with self.autoindex():
            # Match both the topic condition but the queried tag
            match = VisibleDatasetFactory.create_batch(2,
                                                       tags=['in', 'filtered'])
            # Match the topic condition but not the queried tag
            no_match = VisibleDatasetFactory.create_batch(2, tags=['in'])
            # Excluded because not matching one of the topic tag
            excluded = VisibleDatasetFactory.create_batch(
                2, tags=['out', 'filtered'])
        topic = TopicFactory(tags=['in', 'no-match'])

        query = topic_search_for(topic, DatasetSearch, tag='filtered')
        result = search.query(query)

        found = [d.id for d in result]

        self.assertEqual(len(found), 2)

        for dataset in match:
            self.assertIn(dataset.id, found)
        for dataset in no_match + excluded:
            self.assertNotIn(dataset.id, found)
Beispiel #24
0
def generate_fixtures(datasets, reuses):
    '''Build sample fixture data (users, datasets and reuses).'''
    user = UserFactory()
    log.info('Generated user "{user.email}".'.format(user=user))

    organization = OrganizationFactory(members=[Member(user=user)])
    log.info('Generated organization "{org.name}".'.format(org=organization))

    for _ in range(datasets):
        dataset = VisibleDatasetFactory(organization=organization)
        DiscussionFactory(subject=dataset, user=user)
        ReuseFactory.create_batch(reuses, datasets=[dataset], owner=user)

    msg = 'Generated {datasets} dataset(s) with {reuses} reuse(s) each.'
    log.info(msg.format(**locals()))
Beispiel #25
0
 def test_community_resource_api_create_remote(self):
     '''It should create a remote community resource from the API'''
     user = self.login()
     dataset = VisibleDatasetFactory()
     attrs = CommunityResourceFactory.as_dict()
     attrs['dataset'] = str(dataset.id)
     response = self.post(url_for('api.community_resources'), attrs)
     self.assert201(response)
     data = json.loads(response.data)
     self.assertEqual(data['title'], attrs['title'])
     self.assertEqual(data['url'], attrs['url'])
     self.assertEqual(CommunityResource.objects.count(), 1)
     community_resource = CommunityResource.objects.first()
     self.assertEqual(community_resource.dataset, dataset)
     self.assertEqual(community_resource.owner, user)
     self.assertIsNone(community_resource.organization)
Beispiel #26
0
    def test_render_display(self):
        '''It should render a topic page'''
        with self.autoindex():
            reuses = [
                VisibleReuseFactory(tags=['tag-{0}'.format(i)])
                for i in range(3)
            ]
            datasets = [
                VisibleDatasetFactory(tags=['tag-{0}'.format(i)])
                for i in range(3)
            ]
        topic = TopicFactory(tags=['tag-0', 'tag-2'],
                             datasets=datasets,
                             reuses=reuses)

        response = self.get(url_for('topics.display', topic=topic))
        self.assert200(response)
Beispiel #27
0
 def test_regions_with_other_datasets_logged_in(self):
     self.login()
     with self.autoindex():
         organization = OrganizationFactory()
         for _ in range(3):
             VisibleDatasetFactory(
                 organization=organization,
                 spatial=SpatialCoverageFactory(zones=[self.paca.id]))
     response = self.client.get(
         url_for('territories.territory', territory=self.paca))
     self.assert200(response)
     data = response.data.decode('utf-8')
     base_datasets = self.get_context_variable('base_datasets')
     self.assertEqual(len(base_datasets), 0)
     other_datasets = self.get_context_variable('other_datasets')
     self.assertEqual(len(other_datasets), 3)
     self.assertFalse(self.get_context_variable('has_pertinent_datasets'))
     self.assertEqual(self.get_context_variable('territory_datasets'), [])
     self.assertIn('If you want your datasets to appear in that list', data)
Beispiel #28
0
 def test_community_resource_api_create(self):
     '''It should create a community resource from the API'''
     dataset = VisibleDatasetFactory()
     self.login()
     response = self.post(url_for('api.upload_new_community_resource',
                                  dataset=dataset),
                          {'file': (StringIO(b'aaa'), 'test.txt')},
                          json=False)
     self.assert201(response)
     data = json.loads(response.data)
     resource_id = data['id']
     self.assertEqual(data['title'], 'test.txt')
     response = self.put(
         url_for('api.community_resource', community=resource_id), data)
     self.assertStatus(response, 200)
     self.assertEqual(CommunityResource.objects.count(), 1)
     community_resource = CommunityResource.objects.first()
     self.assertEqual(community_resource.owner, self.user)
     self.assertIsNone(community_resource.organization)
Beispiel #29
0
    def test_pending_transfer_request_for_user(self):
        user = UserFactory()
        datasets = VisibleDatasetFactory.create_batch(2, owner=user)
        recipient = UserFactory()
        comment = faker.sentence()
        transfers = {}

        login_user(user)
        for dataset in datasets:
            transfer = request_transfer(dataset, recipient, comment)
            transfers[transfer.id] = transfer

        assert len(transfer_request_notifications(user)) == 0

        notifications = transfer_request_notifications(recipient)
        assert len(notifications) == len(datasets)
        for dt, details in notifications:
            transfer = transfers[details['id']]
            assert details['subject']['class'] == 'dataset'
            assert details['subject']['id'] == transfer.subject.id
Beispiel #30
0
    def test_pending_transfer_request_for_user(self):
        user = UserFactory()
        datasets = VisibleDatasetFactory.create_batch(2, owner=user)
        recipient = UserFactory()
        comment = faker.sentence()
        transfers = {}

        login_user(user)
        for dataset in datasets:
            transfer = request_transfer(dataset, recipient, comment)
            transfers[transfer.id] = transfer

        assert len(transfer_request_notifications(user)) == 0

        notifications = transfer_request_notifications(recipient)
        assert len(notifications) == len(datasets)
        for dt, details in notifications:
            transfer = transfers[details['id']]
            assert details['subject']['class'] == 'dataset'
            assert details['subject']['id'] == transfer.subject.id
Beispiel #31
0
    def test_producer_should_send_a_message_with_payload_if_indexable(self):
        kafka_mock = Mock()
        KafkaProducerSingleton.get_instance = lambda: kafka_mock
        fake_data = VisibleDatasetFactory(id='61fd30cb29ea95c7bc0e1211')

        reindex.run(*as_task_param(fake_data))
        producer = KafkaProducerSingleton.get_instance()

        expected_value = {
            'service': 'udata',
            'data': DatasetSearch.serialize(fake_data),
            'meta': {
                'message_type': 'dataset.index',
                'index': 'dataset'
            }
        }
        topic = self.app.config['UDATA_INSTANCE_NAME'] + '.dataset.index'
        producer.send.assert_called_with(topic,
                                         value=expected_value,
                                         key=b'61fd30cb29ea95c7bc0e1211')
Beispiel #32
0
 def test_community_resource_api_create_as_org(self):
     '''It should create a community resource as org from the API'''
     dataset = VisibleDatasetFactory()
     user = self.login()
     org = OrganizationFactory(members=[Member(user=user, role='admin')])
     response = self.post(url_for('api.upload_new_community_resource',
                                  dataset=dataset),
                          {'file': (StringIO(b'aaa'), 'test.txt')},
                          json=False)
     self.assert201(response)
     data = json.loads(response.data)
     self.assertEqual(data['title'], 'test.txt')
     resource_id = data['id']
     data['organization'] = str(org.id)
     response = self.put(
         url_for('api.community_resource', community=resource_id), data)
     self.assertStatus(response, 200)
     self.assertEqual(CommunityResource.objects.count(), 1)
     community_resource = CommunityResource.objects.first()
     self.assertEqual(community_resource.organization, org)
     self.assertIsNone(community_resource.owner)
Beispiel #33
0
    def test_count(self):
        for i in range(1, 4):
            # Tags should be normalized and deduplicated.
            tags = ['Tag "{0}"'.format(j) for j in range(i)] + ['tag-0']
            VisibleDatasetFactory(tags=tags)
            VisibleReuseFactory(tags=tags)

        count_tags.run()

        expected = {
            'tag-0': 3,
            'tag-1': 2,
            'tag-2': 1,
        }

        self.assertEqual(len(Tag.objects), len(expected))

        for name, count in expected.items():
            tag = Tag.objects.get(name=name)
            self.assertEqual(tag.total, 2 * count)
            self.assertEqual(tag.counts['datasets'], count)
            self.assertEqual(tag.counts['reuses'], count)
Beispiel #34
0
    def test_pending_transfer_request_for_org(self):
        user = UserFactory()
        datasets = VisibleDatasetFactory.create_batch(2, owner=user)
        recipient = UserFactory()
        member = Member(user=recipient, role='editor')
        org = OrganizationFactory(members=[member])
        comment = faker.sentence()
        transfers = {}

        login_user(user)
        for dataset in datasets:
            transfer = request_transfer(dataset, org, comment)
            transfers[transfer.id] = transfer

        self.assertEqual(len(transfer_request_notifications(user)), 0)

        notifications = transfer_request_notifications(recipient)
        self.assertEqual(len(notifications), len(datasets))
        for dt, details in notifications:
            transfer = transfers[details['id']]
            self.assertEqual(details['subject']['class'], 'dataset')
            self.assertEqual(details['subject']['id'], transfer.subject.id)
Beispiel #35
0
    def test_results_get_objects(self):
        data = []
        for _ in range(3):
            random_dataset = VisibleDatasetFactory()
            data.append(DatasetSearch.serialize(random_dataset))

        search_class = DatasetSearch.temp_search()
        search_query = search_class(params={})
        service_result = {
            "data": data,
            "next_page": None,
            "page": 1,
            "previous_page": None,
            "page_size": 20,
            "total_pages": 1,
            "total": 3
        }
        search_results = SearchResult(query=search_query,
                                      result=service_result.pop('data'),
                                      **service_result)

        assert len(search_results.get_objects()) == 3
    def test_render_display_with_private_datasets(self):
        '''It should render the organization page with some private datasets'''
        me = self.login()
        member = Member(user=me, role='editor')
        organization = OrganizationFactory(members=[member])
        datasets = [
            DatasetFactory(organization=organization) for _ in range(2)
        ]
        private_datasets = [
            VisibleDatasetFactory(organization=organization, private=True)
            for _ in range(2)
        ]
        response = self.get(url_for('organizations.show', org=organization))

        self.assert200(response)
        rendered_datasets = self.get_context_variable('datasets')
        self.assertEqual(len(rendered_datasets), 0)

        rendered_private_datasets = self.get_context_variable(
            'private_datasets')
        self.assertEqual(len(rendered_private_datasets),
                         len(datasets) + len(private_datasets))
Beispiel #37
0
    def test_coverage_for_level(self):
        GeoLevelFactory(id='top')
        GeoLevelFactory(id='sub', parents=['top'])
        GeoLevelFactory(id='child', parents=['sub'])

        topzones, subzones, childzones = [], [], []
        for _ in range(2):
            zone = GeoZoneFactory(level='top')
            topzones.append(zone)
            for _ in range(2):
                subzone = GeoZoneFactory(level='sub', parents=[zone.id])
                subzones.append(subzone)
                for _ in range(2):
                    childzone = GeoZoneFactory(
                        level='child', parents=[zone.id, subzone.id])
                    childzones.append(childzone)

        for zone in topzones + subzones + childzones:
            VisibleDatasetFactory(
                spatial=SpatialCoverageFactory(zones=[zone.id]))

        response = self.get(url_for('api.spatial_coverage', level='sub'))
        self.assert200(response)
        self.assertEqual(len(response.json['features']), len(subzones))

        for feature in response.json['features']:
            self.assertEqual(feature['type'], 'Feature')

            zone = get_by(subzones, 'id', feature['id'])
            self.assertIsNotNone(zone)
            assert_json_equal(feature['geometry'], zone.geom)

            properties = feature['properties']
            self.assertEqual(properties['name'], zone.name)
            self.assertEqual(properties['code'], zone.code)
            self.assertEqual(properties['level'], 'sub')
            # Nested levels datasets should be counted
            self.assertEqual(properties['datasets'], 3)
Beispiel #38
0
    def test_reindex_model(self):
        kafka_mock = Mock()
        KafkaProducerSingleton.get_instance = lambda: kafka_mock
        fake_data = VisibleDatasetFactory(id='61fd30cb29ea95c7bc0e1211')

        producer = KafkaProducerSingleton.get_instance()

        index_model(DatasetSearch,
                    start=datetime.datetime(2022, 2, 20, 20, 2),
                    reindex=True)

        expected_value = {
            'service': 'udata',
            'data': DatasetSearch.serialize(fake_data),
            'meta': {
                'message_type': 'dataset.reindex',
                'index': 'dataset-2022-02-20-20-02'
            }
        }
        topic = self.app.config['UDATA_INSTANCE_NAME'] + '.dataset.reindex'
        producer.send.assert_called_with(topic,
                                         value=expected_value,
                                         key=b'61fd30cb29ea95c7bc0e1211')
Beispiel #39
0
 def test_community_resource_api_update_with_file(self):
     '''It should update a community resource file from the API'''
     dataset = VisibleDatasetFactory()
     user = self.login()
     community_resource = CommunityResourceFactory(dataset=dataset,
                                                   owner=user)
     response = self.post(url_for('api.upload_community_resource',
                                  community=community_resource),
                          {'file': (StringIO(b'aaa'), 'test.txt')},
                          json=False)
     self.assert200(response)
     data = json.loads(response.data)
     self.assertEqual(data['id'], str(community_resource.id))
     self.assertEqual(data['title'], 'test.txt')
     data['description'] = 'new description'
     response = self.put(
         url_for('api.community_resource', community=community_resource),
         data)
     self.assert200(response)
     self.assertEqual(CommunityResource.objects.count(), 1)
     self.assertEqual(CommunityResource.objects.first().description,
                      'new description')
     self.assertTrue(
         CommunityResource.objects.first().url.endswith('test.txt'))
    def test_render_display_with_private_assets_only_member(self):
        '''It should render the organization page without private assets'''
        organization = OrganizationFactory()
        for _ in range(2):
            DatasetFactory(organization=organization)
            VisibleDatasetFactory(organization=organization, private=True)
            ReuseFactory(organization=organization)
            VisibleReuseFactory(organization=organization, private=True)
        response = self.get(url_for('organizations.show', org=organization))

        self.assert200(response)

        rendered_datasets = self.get_context_variable('datasets')
        self.assertEqual(len(rendered_datasets), 0)

        rendered_reuses = self.get_context_variable('reuses')
        self.assertEqual(len(rendered_reuses), 0)

        rendered_private_datasets = self.get_context_variable(
            'private_datasets')
        self.assertEqual(len(rendered_private_datasets), 0)

        rendered_private_reuses = self.get_context_variable('private_reuses')
        self.assertEqual(len(rendered_private_reuses), 0)
Beispiel #41
0
 def test_render_display(self):
     '''It should render the dataset page'''
     dataset = VisibleDatasetFactory()
     url = url_for('datasets.show', dataset=dataset)
     response = self.get(url)
     self.assert200(response)