Files
Jonathon Broughton 168a1f517a
build and deploy Speckle functions / publish-automate-function-version (push) Has been cancelled
Robust rule validation and UX (#36)
* Add minimum severity and hide skipped options

- Introduced `minimum_severity` to filter reported results.
- Added `hide_skipped` option to control visibility of skipped tests.
- Updated rule application logic to respect new parameters.

* Improve rule processing and validation

- Enhanced rule reading to group rules and handle validation messages.
- Added functions to process and validate rule numbers in the spreadsheet.
- Improved error handling when reading TSV files.

* Improve docstrings and clean up code

- Added docstring for `PropertyMatchMode` enum.
- Removed unnecessary blank lines in `FunctionInputs`.
- Commented out unused `property_match_mode` field with explanation.

* Improve rule validation logic

Added a new function to validate the structure of rule groups. Key updates include:
- Checks for required columns and conditions.
- Ensures rules start with "WHERE" and have at most one "CHECK".
- Validates that "CHECK" is the last condition if present.
- Raises descriptive errors for invalid structures.

* Refactor rule processing logic

- Added a new function to separate filters and final checks.
- Simplified condition evaluation by using the new function.
- Improved handling of empty rule groups and speckle objects.
- Enhanced clarity in filtering logic for better maintainability.

* Refactor number conversion logic in comparison

- Updated boolean comparison method call for clarity.
- Introduced a helper function to safely convert strings to numbers, handling whitespace and negative values.
- Simplified the type-checking process for string-to-float conversion.

* Update comparison logic in parameter checks

Flipped the comparison logic to match user expectations for parameter value checks. Updated docstrings for better clarity on UX perspective. Added error handling for non-numeric values to improve robustness.

* Refactor method names for clarity

Updated method names to remove leading underscores for better readability and consistency. Added a docstring to describe the purpose of the rules module.

* Add tests for comparison and rule processing

- Introduced a new test suite for value comparisons, covering numeric strings, boolean strings, case sensitivity in string comparisons, and float comparison with tolerance.
- Created a test suite for parameter handling functionality to validate the existence and retrieval of parameters in v2 and v3 objects.
- Added tests for rule processing functionality using both explicit CHECK format and legacy format rules.
2025-02-18 18:56:02 +00:00

87 lines
2.9 KiB
Python

from typing import Any
import pytest
from src.rules import PropertyRules
class TestValueComparison:
"""Test suite for value comparison functionality."""
@pytest.mark.parametrize(
"value1, value2",
[
# Basic numeric strings
("1400", 1400.0),
("1400.0", 1400),
("1400.00", 1400),
# Whitespace handling
(" 1400 ", 1400.0),
(" 1400 ", 1400.0),
("\t1400\n", 1400.0),
# Negative numbers
("-1400", -1400.0),
(" -1400 ", -1400.0),
("-1400.0", -1400),
# Zero handling
("0", 0.0),
("-0", 0.0),
("0.0", 0),
# Simple integers
("1", 1),
("1.0", 1),
],
)
def test_numeric_string_comparison(self, value1: Any, value2: Any):
"""Test comparison of numeric strings with numbers."""
assert PropertyRules.compare_values(value1, value2)
# Test reverse comparison
assert PropertyRules.compare_values(value2, value1)
@pytest.mark.parametrize(
"value1, value2, expected",
[
("Yes", True, True),
("No", False, True),
("yes", True, True),
("no", False, True),
("YES", True, True),
("NO", False, True),
("true", True, True),
("false", False, True),
("True", True, True),
("False", False, True),
],
)
def test_boolean_string_comparison(self, value1: str, value2: bool, expected: bool):
"""Test comparison of boolean strings with booleans."""
assert PropertyRules.compare_values(value1, value2) == expected
# Test reverse comparison
assert PropertyRules.compare_values(value2, value1) == expected
@pytest.mark.parametrize(
"value1, value2, case_sensitive, expected",
[
("hello", "HELLO", False, True),
("hello", "HELLO", True, False),
("Hello", "hello", False, True),
("Hello", "Hello", True, True),
],
)
def test_string_comparison(self, value1: str, value2: str, case_sensitive: bool, expected: bool):
"""Test string comparison with case sensitivity options."""
assert PropertyRules.compare_values(value1, value2, case_sensitive=case_sensitive) == expected
@pytest.mark.parametrize(
"value1, value2, tolerance, expected",
[
(1.0001, 1.0, 1e-3, True),
(1.0001, 1.0, 1e-6, False),
(1.00000001, 1.0, 1e-6, True),
(-1.0001, -1.0, 1e-3, True),
],
)
def test_float_comparison_tolerance(self, value1: float, value2: float, tolerance: float, expected: bool):
"""Test float comparison with different tolerance levels."""
assert PropertyRules.compare_values(value1, value2, tolerance=tolerance) == expected