Skip to content

Commit

Permalink
Issue #26: Fields custom getters and setters.
Browse files Browse the repository at this point in the history
  • Loading branch information
boonya committed Nov 18, 2016
1 parent 7de1816 commit 902333b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
2 changes: 1 addition & 1 deletion domain_models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Domain models."""

VERSION = '0.0.9'
VERSION = '0.0.10'
22 changes: 20 additions & 2 deletions domain_models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ def __init__(self, default=None, required=False):
self.name = None
self.storage_name = None

self.setter_fn = None
self.getter_fn = None

self.model_cls = None

self.default = default
Expand Down Expand Up @@ -60,7 +63,11 @@ def get_value(self, model, default=None):
if default is not None:
default = self._converter(default)

value = getattr(model, self.storage_name)
if callable(self.getter_fn):
value = self.getter_fn(model)
else:
value = getattr(model, self.storage_name)

return value if value is not None else default

def set_value(self, model, value):
Expand All @@ -75,7 +82,10 @@ def set_value(self, model, value):
if value is not None:
value = self._converter(value)

setattr(model, self.storage_name, value)
if callable(self.setter_fn) and value is not None:
self.setter_fn(model, value)
else:
setattr(model, self.storage_name, value)

def get_builtin_type(self, model):
"""Return built-in type representation of Field.
Expand All @@ -85,6 +95,14 @@ def get_builtin_type(self, model):
"""
return self.get_value(model)

def getter(self, fn):
"""Set function for implementation custom getter feature."""
self.getter_fn = fn

def setter(self, fn):
"""Set function for implementation custom setter feature."""
self.setter_fn = fn

def _converter(self, value):
"""Convert raw input value of the field.
Expand Down
37 changes: 37 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ class RequiredFieldModel(models.DomainModel):
field_required = fields.Field(required=True)


class CustomGetterSetter(models.DomainModel):
open_id = fields.Int()
id = fields.Int()

@open_id.getter
def _get_oid(self):
return self.id << 8

@open_id.setter
def _set_oid(self, value):
self.id = value >> 8


class FieldTest(unittest.TestCase):
"""Base field tests."""

Expand Down Expand Up @@ -454,3 +467,27 @@ def test_set_incorrect(self):

with self.assertRaises(TypeError):
model.collection_field = [some_object]


class CustomGetterSetterTest(unittest.TestCase):
"""Test cases for custom getters and setters feature."""

def test_getter(self):
test_model = CustomGetterSetter()
test_model.id = 1
self.assertEqual(test_model.id, 1)
self.assertEqual(test_model.open_id, 256)

test_model = CustomGetterSetter(id=1)
self.assertEqual(test_model.id, 1)
self.assertEqual(test_model.open_id, 256)

def test_setter(self):
test_model = CustomGetterSetter()
test_model.open_id = 256
self.assertEqual(test_model.id, 1)
self.assertEqual(test_model.open_id, 256)

test_model = CustomGetterSetter(open_id=256)
self.assertEqual(test_model.id, 1)
self.assertEqual(test_model.open_id, 256)
2 changes: 0 additions & 2 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import unittest2 as unittest

import six

from domain_models import models
from domain_models import fields
from domain_models import collections
Expand Down

0 comments on commit 902333b

Please sign in to comment.