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 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 10:15:34 +00:00
|
|
|
from .models import Book
|
|
|
|
|
|
|
|
|
|
|
|
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']
|
2014-10-13 12:52:13 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
def test_form(self):
|
2016-09-22 10:15:34 +00:00
|
|
|
form_class = modelform_factory(Book, fields='__all__')
|
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')
|