From 2ae068c2d220a8c12a4d9b1490f0511ed48d407b Mon Sep 17 00:00:00 2001 From: gpjacobs Date: Sat, 12 Oct 2019 15:30:48 -0500 Subject: [PATCH] Improving error handling for null values --- lbry/lbry/schema/claim.py | 7 ++++++- lbry/tests/unit/schema/test_models.py | 21 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lbry/lbry/schema/claim.py b/lbry/lbry/schema/claim.py index d845b4658..41abb2e2d 100644 --- a/lbry/lbry/schema/claim.py +++ b/lbry/lbry/schema/claim.py @@ -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: diff --git a/lbry/tests/unit/schema/test_models.py b/lbry/tests/unit/schema/test_models.py index 40576b708..f8d834295 100644 --- a/lbry/tests/unit/schema/test_models.py +++ b/lbry/tests/unit/schema/test_models.py @@ -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) +