Skip to content

Commit

Permalink
fix: validate_and_update does not raise an exception anymore, simplif…
Browse files Browse the repository at this point in the history
…y function + make tests pass again
  • Loading branch information
robinvandernoord committed May 8, 2024
1 parent b7d09b7 commit e7a33f6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
5 changes: 1 addition & 4 deletions src/typedal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,10 +953,7 @@ def validate_and_update(
"""
table = self._ensure_table_defined()

try:
result = table.validate_and_update(query, **fields)
except Exception as e:
result = {"errors": {"exception": str(e)}}
result = table.validate_and_update(query, **fields)

if errors := result.get("errors"):
return None, errors
Expand Down
10 changes: 9 additions & 1 deletion tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,25 @@ class NewStyle(TypedTable):
assert old_style.validate_and_update(old_style.id == 1, string_field=123, int_field="abc")["errors"]
assert old_style.validate_and_update(old_style.id == 1, string_field="123", int_field=123)["id"]

# errors - int field is a string
instance, errors = NewStyle.validate_and_update(NewStyle.id == 1, string_field=123, int_field="abc")
assert not instance
assert errors

# errors - required field is None
instance, errors = NewStyle.validate_and_update(NewStyle.id == 1, string_field=None, int_field=None)
assert not instance
assert errors

# success - types match
instance, errors = NewStyle.validate_and_update(NewStyle.id == 1, string_field="123", int_field=123)
assert instance
assert not errors

# no instance because id 99 doesn't exist, but also no errors because types match:
instance, errors = NewStyle.validate_and_update(NewStyle.id == 99, string_field="123", int_field=123)
assert not instance
assert errors
assert not errors

assert old_style.validate_and_update_or_insert(old_style.id == 1, string_field=123, int_field="abc")["errors"]
assert old_style.validate_and_update_or_insert(old_style.id == 101, string_field=123, int_field="abc")["errors"]
Expand Down

0 comments on commit e7a33f6

Please sign in to comment.