class FeatureSerializerTestCase(ModelTestCase):
    def setUp(self):
        super(FeatureSerializerTestCase, self).setUp()
        attrs = {'id': 1,
                 'crs': 4326,
                 'geometry': {'type': 'Polygon',
                              'coordinates': self.coords},
                 'properties': {'name': 'Argentina'}}
        self.expected = Feature(**attrs)

    def test_serialize(self):
        serializer = LocationFeatureSerializer(self.obj)
        self.assertEqual(serializer.data, self.expected)

    def test_serialize_list(self):
        serializer = LocationFeatureSerializer([self.obj], many=True)
        feat = self.expected.copy()
        feat.pop('crs')
        self.assertEqual(serializer.data, FeatureCollection([feat]))

    def test_serialize_queryset(self):
        serializer = LocationFeatureSerializer(
            Location.objects.all(), many=True)
        feat = self.expected.copy()
        crs = feat.pop('crs')
        self.assertEqual(serializer.data['features'][0], feat)
        self.assertEqual(serializer.data['crs'], crs)

    def test_deserialize(self):
        serializer = LocationFeatureSerializer(data=self.expected)
        self.assertTrue(serializer.is_valid())
        self.assertEqual(serializer.object.geom, self.obj.geom)

    def test_deserialize_projected(self):
        feat = Feature(**dict(self.expected, crs=4269)).copy()
        serializer = LocationFeatureSerializer(data=feat)
        self.assertTrue(serializer.is_valid())
        self.assertEqual(serializer.object.geom.srid, 4269)

    def test_deserialize_list(self):
        features = [self.expected.copy(), self.expected.copy()]
        serializer = LocationFeatureSerializer(data=features, many=True)
        self.assertTrue(serializer.is_valid())
        self.assertEqual(serializer.object[0].geom, self.obj.geom)
class FeatureSerializerTestCase(ModelTestCase):
    def setUp(self):
        super(FeatureSerializerTestCase, self).setUp()
        attrs = {'id': 1,
                 'crs': 4326,
                 'geometry': {'type': 'Polygon',
                              'coordinates': self.coords},
                 'properties': {'name': 'Argentina'}}
        self.expected = Feature(**attrs)

    def test_geometry_field_source(self):
        response = self.client.get('/locations/', {'format': 'geojson', 'page': 1})
        context = {k: v for k, v in response.renderer_context.items()
                   if k in ('request', 'view')}
        serializer = LocationFeatureSerializer(
            Location.objects.annotate(geojson=functions.AsGeoJSON('geom')),
            many=True, context=context)
        fields = serializer.child.fields
        self.assertEqual(fields['geom'].source, 'geojson')
        # Test serializing paginated objects.
        page = response.renderer_context['view'].paginator.page
        serializer = LocationFeatureSerializer(
            page, many=True, context=context)
        fields = serializer.child.fields
        self.assertEqual(fields['geom'].source, 'geojson')

    def test_serialize(self):
        serializer = LocationFeatureSerializer(self.obj)
        self.assertEqual(serializer.data, self.expected)

    def test_serialize_list(self):
        serializer = LocationFeatureSerializer([self.obj], many=True)
        feat = self.expected.copy()
        feat.pop('crs')
        self.assertEqual(serializer.data, FeatureCollection([feat]))

    def test_serialize_queryset(self):
        serializer = LocationFeatureSerializer(
            Location.objects.all(), many=True)
        feat = self.expected.copy()
        crs = feat.pop('crs')
        self.assertEqual(serializer.data['features'][0], feat)
        self.assertEqual(serializer.data['crs'], crs)

    def test_serialize_queryset_simplify(self):
        fn = query.Simplify(functions.Transform('geom', 4269), 1.01)
        qs = Location.objects.all()
        for obj in qs:
            obj.geom = obj.geom.buffer(1.5)
            obj.save()
        qs = qs.annotate(simplify=fn)
        obj = qs[0]
        serializer = SimplifyLocationSerializer(obj)
        g = geos.GEOSGeometry(json.dumps(serializer.data['geometry']),
                              srid=obj.simplify.srid)
        self.assertEqual(g, obj.simplify)
        self.assertEqual(obj.simplify.srid, 4269)
        self.assertEqual(serializer.data['crs']['properties']['name'][-4:], '4269')

    def test_deserialize(self):
        serializer = LocationFeatureSerializer(data=self.expected)
        self.assertTrue(serializer.is_valid())
        object = serializer.save()
        self.assertEqual(object.geom, self.obj.geom)

    def test_deserialize_projected(self):
        feat = Feature(**dict(self.expected, crs=4269)).copy()
        serializer = LocationFeatureSerializer(data=feat)
        self.assertTrue(serializer.is_valid())
        object = serializer.save()
        self.assertEqual(object.geom.srid, 4269)

    def test_deserialize_list(self):
        features = [self.expected.copy(), self.expected.copy()]
        serializer = LocationFeatureSerializer(data=features, many=True)
        self.assertTrue(serializer.is_valid())
        object = serializer.save()
        self.assertEqual(object[0].geom, self.obj.geom)
class FeatureSerializerTestCase(ModelTestCase):
    def setUp(self):
        super(FeatureSerializerTestCase, self).setUp()
        attrs = {
            'id': 1,
            'crs': 4326,
            'geometry': {
                'type': 'Polygon',
                'coordinates': self.coords
            },
            'properties': {
                'name': 'Argentina'
            }
        }
        self.expected = Feature(**attrs)

    def test_geometry_field_source(self):
        request = factory.get('/', {'format': 'geojson', 'page': 1})
        response = PaginatedGeoListView.as_view()(request)
        context = {
            'request': response,
            'view': response.renderer_context['view']
        }
        serializer = LocationFeatureSerializer(Location.objects.geojson(),
                                               many=True,
                                               context=context)
        fields = serializer.child.fields
        self.assertEqual(fields['geom'].source, 'geojson')
        # Test serializing paginated objects.
        page = response.renderer_context['view'].paginator.page
        serializer = LocationFeatureSerializer(page,
                                               many=True,
                                               context=context)
        fields = serializer.child.fields
        self.assertEqual(fields['geom'].source, 'geojson')

    def test_serialize(self):
        serializer = LocationFeatureSerializer(self.obj)
        self.assertEqual(serializer.data, self.expected)

    def test_serialize_list(self):
        serializer = LocationFeatureSerializer([self.obj], many=True)
        feat = self.expected.copy()
        feat.pop('crs')
        self.assertEqual(serializer.data, FeatureCollection([feat]))

    def test_serialize_queryset(self):
        serializer = LocationFeatureSerializer(Location.objects.all(),
                                               many=True)
        feat = self.expected.copy()
        crs = feat.pop('crs')
        self.assertEqual(serializer.data['features'][0], feat)
        self.assertEqual(serializer.data['crs'], crs)

    def test_serialize_queryset_simplify(self):
        # Too high of simplification tolerance should return empty geometry.
        serializer = LocationFeatureSerializer(
            Location.objects.simplify(10, srid=4269)[0])
        self.assertEqual(serializer.data['geometry'], {})

    def test_deserialize(self):
        serializer = LocationFeatureSerializer(data=self.expected)
        self.assertTrue(serializer.is_valid())
        object = serializer.save()
        self.assertEqual(object.geom, self.obj.geom)

    def test_deserialize_projected(self):
        feat = Feature(**dict(self.expected, crs=4269)).copy()
        serializer = LocationFeatureSerializer(data=feat)
        self.assertTrue(serializer.is_valid())
        object = serializer.save()
        self.assertEqual(object.geom.srid, 4269)

    def test_deserialize_list(self):
        features = [self.expected.copy(), self.expected.copy()]
        serializer = LocationFeatureSerializer(data=features, many=True)
        self.assertTrue(serializer.is_valid())
        object = serializer.save()
        self.assertEqual(object[0].geom, self.obj.geom)
class FeatureSerializerTestCase(ModelTestCase):
    def setUp(self):
        super(FeatureSerializerTestCase, self).setUp()
        attrs = {'id': 1,
                 'crs': 4326,
                 'geometry': {'type': 'Polygon',
                              'coordinates': self.coords},
                 'properties': {'name': 'Argentina'}}
        self.expected = Feature(**attrs)

    def test_geometry_field_source(self):
        request = factory.get('/', {'format': 'geojson', 'page': 1})
        response = PaginatedGeoListView.as_view()(request)
        context = {'request': response,
                   'view': response.renderer_context['view']}
        serializer = LocationFeatureSerializer(
            Location.objects.geojson(), many=True, context=context)
        fields = serializer.child.fields
        self.assertEqual(fields['geom'].source, 'geojson')
        # Test serializing paginated objects.
        page = response.renderer_context['view'].paginator.page
        serializer = LocationFeatureSerializer(
            page, many=True, context=context)
        fields = serializer.child.fields
        self.assertEqual(fields['geom'].source, 'geojson')

    def test_serialize(self):
        serializer = LocationFeatureSerializer(self.obj)
        self.assertEqual(serializer.data, self.expected)

    def test_serialize_list(self):
        serializer = LocationFeatureSerializer([self.obj], many=True)
        feat = self.expected.copy()
        feat.pop('crs')
        self.assertEqual(serializer.data, FeatureCollection([feat]))

    def test_serialize_queryset(self):
        serializer = LocationFeatureSerializer(
            Location.objects.all(), many=True)
        feat = self.expected.copy()
        crs = feat.pop('crs')
        self.assertEqual(serializer.data['features'][0], feat)
        self.assertEqual(serializer.data['crs'], crs)

    def test_deserialize(self):
        serializer = LocationFeatureSerializer(data=self.expected)
        self.assertTrue(serializer.is_valid())
        object = serializer.save()
        self.assertEqual(object.geom, self.obj.geom)

    def test_deserialize_projected(self):
        feat = Feature(**dict(self.expected, crs=4269)).copy()
        serializer = LocationFeatureSerializer(data=feat)
        self.assertTrue(serializer.is_valid())
        object = serializer.save()
        self.assertEqual(object.geom.srid, 4269)

    def test_deserialize_list(self):
        features = [self.expected.copy(), self.expected.copy()]
        serializer = LocationFeatureSerializer(data=features, many=True)
        self.assertTrue(serializer.is_valid())
        object = serializer.save()
        self.assertEqual(object[0].geom, self.obj.geom)