def create_geozones_fixtures(): bdr = GeoZoneFactory( id='fr/county/13', level='fr/county', name='Bouches-du-Rhône') arles = GeoZoneFactory( id='fr/town/13004', level='fr/town', parents=[bdr.id], name='Arles', code='13004', population=52439) return bdr, arles
def test_suggest_zones_on_extra_key(self): '''It should suggest zones based on any key''' with self.autoindex(): for i in range(4): GeoZoneFactory(name='in' if i % 2 else 'not-in', keys={ str(i): 'test-{0}'.format(i) if i % 2 else faker.word() }) response = self.get(url_for('api.suggest_zones'), qs={ 'q': 'test', 'size': '5' }) self.assert200(response) self.assertEqual(len(response.json), 2) for suggestion in response.json: self.assertIn('id', suggestion) self.assertIn('name', suggestion) self.assertIn('code', suggestion) self.assertIn('level', suggestion) self.assertIn('keys', suggestion) self.assertIsInstance(suggestion['keys'], dict) self.assertEqual(suggestion['name'], 'in')
def test_initial_values(self): Fake, FakeForm = self.factory() zones = [GeoZoneFactory() for _ in range(3)] fake = Fake(spatial=SpatialCoverage(zones=zones)) form = FakeForm(None, fake) self.assertEqual(form.spatial.zones._value(), ','.join([z.id for z in zones]))
def test_basic(self): arles = GeoZoneFactory(id='fr/town/13004', level='fr/town', name='Arles', code='13004', population=52439) response = self.client.get( url_for('territories.territory', territory=arles)) self.assert404(response) # By default territories are deactivated.
def test_towns_within_sitemap(self): '''It should return the town from the sitemap.''' territory = GeoZoneFactory( id='fr/town/13004', name='Arles', code='13004', level='fr/town') self.get_sitemap_tree() url = self.get_by_url('territories.territory', territory=territory) self.assertIsNotNone(url) self.assert_url(url, 0.5, 'weekly')
def test_with_gouvfr_territory_datasets(self): bdr = GeoZoneFactory( id='fr/county/13', level='fr/county', name='Bouches-du-Rhône') arles = GeoZoneFactory( id='fr/town/13004', level='fr/town', parents=[bdr.id], name='Arles', code='13004', population=52439) response = self.client.get( url_for('territories.territory', territory=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), 10) for dataset in territory_datasets: self.assertIn( '<div data-udata-territory-id="{dataset.slug}"'.format( dataset=dataset), data) self.assertIn(bdr.name, data)
def test_with_initial(self): Fake, FakeForm = self.factory() zones = [GeoZoneFactory() for _ in range(3)] fake = Fake(spatial=SpatialCoverage( zones=zones, granularity=random_spatial_granularity())) zone = GeoZoneFactory() data = MultiDict({ 'spatial-zones': zone.id, 'spatial-granularity': random_spatial_granularity() }) form = FakeForm(data, fake) form.validate() self.assertEqual(form.errors, {}) form.populate_obj(fake) self.assertEqual(len(fake.spatial.zones), 1) self.assertEqual(fake.spatial.zones[0], zone)
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.assertEqual(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_suggest_zones_no_match(self): '''It should not provide zones suggestions if no match''' with self.autoindex(): for i in range(3): GeoZoneFactory(name=5 * '{0}'.format(i), code=3 * '{0}'.format(i)) response = self.get(url_for('api.suggest_zones'), qs={ 'q': 'xxxxxx', 'size': '5' }) self.assert200(response) self.assertEqual(len(response.json), 0)
def test_with_valid_zone(self): Fake, FakeForm = self.factory() zone = GeoZoneFactory() fake = Fake() form = FakeForm( MultiDict({ 'spatial-zones': zone.id, 'spatial-granularity': random_spatial_granularity() })) form.validate() self.assertEqual(form.errors, {}) form.populate_obj(fake) self.assertEqual(len(fake.spatial.zones), 1) self.assertEqual(fake.spatial.zones[0], zone)
def test_with_valid_zone_from_json(self): Fake, FakeForm = self.factory() zone = GeoZoneFactory() fake = Fake() form = FakeForm.from_json({ 'spatial': { 'zones': zone.id, 'granularity': random_spatial_granularity() } }) form.validate() self.assertEqual(form.errors, {}) form.populate_obj(fake) self.assertEqual(len(fake.spatial.zones), 1) self.assertEqual(fake.spatial.zones[0], zone)
def test_oembed_territory_api_get(self): '''It should fetch a territory in the oembed format.''' arles = GeoZoneFactory(id='fr/town/13004', level='fr/town', name='Arles', code='13004', population=52439) licence_ouverte = LicenseFactory(id='fr-lo', title='Licence Ouverte') LicenseFactory(id='notspecified', title='Not Specified') for territory_dataset_class in TERRITORY_DATASETS.values(): organization = OrganizationFactory( id=territory_dataset_class.organization_id) territory = territory_dataset_class(arles) reference = 'territory-{id}'.format(id=territory.slug) response = self.get(url_for('api.oembeds', references=reference)) self.assert200(response) data = json.loads(response.data)[0] self.assertIn('html', data) self.assertIn('width', data) self.assertIn('maxwidth', data) self.assertIn('height', data) self.assertIn('maxheight', data) self.assertTrue(data['type'], 'rich') self.assertTrue(data['version'], '1.0') self.assertIn(territory.title, data['html']) self.assertIn(cgi.escape(territory.url), data['html']) self.assertIn('alt="{name}"'.format(name=organization.name), data['html']) self.assertIn(md(territory.description, source_tooltip=True), data['html']) self.assertIn('Download from localhost', data['html']) self.assertIn('Add to your own website', data['html']) if territory_dataset_class != TERRITORY_DATASETS['comptes']: self.assertIn( 'License: {title}'.format(title=licence_ouverte.title), data['html']) self.assertIn( '© {license_id}'.format(license_id=licence_ouverte.id), data['html']) self.assertIn( '<a data-tooltip="Source" href="http://localhost/datasets', data['html'])
def test_with_valid_zone_ids(self): Fake, FakeForm = self.factory() zones = [GeoZoneFactory() for _ in range(3)] fake = Fake() form = FakeForm( MultiDict({ 'spatial-zones': ','.join([z.id for z in zones]), 'spatial-granularity': random_spatial_granularity() })) form.validate() self.assertEqual(form.errors, {}) form.populate_obj(fake) self.assertEqual(len(fake.spatial.zones), len(zones)) expected_zones = dict((z.id, z) for z in zones) for zone in fake.spatial.zones: self.assertIn(zone.id, expected_zones) self.assertEqual(zone, expected_zones[zone.id])
def test_zones_api_many(self): zones = [GeoZoneFactory() for _ in range(3)] url = url_for('api.zones', ids=zones) response = self.get(url) self.assert200(response) self.assertEqual(len(response.json['features']), len(zones)) for zone, feature in zip(zones, response.json['features']): self.assertEqual(feature['type'], 'Feature') self.assertEqual(feature['geometry'], zone.geom) self.assertEqual(feature['id'], zone.id) properties = feature['properties'] self.assertEqual(properties['name'], zone.name) self.assertEqual(properties['code'], zone.code) self.assertEqual(properties['level'], zone.level) self.assertEqual(properties['parents'], zone.parents) self.assertEqual(properties['population'], zone.population) self.assertEqual(properties['area'], zone.area) self.assertEqual(properties['keys'], zone.keys)
def test_zones_api_one(self): zone = GeoZoneFactory() url = url_for('api.zones', ids=[zone.id]) response = self.get(url) self.assert200(response) self.assertEqual(len(response.json['features']), 1) feature = response.json['features'][0] self.assertEqual(feature['type'], 'Feature') self.assertEqual(feature['geometry'], zone.geom) self.assertEqual(feature['id'], zone.id) properties = feature['properties'] self.assertEqual(properties['name'], zone.name) self.assertEqual(properties['code'], zone.code) self.assertEqual(properties['level'], zone.level) self.assertEqual(properties['parents'], zone.parents) self.assertEqual(properties['population'], zone.population) self.assertEqual(properties['area'], zone.area) self.assertEqual(properties['keys'], zone.keys)
def test_suggest_zones_on_code(self): '''It should suggest zones based on its code''' with self.autoindex(): for i in range(4): GeoZoneFactory(code='test-{0}'.format(i) if i % 2 else faker.word()) response = self.get(url_for('api.suggest_zones'), qs={ 'q': 'test', 'size': '5' }) self.assert200(response) self.assertEqual(len(response.json), 2) for suggestion in response.json: self.assertIn('id', suggestion) self.assertIn('name', suggestion) self.assertIn('code', suggestion) self.assertIn('level', suggestion) self.assertIn('keys', suggestion) self.assertIsInstance(suggestion['keys'], dict) self.assertTrue(suggestion['code'].startswith('test'))