Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #26: Fields custom getters and setters. #34

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
47 changes: 30 additions & 17 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
sudo: false
language: python
install: pip install tox
script: tox
python:
- 3.5
env:
- TOXENV=coveralls
- TOXENV=pylint
- TOXENV=flake8
- TOXENV=pydocstyle
- TOXENV=py26
- TOXENV=py27
- TOXENV=py33
- TOXENV=py34
- TOXENV=py35
- TOXENV=pypy
- TOXENV=pypy3
install:
- pip install tox
script:
- tox
language:
- python
matrix:
include:
- python: 2.7
env: TOXENV=coveralls
- python: 2.7
env: TOXENV=pylint
- python: 2.7
env: TOXENV=flake8
- python: 2.7
env: TOXENV=pydocstyle
- python: 2.6
env: TOXENV=py26
- python: 2.7
env: TOXENV=py27
- python: 3.3
env: TOXENV=py33
- python: 3.4
env: TOXENV=py34
- python: 3.5
env: TOXENV=py35
- python: 3.6
env: TOXENV=py36
- python: pypy
env: TOXENV=pypy
notifications:
slack:
secure: QHOypFu1TxCmkbCeNSqY39Bdj+hxOnA7H0zpTZrxDouLFdD+Ah5cKHFOFjFULruCdRo+JI8vPN0fmgRviUcyFVjKbs/3sEEUNU//JJpBPDQB9Epdc8jVp1w1GDF4fN/ujg3XW19U6N1dORRQBFi5EuJn/seYErogIs6F/4G8nEstyTLWqZ7DgnVBMM5aUVuwDae+iUNTutWTvVwJVqS84JC2rFzsRmXc9RJou6/PljBGogE5t+RYHWWAzSodwf3fMIdC7nKk+U6p5IwaDd021vgqk6Dduhu5qum89KsV80hP97qTIiJO/bz5gX6S/DG4cjjXFo2ELZARcRdQ2G7PJBooomiMPW3OTm4uTIbjxDKSIlZm/miHjUVv/+Am5TYgYPnOqU9RlYxAGFbKmkOXDwgJK3nyK8s4hK19cGss3hbRPC7VWRXAaGmO6Yk+UH7wUAbAPSzXvLT/GXc8VlY276KIFVoBX0dLlqSLt/jHrVvKZL1djXKIEa1cDUA9rDqybn+/Oue0QF9ask34lNM2Uu/5IdMAUz8V/U7UHtlfoYFMOJIbgzjUHRLAcIx9+k908rHAOzkMnPns9rDHkJFgJaj3n6Q5HJpQUMwq89Lhb1NHry0nNRE5awtXio6cRcri/ApVxFA+juuyRS64y1Adi8Su/ENAw4CkVeURJuMOqPk=
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
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def run(self):
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy',
'Topic :: Software Development',
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
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tox]
envlist=
coveralls, pylint, flake8, pydocstyle, py26, py27, py33, py34, py35, pypy, pypy3
coveralls, pylint, flake8, pydocstyle, py26, py27, py33, py34, py35, py36, pypy

[testenv]
whitelist_externals=
Expand Down