File: test_pydantic.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 (95 lines) | stat: -rw-r--r-- 3,865 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
"""
Description: Test the validator for Pydantic model validation

"""

import pytest

from stac_validator import stac_validator


def test_pydantic_item_local_v110():
    stac_file = "tests/test_data/v110/simple-item.json"
    stac = stac_validator.StacValidate(stac_file, pydantic=True)
    stac.run()
    assert stac.message[0]["version"] == "1.1.0"
    assert stac.message[0]["path"] == "tests/test_data/v110/simple-item.json"
    assert stac.message[0]["schema"] == ["stac-pydantic Item model"]
    assert stac.message[0]["valid_stac"] is True
    assert stac.message[0]["asset_type"] == "ITEM"
    assert stac.message[0]["validation_method"] == "pydantic"
    assert stac.message[0]["model_validation"] == "passed"


@pytest.mark.network
def test_pydantic_collection_local_v110():
    stac_file = "tests/test_data/v110/collection.json"
    stac = stac_validator.StacValidate(stac_file, pydantic=True)
    stac.run()
    assert stac.message[0]["version"] == "1.1.0"
    assert stac.message[0]["path"] == "tests/test_data/v110/collection.json"
    assert stac.message[0]["schema"] == ["stac-pydantic Collection model"]
    assert stac.message[0]["valid_stac"] is True
    assert stac.message[0]["asset_type"] == "COLLECTION"
    assert stac.message[0]["validation_method"] == "pydantic"
    assert "extension_schemas" in stac.message[0]
    assert stac.message[0]["model_validation"] == "passed"


# Find a catalog file in v100 directory since v110 doesn't have one
def test_pydantic_catalog_local_v100():
    stac_file = "tests/test_data/v100/catalog.json"
    stac = stac_validator.StacValidate(stac_file, pydantic=True)
    stac.run()
    assert stac.message[0]["version"] == "1.0.0"
    assert stac.message[0]["path"] == "tests/test_data/v100/catalog.json"
    assert stac.message[0]["schema"] == ["stac-pydantic Catalog model"]
    assert stac.message[0]["valid_stac"] is True
    assert stac.message[0]["asset_type"] == "CATALOG"
    assert stac.message[0]["validation_method"] == "pydantic"
    assert stac.message[0]["model_validation"] == "passed"


def test_pydantic_invalid_item():
    # Test with a file that should fail Pydantic validation
    stac_file = "tests/test_data/bad_data/bad_item_v090.json"
    stac = stac_validator.StacValidate(stac_file, pydantic=True)
    stac.run()
    assert stac.message[0]["version"] == "0.9.0"
    assert stac.message[0]["path"] == "tests/test_data/bad_data/bad_item_v090.json"
    assert stac.message[0]["valid_stac"] is False
    assert stac.message[0]["asset_type"] == "ITEM"
    assert stac.message[0]["validation_method"] == "pydantic"
    assert "error_type" in stac.message[0]
    assert "error_message" in stac.message[0]


def test_pydantic_recursive():
    """Test pydantic validation in recursive mode."""
    stac_file = "tests/test_data/local_cat/example-catalog/catalog.json"
    stac = stac_validator.StacValidate(
        stac_file, pydantic=True, recursive=True, max_depth=2  # Limit depth for testing
    )
    stac.run()

    # Check that we have validation messages
    assert len(stac.message) > 0

    # Check each validation message
    for msg in stac.message:
        # Check that validator_engine is set to pydantic
        assert msg["validator_engine"] == "pydantic"

        # Check that validation_method is recursive
        assert msg["validation_method"] == "recursive"

        # Check that valid_stac is a boolean
        assert isinstance(msg["valid_stac"], bool)

        # Check that schema is set to pydantic model based on asset type
        if msg["asset_type"] == "ITEM":
            assert msg["schema"] == ["stac-pydantic Item model"]
        elif msg["asset_type"] == "COLLECTION":
            assert msg["schema"] == ["stac-pydantic Collection model"]
        elif msg["asset_type"] == "CATALOG":
            assert msg["schema"] == ["stac-pydantic Catalog model"]