diff --git a/goblin/abc.py b/goblin/abc.py index f57becc3eadc462bc364327ddd236ae7eff1e3b1..f4fa6a1f2f06cbc3ccf3683743453047ac7c415a 100644 --- a/goblin/abc.py +++ b/goblin/abc.py @@ -34,7 +34,7 @@ class DataType(abc.ABC): @abc.abstractmethod def to_db(self, val=None): """Convert property value to db compatible format""" - if not val: + if val is None: val = self._val return val diff --git a/goblin/properties.py b/goblin/properties.py index a5e77e21efeb437de2e83ebaa9aa9b0e194e0bfb..c958927b5689496a45b5a88f1b0784fdbcb2df30 100644 --- a/goblin/properties.py +++ b/goblin/properties.py @@ -21,6 +21,7 @@ import logging from goblin import abc, exception + logger = logging.getLogger(__name__) @@ -130,7 +131,7 @@ class Float(abc.DataType): return super().to_ogm(val) -class Bool(abc.DataType): +class Boolean(abc.DataType): def validate(self, val): try: val = bool(val) diff --git a/tests/conftest.py b/tests/conftest.py index 949553e5e9e9fedf4922ce6782d1afe793c42d26..16854f7caa0cd1c31e497b98aa1388b90ae1d1bd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -88,6 +88,16 @@ def integer(): return properties.Integer() +@pytest.fixture +def flt(): + return properties.Float() + + +@pytest.fixture +def boolean(): + return properties.Boolean() + + @pytest.fixture def person(): return Person() @@ -157,3 +167,13 @@ def string_class(): @pytest.fixture def integer_class(): return properties.Integer + + +@pytest.fixture +def flt_class(): + return properties.Float + + +@pytest.fixture +def boolean_class(): + return properties.Boolean diff --git a/tests/test_properties.py b/tests/test_properties.py index 311f53c1f2f21ab20fe4ead6d7ce655bcdb69c00..bcba03b917c1eba7c5631a81cdc5e56b4c659322 100644 --- a/tests/test_properties.py +++ b/tests/test_properties.py @@ -84,3 +84,50 @@ class TestInteger: def test_initval_to_db(self, integer_class): integer = integer_class(1) assert integer.to_db() == 1 + + +class TestFloat: + + def test_validation(self, flt): + assert flt.validate(1.2) == 1.2 + with pytest.raises(Exception): + flt.validate('hello') + + def test_to_db(self, flt): + assert flt.to_db(1.2) == 1.2 + + def test_to_ogm(self, flt): + assert flt.to_db(1.2) == 1.2 + + def test_initval_to_db(self, flt_class): + flt = flt_class(1.2) + assert flt.to_db() == 1.2 + + +class TestBoolean: + + def test_validation_true(self, boolean): + assert boolean.validate(True) == True + + def test_validation_false(self, boolean): + assert boolean.validate(False) == False + + def test_to_db_true(self, boolean): + assert boolean.to_db(True) == True + + def test_to_db_false(self, boolean): + assert boolean.to_db(False) == False + + def test_to_ogm_true(self, boolean): + assert boolean.to_ogm(True) == True + + def test_to_ogm_false(self, boolean): + assert boolean.to_ogm(False) == False + + def test_initval_to_db_true(self, boolean_class): + boolean = boolean_class(True) + assert boolean.to_db() == True + + def test_initval_to_db_true(self, boolean_class): + boolean = boolean_class(False) + assert boolean.to_db() == False