Exemplo n.º 1
0
class LoadPOITest(TestCase):
    def setUp(self):
        self.cmd = Command()
        self.filename = os.path.join(os.path.dirname(__file__), 'data',
                                     'poi.shp')
        self.path = PathFactory.create()

    def test_command_fails_if_no_arg(self):
        self.assertRaises(CommandError, self.cmd.execute)

    def test_command_fails_if_too_many_args(self):
        self.assertRaises(CommandError, self.cmd.execute, 'a', 'b')

    def test_command_fails_if_filename_missing(self):
        self.assertRaises(CommandError, self.cmd.execute, 'toto.shp')

    def test_command_shows_number_of_objects(self):
        output = StringIO()
        self.cmd.execute(self.filename, stdout=output)
        self.assertIn('2 objects found', output.getvalue())

    def test_create_pois_is_executed(self):
        with patch.object(Command, 'create_poi') as mocked:
            self.cmd.execute(self.filename)
            self.assertEquals(mocked.call_count, 2)

    def test_create_pois_receives_geometries(self):
        geom1 = GEOSGeometry('POINT(-1.36308670782119 -5.98358469800134696)')
        geom2 = GEOSGeometry('POINT(-1.363087202331107 -5.98358423531846917)')
        with patch.object(Command, 'create_poi') as mocked:
            self.cmd.execute(self.filename)
            call1 = mocked.call_args_list[0][0]
            call2 = mocked.call_args_list[1][0]
            self.assertEquals(call1[0], geom1)
            self.assertEquals(call2[0], geom2)

    def test_create_pois_receives_fields_names_and_types(self):
        with patch.object(Command, 'create_poi') as mocked:
            self.cmd.execute(self.filename)
            call1 = mocked.call_args_list[0][0]
            call2 = mocked.call_args_list[1][0]
            self.assertEquals(call1[1], 'pont')
            self.assertEquals(call2[1], 'pancarte 1')
            self.assertEquals(call1[2], u'équipement')
            self.assertEquals(call2[2], 'signaletique')

    def test_create_pois_receives_null_if_field_missing(self):
        self.cmd.field_name = 'name2'
        with patch.object(Command, 'create_poi') as mocked:
            self.cmd.execute(self.filename)
            call1 = mocked.call_args_list[0][0]
            self.assertEquals(call1[1], None)

    def test_pois_are_created(self):
        geom = GEOSGeometry('POINT(1 1)')
        before = len(POI.objects.all())
        self.cmd.create_poi(geom, 'bridge', 'infra')
        after = len(POI.objects.all())
        self.assertEquals(after - before, 1)

    def test_pois_are_attached_to_paths(self):
        geom = GEOSGeometry('POINT(1 1)')
        poi = self.cmd.create_poi(geom, 'bridge', 'infra')
        self.assertEquals([self.path], list(poi.paths.all()))
Exemplo n.º 2
0
class LoadPOITest(TestCase):
    def setUp(self):
        self.cmd = Command()
        self.filename = os.path.join(os.path.dirname(__file__), 'data',
                                     'poi.shp')
        self.path = PathFactory.create()

    def test_command_shows_number_of_objects(self):
        output = StringIO()
        call_command('loadpoi',
                     self.filename,
                     verbosity=2,
                     name_field='name',
                     type_field='type',
                     stdout=output)
        self.assertIn('2 objects found', output.getvalue())

    def test_command_fail_type_missing(self):
        output = StringIO()
        call_command('loadpoi',
                     self.filename,
                     verbosity=1,
                     name_field='name',
                     stdout=output)
        self.assertIn('Set it with --type-field, or set a default',
                      output.getvalue())

    def test_command_fail_name_missing(self):
        output = StringIO()
        call_command('loadpoi',
                     self.filename,
                     verbosity=1,
                     type_field='type',
                     stdout=output)
        self.assertIn('Set it with --name-field, or set a default',
                      output.getvalue())

    @mock.patch(
        'geotrek.trekking.management.commands.loadpoi.Command.create_poi')
    def test_command_fail_rollback(self, mocke):
        mocke.side_effect = Exception('This is a test')
        output = StringIO()
        with self.assertRaises(Exception):
            call_command('loadpoi',
                         self.filename,
                         verbosity=1,
                         name_field='name',
                         type_field='type',
                         stdout=output)
        self.assertIn('An error occured, rolling back operations.',
                      output.getvalue())
        self.assertEqual(POI.objects.count(), 0)

    def test_create_pois_is_executed(self):
        with patch.object(Command, 'create_poi') as mocked:
            self.cmd.handle(point_layer=self.filename,
                            verbosity=0,
                            name_field='name',
                            type_field='type',
                            encoding='utf-8')
            self.assertEqual(mocked.call_count, 2)

    def test_create_pois_receives_geometries(self):
        with patch.object(Command, 'create_poi') as mocked:
            self.cmd.handle(point_layer=self.filename,
                            verbosity=0,
                            name_field='name',
                            type_field='type',
                            encoding='utf-8')
            call1 = mocked.call_args_list[0][0]
            call2 = mocked.call_args_list[1][0]
            self.assertAlmostEqual(call1[0].x, -1.3630867, places=7)
            self.assertAlmostEqual(call1[0].y, -5.9835847, places=7)
            self.assertAlmostEqual(call2[0].x, -1.3630872, places=7)
            self.assertAlmostEqual(call2[0].y, -5.9835842, places=7)
            self.assertEqual(call1[0].geom_type, 'Point')

    def test_create_pois_receives_fields_names_and_types(self):
        with patch.object(Command, 'create_poi') as mocked:
            self.cmd.handle(point_layer=self.filename,
                            verbosity=0,
                            name_field='name',
                            type_field='type',
                            encoding='utf-8')
            call1 = mocked.call_args_list[0][0]
            call2 = mocked.call_args_list[1][0]
            self.assertEqual(call1[1], 'pont')
            self.assertEqual(call2[1], 'pancarte 1')
            self.assertEqual(call1[2], 'équipement')
            self.assertEqual(call2[2], 'signaletique')

    def test_create_pois_name_default_if_field_missing(self):
        with patch.object(Command, 'create_poi') as mocked:
            self.cmd.handle(point_layer=self.filename,
                            verbosity=0,
                            name_default='test',
                            type_field='type',
                            encoding='utf-8')
            call1 = mocked.call_args_list[0][0]
            self.assertEqual(call1[1], 'test')

    def test_pois_are_created(self):
        geom = GEOSGeometry('POINT(1 1)', srid=4326)
        before = len(POI.objects.all())
        self.cmd.create_poi(geom, 'bridge', 'infra', 'description')
        after = len(POI.objects.all())
        self.assertEqual(after - before, 1)

    @skipIf(not settings.TREKKING_TOPOLOGY_ENABLED,
            'Test with dynamic segmentation only')
    def test_pois_are_attached_to_paths(self):
        geom = GEOSGeometry('POINT(1 1)', srid=4326)
        poi = self.cmd.create_poi(geom, 'bridge', 'infra', 'description')
        self.assertEqual([self.path], list(poi.paths.all()))
Exemplo n.º 3
0
class LoadPOITest(TestCase):
    def setUp(self):
        self.cmd = Command()
        self.filename = os.path.join(os.path.dirname(__file__),
                                     'data', 'poi.shp')
        self.path = PathFactory.create()

    def test_command_fails_if_no_arg(self):
        self.assertRaises(CommandError, self.cmd.execute)

    def test_command_fails_if_too_many_args(self):
        self.assertRaises(CommandError, self.cmd.execute, 'a', 'b')

    def test_command_fails_if_filename_missing(self):
        self.assertRaises(CommandError, self.cmd.execute, 'toto.shp')

    def test_command_shows_number_of_objects(self):
        output = StringIO()
        self.cmd.execute(self.filename, stdout=output)
        self.assertIn('2 objects found', output.getvalue())

    def test_create_pois_is_executed(self):
        with patch.object(Command, 'create_poi') as mocked:
            self.cmd.execute(self.filename)
            self.assertEquals(mocked.call_count, 2)

    def test_create_pois_receives_geometries(self):
        geom1 = GEOSGeometry('POINT(-1.36308670782119 -5.98358469800134696)')
        geom2 = GEOSGeometry('POINT(-1.363087202331107 -5.98358423531846917)')
        with patch.object(Command, 'create_poi') as mocked:
            self.cmd.execute(self.filename)
            call1 = mocked.call_args_list[0][0]
            call2 = mocked.call_args_list[1][0]
            self.assertEquals(call1[0], geom1)
            self.assertEquals(call2[0], geom2)

    def test_create_pois_receives_fields_names_and_types(self):
        with patch.object(Command, 'create_poi') as mocked:
            self.cmd.execute(self.filename)
            call1 = mocked.call_args_list[0][0]
            call2 = mocked.call_args_list[1][0]
            self.assertEquals(call1[1], 'pont')
            self.assertEquals(call2[1], 'pancarte 1')
            self.assertEquals(call1[2], u'équipement')
            self.assertEquals(call2[2], 'signaletique')

    def test_create_pois_receives_null_if_field_missing(self):
        self.cmd.field_name = 'name2'
        with patch.object(Command, 'create_poi') as mocked:
            self.cmd.execute(self.filename)
            call1 = mocked.call_args_list[0][0]
            self.assertEquals(call1[1], None)

    def test_pois_are_created(self):
        geom = GEOSGeometry('POINT(1 1)')
        before = len(POI.objects.all())
        self.cmd.create_poi(geom, 'bridge', 'infra')
        after = len(POI.objects.all())
        self.assertEquals(after - before, 1)

    def test_pois_are_attached_to_paths(self):
        geom = GEOSGeometry('POINT(1 1)')
        poi = self.cmd.create_poi(geom, 'bridge', 'infra')
        self.assertEquals([self.path], list(poi.paths.all()))
Exemplo n.º 4
0
class LoadPOITest(TestCase):
    def setUp(self):
        self.cmd = Command()
        self.filename = os.path.join(os.path.dirname(__file__), 'data',
                                     'poi.shp')
        self.path = PathFactory.create()

    def test_command_fails_if_no_arg(self):
        self.assertRaises(CommandError, call_command, 'loadpoi')

    def test_command_fails_if_too_many_args(self):
        self.assertRaises(CommandError, call_command, 'loadpoi', 'a', 'b')

    def test_command_fails_if_filename_missing(self):
        self.assertRaises(CommandError, call_command, 'loadpoi', 'toto.shp')

    def test_command_shows_number_of_objects(self):
        output = StringIO()
        call_command('loadpoi', self.filename, verbosity=1, stdout=output)
        self.assertIn('2 objects found', output.getvalue())

    def test_create_pois_is_executed(self):
        with patch.object(Command, 'create_poi') as mocked:
            self.cmd.handle(point_layer=self.filename, verbosity=0)
            self.assertEqual(mocked.call_count, 2)

    def test_create_pois_receives_geometries(self):
        geom1 = b'POINT (-1.3630867 -5.9835847)'
        geom2 = b'POINT (-1.3630872 -5.9835842)'
        with patch.object(Command, 'create_poi') as mocked:
            self.cmd.handle(point_layer=self.filename, verbosity=0)
            call1 = mocked.call_args_list[0][0]
            call2 = mocked.call_args_list[1][0]
            self.assertEqual(WKTWriter(precision=7).write(call1[0]), geom1)
            self.assertEqual(WKTWriter(precision=7).write(call2[0]), geom2)

    def test_create_pois_receives_fields_names_and_types(self):
        with patch.object(Command, 'create_poi') as mocked:
            self.cmd.handle(point_layer=self.filename, verbosity=0)
            call1 = mocked.call_args_list[0][0]
            call2 = mocked.call_args_list[1][0]
            self.assertEqual(call1[1], 'pont')
            self.assertEqual(call2[1], 'pancarte 1')
            self.assertEqual(call1[2], 'équipement')
            self.assertEqual(call2[2], 'signaletique')

    def test_create_pois_receives_null_if_field_missing(self):
        self.cmd.field_name = 'name2'
        with patch.object(Command, 'create_poi') as mocked:
            self.cmd.handle(point_layer=self.filename, verbosity=0)
            call1 = mocked.call_args_list[0][0]
            self.assertEqual(call1[1], None)

    def test_pois_are_created(self):
        geom = GEOSGeometry('POINT(1 1)')
        before = len(POI.objects.all())
        self.cmd.create_poi(geom, 'bridge', 'infra')
        after = len(POI.objects.all())
        self.assertEqual(after - before, 1)

    @skipIf(not settings.TREKKING_TOPOLOGY_ENABLED,
            'Test with dynamic segmentation only')
    def test_pois_are_attached_to_paths(self):
        geom = GEOSGeometry('POINT(1 1)')
        poi = self.cmd.create_poi(geom, 'bridge', 'infra')
        self.assertEqual([self.path], list(poi.paths.all()))