diff --git a/goblin/abc.py b/goblin/abc.py index 8fbe186c78cc340e6ce18303115145b181fe2f92..7369eabb53eff31a168d8c37299121a0ab89e72e 100644 --- a/goblin/abc.py +++ b/goblin/abc.py @@ -23,6 +23,8 @@ class DataType(abc.ABC): Abstract base class for Goblin Data Types. All custom data types should inherit from :py:class:`DataType`. """ + def __init__(self, val=None): + self._val = val @abc.abstractmethod def validate(self): @@ -30,8 +32,10 @@ class DataType(abc.ABC): raise NotImplementedError @abc.abstractmethod - def to_db(self, val): + def to_db(self, val=None): """Convert property value to db compatible format""" + if not val: + val = self._val return val @abc.abstractmethod diff --git a/goblin/properties.py b/goblin/properties.py index 5906312a5ff679ab0a0cf9698ed2be49b5fe2b3e..9b240cdc4748c78bd8c48d8a84c2d020899804a4 100644 --- a/goblin/properties.py +++ b/goblin/properties.py @@ -89,8 +89,8 @@ class String(abc.DataType): raise exception.ValidationError( '{} is not a valid string'.format(val)) from e - def to_db(self, val): - return super().to_db(val) + def to_db(self, val=None): + return super().to_db(val=val) def to_ogm(self, val): return super().to_ogm(val) @@ -107,8 +107,8 @@ class Integer(abc.DataType): raise exception.ValidationError( '{} is not a valid integer'.format(val)) from e - def to_db(self, val): - return super().to_db(val) + def to_db(self, val=None): + return super().to_db(val=val) def to_ogm(self, val): return super().to_ogm(val) diff --git a/tests/conftest.py b/tests/conftest.py index 3775dd98a0234073c8406bd1b9b2bed695da2cc0..949553e5e9e9fedf4922ce6782d1afe793c42d26 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -147,3 +147,13 @@ def lives_in_class(): @pytest.fixture def place_name_class(): return PlaceName + + +@pytest.fixture +def string_class(): + return properties.String + + +@pytest.fixture +def integer_class(): + return properties.Integer diff --git a/tests/test_properties.py b/tests/test_properties.py index db44cdc69304c13ed846b446875903a1c6d78098..311f53c1f2f21ab20fe4ead6d7ce655bcdb69c00 100644 --- a/tests/test_properties.py +++ b/tests/test_properties.py @@ -63,6 +63,10 @@ class TestString: def test_to_ogm(self, string): assert string.to_ogm('hello') == 'hello' + def test_initval_to_db(self, string_class): + string = string_class('hello') + assert string.to_db() == 'hello' + class TestInteger: @@ -76,3 +80,7 @@ class TestInteger: def test_to_ogm(self, integer): assert integer.to_db(1) == 1 + + def test_initval_to_db(self, integer_class): + integer = integer_class(1) + assert integer.to_db() == 1