Example #1
0
    def test_my_org_discussions_with_search(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 = [
            DatasetDiscussion.objects.create(subject=dataset,
                                             title='foo',
                                             user=user),
            ReuseDiscussion.objects.create(subject=org_reuse,
                                           title='foo',
                                           user=user),
        ]

        # Should not be listed.
        ReuseDiscussion.objects.create(subject=reuse, title='', user=user),
        DatasetDiscussion.objects.create(subject=org_dataset,
                                         title='',
                                         user=user),

        # Should really not be listed.
        DatasetDiscussion.objects.create(subject=VisibleDatasetFactory(),
                                         title='foo',
                                         user=user)
        ReuseDiscussion.objects.create(subject=ReuseFactory(),
                                       title='foo',
                                       user=user)

        response = self.get(url_for('api.my_org_discussions'), qs={'q': 'foo'})
        self.assert200(response)
        self.assertEqual(len(response.json), len(discussions))
Example #2
0
    def test_coverage_for_level(self):
        register_level('country', 'included', 'Included level')
        included = [TerritoryFactory(level='included') for _ in range(2)]
        excluded = [TerritoryFactory(level='country') for _ in range(2)]
        [
            VisibleDatasetFactory(spatial=SpatialCoverageFactory(
                territories=[t.reference()])) for t in included
        ]
        [
            VisibleDatasetFactory(spatial=SpatialCoverageFactory(
                territories=[t.reference()])) for t in excluded
        ]

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

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

            territory = get_by(included, 'id', ObjectId(feature['id']))
            self.assertIsNotNone(territory)
            self.assertEqual(feature['geometry'], territory.geom)

            properties = feature['properties']
            self.assertEqual(properties['name'], territory.name)
            self.assertEqual(properties['code'], territory.code)
            self.assertEqual(properties['level'], 'included')
            self.assertEqual(properties['datasets'], 1)
Example #3
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)

        issues = [
            DatasetIssue.objects.create(subject=dataset, title='', user=user),
            DatasetIssue.objects.create(subject=org_dataset,
                                        title='',
                                        user=user),
            ReuseIssue.objects.create(subject=reuse, title='', user=user),
            ReuseIssue.objects.create(subject=org_reuse, title='', user=user),
        ]

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

        response = self.get(url_for('api.my_org_issues'))
        self.assert200(response)
        self.assertEqual(len(response.json), len(issues))
Example #4
0
    def test_my_org_datasets(self):
        user = self.login()
        member = Member(user=user, role='editor')
        organization = OrganizationFactory(members=[member])
        community_resources = [
            VisibleDatasetFactory(owner=user) for _ in range(2)
        ]
        org_datasets = [
            VisibleDatasetFactory(organization=organization) for _ in range(2)
        ]

        response = self.get(url_for('api.my_org_datasets'))
        self.assert200(response)
        self.assertEqual(len(response.json),
                         len(community_resources) + len(org_datasets))
 def test_with_other_datasets_and_pertinent_ones(self):
     user = self.login()
     with self.autoindex():
         member = Member(user=user, role='admin')
         organization = OrganizationFactory(members=[member])
         for _ in range(3):
             VisibleDatasetFactory(
                 organization=organization,
                 spatial=SpatialCoverageFactory(zones=[self.arles.id]))
     response = self.client.get(
         url_for('territories.territory', territory=self.arles))
     self.assert200(response)
     data = response.data.decode('utf-8')
     self.assertIn('Arles', data)
     territory_datasets = self.get_context_variable('territory_datasets')
     self.assertEqual(len(territory_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.assertTrue(self.get_context_variable('has_pertinent_datasets'))
     self.assertEqual(self.get_context_variable('town_datasets'), [])
     self.assertIn('Some of your datasets have an exact match!', data)
Example #6
0
    def test_my_org_datasets_with_search(self):
        user = self.login()
        member = Member(user=user, role='editor')
        organization = OrganizationFactory(members=[member])
        datasets = [
            VisibleDatasetFactory(owner=user, title='foo'),
        ]
        org_datasets = [
            VisibleDatasetFactory(organization=organization, title='foo'),
        ]

        # Should not be listed.
        VisibleDatasetFactory(owner=user)
        VisibleDatasetFactory(organization=organization)

        response = self.get(url_for('api.my_org_datasets'), qs={'q': 'foo'})
        self.assert200(response)
        self.assertEqual(len(response.json), len(datasets) + len(org_datasets))
Example #7
0
    def test_set_home_datasets(self):
        ids = [d.id for d in VisibleDatasetFactory.create_batch(3)]

        self.login(AdminFactory())
        response = self.put(url_for('api.home_datasets'), ids)

        self.assert200(response)
        self.assertEqual(len(response.json), len(ids))

        site = Site.objects.get(id=self.app.config['SITE_ID'])

        self.assertEqual([d.id for d in site.settings.home_datasets], ids)
Example #8
0
    def test_get_home_datasets(self):
        site = SiteFactory.create(
            id=self.app.config['SITE_ID'],
            settings__home_datasets=VisibleDatasetFactory.create_batch(3)
        )
        current_site.reload()

        self.login(AdminFactory())
        response = self.get(url_for('api.home_datasets'))
        self.assert200(response)

        self.assertEqual(len(response.json), len(site.settings.home_datasets))
    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'])
    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)
 def test_with_other_datasets_logged_in(self):
     self.login()
     with self.autoindex():
         organization = OrganizationFactory()
         for _ in range(3):
             VisibleDatasetFactory(
                 organization=organization,
                 spatial=SpatialCoverageFactory(zones=[self.arles.id]))
     response = self.client.get(
         url_for('territories.territory', territory=self.arles))
     self.assert200(response)
     data = response.data.decode('utf-8')
     territory_datasets = self.get_context_variable('territory_datasets')
     self.assertEqual(len(territory_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('town_datasets'), [])
     self.assertIn('If you want your datasets to appear in that list', data)
Example #12
0
    def test_count(self):
        for i in range(1, 4):
            VisibleDatasetFactory(tags=['tag-{0}'.format(j) for j in range(i)])
            VisibleReuseFactory(tags=['tag-{0}'.format(j) for j in range(i)])

        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)
Example #13
0
    def test_count(self):
        for i in range(1, 4):
            # Tags should be slugified 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)
Example #14
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)
            self.assertJsonEqual(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)
 def test_with_other_datasets(self):
     with self.autoindex():
         organization = OrganizationFactory()
         for _ in range(3):
             VisibleDatasetFactory(
                 organization=organization,
                 spatial=SpatialCoverageFactory(zones=[self.arles.id]))
     response = self.client.get(
         url_for('territories.territory', territory=self.arles))
     self.assert200(response)
     data = response.data.decode('utf-8')
     self.assertIn('Arles', data)
     territory_datasets = self.get_context_variable('territory_datasets')
     self.assertEqual(len(territory_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.assertFalse(self.get_context_variable('has_pertinent_datasets'))
     self.assertEqual(self.get_context_variable('town_datasets'), [])
     self.assertIn('You want to add your own datasets to that list?', data)
Example #16
0
 def test_render_with_data(self):
     for i in range(3):
         badge = Badge(kind=OPENFIELD16)
         VisibleDatasetFactory(badges=[badge])
     response = self.client.get(url_for('gouvfr.openfield16'))
     self.assert200(response)