Skip to content

Commit

Permalink
Added tests for fields module
Browse files Browse the repository at this point in the history
  • Loading branch information
Relrin committed Nov 3, 2016
1 parent 73dd826 commit 65e248e
Show file tree
Hide file tree
Showing 8 changed files with 1,354 additions and 36 deletions.
8 changes: 0 additions & 8 deletions aiorest_ws/db/orm/django/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@


try:
# DecimalValidator is unavailable in Django < 1.9
from django.core.validators import DecimalValidator
except ImportError:
DecimalValidator = None
Expand All @@ -32,7 +31,6 @@
postgres_fields = None


# JSONField is only supported from 1.9 onwards
try:
from django.contrib.postgres.fields import JSONField
except ImportError:
Expand Down Expand Up @@ -70,16 +68,10 @@ def get_related_model(field):
# field.rel is deprecated from 1.9 onwards
def get_remote_field(field, **kwargs):
if 'default' in kwargs:
if django.VERSION < (1, 9):
return getattr(field, 'rel', kwargs['default'])
return getattr(field, 'remote_field', kwargs['default'])

if django.VERSION < (1, 9):
return field.rel
return field.remote_field


def value_from_object(field, obj):
if django.VERSION < (1, 9):
return field._get_val_from_obj(obj)
return field.value_from_object(obj)
30 changes: 3 additions & 27 deletions aiorest_ws/db/orm/django/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

from aiorest_ws.conf import settings
from aiorest_ws.db.orm import fields
from aiorest_ws.db.orm.abstract import SkipField
from aiorest_ws.db.orm.django.compat import get_remote_field, \
value_from_object
from aiorest_ws.db.orm.fields import empty
Expand Down Expand Up @@ -156,11 +155,10 @@ class ModelField(fields.ModelField):
}

def __init__(self, model_field, **kwargs):
self.model_field = model_field
# The `max_length` option is supported by Django's base `Field` class,
# so we'd better support it here.
max_length = kwargs.pop('max_length', None)
super(ModelField, self).__init__(**kwargs)
super(ModelField, self).__init__(model_field, **kwargs)
if max_length is not None:
message = self.error_messages['max_length'].format(
max_length=max_length
Expand Down Expand Up @@ -190,30 +188,8 @@ class SerializerMethodField(fields.SerializerMethodField):
pass


class CreateOnlyDefault(object):
"""
This class may be used to provide default values that are only used
for create operations, but that do not return any value for update
operations.
"""
def __init__(self, default):
self.default = default

def set_context(self, serializer_field):
self.is_update = serializer_field.parent.instance is not None
has_set_context = hasattr(self.default, 'set_context')
if callable(self.default) and has_set_context and not self.is_update:
self.default.set_context(serializer_field)

def __call__(self):
if self.is_update:
raise SkipField()
if callable(self.default):
return self.default()
return self.default

def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, repr(self.default))
class CreateOnlyDefault(fields.CreateOnlyDefault):
pass


class EmailField(CharField):
Expand Down
2 changes: 1 addition & 1 deletion aiorest_ws/utils/date/dateparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,5 @@ def parse_duration(value):
sign = -1 if kw.pop('sign', '+') == '-' else 1
if kw.get('microseconds'):
kw['microseconds'] = kw['microseconds'].ljust(6, '0')
kw = {k: float(v) for k, v in iter(kw) if v is not None}
kw = {k: float(v) for k, v in kw.items() if v is not None}
return sign * datetime.timedelta(**kw)
15 changes: 15 additions & 0 deletions tests/db/orm/django/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
from django import setup
from django.conf import settings


if not settings.configured:
settings.configure(
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
}
},
)
setup()
31 changes: 31 additions & 0 deletions tests/db/orm/django/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
import unittest

from django.apps import apps
from django.db import connections


class DjangoUnitTest(unittest.TestCase):

models = []
apps = ()

@classmethod
def setUpClass(cls):
super(DjangoUnitTest, cls).setUpClass()
if apps:
apps.populate(cls.apps)

conn = connections['default']
with conn.schema_editor() as editor:
for model in cls.models:
editor.create_model(model)

@classmethod
def tearDownClass(cls):
super(DjangoUnitTest, cls).tearDownClass()

conn = connections['default']
with conn.schema_editor() as editor:
for model in cls.models:
editor.delete_model(model)
Loading

0 comments on commit 65e248e

Please sign in to comment.