-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_catalog_fetch.py
More file actions
124 lines (95 loc) · 3.91 KB
/
Copy pathtest_catalog_fetch.py
File metadata and controls
124 lines (95 loc) · 3.91 KB
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
"""Catalog fetch tests."""
from __future__ import annotations
import json
from pathlib import Path
from unittest.mock import patch
import pytest
from create_awesome_python_app.catalog import (
DEFAULT_CATALOG_URL,
catalog_cache_path,
catalog_url,
get_catalog_data,
reset_catalog_cache_for_tests,
reset_fixture_root_for_tests,
resolve_fixture_root,
set_fixture_root_for_tests,
)
FIXTURE_PATH = (
Path(__file__).resolve().parents[3] / "fixtures" / "catalog" / "templates.json"
)
@pytest.fixture(autouse=True)
def _reset_cache(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
reset_catalog_cache_for_tests()
reset_fixture_root_for_tests()
monkeypatch.setenv("CPA_CACHE_DIR", str(tmp_path / "cache"))
monkeypatch.delenv("CPA_FIXTURE_DIR", raising=False)
monkeypatch.delenv("CPA_CATALOG_FIXTURE", raising=False)
yield
reset_fixture_root_for_tests()
reset_catalog_cache_for_tests()
def test_default_catalog_url(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("CPA_CATALOG_URL", raising=False)
assert "Create-Python-App/cpa-templates" in catalog_url()
monkeypatch.setenv("CPA_CATALOG_URL", "https://example.com/templates.json")
assert catalog_url() == "https://example.com/templates.json"
def test_get_catalog_data_fetches_and_caches(tmp_path: Path) -> None:
payload = json.loads(FIXTURE_PATH.read_text(encoding="utf-8"))
class FakeResponse:
def read(self) -> bytes:
return json.dumps(payload).encode("utf-8")
def __enter__(self) -> FakeResponse:
return self
def __exit__(self, *args: object) -> None:
return None
with patch(
"create_awesome_python_app.catalog.urllib.request.urlopen",
return_value=FakeResponse(),
):
data = get_catalog_data(force_refresh=True)
assert any(t["slug"] == "fastapi-starter" for t in data["templates"])
assert catalog_cache_path().is_file()
def test_get_catalog_data_fixture_fallback(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setenv("CPA_CATALOG_FIXTURE", "1")
data = get_catalog_data(force_refresh=True)
assert data["templates"]
def test_get_catalog_data_disk_fallback_on_network_error(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
payload = {"templates": [{"slug": "cached"}], "extensions": [], "categories": []}
cache_file = catalog_cache_path()
cache_file.parent.mkdir(parents=True, exist_ok=True)
cache_file.write_text(json.dumps(payload), encoding="utf-8")
with patch(
"create_awesome_python_app.catalog._fetch_remote",
side_effect=OSError("network down"),
):
data = get_catalog_data(force_refresh=True)
assert data["templates"][0]["slug"] == "cached"
def test_default_url_points_to_cpa_templates() -> None:
assert DEFAULT_CATALOG_URL.endswith("/cpa-templates/main/templates.json")
def test_resolve_fixture_root_respects_env(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setenv("CPA_FIXTURE_DIR", str(tmp_path))
assert resolve_fixture_root() == tmp_path.resolve()
def test_get_catalog_data_fixture_uses_custom_dir(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
catalog_dir = tmp_path / "fixtures" / "catalog"
catalog_dir.mkdir(parents=True)
payload = {
"templates": [{"slug": "from-env-fixture"}],
"extensions": [],
"categories": [],
}
(catalog_dir / "templates.json").write_text(json.dumps(payload), encoding="utf-8")
monkeypatch.setenv("CPA_CATALOG_FIXTURE", "1")
monkeypatch.setenv("CPA_FIXTURE_DIR", str(tmp_path))
data = get_catalog_data(force_refresh=True)
assert data["templates"][0]["slug"] == "from-env-fixture"
def test_set_fixture_root_for_tests(tmp_path: Path) -> None:
set_fixture_root_for_tests(tmp_path)
assert resolve_fixture_root() == tmp_path
reset_fixture_root_for_tests()