Add 'is set' predicate and not empty check
- Introduced a new predicate for checking if a parameter is set. - Added a method to verify if a parameter exists and isn't empty. - Updated tests to cover the new 'not empty' functionality.
This commit is contained in:
@@ -17,4 +17,5 @@ PREDICATE_METHOD_MAP = {
|
||||
"identical to": PropertyRules.is_identical_value.__name__,
|
||||
"contains": PropertyRules.is_parameter_value_containing.__name__,
|
||||
"does not contain": PropertyRules.is_parameter_value_not_containing.__name__,
|
||||
"is set": PropertyRules.is_parameter_value_not_empty.__name__,
|
||||
}
|
||||
|
||||
@@ -890,3 +890,35 @@ class PropertyRules:
|
||||
return PropertyRules.compare_values(
|
||||
parameter_value, value_to_match, case_sensitive=True, tolerance=0, allow_yes_no_bools=False, use_exact=True
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def is_parameter_value_not_empty(speckle_object: Base, parameter_name: str) -> bool:
|
||||
"""Checks if parameter exists and has a non-empty value.
|
||||
|
||||
Args:
|
||||
speckle_object: The Speckle object to check
|
||||
parameter_name: Name of the parameter to check
|
||||
|
||||
Returns:
|
||||
bool: True if parameter exists and has a non-empty value
|
||||
"""
|
||||
# Get the parameter value
|
||||
parameter_value = PropertyRules.get_parameter_value(speckle_object, parameter_name)
|
||||
|
||||
# Check if parameter exists
|
||||
if parameter_value is None:
|
||||
return False
|
||||
|
||||
# Check if value is an empty string
|
||||
if isinstance(parameter_value, str) and parameter_value.strip() == "":
|
||||
return False
|
||||
|
||||
# Check if value is an empty list or collection
|
||||
if hasattr(parameter_value, "__len__") and len(parameter_value) == 0:
|
||||
return False
|
||||
|
||||
# Additional check for "None" string which can sometimes appear in exports
|
||||
if isinstance(parameter_value, str) and parameter_value.lower() == "none":
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
@@ -415,3 +415,20 @@ class TestParameterHandling:
|
||||
"""Test negative substring matching on parameter values."""
|
||||
v2_obj, _ = test_objects
|
||||
assert PropertyRules.is_parameter_value_not_containing(v2_obj, param_name, substring) == expected_result
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"param_name, expected_result",
|
||||
[
|
||||
("category", True), # Parameter exists with non-empty value
|
||||
("family", True), # Parameter exists with non-empty value
|
||||
("non_existent_param", False), # Parameter doesn't exist
|
||||
# The following would require setup with empty values
|
||||
# ("empty_string_param", False), # Parameter exists but has empty string value
|
||||
# ("none_string_param", False), # Parameter exists but has "None" string value
|
||||
],
|
||||
)
|
||||
def test_parameter_not_empty(self, test_objects, param_name, expected_result):
|
||||
"""Test 'not empty' check on parameter values."""
|
||||
v2_obj, _ = test_objects
|
||||
|
||||
assert PropertyRules.is_parameter_value_not_empty(v2_obj, param_name) == expected_result
|
||||
|
||||
Reference in New Issue
Block a user