Beispiel #1
0
class FeatureDetailTestCase(APITestCase):
    def setUp(self) -> None:
        self.layer_trek = LayerSchemaFactory(
            geom_type=GeometryTypes.LineString)
        self.layer_city = LayerSchemaFactory(geom_type=GeometryTypes.Polygon)
        self.trek = FeatureFactory(layer=self.layer_trek,
                                   geom='LINESTRING(0 0, 1 1, 2 2, 3 3)')
        self.city_uncover = FeatureFactory(
            layer=self.layer_city,
            geom='POLYGON((4 4, 4 7, 7 7, 7 4, 4 4))',
            properties={
                "name": "Cahors",
                "age": 50000
            })
        self.detail_url = reverse('feature-detail',
                                  args=(
                                      self.layer_city.pk,
                                      self.city_uncover.identifier,
                                  ))
        self.super_user = UserFactory(is_superuser=True)
        self.client.force_authenticate(self.super_user)

    def test_feature_detail(self):
        response = self.client.get(self.detail_url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_feature_patch_keep_properties(self):
        response = self.client.patch(self.detail_url,
                                     data={"properties": {
                                         "name": "Divona"
                                     }})
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        data = response.json()
        self.assertListEqual(sorted(list(data['properties'].keys())),
                             sorted(['name', 'age']))

    def test_feature_put_replace_properties(self):
        response = self.client.put(self.detail_url,
                                   data={
                                       'geom':
                                       'POLYGON((4 4, 4 7, 7 7, 7 4, 4 4))',
                                       "properties": {
                                           "name": "Divona"
                                       }
                                   })
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        data = response.json()
        self.assertListEqual(sorted(list(data['properties'].keys())),
                             sorted([
                                 'name',
                             ]), data)

    def test_feature_put_point_empty_wkt(self):
        response = self.client.put(self.detail_url,
                                   data={
                                       'geom': 'POINT EMPTY',
                                       "properties": {
                                           "name": "Divona"
                                       }
                                   })
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        data = response.json()
        self.assertEqual(data['geom'], ['Geometry is empty'])

    def test_feature_put_point_empty_geojson(self):
        response = self.client.put(self.detail_url,
                                   data={
                                       'geom':
                                       '{"type": "Point", "coordinates": []}',
                                       "properties": {
                                           "name": "Divona"
                                       }
                                   })
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        data = response.json()
        self.assertEqual(data['geom'], [
            'Unable to convert to python object: Invalid geometry pointer '
            'returned from "OGR_G_CreateGeometryFromJson".'
        ])

    def test_feature_put_polygon_not_valid(self):
        response = self.client.put(self.detail_url,
                                   data={
                                       'geom':
                                       'POLYGON((0 0, 1 1, 1 2, 1 1, 0 0))',
                                       "properties": {
                                           "name": "Divona"
                                       }
                                   })
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        data = response.json()
        self.assertEqual(data['geom'], ['Geometry is not valid'])

    @patch('geostore.settings.GEOSTORE_RELATION_CELERY_ASYNC',
           new_callable=PropertyMock)
    def test_relation(self, mock_relation):
        mock_relation.return_value = True
        city_cover = FeatureFactory(layer=self.layer_city,
                                    geom='POLYGON((0 0, 0 3, 3 3, 3 0, 0 0))')
        intersect_relation = LayerRelation.objects.create(
            relation_type='intersects',
            origin=self.layer_trek,
            destination=self.layer_city,
        )
        url = reverse('feature-relation',
                      args=(self.layer_trek.pk, self.trek.identifier,
                            intersect_relation.pk))
        # city cover should be present after sync
        self.trek.sync_relations(intersect_relation.pk)
        response = self.client.get(url, data={'page_size': 1})
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        data = response.json()
        self.assertEqual(len(data), 1, data)

        # city cover should not be present after deletion
        city_cover.delete()
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        data = response.json()
        self.assertEqual(len(data), 0, data)

    @patch('geostore.settings.GEOSTORE_RELATION_CELERY_ASYNC',
           new_callable=PropertyMock)
    def test_relation_geojson(self, mock_relation):
        mock_relation.return_value = True
        city_cover = FeatureFactory(layer=self.layer_city,
                                    geom='POLYGON((0 0, 0 3, 3 3, 3 0, 0 0))')
        city_cover_2 = FeatureFactory(
            layer=self.layer_city, geom='POLYGON((0 0, 0 3, 4 4, 3 0, 0 0))')
        intersect_relation = LayerRelation.objects.create(
            relation_type='intersects',
            origin=self.layer_trek,
            destination=self.layer_city,
        )
        url = reverse('feature-relation',
                      args=(self.layer_trek.pk, self.trek.identifier,
                            intersect_relation.pk))
        # city cover should be present after sync
        self.trek.sync_relations(intersect_relation.pk)
        response = self.client.get(url, data={'format': 'geojson'})

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        data = response.json()
        self.assertEqual(len(data['features']), 2, data)

        # city cover should not be present after deletion
        city_cover.delete()
        city_cover_2.delete()
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        data = response.json()
        self.assertEqual(len(data), 0, data)

    @patch('geostore.views.FeatureViewSet.pagination_class',
           new_callable=PropertyMock)
    def test_relation_with_pagination(self, mock_view):
        class MyPagination(PageNumberPagination):
            page_size = 1
            page_size_query_param = 'page_size'

        mock_view.return_value = MyPagination
        FeatureFactory(layer=self.layer_city,
                       geom='POLYGON((0 0, 0 3, 3 3, 3 0, 0 0))')
        intersect_relation = LayerRelation.objects.create(
            relation_type='intersects',
            origin=self.layer_trek,
            destination=self.layer_city,
        )
        url = reverse('feature-relation',
                      args=(self.layer_trek.pk, self.trek.identifier,
                            intersect_relation.pk))
        # city cover should be present after sync
        self.trek.sync_relations(intersect_relation.pk)
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        data = response.json()
        self.assertEqual(len(data['results']), 1, data)
        self.assertTrue(mock_view.called)
Beispiel #2
0
class LayerRelationTestCase(TestCase):
    def setUp(self) -> None:
        self.layer_trek = LayerSchemaFactory(
            geom_type=GeometryTypes.LineString)
        self.layer_city = LayerSchemaFactory(geom_type=GeometryTypes.Polygon)
        self.trek = FeatureFactory(layer=self.layer_trek,
                                   geom='LINESTRING(0 0, 1 1, 2 2, 3 3)')
        self.city_cover = FeatureFactory(
            layer=self.layer_city, geom='POLYGON((0 0, 0 3, 3 3, 3 0, 0 0))')
        self.city_uncover = FeatureFactory(
            layer=self.layer_city, geom='POLYGON((4 4, 4 7, 7 7, 7 4, 4 4))')

    def test_str(self):
        intersect_relation = LayerRelation.objects.create(
            relation_type='intersects',
            origin=self.layer_trek,
            destination=self.layer_city,
        )
        self.assertEqual(str(intersect_relation), intersect_relation.name)

    def test_sync_relations_intersects(self):
        intersect_relation = LayerRelation.objects.create(
            relation_type='intersects',
            origin=self.layer_trek,
            destination=self.layer_city,
        )
        # city cover should be present after sync
        self.trek.sync_relations(intersect_relation.pk)
        self.assertListEqual(
            list(
                self.trek.relations_as_origin.filter(
                    relation=intersect_relation).values_list('destination__pk',
                                                             flat=True)),
            [self.city_cover.pk])
        # city cover should not be present after deletion
        intersect_relation.delete()
        self.assertListEqual(
            list(
                self.trek.relations_as_origin.filter(
                    relation=intersect_relation).values_list('destination__pk',
                                                             flat=True)), [])

    def test_sync_relations_distance(self):
        distance_relation = LayerRelation.objects.create(
            relation_type='distance',
            origin=self.layer_trek,
            destination=self.layer_city,
            settings={'distance': 1000})
        # city cover should be present after sync
        self.trek.sync_relations(distance_relation.pk)
        self.assertListEqual(
            list(
                self.trek.relations_as_origin.filter(
                    relation=distance_relation).values_list('destination__pk',
                                                            flat=True)),
            [self.city_cover.pk])
        # city cover should not be present after deletion
        distance_relation.delete()
        self.assertListEqual(
            list(
                self.trek.relations_as_origin.filter(
                    relation=distance_relation).values_list('destination__pk',
                                                            flat=True)), [])

    def test_bad_relation(self):
        bad_relation = LayerRelation.objects.create(
            relation_type='distance',
            origin=self.layer_city,
            destination=self.layer_trek,
            settings={'distance': 1000})
        self.city_cover.sync_relations(bad_relation.pk)
        self.assertListEqual(
            list(
                self.trek.get_computed_relation_qs(
                    relation=bad_relation).values_list('pk', flat=True)), [])

    def test_relation_property(self):
        self.assertEqual(self.trek.relations, {})