예제 #1
0
    def test_collection_deserialization_invalid_provider(self):
        data = self.data_factory.create_collection_sample(
            sample='collection-invalid-providers').get_json('deserialize')

        # translate to Python native:
        serializer = CollectionSerializer(data=data)
        with self.assertRaises(ValidationError):
            serializer.is_valid(raise_exception=True)
예제 #2
0
    def test_collection_deserialization_update_existing(self):
        # Create a collection
        collection = self.data_factory.create_collection_sample(
            sample='collection-1').model

        # Get a new samples based on another sample
        sample = self.data_factory.create_collection_sample(
            sample='collection-4', name=collection.name)
        context = {
            'request':
            api_request_mocker.get(
                f'{STAC_BASE_V}/collections/{collection.name}')
        }
        serializer = CollectionSerializer(collection,
                                          data=sample.get_json('deserialize'),
                                          context=context)
        serializer.is_valid(raise_exception=True)
        collection = serializer.save()

        # serialize the object and test it against the one above
        # mock a request needed for the serialization of links
        serializer = CollectionSerializer(collection, context=context)
        python_native = serializer.data
        self.check_stac_collection(sample.json, python_native)

        self.assertNotIn('providers',
                         python_native,
                         msg='Providers have not been removed')
        self.assertIn('links', python_native, msg='Generated links missing')
        self.assertIsNone(get_link(python_native['links'], 'describedBy'),
                          msg='User link describedBy have not been removed')
예제 #3
0
    def test_collection_deserialization_create_only_required(self):
        sample = self.data_factory.create_collection_sample(required_only=True)
        serializer = CollectionSerializer(data=sample.get_json('deserialize'))
        serializer.is_valid(raise_exception=True)
        collection = serializer.save()

        # mock a request needed for the serialization of links
        context = {
            'request':
            api_request_mocker.get(
                f'{STAC_BASE_V}/collections/{collection.name}')
        }
        serializer = CollectionSerializer(collection, context=context)
        python_native = serializer.data
        self.check_stac_collection(sample.json, python_native)
예제 #4
0
)
collection3.save()
# populate the ManyToMany relation fields
collection3.links.add(link_root)
collection3.providers.add(provider1, provider3)
collection3.save()

provider1.save()
provider2.save()
provider3.save()
link_root.save()


# test the serialization process:
# translate into Python native
serializer = CollectionSerializer(collection1)
serializer.data

# translate into json
content = JSONRenderer().render(serializer.data)
content

# back-translate into Python native
stream = io.BytesIO(content)
data = JSONParser().parse(stream)

# back-translate into fully populated object instance
serializer = CollectionSerializer(data=data)
serializer.is_valid()  # hopefully True, if False, serializer.errors gives hints

serializer.validated_data
예제 #5
0
    def test_collection_serialization(self):
        collection_name = self.collection.model.name
        # mock a request needed for the serialization of links
        context = {
            'request':
            api_request_mocker.get(
                f'{STAC_BASE_V}/collections/{collection_name}')
        }

        # transate to Python native:
        serializer = CollectionSerializer(self.collection.model,
                                          context=context)
        python_native = serializer.data
        logger.debug('python native:\n%s', pformat(python_native))

        # translate to JSON:
        content = JSONRenderer().render(python_native)
        logger.debug('json string: %s', content.decode("utf-8"))

        expected = self.collection.get_json('serialize')
        expected.update({
            'created':
            isoformat(self.collection_created),
            'crs': ['http://www.opengis.net/def/crs/OGC/1.3/CRS84'],
            'extent':
            OrderedDict([('spatial', {
                'bbox': [[5.644711, 46.775054, 7.602408, 49.014995]]
            }),
                         ('temporal', {
                             'interval':
                             [['2020-10-28T13:05:10Z', '2020-10-28T13:05:10Z']]
                         })]),
            'itemType':
            'Feature',
            'links': [
                OrderedDict([
                    ('rel', 'self'),
                    ('href',
                     f'http://testserver/api/stac/v0.9/collections/{collection_name}'
                     ),
                ]),
                OrderedDict([
                    ('rel', 'root'),
                    ('href', 'http://testserver/api/stac/v0.9/'),
                ]),
                OrderedDict([
                    ('rel', 'parent'),
                    ('href', 'http://testserver/api/stac/v0.9/collections'),
                ]),
                OrderedDict([
                    ('rel', 'items'),
                    ('href',
                     f'http://testserver/api/stac/v0.9/collections/{collection_name}/items'
                     ),
                ]),
                OrderedDict([
                    ('href', 'https://www.example.com/described-by'),
                    ('rel', 'describedBy'),
                    ('type', 'description'),
                    ('title', 'This is an extra link'),
                ])
            ],
            'providers': [
                {
                    'name': 'provider-1',
                    'roles': ['licensor', 'producer'],
                    'description':
                    'This is a full description of the provider',
                    'url': 'https://www.provider.com'
                },
                {
                    'name': 'provider-2',
                    'roles': ['licensor'],
                    'description':
                    'This is a full description of a second provider',
                    'url': 'https://www.provider.com/provider-2'
                },
                {
                    'name': 'provider-3',
                },
            ],
            'stac_extensions': [
                'eo', 'proj', 'view',
                'https://data.geo.admin.ch/stac/geoadmin-extension/1.0/schema.json'
            ],
            'stac_version':
            settings.STAC_VERSION,
            'summaries': {
                'eo:gsd': [3.4],
                'geoadmin:variant': ['kgrs'],
                'proj:epsg': [2056],
            },
            'updated':
            isoformat(self.collection_created)
        })
        self.check_stac_collection(expected, python_native)
예제 #6
0
    def test_empty_collection_serialization(self):
        collection_name = self.collection.model.name
        # mock a request needed for the serialization of links
        context = {
            'request':
            api_request_mocker.get(
                f'{STAC_BASE_V}/collections/{collection_name}')
        }

        # transate to Python native:
        serializer = CollectionSerializer(self.collection.model,
                                          context=context)
        python_native = serializer.data
        logger.debug('python native:\n%s', pformat(python_native))

        # translate to JSON:
        content = JSONRenderer().render(python_native)
        logger.debug('json string: %s', content.decode("utf-8"))

        expected = self.collection.get_json('serialize')
        expected.update({
            'created':
            isoformat(self.collection_created),
            'crs': ['http://www.opengis.net/def/crs/OGC/1.3/CRS84'],
            'extent': {
                'spatial': {
                    'bbox': [[]]
                },
                'temporal': {
                    'interval': [[None, None]]
                }
            },
            'itemType':
            'Feature',
            'links': [
                OrderedDict([
                    ('rel', 'self'),
                    ('href',
                     f'http://testserver/api/stac/v0.9/collections/{collection_name}'
                     ),
                ]),
                OrderedDict([
                    ('rel', 'root'),
                    ('href', 'http://testserver/api/stac/v0.9/'),
                ]),
                OrderedDict([
                    ('rel', 'parent'),
                    ('href', 'http://testserver/api/stac/v0.9/'),
                ]),
                OrderedDict([
                    ('rel', 'items'),
                    ('href',
                     f'http://testserver/api/stac/v0.9/collections/{collection_name}/items'
                     ),
                ]),
                OrderedDict([
                    ('href', 'https://www.example.com/described-by'),
                    ('rel', 'describedBy'),
                    ('type', 'description'),
                    ('title', 'This is an extra collection link'),
                ])
            ],
            'providers': [
                {
                    'name': 'provider-1',
                    'roles': ['licensor', 'producer'],
                    'description':
                    'This is a full description of the provider',
                    'url': 'https://www.provider.com'
                },
                {
                    'name': 'provider-2',
                    'roles': ['licensor'],
                    'description':
                    'This is a full description of a second provider',
                    'url': 'https://www.provider.com/provider-2'
                },
                {
                    'name': 'provider-3',
                },
            ],
            'stac_version':
            settings.STAC_VERSION,
            'summaries': {},
            'updated':
            isoformat(self.collection_created)
        })
        self.check_stac_collection(expected, python_native)