-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_validate.py
More file actions
291 lines (224 loc) · 9.25 KB
/
test_validate.py
File metadata and controls
291 lines (224 loc) · 9.25 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# test_validate.py
# ─────────────────────────────────────────────────────────────────────────────
# Tests for the validation logic in validate.py.
#
# These tests verify that the PROGRAM behaves correctly — they do not check
# your actual metadata.py or schema.py data.
#
# When to run:
# Only if you modify validate.py or pack_bundle.py.
# Not needed for normal daily workflow (pack_bundle.py handles that).
#
# How to run:
# python test_validate.py
# ─────────────────────────────────────────────────────────────────────────────
import sys
import tempfile
from pathlib import Path
import validate as v
# ── Minimal test runner (no pytest needed) ────────────────────────────────────
passed = 0
failed = 0
def expect_errors(label, errors, expected_fragment):
"""Assert that at least one error contains the expected fragment."""
global passed, failed
matches = [e for e in errors if expected_fragment in e]
if matches:
print(f" ✓ {label}")
passed += 1
else:
print(f" ✗ {label}")
print(f" Expected fragment: '{expected_fragment}'")
print(f" Actual errors: {errors if errors else '(none)'}")
failed += 1
def expect_no_errors(label, errors):
"""Assert that the error list is empty."""
global passed, failed
if not errors:
print(f" ✓ {label}")
passed += 1
else:
print(f" ✗ {label}")
print(f" Expected no errors, got: {errors}")
failed += 1
def expect_warnings(label, warnings, expected_fragment):
global passed, failed
matches = [w for w in warnings if expected_fragment in w]
if matches:
print(f" ✓ {label}")
passed += 1
else:
print(f" ✗ {label}")
print(f" Expected fragment: '{expected_fragment}'")
print(f" Actual warnings: {warnings if warnings else '(none)'}")
failed += 1
# ── Good base fixtures ────────────────────────────────────────────────────────
GOOD_COLUMNS = [
{"key": "name", "label": "Document name", "required": True, "type": "text"},
{"key": "status", "label": "Status", "required": True, "type": "text"},
{"key": "change_date", "label": "Change date", "required": True, "type": "date"},
{"key": "version", "label": "Version", "required": False, "type": "version"},
]
GOOD_FILES = [
{"filename": "report.pdf", "name": "Report", "status": "Approved", "change_date": "2025-01-15", "version": "1.0"},
{"filename": "drawing.dwg", "name": "Drawing", "status": "Draft", "change_date": "2025-03-01", "version": "0.2"},
]
# ── Tests: schema structure ───────────────────────────────────────────────────
print("\nschema structure")
expect_no_errors(
"valid schema passes cleanly",
v.check_schema_structure(GOOD_COLUMNS)
)
expect_errors(
"schema entry missing 'key' is caught",
v.check_schema_structure([{"label": "X", "required": True, "type": "text"}]),
"missing field 'key'"
)
expect_errors(
"schema entry missing 'type' is caught",
v.check_schema_structure([{"key": "x", "label": "X", "required": True}]),
"missing field 'type'"
)
expect_errors(
"invalid type value is caught",
v.check_schema_structure([{"key": "x", "label": "X", "required": True, "type": "number"}]),
"must be one of"
)
expect_errors(
"'required' must be bool not string",
v.check_schema_structure([{"key": "x", "label": "X", "required": "yes", "type": "text"}]),
"'required' must be True or False"
)
# ── Tests: schema duplicate keys ─────────────────────────────────────────────
print("\nschema duplicate keys")
expect_no_errors(
"unique keys pass",
v.check_schema_no_duplicate_keys(GOOD_COLUMNS)
)
expect_errors(
"duplicate key is caught",
v.check_schema_no_duplicate_keys([
{"key": "name", "label": "A", "required": True, "type": "text"},
{"key": "name", "label": "B", "required": True, "type": "text"},
]),
"duplicate key 'name'"
)
# ── Tests: metadata filename ──────────────────────────────────────────────────
print("\nmetadata filename")
expect_no_errors(
"valid filenames pass",
v.check_metadata_has_filename(GOOD_FILES)
)
expect_errors(
"missing filename is caught",
v.check_metadata_has_filename([{"name": "X", "status": "Draft"}]),
"missing 'filename'"
)
expect_errors(
"empty filename string is caught",
v.check_metadata_has_filename([{"filename": " "}]),
"non-empty string"
)
# ── Tests: duplicate filenames ────────────────────────────────────────────────
print("\nduplicate filenames")
expect_no_errors(
"unique filenames pass",
v.check_metadata_no_duplicate_filenames(GOOD_FILES)
)
expect_errors(
"duplicate filename is caught",
v.check_metadata_no_duplicate_filenames([
{"filename": "a.pdf"},
{"filename": "a.pdf"},
]),
"duplicate filename 'a.pdf'"
)
# ── Tests: required fields ────────────────────────────────────────────────────
print("\nrequired fields")
expect_no_errors(
"all required fields present passes",
v.check_required_fields_present(GOOD_FILES, GOOD_COLUMNS)
)
expect_errors(
"missing required field is caught",
v.check_required_fields_present(
[{"filename": "x.pdf", "name": "X", "change_date": "2025-01-01"}],
GOOD_COLUMNS
),
"missing required field 'status'"
)
# ── Tests: extra keys ─────────────────────────────────────────────────────────
print("\nextra keys")
expect_no_errors(
"no extra keys → no warnings",
v.check_no_extra_keys(GOOD_FILES, GOOD_COLUMNS)
)
expect_warnings(
"extra key produces a warning",
v.check_no_extra_keys(
[{"filename": "x.pdf", "name": "X", "status": "Draft",
"change_date": "2025-01-01", "mystery_field": "???"}],
GOOD_COLUMNS
),
"mystery_field"
)
# ── Tests: date format ────────────────────────────────────────────────────────
print("\ndate format")
expect_no_errors(
"valid date passes",
v.check_date_format(GOOD_FILES, GOOD_COLUMNS)
)
expect_errors(
"wrong date format is caught",
v.check_date_format(
[{"filename": "x.pdf", "change_date": "15-01-2025"}],
GOOD_COLUMNS
),
"not a valid date"
)
expect_errors(
"non-date string is caught",
v.check_date_format(
[{"filename": "x.pdf", "change_date": "January 2025"}],
GOOD_COLUMNS
),
"not a valid date"
)
# ── Tests: files exist in input/ ─────────────────────────────────────────────
print("\nfiles exist in input/")
with tempfile.TemporaryDirectory() as tmpdir:
tmp = Path(tmpdir)
(tmp / "present.pdf").write_bytes(b"%PDF-1.4")
expect_no_errors(
"existing file passes",
v.check_files_exist_in_input([{"filename": "present.pdf"}], tmp)
)
expect_errors(
"missing file is caught",
v.check_files_exist_in_input([{"filename": "ghost.pdf"}], tmp),
"not found in"
)
# ── Tests: orphan files in input/ ────────────────────────────────────────────
print("\norphan files in input/")
with tempfile.TemporaryDirectory() as tmpdir:
tmp = Path(tmpdir)
(tmp / "orphan.pdf").write_bytes(b"%PDF-1.4")
expect_warnings(
"file in input/ with no metadata entry produces warning",
v.check_all_input_files_have_metadata([], tmp),
"orphan.pdf"
)
expect_no_errors(
"declared file does not produce orphan warning",
v.check_all_input_files_have_metadata([{"filename": "orphan.pdf"}], tmp)
)
# ── Summary ───────────────────────────────────────────────────────────────────
total = passed + failed
print(f"\n{'─'*50}")
print(f" {passed}/{total} tests passed", end="")
if failed:
print(f" — {failed} FAILED")
sys.exit(1)
else:
print(" — all good.")
sys.exit(0)