def test_as_dict(self):
        self.assertEqual(BoundarySet(
            slug='foo',
            name='Foo',
            singular='Foe',
            authority='King',
            domain='Fooland',
            source_url='http://example.com/',
            notes='Noted',
            licence_url='http://example.com/licence',
            last_updated=date(2000, 1, 1),
            extent=[0, 0, 1, 1],
            start_date=date(2000, 1, 1),
            end_date=date(2010, 1, 1),
            extra={
                'bar': 'baz',
            },
        ).as_dict(), {
            'related': {
                'boundaries_url': '/boundaries/foo/',
            },
            'name_plural': 'Foo',
            'name_singular': 'Foe',
            'authority': 'King',
            'domain': 'Fooland',
            'source_url': 'http://example.com/',
            'notes': 'Noted',
            'licence_url': 'http://example.com/licence',
            'last_updated': '2000-01-01',
            'extent': [0, 0, 1, 1],
            'start_date': '2000-01-01',
            'end_date': '2010-01-01',
            'extra': {
                'bar': 'baz',
            },
        })

        self.assertEqual(BoundarySet(
            slug='foo',
        ).as_dict(), {
            'related': {
                'boundaries_url': '/boundaries/foo/',
            },
            'name_plural': '',
            'name_singular': '',
            'authority': '',
            'domain': '',
            'source_url': '',
            'notes': '',
            'licence_url': '',
            'last_updated': None,
            'extent': None,
            'start_date': None,
            'end_date': None,
            'extra': {},
        })
    def setUp(self):
        self.definition = definition = Definition({
            'last_updated': date(2000, 1, 1),
            'encoding': 'utf-8',
            'name': 'Districts',
            'name_func': clean_attr('Name'),
            'id_func': attr('ID'),
            'slug_func': attr('Code'),
            'is_valid_func': lambda f: f.get('ID') == '1',
            'label_point_func': lambda f: Point(0, 1),
        })

        self.fields = {
            'Name': 'VALID',
            'ID': '1',
            'Code': '\tFoo—Bar–Baz \r Bzz\n',  # m-dash, n-dash
        }

        self.boundary_set = BoundarySet(
            last_updated=definition['last_updated'],
            name=definition['name'],
            singular=definition['singular'],
        )

        self.feature = Feature(FeatureProxy(self.fields), definition)

        self.other = Feature(FeatureProxy({
            'Name': 'INVALID',
            'ID': 100,
            'Code': 3,
        }), definition, SpatialReference(4269), self.boundary_set)
Example #3
0
    def setUp(self):
        self.definition = definition = Definition({
            'last_updated':
            date(2000, 1, 1),
            'encoding':
            'utf-8',
            'name':
            'Districts',
            'name_func':
            clean_attr('Name'),
            'id_func':
            attr('ID'),
            'slug_func':
            attr('Code'),
            'is_valid_func':
            lambda f: f.get('ID') == '1',
            'label_point_func':
            lambda f: Point(0, 1),
        })

        self.fields = {
            'Name': 'VALID',
            'ID': '1',
            'Code': '\tFoo—Bar–Baz \r Bzz\n',  # m-dash, n-dash
        }

        self.boundary_set = BoundarySet(
            last_updated=definition['last_updated'],
            name=definition['name'],
            singular=definition['singular'],
        )

        self.feature = Feature(FeatureProxy(self.fields), definition)

        self.other = Feature(
            FeatureProxy({
                'Name': 'INVALID',
                'ID': 100,
                'Code': 3,
            }), definition, SpatialReference(4269), self.boundary_set)
 def test_get_dicts(self):
     sets = [
         BoundarySet(name='Foo', slug='foo', domain='Fooland'),
         BoundarySet(name='Bar', slug='bar', domain='Barland'),
     ]
     self.assertEqual(BoundarySet.get_dicts(sets), [
         {
             'url': '/boundary-sets/foo/',
             'related': {
                 'boundaries_url': '/boundaries/foo/',
             },
             'name': 'Foo',
             'domain': 'Fooland',
         },
         {
             'url': '/boundary-sets/bar/',
             'related': {
                 'boundaries_url': '/boundaries/bar/',
             },
             'name': 'Bar',
             'domain': 'Barland',
         },
     ])
 def test_prepare_queryset_for_get_dicts(self):
     geom = GEOSGeometry('MULTIPOLYGON(((0 0,0 5,5 5,0 0)))')
     Boundary.objects.create(
         slug='bar',
         set=BoundarySet(slug='foo'),
         name='Bar',
         set_name='Foo',
         external_id=1,
         shape=geom,
         simple_shape=geom,
     )
     # Coerce the django.contrib.gis.db.models.query.GeoValuesListQuerySet.
     self.assertEqual(list(Boundary.prepare_queryset_for_get_dicts(Boundary.objects)), [
         ('bar', 'foo', 'Bar', 'Foo', '1'),
     ])
 def test___str__(self):
     self.assertEqual(str(BoundarySet(name='Foo Bar')), 'Foo Bar')
class LoadBoundaryTestCase(TestCase):
    definition = Definition({
        'last_updated': date(2000, 1, 1),
        'name': 'Districts',
        'name_func': lambda feature: 'Test',
    })

    boundary_set = BoundarySet(
        last_updated=definition['last_updated'],
        name=definition['name'],
        singular=definition['singular'],
    )

    feature = Feature(FeatureProxy({}), definition, boundary_set=boundary_set)

    def test_no_merge_strategy(self):
        boundary = Command().load_boundary(self.feature)
        self.assertEqual(boundary.set, self.boundary_set)
        self.assertEqual(boundary.set_name, 'District')
        self.assertEqual(boundary.external_id, '')
        self.assertEqual(boundary.name, 'Test')
        self.assertEqual(boundary.slug, 'test')
        self.assertEqual(boundary.metadata, {})
        self.assertEqual(boundary.shape.ogr.wkt,
                         'MULTIPOLYGON (((0 0,0.0001 0.0001,0 5,5 5,0 0)))')
        self.assertEqual(boundary.simple_shape.ogr.wkt,
                         'MULTIPOLYGON (((0 0,0 5,5 5,0 0)))')
        self.assertEqual(boundary.centroid.ogr.wkt,
                         'POINT (1.6667 3.333366666666666)')
        self.assertEqual(boundary.extent,
                         (0.0, 0.0, 4.999999999999999, 4.999999999999999))
        self.assertEqual(boundary.label_point, None)

    def test_invalid_merge_strategy_when_nothing_to_merge(self):
        try:
            Command().load_boundary(self.feature, 'invalid')
        except Exception as e:
            self.fail('Exception %s raised: %s %s' %
                      (type(e).__name__, e, traceback.format_exc()))

    def test_invalid_merge_strategy(self):
        Command().load_boundary(self.feature, 'invalid')

        self.assertRaisesRegexp(
            ValueError,
            r"\AThe merge strategy 'invalid' must be 'combine' or 'union'.\Z",
            Command().load_boundary, self.feature, 'invalid')

    def test_combine_merge_strategy(self):
        self.boundary_set.save()
        Command().load_boundary(self.feature, 'invalid')

        boundary = Command().load_boundary(self.feature, 'combine')
        self.assertEqual(
            boundary.shape.ogr.wkt,
            'MULTIPOLYGON (((0 0,0.0001 0.0001,0 5,5 5,0 0)),((0 0,0.0001 0.0001,0 5,5 5,0 0)))'
        )
        self.assertEqual(
            boundary.simple_shape.ogr.wkt,
            'MULTIPOLYGON (((0 0,0 5,5 5,0 0)),((0 0,0 5,5 5,0 0)))')
        self.assertEqual(boundary.centroid.ogr.wkt,
                         'POINT (1.6667 3.333366666666667)')
        self.assertEqual(boundary.extent, (0.0, 0.0, 5.0, 5.0))

    def test_union_merge_strategy(self):
        self.boundary_set.save()
        Command().load_boundary(self.feature, 'invalid')

        boundary = Command().load_boundary(self.feature, 'union')
        self.assertEqual(
            boundary.shape.ogr.wkt,
            'MULTIPOLYGON (((0.0001 0.0001,0 5,0 5,0 5,5 5,5 5,0.0001 0.0001)))'
        )
        self.assertEqual(
            boundary.simple_shape.ogr.wkt,
            'MULTIPOLYGON (((0.0001 0.0001,0 5,5 5,5 5,0.0001 0.0001)))')
        self.assertEqual(boundary.centroid.ogr.wkt,
                         'POINT (1.6667 3.333366666666667)')
        self.assertEqual(boundary.extent, (0.0, 0.0001, 5.0, 5.0))
 def test_boundary_set(self):
     self.assertEqual(Boundary(set=BoundarySet(slug='foo')).boundary_set, 'foo')
class FeatureTestCase(TestCase):

    def setUp(self):
        self.definition = definition = Definition({
            'last_updated': date(2000, 1, 1),
            'encoding': 'utf-8',
            'name': 'Districts',
            'name_func': clean_attr('Name'),
            'id_func': attr('ID'),
            'slug_func': attr('Code'),
            'is_valid_func': lambda f: f.get('ID') == '1',
            'label_point_func': lambda f: Point(0, 1),
        })

        self.fields = {
            'Name': 'VALID',
            'ID': '1',
            'Code': '\tFoo—Bar–Baz \r Bzz\n',  # m-dash, n-dash
        }

        self.boundary_set = BoundarySet(
            last_updated=definition['last_updated'],
            name=definition['name'],
            singular=definition['singular'],
        )

        self.feature = Feature(FeatureProxy(self.fields), definition)

        self.other = Feature(FeatureProxy({
            'Name': 'INVALID',
            'ID': 100,
            'Code': 3,
        }), definition, SpatialReference(4269), self.boundary_set)

    def test_init(self):
        self.assertEqual(self.feature.boundary_set, None)
        self.assertEqual(self.other.boundary_set, self.boundary_set)

    def test_str(self):
        self.assertEqual(str(self.feature), 'Valid')
        self.assertEqual(str(self.other), 'Invalid')

    def test_get(self):
        self.assertEqual(self.feature.get('Name'), 'VALID')

    def test_is_valid(self):
        self.assertTrue(self.feature.is_valid())
        self.assertFalse(self.other.is_valid())

    def test_name(self):
        self.assertEqual(self.feature.name, 'Valid')
        self.assertEqual(self.other.name, 'Invalid')

    def test_id(self):
        self.assertEqual(self.feature.id, '1')
        self.assertEqual(self.other.id, '100')

    def test_slug(self):
        self.assertEqual(self.feature.slug, 'foo-bar-baz-bzz')
        self.assertEqual(self.other.slug, '3')

    def test_label_point(self):
        self.assertEqual(self.feature.label_point, Point(0, 1))

    def test_metadata(self):
        self.assertEqual(self.feature.metadata, self.fields)

    def test_boundary_set(self):
        self.feature.boundary_set = self.boundary_set

        self.assertEqual(self.feature.boundary_set, self.boundary_set)

        self.feature.boundary_set = None

    def test_create_boundary(self):
        self.feature.boundary_set = self.boundary_set

        self.boundary_set.save()
        boundary = self.feature.create_boundary()
        self.assertEqual(boundary.set, self.boundary_set)
        self.assertEqual(boundary.set_name, 'District')
        self.assertEqual(boundary.external_id, '1')
        self.assertEqual(boundary.name, 'Valid')
        self.assertEqual(boundary.slug, 'foo-bar-baz-bzz')
        self.assertEqual(boundary.metadata, self.fields)
        self.assertEqual(boundary.shape.ogr.wkt, 'MULTIPOLYGON (((0 0,0.0001 0.0001,0 5,5 5,0 0)))')
        self.assertEqual(boundary.simple_shape.ogr.wkt, 'MULTIPOLYGON (((0 0,0 5,5 5,0 0)))')
        self.assertEqual(boundary.centroid.ogr.wkt, 'POINT (1.6667 3.333366666666666)')
        self.assertEqual(boundary.extent, (0.0, 0.0, 4.999999999999999, 4.999999999999999))
        self.assertEqual(boundary.label_point, Point(0, 1, srid=4326))
        self.assertEqual(boundary.start_date, None)
        self.assertEqual(boundary.end_date, None)

        self.feature.boundary_set = None
Example #10
0
class FeatureTestCase(TestCase):
    definition = Definition({
        'last_updated': date(2000, 1, 1),
        'encoding': 'utf-8',
        'name': 'Districts',
        'name_func': clean_attr('Name'),
        'id_func': attr('ID'),
        'slug_func': attr('Code'),
        'is_valid_func': lambda f: f.get('ID') == '1',
        'label_point_func': lambda f: Point(0, 1),
    })

    fields = {
        'Name': 'VALID',
        'ID': '1',
        'Code': '\tFoo—Bar–Baz \r Bzz\n',  # m-dash, n-dash
    }

    boundary_set = BoundarySet(
        last_updated=definition['last_updated'],
        name=definition['name'],
        singular=definition['singular'],
    )

    feature = Feature(FeatureProxy(fields), definition)

    other = Feature(FeatureProxy({
        'Name': 'INVALID',
        'ID': 100,
        'Code': 3,
    }), definition, SpatialReference(4269), boundary_set)

    def test_init(self):
        self.assertEqual(self.feature.boundary_set, None)
        self.assertEqual(self.other.boundary_set, self.boundary_set)

    def test_str(self):
        self.assertEqual(str(self.feature), 'Valid')
        self.assertEqual(str(self.other), 'Invalid')

    def test_get(self):
        self.assertEqual(self.feature.get('Name'), 'VALID')

    def test_is_valid(self):
        self.assertTrue(self.feature.is_valid())
        self.assertFalse(self.other.is_valid())

    def test_name(self):
        self.assertEqual(self.feature.name, 'Valid')
        self.assertEqual(self.other.name, 'Invalid')

    def test_id(self):
        self.assertEqual(self.feature.id, '1')
        self.assertEqual(self.other.id, '100')

    def test_slug(self):
        self.assertEqual(self.feature.slug, 'foo-bar-baz-bzz')
        self.assertEqual(self.other.slug, '3')

    def test_label_point(self):
        self.assertEqual(self.feature.label_point, Point(0, 1))

    def test_metadata(self):
        self.assertEqual(self.feature.metadata, self.fields)

    def test_boundary_set(self):
        self.feature.boundary_set = self.boundary_set

        self.assertEqual(self.feature.boundary_set, self.boundary_set)

        self.feature.boundary_set = None

    def test_create_boundary(self):
        self.feature.boundary_set = self.boundary_set

        self.boundary_set.save()
        boundary = self.feature.create_boundary()
        self.assertEqual(boundary.set, self.boundary_set)
        self.assertEqual(boundary.set_name, 'District')
        self.assertEqual(boundary.external_id, '1')
        self.assertEqual(boundary.name, 'Valid')
        self.assertEqual(boundary.slug, 'foo-bar-baz-bzz')
        self.assertEqual(boundary.metadata, self.fields)
        self.assertEqual(boundary.shape.ogr.wkt, 'MULTIPOLYGON (((0 0,0.0001 0.0001,0 5,5 5,0 0)))')
        self.assertEqual(boundary.simple_shape.ogr.wkt, 'MULTIPOLYGON (((0 0,0 5,5 5,0 0)))')
        self.assertEqual(boundary.centroid.ogr.wkt, 'POINT (1.6667 3.333366666666666)')
        self.assertEqual(boundary.extent, (0.0, 0.0, 4.999999999999999, 4.999999999999999))
        self.assertEqual(boundary.label_point, Point(0, 1))

        self.feature.boundary_set = None