File: test_validate_item_collection.py

package info (click to toggle)
stac-validator 3.10.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,280 kB
  • sloc: python: 3,429; makefile: 35
file content (598 lines) | stat: -rw-r--r-- 24,722 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
"""
Description: Test stac-validator on item-collection validation.

"""

import pytest

from stac_validator import stac_validator


@pytest.mark.network
def test_validate_item_collection_in_memory():
    item_collection = {
        "type": "FeatureCollection",
        "features": [
            {
                "stac_version": "1.0.0",
                "stac_extensions": [],
                "type": "Feature",
                "id": "20201211_223832_CS2_A",
                "bbox": [
                    172.91173669923782,
                    1.3438851951615003,
                    172.95469614953714,
                    1.3690476620161975,
                ],
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [172.91173669923782, 1.3438851951615003],
                            [172.95469614953714, 1.3438851951615003],
                            [172.95469614953714, 1.3690476620161975],
                            [172.91173669923782, 1.3690476620161975],
                            [172.91173669923782, 1.3438851951615003],
                        ]
                    ],
                },
                "properties": {
                    "title": "Core Item",
                    "description": "A sample STAC Item that includes examples of all common metadata",
                    "datetime": None,
                    "start_datetime": "2020-12-11T22:38:32.125Z",
                    "end_datetime": "2020-12-11T22:38:32.327Z",
                    "created": "2020-12-12T01:48:13.725Z",
                    "updated": "2020-12-12T01:48:13.725Z",
                    "platform": "cool_sat1",
                    "instruments": ["cool_sensor_v1"],
                    "constellation": "ion",
                    "mission": "collection 5624",
                    "gsd": 0.512,
                },
                "collection": "simple-collection",
                "links": [
                    {
                        "rel": "collection",
                        "href": "./collection.json",
                        "type": "application/json",
                        "title": "Simple Example Collection",
                    },
                    {
                        "rel": "root",
                        "href": "./collection.json",
                        "type": "application/json",
                        "title": "Simple Example Collection",
                    },
                    {
                        "rel": "parent",
                        "href": "./collection.json",
                        "type": "application/json",
                        "title": "Simple Example Collection",
                    },
                    {
                        "rel": "alternate",
                        "type": "text/html",
                        "href": "http://remotedata.io/catalog/20201211_223832_CS2/index.html",
                        "title": "HTML version of this STAC Item",
                    },
                ],
                "assets": {
                    "analytic": {
                        "href": "https://storage.googleapis.com/open-cogs/stac-examples/20201211_223832_CS2_analytic.tif",
                        "type": "image/tiff; application=geotiff; profile=cloud-optimized",
                        "title": "4-Band Analytic",
                        "roles": ["data"],
                    },
                    "thumbnail": {
                        "href": "https://storage.googleapis.com/open-cogs/stac-examples/20201211_223832_CS2.jpg",
                        "title": "Thumbnail",
                        "type": "image/png",
                        "roles": ["thumbnail"],
                    },
                    "visual": {
                        "href": "https://storage.googleapis.com/open-cogs/stac-examples/20201211_223832_CS2.tif",
                        "type": "image/tiff; application=geotiff; profile=cloud-optimized",
                        "title": "3-Band Visual",
                        "roles": ["visual"],
                    },
                    "udm": {
                        "href": "https://storage.googleapis.com/open-cogs/stac-examples/20201211_223832_CS2_analytic_udm.tif",
                        "title": "Unusable Data Mask",
                        "type": "image/tiff; application=geotiff;",
                    },
                    "json-metadata": {
                        "href": "http://remotedata.io/catalog/20201211_223832_CS2/extended-metadata.json",
                        "title": "Extended Metadata",
                        "type": "application/json",
                        "roles": ["metadata"],
                    },
                    "ephemeris": {
                        "href": "http://cool-sat.com/catalog/20201211_223832_CS2/20201211_223832_CS2.EPH",
                        "title": "Satellite Ephemeris Metadata",
                    },
                },
            },
            {
                "stac_version": "1.0.0",
                "stac_extensions": [],
                "type": "Feature",
                "id": "20201211_223832_CS2_B",
                "bbox": [
                    172.91173669923782,
                    1.3438851951615003,
                    172.95469614953714,
                    1.3690476620161975,
                ],
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [172.91173669923782, 1.3438851951615003],
                            [172.95469614953714, 1.3438851951615003],
                            [172.95469614953714, 1.3690476620161975],
                            [172.91173669923782, 1.3690476620161975],
                            [172.91173669923782, 1.3438851951615003],
                        ]
                    ],
                },
                "properties": {
                    "title": "Core Item",
                    "description": "A sample STAC Item that includes examples of all common metadata",
                    "datetime": None,
                    "start_datetime": "2020-12-11T22:38:32.125Z",
                    "end_datetime": "2020-12-11T22:38:32.327Z",
                    "created": "2020-12-12T01:48:13.725Z",
                    "updated": "2020-12-12T01:48:13.725Z",
                    "platform": "cool_sat1",
                    "instruments": ["cool_sensor_v1"],
                    "constellation": "ion",
                    "mission": "collection 5624",
                    "gsd": 0.512,
                },
                "collection": "simple-collection",
                "links": [
                    {
                        "rel": "collection",
                        "href": "./collection.json",
                        "type": "application/json",
                        "title": "Simple Example Collection",
                    },
                    {
                        "rel": "root",
                        "href": "./collection.json",
                        "type": "application/json",
                        "title": "Simple Example Collection",
                    },
                    {
                        "rel": "parent",
                        "href": "./collection.json",
                        "type": "application/json",
                        "title": "Simple Example Collection",
                    },
                    {
                        "rel": "alternate",
                        "type": "text/html",
                        "href": "http://remotedata.io/catalog/20201211_223832_CS2/index.html",
                        "title": "HTML version of this STAC Item",
                    },
                ],
                "assets": {
                    "analytic": {
                        "href": "https://storage.googleapis.com/open-cogs/stac-examples/20201211_223832_CS2_analytic.tif",
                        "type": "image/tiff; application=geotiff; profile=cloud-optimized",
                        "title": "4-Band Analytic",
                        "roles": ["data"],
                    },
                    "thumbnail": {
                        "href": "https://storage.googleapis.com/open-cogs/stac-examples/20201211_223832_CS2.jpg",
                        "title": "Thumbnail",
                        "type": "image/png",
                        "roles": ["thumbnail"],
                    },
                    "visual": {
                        "href": "https://storage.googleapis.com/open-cogs/stac-examples/20201211_223832_CS2.tif",
                        "type": "image/tiff; application=geotiff; profile=cloud-optimized",
                        "title": "3-Band Visual",
                        "roles": ["visual"],
                    },
                    "udm": {
                        "href": "https://storage.googleapis.com/open-cogs/stac-examples/20201211_223832_CS2_analytic_udm.tif",
                        "title": "Unusable Data Mask",
                        "type": "image/tiff; application=geotiff;",
                    },
                    "json-metadata": {
                        "href": "http://remotedata.io/catalog/20201211_223832_CS2/extended-metadata.json",
                        "title": "Extended Metadata",
                        "type": "application/json",
                        "roles": ["metadata"],
                    },
                    "ephemeris": {
                        "href": "http://cool-sat.com/catalog/20201211_223832_CS2/20201211_223832_CS2.EPH",
                        "title": "Satellite Ephemeris Metadata",
                    },
                },
            },
        ],
        "links": [
            {
                "rel": "next",
                "href": "https://stac-api.example.com/search?page=3",
                "type": "application/geo+json",
            },
            {
                "rel": "prev",
                "href": "https://stac-api.example.com/search?page=1",
                "type": "application/geo+json",
            },
        ],
    }

    stac = stac_validator.StacValidate()
    stac.validate_item_collection_dict(item_collection)
    assert stac.message == [
        {
            "version": "1.0.0",
            "path": None,
            "schema": [
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json"
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
        {
            "version": "1.0.0",
            "path": None,
            "schema": [
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json"
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
    ]


@pytest.mark.network
def test_validate_item_collection_remote():
    stac_file = (
        "https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a/items"
    )
    stac = stac_validator.StacValidate(stac_file, item_collection=True)
    stac.validate_item_collection()

    # Verify we have exactly 10 items
    assert len(stac.message) == 10, f"Expected 10 items, got {len(stac.message)}"

    # Define the base URL for path assertions
    base_url = (
        "https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a/items"
    )

    # Define the expected schema for all items
    expected_schema = [
        "https://cdn.staclint.com/v1.0.0-beta.1/extension/eo.json",
        "https://cdn.staclint.com/v1.0.0-beta.1/extension/view.json",
        "https://cdn.staclint.com/v1.0.0-beta.1/extension/projection.json",
        "https://schemas.stacspec.org/v1.0.0-beta.2/item-spec/json-schema/item.json",
    ]

    # Track which item IDs we've seen
    seen_ids = set()

    # Check each message individually
    for i, msg in enumerate(stac.message):
        # Check basic structure
        assert isinstance(msg, dict), f"Message {i} is not a dictionary"
        assert "path" in msg, f"Message {i} is missing 'path'"
        assert "version" in msg, f"Message {i} is missing 'version'"
        assert "schema" in msg, f"Message {i} is missing 'schema'"
        assert "valid_stac" in msg, f"Message {i} is missing 'valid_stac'"
        assert "asset_type" in msg, f"Message {i} is missing 'asset_type'"
        assert "validation_method" in msg, f"Message {i} is missing 'validation_method'"

        # Check version
        assert (
            msg["version"] == "1.0.0-beta.2"
        ), f"Message {i} has unexpected version: {msg['version']}"

        # Check path format
        assert msg["path"].startswith(
            base_url + "/"
        ), f"Message {i} path does not start with base URL: {msg['path']}"

        # Extract and store the item ID
        start_pos = len(base_url) + 1
        item_id = msg["path"][start_pos:]
        assert item_id not in seen_ids, f"Duplicate item ID found: {item_id}"
        seen_ids.add(item_id)

        # Check schema
        assert (
            msg["schema"] == expected_schema
        ), f"Message {i} has unexpected schema: {msg['schema']}"

        # Check boolean flags
        assert msg["valid_stac"] is True, f"Message {i} has valid_stac=False"

        # Check enums
        assert (
            msg["asset_type"] == "ITEM"
        ), f"Message {i} has unexpected asset_type: {msg['asset_type']}"
        assert (
            msg["validation_method"] == "default"
        ), f"Message {i} has unexpected validation_method: {msg['validation_method']}"

    # Verify we saw all expected item IDs (optional, can be adjusted based on known IDs)
    # This is just an example - you might want to make this more specific
    assert len(seen_ids) == 10, f"Expected 10 unique item IDs, got {len(seen_ids)}"


@pytest.mark.network
def test_validate_item_collection_remote_pages():
    stac_file = "https://stac.geobon.org/collections/chelsa-clim/items"
    stac = stac_validator.StacValidate(stac_file, item_collection=True, pages=2)
    stac.validate_item_collection()

    assert stac.message == [
        {
            "version": "1.0.0",
            "path": "https://stac.geobon.org/collections/chelsa-clim/items/bio9",
            "schema": [
                "https://stac-extensions.github.io/projection/v1.0.0/schema.json",
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json",
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
        {
            "version": "1.0.0",
            "path": "https://stac.geobon.org/collections/chelsa-clim/items/bio8",
            "schema": [
                "https://stac-extensions.github.io/projection/v1.0.0/schema.json",
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json",
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
        {
            "version": "1.0.0",
            "path": "https://stac.geobon.org/collections/chelsa-clim/items/bio7",
            "schema": [
                "https://stac-extensions.github.io/projection/v1.0.0/schema.json",
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json",
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
        {
            "version": "1.0.0",
            "path": "https://stac.geobon.org/collections/chelsa-clim/items/bio6",
            "schema": [
                "https://stac-extensions.github.io/projection/v1.0.0/schema.json",
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json",
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
        {
            "version": "1.0.0",
            "path": "https://stac.geobon.org/collections/chelsa-clim/items/bio5",
            "schema": [
                "https://stac-extensions.github.io/projection/v1.0.0/schema.json",
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json",
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
        {
            "version": "1.0.0",
            "path": "https://stac.geobon.org/collections/chelsa-clim/items/bio4",
            "schema": [
                "https://stac-extensions.github.io/projection/v1.0.0/schema.json",
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json",
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
        {
            "version": "1.0.0",
            "path": "https://stac.geobon.org/collections/chelsa-clim/items/bio3",
            "schema": [
                "https://stac-extensions.github.io/projection/v1.0.0/schema.json",
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json",
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
        {
            "version": "1.0.0",
            "path": "https://stac.geobon.org/collections/chelsa-clim/items/bio2",
            "schema": [
                "https://stac-extensions.github.io/projection/v1.0.0/schema.json",
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json",
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
        {
            "version": "1.0.0",
            "path": "https://stac.geobon.org/collections/chelsa-clim/items/bio19",
            "schema": [
                "https://stac-extensions.github.io/projection/v1.0.0/schema.json",
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json",
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
        {
            "version": "1.0.0",
            "path": "https://stac.geobon.org/collections/chelsa-clim/items/bio18",
            "schema": [
                "https://stac-extensions.github.io/projection/v1.0.0/schema.json",
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json",
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
        {
            "version": "1.0.0",
            "path": "https://stac.geobon.org/collections/chelsa-clim/items/bio17",
            "schema": [
                "https://stac-extensions.github.io/projection/v1.0.0/schema.json",
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json",
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
        {
            "version": "1.0.0",
            "path": "https://stac.geobon.org/collections/chelsa-clim/items/bio16",
            "schema": [
                "https://stac-extensions.github.io/projection/v1.0.0/schema.json",
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json",
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
        {
            "version": "1.0.0",
            "path": "https://stac.geobon.org/collections/chelsa-clim/items/bio15",
            "schema": [
                "https://stac-extensions.github.io/projection/v1.0.0/schema.json",
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json",
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
        {
            "version": "1.0.0",
            "path": "https://stac.geobon.org/collections/chelsa-clim/items/bio14",
            "schema": [
                "https://stac-extensions.github.io/projection/v1.0.0/schema.json",
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json",
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
        {
            "version": "1.0.0",
            "path": "https://stac.geobon.org/collections/chelsa-clim/items/bio13",
            "schema": [
                "https://stac-extensions.github.io/projection/v1.0.0/schema.json",
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json",
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
        {
            "version": "1.0.0",
            "path": "https://stac.geobon.org/collections/chelsa-clim/items/bio12",
            "schema": [
                "https://stac-extensions.github.io/projection/v1.0.0/schema.json",
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json",
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
        {
            "version": "1.0.0",
            "path": "https://stac.geobon.org/collections/chelsa-clim/items/bio11",
            "schema": [
                "https://stac-extensions.github.io/projection/v1.0.0/schema.json",
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json",
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
        {
            "version": "1.0.0",
            "path": "https://stac.geobon.org/collections/chelsa-clim/items/bio10",
            "schema": [
                "https://stac-extensions.github.io/projection/v1.0.0/schema.json",
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json",
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
        {
            "version": "1.0.0",
            "path": "https://stac.geobon.org/collections/chelsa-clim/items/bio1",
            "schema": [
                "https://stac-extensions.github.io/projection/v1.0.0/schema.json",
                "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json",
            ],
            "valid_stac": True,
            "asset_type": "ITEM",
            "validation_method": "default",
        },
    ]


@pytest.mark.network
def test_validate_item_collection_remote_pages_1_v110():
    stac_file = "https://stac.dataspace.copernicus.eu/v1/collections/sentinel-3-olci-2-wfr-nrt/items"
    stac = stac_validator.StacValidate(stac_file, item_collection=True, pages=1)
    stac.validate_item_collection()

    # Check that we got some messages back
    assert len(stac.message) > 0

    # Check each message has the required fields
    required_fields = {
        "version",
        "path",
        "schema",
        "valid_stac",
        "asset_type",
        "validation_method",
    }
    for msg in stac.message:
        # Check all required fields are present
        assert all(
            field in msg for field in required_fields
        ), f"Missing required field in message: {msg}"

        # Check the message is for an ITEM
        assert msg["asset_type"] == "ITEM"

        # Check the validation method is correct
        assert msg["validation_method"] == "default"

        # Check the schema contains expected schemas (checking for a subset to be more resilient)
        expected_schemas = {
            "https://stac-extensions.github.io/eo/v2.0.0/schema.json",
            "https://stac-extensions.github.io/file/v2.1.0/schema.json",
            "https://schemas.stacspec.org/v1.1.0/item-spec/json-schema/item.json",
        }
        assert all(
            schema in msg["schema"] for schema in expected_schemas
        ), f"Missing expected schemas in {msg['schema']}"
    assert len(stac.message) == 10


@pytest.mark.network
def test_validate_item_collection_remote_pages_3_v110():
    stac_file = "https://stac.dataspace.copernicus.eu/v1/collections/sentinel-3-olci-2-wfr-nrt/items"
    stac = stac_validator.StacValidate(stac_file, item_collection=True, pages=3)
    stac.validate_item_collection()
    assert len(stac.message) == 30