Skip to content

Commit 5cad9b4

Browse files
committed
handle bad input in _logical_to_schema_name
1 parent 5de953a commit 5cad9b4

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/dataverse_sdk/data/odata.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,10 +719,20 @@ def _logical_to_schema_name(self, logical_name: str) -> str:
719719
# Normalize logical name first
720720
logical_name = self._normalize_logical_name(logical_name)
721721

722+
# Validate not empty after normalization
723+
if not logical_name:
724+
raise ValueError("logical_name cannot be empty or whitespace-only")
725+
722726
if "_" not in logical_name:
723727
# No prefix, just capitalize first letter
724728
return logical_name[:1].upper() + logical_name[1:]
729+
725730
prefix, rest = logical_name.split("_", 1)
731+
732+
# Validate that rest is not empty (e.g., "new_" is invalid)
733+
if not rest:
734+
raise ValueError(f"logical_name '{logical_name}' has empty part after underscore (expected format: 'prefix_name')")
735+
726736
# Capitalize first letter of the part after prefix
727737
return f"{prefix}_{rest[:1].upper()}{rest[1:]}"
728738

tests/unit/data/test_naming_normalization.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,5 +645,25 @@ def test_logical_to_schema_name_case_insensitive():
645645
assert c._logical_to_schema_name("Account") == "Account"
646646

647647

648+
def test_logical_to_schema_name_edge_cases():
649+
"""Test that _logical_to_schema_name handles edge cases correctly."""
650+
c = TestableClient([])
651+
652+
# Empty string should raise ValueError
653+
with pytest.raises(ValueError, match="cannot be empty"):
654+
c._logical_to_schema_name("")
655+
656+
# Whitespace-only should raise ValueError
657+
with pytest.raises(ValueError, match="cannot be empty"):
658+
c._logical_to_schema_name(" ")
659+
660+
# Trailing underscore with no suffix should raise ValueError
661+
with pytest.raises(ValueError, match="empty part after underscore"):
662+
c._logical_to_schema_name("new_")
663+
664+
# Multiple underscores - only first split matters
665+
assert c._logical_to_schema_name("new_sample_item") == "new_Sample_item"
666+
667+
648668
if __name__ == "__main__":
649669
pytest.main([__file__, "-v"])

0 commit comments

Comments
 (0)