climate_ref_core.regression.compare
#
Content comparison utilities for regression testing.
Tolerance
#
Float comparison tolerance for bundle regression checks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rtol
|
Relative tolerance — the allowed proportional difference between expected
and actual float values (default |
required | |
atol
|
Absolute tolerance — the floor difference always permitted,
regardless of magnitude (default |
required |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/compare.py
assert_bundle_regression(expected_path, actual_path, *, slug, tol=Tolerance(), replacements)
#
Assert that a regenerated committed-bundle JSON file matches the committed copy.
Algorithm:
- Byte-equal fast path — if the raw bytes of both files are identical, return immediately.
- Parse both files as JSON.
- Rewrite both dict keys and leaf string values in the regenerated
(
actual) document usingreplacements, applied longest-key-first so that a shorter placeholder cannot pre-empt a longer overlapping one. - Call :func:
compare_json_contentwith the given tolerance. - Raise
AssertionErrorwith the full mismatch list and a remediation hint if any mismatches are found.
The replacements map follows the convention used throughout the testing
infrastructure: keys are real runtime values (absolute paths, recipe-dir
timestamps), values are the committed-bundle placeholders (e.g.
"<OUTPUT_DIR>", "<TEST_DATA_DIR>", "<RECIPE_RUN>").
Only the actual document is rewritten: the committed expected file is
assumed to already contain placeholders, which
:func:~climate_ref_core.regression.capture.write_committed_bundle guarantees
at capture time. A hand-edited baseline with raw paths will surface as ordinary
value mismatches.
Both <OUTPUT_DIR> and <RECIPE_RUN> participate in dict-KEY rewriting
because ESMValTool's output.json embeds absolute paths as object keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected_path
|
Path
|
Path to the committed (on-disk) bundle file containing placeholders. |
required |
actual_path
|
Path
|
Path to the regenerated bundle file containing real runtime paths. |
required |
slug
|
str
|
Diagnostic slug used in error messages. |
required |
tol
|
Tolerance
|
Float comparison tolerance. |
Tolerance()
|
replacements
|
dict[str, str]
|
Mapping of |
required |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the bundles differ beyond tolerance after sanitisation. |
Notes
If expected_path does not exist (a legacy regression without a committed bundle),
the comparison is skipped silently and the function returns.
Source code in packages/climate-ref-core/src/climate_ref_core/regression/compare.py
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 | |
compare_json_content(expected, actual, *, tol, path='')
#
Recursively compare two parsed JSON values with float tolerance.
Rules:
- Floats: compared with relative tolerance tol.rtol
and absolute tolerance tol.atol.
- Ints, strings, bools, None: exact equality.
- Lists: element-by-element, same length required.
- Dicts: key sets must match; values compared recursively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected
|
Any
|
The reference (committed) parsed JSON value. |
required |
actual
|
Any
|
The regenerated parsed JSON value. |
required |
tol
|
Tolerance
|
Float comparison tolerance. |
required |
path
|
str
|
Dot-/bracket-notation path prefix for error messages (empty at top level). |
''
|
Returns:
| Type | Description |
|---|---|
list[str]
|
A list of human-readable mismatch descriptions. An empty list means the values are equivalent within tolerance. |