def test_wrong_fields_fail(self): StructureFactory.create(name='structure') filename = os.path.join(os.path.dirname(__file__), 'data', 'signage.shp') output = StringIO() call_command('loadsignage', filename, type_field='wrong_type_field', stdout=output) call_command('loadsignage', filename, type_default='label', name_field='wrong_name_field', stdout=output) call_command('loadsignage', filename, type_default='label', name_field='name', condition_field='wrong_condition_field', stdout=output) call_command('loadsignage', filename, type_default='label', name_field='name', description_field='wrong_description_field', stdout=output) call_command('loadsignage', filename, type_default='label', name_field='name', year_field='wrong_implantation_year_field', stdout=output) call_command('loadsignage', filename, type_default='label', name_field='name', structure_field='wrong_structure_field', stdout=output) call_command('loadsignage', filename, type_default='label', name_field='name', eid_field='wrong_eid_field', stdout=output) elements_to_check = ['wrong_type_field', 'wrong_name_field', 'wrong_condition_field', 'wrong_description_field', 'wrong_implantation_year_field', 'wrong_structure_field', 'wrong_eid_field'] self.assertEqual(output.getvalue().count("set a default value"), 2) self.assertEqual(output.getvalue().count("Change your"), 5) self.assertEqual(output.getvalue().count("set a default value"), 2) for element in elements_to_check: self.assertIn("Field '{}' not found in data source".format(element), output.getvalue())
def test_load_dive_bad_multipoints(self): output = StringIO() StructureFactory.create(name='structure') filename = os.path.join(os.path.dirname(__file__), 'data', 'dive_bad_multipoint.geojson') with self.assertRaisesRegexp(CommandError, 'One of your geometry is a MultiPoint object with multiple points'): call_command('loaddive', filename, name_field='name', depth_field='depth', practice_default='Practice', structure_default='structure', verbosity=2, stdout=output)
def test_structurerelated_filter(self): def test_structure(structure, stake): user = self.userfactory(password='******') p = user.profile p.structure = structure p.save() success = self.client.login(username=user.username, password='******') self.assertTrue(success) response = self.client.get(Path.get_add_url()) self.assertEqual(response.status_code, 200) self.assertTrue('form' in response.context) form = response.context['form'] self.assertTrue('stake' in form.fields) stakefield = form.fields['stake'] self.assertTrue((stake.pk, unicode(stake)) in stakefield.choices) self.client.logout() # Test for two structures s1 = StructureFactory.create() s2 = StructureFactory.create() st1 = StakeFactory.create(structure=s1) StakeFactory.create(structure=s1) st2 = StakeFactory.create(structure=s2) StakeFactory.create(structure=s2) test_structure(s1, st1) test_structure(s2, st2)
def test_wrong_fields_fail(self): StructureFactory.create(name='structure') filename = os.path.join(os.path.dirname(__file__), 'data', 'dive.shp') output = StringIO() call_command('loaddive', filename, name_field='wrong_name_field', structure_default='structure', stdout=output) call_command('loaddive', filename, name_field='name', depth_field='wrong_depth_field', structure_default='structure', stdout=output) call_command('loaddive', filename, name_field='name', depth_field='depth', eid_field='wrong_eid_field', structure_default='structure', stdout=output) elements_to_check = [ 'wrong_name_field', 'wrong_depth_field', 'wrong_eid_field' ] self.assertEqual(output.getvalue().count("Set it with"), 2) self.assertEqual( output.getvalue().count("Change your --eid-field option"), 1) for element in elements_to_check: self.assertIn( "Field '{}' not found in data source".format(element), output.getvalue())
def test_load_dive_wrong_structure_default(self): output = StringIO() StructureFactory.create(name='structure') filename = os.path.join(os.path.dirname(__file__), 'data', 'dive.shp') DiveFactory.create(name='name', eid='eid1', depth=10) call_command('loaddive', filename, name_field='name', depth_field='depth', practice_default='Practice', structure_default='wrong_structure_default', verbosity=2, stdout=output) self.assertIn("Structure wrong_structure_default set in options doesn't exist", output.getvalue())
def test_load_wrong_type_geom(self): output = StringIO() StructureFactory.create(name='structure') filename = os.path.join(os.path.dirname(__file__), 'data', 'line.geojson') with self.assertRaises(GEOSException): call_command('loaddive', filename, name_field='name', depth_field='depth', practice_default='Practice', structure_default='structure', verbosity=2, stdout=output) self.assertIn('An error occured, rolling back operations.', output.getvalue()) self.assertEqual(Dive.objects.count(), 0)
def test_line_fail_rolling_back(self): StructureFactory.create(name='structure') filename = os.path.join(os.path.dirname(__file__), 'data', 'line.geojson') output = StringIO() with self.assertRaises(IndexError): call_command('loadsignage', filename, type_default='label', name_default='name', stdout=output) self.assertIn('An error occured, rolling back operations.', output.getvalue()) self.assertEqual(Signage.objects.count(), 0)
def test_missing_defaults(self): StructureFactory.create(name='structure') filename = os.path.join(os.path.dirname(__file__), 'data', 'signage.shp') output = StringIO() call_command('loadinfrastructure', filename, stdout=output) call_command('loadinfrastructure', filename, type_default='label', stdout=output) elements_to_check = ['type', 'name'] self.assertEqual(output.getvalue().count("Field 'None' not found in data source."), 2) for element in elements_to_check: self.assertIn("Set it with --{0}-field, or set a default value with --{0}-default".format(element), output.getvalue())
def test_structure_is_not_changed_with_permission_error(self): self.login() perm = Permission.objects.get(codename='can_bypass_structure') self.user.user_permissions.add(perm) structure = StructureFactory() structure_2 = StructureFactory() self.assertNotEqual(structure, self.user.profile.structure) obj = self.modelfactory.create(structure=structure) data = self.get_good_data() data['structure'] = structure_2.pk result = self.client.post(obj.get_update_url(), data) self.assertEqual(result.status_code, 200) self.assertIn("Please select a choice related to all structures", result.content) self.logout()
def test_command_unset_structure(self): structure1 = StructureFactory.create(name="coucou") structure2 = StructureFactory.create(name="coco") infratype1 = InfrastructureTypeFactory.create(label="annyeong", structure=structure1, pictogram=None) infratype2 = InfrastructureTypeFactory.create(label="annyeong", structure=structure2, pictogram=None) path = PathFactory.create(name="pass") usage1 = UsageFactory.create(usage="hello", structure=structure1) usage2 = UsageFactory.create(usage="hello", structure=structure2) path.usages.add(usage1) path.usages.add(usage2) infrastructure1 = InfrastructureFactory.create(name='pissenlit', type=infratype1) infrastructure2 = InfrastructureFactory.create(name='rhododendron', type=infratype2) self.assertEqual(InfrastructureType.objects.count(), 2) self.assertEqual(Usage.objects.count(), 2) self.assertEqual(infrastructure1.type.label, 'annyeong') self.assertEqual(infrastructure1.type.structure.name, 'coucou') self.assertEqual(infrastructure2.type.label, 'annyeong') self.assertEqual(infrastructure2.type.structure.name, 'coco') self.assertEqual(path.usages.count(), 2) self.assertEqual(usage1.structure.name, 'coucou') self.assertEqual(usage2.structure.name, 'coco') output = StringIO() call_command('unset_structure', '--all', verbosity=2, stdout=output) response = output.getvalue() self.assertIn("Create hello", response) self.assertEqual(InfrastructureType.objects.count(), 1) self.assertEqual(Usage.objects.count(), 1) infra = Infrastructure.objects.first() self.assertEqual(infra.type.label, 'annyeong') self.assertEqual(infra.type.structure, None) path_usages = Path.objects.first().usages.first() self.assertEqual(path_usages.usage, 'hello') self.assertEqual(path_usages.structure, None)
def test_structure_is_not_changed_with_permission_error(self): self.login() perm = Permission.objects.get(codename='can_bypass_structure') self.user.user_permissions.add(perm) structure = StructureFactory() structure_2 = StructureFactory() source = PathSource.objects.create(source="Source_1", structure=structure) self.assertNotEqual(structure, self.user.profile.structure) obj = self.modelfactory.create(structure=structure) data = self.get_good_data().copy() data['source'] = source.pk data['structure'] = structure_2.pk response = self.client.post(obj.get_update_url(), data) self.assertContains(response, "Please select a choice related to all structures") self.logout()
def test_load_signage_with_fields(self): output = StringIO() structure = StructureFactory.create(name='structure') filename = os.path.join(os.path.dirname(__file__), 'data', 'signage.shp') SignageFactory(name="name") call_command('loadsignage', filename, type_field='label', name_field='name', condition_field='condition', structure_default='structure', description_field='descriptio', year_field='year', verbosity=1, stdout=output) self.assertIn('Signages will be linked to %s' % structure, output.getvalue()) self.assertIn("SignageType 'type' created", output.getvalue()) self.assertIn("Condition Type 'condition' created", output.getvalue()) value = Signage.objects.all() names = [val.name for val in value] years = [val.implantation_year for val in value] self.assertIn('coucou', names) self.assertIn(2010, years) self.assertIn(2012, years) self.assertEquals(value.count(), 3)
def test_filters_structure(self): other_structure = StructureFactory.create(name='other') self.sensitivearea_other_structure = SensitiveAreaFactory.create(structure=other_structure) url = '/api/v2/sensitivearea/?format=json&language=en&period=ignore&structures={}'.format(other_structure.pk) response = self.client.get(url) self.assertEqual(response.json()['count'], 1) self.assertEqual(response.json()['results'][0]['name'], self.sensitivearea_other_structure.species.name)
def test_load_infrastructure(self): output = StringIO() structure = StructureFactory.create(name='structure') filename = os.path.join(os.path.dirname(__file__), 'data', 'infrastructure.shp') call_command('loadinfrastructure', filename, type_default='label', name_default='name', condition_default='condition', structure_default='structure', description_default='description', year_default=2010, verbosity=2, stdout=output) self.assertIn('Infrastructures will be linked to %s' % structure, output.getvalue()) self.assertIn('2 objects created.', output.getvalue()) value = Infrastructure.objects.filter(name='name').order_by('pk') self.assertEqual(2010, value[0].implantation_year) self.assertEqual(value.count(), 2) self.assertAlmostEqual(value[0].geom.x, -436345.704831, places=5) self.assertAlmostEqual(value[0].geom.y, 1176487.742917, places=5) self.assertAlmostEqual(value[1].geom.x, -436345.505347, places=5) self.assertAlmostEqual(value[1].geom.y, 1176480.918334, places=5)
def test_load_infrastructure_with_fields_use_structure(self): output = StringIO() structure = StructureFactory.create(name='structure') filename = os.path.join(os.path.dirname(__file__), 'data', 'infrastructure.shp') call_command('loadinfrastructure', filename, type_field='label', name_field='name', condition_field='condition', structure_default='structure', use_structure=True, description_field='descriptio', year_field='year', verbosity=1, stdout=output) self.assertIn('Infrastructures will be linked to %s' % structure, output.getvalue()) self.assertIn("InfrastructureType 'type (%s)' created" % structure, output.getvalue()) self.assertIn("Condition Type 'condition (%s)' created" % structure, output.getvalue()) value = Infrastructure.objects.all() names = [val.name for val in value] years = [val.implantation_year for val in value] self.assertIn('coucou', names) self.assertIn(2010, years) self.assertIn(2012, years) self.assertEqual(value.count(), 2)
def test_load_infrastructure(self): output = StringIO() structure = StructureFactory.create(name='structure') filename = os.path.join(os.path.dirname(__file__), 'data', 'signage.shp') building = InfrastructureFactory(name="name", implantation_year=2010) call_command('loadinfrastructure', filename, '--infrastructure', type_default='label', name_default='name', condition_default='condition', structure_default='structure', description_default='description', year_default=2010, verbosity=2, stdout=output) self.assertIn('Infrastructures will be linked to %s' % structure, output.getvalue()) self.assertIn('2 objects created.', output.getvalue()) value = Infrastructure.objects.all() self.assertEquals(building.name, value[1].name) self.assertEquals(building.implantation_year, value[1].implantation_year) self.assertEquals(value.count(), 3)
def test_unset_structure_without_structure_keep_pk(self): structure = StructureFactory.create(name="Test") infratype_no_structure = InfrastructureTypeFactory.create( label="type1", structure=None, pictogram=None) InfrastructureFactory.create(name='infra1', type=infratype_no_structure) infratype_structure = InfrastructureTypeFactory.create( label="type2", structure=structure, pictogram=None) InfrastructureFactory.create(name='infra2', type=infratype_structure) self.assertEqual(InfrastructureType.objects.count(), 2) self.assertIsNone(infratype_no_structure.structure) self.assertIsNotNone(infratype_structure.structure) old_pk_no_structure = infratype_no_structure.pk old_pk_structure = infratype_structure.pk call_command('unset_structure', '--all', verbosity=0) type1 = InfrastructureType.objects.get(label="type1") type2 = InfrastructureType.objects.get(label="type1") new_pk_no_structure = type1.pk new_pk_structure = type2.pk self.assertEqual(old_pk_no_structure, new_pk_no_structure) self.assertNotEqual(old_pk_structure, new_pk_structure) self.assertIsNone(type1.structure) self.assertIsNone(type2.structure)
def get_good_data(self): return { 'structure': StructureFactory.create().pk, 'name_en': 'test', 'category': TouristicContentCategoryFactory.create().pk, 'geom': '{"type": "Point", "coordinates":[0, 0]}', }
def test_load_dive_eid(self): output = StringIO() structure = StructureFactory.create(name='structure') filename = os.path.join(os.path.dirname(__file__), 'data', 'dive.shp') DiveFactory.create(name='name', eid='eid1', depth=10) call_command('loaddive', filename, name_field='name', depth_field='depth', eid_field='eid', practice_default='Practice', structure_default='structure', verbosity=2, stdout=output) self.assertIn('Dives will be linked to %s' % structure, output.getvalue()) self.assertIn('2 objects created.', output.getvalue()) value = Dive.objects.filter(name='name') self.assertEqual( 5, value[0].depth ) # The dive was updated because has the same eid (eid1) self.assertEqual('Practice', value[0].practice.name) self.assertEqual(value.count(), 1) self.assertEqual(Dive.objects.count(), 2) self.assertAlmostEqual(value[0].geom.x, -436345.704831, places=5) self.assertAlmostEqual(value[0].geom.y, 1176487.742917, places=5)
def test_signage_type_change_not_same_structure(self): self.login() structure = StructureFactory(name="Other") signagetype = SignageTypeFactory.create(structure=structure) change_url = reverse('admin:signage_signagetype_change', args=[signagetype.pk]) response = self.client.get(change_url) self.assertEqual(response.status_code, 200) self.assertContains(response, '<select name="structure" id="id_structure">')
def test_structure_is_not_changed_without_permission(self): self.login() structure = StructureFactory() self.assertNotEqual(structure, self.user.profile.structure) self.assertFalse(self.user.has_perm('authent.can_bypass_structure')) obj = self.modelfactory.create(structure=structure) self.client.post(obj.get_update_url(), self.get_good_data()) self.assertEqual(obj.structure, structure)
def test_merge_works_wrong_structure(self): self.login() other_structure = StructureFactory(name="Other") p1 = PathFactory.create(name="AB", geom=LineString((0, 0), (1, 0))) p2 = PathFactory.create(name="BC", geom=LineString((1, 0), (2, 0)), structure=other_structure) response = self.client.post(reverse('core:merge_path'), {'path[]': [p1.pk, p2.pk]}) self.assertEqual({'error': "You don't have the right to change these paths"}, response.json()) self.logout()
def setUp(self): profile = UserProfileFactory.create(user__username='******', user__password='******') user = profile.user user.groups.add(Group.objects.get(name=u"Référents communication")) self.client.login(username=user.username, password='******') self.content1 = POIFactory.create() structure = StructureFactory.create() self.content2 = POIFactory.create(structure=structure)
def test_structure_is_not_changed(self): if not hasattr(self.model, 'structure'): return self.login() structure = StructureFactory() self.assertNotEqual(structure, self.user.profile.structure) obj = self.modelfactory.create(structure=structure) self.client.post(obj.get_update_url(), self.get_good_data()) self.assertEqual(obj.structure, structure)
def setUp(self): profile = UserProfileFactory.create(user__username='******', user__password='******') user = profile.user user.groups.add(Group.objects.get(name=u"Référents communication")) self.client.login(username=user.username, password='******') self.event1 = TouristicEventFactory.create() structure = StructureFactory.create() self.event2 = TouristicEventFactory.create(structure=structure)
def test_sealing_change_not_same_structure(self): self.login() structure = StructureFactory(name="Other") sealing = SealingFactory.create(structure=structure) change_url = reverse('admin:signage_sealing_change', args=[sealing.pk]) response = self.client.get(change_url) self.assertEquals(response.status_code, 200) self.assertIn('<select name="structure" id="id_structure">', response.content)
def test_load_infrastructure_bad_multipoints_error(self): output = StringIO() StructureFactory.create(name='structure') filename = os.path.join(os.path.dirname(__file__), 'data', 'infrastructure_bad_multipoint.geojson') with self.assertRaisesRegex( CommandError, 'One of your geometry is a MultiPoint object with multiple points' ): call_command('loadinfrastructure', filename, type_default='label', name_default='name', condition_default='condition', structure_default='structure', description_default='description', year_default=2010, verbosity=2, stdout=output)
def test_sealing_change_not_same_structure(self): self.login() structure = StructureFactory(name="Other") sealing = SealingFactory.create(structure=structure) change_url = reverse('admin:signage_sealing_change', args=[sealing.pk]) response = self.client.get(change_url) self.assertEqual(response.status_code, 302) self.assertEqual(Sealing.objects.get(pk=self.sealing.pk).label, self.sealing.label) self.assertEqual(response.url, '/admin/')
def test_bladetype_change_not_same_structure(self): self.login() structure = StructureFactory(name="Other") bladetype = BladeTypeFactory.create(structure=structure) change_url = reverse('admin:signage_bladetype_change', args=[bladetype.pk]) response = self.client.get(change_url) self.assertEqual(response.status_code, 302) self.assertEqual(BladeType.objects.get(pk=self.bladetype.pk).label, self.bladetype.label) self.assertEqual(response.url, '/admin/')
def test_load_dive_no_eid_no_practice_default(self): output = StringIO() structure = StructureFactory.create(name='structure') filename = os.path.join(os.path.dirname(__file__), 'data', 'dive.shp') call_command('loaddive', filename, name_field='name', depth_field='depth', structure_default='structure', verbosity=2, stdout=output) self.assertIn('Dives will be linked to %s' % structure, output.getvalue()) self.assertIn('2 objects created.', output.getvalue()) value = Dive.objects.get(name='name') self.assertIsNone(value.practice)
def test_infrastructuretype_cannot_be_change_not_same_structure(self): self.login() structure = StructureFactory(name="Other") infra = InfrastructureTypeFactory.create(structure=structure) change_url = reverse('admin:infrastructure_infrastructuretype_change', args=[infra.pk]) response = self.client.get(change_url) self.assertEqual(response.status_code, 302) self.assertEqual(InfrastructureType.objects.get(pk=self.infra.pk).label, self.infra.label) self.assertEqual(response.url, '/admin/')
def test_structurerelated_filter_with_none(self): s1 = StructureFactory.create() s2 = StructureFactory.create() st0 = StakeFactory.create(structure=None) st1 = StakeFactory.create(structure=s1) st2 = StakeFactory.create(structure=s2) user = self.userfactory(password='******') p = user.profile p.structure = s1 p.save() success = self.client.login(username=user.username, password='******') self.assertTrue(success) response = self.client.get(Path.get_add_url()) self.assertEqual(response.status_code, 200) self.assertTrue('form' in response.context) form = response.context['form'] self.assertTrue('stake' in form.fields) stakefield = form.fields['stake'] self.assertTrue((st0.pk, unicode(st0)) in stakefield.choices) self.assertTrue((st1.pk, unicode(st1)) in stakefield.choices) self.assertFalse((st2.pk, unicode(st2)) in stakefield.choices)
def setUp(self): profile = UserProfileFactory.create(user__username='******', user__password='******') user = profile.user user.user_permissions.add(Permission.objects.get(codename=u"add_sensitivearea")) user.user_permissions.add(Permission.objects.get(codename=u"change_sensitivearea")) user.user_permissions.add(Permission.objects.get(codename=u"delete_sensitivearea")) user.user_permissions.add(Permission.objects.get(codename=u"read_sensitivearea")) user.user_permissions.add(Permission.objects.get(codename=u"export_sensitivearea")) self.client.login(username=user.username, password='******') self.area1 = SensitiveAreaFactory.create() structure = StructureFactory.create() self.area2 = SensitiveAreaFactory.create(structure=structure)
def test_load_signage(self): output = StringIO() structure = StructureFactory.create(name='structure') filename = os.path.join(os.path.dirname(__file__), 'data', 'signage.shp') signage = SignageFactory(name="name", implantation_year=2010) call_command('loadsignage', filename, type_default='label', name_default='name', condition_default='condition', structure_default='structure', description_default='description', year_default=2010, verbosity=2, stdout=output) self.assertIn('Signages will be linked to %s' % structure, output.getvalue()) self.assertIn('2 objects created.', output.getvalue()) value = Signage.objects.all() self.assertEquals(signage.name, value[1].name) self.assertEquals(signage.implantation_year, value[1].implantation_year) self.assertEquals(value.count(), 3)