|
| 1 | +import contextlib |
| 2 | +import copy |
1 | 3 | import unittest |
2 | 4 | import os |
3 | 5 | import shutil |
| 6 | + |
4 | 7 | from hed import load_schema_version |
5 | 8 | from hed.scripts.schema_script_util import ( |
6 | 9 | add_extension, |
7 | 10 | sort_base_schemas, |
8 | 11 | validate_all_schema_formats, |
9 | 12 | validate_schema, |
| 13 | + validate_schema_object, |
10 | 14 | validate_all_schemas, |
11 | 15 | ) |
12 | | -import contextlib |
13 | 16 |
|
14 | 17 |
|
15 | 18 | class TestAddExtension(unittest.TestCase): |
@@ -265,3 +268,53 @@ def test_uppercase_extension_policy_enforcement(self): |
265 | 268 | # Clean up |
266 | 269 | if os.path.exists(uppercase_file): |
267 | 270 | os.remove(uppercase_file) |
| 271 | + |
| 272 | + |
| 273 | +class TestCheckWarnings(unittest.TestCase): |
| 274 | + """Tests for the check_warnings parameter in validate_schema_object and validate_schema.""" |
| 275 | + |
| 276 | + @classmethod |
| 277 | + def setUpClass(cls): |
| 278 | + clean = load_schema_version("8.3.0") |
| 279 | + cls.clean_schema = clean |
| 280 | + # Deep-copy so the cached shared instance is not mutated. |
| 281 | + # Setting version to a future value triggers SCHEMA_PRERELEASE_VERSION_USED (warning only). |
| 282 | + cls.warning_schema = copy.deepcopy(clean) |
| 283 | + cls.warning_schema.header_attributes["version"] = "999.0.0" |
| 284 | + |
| 285 | + def test_clean_schema_check_warnings_false(self): |
| 286 | + """A fully compliant schema produces no issues with check_warnings=False.""" |
| 287 | + with contextlib.redirect_stdout(None): |
| 288 | + issues = validate_schema_object(self.clean_schema, "test", check_warnings=False) |
| 289 | + self.assertEqual(issues, []) |
| 290 | + |
| 291 | + def test_clean_schema_check_warnings_true(self): |
| 292 | + """A fully compliant schema produces no issues with check_warnings=True.""" |
| 293 | + with contextlib.redirect_stdout(None): |
| 294 | + issues = validate_schema_object(self.clean_schema, "test", check_warnings=True) |
| 295 | + self.assertEqual(issues, []) |
| 296 | + |
| 297 | + def test_warning_schema_check_warnings_true_reports_issues(self): |
| 298 | + """A prerelease version generates a warning that is reported when check_warnings=True.""" |
| 299 | + with contextlib.redirect_stdout(None): |
| 300 | + issues = validate_schema_object(self.warning_schema, "test", check_warnings=True) |
| 301 | + self.assertTrue(issues, "Expected at least one issue for prerelease version warning") |
| 302 | + |
| 303 | + def test_warning_schema_check_warnings_false_suppresses_warnings(self): |
| 304 | + """Warnings are suppressed and validation passes when check_warnings=False.""" |
| 305 | + with contextlib.redirect_stdout(None): |
| 306 | + issues = validate_schema_object(self.warning_schema, "test", check_warnings=False) |
| 307 | + self.assertEqual(issues, []) |
| 308 | + |
| 309 | + def test_validate_schema_default_is_warnings_false(self): |
| 310 | + """validate_schema default check_warnings=False matches explicit False.""" |
| 311 | + schema_path = os.path.join( |
| 312 | + os.path.dirname(os.path.dirname(__file__)), |
| 313 | + "data", |
| 314 | + "schema_tests", |
| 315 | + "HED8.2.0.mediawiki", |
| 316 | + ) |
| 317 | + with contextlib.redirect_stdout(None): |
| 318 | + default_issues = validate_schema(schema_path) |
| 319 | + explicit_issues = validate_schema(schema_path, check_warnings=False) |
| 320 | + self.assertEqual(default_issues, explicit_issues) |
0 commit comments