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_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_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_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_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_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_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 get_good_data(self): return { 'structure': StructureFactory.create().pk, 'name_en': 'test', 'category': TouristicContentCategoryFactory.create().pk, 'geom': '{"type": "Point", "coordinates":[0, 0]}', }
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 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_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_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_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 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 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_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_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_load_signage(self): StructureFactory.create(name='structure') filename = os.path.join(os.path.dirname(__file__), 'data', 'signage.shp') signage = SignageFactory(name="name", implantation_year=2010) call_command('loadinfrastructure', filename, '--signage', type_default='label', name_default='name', condition_default='condition', structure_default='structure', description_default='description', year_default=2010, verbosity=0) 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)
def test_anonymous_action(self): self.client.logout() self.client.post('/touristicevent/add/', data={ 'structure': StructureFactory.create().pk, 'name_en': 'test', 'geom': '{"type": "Point", "coordinates": [0, 0]}', 'model': 'touristicevent', }) self.assertEqual(LogEntry.objects.count(), 0)
def test_load_signage_bad_multipoints_error(self): output = StringIO() StructureFactory.create(name='structure') SignageFactory(name="name", implantation_year=2010) filename = os.path.join(os.path.dirname(__file__), 'data', 'signage_bad_multipoint.geojson') with self.assertRaises(CommandError) as e: 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.assertEqual( 'One of your geometry is a MultiPoint object with multiple points', e.exception.message)
def get_good_data(self): return { 'structure': StructureFactory.create().pk, 'name_en': 'test en', 'name_fr': 'test fr', 'geom': '{"type": "GeometryCollection", "geometries": [{"type": "Point", "coordinates":[0, 0]}]}', }
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_creation_form_line(self): path = PathFactory.create(geom=LineString(Point(700000, 6600000), Point(700300, 6600300), srid=settings.SRID)) self.super_user = SuperUserFactory.create(username='******', password='******') self.client.login(username='******', password='******') data = self.get_good_data() data['structure'] = StructureFactory.create().pk data['topology'] = '{"paths": [%s], "positions":{"0":[0,1]}}' % path.pk, response = self.client.post('%s' % (Intervention.get_add_url()), data) self.assertEqual(PathAggregation.objects.count(), 1) self.assertEqual(response.status_code, 302) self.assertEqual(Intervention.objects.first().geom, path.geom) self.assertEqual(Intervention.objects.first().target.kind, 'INTERVENTION')
def setUp(self): profile = UserProfileFactory.create(user__username='******', user__password='******') user = profile.user user.user_permissions.add(Permission.objects.get(codename="add_sensitivearea")) user.user_permissions.add(Permission.objects.get(codename="change_sensitivearea")) user.user_permissions.add(Permission.objects.get(codename="delete_sensitivearea")) user.user_permissions.add(Permission.objects.get(codename="read_sensitivearea")) user.user_permissions.add(Permission.objects.get(codename="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_dive_good_multipoints(self): output = StringIO() structure = StructureFactory.create(name='structure') filename = os.path.join(os.path.dirname(__file__), 'data', 'dive_good_multipoint.geojson') call_command('loaddive', filename, name_field='name', depth_field='depth', practice_default='Practice', structure_default='structure', verbosity=2, stdout=output) self.assertIn('Dives will be linked to %s' % structure, output.getvalue()) self.assertIn('1 objects created.', output.getvalue()) value = Dive.objects.get(name='name') self.assertEqual(10, value.depth) self.assertEqual('Practice', value.practice.name) self.assertAlmostEqual(value.geom.x, 402314.30044897617) self.assertAlmostEqual(value.geom.y, 905126.7898456538)
def test_bladetype_can_be_change(self): self.login() change_url = reverse('admin:signage_bladetype_change', args=[self.bladetype.pk]) response = self.client.post(change_url, { 'label': 'coucou', 'structure': StructureFactory.create().pk }) self.assertEqual(response.status_code, 302) self.assertEqual( BladeType.objects.get(pk=self.bladetype.pk).label, 'coucou') self.assertEqual(response.url, '/admin/signage/bladetype/')
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)
def test_update_view_logs_change(self): obj = TouristicEventFactory.create() self.client.post('/touristicevent/edit/{0}/'.format(obj.pk), data={ 'structure': StructureFactory.create().pk, 'name_en': 'test', 'geom': '{"type": "Point", "coordinates": [0, 0]}', 'model': 'touristicevent', }) self.assertEqual(LogEntry.objects.count(), 1) entry = LogEntry.objects.get() self.assertEqual(entry.get_edited_object(), obj) self.assertEqual(entry.action_flag, CHANGE) self.assertEqual(entry.user, self.user)