Revert "Began implementing a shared set of test models to speed up tests."
This reverts commit 22b7870e40
.
This commit is contained in:
parent
dfcce4288a
commit
f5d4849cbe
|
@ -5,7 +5,6 @@ from operator import attrgetter
|
||||||
|
|
||||||
from django.core.exceptions import FieldError
|
from django.core.exceptions import FieldError
|
||||||
from django.test import TestCase, skipUnlessDBFeature
|
from django.test import TestCase, skipUnlessDBFeature
|
||||||
from django.utils import six
|
|
||||||
|
|
||||||
from shared_models.models import Author, Book
|
from shared_models.models import Author, Book
|
||||||
|
|
||||||
|
@ -21,19 +20,19 @@ class LookupTests(TestCase):
|
||||||
self.au2 = Author(name='Author 2')
|
self.au2 = Author(name='Author 2')
|
||||||
self.au2.save()
|
self.au2.save()
|
||||||
# Create a couple of Books.
|
# Create a couple of Books.
|
||||||
self.b1 = Book(title='Book 1', pubdate=datetime(2005, 7, 26), author=self.au1)
|
self.b1 = Book(name='Book 1', pubdate=datetime(2005, 7, 26), author=self.au1)
|
||||||
self.b1.save()
|
self.b1.save()
|
||||||
self.b2 = Book(title='Book 2', pubdate=datetime(2005, 7, 27), author=self.au1)
|
self.b2 = Book(name='Book 2', pubdate=datetime(2005, 7, 27), author=self.au1)
|
||||||
self.b2.save()
|
self.b2.save()
|
||||||
self.b3 = Book(title='Book 3', pubdate=datetime(2005, 7, 27), author=self.au1)
|
self.b3 = Book(name='Book 3', pubdate=datetime(2005, 7, 27), author=self.au1)
|
||||||
self.b3.save()
|
self.b3.save()
|
||||||
self.b4 = Book(title='Book 4', pubdate=datetime(2005, 7, 28), author=self.au1)
|
self.b4 = Book(name='Book 4', pubdate=datetime(2005, 7, 28), author=self.au1)
|
||||||
self.b4.save()
|
self.b4.save()
|
||||||
self.b5 = Book(title='Book 5', pubdate=datetime(2005, 8, 1, 9, 0), author=self.au2)
|
self.b5 = Book(name='Book 5', pubdate=datetime(2005, 8, 1, 9, 0), author=self.au2)
|
||||||
self.b5.save()
|
self.b5.save()
|
||||||
self.b6 = Book(title='Book 6', pubdate=datetime(2005, 8, 1, 8, 0), author=self.au2)
|
self.b6 = Book(name='Book 6', pubdate=datetime(2005, 8, 1, 8, 0), author=self.au2)
|
||||||
self.b6.save()
|
self.b6.save()
|
||||||
self.b7 = Book(title='Book 7', pubdate=datetime(2005, 7, 27), author=self.au2)
|
self.b7 = Book(name='Book 7', pubdate=datetime(2005, 7, 27), author=self.au2)
|
||||||
self.b7.save()
|
self.b7.save()
|
||||||
# Create a few Tags.
|
# Create a few Tags.
|
||||||
self.t1 = Tag(name='Tag 1')
|
self.t1 = Tag(name='Tag 1')
|
||||||
|
@ -86,18 +85,18 @@ class LookupTests(TestCase):
|
||||||
'Book 7',
|
'Book 7',
|
||||||
'Book 1',
|
'Book 1',
|
||||||
],
|
],
|
||||||
transform=attrgetter('title'))
|
transform=attrgetter('name'))
|
||||||
# iterator() can be used on any QuerySet.
|
# iterator() can be used on any QuerySet.
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
Book.objects.filter(title__endswith='4').iterator(),
|
Book.objects.filter(name__endswith='4').iterator(),
|
||||||
['Book 4'],
|
['Book 4'],
|
||||||
transform=attrgetter('title'))
|
transform=attrgetter('name'))
|
||||||
|
|
||||||
def test_count(self):
|
def test_count(self):
|
||||||
# count() returns the number of objects matching search criteria.
|
# count() returns the number of objects matching search criteria.
|
||||||
self.assertEqual(Book.objects.count(), 7)
|
self.assertEqual(Book.objects.count(), 7)
|
||||||
self.assertEqual(Book.objects.filter(pubdate__exact=datetime(2005, 7, 27)).count(), 3)
|
self.assertEqual(Book.objects.filter(pubdate__exact=datetime(2005, 7, 27)).count(), 3)
|
||||||
self.assertEqual(Book.objects.filter(title__startswith='Blah blah').count(), 0)
|
self.assertEqual(Book.objects.filter(name__startswith='Blah blah').count(), 0)
|
||||||
|
|
||||||
# count() should respect sliced query sets.
|
# count() should respect sliced query sets.
|
||||||
articles = Book.objects.all()
|
articles = Book.objects.all()
|
||||||
|
@ -129,43 +128,43 @@ class LookupTests(TestCase):
|
||||||
# values() returns a list of dictionaries instead of object instances --
|
# values() returns a list of dictionaries instead of object instances --
|
||||||
# and you can specify which fields you want to retrieve.
|
# and you can specify which fields you want to retrieve.
|
||||||
identity = lambda x:x
|
identity = lambda x:x
|
||||||
self.assertQuerysetEqual(Book.objects.values('title'),
|
self.assertQuerysetEqual(Book.objects.values('name'),
|
||||||
[
|
[
|
||||||
{'title': 'Book 5'},
|
{'name': 'Book 5'},
|
||||||
{'title': 'Book 6'},
|
{'name': 'Book 6'},
|
||||||
{'title': 'Book 4'},
|
{'name': 'Book 4'},
|
||||||
{'title': 'Book 2'},
|
{'name': 'Book 2'},
|
||||||
{'title': 'Book 3'},
|
{'name': 'Book 3'},
|
||||||
{'title': 'Book 7'},
|
{'name': 'Book 7'},
|
||||||
{'title': 'Book 1'},
|
{'name': 'Book 1'},
|
||||||
],
|
],
|
||||||
transform=identity)
|
transform=identity)
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
Book.objects.filter(pubdate__exact=datetime(2005, 7, 27)).values('id'),
|
Book.objects.filter(pubdate__exact=datetime(2005, 7, 27)).values('id'),
|
||||||
[{'id': self.b2.id}, {'id': self.b3.id}, {'id': self.b7.id}],
|
[{'id': self.b2.id}, {'id': self.b3.id}, {'id': self.b7.id}],
|
||||||
transform=identity)
|
transform=identity)
|
||||||
self.assertQuerysetEqual(Book.objects.values('id', 'title'),
|
self.assertQuerysetEqual(Book.objects.values('id', 'name'),
|
||||||
[
|
[
|
||||||
{'id': self.b5.id, 'title': 'Book 5'},
|
{'id': self.b5.id, 'name': 'Book 5'},
|
||||||
{'id': self.b6.id, 'title': 'Book 6'},
|
{'id': self.b6.id, 'name': 'Book 6'},
|
||||||
{'id': self.b4.id, 'title': 'Book 4'},
|
{'id': self.b4.id, 'name': 'Book 4'},
|
||||||
{'id': self.b2.id, 'title': 'Book 2'},
|
{'id': self.b2.id, 'name': 'Book 2'},
|
||||||
{'id': self.b3.id, 'title': 'Book 3'},
|
{'id': self.b3.id, 'name': 'Book 3'},
|
||||||
{'id': self.b7.id, 'title': 'Book 7'},
|
{'id': self.b7.id, 'name': 'Book 7'},
|
||||||
{'id': self.b1.id, 'title': 'Book 1'},
|
{'id': self.b1.id, 'name': 'Book 1'},
|
||||||
],
|
],
|
||||||
transform=identity)
|
transform=identity)
|
||||||
# You can use values() with iterator() for memory savings,
|
# You can use values() with iterator() for memory savings,
|
||||||
# because iterator() uses database-level iteration.
|
# because iterator() uses database-level iteration.
|
||||||
self.assertQuerysetEqual(Book.objects.values('id', 'title').iterator(),
|
self.assertQuerysetEqual(Book.objects.values('id', 'name').iterator(),
|
||||||
[
|
[
|
||||||
{'title': 'Book 5', 'id': self.b5.id},
|
{'name': 'Book 5', 'id': self.b5.id},
|
||||||
{'title': 'Book 6', 'id': self.b6.id},
|
{'name': 'Book 6', 'id': self.b6.id},
|
||||||
{'title': 'Book 4', 'id': self.b4.id},
|
{'name': 'Book 4', 'id': self.b4.id},
|
||||||
{'title': 'Book 2', 'id': self.b2.id},
|
{'name': 'Book 2', 'id': self.b2.id},
|
||||||
{'title': 'Book 3', 'id': self.b3.id},
|
{'name': 'Book 3', 'id': self.b3.id},
|
||||||
{'title': 'Book 7', 'id': self.b7.id},
|
{'name': 'Book 7', 'id': self.b7.id},
|
||||||
{'title': 'Book 1', 'id': self.b1.id},
|
{'name': 'Book 1', 'id': self.b1.id},
|
||||||
],
|
],
|
||||||
transform=identity)
|
transform=identity)
|
||||||
# The values() method works with "extra" fields specified in extra(select).
|
# The values() method works with "extra" fields specified in extra(select).
|
||||||
|
@ -205,39 +204,39 @@ class LookupTests(TestCase):
|
||||||
}], transform=identity)
|
}], transform=identity)
|
||||||
# You can specify fields from forward and reverse relations, just like filter().
|
# You can specify fields from forward and reverse relations, just like filter().
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
Book.objects.values('title', 'author__name'),
|
Book.objects.values('name', 'author__name'),
|
||||||
[
|
[
|
||||||
{'title': self.b5.title, 'author__name': self.au2.name},
|
{'name': self.b5.name, 'author__name': self.au2.name},
|
||||||
{'title': self.b6.title, 'author__name': self.au2.name},
|
{'name': self.b6.name, 'author__name': self.au2.name},
|
||||||
{'title': self.b4.title, 'author__name': self.au1.name},
|
{'name': self.b4.name, 'author__name': self.au1.name},
|
||||||
{'title': self.b2.title, 'author__name': self.au1.name},
|
{'name': self.b2.name, 'author__name': self.au1.name},
|
||||||
{'title': self.b3.title, 'author__name': self.au1.name},
|
{'name': self.b3.name, 'author__name': self.au1.name},
|
||||||
{'title': self.b7.title, 'author__name': self.au2.name},
|
{'name': self.b7.name, 'author__name': self.au2.name},
|
||||||
{'title': self.b1.title, 'author__name': self.au1.name},
|
{'name': self.b1.name, 'author__name': self.au1.name},
|
||||||
], transform=identity)
|
], transform=identity)
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
Author.objects.values('name', 'book__title').order_by('name', 'book__title'),
|
Author.objects.values('name', 'book__name').order_by('name', 'book__name'),
|
||||||
[
|
[
|
||||||
{'name': self.au1.name, 'book__title': self.b1.title},
|
{'name': self.au1.name, 'book__name': self.b1.name},
|
||||||
{'name': self.au1.name, 'book__title': self.b2.title},
|
{'name': self.au1.name, 'book__name': self.b2.name},
|
||||||
{'name': self.au1.name, 'book__title': self.b3.title},
|
{'name': self.au1.name, 'book__name': self.b3.name},
|
||||||
{'name': self.au1.name, 'book__title': self.b4.title},
|
{'name': self.au1.name, 'book__name': self.b4.name},
|
||||||
{'name': self.au2.name, 'book__title': self.b5.title},
|
{'name': self.au2.name, 'book__name': self.b5.name},
|
||||||
{'name': self.au2.name, 'book__title': self.b6.title},
|
{'name': self.au2.name, 'book__name': self.b6.name},
|
||||||
{'name': self.au2.name, 'book__title': self.b7.title},
|
{'name': self.au2.name, 'book__name': self.b7.name},
|
||||||
], transform=identity)
|
], transform=identity)
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
Author.objects.values('name', 'book__title', 'book__tag__name').order_by('name', 'book__title', 'book__tag__name'),
|
Author.objects.values('name', 'book__name', 'book__tag__name').order_by('name', 'book__name', 'book__tag__name'),
|
||||||
[
|
[
|
||||||
{'name': self.au1.name, 'book__title': self.b1.title, 'book__tag__name': self.t1.name},
|
{'name': self.au1.name, 'book__name': self.b1.name, 'book__tag__name': self.t1.name},
|
||||||
{'name': self.au1.name, 'book__title': self.b2.title, 'book__tag__name': self.t1.name},
|
{'name': self.au1.name, 'book__name': self.b2.name, 'book__tag__name': self.t1.name},
|
||||||
{'name': self.au1.name, 'book__title': self.b3.title, 'book__tag__name': self.t1.name},
|
{'name': self.au1.name, 'book__name': self.b3.name, 'book__tag__name': self.t1.name},
|
||||||
{'name': self.au1.name, 'book__title': self.b3.title, 'book__tag__name': self.t2.name},
|
{'name': self.au1.name, 'book__name': self.b3.name, 'book__tag__name': self.t2.name},
|
||||||
{'name': self.au1.name, 'book__title': self.b4.title, 'book__tag__name': self.t2.name},
|
{'name': self.au1.name, 'book__name': self.b4.name, 'book__tag__name': self.t2.name},
|
||||||
{'name': self.au2.name, 'book__title': self.b5.title, 'book__tag__name': self.t2.name},
|
{'name': self.au2.name, 'book__name': self.b5.name, 'book__tag__name': self.t2.name},
|
||||||
{'name': self.au2.name, 'book__title': self.b5.title, 'book__tag__name': self.t3.name},
|
{'name': self.au2.name, 'book__name': self.b5.name, 'book__tag__name': self.t3.name},
|
||||||
{'name': self.au2.name, 'book__title': self.b6.title, 'book__tag__name': self.t3.name},
|
{'name': self.au2.name, 'book__name': self.b6.name, 'book__tag__name': self.t3.name},
|
||||||
{'name': self.au2.name, 'book__title': self.b7.title, 'book__tag__name': self.t3.name},
|
{'name': self.au2.name, 'book__name': self.b7.name, 'book__tag__name': self.t3.name},
|
||||||
], transform=identity)
|
], transform=identity)
|
||||||
# However, an exception FieldDoesNotExist will be thrown if you specify
|
# However, an exception FieldDoesNotExist will be thrown if you specify
|
||||||
# a non-existent field name in values() (a field that is neither in the
|
# a non-existent field name in values() (a field that is neither in the
|
||||||
|
@ -250,7 +249,7 @@ class LookupTests(TestCase):
|
||||||
[{
|
[{
|
||||||
'id': self.b5.id,
|
'id': self.b5.id,
|
||||||
'author_id': self.au2.id,
|
'author_id': self.au2.id,
|
||||||
'title': 'Book 5',
|
'name': 'Book 5',
|
||||||
'pages': 0,
|
'pages': 0,
|
||||||
'pubdate': datetime(2005, 8, 1, 9, 0)
|
'pubdate': datetime(2005, 8, 1, 9, 0)
|
||||||
}], transform=identity)
|
}], transform=identity)
|
||||||
|
@ -261,7 +260,7 @@ class LookupTests(TestCase):
|
||||||
# Within each tuple, the order of the elements is the same as the order
|
# Within each tuple, the order of the elements is the same as the order
|
||||||
# of fields in the values_list() call.
|
# of fields in the values_list() call.
|
||||||
identity = lambda x:x
|
identity = lambda x:x
|
||||||
self.assertQuerysetEqual(Book.objects.values_list('title'),
|
self.assertQuerysetEqual(Book.objects.values_list('name'),
|
||||||
[
|
[
|
||||||
('Book 5',),
|
('Book 5',),
|
||||||
('Book 6',),
|
('Book 6',),
|
||||||
|
@ -310,19 +309,19 @@ class LookupTests(TestCase):
|
||||||
],
|
],
|
||||||
transform=identity)
|
transform=identity)
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
Author.objects.values_list('name', 'book__title', 'book__tag__name').order_by('name', 'book__title', 'book__tag__name'),
|
Author.objects.values_list('name', 'book__name', 'book__tag__name').order_by('name', 'book__name', 'book__tag__name'),
|
||||||
[
|
[
|
||||||
(self.au1.name, self.b1.title, self.t1.name),
|
(self.au1.name, self.b1.name, self.t1.name),
|
||||||
(self.au1.name, self.b2.title, self.t1.name),
|
(self.au1.name, self.b2.name, self.t1.name),
|
||||||
(self.au1.name, self.b3.title, self.t1.name),
|
(self.au1.name, self.b3.name, self.t1.name),
|
||||||
(self.au1.name, self.b3.title, self.t2.name),
|
(self.au1.name, self.b3.name, self.t2.name),
|
||||||
(self.au1.name, self.b4.title, self.t2.name),
|
(self.au1.name, self.b4.name, self.t2.name),
|
||||||
(self.au2.name, self.b5.title, self.t2.name),
|
(self.au2.name, self.b5.name, self.t2.name),
|
||||||
(self.au2.name, self.b5.title, self.t3.name),
|
(self.au2.name, self.b5.name, self.t3.name),
|
||||||
(self.au2.name, self.b6.title, self.t3.name),
|
(self.au2.name, self.b6.name, self.t3.name),
|
||||||
(self.au2.name, self.b7.title, self.t3.name),
|
(self.au2.name, self.b7.name, self.t3.name),
|
||||||
], transform=identity)
|
], transform=identity)
|
||||||
self.assertRaises(TypeError, Book.objects.values_list, 'id', 'title', flat=True)
|
self.assertRaises(TypeError, Book.objects.values_list, 'id', 'name', flat=True)
|
||||||
|
|
||||||
def test_get_next_previous_by(self):
|
def test_get_next_previous_by(self):
|
||||||
# Every DateField and DateTimeField creates get_next_by_FOO() and
|
# Every DateField and DateTimeField creates get_next_by_FOO() and
|
||||||
|
@ -333,7 +332,7 @@ class LookupTests(TestCase):
|
||||||
'<Book: Book 2>')
|
'<Book: Book 2>')
|
||||||
self.assertEqual(repr(self.b2.get_next_by_pubdate()),
|
self.assertEqual(repr(self.b2.get_next_by_pubdate()),
|
||||||
'<Book: Book 3>')
|
'<Book: Book 3>')
|
||||||
self.assertEqual(repr(self.b2.get_next_by_pubdate(title__endswith='6')),
|
self.assertEqual(repr(self.b2.get_next_by_pubdate(name__endswith='6')),
|
||||||
'<Book: Book 6>')
|
'<Book: Book 6>')
|
||||||
self.assertEqual(repr(self.b3.get_next_by_pubdate()),
|
self.assertEqual(repr(self.b3.get_next_by_pubdate()),
|
||||||
'<Book: Book 7>')
|
'<Book: Book 7>')
|
||||||
|
@ -361,9 +360,9 @@ class LookupTests(TestCase):
|
||||||
def test_escaping(self):
|
def test_escaping(self):
|
||||||
# Underscores, percent signs and backslashes have special meaning in the
|
# Underscores, percent signs and backslashes have special meaning in the
|
||||||
# underlying SQL code, but Django handles the quoting of them automatically.
|
# underlying SQL code, but Django handles the quoting of them automatically.
|
||||||
b8 = Book(title='Book_ with underscore', pubdate=datetime(2005, 11, 20))
|
b8 = Book(name='Book_ with underscore', pubdate=datetime(2005, 11, 20))
|
||||||
b8.save()
|
b8.save()
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__startswith='Book'),
|
self.assertQuerysetEqual(Book.objects.filter(name__startswith='Book'),
|
||||||
[
|
[
|
||||||
'<Book: Book_ with underscore>',
|
'<Book: Book_ with underscore>',
|
||||||
'<Book: Book 5>',
|
'<Book: Book 5>',
|
||||||
|
@ -374,11 +373,11 @@ class LookupTests(TestCase):
|
||||||
'<Book: Book 7>',
|
'<Book: Book 7>',
|
||||||
'<Book: Book 1>',
|
'<Book: Book 1>',
|
||||||
])
|
])
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__startswith='Book_'),
|
self.assertQuerysetEqual(Book.objects.filter(name__startswith='Book_'),
|
||||||
['<Book: Book_ with underscore>'])
|
['<Book: Book_ with underscore>'])
|
||||||
b9 = Book(title='Book% with percent sign', pubdate=datetime(2005, 11, 21))
|
b9 = Book(name='Book% with percent sign', pubdate=datetime(2005, 11, 21))
|
||||||
b9.save()
|
b9.save()
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__startswith='Book'),
|
self.assertQuerysetEqual(Book.objects.filter(name__startswith='Book'),
|
||||||
[
|
[
|
||||||
'<Book: Book% with percent sign>',
|
'<Book: Book% with percent sign>',
|
||||||
'<Book: Book_ with underscore>',
|
'<Book: Book_ with underscore>',
|
||||||
|
@ -390,21 +389,21 @@ class LookupTests(TestCase):
|
||||||
'<Book: Book 7>',
|
'<Book: Book 7>',
|
||||||
'<Book: Book 1>',
|
'<Book: Book 1>',
|
||||||
])
|
])
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__startswith='Book%'),
|
self.assertQuerysetEqual(Book.objects.filter(name__startswith='Book%'),
|
||||||
['<Book: Book% with percent sign>'])
|
['<Book: Book% with percent sign>'])
|
||||||
b10 = Book(title='Book with \\ backslash', pubdate=datetime(2005, 11, 22))
|
b10 = Book(name='Book with \\ backslash', pubdate=datetime(2005, 11, 22))
|
||||||
b10.save()
|
b10.save()
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__contains='\\'),
|
self.assertQuerysetEqual(Book.objects.filter(name__contains='\\'),
|
||||||
['<Book: Book with \ backslash>'])
|
['<Book: Book with \ backslash>'])
|
||||||
|
|
||||||
def test_exclude(self):
|
def test_exclude(self):
|
||||||
b8 = Book.objects.create(title='Book_ with underscore', pubdate=datetime(2005, 11, 20))
|
b8 = Book.objects.create(name='Book_ with underscore', pubdate=datetime(2005, 11, 20))
|
||||||
b9 = Book.objects.create(title='Book% with percent sign', pubdate=datetime(2005, 11, 21))
|
b9 = Book.objects.create(name='Book% with percent sign', pubdate=datetime(2005, 11, 21))
|
||||||
b10 = Book.objects.create(title='Book with \\ backslash', pubdate=datetime(2005, 11, 22))
|
b10 = Book.objects.create(name='Book with \\ backslash', pubdate=datetime(2005, 11, 22))
|
||||||
|
|
||||||
# exclude() is the opposite of filter() when doing lookups:
|
# exclude() is the opposite of filter() when doing lookups:
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
Book.objects.filter(title__contains='Book').exclude(title__contains='with'),
|
Book.objects.filter(name__contains='Book').exclude(name__contains='with'),
|
||||||
[
|
[
|
||||||
'<Book: Book 5>',
|
'<Book: Book 5>',
|
||||||
'<Book: Book 6>',
|
'<Book: Book 6>',
|
||||||
|
@ -414,7 +413,7 @@ class LookupTests(TestCase):
|
||||||
'<Book: Book 7>',
|
'<Book: Book 7>',
|
||||||
'<Book: Book 1>',
|
'<Book: Book 1>',
|
||||||
])
|
])
|
||||||
self.assertQuerysetEqual(Book.objects.exclude(title__startswith="Book_"),
|
self.assertQuerysetEqual(Book.objects.exclude(name__startswith="Book_"),
|
||||||
[
|
[
|
||||||
'<Book: Book with \\ backslash>',
|
'<Book: Book with \\ backslash>',
|
||||||
'<Book: Book% with percent sign>',
|
'<Book: Book% with percent sign>',
|
||||||
|
@ -426,7 +425,7 @@ class LookupTests(TestCase):
|
||||||
'<Book: Book 7>',
|
'<Book: Book 7>',
|
||||||
'<Book: Book 1>',
|
'<Book: Book 1>',
|
||||||
])
|
])
|
||||||
self.assertQuerysetEqual(Book.objects.exclude(title="Book 7"),
|
self.assertQuerysetEqual(Book.objects.exclude(name="Book 7"),
|
||||||
[
|
[
|
||||||
'<Book: Book with \\ backslash>',
|
'<Book: Book with \\ backslash>',
|
||||||
'<Book: Book% with percent sign>',
|
'<Book: Book% with percent sign>',
|
||||||
|
@ -443,12 +442,12 @@ class LookupTests(TestCase):
|
||||||
# none() returns a QuerySet that behaves like any other QuerySet object
|
# none() returns a QuerySet that behaves like any other QuerySet object
|
||||||
self.assertQuerysetEqual(Book.objects.none(), [])
|
self.assertQuerysetEqual(Book.objects.none(), [])
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
Book.objects.none().filter(title__startswith='Book'), [])
|
Book.objects.none().filter(name__startswith='Book'), [])
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
Book.objects.filter(title__startswith='Book').none(), [])
|
Book.objects.filter(name__startswith='Book').none(), [])
|
||||||
self.assertEqual(Book.objects.none().count(), 0)
|
self.assertEqual(Book.objects.none().count(), 0)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
Book.objects.none().update(title="This should not take effect"), 0)
|
Book.objects.none().update(name="This should not take effect"), 0)
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
[article for article in Book.objects.none().iterator()],
|
[article for article in Book.objects.none().iterator()],
|
||||||
[])
|
[])
|
||||||
|
@ -469,41 +468,46 @@ class LookupTests(TestCase):
|
||||||
|
|
||||||
def test_error_messages(self):
|
def test_error_messages(self):
|
||||||
# Programming errors are pointed out with nice error messages
|
# Programming errors are pointed out with nice error messages
|
||||||
with six.assertRaisesRegex(self, FieldError, "Cannot resolve keyword 'pubdate_year' "
|
try:
|
||||||
"into field. Choices are: .+"):
|
|
||||||
Book.objects.filter(pubdate_year='2005').count()
|
Book.objects.filter(pubdate_year='2005').count()
|
||||||
|
self.fail('FieldError not raised')
|
||||||
with self.assertRaises(FieldError, msg="Join on field 'title' not permitted. "
|
except FieldError as ex:
|
||||||
"Did you misspell 'starts' for the lookup type?"):
|
self.assertEqual(str(ex), "Cannot resolve keyword 'pubdate_year' "
|
||||||
Book.objects.filter(title__starts='Book')
|
"into field. Choices are: author, id, name, pages, pubdate, tag, tags")
|
||||||
|
try:
|
||||||
|
Book.objects.filter(name__starts='Book')
|
||||||
|
self.fail('FieldError not raised')
|
||||||
|
except FieldError as ex:
|
||||||
|
self.assertEqual(str(ex), "Join on field 'name' not permitted. "
|
||||||
|
"Did you misspell 'starts' for the lookup type?")
|
||||||
|
|
||||||
def test_regex(self):
|
def test_regex(self):
|
||||||
# Create some articles with a bit more interesting names for testing field lookups:
|
# Create some articles with a bit more interesting names for testing field lookups:
|
||||||
for a in Book.objects.all():
|
for a in Book.objects.all():
|
||||||
a.delete()
|
a.delete()
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
b1 = Book(pubdate=now, title='f')
|
b1 = Book(pubdate=now, name='f')
|
||||||
b1.save()
|
b1.save()
|
||||||
b2 = Book(pubdate=now, title='fo')
|
b2 = Book(pubdate=now, name='fo')
|
||||||
b2.save()
|
b2.save()
|
||||||
b3 = Book(pubdate=now, title='foo')
|
b3 = Book(pubdate=now, name='foo')
|
||||||
b3.save()
|
b3.save()
|
||||||
b4 = Book(pubdate=now, title='fooo')
|
b4 = Book(pubdate=now, name='fooo')
|
||||||
b4.save()
|
b4.save()
|
||||||
b5 = Book(pubdate=now, title='hey-Foo')
|
b5 = Book(pubdate=now, name='hey-Foo')
|
||||||
b5.save()
|
b5.save()
|
||||||
b6 = Book(pubdate=now, title='bar')
|
b6 = Book(pubdate=now, name='bar')
|
||||||
b6.save()
|
b6.save()
|
||||||
b7 = Book(pubdate=now, title='AbBa')
|
b7 = Book(pubdate=now, name='AbBa')
|
||||||
b7.save()
|
b7.save()
|
||||||
b8 = Book(pubdate=now, title='baz')
|
b8 = Book(pubdate=now, name='baz')
|
||||||
b8.save()
|
b8.save()
|
||||||
b9 = Book(pubdate=now, title='baxZ')
|
b9 = Book(pubdate=now, name='baxZ')
|
||||||
b9.save()
|
b9.save()
|
||||||
# zero-or-more
|
# zero-or-more
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__regex=r'fo*'),
|
self.assertQuerysetEqual(Book.objects.filter(name__regex=r'fo*'),
|
||||||
['<Book: f>', '<Book: fo>', '<Book: foo>', '<Book: fooo>'])
|
['<Book: f>', '<Book: fo>', '<Book: foo>', '<Book: fooo>'])
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__iregex=r'fo*'),
|
self.assertQuerysetEqual(Book.objects.filter(name__iregex=r'fo*'),
|
||||||
[
|
[
|
||||||
'<Book: f>',
|
'<Book: f>',
|
||||||
'<Book: fo>',
|
'<Book: fo>',
|
||||||
|
@ -512,54 +516,54 @@ class LookupTests(TestCase):
|
||||||
'<Book: hey-Foo>',
|
'<Book: hey-Foo>',
|
||||||
])
|
])
|
||||||
# one-or-more
|
# one-or-more
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__regex=r'fo+'),
|
self.assertQuerysetEqual(Book.objects.filter(name__regex=r'fo+'),
|
||||||
['<Book: fo>', '<Book: foo>', '<Book: fooo>'])
|
['<Book: fo>', '<Book: foo>', '<Book: fooo>'])
|
||||||
# wildcard
|
# wildcard
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__regex=r'fooo?'),
|
self.assertQuerysetEqual(Book.objects.filter(name__regex=r'fooo?'),
|
||||||
['<Book: foo>', '<Book: fooo>'])
|
['<Book: foo>', '<Book: fooo>'])
|
||||||
# leading anchor
|
# leading anchor
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__regex=r'^b'),
|
self.assertQuerysetEqual(Book.objects.filter(name__regex=r'^b'),
|
||||||
['<Book: bar>', '<Book: baxZ>', '<Book: baz>'])
|
['<Book: bar>', '<Book: baxZ>', '<Book: baz>'])
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__iregex=r'^a'),
|
self.assertQuerysetEqual(Book.objects.filter(name__iregex=r'^a'),
|
||||||
['<Book: AbBa>'])
|
['<Book: AbBa>'])
|
||||||
# trailing anchor
|
# trailing anchor
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__regex=r'z$'),
|
self.assertQuerysetEqual(Book.objects.filter(name__regex=r'z$'),
|
||||||
['<Book: baz>'])
|
['<Book: baz>'])
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__iregex=r'z$'),
|
self.assertQuerysetEqual(Book.objects.filter(name__iregex=r'z$'),
|
||||||
['<Book: baxZ>', '<Book: baz>'])
|
['<Book: baxZ>', '<Book: baz>'])
|
||||||
# character sets
|
# character sets
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__regex=r'ba[rz]'),
|
self.assertQuerysetEqual(Book.objects.filter(name__regex=r'ba[rz]'),
|
||||||
['<Book: bar>', '<Book: baz>'])
|
['<Book: bar>', '<Book: baz>'])
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__regex=r'ba.[RxZ]'),
|
self.assertQuerysetEqual(Book.objects.filter(name__regex=r'ba.[RxZ]'),
|
||||||
['<Book: baxZ>'])
|
['<Book: baxZ>'])
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__iregex=r'ba[RxZ]'),
|
self.assertQuerysetEqual(Book.objects.filter(name__iregex=r'ba[RxZ]'),
|
||||||
['<Book: bar>', '<Book: baxZ>', '<Book: baz>'])
|
['<Book: bar>', '<Book: baxZ>', '<Book: baz>'])
|
||||||
|
|
||||||
# and more articles:
|
# and more articles:
|
||||||
b10 = Book(pubdate=now, title='foobar')
|
b10 = Book(pubdate=now, name='foobar')
|
||||||
b10.save()
|
b10.save()
|
||||||
b11 = Book(pubdate=now, title='foobaz')
|
b11 = Book(pubdate=now, name='foobaz')
|
||||||
b11.save()
|
b11.save()
|
||||||
b12 = Book(pubdate=now, title='ooF')
|
b12 = Book(pubdate=now, name='ooF')
|
||||||
b12.save()
|
b12.save()
|
||||||
b13 = Book(pubdate=now, title='foobarbaz')
|
b13 = Book(pubdate=now, name='foobarbaz')
|
||||||
b13.save()
|
b13.save()
|
||||||
b14 = Book(pubdate=now, title='zoocarfaz')
|
b14 = Book(pubdate=now, name='zoocarfaz')
|
||||||
b14.save()
|
b14.save()
|
||||||
b15 = Book(pubdate=now, title='barfoobaz')
|
b15 = Book(pubdate=now, name='barfoobaz')
|
||||||
b15.save()
|
b15.save()
|
||||||
b16 = Book(pubdate=now, title='bazbaRFOO')
|
b16 = Book(pubdate=now, name='bazbaRFOO')
|
||||||
b16.save()
|
b16.save()
|
||||||
|
|
||||||
# alternation
|
# alternation
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__regex=r'oo(f|b)'),
|
self.assertQuerysetEqual(Book.objects.filter(name__regex=r'oo(f|b)'),
|
||||||
[
|
[
|
||||||
'<Book: barfoobaz>',
|
'<Book: barfoobaz>',
|
||||||
'<Book: foobar>',
|
'<Book: foobar>',
|
||||||
'<Book: foobarbaz>',
|
'<Book: foobarbaz>',
|
||||||
'<Book: foobaz>',
|
'<Book: foobaz>',
|
||||||
])
|
])
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__iregex=r'oo(f|b)'),
|
self.assertQuerysetEqual(Book.objects.filter(name__iregex=r'oo(f|b)'),
|
||||||
[
|
[
|
||||||
'<Book: barfoobaz>',
|
'<Book: barfoobaz>',
|
||||||
'<Book: foobar>',
|
'<Book: foobar>',
|
||||||
|
@ -567,11 +571,11 @@ class LookupTests(TestCase):
|
||||||
'<Book: foobaz>',
|
'<Book: foobaz>',
|
||||||
'<Book: ooF>',
|
'<Book: ooF>',
|
||||||
])
|
])
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__regex=r'^foo(f|b)'),
|
self.assertQuerysetEqual(Book.objects.filter(name__regex=r'^foo(f|b)'),
|
||||||
['<Book: foobar>', '<Book: foobarbaz>', '<Book: foobaz>'])
|
['<Book: foobar>', '<Book: foobarbaz>', '<Book: foobaz>'])
|
||||||
|
|
||||||
# greedy matching
|
# greedy matching
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__regex=r'b.*az'),
|
self.assertQuerysetEqual(Book.objects.filter(name__regex=r'b.*az'),
|
||||||
[
|
[
|
||||||
'<Book: barfoobaz>',
|
'<Book: barfoobaz>',
|
||||||
'<Book: baz>',
|
'<Book: baz>',
|
||||||
|
@ -579,7 +583,7 @@ class LookupTests(TestCase):
|
||||||
'<Book: foobarbaz>',
|
'<Book: foobarbaz>',
|
||||||
'<Book: foobaz>',
|
'<Book: foobaz>',
|
||||||
])
|
])
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__iregex=r'b.*ar'),
|
self.assertQuerysetEqual(Book.objects.filter(name__iregex=r'b.*ar'),
|
||||||
[
|
[
|
||||||
'<Book: bar>',
|
'<Book: bar>',
|
||||||
'<Book: barfoobaz>',
|
'<Book: barfoobaz>',
|
||||||
|
@ -592,21 +596,21 @@ class LookupTests(TestCase):
|
||||||
def test_regex_backreferencing(self):
|
def test_regex_backreferencing(self):
|
||||||
# grouping and backreferences
|
# grouping and backreferences
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
b10 = Book(pubdate=now, title='foobar')
|
b10 = Book(pubdate=now, name='foobar')
|
||||||
b10.save()
|
b10.save()
|
||||||
b11 = Book(pubdate=now, title='foobaz')
|
b11 = Book(pubdate=now, name='foobaz')
|
||||||
b11.save()
|
b11.save()
|
||||||
b12 = Book(pubdate=now, title='ooF')
|
b12 = Book(pubdate=now, name='ooF')
|
||||||
b12.save()
|
b12.save()
|
||||||
b13 = Book(pubdate=now, title='foobarbaz')
|
b13 = Book(pubdate=now, name='foobarbaz')
|
||||||
b13.save()
|
b13.save()
|
||||||
b14 = Book(pubdate=now, title='zoocarfaz')
|
b14 = Book(pubdate=now, name='zoocarfaz')
|
||||||
b14.save()
|
b14.save()
|
||||||
b15 = Book(pubdate=now, title='barfoobaz')
|
b15 = Book(pubdate=now, name='barfoobaz')
|
||||||
b15.save()
|
b15.save()
|
||||||
b16 = Book(pubdate=now, title='bazbaRFOO')
|
b16 = Book(pubdate=now, name='bazbaRFOO')
|
||||||
b16.save()
|
b16.save()
|
||||||
self.assertQuerysetEqual(Book.objects.filter(title__regex=r'b(.).*b\1'),
|
self.assertQuerysetEqual(Book.objects.filter(name__regex=r'b(.).*b\1'),
|
||||||
['<Book: barfoobaz>', '<Book: bazbaRFOO>', '<Book: foobarbaz>'])
|
['<Book: barfoobaz>', '<Book: bazbaRFOO>', '<Book: foobarbaz>'])
|
||||||
|
|
||||||
def test_nonfield_lookups(self):
|
def test_nonfield_lookups(self):
|
||||||
|
|
|
@ -17,7 +17,6 @@ from django.db import models
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
from django.utils.encoding import python_2_unicode_compatible
|
from django.utils.encoding import python_2_unicode_compatible
|
||||||
|
|
||||||
from shared_models.models import Author, Book
|
|
||||||
|
|
||||||
temp_storage_dir = tempfile.mkdtemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
|
temp_storage_dir = tempfile.mkdtemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
|
||||||
temp_storage = FileSystemStorage(temp_storage_dir)
|
temp_storage = FileSystemStorage(temp_storage_dir)
|
||||||
|
@ -46,13 +45,23 @@ class Category(models.Model):
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return self.__str__()
|
return self.__str__()
|
||||||
|
|
||||||
|
@python_2_unicode_compatible
|
||||||
|
class Writer(models.Model):
|
||||||
|
name = models.CharField(max_length=50, help_text='Use both first and last names.')
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
ordering = ('name',)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
@python_2_unicode_compatible
|
@python_2_unicode_compatible
|
||||||
class Article(models.Model):
|
class Article(models.Model):
|
||||||
headline = models.CharField(max_length=50)
|
headline = models.CharField(max_length=50)
|
||||||
slug = models.SlugField()
|
slug = models.SlugField()
|
||||||
pub_date = models.DateField()
|
pub_date = models.DateField()
|
||||||
created = models.DateField(editable=False)
|
created = models.DateField(editable=False)
|
||||||
writer = models.ForeignKey(Author)
|
writer = models.ForeignKey(Writer)
|
||||||
article = models.TextField()
|
article = models.TextField()
|
||||||
categories = models.ManyToManyField(Category, blank=True)
|
categories = models.ManyToManyField(Category, blank=True)
|
||||||
status = models.PositiveIntegerField(choices=ARTICLE_STATUS, blank=True, null=True)
|
status = models.PositiveIntegerField(choices=ARTICLE_STATUS, blank=True, null=True)
|
||||||
|
@ -72,12 +81,12 @@ class ImprovedArticle(models.Model):
|
||||||
class ImprovedArticleWithParentLink(models.Model):
|
class ImprovedArticleWithParentLink(models.Model):
|
||||||
article = models.OneToOneField(Article, parent_link=True)
|
article = models.OneToOneField(Article, parent_link=True)
|
||||||
|
|
||||||
class BetterAuthor(Author):
|
class BetterWriter(Writer):
|
||||||
score = models.IntegerField()
|
score = models.IntegerField()
|
||||||
|
|
||||||
@python_2_unicode_compatible
|
@python_2_unicode_compatible
|
||||||
class AuthorProfile(models.Model):
|
class WriterProfile(models.Model):
|
||||||
writer = models.OneToOneField(Author, primary_key=True)
|
writer = models.OneToOneField(Writer, primary_key=True)
|
||||||
age = models.PositiveIntegerField()
|
age = models.PositiveIntegerField()
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
@ -177,6 +186,14 @@ class Inventory(models.Model):
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return self.__str__()
|
return self.__str__()
|
||||||
|
|
||||||
|
class Book(models.Model):
|
||||||
|
title = models.CharField(max_length=40)
|
||||||
|
author = models.ForeignKey(Writer, blank=True, null=True)
|
||||||
|
special_id = models.IntegerField(blank=True, null=True, unique=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
unique_together = ('title', 'author')
|
||||||
|
|
||||||
class BookXtra(models.Model):
|
class BookXtra(models.Model):
|
||||||
isbn = models.CharField(max_length=16, unique=True)
|
isbn = models.CharField(max_length=16, unique=True)
|
||||||
suffix1 = models.IntegerField(blank=True, default=0)
|
suffix1 = models.IntegerField(blank=True, default=0)
|
||||||
|
|
|
@ -17,13 +17,11 @@ from django.utils.unittest import skipUnless
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
|
|
||||||
from shared_models.models import Author, Book
|
from .models import (Article, ArticleStatus, BetterWriter, BigInt, Book,
|
||||||
|
|
||||||
from .models import (Article, ArticleStatus, BetterAuthor, BigInt,
|
|
||||||
Category, CommaSeparatedInteger, CustomFieldForExclusionModel, DerivedBook,
|
Category, CommaSeparatedInteger, CustomFieldForExclusionModel, DerivedBook,
|
||||||
DerivedPost, ExplicitPK, FlexibleDatePost, ImprovedArticle,
|
DerivedPost, ExplicitPK, FlexibleDatePost, ImprovedArticle,
|
||||||
ImprovedArticleWithParentLink, Inventory, Post, Price,
|
ImprovedArticleWithParentLink, Inventory, Post, Price,
|
||||||
Product, TextFile, AuthorProfile, Colour, ColourfulItem,
|
Product, TextFile, Writer, WriterProfile, Colour, ColourfulItem,
|
||||||
ArticleStatusNote, DateTimePost, test_images)
|
ArticleStatusNote, DateTimePost, test_images)
|
||||||
|
|
||||||
if test_images:
|
if test_images:
|
||||||
|
@ -54,13 +52,11 @@ class PriceForm(forms.ModelForm):
|
||||||
|
|
||||||
class BookForm(forms.ModelForm):
|
class BookForm(forms.ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
fields = ['title', 'author', 'pubdate']
|
|
||||||
model = Book
|
model = Book
|
||||||
|
|
||||||
|
|
||||||
class DerivedBookForm(forms.ModelForm):
|
class DerivedBookForm(forms.ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
fields = ['title', 'author', 'isbn', 'suffix1', 'suffix2']
|
|
||||||
model = DerivedBook
|
model = DerivedBook
|
||||||
|
|
||||||
|
|
||||||
|
@ -88,11 +84,11 @@ class DerivedPostForm(forms.ModelForm):
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
class CustomAuthorForm(forms.ModelForm):
|
class CustomWriterForm(forms.ModelForm):
|
||||||
name = forms.CharField(required=False)
|
name = forms.CharField(required=False)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Author
|
model = Writer
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
|
@ -128,7 +124,7 @@ class PartialArticleForm(forms.ModelForm):
|
||||||
|
|
||||||
class RoykoForm(forms.ModelForm):
|
class RoykoForm(forms.ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Author
|
model = Writer
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
|
@ -188,15 +184,14 @@ class ImprovedArticleWithParentLinkForm(forms.ModelForm):
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
class BetterAuthorForm(forms.ModelForm):
|
class BetterWriterForm(forms.ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = BetterAuthor
|
model = BetterWriter
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
|
class WriterProfileForm(forms.ModelForm):
|
||||||
class AuthorProfileForm(forms.ModelForm):
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = AuthorProfile
|
model = WriterProfile
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
|
@ -324,14 +319,14 @@ class ModelFormBaseTest(TestCase):
|
||||||
forms.fields.BooleanField)
|
forms.fields.BooleanField)
|
||||||
|
|
||||||
def test_override_field(self):
|
def test_override_field(self):
|
||||||
class AuthorForm(forms.ModelForm):
|
class WriterForm(forms.ModelForm):
|
||||||
book = forms.CharField(required=False)
|
book = forms.CharField(required=False)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Author
|
model = Writer
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
wf = AuthorForm({'name': 'Richard Lockridge'})
|
wf = WriterForm({'name': 'Richard Lockridge'})
|
||||||
self.assertTrue(wf.is_valid())
|
self.assertTrue(wf.is_valid())
|
||||||
|
|
||||||
def test_limit_nonexistent_field(self):
|
def test_limit_nonexistent_field(self):
|
||||||
|
@ -556,7 +551,7 @@ class ValidationTest(TestCase):
|
||||||
assert form.is_valid()
|
assert form.is_valid()
|
||||||
|
|
||||||
def test_notrequired_overrides_notblank(self):
|
def test_notrequired_overrides_notblank(self):
|
||||||
form = CustomAuthorForm({})
|
form = CustomWriterForm({})
|
||||||
assert form.is_valid()
|
assert form.is_valid()
|
||||||
|
|
||||||
|
|
||||||
|
@ -565,7 +560,7 @@ class ValidationTest(TestCase):
|
||||||
# unique/unique_together validation
|
# unique/unique_together validation
|
||||||
class UniqueTest(TestCase):
|
class UniqueTest(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.author = Author.objects.create(name='Mike Royko')
|
self.writer = Writer.objects.create(name='Mike Royko')
|
||||||
|
|
||||||
def test_simple_unique(self):
|
def test_simple_unique(self):
|
||||||
form = ProductForm({'slug': 'teddy-bear-blue'})
|
form = ProductForm({'slug': 'teddy-bear-blue'})
|
||||||
|
@ -589,31 +584,33 @@ class UniqueTest(TestCase):
|
||||||
|
|
||||||
def test_unique_null(self):
|
def test_unique_null(self):
|
||||||
title = 'I May Be Wrong But I Doubt It'
|
title = 'I May Be Wrong But I Doubt It'
|
||||||
form = BookForm({'title': title, 'author': self.author.pk, 'pubdate': '2012-12-12 00:00:00'})
|
form = BookForm({'title': title, 'author': self.writer.pk})
|
||||||
self.assertTrue(form.is_valid())
|
self.assertTrue(form.is_valid())
|
||||||
form.save()
|
form.save()
|
||||||
form = BookForm({'title': title, 'author': self.author.pk, 'pubdate': '2012-12-12 00:00:00'})
|
form = BookForm({'title': title, 'author': self.writer.pk})
|
||||||
self.assertFalse(form.is_valid())
|
self.assertFalse(form.is_valid())
|
||||||
self.assertEqual(len(form.errors), 1)
|
self.assertEqual(len(form.errors), 1)
|
||||||
self.assertEqual(form.errors['__all__'], ['Book with this Title and Author already exists.'])
|
self.assertEqual(form.errors['__all__'], ['Book with this Title and Author already exists.'])
|
||||||
form = BookForm({'title': title, 'pubdate': '2012-12-12 00:00:00'})
|
form = BookForm({'title': title})
|
||||||
self.assertTrue(form.is_valid())
|
self.assertTrue(form.is_valid())
|
||||||
form.save()
|
form.save()
|
||||||
form = BookForm({'title': title, 'pubdate': '2012-12-12 00:00:00'})
|
form = BookForm({'title': title})
|
||||||
self.assertTrue(form.is_valid())
|
self.assertTrue(form.is_valid())
|
||||||
|
|
||||||
def test_inherited_unique(self):
|
def test_inherited_unique(self):
|
||||||
form = BetterAuthorForm({'name': 'Mike Royko', 'score': 3})
|
title = 'Boss'
|
||||||
|
Book.objects.create(title=title, author=self.writer, special_id=1)
|
||||||
|
form = DerivedBookForm({'title': 'Other', 'author': self.writer.pk, 'special_id': '1', 'isbn': '12345'})
|
||||||
self.assertFalse(form.is_valid())
|
self.assertFalse(form.is_valid())
|
||||||
self.assertEqual(len(form.errors), 1)
|
self.assertEqual(len(form.errors), 1)
|
||||||
self.assertEqual(form.errors['name'], ['Author with this Name already exists.'])
|
self.assertEqual(form.errors['special_id'], ['Book with this Special id already exists.'])
|
||||||
|
|
||||||
def test_inherited_unique_together(self):
|
def test_inherited_unique_together(self):
|
||||||
title = 'Boss'
|
title = 'Boss'
|
||||||
form = BookForm({'title': title, 'author': self.author.pk, 'pubdate': '2012-12-12 00:00:00'})
|
form = BookForm({'title': title, 'author': self.writer.pk})
|
||||||
self.assertTrue(form.is_valid())
|
self.assertTrue(form.is_valid())
|
||||||
form.save()
|
form.save()
|
||||||
form = DerivedBookForm({'title': title, 'author': self.author.pk, 'isbn': '12345'})
|
form = DerivedBookForm({'title': title, 'author': self.writer.pk, 'isbn': '12345'})
|
||||||
self.assertFalse(form.is_valid())
|
self.assertFalse(form.is_valid())
|
||||||
self.assertEqual(len(form.errors), 1)
|
self.assertEqual(len(form.errors), 1)
|
||||||
self.assertEqual(form.errors['__all__'], ['Book with this Title and Author already exists.'])
|
self.assertEqual(form.errors['__all__'], ['Book with this Title and Author already exists.'])
|
||||||
|
@ -621,9 +618,8 @@ class UniqueTest(TestCase):
|
||||||
def test_abstract_inherited_unique(self):
|
def test_abstract_inherited_unique(self):
|
||||||
title = 'Boss'
|
title = 'Boss'
|
||||||
isbn = '12345'
|
isbn = '12345'
|
||||||
dbook = DerivedBook.objects.create(title=title, author=self.author, isbn=isbn,
|
dbook = DerivedBook.objects.create(title=title, author=self.writer, isbn=isbn)
|
||||||
pubdate='2012-12-12 00:00')
|
form = DerivedBookForm({'title': 'Other', 'author': self.writer.pk, 'isbn': isbn})
|
||||||
form = DerivedBookForm({'title': 'Other', 'author': self.author.pk, 'isbn': isbn})
|
|
||||||
self.assertFalse(form.is_valid())
|
self.assertFalse(form.is_valid())
|
||||||
self.assertEqual(len(form.errors), 1)
|
self.assertEqual(len(form.errors), 1)
|
||||||
self.assertEqual(form.errors['isbn'], ['Derived book with this Isbn already exists.'])
|
self.assertEqual(form.errors['isbn'], ['Derived book with this Isbn already exists.'])
|
||||||
|
@ -631,11 +627,10 @@ class UniqueTest(TestCase):
|
||||||
def test_abstract_inherited_unique_together(self):
|
def test_abstract_inherited_unique_together(self):
|
||||||
title = 'Boss'
|
title = 'Boss'
|
||||||
isbn = '12345'
|
isbn = '12345'
|
||||||
dbook = DerivedBook.objects.create(title=title, author=self.author, isbn=isbn,
|
dbook = DerivedBook.objects.create(title=title, author=self.writer, isbn=isbn)
|
||||||
pubdate='2012-12-12 00:00')
|
|
||||||
form = DerivedBookForm({
|
form = DerivedBookForm({
|
||||||
'title': 'Other',
|
'title': 'Other',
|
||||||
'author': self.author.pk,
|
'author': self.writer.pk,
|
||||||
'isbn': '9876',
|
'isbn': '9876',
|
||||||
'suffix1': '0',
|
'suffix1': '0',
|
||||||
'suffix2': '0'
|
'suffix2': '0'
|
||||||
|
@ -753,7 +748,7 @@ class ModelToDictTests(TestCase):
|
||||||
]
|
]
|
||||||
for c in categories:
|
for c in categories:
|
||||||
c.save()
|
c.save()
|
||||||
writer = Author(name='Test writer')
|
writer = Writer(name='Test writer')
|
||||||
writer.save()
|
writer.save()
|
||||||
|
|
||||||
art = Article(
|
art = Article(
|
||||||
|
@ -862,10 +857,10 @@ class OldFormForXTests(TestCase):
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
f.save()
|
f.save()
|
||||||
|
|
||||||
# Create a couple of Authors.
|
# Create a couple of Writers.
|
||||||
w_royko = Author(name='Mike Royko')
|
w_royko = Writer(name='Mike Royko')
|
||||||
w_royko.save()
|
w_royko.save()
|
||||||
w_woodward = Author(name='Bob Woodward')
|
w_woodward = Writer(name='Bob Woodward')
|
||||||
w_woodward.save()
|
w_woodward.save()
|
||||||
# ManyToManyFields are represented by a MultipleChoiceField, ForeignKeys and any
|
# ManyToManyFields are represented by a MultipleChoiceField, ForeignKeys and any
|
||||||
# fields with the 'choices' attribute are represented by a ChoiceField.
|
# fields with the 'choices' attribute are represented by a ChoiceField.
|
||||||
|
@ -903,9 +898,9 @@ class OldFormForXTests(TestCase):
|
||||||
|
|
||||||
# When the ModelForm is passed an instance, that instance's current values are
|
# When the ModelForm is passed an instance, that instance's current values are
|
||||||
# inserted as 'initial' data in each Field.
|
# inserted as 'initial' data in each Field.
|
||||||
w = Author.objects.get(name='Mike Royko')
|
w = Writer.objects.get(name='Mike Royko')
|
||||||
f = RoykoForm(auto_id=False, instance=w)
|
f = RoykoForm(auto_id=False, instance=w)
|
||||||
self.assertHTMLEqual(six.text_type(f), '''<tr><th>Name:</th><td><input type="text" name="name" value="Mike Royko" maxlength="100" /><br /><span class="helptext">Use both first and last names.</span></td></tr>''')
|
self.assertHTMLEqual(six.text_type(f), '''<tr><th>Name:</th><td><input type="text" name="name" value="Mike Royko" maxlength="50" /><br /><span class="helptext">Use both first and last names.</span></td></tr>''')
|
||||||
|
|
||||||
art = Article(
|
art = Article(
|
||||||
headline='Test article',
|
headline='Test article',
|
||||||
|
@ -1117,7 +1112,7 @@ class OldFormForXTests(TestCase):
|
||||||
|
|
||||||
c4 = Category.objects.create(name='Fourth', url='4th')
|
c4 = Category.objects.create(name='Fourth', url='4th')
|
||||||
self.assertEqual(c4.name, 'Fourth')
|
self.assertEqual(c4.name, 'Fourth')
|
||||||
w_bernstein = Author.objects.create(name='Carl Bernstein')
|
w_bernstein = Writer.objects.create(name='Carl Bernstein')
|
||||||
self.assertEqual(w_bernstein.name, 'Carl Bernstein')
|
self.assertEqual(w_bernstein.name, 'Carl Bernstein')
|
||||||
self.assertHTMLEqual(f.as_ul(), '''<li>Headline: <input type="text" name="headline" maxlength="50" /></li>
|
self.assertHTMLEqual(f.as_ul(), '''<li>Headline: <input type="text" name="headline" maxlength="50" /></li>
|
||||||
<li>Slug: <input type="text" name="slug" maxlength="50" /></li>
|
<li>Slug: <input type="text" name="slug" maxlength="50" /></li>
|
||||||
|
@ -1294,17 +1289,17 @@ class OldFormForXTests(TestCase):
|
||||||
|
|
||||||
self.assertEqual(list(ImprovedArticleWithParentLinkForm.base_fields), [])
|
self.assertEqual(list(ImprovedArticleWithParentLinkForm.base_fields), [])
|
||||||
|
|
||||||
bw = BetterAuthor(name='Joe Better', score=10)
|
bw = BetterWriter(name='Joe Better', score=10)
|
||||||
bw.save()
|
bw.save()
|
||||||
self.assertEqual(sorted(model_to_dict(bw)),
|
self.assertEqual(sorted(model_to_dict(bw)),
|
||||||
['author_ptr', 'id', 'name', 'score'])
|
['id', 'name', 'score', 'writer_ptr'])
|
||||||
|
|
||||||
form = BetterAuthorForm({'name': 'Some Name', 'score': 12})
|
form = BetterWriterForm({'name': 'Some Name', 'score': 12})
|
||||||
self.assertEqual(form.is_valid(), True)
|
self.assertEqual(form.is_valid(), True)
|
||||||
bw2 = form.save()
|
bw2 = form.save()
|
||||||
bw2.delete()
|
bw2.delete()
|
||||||
|
|
||||||
form = AuthorProfileForm()
|
form = WriterProfileForm()
|
||||||
self.assertHTMLEqual(form.as_p(), '''<p><label for="id_writer">Writer:</label> <select name="writer" id="id_writer">
|
self.assertHTMLEqual(form.as_p(), '''<p><label for="id_writer">Writer:</label> <select name="writer" id="id_writer">
|
||||||
<option value="" selected="selected">---------</option>
|
<option value="" selected="selected">---------</option>
|
||||||
<option value="%s">Bob Woodward</option>
|
<option value="%s">Bob Woodward</option>
|
||||||
|
@ -1318,11 +1313,11 @@ class OldFormForXTests(TestCase):
|
||||||
'writer': six.text_type(w_woodward.pk),
|
'writer': six.text_type(w_woodward.pk),
|
||||||
'age': '65',
|
'age': '65',
|
||||||
}
|
}
|
||||||
form = AuthorProfileForm(data)
|
form = WriterProfileForm(data)
|
||||||
instance = form.save()
|
instance = form.save()
|
||||||
self.assertEqual(six.text_type(instance), 'Bob Woodward is 65')
|
self.assertEqual(six.text_type(instance), 'Bob Woodward is 65')
|
||||||
|
|
||||||
form = AuthorProfileForm(instance=instance)
|
form = WriterProfileForm(instance=instance)
|
||||||
self.assertHTMLEqual(form.as_p(), '''<p><label for="id_writer">Writer:</label> <select name="writer" id="id_writer">
|
self.assertHTMLEqual(form.as_p(), '''<p><label for="id_writer">Writer:</label> <select name="writer" id="id_writer">
|
||||||
<option value="">---------</option>
|
<option value="">---------</option>
|
||||||
<option value="%s" selected="selected">Bob Woodward</option>
|
<option value="%s" selected="selected">Bob Woodward</option>
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
from __future__ import unicode_literals
|
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.encoding import python_2_unicode_compatible
|
from django.utils.encoding import python_2_unicode_compatible
|
||||||
|
@ -11,11 +9,7 @@ class Tag(models.Model):
|
||||||
|
|
||||||
@python_2_unicode_compatible
|
@python_2_unicode_compatible
|
||||||
class Author(models.Model):
|
class Author(models.Model):
|
||||||
name = models.CharField(max_length=100, help_text='Use both first and last names.',
|
name = models.CharField(max_length=100)
|
||||||
unique=True)
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
ordering = ['name']
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
@ -23,15 +17,14 @@ class Author(models.Model):
|
||||||
|
|
||||||
@python_2_unicode_compatible
|
@python_2_unicode_compatible
|
||||||
class Book(models.Model):
|
class Book(models.Model):
|
||||||
title = models.CharField(max_length=200)
|
name = models.CharField(max_length=200)
|
||||||
pages = models.IntegerField(default=0)
|
pages = models.IntegerField(default=0)
|
||||||
author = models.ForeignKey(Author, null=True, blank=True)
|
author = models.ForeignKey(Author, null=True)
|
||||||
pubdate = models.DateTimeField()
|
pubdate = models.DateTimeField()
|
||||||
tags = models.ManyToManyField(Tag)
|
tags = models.ManyToManyField(Tag)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
ordering = ['-pubdate', 'title']
|
ordering = ['-pubdate', 'name']
|
||||||
unique_together = ['title', 'author']
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.title
|
return self.name
|
||||||
|
|
|
@ -77,7 +77,7 @@ class SignalsRegressTests(TestCase):
|
||||||
"Is created"
|
"Is created"
|
||||||
])
|
])
|
||||||
|
|
||||||
b1 = Book(title='Snow Crash', pubdate='2012-02-02 12:00')
|
b1 = Book(name='Snow Crash', pubdate='2012-02-02 12:00')
|
||||||
self.assertEqual(self.get_signal_output(b1.save), [
|
self.assertEqual(self.get_signal_output(b1.save), [
|
||||||
"pre_save signal, Snow Crash",
|
"pre_save signal, Snow Crash",
|
||||||
"post_save signal, Snow Crash",
|
"post_save signal, Snow Crash",
|
||||||
|
@ -87,7 +87,7 @@ class SignalsRegressTests(TestCase):
|
||||||
def test_m2m_signals(self):
|
def test_m2m_signals(self):
|
||||||
""" Assigning and removing to/from m2m shouldn't generate an m2m signal """
|
""" Assigning and removing to/from m2m shouldn't generate an m2m signal """
|
||||||
|
|
||||||
b1 = Book(title='Snow Crash', pubdate='2012-02-02 12:00')
|
b1 = Book(name='Snow Crash', pubdate='2012-02-02 12:00')
|
||||||
self.get_signal_output(b1.save)
|
self.get_signal_output(b1.save)
|
||||||
a1 = Author(name='Neal Stephenson')
|
a1 = Author(name='Neal Stephenson')
|
||||||
self.get_signal_output(a1.save)
|
self.get_signal_output(a1.save)
|
||||||
|
|
Loading…
Reference in New Issue