forked from NOAA-OWP/hydrotools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_build_constants.py
More file actions
186 lines (158 loc) · 5.66 KB
/
test_build_constants.py
File metadata and controls
186 lines (158 loc) · 5.66 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
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
"""Test the build_constants stand-alone script."""
import pytest
from unittest.mock import patch
from click.testing import CliRunner
from typing import Any
from build_constants import get_template_data, write_constants_module
@pytest.fixture
def mock_template() -> str:
"""Mock test template."""
return """
from enum import StrEnum
class USGSCollection(StrEnum):
{% for item in collections %}
{{ item.enum_member }} = "{{ item.value }}"
{% endfor %}
"""
@pytest.fixture
def mock_schema() -> dict[str, Any]:
"""Mock test schema."""
return {
"paths": {
"/collections/monitoring-locations/items": {"get": {}},
"/collections/daily/items": {"get": {}},
"/conformance": {"get": {}}
}
}
@pytest.fixture
def bad_schema_special() -> dict[str, Any]:
"""Bad test schema with special characters."""
return {
"paths": {
"/collections/@@$$%%/items": {"get": {}},
"/collections/daily/items": {"get": {}},
"/conformance": {"get": {}}
}
}
@pytest.fixture
def bad_schema_digits() -> dict[str, Any]:
"""Bad test schema with initial digit."""
return {
"paths": {
"/collections/123/items": {"get": {}},
"/collections/daily/items": {"get": {}},
"/conformance": {"get": {}}
}
}
def test_get_template_data(mock_schema):
"""Verify extraction of collections."""
data = get_template_data(mock_schema)
assert len(data) == 2
assert data[0]["value"] == "daily"
assert data[1]["enum_member"] == "MONITORING_LOCATIONS"
def test_cli_output_to_stdout(mock_schema, mock_template, tmp_path):
"""Verify CLI."""
# Setup template file
template_directory = tmp_path / "templates"
template_directory.mkdir()
template_name = "constants.py.j2"
template_file = template_directory / template_name
template_file.write_text(mock_template)
# Patch and run CLI
with patch("build_constants.get_schema") as mock_get_schema:
mock_get_schema.return_value = mock_schema
runner = CliRunner()
result = runner.invoke(
write_constants_module,
[str(template_directory), "--name", template_name]
)
assert result.exit_code == 0
assert "class USGSCollection" in result.output
assert "daily" in result.output
assert "MONITORING_LOCATIONS" in result.output
def test_cli_overwrite_error(mock_schema, mock_template, tmp_path):
"""Verify CLI."""
# Setup template file
template_directory = tmp_path / "templates"
template_directory.mkdir()
template_name = "constants.py.j2"
template_file = template_directory / template_name
template_file.write_text(mock_template)
output_file = template_directory / "constant.py"
output_file.touch()
# Patch and run CLI
with patch("build_constants.get_schema") as mock_get_schema:
mock_get_schema.return_value = mock_schema
runner = CliRunner()
result = runner.invoke(
write_constants_module,
[
str(template_directory),
"--name",
template_name,
"--output",
str(output_file)
]
)
assert result.exit_code == 1
assert isinstance(result.exception, FileExistsError)
def test_cli_overwrite(mock_schema, mock_template, tmp_path):
"""Verify CLI."""
# Setup template file
template_directory = tmp_path / "templates"
template_directory.mkdir()
template_name = "constants.py.j2"
template_file = template_directory / template_name
template_file.write_text(mock_template)
output_file = template_directory / "constant.py"
output_file.touch()
# Patch and run CLI
with patch("build_constants.get_schema") as mock_get_schema:
mock_get_schema.return_value = mock_schema
runner = CliRunner()
result = runner.invoke(
write_constants_module,
[
str(template_directory),
"--name",
template_name,
"--output",
str(output_file),
"--overwrite"
]
)
assert result.exit_code == 0
def test_bad_schema_special(bad_schema_special):
"""Verify raises SyntaxError."""
with pytest.raises(SyntaxError):
data = get_template_data(bad_schema_special)
def test_bad_schema_digits(bad_schema_digits):
"""Verify raises SyntaxError."""
with pytest.raises(SyntaxError):
data = get_template_data(bad_schema_digits)
def test_ignore_bad_schema(bad_schema_special):
"""Verify extraction of collections."""
data = get_template_data(bad_schema_special, ignore_errors=True)
assert len(data) == 1
assert data[0]["value"] == "daily"
assert data[0]["enum_member"] == "DAILY"
def test_cli_ignore_errors(bad_schema_digits, mock_template, tmp_path):
"""Verify CLI."""
# Setup template file
template_directory = tmp_path / "templates"
template_directory.mkdir()
template_name = "constants.py.j2"
template_file = template_directory / template_name
template_file.write_text(mock_template)
# Patch and run CLI
with patch("build_constants.get_schema") as mock_get_schema:
mock_get_schema.return_value = bad_schema_digits
runner = CliRunner()
result = runner.invoke(
write_constants_module,
[str(template_directory), "--name", template_name, "--ignore-errors"],
)
assert result.exit_code == 0
assert "class USGSCollection" in result.output
assert "daily" in result.output
assert "DAILY" in result.output