2013-11-26 18:20:19 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright (c) 2013 by Pablo Martín <goinnn@gmail.com>
|
|
|
|
#
|
|
|
|
# This software is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This software is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with this software. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-09-22 20:12:56 +00:00
|
|
|
import sys
|
|
|
|
|
2016-09-22 10:15:34 +00:00
|
|
|
from django import VERSION
|
2013-11-26 19:25:01 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2013-11-26 19:04:41 +00:00
|
|
|
from django.forms.models import modelform_factory
|
2013-11-26 18:20:19 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
|
2016-09-22 16:54:17 +00:00
|
|
|
from multiselectfield.utils import get_max_length
|
|
|
|
|
2016-09-23 11:55:08 +00:00
|
|
|
from .models import Book, PROVINCES, STATES, PROVINCES_AND_STATES
|
2016-09-22 10:15:34 +00:00
|
|
|
|
|
|
|
|
2016-09-22 20:12:56 +00:00
|
|
|
if sys.version_info < (3,):
|
|
|
|
u = unicode
|
|
|
|
else:
|
|
|
|
u = str
|
|
|
|
|
|
|
|
|
2016-09-22 10:15:34 +00:00
|
|
|
if VERSION < (1, 9):
|
|
|
|
def get_field(model, name):
|
|
|
|
return model._meta.get_field_by_name(name)[0]
|
|
|
|
else:
|
|
|
|
def get_field(model, name):
|
|
|
|
return model._meta.get_field(name)
|
2013-11-26 18:20:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MultiSelectTestCase(TestCase):
|
|
|
|
|
2014-10-13 13:57:44 +00:00
|
|
|
fixtures = ['app_data.json']
|
2016-09-23 11:55:08 +00:00
|
|
|
maxDiff = 4000
|
2014-10-13 12:52:13 +00:00
|
|
|
|
2016-09-22 20:12:56 +00:00
|
|
|
def assertListEqual(self, left, right, msg=None):
|
|
|
|
if sys.version_info >= (3, 2):
|
|
|
|
# Added in Python 3.2
|
|
|
|
self.assertCountEqual(left, right, msg=msg)
|
|
|
|
else:
|
|
|
|
# Manually check list equality
|
|
|
|
self.assertEqual(len(left), len(right), msg=msg)
|
|
|
|
for i, tag_list in enumerate(left):
|
|
|
|
for j, tag in enumerate(tag_list):
|
|
|
|
self.assertEqual(tag, right[i][j], msg=msg)
|
|
|
|
|
|
|
|
def assertStringEqual(self, left, right, msg=None):
|
|
|
|
_msg = "Chars in position %%d differ: %%s != %%s. %s" % msg
|
|
|
|
|
|
|
|
# Compare characters individually
|
|
|
|
for i, chars in enumerate(zip(left, right)):
|
|
|
|
self.assertEqual(chars[0], chars[1], msg=_msg % (i, chars[0], chars[1]))
|
|
|
|
|
2013-11-26 18:20:19 +00:00
|
|
|
def test_filter(self):
|
|
|
|
self.assertEqual(Book.objects.filter(tags__contains='sex').count(), 1)
|
|
|
|
self.assertEqual(Book.objects.filter(tags__contains='boring').count(), 0)
|
2013-11-26 19:04:41 +00:00
|
|
|
|
2016-09-22 20:12:56 +00:00
|
|
|
def test_values_list(self):
|
|
|
|
tag_list_list = Book.objects.all().values_list('tags', flat=True)
|
|
|
|
categories_list_list = Book.objects.all().values_list('categories', flat=True)
|
|
|
|
|
|
|
|
# Workaround for Django bug #9619
|
|
|
|
# https://code.djangoproject.com/ticket/9619
|
|
|
|
# For Django 1.6 and 1.7, calling values() or values_list() doesn't
|
|
|
|
# call Field.from_db_field, it simply returns a Python representation
|
|
|
|
# of the data in the database (which in our case is a string of
|
|
|
|
# comma-separated values). The bug was fixed in Django 1.8+.
|
|
|
|
if VERSION >= (1, 6) and VERSION < (1, 8):
|
|
|
|
self.assertStringEqual(tag_list_list, [u('sex,work,happy')])
|
|
|
|
self.assertStringEqual(categories_list_list, [u('1,3,5')])
|
|
|
|
else:
|
|
|
|
self.assertListEqual(tag_list_list, [['sex', 'work', 'happy']])
|
|
|
|
self.assertListEqual(categories_list_list, [['1', '3', '5']])
|
|
|
|
|
2013-11-26 19:04:41 +00:00
|
|
|
def test_form(self):
|
2016-09-22 20:12:56 +00:00
|
|
|
form_class = modelform_factory(Book, fields=('title', 'tags', 'categories'))
|
2013-11-26 19:04:41 +00:00
|
|
|
self.assertEqual(len(form_class.base_fields), 3)
|
|
|
|
form = form_class({'title': 'new book',
|
|
|
|
'categories': '1,2'})
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
|
|
|
|
def test_object(self):
|
2013-11-26 19:34:25 +00:00
|
|
|
book = Book.objects.get(id=1)
|
2013-11-26 19:04:41 +00:00
|
|
|
self.assertEqual(book.get_tags_display(), 'Sex, Work, Happy')
|
2014-10-13 14:24:40 +00:00
|
|
|
self.assertEqual(book.get_tags_list(), ['Sex', 'Work', 'Happy'])
|
2013-11-26 19:04:41 +00:00
|
|
|
self.assertEqual(book.get_categories_display(), 'Handbooks and manuals by discipline, Books of literary criticism, Books about literature')
|
2014-10-13 14:24:40 +00:00
|
|
|
self.assertEqual(book.get_categories_list(), ['Handbooks and manuals by discipline', 'Books of literary criticism', 'Books about literature'])
|
|
|
|
|
|
|
|
self.assertEqual(book.get_tags_list(), book.get_tags_display().split(', '))
|
|
|
|
self.assertEqual(book.get_categories_list(), book.get_categories_display().split(', '))
|
2013-11-26 19:25:01 +00:00
|
|
|
|
|
|
|
def test_validate(self):
|
2013-11-26 19:34:25 +00:00
|
|
|
book = Book.objects.get(id=1)
|
2016-09-22 10:15:34 +00:00
|
|
|
get_field(Book, 'tags').clean(['sex', 'work'], book)
|
2013-11-26 19:25:01 +00:00
|
|
|
try:
|
2016-09-22 10:15:34 +00:00
|
|
|
get_field(Book, 'tags').clean(['sex1', 'work'], book)
|
2013-11-26 19:25:01 +00:00
|
|
|
raise AssertionError()
|
|
|
|
except ValidationError:
|
|
|
|
pass
|
|
|
|
|
2016-09-22 10:15:34 +00:00
|
|
|
get_field(Book, 'categories').clean(['1', '2', '3'], book)
|
2013-11-26 19:25:01 +00:00
|
|
|
try:
|
2016-09-22 10:15:34 +00:00
|
|
|
get_field(Book, 'categories').clean(['1', '2', '3', '4'], book)
|
2013-11-26 19:25:01 +00:00
|
|
|
raise AssertionError()
|
|
|
|
except ValidationError:
|
|
|
|
pass
|
|
|
|
try:
|
2016-09-22 10:15:34 +00:00
|
|
|
get_field(Book, 'categories').clean(['11', '12', '13'], book)
|
2013-11-26 19:25:01 +00:00
|
|
|
raise AssertionError()
|
|
|
|
except ValidationError:
|
|
|
|
pass
|
2013-11-26 19:34:25 +00:00
|
|
|
|
|
|
|
def test_serializer(self):
|
|
|
|
book = Book.objects.get(id=1)
|
2016-09-22 10:15:34 +00:00
|
|
|
self.assertEqual(get_field(Book, 'tags').value_to_string(book), 'sex,work,happy')
|
|
|
|
self.assertEqual(get_field(Book, 'categories').value_to_string(book), '1,3,5')
|
2016-09-22 16:54:17 +00:00
|
|
|
|
2016-09-23 10:53:51 +00:00
|
|
|
def test_flatchoices(self):
|
|
|
|
self.assertEqual(get_field(Book, 'published_in').flatchoices, list(PROVINCES+STATES))
|
|
|
|
|
2016-09-23 11:55:08 +00:00
|
|
|
def test_named_groups(self):
|
|
|
|
self.assertEqual(get_field(Book, 'published_in').choices, PROVINCES_AND_STATES)
|
|
|
|
|
|
|
|
def test_named_groups_form(self):
|
|
|
|
form_class = modelform_factory(Book, fields=('published_in',))
|
|
|
|
self.assertEqual(len(form_class.base_fields), 1)
|
|
|
|
form = form_class(initial={'published_in': ['BC', 'AK']})
|
|
|
|
|
|
|
|
expected_html = u("""<p><label for="id_published_in_0">Province or State:</label> <ul id="id_published_in"><li>Canada - Provinces<ul id="id_published_in_0"><li><label for="id_published_in_0_0"><input id="id_published_in_0_0" name="published_in" type="checkbox" value="AB" /> Alberta</label></li>\n"""
|
|
|
|
"""<li><label for="id_published_in_0_1"><input checked="checked" id="id_published_in_0_1" name="published_in" type="checkbox" value="BC" /> British Columbia</label></li></ul></li>\n"""
|
|
|
|
"""<li>USA - States<ul id="id_published_in_1"><li><label for="id_published_in_1_0"><input checked="checked" id="id_published_in_1_0" name="published_in" type="checkbox" value="AK" /> Alaska</label></li>\n"""
|
|
|
|
"""<li><label for="id_published_in_1_1"><input id="id_published_in_1_1" name="published_in" type="checkbox" value="AL" /> Alabama</label></li>\n"""
|
|
|
|
"""<li><label for="id_published_in_1_2"><input id="id_published_in_1_2" name="published_in" type="checkbox" value="AZ" /> Arizona</label></li></ul></li></ul></p>""")
|
|
|
|
actual_html = form.as_p()
|
|
|
|
|
|
|
|
if VERSION < (1, 6) or VERSION >= (1, 7):
|
|
|
|
# Django 1.6 renders the Python repr() for each group (eg: tuples
|
|
|
|
# with HTML entities), so we skip the test for that version
|
|
|
|
self.assertEqual(expected_html.replace('\n', ''), actual_html.replace('\n', ''))
|
|
|
|
|
2016-09-22 16:54:17 +00:00
|
|
|
|
|
|
|
class MultiSelectUtilsTestCase(TestCase):
|
|
|
|
def test_get_max_length_max_length_is_not_none(self):
|
2016-09-22 20:12:56 +00:00
|
|
|
self.assertEqual(get_max_length([], 5), 5)
|
2016-09-22 16:54:17 +00:00
|
|
|
|
|
|
|
def test_get_max_length_max_length_is_none_and_choices_is_empty(self):
|
2016-09-22 20:12:56 +00:00
|
|
|
self.assertEqual(get_max_length([], None), 200)
|
2016-09-22 16:54:17 +00:00
|
|
|
|
|
|
|
def test_get_max_length_max_length_is_none_and_choices_is_not_empty(self):
|
|
|
|
choices = [
|
|
|
|
('key1', 'value1'),
|
|
|
|
('key2', 'value2'),
|
|
|
|
('key3', 'value3'),
|
|
|
|
]
|
2016-09-22 20:12:56 +00:00
|
|
|
self.assertEqual(get_max_length(choices, None), 14)
|