File: test_html.py

package info (click to toggle)
pystac 1.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 19,904 kB
  • sloc: python: 24,370; makefile: 124; sh: 7
file content (98 lines) | stat: -rw-r--r-- 2,783 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
import sys
from typing import Any

import html5lib
import pytest
from pytest_mock import MockerFixture

import pystac
from pystac.html.jinja_env import get_jinja_env
from tests.utils import TestCases


def parse_html(stac_html: str) -> None:
    html5lib.HTMLParser(strict=True).parse("<!DOCTYPE html>\n" + stac_html)


@pytest.mark.parametrize(
    "path,cls",
    [
        ("data-files/item/sample-item.json", pystac.Item),
        (
            "data-files/catalogs/test-case-1/country-1/area-1-1/collection.json",
            pystac.Collection,
        ),
        ("data-files/catalogs/test-case-1/catalog.json", pystac.Catalog),
        (
            "data-files/item-collection/sample-item-collection.json",
            pystac.ItemCollection,
        ),
    ],
)
def test_from_file_valid_html(path: str, cls: Any) -> None:
    href = TestCases.get_path(path)
    obj = cls.from_file(href)
    parse_html(obj._repr_html_())


@pytest.mark.parametrize(
    "path,cls",
    [
        ("data-files/item/sample-item.json", pystac.Item),
        (
            "data-files/catalogs/test-case-1/country-1/area-1-1/collection.json",
            pystac.Collection,
        ),
        ("data-files/catalogs/test-case-1/catalog.json", pystac.Catalog),
        (
            "data-files/item-collection/sample-item-collection.json",
            pystac.ItemCollection,
        ),
    ],
)
def test_from_file_missing_jinja2(mocker: MockerFixture, path: str, cls: Any) -> None:
    get_jinja_env.cache_clear()

    mocker.patch.dict(sys.modules, {"jinja2": None})
    assert get_jinja_env() is None

    href = TestCases.get_path(path)
    obj = cls.from_file(href)
    assert cls.__name__ in obj._repr_html_()

    get_jinja_env.cache_clear()


def test_nested_objects_valid_html() -> None:
    href = TestCases.get_path("data-files/item/sample-item-asset-properties.json")
    item = pystac.Item.from_file(href)

    link = item.links[0]
    parse_html(link._repr_html_())

    asset = item.assets["thumbnail"]
    parse_html(asset._repr_html_())

    provider = (asset.common_metadata.providers or [])[0]
    parse_html(provider._repr_html_())


def test_nested_objects_missing_jinja2(mocker: MockerFixture) -> None:
    get_jinja_env.cache_clear()

    mocker.patch.dict(sys.modules, {"jinja2": None})
    assert get_jinja_env() is None

    href = TestCases.get_path("data-files/item/sample-item-asset-properties.json")
    item = pystac.Item.from_file(href)

    link = item.links[0]
    assert pystac.Link.__name__ in link._repr_html_()

    asset = item.assets["thumbnail"]
    assert pystac.Asset.__name__ in asset._repr_html_()

    provider = (asset.common_metadata.providers or [])[0]
    assert pystac.Provider.__name__ in provider._repr_html_()

    get_jinja_env.cache_clear()