File: test_pystac_client.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 (43 lines) | stat: -rw-r--r-- 1,276 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
# mypy: ignore-errors
import builtins
import sys
from types import ModuleType
from typing import Any

import pytest

real_import = builtins.__import__

try:
    import pystac_client  # noqa:F401

    HAS_PYSTAC_CLIENT = True
except ImportError:
    HAS_PYSTAC_CLIENT = False


@pytest.mark.skipif(not HAS_PYSTAC_CLIENT, reason="requires pystac_client")
def test_import_pystac_client_when_available() -> None:
    from pystac.client import Client

    assert Client.__doc__


# copied from
# http://materials-scientist.com/blog/2021/02/11/mocking-failing-module-import-python/
def monkey_import_importerror(name: str, *args: Any, **kwargs: Any) -> ModuleType:
    if name == "pystac_client":
        raise ImportError(f"No module named '{name}'")
    return real_import(name, *args, **kwargs)


def test_import_pystac_client_when_not_available(monkeypatch: Any) -> None:
    if HAS_PYSTAC_CLIENT:
        monkeypatch.delitem(sys.modules, "pystac_client", raising=False)
        monkeypatch.setattr(builtins, "__import__", monkey_import_importerror)

    with pytest.raises(ImportError):
        from pystac_client import Client  # noqa:F401

    with pytest.raises(ImportError, match="Please install pystac-client:"):
        from pystac.client import Client  # noqa:F401,F811