Skip to content

Commit

Permalink
Add ModelForm test and some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jieter committed Jan 24, 2018
1 parent ac3a249 commit 92bcbc0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
1 change: 0 additions & 1 deletion modeltrans/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def __init__(self, original_field, language=None, *args, **kwargs):

self.blank = kwargs['blank']
self.null = kwargs['null']
self.editable = kwargs.get('editable', True)

self.concrete = False

Expand Down
42 changes: 32 additions & 10 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.test import TestCase
from django.forms import ModelForm
from django.test import TestCase
from django.utils.translation import override

from .app.models import Blog


class ModelFormTest(TestCase):
def test_modelform(self):
class BlogForm(ModelForm):
class Meta:
model = Blog
fields = ('title_i18n', 'body_i18n', )

article = Blog(title='English', title_nl='Nederlands')

from modeltrans.migration import I18nDataMigration, I18nIndexMigration, get_translatable_models
with override('nl'):
form = BlogForm(instance=article, data={
'title_i18n': 'Nederlandse taal',
'body_i18n': 'foo'
})
form.save()

from .app.models import Blog, Category
article.refresh_from_db()
self.assertEqual(article.title_nl, 'Nederlandse taal')
self.assertEqual(article.title_en, 'English')

with override('en'):
form = BlogForm(instance=article, data={
'title_i18n': 'English language',
'body_i18n': 'foo'
})
form.save()

# class ModelFormTest(TestCase):
# def test_modelform(self):
#
# class BlogForm(ModelForm):
# class Meta:
# model = Blog
# fields = ('title_i18n', 'body_i18n', )
article.refresh_from_db()
self.assertEqual(article.title_nl, 'Nederlandse taal')
self.assertEqual(article.title_en, 'English language')
16 changes: 8 additions & 8 deletions tests/test_querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@


def key(queryset, key, sep=None):
l = list([getattr(model, key) for model in queryset])
items = list([getattr(model, key) for model in queryset])
if sep is not None:
l = sep.join(l)
return l
items = sep.join(items)
return items


class GetFieldTest(TestCase):
Expand Down Expand Up @@ -303,19 +303,19 @@ def setUpTestData(self):
def test_regular_fields(self):
qs = Blog.objects.all().order_by('-title')

self.assertEquals(key(qs, 'title'), 'G,F,E,D,C,B,A'.split(','))
self.assertEquals(key(qs, 'title', sep=' '), 'G F E D C B A')

def test_order_by_two_fields(self):
'''Multiple translated fields should work too'''
qs = Blog.objects.all().order_by('-title_fr', 'title_nl')

self.assertEquals(key(qs, 'title_nl'), 'X Y Z A B C D'.split())
self.assertEquals(key(qs, 'title_nl', sep=' '), 'X Y Z A B C D')

def test_order_asc(self):
qs = Blog.objects.all().order_by('title_nl')

self.assertEquals(key(qs, 'title_nl'), sorted(self.NL))
self.assertEquals(key(qs, 'title'), 'A B C D G F E'.split())
self.assertEquals(key(qs, 'title', sep=' '), 'A B C D G F E')

def test_order_desc(self):
qs = Blog.objects.all().order_by('-title_nl')
Expand Down Expand Up @@ -395,10 +395,10 @@ def test_annotate_upper(self):

def test_annotate_length(self):
with override('nl'):
qs = Blog.objects.annotate(l=models.functions.Length('title_i18n'))
qs = Blog.objects.annotate(len=models.functions.Length('title_i18n'))

self.assertEquals(
list(qs.values_list('l', flat=True)),
list(qs.values_list('len', flat=True)),
list(map(len, ['VALK', 'VULTURE', 'BAT', 'DOLFIN', 'ZEBRA']))
)

Expand Down
5 changes: 4 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ commands =

[testenv:flake8]
basepython = python2.7
commands = flake8
deps = flake8==3.5.0
skip_install = True
commands = flake8

[flake8]
exclude = **/migrations/*.py,.git,.tox,__pycache__
Expand All @@ -61,6 +62,7 @@ max-line-length = 120
[testenv:isort]
whitelist_externals = make
deps = isort==4.2.15
skip_install = True
basepython = python3.6
commands = make isort

Expand All @@ -69,5 +71,6 @@ basepython = python3.6
whitelist_externals = make
changedir = docs
commands = make html
skip_install = True
deps =
-r{toxinidir}/docs/requirements.txt

0 comments on commit 92bcbc0

Please sign in to comment.