Files
speckle-automate-checker/tests/test_parameters.py
T
Jonathon Broughton 8582444e56 Add V3 compatibility (#23)
* Update project configuration and name

- Excluded virtual environment folder from module content.
- Changed JDK name to "WSL Checker" for clarity.
- Updated project name in the configuration file.

* Update project configuration and dependencies

- Added source folders for `src` and `tests`.
- Excluded `.devcontainer` and `.idea` directories.
- Updated Black settings in the project configuration.
- Changed Ruff version from 0.9.5 to 0.9.6.
- Enhanced rule processing by grouping rules before application.
- Introduced a new property match mode for better parameter matching.
- Removed unused test files and configurations to clean up the codebase.

* Shifting to local uv - editing toml

* Update CI workflow and project dependencies

- Added steps to the CI workflow for better clarity.
- Switched from Poetry to pip for dependency management.
- Removed poetry.lock file as it's no longer needed.
- Updated Python version specification to 3.12.
- Adjusted project configuration files for new environment settings.

* Update dependencies for pydantic and related packages

- Added pydantic version 2.11.0a2 to dependencies.
- Updated pydantic-core to version 2.29.0.
- Adjusted URLs and hashes for the new package versions in lock file.
- Ensured compatibility with existing requirements in project configuration.

* Update dependencies to stable versions

- Downgraded Pydantic from 2.11.0a2 to 2.10.6
- Updated Pydantic-core version from 2.29.0 to 2.27.2
- Adjusted URLs and hashes for the new package versions in lock file

* Update dependencies to latest versions

- Upgraded Pydantic from 2.10.6 to 2.11.0a2
- Updated Pydantic Core from 2.27.2 to 2.29.0
- Adjusted source URLs and hashes for new package versions

* Update dependencies to stable versions

- Downgraded Pydantic from 2.11.0a2 to 2.10.6
- Updated Pydantic-core version from 2.29.0 to 2.27.2
- Adjusted URLs and hashes for the new package versions in lock file

* Add pre-commit hook and setup script

- Introduced a pre-commit hook to manage dependencies.
- Automatically installs and exports requirements before commits.
- Added a setup script to copy the hook and set permissions.

* Update project config and clean up unused files

- Removed Black configuration options from the project settings.
- Changed Ruff to run on save instead of reformat.
- Deleted example function inputs JSON file.
- Removed flatten helper module as it's no longer needed.
- Updated pyproject.toml with new line length and ignore rules.
- Cleaned up import statements in local test file.

* Update line length and tidy up code comments

- Increased the maximum line length from 100 to 120.
- Cleaned up import statements for better readability.
- Improved docstrings for clarity and consistency.
- Removed unnecessary blank lines and commented-out code.

* Refactor rules handling and improve documentation

- Updated class names from `RevitRules` to `PropertyRules`.
- Adjusted method calls to reflect the new class structure.
- Cleaned up code formatting for better readability.
- Enhanced developer README with clearer setup instructions.

* Add filtering and rule processing features

- Introduced a new filter function to sort objects by category.
- Created a predicates module for mapping spreadsheet predicates to rule methods.
- Refactored rules handling, simplifying parameter checks and value retrievals.
- Enhanced the main automation function with improved input handling and context management.

* Past WIP rules for processing Speckle parameters

- Introduced a new class to manage parameter rules.
- Added methods to check and retrieve display values from Speckle objects.
- Implemented filtering for displayable objects based on defined criteria.
- Created functions to handle Revit parameter checks and value retrievals.
- Included various utility methods for comparing parameter values against thresholds, ranges, and lists.

* Add new JSON test data for building elements

- Introduced a new JSON file containing detailed specifications for various building elements.
- Included parameters such as ID, type, level details, and multiple attributes related to structural components.
- Added information on dimensions, materials, and classifications relevant to the construction context.

* Rename test files and add new parameter tests

- Renamed localtest.py to test_function.py for clarity.
- Added a new test file for parameter checks, covering:
  - Object deserialization structure
  - Parameter existence and value retrieval in v2 and v3 objects
  - Numeric comparisons and pattern matching on parameters
  - List-based and boolean parameter checks

* Enhance helper functions and add rule processing

- Updated helper functions for better item retrieval and checks.
- Added new functions to evaluate conditions and process rules against Speckle objects.
- Improved logging with a dedicated print function.
- Streamlined flattening logic in existing methods.

* Add test runner config and new web resources

- Added pytest as the project test runner.
- Created a new XML file for web resource paths.
- Included pytest-assertcount in development dependencies.

* Add robust value comparison methods

- Introduced new static methods for comparing values:
  - `is_equal_value`: Compares two values with type handling and optional case sensitivity.
  - `is_not_equal_value`: Checks if two values are not equal.
  - `is_identical_value`: Verifies if two values are exactly identical, considering case sensitivity and no tolerance for floats.
  - `is_not_identical_value`: Checks if two values are not identical.
- Enhanced handling of strings that represent numbers.

* Refactor rules and parameter handling

- Commented out large sections of code for clarity

* Update predicates and enhance tests

- Renamed equality-related methods for clarity.
- Added new methods for "not equal" and "not identical" checks.
- Updated test to return specific objects instead of walls.
- Changed property access in tests to use `.keys()`.
- Introduced multiple tests for stringified number comparisons, case sensitivity, and floating-point precision.

* Add tests for severity level conversion

Created a new test file to validate the `get_severity` function.
- Added parameterised tests for various input cases including valid and invalid severity strings, empty inputs, and non-string types.
- Ensured that all edge cases default to the correct enum value.

* Refactor parameter tests for clarity and efficiency

- Simplified assertions in parameter existence tests.
- Added parameterisation to reduce redundancy across tests.
- Enhanced error messages for better debugging.
- Consolidated similar test cases for v2 and v3 objects.
- Improved structure of boolean and numeric comparison tests.

* Enhance spreadsheet data handling

- Added a function to convert mixed column types.
- Updated the main reading function to use this new conversion.
- Improved error handling for reading TSV files.

* Update version handling in automation function

- Added a global VERSION variable for tracking.
- Updated comments to reflect changes from 'version' to 'VERSION'.
- Adjusted logic to retrieve the version from the root object.
- Enhanced success message to include the current version.

* Improve boolean handling and property search

- Added None handling in boolean conversion.
- Enhanced `find_property` to return raw values if needed.
- Updated traversal logic to avoid revisiting objects.
- Refined comparison methods for better type handling and tolerance.
- Introduced new methods for strict value comparisons with detailed arguments.

* Update predicate method mappings

Switched from string references to actual method names in the predicate mapping. This improves clarity and reduces errors by directly linking predicates to their corresponding methods in PropertyRules. Adjusted some predicate phrases for consistency as well.

* Enhance rule evaluation with severity handling

- Added optional parameters for rule and case numbers in condition evaluation.
- Introduced an Enum for severity levels to standardise reporting.
- Created a function to convert string severities into the SeverityLevel enum, handling various input formats.
- Refactored metadata generation to use the new severity function.
2025-02-17 23:50:34 +00:00

405 lines
16 KiB
Python

import os
from typing import Any
import pytest
from dotenv import load_dotenv
from speckle_automate import AutomationContext, AutomationRunData # noqa: F401, F403
# from speckle_automate.fixtures import * # noqa: F401, F403
from specklepy.api.client import SpeckleClient
from specklepy.core.api import operations
from specklepy.objects.base import Base
from specklepy.transports.server import ServerTransport
from helpers import speckle_print
from src.rules import PropertyRules
def load_test_objects(v2_wall: Any, v3_wall: Any) -> tuple[Base, Base]:
"""Load test objects from a Speckle server."""
client = SpeckleClient(host="https://app.speckle.systems", use_ssl=True)
load_dotenv(dotenv_path="../.env")
client.authenticate_with_token(os.getenv("SPECKLE_TOKEN"))
transport = ServerTransport(client=client, stream_id=os.getenv("SPECKLE_PROJECT_ID"))
speckle_print(v2_wall)
v2_obj = operations.receive("cdb18060dc48281909e94f0f1d8d3cc0", transport)
v3_obj = operations.receive("46f06fef727d64a0bbcbd7ced51e0cd2", transport)
# return v2_wall, v3_wall
return v2_obj, v3_obj
@pytest.fixture
def test_objects(v2_wall: Any, v3_wall: Any) -> tuple[Base, Base]:
"""Pytest fixture to provide test objects."""
return load_test_objects(v2_wall, v3_wall)
def test_deserialization_structure(test_objects):
"""Test that objects are properly deserialized with correct structure."""
v2_obj, v3_obj = test_objects
# Check base class type
for obj in [v2_obj, v3_obj]:
assert isinstance(obj, Base), f"Expected {obj} to be an instance of Base"
# Check v2 structure
assert hasattr(v2_obj, "parameters"), "v2_obj should have 'parameters' attribute"
assert v2_obj["parameters"] is not None, "v2_obj['parameters'] should not be None"
# Check v3 structure
assert hasattr(v3_obj, "properties"), "v3_obj should have 'properties' attribute"
assert v3_obj["properties"] is not None, "v3_obj['properties'] should not be None"
assert "Parameters" in v3_obj["properties"], "'Parameters' key should exist in v3_obj['properties']"
@pytest.mark.parametrize(
"param_name, expected_result",
[
("category", True), # Test parameters that should exist
("WALL_ATTR_WIDTH_PARAM", True), # Test nested parameters
("WALL_ATTR_WIDTH_PARAM.value", True),
("WALL_ATTR_WIDTH_PARAM.id", True),
("WALL_ATTR_WIDTH_PARAM.units", True),
("non_existent_param", False), # Test non-existent parameters
],
)
def test_v2_parameter_exists(test_objects, param_name, expected_result):
"""Test parameter existence checking in v2 objects."""
v2_obj, _ = test_objects
assert PropertyRules.has_parameter(v2_obj, param_name) == expected_result
@pytest.mark.parametrize(
"param_name",
[
"WALL_ATTR_WIDTH_PARAM.id",
"WALL_ATTR_WIDTH_PARAM.value",
"WALL_ATTR_WIDTH_PARAM",
"WALL_ATTR_WIDTH_PARAM.units",
],
)
def test_v2_parameter_value_retrieval(test_objects, param_name):
"""Test parameter value retrieval in v2 objects."""
v2_obj, _ = test_objects
assert PropertyRules.get_parameter_value(v2_obj, param_name)
@pytest.mark.parametrize(
"param_name, expected_result",
[
("category", True), # Test parameters that should exist
("Width", True), # Test nested parameters
("non_existent_param", False), # Test non-existent parameters
],
)
def test_v3_parameter_exists(test_objects, param_name, expected_result):
"""Test parameter existence checking in v3 objects."""
_, v3_obj = test_objects
assert PropertyRules.has_parameter(v3_obj, param_name) == expected_result
@pytest.mark.parametrize(
"param_name_1, param_name_2",
[
(
"properties.Parameters.Instance Parameters.Dimensions.Length.value",
"Instance Parameters.Dimensions.Length",
),
],
)
def test_v3_parameter_search_equivalence(test_objects, param_name_1, param_name_2):
"""Test parameter existence checking equivalence in v3 objects."""
_, v3_obj = test_objects
assert PropertyRules.get_parameter_value(v3_obj, param_name_1) == PropertyRules.get_parameter_value(
v3_obj, param_name_2
)
@pytest.mark.parametrize(
"obj_version, param_name, expected_value, default_value",
[
# Test direct parameters
("v2", "category", "Walls", None),
("v3", "category", "Walls", None),
# Test nested parameters - using both internal and friendly names
("v2", "WALL_ATTR_WIDTH_PARAM", 300, None),
("v3", "Construction.Width", 300, None),
# Test parameters with units
("v2", "CURVE_ELEM_LENGTH", 5300.000000000001, None),
("v3", "Instance Parameters.Dimensions.Length", 5300.000000000001, None),
# Test non-existent parameters with a default value
("v2", "parameters.non_existent", "default", "default"),
("v3", "properties.Parameters.non_existent", "default", "default"),
],
)
def test_parameter_value_retrieval(test_objects, obj_version, param_name, expected_value, default_value):
"""Test parameter value retrieval from both v2 and v3 objects."""
v2_obj, v3_obj = test_objects
obj = v2_obj if obj_version == "v2" else v3_obj
result = PropertyRules.get_parameter_value(obj, param_name, default_value=default_value)
assert result == expected_value
@pytest.mark.parametrize(
"param_name, expected_value, expected_result",
[
("category", "Walls", True), # Test exact match
("WALL_ATTR_WIDTH_PARAM", 300, True), # Test numeric match
("category", "Windows", False), # Test non-match
],
)
def test_v2_parameter_value_matching(test_objects, param_name, expected_value, expected_result):
"""Test parameter value matching in v2 objects."""
v2_obj, _ = test_objects
assert PropertyRules.is_parameter_value(v2_obj, param_name, expected_value) == expected_result
@pytest.mark.parametrize(
"param_name, expected_value, expected_result",
[
("category", "Walls", True), # Test exact match
("Width", 300, True), # Test numeric match
("category", "Windows", False), # Test non-match
],
)
def test_v3_parameter_value_matching(test_objects, param_name, expected_value, expected_result):
"""Test parameter value matching in v3 objects."""
_, v3_obj = test_objects
assert PropertyRules.is_parameter_value(v3_obj, param_name, expected_value) == expected_result
@pytest.mark.parametrize(
"comparison_func, param_name, value, expected_result",
[
(PropertyRules.is_parameter_value_greater_than, "WALL_ATTR_WIDTH_PARAM", "200", True), # Test greater than
(PropertyRules.is_parameter_value_less_than, "WALL_ATTR_WIDTH_PARAM", "400", True), # Test less than
(PropertyRules.is_parameter_value_in_range, "WALL_ATTR_WIDTH_PARAM", "200,400", True), # Test in range
],
)
def test_v2_parameter_numeric_comparisons(test_objects, comparison_func, param_name, value, expected_result):
"""Test numeric parameter comparisons in v2 objects."""
v2_obj, _ = test_objects
assert comparison_func(v2_obj, param_name, value) == expected_result
@pytest.mark.parametrize(
"comparison_func, param_name, value, expected_result",
[
(PropertyRules.is_parameter_value_greater_than, "Width", "200", True), # Test greater than
(PropertyRules.is_parameter_value_less_than, "Width", "400", True), # Test less than
(PropertyRules.is_parameter_value_in_range, "Width", "200,400", True), # Test in range
],
)
def test_v3_parameter_numeric_comparisons(test_objects, comparison_func, param_name, value, expected_result):
"""Test numeric parameter comparisons in v3 objects."""
_, v3_obj = test_objects
assert comparison_func(v3_obj, param_name, value) == expected_result
@pytest.mark.parametrize(
"param_name, pattern, fuzzy, expected_result",
[
("category", "^Walls$", False, True), # Test exact pattern matches
("category", "Walls", True, True), # Test fuzzy matches
("category", "Wall", False, True), # Test partial pattern matches
("category", "^Windows$", False, False), # Test non-matches
],
)
def test_v2_parameter_value_like(test_objects, param_name, pattern, fuzzy, expected_result):
"""Test pattern matching on parameter values in v2 objects."""
v2_obj, _ = test_objects
assert PropertyRules.is_parameter_value_like(v2_obj, param_name, pattern, fuzzy=fuzzy) == expected_result
@pytest.mark.parametrize(
"param_name, pattern, fuzzy, expected_result",
[
("category", "^Walls$", False, True), # Test exact pattern matches
("category", "Walls", True, True), # Test fuzzy matches
("category", "Wall", False, True), # Test partial pattern matches
("category", "^Windows$", False, False), # Test non-matches
],
)
def test_v3_parameter_value_like(test_objects, param_name, pattern, fuzzy, expected_result):
"""Test pattern matching on parameter values in v3 objects."""
_, v3_obj = test_objects
assert PropertyRules.is_parameter_value_like(v3_obj, param_name, pattern, fuzzy=fuzzy) == expected_result
@pytest.mark.parametrize(
"param_name, valid_list, expected_result",
[
("category", ["Walls", "Windows", "Doors"], True), # Test value in list
("category", "Walls,Windows,Doors", True), # Test comma-separated string list
("category", ["Windows", "Doors"], False), # Test value not in list
],
)
def test_v2_parameter_lists(test_objects, param_name, valid_list, expected_result):
"""Test list-based parameter checks in v2 objects."""
v2_obj, _ = test_objects
assert PropertyRules.is_parameter_value_in_list(v2_obj, param_name, valid_list) == expected_result
@pytest.mark.parametrize(
"param_name, valid_list, expected_result",
[
("category", ["Walls", "Windows", "Doors"], True), # Test value in list
("category", "Walls,Windows,Doors", True), # Test comma-separated string list
("category", ["Windows", "Doors"], False), # Test value not in list
],
)
def test_v3_parameter_lists(test_objects, param_name, valid_list, expected_result):
"""Test list-based parameter checks in v3 objects."""
_, v3_obj = test_objects
assert PropertyRules.is_parameter_value_in_list(v3_obj, param_name, valid_list) == expected_result
@pytest.mark.parametrize(
"param_name, expected_result",
[
("WALL_ATTR_ROOM_BOUNDING.value", True), # Test true values
("wall_top_is_attached", False), # Test false values
],
)
def test_v2_boolean_parameters(test_objects, param_name, expected_result):
"""Test boolean parameter checks in v2 objects."""
v2_obj, _ = test_objects
if expected_result:
assert PropertyRules.is_parameter_value_true(v2_obj, param_name)
else:
assert PropertyRules.is_parameter_value_false(v2_obj, param_name)
@pytest.mark.parametrize(
"param_name, expected_result",
[
("Room Bounding", True), # Test true values
("top is attached", False), # Test false values
("Top is Attached", False), # Case sensitivity test
],
)
def test_v3_boolean_parameters(test_objects, param_name, expected_result):
"""Test boolean parameter checks in v3 objects."""
_, v3_obj = test_objects
if expected_result:
assert PropertyRules.is_parameter_value_true(v3_obj, param_name)
else:
assert PropertyRules.is_parameter_value_false(v3_obj, param_name)
@pytest.mark.parametrize(
"param_name, expected_value, expected_result",
[
# Test numeric value comparisons
("WALL_ATTR_WIDTH_PARAM", 300, True),
("WALL_ATTR_WIDTH_PARAM.value", 300, True),
("baseLine.length", 5300.000000000002, True),
# Test string value comparisons
("STRUCTURAL_MATERIAL_PARAM.value", "Fc24", True),
("ee1f33e1-5506-4a64-b87b-7b98d30aea52.value", "W30", True),
# Test non-matches
("WALL_ATTR_WIDTH_PARAM", 301, False),
("nonexistent_param", "any_value", False),
],
)
def test_v2_parameter_value_comparisons(v2_wall, param_name, expected_value, expected_result):
"""Test value comparisons using v2 wall parameters."""
assert PropertyRules.is_equal_value(v2_wall, param_name, expected_value) == expected_result
@pytest.mark.parametrize(
"attribute, value, expected",
[
# Test numeric value comparisons
("Type Parameters.Structure.Fc24 (0).thickness", 300, True),
("location.length", 5300.000000000002, True),
("location.length", 5300, True),
# Test string value comparisons
("Type Parameters.Text.符号.value", "W30", True),
("Instance Parameters.Structural.Structural.value", "Yes", True),
# Test non-matches
("Type Parameters.Structure.Fc24 (0).thickness", 301, False),
("nonexistent_param", "any_value", False),
],
)
def test_v3_parameter_value_comparisons(v3_wall, attribute, value, expected):
"""Test value comparisons using v3 wall parameters."""
assert PropertyRules.is_equal_value(v3_wall, attribute, value) == expected
@pytest.mark.parametrize(
"wall, attribute, value, expected",
[
# V2 wall tests
("v2_wall", "WALL_ATTR_WIDTH_PARAM.value", 300, True),
("v2_wall", "type", "W30(Fc24)", True),
("v2_wall", "WALL_ATTR_WIDTH_PARAM.value", 300.0001, False),
# V3 wall tests
("v3_wall", "Type Parameters.Structure.Fc24 (0).thickness", 300, True),
("v3_wall", "type", "W30(Fc24)", True),
("v3_wall", "Type Parameters.Structure.Fc24 (0).thickness", 300.0001, False),
("v3_wall", "location.length", 5300.000000000002, True),
("v3_wall", "location.length", 5300, False),
],
)
def test_identical_comparisons(request, wall, attribute, value, expected):
"""Test identical value comparisons on both wall versions."""
wall_instance = request.getfixturevalue(wall)
assert PropertyRules.is_identical_value(wall_instance, attribute, value) == expected
@pytest.mark.parametrize(
"wall, attribute, value",
[
# V2 wall tests
("v2_wall", "WALL_ATTR_WIDTH_PARAM.value", 301),
("v2_wall", "STRUCTURAL_MATERIAL_PARAM.value", "Fc25"),
("v2_wall", "nonexistent_param", "any_value"),
# V3 wall tests
("v3_wall", "Type Parameters.Structure.Fc24 (0).thickness", 301),
("v3_wall", "Type Parameters.Text.符号.value", "W31"),
("v3_wall", "nonexistent_param", "any_value"),
],
)
def test_not_equal_comparisons(request, wall, attribute, value):
"""Test not equal comparisons on both wall versions."""
wall_instance = request.getfixturevalue(wall)
assert PropertyRules.is_not_equal_value(wall_instance, attribute, value)
@pytest.mark.parametrize(
"attribute, value, expected_equal, expected_identical",
[
# Test Yes/No conversion in equals (should convert)
("Instance Parameters.Structural.Structural.value", True, True, False), # Yes vs True
("Instance Parameters.Structural.Structural.value", "Yes", True, True), # Yes vs "Yes"
("Instance Parameters.Structural.Structural.value", "yes", True, False), # Yes vs "yes"
],
)
def test_boolean_conversions(v3_wall, attribute, value, expected_equal, expected_identical):
"""Test conversion of Yes/No strings to boolean values."""
assert PropertyRules.is_equal_value(v3_wall, attribute, value) == expected_equal
assert PropertyRules.is_identical_value(v3_wall, attribute, value) == expected_identical
@pytest.mark.parametrize(
"wall, attribute, expected_value",
[
# V2 wall tests
("v2_wall", "WALL_ATTR_WIDTH_PARAM.value", "300"),
("v2_wall", "baseLine.length", "5300.000000000002"),
# V3 wall tests
("v3_wall", "Type Parameters.Structure.Fc24 (0).thickness", "300"),
("v3_wall", "location.length", "5300.000000000002"),
],
)
def test_numeric_string_handling(wall, attribute, expected_value, request):
"""Test handling of numeric strings in both wall versions."""
wall_instance = request.getfixturevalue(wall) # Retrieve fixture dynamically
assert PropertyRules.is_equal_value(wall_instance, attribute, expected_value)