Improving error handling for null values

This commit is contained in:
gpjacobs 2019-10-12 15:30:48 -05:00 committed by Lex Berezhny
parent 5e07e8dbbd
commit 2ae068c2d2
2 changed files with 26 additions and 2 deletions

View file

@ -124,7 +124,12 @@ class BaseClaim:
for field in self.object_fields:
if key.startswith(f'{field}_'):
attr = getattr(self, field)
setattr(attr, key[len(f'{field}_'):], kwargs.pop(key))
attr_value = kwargs.pop(key)
if attr_value is None:
raise ValueError(f"Error updating claim - Null value provided for attribute {field}")
setattr(attr, key[len(f'{field}_'):], attr_value)
continue
for l in self.repeat_fields:

View file

@ -1,7 +1,8 @@
from unittest import TestCase
from unittest.mock import patch
from decimal import Decimal
from lbry.schema.claim import Claim, Stream, Collection
from lbry.schema.claim import Claim, Stream, Collection, BaseClaim
class TestClaimContainerAwareness(TestCase):
@ -195,3 +196,21 @@ class TestLocations(TestCase):
stream = Stream()
stream.locations.append({"country": "UA"})
self.assertEqual(stream.locations[0].country, 'UA')
class TestBaseClaimUpdates(TestCase):
@patch('lbry.schema.claim.Claim')
def test_claim_update_url_ok(self, MockClaim):
# test if an attribute is provided correctly
base_claim = BaseClaim(MockClaim)
base_claim.update(thumbnail_url="somescheme:some/path")
self.assertEqual(base_claim.thumbnail.url, "somescheme:some/path")
@patch('lbry.schema.claim.Claim')
def test_claim_update_url_error(self, MockClaim):
# test if an attribute is null
base_claim = BaseClaim(MockClaim)
with self.assertRaisesRegex(ValueError, "Error updating claim - Null value provided for attribute thumbnail"):
base_claim.update(thumbnail_url=None)