Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Don't treat 0 number as an empty attribute value #920

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mwdb/schema/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class AttributeValueSchema(Schema):

@validates("value")
def validate_value(self, value):
# Currently only truthy values and False are allowed as values
if not value and value is not False:
# 0 and False are not empty values
if value in ["", {}, [], None]:
raise ValidationError("Value shouldn't be empty")


Expand Down
16 changes: 16 additions & 0 deletions tests/backend/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,19 @@ def test_json_attribute_uniqueness(admin_session, attr_session, random_attribute
and attr["value"] == first_value == second_value
]
assert len(stored_attributes) == 1


def test_attribute_falsy_values(admin_session, random_attribute):
sample_id, attr_name = random_attribute
admin_session.add_attribute(sample_id, attr_name, 0)
admin_session.add_attribute(sample_id, attr_name, False)
with ShouldRaise(400):
admin_session.add_attribute(sample_id, attr_name, [])
with ShouldRaise(400):
admin_session.add_attribute(sample_id, attr_name, {})
with ShouldRaise(400):
admin_session.add_attribute(sample_id, attr_name, None)
with ShouldRaise(400):
admin_session.add_attribute(sample_id, attr_name, "")
admin_session.add_attribute(sample_id, attr_name, ["nonempty"])
admin_session.add_attribute(sample_id, attr_name, {"nonempty": None})
Loading