|
| 1 | +""" |
| 2 | +Tests for BaseProcessor.build_test_info() method. |
| 3 | +
|
| 4 | +Tests verify wrapper version extraction from test_info file |
| 5 | +and document the current behavior where both version and |
| 6 | +wrapper_version fields are set to the same value. |
| 7 | +""" |
| 8 | + |
| 9 | +import json |
| 10 | +import pytest |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +from chronicler.processors.base_processor import BaseProcessor |
| 14 | +from chronicler.schema import TestInfo |
| 15 | + |
| 16 | +pytestmark = pytest.mark.unit |
| 17 | + |
| 18 | + |
| 19 | +class MinimalProcessor(BaseProcessor): |
| 20 | + """Minimal concrete processor for testing BaseProcessor methods.""" |
| 21 | + |
| 22 | + def get_test_name(self) -> str: |
| 23 | + return "minimal_test" |
| 24 | + |
| 25 | + def parse_runs(self, extracted_result): |
| 26 | + return {} |
| 27 | + |
| 28 | + |
| 29 | +def test_build_test_info_extracts_wrapper_version_from_test_info(tmp_path): |
| 30 | + """ |
| 31 | + Verify build_test_info() extracts wrapper version from test_info file. |
| 32 | +
|
| 33 | + The test_info file contains wrapper repository versions (e.g., "v2.8") |
| 34 | + which should be extracted and set to both version and wrapper_version |
| 35 | + fields (current behavior). |
| 36 | + """ |
| 37 | + # Setup: Create test_info file with wrapper version |
| 38 | + test_info_data = { |
| 39 | + "minimal_test": { |
| 40 | + "test_name": "minimal_test", |
| 41 | + "repo_file": "v2.8.tar.gz" |
| 42 | + } |
| 43 | + } |
| 44 | + test_info_file = tmp_path / "test_info" |
| 45 | + test_info_file.write_text(json.dumps(test_info_data)) |
| 46 | + |
| 47 | + # Execute |
| 48 | + processor = MinimalProcessor(str(tmp_path)) |
| 49 | + result = processor.build_test_info() |
| 50 | + |
| 51 | + # Verify |
| 52 | + assert isinstance(result, TestInfo) |
| 53 | + assert result.name == "minimal_test" |
| 54 | + assert result.version == "v2.8", "Should extract wrapper version from repo_file" |
| 55 | + assert result.wrapper_version == "v2.8", "Should set wrapper_version to same value" |
| 56 | + |
| 57 | + |
| 58 | +def test_build_test_info_returns_unknown_when_no_test_info_file(tmp_path): |
| 59 | + """ |
| 60 | + When test_info file is missing, both version fields should be "unknown". |
| 61 | + """ |
| 62 | + # Execute (no test_info file created) |
| 63 | + processor = MinimalProcessor(str(tmp_path)) |
| 64 | + result = processor.build_test_info() |
| 65 | + |
| 66 | + # Verify |
| 67 | + assert result.version == "unknown" |
| 68 | + assert result.wrapper_version == "unknown" |
| 69 | + |
| 70 | + |
| 71 | +def test_build_test_info_returns_unknown_when_test_not_in_test_info(tmp_path): |
| 72 | + """ |
| 73 | + When test_info exists but doesn't contain the test, return "unknown". |
| 74 | + """ |
| 75 | + # Setup: Create test_info with different test |
| 76 | + test_info_data = { |
| 77 | + "other_test": { |
| 78 | + "test_name": "other_test", |
| 79 | + "repo_file": "v1.0.tar.gz" |
| 80 | + } |
| 81 | + } |
| 82 | + test_info_file = tmp_path / "test_info" |
| 83 | + test_info_file.write_text(json.dumps(test_info_data)) |
| 84 | + |
| 85 | + # Execute |
| 86 | + processor = MinimalProcessor(str(tmp_path)) |
| 87 | + result = processor.build_test_info() |
| 88 | + |
| 89 | + # Verify |
| 90 | + assert result.version == "unknown" |
| 91 | + assert result.wrapper_version == "unknown" |
| 92 | + |
| 93 | + |
| 94 | +def test_build_test_info_handles_malformed_json(tmp_path): |
| 95 | + """ |
| 96 | + Malformed test_info file should log warning and return "unknown". |
| 97 | + """ |
| 98 | + # Setup: Create invalid JSON |
| 99 | + test_info_file = tmp_path / "test_info" |
| 100 | + test_info_file.write_text("{ invalid json }") |
| 101 | + |
| 102 | + # Execute |
| 103 | + processor = MinimalProcessor(str(tmp_path)) |
| 104 | + result = processor.build_test_info() |
| 105 | + |
| 106 | + # Verify |
| 107 | + assert result.version == "unknown" |
| 108 | + assert result.wrapper_version == "unknown" |
| 109 | + |
| 110 | + |
| 111 | +def test_build_test_info_handles_non_string_repo_file(tmp_path): |
| 112 | + """ |
| 113 | + Non-string repo_file values should be handled gracefully. |
| 114 | +
|
| 115 | + If test_info contains valid JSON but repo_file is null, an integer, |
| 116 | + or other non-string value, the processor should handle it gracefully |
| 117 | + rather than raising AttributeError. |
| 118 | + """ |
| 119 | + # Setup: Create test_info with null repo_file |
| 120 | + test_info_data = { |
| 121 | + "minimal_test": { |
| 122 | + "test_name": "minimal_test", |
| 123 | + "repo_file": None |
| 124 | + } |
| 125 | + } |
| 126 | + test_info_file = tmp_path / "test_info" |
| 127 | + test_info_file.write_text(json.dumps(test_info_data)) |
| 128 | + |
| 129 | + # Execute |
| 130 | + processor = MinimalProcessor(str(tmp_path)) |
| 131 | + result = processor.build_test_info() |
| 132 | + |
| 133 | + # Verify - should fall back to "unknown" rather than crash |
| 134 | + assert result.version == "unknown" |
| 135 | + assert result.wrapper_version == "unknown" |
| 136 | + |
| 137 | + |
| 138 | +def test_build_test_info_handles_non_dict_test_info(tmp_path): |
| 139 | + """ |
| 140 | + Non-dict test_info data should be handled gracefully. |
| 141 | +
|
| 142 | + If test_info contains a list or other non-dict structure, |
| 143 | + the processor should handle it gracefully rather than raising |
| 144 | + AttributeError when calling .items(). |
| 145 | + """ |
| 146 | + # Setup: Create test_info with list instead of dict |
| 147 | + test_info_file = tmp_path / "test_info" |
| 148 | + test_info_file.write_text("[]") |
| 149 | + |
| 150 | + # Execute |
| 151 | + processor = MinimalProcessor(str(tmp_path)) |
| 152 | + result = processor.build_test_info() |
| 153 | + |
| 154 | + # Verify - should fall back to "unknown" rather than crash |
| 155 | + assert result.version == "unknown" |
| 156 | + assert result.wrapper_version == "unknown" |
0 commit comments