Perform approximate comparisons where the expected value is a Decimal.
| 538 | |
| 539 | |
| 540 | class ApproxDecimal(ApproxScalar): |
| 541 | """Perform approximate comparisons where the expected value is a Decimal.""" |
| 542 | |
| 543 | DEFAULT_ABSOLUTE_TOLERANCE = Decimal("1e-12") |
| 544 | DEFAULT_RELATIVE_TOLERANCE = Decimal("1e-6") |
| 545 | |
| 546 | def __repr__(self) -> str: |
| 547 | if isinstance(self.rel, float): |
| 548 | rel = Decimal.from_float(self.rel) |
| 549 | else: |
| 550 | rel = self.rel |
| 551 | |
| 552 | if isinstance(self.abs, float): |
| 553 | abs_ = Decimal.from_float(self.abs) |
| 554 | else: |
| 555 | abs_ = self.abs |
| 556 | |
| 557 | tol_str = "???" |
| 558 | if rel is not None and Decimal("1e-3") <= rel <= Decimal("1e3"): |
| 559 | tol_str = f"{rel:.1e}" |
| 560 | elif abs_ is not None: |
| 561 | tol_str = f"{abs_:.1e}" |
| 562 | |
| 563 | return f"{self.expected} ± {tol_str}" |
| 564 | |
| 565 | |
| 566 | class ApproxTimedelta(ApproxBase): |