From 8cee496f6f72de8e576e25ff184678b06b9dabea Mon Sep 17 00:00:00 2001
From: davebshow <davebshow@gmail.com>
Date: Mon, 18 Jul 2016 20:48:43 -0400
Subject: [PATCH] added ability to pass a value to DataType that is then used
 as a default for to_db func

---
 goblin/abc.py            |  6 +++++-
 goblin/properties.py     |  8 ++++----
 tests/conftest.py        | 10 ++++++++++
 tests/test_properties.py |  8 ++++++++
 4 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/goblin/abc.py b/goblin/abc.py
index 8fbe186..7369eab 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 5906312..9b240cd 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 3775dd9..949553e 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 db44cdc..311f53c 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
-- 
GitLab