From 7a70ef27f6e6ba79d129714546f758a1341a7197 Mon Sep 17 00:00:00 2001 From: davebshow <davebshow@gmail.com> Date: Tue, 19 Jul 2016 15:00:12 -0400 Subject: [PATCH] finished up concrete float and boolean props --- goblin/abc.py | 2 +- goblin/properties.py | 3 ++- tests/conftest.py | 20 +++++++++++++++++ tests/test_properties.py | 47 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 2 deletions(-) diff --git a/goblin/abc.py b/goblin/abc.py index f57becc..f4fa6a1 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 a5e77e2..c958927 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 949553e..16854f7 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 311f53c..bcba03b 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 -- GitLab