| junit_common.py (694f24b00a5ec783a2e8f60ab930eea7b6a5ea1d) | junit_common.py (69ef23b6c4e7d5a45728d9ea1580ce79ef1002a6) |
|---|---|
| 1from abc import ABC, abstractmethod 2import argparse | 1from abc import ABC, abstractmethod 2import argparse |
| 3import csv |
|
| 3from dataclasses import dataclass, field 4import difflib 5from enum import Enum 6from math import isclose 7import os 8from pathlib import Path 9import re 10import subprocess --- 250 unchanged lines hidden (view full) --- 261 zero_tol (float, optional): Tolerance below which values are considered to be zero. Defaults to 3e-10. 262 rel_tol (float, optional): Relative tolerance for comparing non-zero values. Defaults to 1e-2. 263 264 Returns: 265 str: Diff output between result and expected CSVs 266 """ 267 test_lines: List[str] = test_csv.read_text().splitlines() 268 true_lines: List[str] = true_csv.read_text().splitlines() | 4from dataclasses import dataclass, field 5import difflib 6from enum import Enum 7from math import isclose 8import os 9from pathlib import Path 10import re 11import subprocess --- 250 unchanged lines hidden (view full) --- 262 zero_tol (float, optional): Tolerance below which values are considered to be zero. Defaults to 3e-10. 263 rel_tol (float, optional): Relative tolerance for comparing non-zero values. Defaults to 1e-2. 264 265 Returns: 266 str: Diff output between result and expected CSVs 267 """ 268 test_lines: List[str] = test_csv.read_text().splitlines() 269 true_lines: List[str] = true_csv.read_text().splitlines() |
| 270 # Files should not be empty 271 if len(test_lines) == 0: 272 return f'No lines found in test output {test_csv}' 273 if len(true_lines) == 0: 274 return f'No lines found in test source {true_csv}' |
|
| 269 | 275 |
| 270 if test_lines[0] != true_lines[0]: | 276 test_reader: csv.DictReader = csv.DictReader(test_lines) 277 true_reader: csv.DictReader = csv.DictReader(true_lines) 278 if test_reader.fieldnames != true_reader.fieldnames: |
| 271 return ''.join(difflib.unified_diff([f'{test_lines[0]}\n'], [f'{true_lines[0]}\n'], 272 tofile='found CSV columns', fromfile='expected CSV columns')) 273 | 279 return ''.join(difflib.unified_diff([f'{test_lines[0]}\n'], [f'{true_lines[0]}\n'], 280 tofile='found CSV columns', fromfile='expected CSV columns')) 281 |
| 282 if len(test_lines) != len(true_lines): 283 return f'Number of lines in {test_csv} and {true_csv} do not match' |
|
| 274 diff_lines: List[str] = list() | 284 diff_lines: List[str] = list() |
| 275 column_names: List[str] = true_lines[0].strip().split(',') 276 for test_line, true_line in zip(test_lines[1:], true_lines[1:]): 277 test_vals: List[float] = [float(val.strip()) for val in test_line.strip().split(',')] 278 true_vals: List[float] = [float(val.strip()) for val in true_line.strip().split(',')] 279 for test_val, true_val, column_name in zip(test_vals, true_vals, column_names): 280 true_zero: bool = abs(true_val) < zero_tol 281 test_zero: bool = abs(test_val) < zero_tol 282 fail: bool = False 283 if true_zero: 284 fail = not test_zero 285 else: 286 fail = not isclose(test_val, true_val, rel_tol=rel_tol) 287 if fail: 288 diff_lines.append(f'step: {true_line[0]}, column: {column_name}, expected: {true_val}, got: {test_val}') | 285 for test_line, true_line in zip(test_reader, true_reader): 286 for key in test_reader.fieldnames: 287 # Check if the value is numeric 288 try: 289 true_val: float = float(true_line[key]) 290 test_val: float = float(test_line[key]) 291 true_zero: bool = abs(true_val) < zero_tol 292 test_zero: bool = abs(test_val) < zero_tol 293 fail: bool = False 294 if true_zero: 295 fail = not test_zero 296 else: 297 fail = not isclose(test_val, true_val, rel_tol=rel_tol) 298 if fail: 299 diff_lines.append(f'column: {key}, expected: {true_val}, got: {test_val}') 300 except ValueError: 301 if test_line[key] != true_line[key]: 302 diff_lines.append(f'column: {key}, expected: {true_line[key]}, got: {test_line[key]}') 303 |
| 289 return '\n'.join(diff_lines) 290 291 292def diff_cgns(test_cgns: Path, true_cgns: Path, tolerance: float = 1e-12) -> str: 293 """Compare CGNS results against an expected CGSN file with tolerance 294 295 Args: 296 test_cgns (Path): Path to output CGNS file --- 268 unchanged lines hidden --- | 304 return '\n'.join(diff_lines) 305 306 307def diff_cgns(test_cgns: Path, true_cgns: Path, tolerance: float = 1e-12) -> str: 308 """Compare CGNS results against an expected CGSN file with tolerance 309 310 Args: 311 test_cgns (Path): Path to output CGNS file --- 268 unchanged lines hidden --- |