From ff613476beb31db8226ad295862ec1dd5815c80f Mon Sep 17 00:00:00 2001 From: Deimos Date: Thu, 4 Oct 2018 22:44:06 -0600 Subject: [PATCH] Ltree field: convert values to lowercase We're always using lowercase for all ltree usages (group paths, topic tags), so it's best to just have the Ltree field do this conversion. This fixes some minor issues like tag-filtering not working if the casing of the tag was wrong. A few other changes needed to be made as part of this, in places where I was inadvertently passing an Ltree value into the Ltree field, instead of a string. --- tildes/tests/test_group.py | 7 +++---- tildes/tildes/models/group/group.py | 2 +- tildes/tildes/schemas/fields.py | 2 +- tildes/tildes/schemas/topic.py | 2 +- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/tildes/tests/test_group.py b/tildes/tests/test_group.py index 0494d62..b005b10 100644 --- a/tildes/tests/test_group.py +++ b/tildes/tests/test_group.py @@ -39,10 +39,9 @@ def test_middle_element_end_with_underscore(): assert not is_valid_group_path("x.y_.z") -def test_uppercase_letters_invalid(): - """Ensure a group path can't contain uppercase chars.""" - assert is_valid_group_path("comp.lang.c") - assert not is_valid_group_path("comp.lang.C") +def test_uppercase_letters_fixed(): + """Ensure a group path gets uppercase letters converted to lowercase.""" + assert Group("Comp.Lang.C").path == "comp.lang.c" def test_paths_with_invalid_characters(): diff --git a/tildes/tildes/models/group/group.py b/tildes/tildes/models/group/group.py index aa35438..e43d092 100644 --- a/tildes/tildes/models/group/group.py +++ b/tildes/tildes/models/group/group.py @@ -67,7 +67,7 @@ class Group(DatabaseModel): def __init__(self, path: str, short_desc: Optional[str] = None) -> None: """Create a new group.""" - self.path = Ltree(path) + self.path = path self.short_description = short_desc def __acl__(self) -> Sequence[Tuple[str, Any, str]]: diff --git a/tildes/tildes/schemas/fields.py b/tildes/tildes/schemas/fields.py index c1c0a48..3a25a90 100644 --- a/tildes/tildes/schemas/fields.py +++ b/tildes/tildes/schemas/fields.py @@ -147,6 +147,6 @@ class Ltree(Field): def _deserialize(self, value: str, attr: str, data: dict) -> sqlalchemy_utils.Ltree: """Deserialize a string path to an Ltree object.""" try: - return sqlalchemy_utils.Ltree(value) + return sqlalchemy_utils.Ltree(value.lower()) except (TypeError, ValueError): raise ValidationError("Invalid path") diff --git a/tildes/tildes/schemas/topic.py b/tildes/tildes/schemas/topic.py index b6843bc..8e6fc0b 100644 --- a/tildes/tildes/schemas/topic.py +++ b/tildes/tildes/schemas/topic.py @@ -80,7 +80,7 @@ class TopicSchema(Schema): group_schema = GroupSchema(partial=True) for tag in value: try: - group_schema.validate({"path": tag}) + group_schema.validate({"path": str(tag)}) except ValidationError: raise ValidationError("Tag %s is invalid" % tag)