예제 #1
0
    def test_item_deserialization_invalid_link(self):
        sample = self.data_factory.create_item_sample(
            collection=self.collection.model,
            sample='item-invalid-link',
        )

        # translate to Python native:
        serializer = ItemSerializer(data=sample.get_json('deserialize'))
        with self.assertRaises(ValidationError):
            serializer.is_valid(raise_exception=True)
예제 #2
0
    def test_item_deserialization_missing_required(self):
        data = OrderedDict([
            ("collection", self.collection["name"]),
            ("id", "test"),
        ])

        # translate to Python native:
        serializer = ItemSerializer(data=data)
        with self.assertRaises(ValidationError):
            serializer.is_valid(raise_exception=True)
예제 #3
0
    def test_item_deserialization_end_date_before_start_date(self):
        today = datetime.utcnow()
        yesterday = today - timedelta(days=1)
        sample = self.data_factory.create_item_sample(
            collection=self.collection.model,
            sample='item-1',
            properties={
                'start_datetime': isoformat(utc_aware(today)),
                "end_datetime": isoformat(utc_aware(yesterday))
            })

        # translate to Python native:
        serializer = ItemSerializer(data=sample.get_json('deserialize'))
        with self.assertRaises(ValidationError):
            serializer.is_valid(raise_exception=True)
예제 #4
0
    def test_item_serialization_datetime_range(self):
        sample = self.data_factory.create_item_sample(
            collection=self.collection.model, sample='item-2')
        # translate to Python native:
        context = {
            'request':
            api_request_mocker.get(
                f'{STAC_BASE_V}/collections/{self.collection["name"]}/items/{sample["name"]}'
            )
        }
        serializer = ItemSerializer(sample.model, context=context)
        python_native = serializer.data

        logger.debug('python native:\n%s', pformat(python_native))

        # translate to JSON:
        json_string = JSONRenderer().render(python_native,
                                            renderer_context={'indent': 2})
        logger.debug('json string: %s', json_string.decode("utf-8"))

        self.assertSetEqual(
            set([
                'stac_version', 'id', 'bbox', 'geometry', 'type', 'properties',
                'links', 'assets'
            ]).difference(python_native.keys()),
            set(),
            msg="These required fields by the STAC API spec are missing")
        self.check_stac_item(sample.json, python_native,
                             self.collection["name"])
예제 #5
0
    def test_item_deserialization_update_remove_title(self):
        original_sample = self.data_factory.create_item_sample(
            collection=self.collection.model,
            sample='item-1',
        )
        sample = self.data_factory.create_item_sample(
            collection=self.collection.model,
            sample='item-2',
            name=original_sample["name"],
            properties={"datetime": isoformat(utc_aware(datetime.utcnow()))})
        serializer = ItemSerializer(original_sample.model,
                                    data=sample.get_json('deserialize'))
        serializer.is_valid(raise_exception=True)
        item = serializer.save()

        # mock a request needed for the serialization of links
        context = {
            'request':
            api_request_mocker.get(
                f'{STAC_BASE_V}/collections/{self.collection["name"]}/items/{sample["name"]}'
            )
        }
        serializer = ItemSerializer(item, context=context)
        python_native = serializer.data
        self.check_stac_item(sample.json, python_native,
                             self.collection["name"])
        self.assertNotIn('title',
                         python_native['properties'].keys(),
                         msg="Title was not removed")
예제 #6
0
    def test_item_deserialization_update(self):
        original_sample = self.data_factory.create_item_sample(
            collection=self.collection.model,
            sample='item-1',
        )
        sample = self.data_factory.create_item_sample(
            collection=self.collection.model,
            sample='item-2',
            name=original_sample["name"])
        serializer = ItemSerializer(original_sample.model,
                                    data=sample.get_json('deserialize'))
        serializer.is_valid(raise_exception=True)
        item = serializer.save()

        # mock a request needed for the serialization of links
        context = {
            'request':
            api_request_mocker.get(
                f'{STAC_BASE_V}/collections/{self.collection["name"]}/items/{sample["name"]}'
            )
        }
        serializer = ItemSerializer(item, context=context)
        python_native = serializer.data
        self.check_stac_item(sample.json, python_native,
                             self.collection["name"])
        self.assertIsNone(get_link(python_native['links'], 'describedBy'),
                          msg='Link describedBy was not removed in update')
예제 #7
0
    def test_item_serialization(self):

        context = {
            'request':
            api_request_mocker.get(
                f'{STAC_BASE_V}/collections/{self.collection["name"]}/items/{self.item["name"]}'
            )
        }
        serializer = ItemSerializer(self.item.model, context=context)
        python_native = serializer.data

        logger.debug('python native:\n%s', pformat(python_native))

        # translate to JSON:
        json_string = JSONRenderer().render(python_native,
                                            renderer_context={'indent': 2})
        logger.debug('json string: %s', json_string.decode("utf-8"))

        self.assertSetEqual(
            set([
                'stac_version', 'id', 'bbox', 'geometry', 'type', 'properties',
                'links', 'assets'
            ]).difference(python_native.keys()),
            set(),
            msg="These required fields by the STAC API spec are missing")

        collection_name = self.collection.model.name
        item_name = self.item.model.name
        expected_asset = self.asset.json
        expected_asset.pop('id')
        expected_asset.pop('item')
        expected = self.item.json
        expected.update({
            'assets': {
                self.asset['name']: expected_asset
            },
            'bbox': (5.644711, 46.775054, 7.602408, 49.014995),
            'stac_extensions': [
                'eo', 'proj', 'view',
                'https://data.geo.admin.ch/stac/geoadmin-extension/1.0/schema.json'
            ],
            'stac_version':
            settings.STAC_VERSION,
            'type':
            'Feature'
        })
        self.check_stac_item(expected, python_native, collection_name)
예제 #8
0
    def test_item_deserialization_create_full(self):
        sample = self.data_factory.create_item_sample(
            collection=self.collection.model, sample='item-1')

        # translate to Python native:
        serializer = ItemSerializer(data=sample.get_json('deserialize'))
        serializer.is_valid(raise_exception=True)
        item = serializer.save()

        # serialize the object and test it against the one above
        # mock a request needed for the serialization of links
        context = {
            'request':
            api_request_mocker.get(
                f'{STAC_BASE_V}/collections/{self.collection["name"]}/items/{sample["name"]}'
            )
        }
        serializer = ItemSerializer(item, context=context)
        python_native = serializer.data
        self.check_stac_item(sample.json, python_native,
                             self.collection["name"])