summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/modeltests/aggregation/models.py321
-rw-r--r--tests/modeltests/aggregation/tests.py565
-rw-r--r--tests/modeltests/choices/models.py26
-rw-r--r--tests/modeltests/choices/tests.py23
-rw-r--r--tests/modeltests/custom_columns/models.py65
-rw-r--r--tests/modeltests/custom_columns/tests.py71
-rw-r--r--tests/modeltests/custom_managers/models.py48
-rw-r--r--tests/modeltests/custom_managers/tests.py71
-rw-r--r--tests/modeltests/custom_methods/models.py23
-rw-r--r--tests/modeltests/custom_methods/tests.py42
-rw-r--r--tests/modeltests/custom_pk/fields.py1
-rw-r--r--tests/modeltests/custom_pk/models.py135
-rw-r--r--tests/modeltests/custom_pk/tests.py183
-rw-r--r--tests/modeltests/defer/models.py164
-rw-r--r--tests/modeltests/defer/tests.py137
-rw-r--r--tests/modeltests/delete/models.py165
-rw-r--r--tests/modeltests/delete/tests.py135
-rw-r--r--tests/modeltests/empty/models.py16
-rw-r--r--tests/modeltests/empty/tests.py15
-rw-r--r--tests/modeltests/expressions/models.py105
-rw-r--r--tests/modeltests/expressions/tests.py218
-rw-r--r--tests/modeltests/field_defaults/models.py38
-rw-r--r--tests/modeltests/field_defaults/tests.py16
-rw-r--r--tests/modeltests/field_subclassing/fields.py8
-rw-r--r--tests/modeltests/field_subclassing/models.py54
-rw-r--r--tests/modeltests/field_subclassing/tests.py64
-rw-r--r--tests/modeltests/files/models.py124
-rw-r--r--tests/modeltests/files/tests.py100
-rw-r--r--tests/modeltests/fixtures/models.py235
-rw-r--r--tests/modeltests/fixtures/tests.py336
-rw-r--r--tests/modeltests/fixtures_model_package/models/__init__.py40
-rw-r--r--tests/modeltests/fixtures_model_package/tests.py71
-rw-r--r--tests/modeltests/force_insert_update/models.py51
-rw-r--r--tests/modeltests/force_insert_update/tests.py38
-rw-r--r--tests/modeltests/get_latest/models.py49
-rw-r--r--tests/modeltests/get_latest/tests.py53
-rw-r--r--tests/modeltests/get_object_or_404/models.py73
-rw-r--r--tests/modeltests/get_object_or_404/tests.py80
-rw-r--r--tests/modeltests/get_or_create/models.py62
-rw-r--r--tests/modeltests/get_or_create/tests.py52
-rw-r--r--tests/modeltests/m2m_and_m2o/models.py44
-rw-r--r--tests/modeltests/m2m_and_m2o/tests.py75
-rw-r--r--tests/modeltests/m2m_intermediary/models.py32
-rw-r--r--tests/modeltests/m2m_intermediary/tests.py38
-rw-r--r--tests/modeltests/m2m_multiple/models.py49
-rw-r--r--tests/modeltests/m2m_multiple/tests.py84
-rw-r--r--tests/modeltests/model_forms/models.py14
-rw-r--r--tests/modeltests/model_forms/tests.py4
-rw-r--r--tests/modeltests/proxy_model_inheritance/tests.py2
-rw-r--r--tests/modeltests/signals/models.py34
-rw-r--r--tests/modeltests/test_client/models.py24
-rw-r--r--tests/modeltests/validation/test_unique.py9
-rw-r--r--tests/regressiontests/admin_changelist/tests.py46
-rw-r--r--tests/regressiontests/admin_scripts/tests.py2
-rw-r--r--tests/regressiontests/admin_views/models.py6
-rw-r--r--tests/regressiontests/admin_views/tests.py113
-rw-r--r--tests/regressiontests/admin_widgets/tests.py12
-rw-r--r--tests/regressiontests/aggregation_regress/models.py323
-rw-r--r--tests/regressiontests/aggregation_regress/tests.py620
-rw-r--r--tests/regressiontests/backends/models.py36
-rw-r--r--tests/regressiontests/backends/tests.py133
-rw-r--r--tests/regressiontests/cache/liberal_backend.py9
-rw-r--r--tests/regressiontests/cache/tests.py91
-rw-r--r--tests/regressiontests/csrf_tests/tests.py55
-rw-r--r--tests/regressiontests/defaultfilters/tests.py9
-rw-r--r--tests/regressiontests/file_storage/tests.py16
-rw-r--r--tests/regressiontests/forms/fields.py16
-rw-r--r--tests/regressiontests/forms/forms.py52
-rw-r--r--tests/regressiontests/forms/input_formats.py894
-rw-r--r--tests/regressiontests/forms/localflavor/au.py2
-rw-r--r--tests/regressiontests/forms/models.py100
-rw-r--r--tests/regressiontests/forms/tests.py2
-rw-r--r--tests/regressiontests/forms/widgets.py51
-rw-r--r--tests/regressiontests/httpwrappers/tests.py711
-rw-r--r--tests/regressiontests/i18n/models.py6
-rw-r--r--tests/regressiontests/i18n/tests.py33
-rw-r--r--tests/regressiontests/localflavor/models.py8
-rw-r--r--tests/regressiontests/localflavor/tests.py84
-rw-r--r--tests/regressiontests/localflavor/us/__init__.py0
-rw-r--r--tests/regressiontests/localflavor/us/forms.py (renamed from tests/regressiontests/localflavor/forms.py)6
-rw-r--r--tests/regressiontests/localflavor/us/models.py13
-rw-r--r--tests/regressiontests/localflavor/us/tests.py82
-rw-r--r--tests/regressiontests/m2m_through_regress/models.py158
-rw-r--r--tests/regressiontests/m2m_through_regress/tests.py128
-rw-r--r--tests/regressiontests/model_forms_regress/models.py3
-rw-r--r--tests/regressiontests/model_forms_regress/tests.py123
-rw-r--r--tests/regressiontests/model_formsets_regress/tests.py62
-rw-r--r--tests/regressiontests/multiple_database/fixtures/pets.json18
-rw-r--r--tests/regressiontests/multiple_database/tests.py171
-rw-r--r--tests/regressiontests/requests/tests.py25
-rw-r--r--tests/regressiontests/serializers_regress/tests.py71
-rw-r--r--tests/regressiontests/templates/filters.py9
-rw-r--r--tests/regressiontests/templates/loaders.py42
-rw-r--r--tests/regressiontests/templates/tests.py34
-rw-r--r--tests/regressiontests/test_client_regress/models.py48
-rw-r--r--tests/regressiontests/test_client_regress/urls.py2
-rw-r--r--tests/regressiontests/test_client_regress/views.py5
-rw-r--r--tests/regressiontests/urlpatterns_reverse/included_named_urls.py10
-rw-r--r--tests/regressiontests/urlpatterns_reverse/included_named_urls2.py9
-rw-r--r--tests/regressiontests/urlpatterns_reverse/included_namespace_urls.py7
-rw-r--r--tests/regressiontests/urlpatterns_reverse/named_urls.py9
-rw-r--r--tests/regressiontests/urlpatterns_reverse/namespace_urls.py9
-rw-r--r--tests/regressiontests/urlpatterns_reverse/tests.py127
-rw-r--r--tests/regressiontests/urlpatterns_reverse/urls_without_full_import.py10
-rw-r--r--tests/regressiontests/urlpatterns_reverse/views.py17
-rw-r--r--tests/regressiontests/utils/timesince.py14
-rw-r--r--tests/regressiontests/views/tests/__init__.py1
-rw-r--r--tests/regressiontests/views/tests/generic/simple.py38
-rw-r--r--tests/regressiontests/views/urls.py10
-rwxr-xr-xtests/runtests.py30
110 files changed, 5979 insertions, 3224 deletions
diff --git a/tests/modeltests/aggregation/models.py b/tests/modeltests/aggregation/models.py
index f50abe651b..ccc12898b7 100644
--- a/tests/modeltests/aggregation/models.py
+++ b/tests/modeltests/aggregation/models.py
@@ -1,6 +1,7 @@
# coding: utf-8
from django.db import models
+
class Author(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
@@ -39,323 +40,3 @@ class Store(models.Model):
def __unicode__(self):
return self.name
-# Tests on 'aggregate'
-# Different backends and numbers.
-__test__ = {'API_TESTS': """
->>> from django.core import management
->>> from decimal import Decimal
->>> from datetime import date
-
-# Reset the database representation of this app.
-# This will return the database to a clean initial state.
->>> management.call_command('flush', verbosity=0, interactive=False)
-
-# Empty Call - request nothing, get nothing.
->>> Author.objects.all().aggregate()
-{}
-
->>> from django.db.models import Avg, Sum, Count, Max, Min
-
-# Single model aggregation
-#
-
-# Single aggregate
-# Average age of Authors
->>> Author.objects.all().aggregate(Avg('age'))
-{'age__avg': 37.4...}
-
-# Multiple aggregates
-# Average and Sum of Author ages
->>> Author.objects.all().aggregate(Sum('age'), Avg('age'))
-{'age__sum': 337, 'age__avg': 37.4...}
-
-# Aggreates interact with filters, and only
-# generate aggregate values for the filtered values
-# Sum of the age of those older than 29 years old
->>> Author.objects.all().filter(age__gt=29).aggregate(Sum('age'))
-{'age__sum': 254}
-
-# Depth-1 Joins
-#
-
-# On Relationships with self
-# Average age of the friends of each author
->>> Author.objects.all().aggregate(Avg('friends__age'))
-{'friends__age__avg': 34.07...}
-
-# On ManyToMany Relationships
-#
-
-# Forward
-# Average age of the Authors of Books with a rating of less than 4.5
->>> Book.objects.all().filter(rating__lt=4.5).aggregate(Avg('authors__age'))
-{'authors__age__avg': 38.2...}
-
-# Backward
-# Average rating of the Books whose Author's name contains the letter 'a'
->>> Author.objects.all().filter(name__contains='a').aggregate(Avg('book__rating'))
-{'book__rating__avg': 4.0}
-
-# On OneToMany Relationships
-#
-
-# Forward
-# Sum of the number of awards of each Book's Publisher
->>> Book.objects.all().aggregate(Sum('publisher__num_awards'))
-{'publisher__num_awards__sum': 30}
-
-# Backward
-# Sum of the price of every Book that has a Publisher
->>> Publisher.objects.all().aggregate(Sum('book__price'))
-{'book__price__sum': Decimal("270.27")}
-
-# Multiple Joins
-#
-
-# Forward
->>> Store.objects.all().aggregate(Max('books__authors__age'))
-{'books__authors__age__max': 57}
-
-# Backward
-# Note that the very long default alias may be truncated
->>> Author.objects.all().aggregate(Min('book__publisher__num_awards'))
-{'book__publisher__num_award...': 1}
-
-# Aggregate outputs can also be aliased.
-
-# Average amazon.com Book rating
->>> Store.objects.filter(name='Amazon.com').aggregate(amazon_mean=Avg('books__rating'))
-{'amazon_mean': 4.08...}
-
-# Tests on annotate()
-
-# An empty annotate call does nothing but return the same QuerySet
->>> Book.objects.all().annotate().order_by('pk')
-[<Book: The Definitive Guide to Django: Web Development Done Right>, <Book: Sams Teach Yourself Django in 24 Hours>, <Book: Practical Django Projects>, <Book: Python Web Development with Django>, <Book: Artificial Intelligence: A Modern Approach>, <Book: Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp>]
-
-# Annotate inserts the alias into the model object with the aggregated result
->>> books = Book.objects.all().annotate(mean_age=Avg('authors__age'))
->>> books.get(pk=1).name
-u'The Definitive Guide to Django: Web Development Done Right'
-
->>> books.get(pk=1).mean_age
-34.5
-
-# On ManyToMany Relationships
-
-# Forward
-# Average age of the Authors of each book with a rating less than 4.5
->>> books = Book.objects.all().filter(rating__lt=4.5).annotate(Avg('authors__age'))
->>> sorted([(b.name, b.authors__age__avg) for b in books])
-[(u'Artificial Intelligence: A Modern Approach', 51.5), (u'Practical Django Projects', 29.0), (u'Python Web Development with Django', 30.3...), (u'Sams Teach Yourself Django in 24 Hours', 45.0)]
-
-# Count the number of authors of each book
->>> books = Book.objects.annotate(num_authors=Count('authors'))
->>> sorted([(b.name, b.num_authors) for b in books])
-[(u'Artificial Intelligence: A Modern Approach', 2), (u'Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp', 1), (u'Practical Django Projects', 1), (u'Python Web Development with Django', 3), (u'Sams Teach Yourself Django in 24 Hours', 1), (u'The Definitive Guide to Django: Web Development Done Right', 2)]
-
-# Backward
-# Average rating of the Books whose Author's names contains the letter 'a'
->>> authors = Author.objects.all().filter(name__contains='a').annotate(Avg('book__rating'))
->>> sorted([(a.name, a.book__rating__avg) for a in authors])
-[(u'Adrian Holovaty', 4.5), (u'Brad Dayley', 3.0), (u'Jacob Kaplan-Moss', 4.5), (u'James Bennett', 4.0), (u'Paul Bissex', 4.0), (u'Stuart Russell', 4.0)]
-
-# Count the number of books written by each author
->>> authors = Author.objects.annotate(num_books=Count('book'))
->>> sorted([(a.name, a.num_books) for a in authors])
-[(u'Adrian Holovaty', 1), (u'Brad Dayley', 1), (u'Jacob Kaplan-Moss', 1), (u'James Bennett', 1), (u'Jeffrey Forcier', 1), (u'Paul Bissex', 1), (u'Peter Norvig', 2), (u'Stuart Russell', 1), (u'Wesley J. Chun', 1)]
-
-# On OneToMany Relationships
-
-# Forward
-# Annotate each book with the number of awards of each Book's Publisher
->>> books = Book.objects.all().annotate(Sum('publisher__num_awards'))
->>> sorted([(b.name, b.publisher__num_awards__sum) for b in books])
-[(u'Artificial Intelligence: A Modern Approach', 7), (u'Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp', 9), (u'Practical Django Projects', 3), (u'Python Web Development with Django', 7), (u'Sams Teach Yourself Django in 24 Hours', 1), (u'The Definitive Guide to Django: Web Development Done Right', 3)]
-
-# Backward
-# Annotate each publisher with the sum of the price of all books sold
->>> publishers = Publisher.objects.all().annotate(Sum('book__price'))
->>> sorted([(p.name, p.book__price__sum) for p in publishers])
-[(u'Apress', Decimal("59.69")), (u"Jonno's House of Books", None), (u'Morgan Kaufmann', Decimal("75.00")), (u'Prentice Hall', Decimal("112.49")), (u'Sams', Decimal("23.09"))]
-
-# Calls to values() are not commutative over annotate().
-
-# Calling values on a queryset that has annotations returns the output
-# as a dictionary
->>> [sorted(o.iteritems()) for o in Book.objects.filter(pk=1).annotate(mean_age=Avg('authors__age')).values()]
-[[('contact_id', 1), ('id', 1), ('isbn', u'159059725'), ('mean_age', 34.5), ('name', u'The Definitive Guide to Django: Web Development Done Right'), ('pages', 447), ('price', Decimal("30...")), ('pubdate', datetime.date(2007, 12, 6)), ('publisher_id', 1), ('rating', 4.5)]]
-
->>> Book.objects.filter(pk=1).annotate(mean_age=Avg('authors__age')).values('pk', 'isbn', 'mean_age')
-[{'pk': 1, 'isbn': u'159059725', 'mean_age': 34.5}]
-
-# Calling values() with parameters reduces the output
->>> Book.objects.filter(pk=1).annotate(mean_age=Avg('authors__age')).values('name')
-[{'name': u'The Definitive Guide to Django: Web Development Done Right'}]
-
-# An empty values() call before annotating has the same effect as an
-# empty values() call after annotating
->>> [sorted(o.iteritems()) for o in Book.objects.filter(pk=1).values().annotate(mean_age=Avg('authors__age'))]
-[[('contact_id', 1), ('id', 1), ('isbn', u'159059725'), ('mean_age', 34.5), ('name', u'The Definitive Guide to Django: Web Development Done Right'), ('pages', 447), ('price', Decimal("30...")), ('pubdate', datetime.date(2007, 12, 6)), ('publisher_id', 1), ('rating', 4.5)]]
-
-# Calling annotate() on a ValuesQuerySet annotates over the groups of
-# fields to be selected by the ValuesQuerySet.
-
-# Note that an extra parameter is added to each dictionary. This
-# parameter is a queryset representing the objects that have been
-# grouped to generate the annotation
-
->>> Book.objects.all().values('rating').annotate(n_authors=Count('authors__id'), mean_age=Avg('authors__age')).order_by('rating')
-[{'rating': 3.0, 'n_authors': 1, 'mean_age': 45.0}, {'rating': 4.0, 'n_authors': 6, 'mean_age': 37.1...}, {'rating': 4.5, 'n_authors': 2, 'mean_age': 34.5}, {'rating': 5.0, 'n_authors': 1, 'mean_age': 57.0}]
-
-# If a join doesn't match any objects, an aggregate returns None
->>> authors = Author.objects.all().annotate(Avg('friends__age')).order_by('id')
->>> len(authors)
-9
->>> sorted([(a.name, a.friends__age__avg) for a in authors])
-[(u'Adrian Holovaty', 32.0), (u'Brad Dayley', None), (u'Jacob Kaplan-Moss', 29.5), (u'James Bennett', 34.0), (u'Jeffrey Forcier', 27.0), (u'Paul Bissex', 31.0), (u'Peter Norvig', 46.0), (u'Stuart Russell', 57.0), (u'Wesley J. Chun', 33.6...)]
-
-
-# The Count aggregation function allows an extra parameter: distinct.
-# This restricts the count results to unique items
->>> Book.objects.all().aggregate(Count('rating'))
-{'rating__count': 6}
-
->>> Book.objects.all().aggregate(Count('rating', distinct=True))
-{'rating__count': 4}
-
-# Retreiving the grouped objects
-
-# When using Count you can also omit the primary key and refer only to
-# the related field name if you want to count all the related objects
-# and not a specific column
->>> explicit = list(Author.objects.annotate(Count('book__id')))
->>> implicit = list(Author.objects.annotate(Count('book')))
->>> explicit == implicit
-True
-
-# Ordering is allowed on aggregates
->>> Book.objects.values('rating').annotate(oldest=Max('authors__age')).order_by('oldest', 'rating')
-[{'rating': 4.5, 'oldest': 35}, {'rating': 3.0, 'oldest': 45}, {'rating': 4.0, 'oldest': 57}, {'rating': 5.0, 'oldest': 57}]
-
->>> Book.objects.values('rating').annotate(oldest=Max('authors__age')).order_by('-oldest', '-rating')
-[{'rating': 5.0, 'oldest': 57}, {'rating': 4.0, 'oldest': 57}, {'rating': 3.0, 'oldest': 45}, {'rating': 4.5, 'oldest': 35}]
-
-# It is possible to aggregate over anotated values
->>> Book.objects.all().annotate(num_authors=Count('authors__id')).aggregate(Avg('num_authors'))
-{'num_authors__avg': 1.66...}
-
-# You can filter the results based on the aggregation alias.
-
-# Lets add a publisher to test the different possibilities for filtering
->>> p = Publisher(name='Expensive Publisher', num_awards=0)
->>> p.save()
->>> Book(name='ExpensiveBook1', pages=1, isbn='111', rating=3.5, price=Decimal("1000"), publisher=p, contact_id=1, pubdate=date(2008,12,1)).save()
->>> Book(name='ExpensiveBook2', pages=1, isbn='222', rating=4.0, price=Decimal("1000"), publisher=p, contact_id=1, pubdate=date(2008,12,2)).save()
->>> Book(name='ExpensiveBook3', pages=1, isbn='333', rating=4.5, price=Decimal("35"), publisher=p, contact_id=1, pubdate=date(2008,12,3)).save()
-
-# Publishers that have:
-
-# (i) more than one book
->>> Publisher.objects.annotate(num_books=Count('book__id')).filter(num_books__gt=1).order_by('pk')
-[<Publisher: Apress>, <Publisher: Prentice Hall>, <Publisher: Expensive Publisher>]
-
-# (ii) a book that cost less than 40
->>> Publisher.objects.filter(book__price__lt=Decimal("40.0")).order_by('pk')
-[<Publisher: Apress>, <Publisher: Apress>, <Publisher: Sams>, <Publisher: Prentice Hall>, <Publisher: Expensive Publisher>]
-
-# (iii) more than one book and (at least) a book that cost less than 40
->>> Publisher.objects.annotate(num_books=Count('book__id')).filter(num_books__gt=1, book__price__lt=Decimal("40.0")).order_by('pk')
-[<Publisher: Apress>, <Publisher: Prentice Hall>, <Publisher: Expensive Publisher>]
-
-# (iv) more than one book that costs less than $40
->>> Publisher.objects.filter(book__price__lt=Decimal("40.0")).annotate(num_books=Count('book__id')).filter(num_books__gt=1).order_by('pk')
-[<Publisher: Apress>]
-
-# Now a bit of testing on the different lookup types
-#
-
->>> Publisher.objects.annotate(num_books=Count('book')).filter(num_books__range=[1, 3]).order_by('pk')
-[<Publisher: Apress>, <Publisher: Sams>, <Publisher: Prentice Hall>, <Publisher: Morgan Kaufmann>, <Publisher: Expensive Publisher>]
-
->>> Publisher.objects.annotate(num_books=Count('book')).filter(num_books__range=[1, 2]).order_by('pk')
-[<Publisher: Apress>, <Publisher: Sams>, <Publisher: Prentice Hall>, <Publisher: Morgan Kaufmann>]
-
->>> Publisher.objects.annotate(num_books=Count('book')).filter(num_books__in=[1, 3]).order_by('pk')
-[<Publisher: Sams>, <Publisher: Morgan Kaufmann>, <Publisher: Expensive Publisher>]
-
->>> Publisher.objects.annotate(num_books=Count('book')).filter(num_books__isnull=True)
-[]
-
->>> p.delete()
-
-# Does Author X have any friends? (or better, how many friends does author X have)
->> Author.objects.filter(pk=1).aggregate(Count('friends__id'))
-{'friends__id__count': 2.0}
-
-# Give me a list of all Books with more than 1 authors
->>> Book.objects.all().annotate(num_authors=Count('authors__name')).filter(num_authors__ge=2).order_by('pk')
-[<Book: The Definitive Guide to Django: Web Development Done Right>, <Book: Artificial Intelligence: A Modern Approach>]
-
-# Give me a list of all Authors that have no friends
->>> Author.objects.all().annotate(num_friends=Count('friends__id', distinct=True)).filter(num_friends=0).order_by('pk')
-[<Author: Brad Dayley>]
-
-# Give me a list of all publishers that have published more than 1 books
->>> Publisher.objects.all().annotate(num_books=Count('book__id')).filter(num_books__gt=1).order_by('pk')
-[<Publisher: Apress>, <Publisher: Prentice Hall>]
-
-# Give me a list of all publishers that have published more than 1 books that cost less than 40
->>> Publisher.objects.all().filter(book__price__lt=Decimal("40.0")).annotate(num_books=Count('book__id')).filter(num_books__gt=1)
-[<Publisher: Apress>]
-
-# Give me a list of all Books that were written by X and one other author.
->>> Book.objects.all().annotate(num_authors=Count('authors__id')).filter(authors__name__contains='Norvig', num_authors__gt=1)
-[<Book: Artificial Intelligence: A Modern Approach>]
-
-# Give me the average rating of all Books that were written by X and one other author.
-#(Aggregate over objects discovered using membership of the m2m set)
-
-# Adding an existing author to another book to test it the right way
->>> a = Author.objects.get(name__contains='Norvig')
->>> b = Book.objects.get(name__contains='Done Right')
->>> b.authors.add(a)
->>> b.save()
-
-# This should do it
->>> Book.objects.all().annotate(num_authors=Count('authors__id')).filter(authors__name__contains='Norvig', num_authors__gt=1).aggregate(Avg('rating'))
-{'rating__avg': 4.25}
->>> b.authors.remove(a)
-
-# Give me a list of all Authors that have published a book with at least one other person
-# (Filters over a count generated on a related object)
-#
-# Cheating: [a for a in Author.objects.all().annotate(num_coleagues=Count('book__authors__id'), num_books=Count('book__id', distinct=True)) if a.num_coleagues - a.num_books > 0]
-# F-Syntax is required. Will be fixed after F objects are available
-
-# Aggregates also work on dates, times and datetimes
->>> Publisher.objects.annotate(earliest_book=Min('book__pubdate')).exclude(earliest_book=None).order_by('earliest_book').values()
-[{'earliest_book': datetime.date(1991, 10, 15), 'num_awards': 9, 'id': 4, 'name': u'Morgan Kaufmann'}, {'earliest_book': datetime.date(1995, 1, 15), 'num_awards': 7, 'id': 3, 'name': u'Prentice Hall'}, {'earliest_book': datetime.date(2007, 12, 6), 'num_awards': 3, 'id': 1, 'name': u'Apress'}, {'earliest_book': datetime.date(2008, 3, 3), 'num_awards': 1, 'id': 2, 'name': u'Sams'}]
-
->>> Store.objects.aggregate(Max('friday_night_closing'), Min("original_opening"))
-{'friday_night_closing__max': datetime.time(23, 59, 59), 'original_opening__min': datetime.datetime(1945, 4, 25, 16, 24, 14)}
-
-# values_list() can also be used
-
->>> Book.objects.filter(pk=1).annotate(mean_age=Avg('authors__age')).values_list('pk', 'isbn', 'mean_age')
-[(1, u'159059725', 34.5)]
-
->>> Book.objects.filter(pk=1).annotate(mean_age=Avg('authors__age')).values_list('isbn')
-[(u'159059725',)]
-
->>> Book.objects.filter(pk=1).annotate(mean_age=Avg('authors__age')).values_list('mean_age')
-[(34.5,)]
-
->>> Book.objects.filter(pk=1).annotate(mean_age=Avg('authors__age')).values_list('mean_age', flat=True)
-[34.5]
-
->>> qs = Book.objects.values_list('price').annotate(count=Count('price')).order_by('-count', 'price')
->>> list(qs) == [(Decimal('29.69'), 2), (Decimal('23.09'), 1), (Decimal('30'), 1), (Decimal('75'), 1), (Decimal('82.8'), 1)]
-True
-
-"""}
diff --git a/tests/modeltests/aggregation/tests.py b/tests/modeltests/aggregation/tests.py
new file mode 100644
index 0000000000..c830368b9d
--- /dev/null
+++ b/tests/modeltests/aggregation/tests.py
@@ -0,0 +1,565 @@
+import datetime
+from decimal import Decimal
+
+from django.db.models import Avg, Sum, Count, Max, Min
+from django.test import TestCase, Approximate
+
+from models import Author, Publisher, Book, Store
+
+
+class BaseAggregateTestCase(TestCase):
+ fixtures = ["initial_data.json"]
+
+ def test_empty_aggregate(self):
+ self.assertEqual(Author.objects.all().aggregate(), {})
+
+ def test_single_aggregate(self):
+ vals = Author.objects.aggregate(Avg("age"))
+ self.assertEqual(vals, {"age__avg": Approximate(37.4, places=1)})
+
+ def test_multiple_aggregates(self):
+ vals = Author.objects.aggregate(Sum("age"), Avg("age"))
+ self.assertEqual(vals, {"age__sum": 337, "age__avg": Approximate(37.4, places=1)})
+
+ def test_filter_aggregate(self):
+ vals = Author.objects.filter(age__gt=29).aggregate(Sum("age"))
+ self.assertEqual(len(vals), 1)
+ self.assertEqual(vals["age__sum"], 254)
+
+ def test_related_aggregate(self):
+ vals = Author.objects.aggregate(Avg("friends__age"))
+ self.assertEqual(len(vals), 1)
+ self.assertAlmostEqual(vals["friends__age__avg"], 34.07, places=2)
+
+ vals = Book.objects.filter(rating__lt=4.5).aggregate(Avg("authors__age"))
+ self.assertEqual(len(vals), 1)
+ self.assertAlmostEqual(vals["authors__age__avg"], 38.2857, places=2)
+
+ vals = Author.objects.all().filter(name__contains="a").aggregate(Avg("book__rating"))
+ self.assertEqual(len(vals), 1)
+ self.assertEqual(vals["book__rating__avg"], 4.0)
+
+ vals = Book.objects.aggregate(Sum("publisher__num_awards"))
+ self.assertEqual(len(vals), 1)
+ self.assertEquals(vals["publisher__num_awards__sum"], 30)
+
+ vals = Publisher.objects.aggregate(Sum("book__price"))
+ self.assertEqual(len(vals), 1)
+ self.assertEqual(vals["book__price__sum"], Decimal("270.27"))
+
+ def test_aggregate_multi_join(self):
+ vals = Store.objects.aggregate(Max("books__authors__age"))
+ self.assertEqual(len(vals), 1)
+ self.assertEqual(vals["books__authors__age__max"], 57)
+
+ vals = Author.objects.aggregate(Min("book__publisher__num_awards"))
+ self.assertEqual(len(vals), 1)
+ self.assertEqual(vals["book__publisher__num_awards__min"], 1)
+
+ def test_aggregate_alias(self):
+ vals = Store.objects.filter(name="Amazon.com").aggregate(amazon_mean=Avg("books__rating"))
+ self.assertEqual(len(vals), 1)
+ self.assertAlmostEqual(vals["amazon_mean"], 4.08, places=2)
+
+ def test_annotate_basic(self):
+ self.assertQuerysetEqual(
+ Book.objects.annotate().order_by('pk'), [
+ "The Definitive Guide to Django: Web Development Done Right",
+ "Sams Teach Yourself Django in 24 Hours",
+ "Practical Django Projects",
+ "Python Web Development with Django",
+ "Artificial Intelligence: A Modern Approach",
+ "Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp"
+ ],
+ lambda b: b.name
+ )
+
+ books = Book.objects.annotate(mean_age=Avg("authors__age"))
+ b = books.get(pk=1)
+ self.assertEqual(
+ b.name,
+ u'The Definitive Guide to Django: Web Development Done Right'
+ )
+ self.assertEqual(b.mean_age, 34.5)
+
+ def test_annotate_m2m(self):
+ books = Book.objects.filter(rating__lt=4.5).annotate(Avg("authors__age")).order_by("name")
+ self.assertQuerysetEqual(
+ books, [
+ (u'Artificial Intelligence: A Modern Approach', 51.5),
+ (u'Practical Django Projects', 29.0),
+ (u'Python Web Development with Django', Approximate(30.3, places=1)),
+ (u'Sams Teach Yourself Django in 24 Hours', 45.0)
+ ],
+ lambda b: (b.name, b.authors__age__avg),
+ )
+
+ books = Book.objects.annotate(num_authors=Count("authors")).order_by("name")
+ self.assertQuerysetEqual(
+ books, [
+ (u'Artificial Intelligence: A Modern Approach', 2),
+ (u'Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp', 1),
+ (u'Practical Django Projects', 1),
+ (u'Python Web Development with Django', 3),
+ (u'Sams Teach Yourself Django in 24 Hours', 1),
+ (u'The Definitive Guide to Django: Web Development Done Right', 2)
+ ],
+ lambda b: (b.name, b.num_authors)
+ )
+
+ def test_backwards_m2m_annotate(self):
+ authors = Author.objects.filter(name__contains="a").annotate(Avg("book__rating")).order_by("name")
+ self.assertQuerysetEqual(
+ authors, [
+ (u'Adrian Holovaty', 4.5),
+ (u'Brad Dayley', 3.0),
+ (u'Jacob Kaplan-Moss', 4.5),
+ (u'James Bennett', 4.0),
+ (u'Paul Bissex', 4.0),
+ (u'Stuart Russell', 4.0)
+ ],
+ lambda a: (a.name, a.book__rating__avg)
+ )
+
+ authors = Author.objects.annotate(num_books=Count("book")).order_by("name")
+ self.assertQuerysetEqual(
+ authors, [
+ (u'Adrian Holovaty', 1),
+ (u'Brad Dayley', 1),
+ (u'Jacob Kaplan-Moss', 1),
+ (u'James Bennett', 1),
+ (u'Jeffrey Forcier', 1),
+ (u'Paul Bissex', 1),
+ (u'Peter Norvig', 2),
+ (u'Stuart Russell', 1),
+ (u'Wesley J. Chun', 1)
+ ],
+ lambda a: (a.name, a.num_books)
+ )
+
+ def test_reverse_fkey_annotate(self):
+ books = Book.objects.annotate(Sum("publisher__num_awards")).order_by("name")
+ self.assertQuerysetEqual(
+ books, [
+ (u'Artificial Intelligence: A Modern Approach', 7),
+ (u'Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp', 9),
+ (u'Practical Django Projects', 3),
+ (u'Python Web Development with Django', 7),
+ (u'Sams Teach Yourself Django in 24 Hours', 1),
+ (u'The Definitive Guide to Django: Web Development Done Right', 3)
+ ],
+ lambda b: (b.name, b.publisher__num_awards__sum)
+ )
+
+ publishers = Publisher.objects.annotate(Sum("book__price")).order_by("name")
+ self.assertQuerysetEqual(
+ publishers, [
+ (u'Apress', Decimal("59.69")),
+ (u"Jonno's House of Books", None),
+ (u'Morgan Kaufmann', Decimal("75.00")),
+ (u'Prentice Hall', Decimal("112.49")),
+ (u'Sams', Decimal("23.09"))
+ ],
+ lambda p: (p.name, p.book__price__sum)
+ )
+
+ def test_annotate_values(self):
+ books = list(Book.objects.filter(pk=1).annotate(mean_age=Avg("authors__age")).values())
+ self.assertEqual(
+ books, [
+ {
+ "contact_id": 1,
+ "id": 1,
+ "isbn": "159059725",
+ "mean_age": 34.5,
+ "name": "The Definitive Guide to Django: Web Development Done Right",
+ "pages": 447,
+ "price": Approximate(Decimal("30")),
+ "pubdate": datetime.date(2007, 12, 6),
+ "publisher_id": 1,
+ "rating": 4.5,
+ }
+ ]
+ )
+
+ books = Book.objects.filter(pk=1).annotate(mean_age=Avg('authors__age')).values('pk', 'isbn', 'mean_age')
+ self.assertEqual(
+ list(books), [
+ {
+ "pk": 1,
+ "isbn": "159059725",
+ "mean_age": 34.5,
+ }
+ ]
+ )
+
+ books = Book.objects.filter(pk=1).annotate(mean_age=Avg("authors__age")).values("name")
+ self.assertEqual(
+ list(books), [
+ {
+ "name": "The Definitive Guide to Django: Web Development Done Right"
+ }
+ ]
+ )
+
+ books = Book.objects.filter(pk=1).values().annotate(mean_age=Avg('authors__age'))
+ self.assertEqual(
+ list(books), [
+ {
+ "contact_id": 1,
+ "id": 1,
+ "isbn": "159059725",
+ "mean_age": 34.5,
+ "name": "The Definitive Guide to Django: Web Development Done Right",
+ "pages": 447,
+ "price": Approximate(Decimal("30")),
+ "pubdate": datetime.date(2007, 12, 6),
+ "publisher_id": 1,
+ "rating": 4.5,
+ }
+ ]
+ )
+
+ books = Book.objects.values("rating").annotate(n_authors=Count("authors__id"), mean_age=Avg("authors__age")).order_by("rating")
+ self.assertEqual(
+ list(books), [
+ {
+ "rating": 3.0,
+ "n_authors": 1,
+ "mean_age": 45.0,
+ },
+ {
+ "rating": 4.0,
+ "n_authors": 6,
+ "mean_age": Approximate(37.16, places=1)
+ },
+ {
+ "rating": 4.5,
+ "n_authors": 2,
+ "mean_age": 34.5,
+ },
+ {
+ "rating": 5.0,
+ "n_authors": 1,
+ "mean_age": 57.0,
+ }
+ ]
+ )
+
+ authors = Author.objects.annotate(Avg("friends__age")).order_by("name")
+ self.assertEqual(len(authors), 9)
+ self.assertQuerysetEqual(
+ authors, [
+ (u'Adrian Holovaty', 32.0),
+ (u'Brad Dayley', None),
+ (u'Jacob Kaplan-Moss', 29.5),
+ (u'James Bennett', 34.0),
+ (u'Jeffrey Forcier', 27.0),
+ (u'Paul Bissex', 31.0),
+ (u'Peter Norvig', 46.0),
+ (u'Stuart Russell', 57.0),
+ (u'Wesley J. Chun', Approximate(33.66, places=1))
+ ],
+ lambda a: (a.name, a.friends__age__avg)
+ )
+
+ def test_count(self):
+ vals = Book.objects.aggregate(Count("rating"))
+ self.assertEqual(vals, {"rating__count": 6})
+
+ vals = Book.objects.aggregate(Count("rating", distinct=True))
+ self.assertEqual(vals, {"rating__count": 4})
+
+ def test_fkey_aggregate(self):
+ explicit = list(Author.objects.annotate(Count('book__id')))
+ implicit = list(Author.objects.annotate(Count('book')))
+ self.assertEqual(explicit, implicit)
+
+ def test_annotate_ordering(self):
+ books = Book.objects.values('rating').annotate(oldest=Max('authors__age')).order_by('oldest', 'rating')
+ self.assertEqual(
+ list(books), [
+ {
+ "rating": 4.5,
+ "oldest": 35,
+ },
+ {
+ "rating": 3.0,
+ "oldest": 45
+ },
+ {
+ "rating": 4.0,
+ "oldest": 57,
+ },
+ {
+ "rating": 5.0,
+ "oldest": 57,
+ }
+ ]
+ )
+
+ books = Book.objects.values("rating").annotate(oldest=Max("authors__age")).order_by("-oldest", "-rating")
+ self.assertEqual(
+ list(books), [
+ {
+ "rating": 5.0,
+ "oldest": 57,
+ },
+ {
+ "rating": 4.0,
+ "oldest": 57,
+ },
+ {
+ "rating": 3.0,
+ "oldest": 45,
+ },
+ {
+ "rating": 4.5,
+ "oldest": 35,
+ }
+ ]
+ )
+
+ def test_aggregate_annotation(self):
+ vals = Book.objects.annotate(num_authors=Count("authors__id")).aggregate(Avg("num_authors"))
+ self.assertEqual(vals, {"num_authors__avg": Approximate(1.66, places=1)})
+
+ def test_filtering(self):
+ p = Publisher.objects.create(name='Expensive Publisher', num_awards=0)
+ Book.objects.create(
+ name='ExpensiveBook1',
+ pages=1,
+ isbn='111',
+ rating=3.5,
+ price=Decimal("1000"),
+ publisher=p,
+ contact_id=1,
+ pubdate=datetime.date(2008,12,1)
+ )
+ Book.objects.create(
+ name='ExpensiveBook2',
+ pages=1,
+ isbn='222',
+ rating=4.0,
+ price=Decimal("1000"),
+ publisher=p,
+ contact_id=1,
+ pubdate=datetime.date(2008,12,2)
+ )
+ Book.objects.create(
+ name='ExpensiveBook3',
+ pages=1,
+ isbn='333',
+ rating=4.5,
+ price=Decimal("35"),
+ publisher=p,
+ contact_id=1,
+ pubdate=datetime.date(2008,12,3)
+ )
+
+ publishers = Publisher.objects.annotate(num_books=Count("book__id")).filter(num_books__gt=1).order_by("pk")
+ self.assertQuerysetEqual(
+ publishers, [
+ "Apress",
+ "Prentice Hall",
+ "Expensive Publisher",
+ ],
+ lambda p: p.name,
+ )
+
+ publishers = Publisher.objects.filter(book__price__lt=Decimal("40.0")).order_by("pk")
+ self.assertQuerysetEqual(
+ publishers, [
+ "Apress",
+ "Apress",
+ "Sams",
+ "Prentice Hall",
+ "Expensive Publisher",
+ ],
+ lambda p: p.name
+ )
+
+ publishers = Publisher.objects.annotate(num_books=Count("book__id")).filter(num_books__gt=1, book__price__lt=Decimal("40.0")).order_by("pk")
+ self.assertQuerysetEqual(
+ publishers, [
+ "Apress",
+ "Prentice Hall",
+ "Expensive Publisher",
+ ],
+ lambda p: p.name,
+ )
+
+ publishers = Publisher.objects.filter(book__price__lt=Decimal("40.0")).annotate(num_books=Count("book__id")).filter(num_books__gt=1).order_by("pk")
+ self.assertQuerysetEqual(
+ publishers, [
+ "Apress",
+ ],
+ lambda p: p.name
+ )
+
+ publishers = Publisher.objects.annotate(num_books=Count("book")).filter(num_books__range=[1, 3]).order_by("pk")
+ self.assertQuerysetEqual(
+ publishers, [
+ "Apress",
+ "Sams",
+ "Prentice Hall",
+ "Morgan Kaufmann",
+ "Expensive Publisher",
+ ],
+ lambda p: p.name
+ )
+
+ publishers = Publisher.objects.annotate(num_books=Count("book")).filter(num_books__range=[1, 2]).order_by("pk")
+ self.assertQuerysetEqual(
+ publishers, [
+ "Apress",
+ "Sams",
+ "Prentice Hall",
+ "Morgan Kaufmann",
+ ],
+ lambda p: p.name
+ )
+
+ publishers = Publisher.objects.annotate(num_books=Count("book")).filter(num_books__in=[1, 3]).order_by("pk")
+ self.assertQuerysetEqual(
+ publishers, [
+ "Sams",
+ "Morgan Kaufmann",
+ "Expensive Publisher",
+ ],
+ lambda p: p.name,
+ )
+
+ publishers = Publisher.objects.annotate(num_books=Count("book")).filter(num_books__isnull=True)
+ self.assertEqual(len(publishers), 0)
+
+ def test_annotation(self):
+ vals = Author.objects.filter(pk=1).aggregate(Count("friends__id"))
+ self.assertEqual(vals, {"friends__id__count": 2})
+
+ books = Book.objects.annotate(num_authors=Count("authors__name")).filter(num_authors__ge=2).order_by("pk")
+ self.assertQuerysetEqual(
+ books, [
+ "The Definitive Guide to Django: Web Development Done Right",
+ "Artificial Intelligence: A Modern Approach",
+ ],
+ lambda b: b.name
+ )
+
+ authors = Author.objects.annotate(num_friends=Count("friends__id", distinct=True)).filter(num_friends=0).order_by("pk")
+ self.assertQuerysetEqual(
+ authors, [
+ "Brad Dayley",
+ ],
+ lambda a: a.name
+ )
+
+ publishers = Publisher.objects.annotate(num_books=Count("book__id")).filter(num_books__gt=1).order_by("pk")
+ self.assertQuerysetEqual(
+ publishers, [
+ "Apress",
+ "Prentice Hall",
+ ],
+ lambda p: p.name
+ )
+
+ publishers = Publisher.objects.filter(book__price__lt=Decimal("40.0")).annotate(num_books=Count("book__id")).filter(num_books__gt=1)
+ self.assertQuerysetEqual(
+ publishers, [
+ "Apress",
+ ],
+ lambda p: p.name
+ )
+
+ books = Book.objects.annotate(num_authors=Count("authors__id")).filter(authors__name__contains="Norvig", num_authors__gt=1)
+ self.assertQuerysetEqual(
+ books, [
+ "Artificial Intelligence: A Modern Approach",
+ ],
+ lambda b: b.name
+ )
+
+ def test_more_aggregation(self):
+ a = Author.objects.get(name__contains='Norvig')
+ b = Book.objects.get(name__contains='Done Right')
+ b.authors.add(a)
+ b.save()
+
+ vals = Book.objects.annotate(num_authors=Count("authors__id")).filter(authors__name__contains="Norvig", num_authors__gt=1).aggregate(Avg("rating"))
+ self.assertEqual(vals, {"rating__avg": 4.25})
+
+ def test_even_more_aggregate(self):
+ publishers = Publisher.objects.annotate(earliest_book=Min("book__pubdate")).exclude(earliest_book=None).order_by("earliest_book").values()
+ self.assertEqual(
+ list(publishers), [
+ {
+ 'earliest_book': datetime.date(1991, 10, 15),
+ 'num_awards': 9,
+ 'id': 4,
+ 'name': u'Morgan Kaufmann'
+ },
+ {
+ 'earliest_book': datetime.date(1995, 1, 15),
+ 'num_awards': 7,
+ 'id': 3,
+ 'name': u'Prentice Hall'
+ },
+ {
+ 'earliest_book': datetime.date(2007, 12, 6),
+ 'num_awards': 3,
+ 'id': 1,
+ 'name': u'Apress'
+ },
+ {
+ 'earliest_book': datetime.date(2008, 3, 3),
+ 'num_awards': 1,
+ 'id': 2,
+ 'name': u'Sams'
+ }
+ ]
+ )
+
+ vals = Store.objects.aggregate(Max("friday_night_closing"), Min("original_opening"))
+ self.assertEqual(
+ vals,
+ {
+ "friday_night_closing__max": datetime.time(23, 59, 59),
+ "original_opening__min": datetime.datetime(1945, 4, 25, 16, 24, 14),
+ }
+ )
+
+ def test_annotate_values_list(self):
+ books = Book.objects.filter(pk=1).annotate(mean_age=Avg("authors__age")).values_list("pk", "isbn", "mean_age")
+ self.assertEqual(
+ list(books), [
+ (1, "159059725", 34.5),
+ ]
+ )
+
+ books = Book.objects.filter(pk=1).annotate(mean_age=Avg("authors__age")).values_list("isbn")
+ self.assertEqual(
+ list(books), [
+ ('159059725',)
+ ]
+ )
+
+ books = Book.objects.filter(pk=1).annotate(mean_age=Avg("authors__age")).values_list("mean_age")
+ self.assertEqual(
+ list(books), [
+ (34.5,)
+ ]
+ )
+
+ books = Book.objects.filter(pk=1).annotate(mean_age=Avg("authors__age")).values_list("mean_age", flat=True)
+ self.assertEqual(list(books), [34.5])
+
+ books = Book.objects.values_list("price").annotate(count=Count("price")).order_by("-count", "price")
+ self.assertEqual(
+ list(books), [
+ (Decimal("29.69"), 2),
+ (Decimal('23.09'), 1),
+ (Decimal('30'), 1),
+ (Decimal('75'), 1),
+ (Decimal('82.8'), 1),
+ ]
+ )
diff --git a/tests/modeltests/choices/models.py b/tests/modeltests/choices/models.py
index e378260598..27316f5dea 100644
--- a/tests/modeltests/choices/models.py
+++ b/tests/modeltests/choices/models.py
@@ -22,29 +22,3 @@ class Person(models.Model):
def __unicode__(self):
return self.name
-
-__test__ = {'API_TESTS':"""
->>> a = Person(name='Adrian', gender='M')
->>> a.save()
->>> s = Person(name='Sara', gender='F')
->>> s.save()
->>> a.gender
-'M'
->>> s.gender
-'F'
->>> a.get_gender_display()
-u'Male'
->>> s.get_gender_display()
-u'Female'
-
-# If the value for the field doesn't correspond to a valid choice,
-# the value itself is provided as a display value.
->>> a.gender = ''
->>> a.get_gender_display()
-u''
-
->>> a.gender = 'U'
->>> a.get_gender_display()
-u'U'
-
-"""}
diff --git a/tests/modeltests/choices/tests.py b/tests/modeltests/choices/tests.py
new file mode 100644
index 0000000000..09023d8113
--- /dev/null
+++ b/tests/modeltests/choices/tests.py
@@ -0,0 +1,23 @@
+from django.test import TestCase
+
+from models import Person
+
+
+class ChoicesTests(TestCase):
+ def test_display(self):
+ a = Person.objects.create(name='Adrian', gender='M')
+ s = Person.objects.create(name='Sara', gender='F')
+ self.assertEqual(a.gender, 'M')
+ self.assertEqual(s.gender, 'F')
+
+ self.assertEqual(a.get_gender_display(), 'Male')
+ self.assertEqual(s.get_gender_display(), 'Female')
+
+ # If the value for the field doesn't correspond to a valid choice,
+ # the value itself is provided as a display value.
+ a.gender = ''
+ self.assertEqual(a.get_gender_display(), '')
+
+ a.gender = 'U'
+ self.assertEqual(a.get_gender_display(), 'U')
+
diff --git a/tests/modeltests/custom_columns/models.py b/tests/modeltests/custom_columns/models.py
index 74691cd1bc..651f8a61b2 100644
--- a/tests/modeltests/custom_columns/models.py
+++ b/tests/modeltests/custom_columns/models.py
@@ -38,68 +38,3 @@ class Article(models.Model):
class Meta:
ordering = ('headline',)
-__test__ = {'API_TESTS':"""
-# Create a Author.
->>> a = Author(first_name='John', last_name='Smith')
->>> a.save()
-
->>> a.id
-1
-
-# Create another author
->>> a2 = Author(first_name='Peter', last_name='Jones')
->>> a2.save()
-
-# Create an article
->>> art = Article(headline='Django lets you build web apps easily')
->>> art.save()
->>> art.authors = [a, a2]
-
-# Although the table and column names on Author have been set to custom values,
-# nothing about using the Author model has changed...
-
-# Query the available authors
->>> Author.objects.all()
-[<Author: Peter Jones>, <Author: John Smith>]
-
->>> Author.objects.filter(first_name__exact='John')
-[<Author: John Smith>]
-
->>> Author.objects.get(first_name__exact='John')
-<Author: John Smith>
-
->>> Author.objects.filter(firstname__exact='John')
-Traceback (most recent call last):
- ...
-FieldError: Cannot resolve keyword 'firstname' into field. Choices are: article, first_name, id, last_name
-
->>> a = Author.objects.get(last_name__exact='Smith')
->>> a.first_name
-u'John'
->>> a.last_name
-u'Smith'
->>> a.firstname
-Traceback (most recent call last):
- ...
-AttributeError: 'Author' object has no attribute 'firstname'
->>> a.last
-Traceback (most recent call last):
- ...
-AttributeError: 'Author' object has no attribute 'last'
-
-# Although the Article table uses a custom m2m table,
-# nothing about using the m2m relationship has changed...
-
-# Get all the authors for an article
->>> art.authors.all()
-[<Author: Peter Jones>, <Author: John Smith>]
-
-# Get the articles for an author
->>> a.article_set.all()
-[<Article: Django lets you build web apps easily>]
-
-# Query the authors across the m2m relation
->>> art.authors.filter(last_name='Jones')
-[<Author: Peter Jones>]
-
-"""}
diff --git a/tests/modeltests/custom_columns/tests.py b/tests/modeltests/custom_columns/tests.py
new file mode 100644
index 0000000000..e8ec21f06f
--- /dev/null
+++ b/tests/modeltests/custom_columns/tests.py
@@ -0,0 +1,71 @@
+from django.core.exceptions import FieldError
+from django.test import TestCase
+
+from models import Author, Article
+
+
+class CustomColumnsTests(TestCase):
+ def test_db_column(self):
+ a1 = Author.objects.create(first_name="John", last_name="Smith")
+ a2 = Author.objects.create(first_name="Peter", last_name="Jones")
+
+ art = Article.objects.create(headline="Django lets you build web apps easily")
+ art.authors = [a1, a2]
+
+ # Although the table and column names on Author have been set to custom
+ # values, nothing about using the Author model has changed...
+
+ # Query the available authors
+ self.assertQuerysetEqual(
+ Author.objects.all(), [
+ "Peter Jones", "John Smith",
+ ],
+ unicode
+ )
+ self.assertQuerysetEqual(
+ Author.objects.filter(first_name__exact="John"), [
+ "John Smith",
+ ],
+ unicode
+ )
+ self.assertEqual(
+ Author.objects.get(first_name__exact="John"),
+ a1,
+ )
+
+ self.assertRaises(FieldError,
+ lambda: Author.objects.filter(firstname__exact="John")
+ )
+
+ a = Author.objects.get(last_name__exact="Smith")
+ a.first_name = "John"
+ a.last_name = "Smith"
+
+ self.assertRaises(AttributeError, lambda: a.firstname)
+ self.assertRaises(AttributeError, lambda: a.last)
+
+ # Although the Article table uses a custom m2m table,
+ # nothing about using the m2m relationship has changed...
+
+ # Get all the authors for an article
+ self.assertQuerysetEqual(
+ art.authors.all(), [
+ "Peter Jones",
+ "John Smith",
+ ],
+ unicode
+ )
+ # Get the articles for an author
+ self.assertQuerysetEqual(
+ a.article_set.all(), [
+ "Django lets you build web apps easily",
+ ],
+ lambda a: a.headline
+ )
+ # Query the authors across the m2m relation
+ self.assertQuerysetEqual(
+ art.authors.filter(last_name='Jones'), [
+ "Peter Jones"
+ ],
+ unicode
+ )
diff --git a/tests/modeltests/custom_managers/models.py b/tests/modeltests/custom_managers/models.py
index 40bf77e273..1052552bb3 100644
--- a/tests/modeltests/custom_managers/models.py
+++ b/tests/modeltests/custom_managers/models.py
@@ -57,51 +57,3 @@ class Car(models.Model):
def __unicode__(self):
return self.name
-
-__test__ = {'API_TESTS':"""
->>> p1 = Person(first_name='Bugs', last_name='Bunny', fun=True)
->>> p1.save()
->>> p2 = Person(first_name='Droopy', last_name='Dog', fun=False)
->>> p2.save()
->>> Person.objects.get_fun_people()
-[<Person: Bugs Bunny>]
-
-# The RelatedManager used on the 'books' descriptor extends the default manager
->>> from modeltests.custom_managers.models import PublishedBookManager
->>> isinstance(p2.books, PublishedBookManager)
-True
-
->>> b1 = Book(title='How to program', author='Rodney Dangerfield', is_published=True)
->>> b1.save()
->>> b2 = Book(title='How to be smart', author='Albert Einstein', is_published=False)
->>> b2.save()
-
-# The default manager, "objects", doesn't exist,
-# because a custom one was provided.
->>> Book.objects
-Traceback (most recent call last):
- ...
-AttributeError: type object 'Book' has no attribute 'objects'
-
-# The RelatedManager used on the 'authors' descriptor extends the default manager
->>> from modeltests.custom_managers.models import PersonManager
->>> isinstance(b2.authors, PersonManager)
-True
-
->>> Book.published_objects.all()
-[<Book: How to program>]
-
->>> c1 = Car(name='Corvette', mileage=21, top_speed=180)
->>> c1.save()
->>> c2 = Car(name='Neon', mileage=31, top_speed=100)
->>> c2.save()
->>> Car.cars.order_by('name')
-[<Car: Corvette>, <Car: Neon>]
->>> Car.fast_cars.all()
-[<Car: Corvette>]
-
-# Each model class gets a "_default_manager" attribute, which is a reference
-# to the first manager defined in the class. In this case, it's "cars".
->>> Car._default_manager.order_by('name')
-[<Car: Corvette>, <Car: Neon>]
-"""}
diff --git a/tests/modeltests/custom_managers/tests.py b/tests/modeltests/custom_managers/tests.py
new file mode 100644
index 0000000000..8721e9ac52
--- /dev/null
+++ b/tests/modeltests/custom_managers/tests.py
@@ -0,0 +1,71 @@
+from django.test import TestCase
+
+from models import Person, Book, Car, PersonManager, PublishedBookManager
+
+
+class CustomManagerTests(TestCase):
+ def test_manager(self):
+ p1 = Person.objects.create(first_name="Bugs", last_name="Bunny", fun=True)
+ p2 = Person.objects.create(first_name="Droopy", last_name="Dog", fun=False)
+
+ self.assertQuerysetEqual(
+ Person.objects.get_fun_people(), [
+ "Bugs Bunny"
+ ],
+ unicode
+ )
+ # The RelatedManager used on the 'books' descriptor extends the default
+ # manager
+ self.assertTrue(isinstance(p2.books, PublishedBookManager))
+
+ b1 = Book.published_objects.create(
+ title="How to program", author="Rodney Dangerfield", is_published=True
+ )
+ b2 = Book.published_objects.create(
+ title="How to be smart", author="Albert Einstein", is_published=False
+ )
+
+ # The default manager, "objects", doesn't exist, because a custom one
+ # was provided.
+ self.assertRaises(AttributeError, lambda: Book.objects)
+
+ # The RelatedManager used on the 'authors' descriptor extends the
+ # default manager
+ self.assertTrue(isinstance(b2.authors, PersonManager))
+
+ self.assertQuerysetEqual(
+ Book.published_objects.all(), [
+ "How to program",
+ ],
+ lambda b: b.title
+ )
+
+ c1 = Car.cars.create(name="Corvette", mileage=21, top_speed=180)
+ c2 = Car.cars.create(name="Neon", mileage=31, top_speed=100)
+
+ self.assertQuerysetEqual(
+ Car.cars.order_by("name"), [
+ "Corvette",
+ "Neon",
+ ],
+ lambda c: c.name
+ )
+
+ self.assertQuerysetEqual(
+ Car.fast_cars.all(), [
+ "Corvette",
+ ],
+ lambda c: c.name
+ )
+
+ # Each model class gets a "_default_manager" attribute, which is a
+ # reference to the first manager defined in the class. In this case,
+ # it's "cars".
+
+ self.assertQuerysetEqual(
+ Car._default_manager.order_by("name"), [
+ "Corvette",
+ "Neon",
+ ],
+ lambda c: c.name
+ )
diff --git a/tests/modeltests/custom_methods/models.py b/tests/modeltests/custom_methods/models.py
index d420871373..15150a6c3f 100644
--- a/tests/modeltests/custom_methods/models.py
+++ b/tests/modeltests/custom_methods/models.py
@@ -33,27 +33,4 @@ class Article(models.Model):
WHERE pub_date = %s
AND id != %s""", [connection.ops.value_to_db_date(self.pub_date),
self.id])
- # The asterisk in "(*row)" tells Python to expand the list into
- # positional arguments to Article().
return [self.__class__(*row) for row in cursor.fetchall()]
-
-__test__ = {'API_TESTS':"""
-# Create a couple of Articles.
->>> from datetime import date
->>> a = Article(id=None, headline='Area man programs in Python', pub_date=date(2005, 7, 27))
->>> a.save()
->>> b = Article(id=None, headline='Beatles reunite', pub_date=date(2005, 7, 27))
->>> b.save()
-
-# Test the custom methods.
->>> a.was_published_today()
-False
->>> a.articles_from_same_day_1()
-[<Article: Beatles reunite>]
->>> a.articles_from_same_day_2()
-[<Article: Beatles reunite>]
->>> b.articles_from_same_day_1()
-[<Article: Area man programs in Python>]
->>> b.articles_from_same_day_2()
-[<Article: Area man programs in Python>]
-"""}
diff --git a/tests/modeltests/custom_methods/tests.py b/tests/modeltests/custom_methods/tests.py
new file mode 100644
index 0000000000..90a7f0da29
--- /dev/null
+++ b/tests/modeltests/custom_methods/tests.py
@@ -0,0 +1,42 @@
+from datetime import date
+
+from django.test import TestCase
+
+from models import Article
+
+
+class MethodsTests(TestCase):
+ def test_custom_methods(self):
+ a = Article.objects.create(
+ headline="Area man programs in Python", pub_date=date(2005, 7, 27)
+ )
+ b = Article.objects.create(
+ headline="Beatles reunite", pub_date=date(2005, 7, 27)
+ )
+
+ self.assertFalse(a.was_published_today())
+ self.assertQuerysetEqual(
+ a.articles_from_same_day_1(), [
+ "Beatles reunite",
+ ],
+ lambda a: a.headline,
+ )
+ self.assertQuerysetEqual(
+ a.articles_from_same_day_2(), [
+ "Beatles reunite",
+ ],
+ lambda a: a.headline
+ )
+
+ self.assertQuerysetEqual(
+ b.articles_from_same_day_1(), [
+ "Area man programs in Python",
+ ],
+ lambda a: a.headline,
+ )
+ self.assertQuerysetEqual(
+ b.articles_from_same_day_2(), [
+ "Area man programs in Python",
+ ],
+ lambda a: a.headline
+ )
diff --git a/tests/modeltests/custom_pk/fields.py b/tests/modeltests/custom_pk/fields.py
index 319e42f974..2eeb80e6ac 100644
--- a/tests/modeltests/custom_pk/fields.py
+++ b/tests/modeltests/custom_pk/fields.py
@@ -3,6 +3,7 @@ import string
from django.db import models
+
class MyWrapper(object):
def __init__(self, value):
self.value = value
diff --git a/tests/modeltests/custom_pk/models.py b/tests/modeltests/custom_pk/models.py
index 84e6480af9..ff2f2bac1b 100644
--- a/tests/modeltests/custom_pk/models.py
+++ b/tests/modeltests/custom_pk/models.py
@@ -40,138 +40,3 @@ class Bar(models.Model):
class Foo(models.Model):
bar = models.ForeignKey(Bar)
-__test__ = {'API_TESTS':"""
->>> dan = Employee(employee_code=123, first_name='Dan', last_name='Jones')
->>> dan.save()
->>> Employee.objects.all()
-[<Employee: Dan Jones>]
-
->>> fran = Employee(employee_code=456, first_name='Fran', last_name='Bones')
->>> fran.save()
->>> Employee.objects.all()
-[<Employee: Fran Bones>, <Employee: Dan Jones>]
-
->>> Employee.objects.get(pk=123)
-<Employee: Dan Jones>
->>> Employee.objects.get(pk=456)
-<Employee: Fran Bones>
->>> Employee.objects.get(pk=42)
-Traceback (most recent call last):
- ...
-DoesNotExist: Employee matching query does not exist.
-
-# Use the name of the primary key, rather than pk.
->>> Employee.objects.get(employee_code__exact=123)
-<Employee: Dan Jones>
-
-# pk can be used as a substitute for the primary key.
->>> Employee.objects.filter(pk__in=[123, 456])
-[<Employee: Fran Bones>, <Employee: Dan Jones>]
-
-# The primary key can be accessed via the pk property on the model.
->>> e = Employee.objects.get(pk=123)
->>> e.pk
-123
-
-# Or we can use the real attribute name for the primary key:
->>> e.employee_code
-123
-
-# Fran got married and changed her last name.
->>> fran = Employee.objects.get(pk=456)
->>> fran.last_name = 'Jones'
->>> fran.save()
->>> Employee.objects.filter(last_name__exact='Jones')
-[<Employee: Dan Jones>, <Employee: Fran Jones>]
->>> emps = Employee.objects.in_bulk([123, 456])
->>> emps[123]
-<Employee: Dan Jones>
-
->>> b = Business(name='Sears')
->>> b.save()
->>> b.employees.add(dan, fran)
->>> b.employees.all()
-[<Employee: Dan Jones>, <Employee: Fran Jones>]
->>> fran.business_set.all()
-[<Business: Sears>]
->>> Business.objects.in_bulk(['Sears'])
-{u'Sears': <Business: Sears>}
-
->>> Business.objects.filter(name__exact='Sears')
-[<Business: Sears>]
->>> Business.objects.filter(pk='Sears')
-[<Business: Sears>]
-
-# Queries across tables, involving primary key
->>> Employee.objects.filter(business__name__exact='Sears')
-[<Employee: Dan Jones>, <Employee: Fran Jones>]
->>> Employee.objects.filter(business__pk='Sears')
-[<Employee: Dan Jones>, <Employee: Fran Jones>]
-
->>> Business.objects.filter(employees__employee_code__exact=123)
-[<Business: Sears>]
->>> Business.objects.filter(employees__pk=123)
-[<Business: Sears>]
->>> Business.objects.filter(employees__first_name__startswith='Fran')
-[<Business: Sears>]
-
-# Primary key may be unicode string
->>> bus = Business(name=u'jaźń')
->>> bus.save()
-
-# The primary key must also obviously be unique, so trying to create a new
-# object with the same primary key will fail.
->>> try:
-... sid = transaction.savepoint()
-... Employee.objects.create(employee_code=123, first_name='Fred', last_name='Jones')
-... transaction.savepoint_commit(sid)
-... except Exception, e:
-... if isinstance(e, IntegrityError):
-... transaction.savepoint_rollback(sid)
-... print "Pass"
-... else:
-... print "Fail with %s" % type(e)
-Pass
-
-# Regression for #10785 -- Custom fields can be used for primary keys.
->>> new_bar = Bar.objects.create()
->>> new_foo = Foo.objects.create(bar=new_bar)
-
-# FIXME: This still doesn't work, but will require some changes in
-# get_db_prep_lookup to fix it.
-# >>> f = Foo.objects.get(bar=new_bar.pk)
-# >>> f == new_foo
-# True
-# >>> f.bar == new_bar
-# True
-
->>> f = Foo.objects.get(bar=new_bar)
->>> f == new_foo
-True
->>> f.bar == new_bar
-True
-
-"""}
-
-# SQLite lets objects be saved with an empty primary key, even though an
-# integer is expected. So we can't check for an error being raised in that case
-# for SQLite. Remove it from the suite for this next bit.
-if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.sqlite3':
- __test__["API_TESTS"] += """
-# The primary key must be specified, so an error is raised if you try to create
-# an object without it.
->>> try:
-... sid = transaction.savepoint()
-... Employee.objects.create(first_name='Tom', last_name='Smith')
-... print 'hello'
-... transaction.savepoint_commit(sid)
-... print 'hello2'
-... except Exception, e:
-... if isinstance(e, IntegrityError):
-... transaction.savepoint_rollback(sid)
-... print "Pass"
-... else:
-... print "Fail with %s" % type(e)
-Pass
-
-"""
diff --git a/tests/modeltests/custom_pk/tests.py b/tests/modeltests/custom_pk/tests.py
new file mode 100644
index 0000000000..6ef4bdd433
--- /dev/null
+++ b/tests/modeltests/custom_pk/tests.py
@@ -0,0 +1,183 @@
+# -*- coding: utf-8 -*-
+from django.conf import settings
+from django.db import DEFAULT_DB_ALIAS, transaction, IntegrityError
+from django.test import TestCase
+
+from models import Employee, Business, Bar, Foo
+
+
+class CustomPKTests(TestCase):
+ def test_custom_pk(self):
+ dan = Employee.objects.create(
+ employee_code=123, first_name="Dan", last_name="Jones"
+ )
+ self.assertQuerysetEqual(
+ Employee.objects.all(), [
+ "Dan Jones",
+ ],
+ unicode
+ )
+
+ fran = Employee.objects.create(
+ employee_code=456, first_name="Fran", last_name="Bones"
+ )
+ self.assertQuerysetEqual(
+ Employee.objects.all(), [
+ "Fran Bones",
+ "Dan Jones",
+ ],
+ unicode
+ )
+
+ self.assertEqual(Employee.objects.get(pk=123), dan)
+ self.assertEqual(Employee.objects.get(pk=456), fran)
+
+ self.assertRaises(Employee.DoesNotExist,
+ lambda: Employee.objects.get(pk=42)
+ )
+
+ # Use the name of the primary key, rather than pk.
+ self.assertEqual(Employee.objects.get(employee_code=123), dan)
+ # pk can be used as a substitute for the primary key.
+ self.assertQuerysetEqual(
+ Employee.objects.filter(pk__in=[123, 456]), [
+ "Fran Bones",
+ "Dan Jones",
+ ],
+ unicode
+ )
+ # The primary key can be accessed via the pk property on the model.
+ e = Employee.objects.get(pk=123)
+ self.assertEqual(e.pk, 123)
+ # Or we can use the real attribute name for the primary key:
+ self.assertEqual(e.employee_code, 123)
+
+ # Fran got married and changed her last name.
+ fran = Employee.objects.get(pk=456)
+ fran.last_name = "Jones"
+ fran.save()
+
+ self.assertQuerysetEqual(
+ Employee.objects.filter(last_name="Jones"), [
+ "Dan Jones",
+ "Fran Jones",
+ ],
+ unicode
+ )
+
+ emps = Employee.objects.in_bulk([123, 456])
+ self.assertEqual(emps[123], dan)
+
+ b = Business.objects.create(name="Sears")
+ b.employees.add(dan, fran)
+ self.assertQuerysetEqual(
+ b.employees.all(), [
+ "Dan Jones",
+ "Fran Jones",
+ ],
+ unicode
+ )
+ self.assertQuerysetEqual(
+ fran.business_set.all(), [
+ "Sears",
+ ],
+ lambda b: b.name
+ )
+
+ self.assertEqual(Business.objects.in_bulk(["Sears"]), {
+ "Sears": b,
+ })
+
+ self.assertQuerysetEqual(
+ Business.objects.filter(name="Sears"), [
+ "Sears"
+ ],
+ lambda b: b.name
+ )
+ self.assertQuerysetEqual(
+ Business.objects.filter(pk="Sears"), [
+ "Sears",
+ ],
+ lambda b: b.name
+ )
+
+ # Queries across tables, involving primary key
+ self.assertQuerysetEqual(
+ Employee.objects.filter(business__name="Sears"), [
+ "Dan Jones",
+ "Fran Jones",
+ ],
+ unicode,
+ )
+ self.assertQuerysetEqual(
+ Employee.objects.filter(business__pk="Sears"), [
+ "Dan Jones",
+ "Fran Jones",
+ ],
+ unicode,
+ )
+
+ self.assertQuerysetEqual(
+ Business.objects.filter(employees__employee_code=123), [
+ "Sears",
+ ],
+ lambda b: b.name
+ )
+ self.assertQuerysetEqual(
+ Business.objects.filter(employees__pk=123), [
+ "Sears",
+ ],
+ lambda b: b.name,
+ )
+
+ self.assertQuerysetEqual(
+ Business.objects.filter(employees__first_name__startswith="Fran"), [
+ "Sears",
+ ],
+ lambda b: b.name
+ )
+
+ def test_unicode_pk(self):
+ # Primary key may be unicode string
+ bus = Business.objects.create(name=u'jaźń')
+
+ def test_unique_pk(self):
+ # The primary key must also obviously be unique, so trying to create a
+ # new object with the same primary key will fail.
+ e = Employee.objects.create(
+ employee_code=123, first_name="Frank", last_name="Jones"
+ )
+ sid = transaction.savepoint()
+ self.assertRaises(IntegrityError,
+ Employee.objects.create, employee_code=123, first_name="Fred", last_name="Jones"
+ )
+ transaction.savepoint_rollback(sid)
+
+ def test_custom_field_pk(self):
+ # Regression for #10785 -- Custom fields can be used for primary keys.
+ new_bar = Bar.objects.create()
+ new_foo = Foo.objects.create(bar=new_bar)
+
+ # FIXME: This still doesn't work, but will require some changes in
+ # get_db_prep_lookup to fix it.
+ # f = Foo.objects.get(bar=new_bar.pk)
+ # self.assertEqual(f, new_foo)
+ # self.assertEqual(f.bar, new_bar)
+
+ f = Foo.objects.get(bar=new_bar)
+ self.assertEqual(f, new_foo),
+ self.assertEqual(f.bar, new_bar)
+
+
+ # SQLite lets objects be saved with an empty primary key, even though an
+ # integer is expected. So we can't check for an error being raised in that
+ # case for SQLite. Remove it from the suite for this next bit.
+ if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.sqlite3':
+ def test_required_pk(self):
+ # The primary key must be specified, so an error is raised if you
+ # try to create an object without it.
+ sid = transaction.savepoint()
+ self.assertRaises(IntegrityError,
+ Employee.objects.create, first_name="Tom", last_name="Smith"
+ )
+ transaction.savepoint_rollback(sid)
diff --git a/tests/modeltests/defer/models.py b/tests/modeltests/defer/models.py
index ac3c876a57..4fddd39d26 100644
--- a/tests/modeltests/defer/models.py
+++ b/tests/modeltests/defer/models.py
@@ -3,7 +3,7 @@ Tests for defer() and only().
"""
from django.db import models
-from django.db.models.query_utils import DeferredAttribute
+
class Secondary(models.Model):
first = models.CharField(max_length=50)
@@ -22,165 +22,3 @@ class Child(Primary):
class BigChild(Primary):
other = models.CharField(max_length=50)
-
-def count_delayed_fields(obj, debug=False):
- """
- Returns the number of delayed attributes on the given model instance.
- """
- count = 0
- for field in obj._meta.fields:
- if isinstance(obj.__class__.__dict__.get(field.attname),
- DeferredAttribute):
- if debug:
- print field.name, field.attname
- count += 1
- return count
-
-
-__test__ = {"API_TEST": """
-To all outward appearances, instances with deferred fields look the same as
-normal instances when we examine attribute values. Therefore we test for the
-number of deferred fields on returned instances (by poking at the internals),
-as a way to observe what is going on.
-
->>> s1 = Secondary.objects.create(first="x1", second="y1")
->>> p1 = Primary.objects.create(name="p1", value="xx", related=s1)
-
->>> qs = Primary.objects.all()
-
->>> count_delayed_fields(qs.defer('name')[0])
-1
->>> count_delayed_fields(qs.only('name')[0])
-2
->>> count_delayed_fields(qs.defer('related__first')[0])
-0
->>> obj = qs.select_related().only('related__first')[0]
->>> count_delayed_fields(obj)
-2
->>> obj.related_id == s1.pk
-True
->>> count_delayed_fields(qs.defer('name').extra(select={'a': 1})[0])
-1
->>> count_delayed_fields(qs.extra(select={'a': 1}).defer('name')[0])
-1
->>> count_delayed_fields(qs.defer('name').defer('value')[0])
-2
->>> count_delayed_fields(qs.only('name').only('value')[0])
-2
->>> count_delayed_fields(qs.only('name').defer('value')[0])
-2
->>> count_delayed_fields(qs.only('name', 'value').defer('value')[0])
-2
->>> count_delayed_fields(qs.defer('name').only('value')[0])
-2
->>> obj = qs.only()[0]
->>> count_delayed_fields(qs.defer(None)[0])
-0
->>> count_delayed_fields(qs.only('name').defer(None)[0])
-0
-
-User values() won't defer anything (you get the full list of dictionaries
-back), but it still works.
->>> qs.defer('name').values()[0] == {'id': p1.id, 'name': u'p1', 'value': 'xx', 'related_id': s1.id}
-True
->>> qs.only('name').values()[0] == {'id': p1.id, 'name': u'p1', 'value': 'xx', 'related_id': s1.id}
-True
-
-Using defer() and only() with get() is also valid.
->>> count_delayed_fields(qs.defer('name').get(pk=p1.pk))
-1
->>> count_delayed_fields(qs.only('name').get(pk=p1.pk))
-2
-
-# KNOWN NOT TO WORK: >>> count_delayed_fields(qs.only('name').select_related('related')[0])
-# KNOWN NOT TO WORK >>> count_delayed_fields(qs.defer('related').select_related('related')[0])
-
-# Saving models with deferred fields is possible (but inefficient, since every
-# field has to be retrieved first).
-
->>> obj = Primary.objects.defer("value").get(name="p1")
->>> obj.name = "a new name"
->>> obj.save()
->>> Primary.objects.all()
-[<Primary: a new name>]
-
-# Regression for #10572 - A subclass with no extra fields can defer fields from the base class
->>> _ = Child.objects.create(name="c1", value="foo", related=s1)
-
-# You can defer a field on a baseclass when the subclass has no fields
->>> obj = Child.objects.defer("value").get(name="c1")
->>> count_delayed_fields(obj)
-1
->>> obj.name
-u"c1"
->>> obj.value
-u"foo"
->>> obj.name = "c2"
->>> obj.save()
-
-# You can retrive a single column on a base class with no fields
->>> obj = Child.objects.only("name").get(name="c2")
->>> count_delayed_fields(obj)
-3
->>> obj.name
-u"c2"
->>> obj.value
-u"foo"
->>> obj.name = "cc"
->>> obj.save()
-
->>> _ = BigChild.objects.create(name="b1", value="foo", related=s1, other="bar")
-
-# You can defer a field on a baseclass
->>> obj = BigChild.objects.defer("value").get(name="b1")
->>> count_delayed_fields(obj)
-1
->>> obj.name
-u"b1"
->>> obj.value
-u"foo"
->>> obj.other
-u"bar"
->>> obj.name = "b2"
->>> obj.save()
-
-# You can defer a field on a subclass
->>> obj = BigChild.objects.defer("other").get(name="b2")
->>> count_delayed_fields(obj)
-1
->>> obj.name
-u"b2"
->>> obj.value
-u"foo"
->>> obj.other
-u"bar"
->>> obj.name = "b3"
->>> obj.save()
-
-# You can retrieve a single field on a baseclass
->>> obj = BigChild.objects.only("name").get(name="b3")
->>> count_delayed_fields(obj)
-4
->>> obj.name
-u"b3"
->>> obj.value
-u"foo"
->>> obj.other
-u"bar"
->>> obj.name = "b4"
->>> obj.save()
-
-# You can retrieve a single field on a baseclass
->>> obj = BigChild.objects.only("other").get(name="b4")
->>> count_delayed_fields(obj)
-4
->>> obj.name
-u"b4"
->>> obj.value
-u"foo"
->>> obj.other
-u"bar"
->>> obj.name = "bb"
->>> obj.save()
-
-"""}
diff --git a/tests/modeltests/defer/tests.py b/tests/modeltests/defer/tests.py
new file mode 100644
index 0000000000..5f6c53dee2
--- /dev/null
+++ b/tests/modeltests/defer/tests.py
@@ -0,0 +1,137 @@
+from django.db.models.query_utils import DeferredAttribute
+from django.test import TestCase
+
+from models import Secondary, Primary, Child, BigChild
+
+
+class DeferTests(TestCase):
+ def assert_delayed(self, obj, num):
+ count = 0
+ for field in obj._meta.fields:
+ if isinstance(obj.__class__.__dict__.get(field.attname),
+ DeferredAttribute):
+ count += 1
+ self.assertEqual(count, num)
+
+ def test_defer(self):
+ # To all outward appearances, instances with deferred fields look the
+ # same as normal instances when we examine attribute values. Therefore
+ # we test for the number of deferred fields on returned instances (by
+ # poking at the internals), as a way to observe what is going on.
+
+ s1 = Secondary.objects.create(first="x1", second="y1")
+ p1 = Primary.objects.create(name="p1", value="xx", related=s1)
+
+ qs = Primary.objects.all()
+
+ self.assert_delayed(qs.defer("name")[0], 1)
+ self.assert_delayed(qs.only("name")[0], 2)
+ self.assert_delayed(qs.defer("related__first")[0], 0)
+
+ obj = qs.select_related().only("related__first")[0]
+ self.assert_delayed(obj, 2)
+
+ self.assertEqual(obj.related_id, s1.pk)
+
+ self.assert_delayed(qs.defer("name").extra(select={"a": 1})[0], 1)
+ self.assert_delayed(qs.extra(select={"a": 1}).defer("name")[0], 1)
+ self.assert_delayed(qs.defer("name").defer("value")[0], 2)
+ self.assert_delayed(qs.only("name").only("value")[0], 2)
+ self.assert_delayed(qs.only("name").defer("value")[0], 2)
+ self.assert_delayed(qs.only("name", "value").defer("value")[0], 2)
+ self.assert_delayed(qs.defer("name").only("value")[0], 2)
+
+ obj = qs.only()[0]
+ self.assert_delayed(qs.defer(None)[0], 0)
+ self.assert_delayed(qs.only("name").defer(None)[0], 0)
+
+ # User values() won't defer anything (you get the full list of
+ # dictionaries back), but it still works.
+ self.assertEqual(qs.defer("name").values()[0], {
+ "id": p1.id,
+ "name": "p1",
+ "value": "xx",
+ "related_id": s1.id,
+ })
+ self.assertEqual(qs.only("name").values()[0], {
+ "id": p1.id,
+ "name": "p1",
+ "value": "xx",
+ "related_id": s1.id,
+ })
+
+ # Using defer() and only() with get() is also valid.
+ self.assert_delayed(qs.defer("name").get(pk=p1.pk), 1)
+ self.assert_delayed(qs.only("name").get(pk=p1.pk), 2)
+
+ # DOES THIS WORK?
+ self.assert_delayed(qs.only("name").select_related("related")[0], 1)
+ self.assert_delayed(qs.defer("related").select_related("related")[0], 0)
+
+ # Saving models with deferred fields is possible (but inefficient,
+ # since every field has to be retrieved first).
+ obj = Primary.objects.defer("value").get(name="p1")
+ obj.name = "a new name"
+ obj.save()
+ self.assertQuerysetEqual(
+ Primary.objects.all(), [
+ "a new name",
+ ],
+ lambda p: p.name
+ )
+
+ # Regression for #10572 - A subclass with no extra fields can defer
+ # fields from the base class
+ Child.objects.create(name="c1", value="foo", related=s1)
+ # You can defer a field on a baseclass when the subclass has no fields
+ obj = Child.objects.defer("value").get(name="c1")
+ self.assert_delayed(obj, 1)
+ self.assertEqual(obj.name, "c1")
+ self.assertEqual(obj.value, "foo")
+ obj.name = "c2"
+ obj.save()
+
+ # You can retrive a single column on a base class with no fields
+ obj = Child.objects.only("name").get(name="c2")
+ self.assert_delayed(obj, 3)
+ self.assertEqual(obj.name, "c2")
+ self.assertEqual(obj.value, "foo")
+ obj.name = "cc"
+ obj.save()
+
+ BigChild.objects.create(name="b1", value="foo", related=s1, other="bar")
+ # You can defer a field on a baseclass
+ obj = BigChild.objects.defer("value").get(name="b1")
+ self.assert_delayed(obj, 1)
+ self.assertEqual(obj.name, "b1")
+ self.assertEqual(obj.value, "foo")
+ self.assertEqual(obj.other, "bar")
+ obj.name = "b2"
+ obj.save()
+
+ # You can defer a field on a subclass
+ obj = BigChild.objects.defer("other").get(name="b2")
+ self.assert_delayed(obj, 1)
+ self.assertEqual(obj.name, "b2")
+ self.assertEqual(obj.value, "foo")
+ self.assertEqual(obj.other, "bar")
+ obj.name = "b3"
+ obj.save()
+
+ # You can retrieve a single field on a baseclass
+ obj = BigChild.objects.only("name").get(name="b3")
+ self.assert_delayed(obj, 4)
+ self.assertEqual(obj.name, "b3")
+ self.assertEqual(obj.value, "foo")
+ self.assertEqual(obj.other, "bar")
+ obj.name = "b4"
+ obj.save()
+
+ # You can retrieve a single field on a baseclass
+ obj = BigChild.objects.only("other").get(name="b4")
+ self.assert_delayed(obj, 4)
+ self.assertEqual(obj.name, "b4")
+ self.assertEqual(obj.value, "foo")
+ self.assertEqual(obj.other, "bar")
+ obj.name = "bb"
+ obj.save()
diff --git a/tests/modeltests/delete/models.py b/tests/modeltests/delete/models.py
index 0e063fd8f0..9c81f6b8f8 100644
--- a/tests/modeltests/delete/models.py
+++ b/tests/modeltests/delete/models.py
@@ -40,168 +40,3 @@ class E(DefaultRepr, models.Model):
class F(DefaultRepr, models.Model):
e = models.ForeignKey(E, related_name='f_rel')
-
-__test__ = {'API_TESTS': """
-### Tests for models A,B,C,D ###
-
-## First, test the CollectedObjects data structure directly
-
->>> from django.db.models.query import CollectedObjects
-
->>> g = CollectedObjects()
->>> g.add("key1", 1, "item1", None)
-False
->>> g["key1"]
-{1: 'item1'}
->>> g.add("key2", 1, "item1", "key1")
-False
->>> g.add("key2", 2, "item2", "key1")
-False
->>> g["key2"]
-{1: 'item1', 2: 'item2'}
->>> g.add("key3", 1, "item1", "key1")
-False
->>> g.add("key3", 1, "item1", "key2")
-True
->>> g.ordered_keys()
-['key3', 'key2', 'key1']
-
->>> g.add("key2", 1, "item1", "key3")
-True
->>> g.ordered_keys()
-Traceback (most recent call last):
- ...
-CyclicDependency: There is a cyclic dependency of items to be processed.
-
-
-## Second, test the usage of CollectedObjects by Model.delete()
-
-# Due to the way that transactions work in the test harness,
-# doing m.delete() here can work but fail in a real situation,
-# since it may delete all objects, but not in the right order.
-# So we manually check that the order of deletion is correct.
-
-# Also, it is possible that the order is correct 'accidentally', due
-# solely to order of imports etc. To check this, we set the order
-# that 'get_models()' will retrieve to a known 'nice' order, and
-# then try again with a known 'tricky' order. Slightly naughty
-# access to internals here :-)
-
-# If implementation changes, then the tests may need to be simplified:
-# - remove the lines that set the .keyOrder and clear the related
-# object caches
-# - remove the second set of tests (with a2, b2 etc)
-
->>> from django.db.models.loading import cache
-
->>> def clear_rel_obj_caches(models):
-... for m in models:
-... if hasattr(m._meta, '_related_objects_cache'):
-... del m._meta._related_objects_cache
-
-# Nice order
->>> cache.app_models['delete'].keyOrder = ['a', 'b', 'c', 'd']
->>> clear_rel_obj_caches([A, B, C, D])
-
->>> a1 = A()
->>> a1.save()
->>> b1 = B(a=a1)
->>> b1.save()
->>> c1 = C(b=b1)
->>> c1.save()
->>> d1 = D(c=c1, a=a1)
->>> d1.save()
-
->>> o = CollectedObjects()
->>> a1._collect_sub_objects(o)
->>> o.keys()
-[<class 'modeltests.delete.models.D'>, <class 'modeltests.delete.models.C'>, <class 'modeltests.delete.models.B'>, <class 'modeltests.delete.models.A'>]
->>> a1.delete()
-
-# Same again with a known bad order
->>> cache.app_models['delete'].keyOrder = ['d', 'c', 'b', 'a']
->>> clear_rel_obj_caches([A, B, C, D])
-
->>> a2 = A()
->>> a2.save()
->>> b2 = B(a=a2)
->>> b2.save()
->>> c2 = C(b=b2)
->>> c2.save()
->>> d2 = D(c=c2, a=a2)
->>> d2.save()
-
->>> o = CollectedObjects()
->>> a2._collect_sub_objects(o)
->>> o.keys()
-[<class 'modeltests.delete.models.D'>, <class 'modeltests.delete.models.C'>, <class 'modeltests.delete.models.B'>, <class 'modeltests.delete.models.A'>]
->>> a2.delete()
-
-### Tests for models E,F - nullable related fields ###
-
-## First, test the CollectedObjects data structure directly
-
->>> g = CollectedObjects()
->>> g.add("key1", 1, "item1", None)
-False
->>> g.add("key2", 1, "item1", "key1", nullable=True)
-False
->>> g.add("key1", 1, "item1", "key2")
-True
->>> g.ordered_keys()
-['key1', 'key2']
-
-## Second, test the usage of CollectedObjects by Model.delete()
-
->>> e1 = E()
->>> e1.save()
->>> f1 = F(e=e1)
->>> f1.save()
->>> e1.f = f1
->>> e1.save()
-
-# Since E.f is nullable, we should delete F first (after nulling out
-# the E.f field), then E.
-
->>> o = CollectedObjects()
->>> e1._collect_sub_objects(o)
->>> o.keys()
-[<class 'modeltests.delete.models.F'>, <class 'modeltests.delete.models.E'>]
-
-# temporarily replace the UpdateQuery class to verify that E.f is actually nulled out first
->>> import django.db.models.sql
->>> class LoggingUpdateQuery(django.db.models.sql.UpdateQuery):
-... def clear_related(self, related_field, pk_list, using):
-... print "CLEARING FIELD",related_field.name
-... return super(LoggingUpdateQuery, self).clear_related(related_field, pk_list, using)
->>> original_class = django.db.models.sql.UpdateQuery
->>> django.db.models.sql.UpdateQuery = LoggingUpdateQuery
->>> e1.delete()
-CLEARING FIELD f
-
->>> e2 = E()
->>> e2.save()
->>> f2 = F(e=e2)
->>> f2.save()
->>> e2.f = f2
->>> e2.save()
-
-# Same deal as before, though we are starting from the other object.
-
->>> o = CollectedObjects()
->>> f2._collect_sub_objects(o)
->>> o.keys()
-[<class 'modeltests.delete.models.F'>, <class 'modeltests.delete.models.E'>]
-
->>> f2.delete()
-CLEARING FIELD f
-
-# Put this back to normal
->>> django.db.models.sql.UpdateQuery = original_class
-
-# Restore the app cache to previous condition so that all models are accounted for.
->>> cache.app_models['delete'].keyOrder = ['a', 'b', 'c', 'd', 'e', 'f']
->>> clear_rel_obj_caches([A, B, C, D, E, F])
-
-"""
-}
diff --git a/tests/modeltests/delete/tests.py b/tests/modeltests/delete/tests.py
new file mode 100644
index 0000000000..7927cce1c1
--- /dev/null
+++ b/tests/modeltests/delete/tests.py
@@ -0,0 +1,135 @@
+from django.db.models import sql
+from django.db.models.loading import cache
+from django.db.models.query import CollectedObjects
+from django.db.models.query_utils import CyclicDependency
+from django.test import TestCase
+
+from models import A, B, C, D, E, F
+
+
+class DeleteTests(TestCase):
+ def clear_rel_obj_caches(self, *models):
+ for m in models:
+ if hasattr(m._meta, '_related_objects_cache'):
+ del m._meta._related_objects_cache
+
+ def order_models(self, *models):
+ cache.app_models["delete"].keyOrder = models
+
+ def setUp(self):
+ self.order_models("a", "b", "c", "d", "e", "f")
+ self.clear_rel_obj_caches(A, B, C, D, E, F)
+
+ def tearDown(self):
+ self.order_models("a", "b", "c", "d", "e", "f")
+ self.clear_rel_obj_caches(A, B, C, D, E, F)
+
+ def test_collected_objects(self):
+ g = CollectedObjects()
+ self.assertFalse(g.add("key1", 1, "item1", None))
+ self.assertEqual(g["key1"], {1: "item1"})
+
+ self.assertFalse(g.add("key2", 1, "item1", "key1"))
+ self.assertFalse(g.add("key2", 2, "item2", "key1"))
+
+ self.assertEqual(g["key2"], {1: "item1", 2: "item2"})
+
+ self.assertFalse(g.add("key3", 1, "item1", "key1"))
+ self.assertTrue(g.add("key3", 1, "item1", "key2"))
+ self.assertEqual(g.ordered_keys(), ["key3", "key2", "key1"])
+
+ self.assertTrue(g.add("key2", 1, "item1", "key3"))
+ self.assertRaises(CyclicDependency, g.ordered_keys)
+
+ def test_delete(self):
+ ## Second, test the usage of CollectedObjects by Model.delete()
+
+ # Due to the way that transactions work in the test harness, doing
+ # m.delete() here can work but fail in a real situation, since it may
+ # delete all objects, but not in the right order. So we manually check
+ # that the order of deletion is correct.
+
+ # Also, it is possible that the order is correct 'accidentally', due
+ # solely to order of imports etc. To check this, we set the order that
+ # 'get_models()' will retrieve to a known 'nice' order, and then try
+ # again with a known 'tricky' order. Slightly naughty access to
+ # internals here :-)
+
+ # If implementation changes, then the tests may need to be simplified:
+ # - remove the lines that set the .keyOrder and clear the related
+ # object caches
+ # - remove the second set of tests (with a2, b2 etc)
+
+ a1 = A.objects.create()
+ b1 = B.objects.create(a=a1)
+ c1 = C.objects.create(b=b1)
+ d1 = D.objects.create(c=c1, a=a1)
+
+ o = CollectedObjects()
+ a1._collect_sub_objects(o)
+ self.assertEqual(o.keys(), [D, C, B, A])
+ a1.delete()
+
+ # Same again with a known bad order
+ self.order_models("d", "c", "b", "a")
+ self.clear_rel_obj_caches(A, B, C, D)
+
+ a2 = A.objects.create()
+ b2 = B.objects.create(a=a2)
+ c2 = C.objects.create(b=b2)
+ d2 = D.objects.create(c=c2, a=a2)
+
+ o = CollectedObjects()
+ a2._collect_sub_objects(o)
+ self.assertEqual(o.keys(), [D, C, B, A])
+ a2.delete()
+
+ def test_collected_objects_null(self):
+ g = CollectedObjects()
+ self.assertFalse(g.add("key1", 1, "item1", None))
+ self.assertFalse(g.add("key2", 1, "item1", "key1", nullable=True))
+ self.assertTrue(g.add("key1", 1, "item1", "key2"))
+ self.assertEqual(g.ordered_keys(), ["key1", "key2"])
+
+ def test_delete_nullable(self):
+ e1 = E.objects.create()
+ f1 = F.objects.create(e=e1)
+ e1.f = f1
+ e1.save()
+
+ # Since E.f is nullable, we should delete F first (after nulling out
+ # the E.f field), then E.
+
+ o = CollectedObjects()
+ e1._collect_sub_objects(o)
+ self.assertEqual(o.keys(), [F, E])
+
+ # temporarily replace the UpdateQuery class to verify that E.f is
+ # actually nulled out first
+
+ logged = []
+ class LoggingUpdateQuery(sql.UpdateQuery):
+ def clear_related(self, related_field, pk_list, using):
+ logged.append(related_field.name)
+ return super(LoggingUpdateQuery, self).clear_related(related_field, pk_list, using)
+ original = sql.UpdateQuery
+ sql.UpdateQuery = LoggingUpdateQuery
+
+ e1.delete()
+ self.assertEqual(logged, ["f"])
+ logged = []
+
+ e2 = E.objects.create()
+ f2 = F.objects.create(e=e2)
+ e2.f = f2
+ e2.save()
+
+ # Same deal as before, though we are starting from the other object.
+ o = CollectedObjects()
+ f2._collect_sub_objects(o)
+ self.assertEqual(o.keys(), [F, E])
+ f2.delete()
+ self.assertEqual(logged, ["f"])
+ logged = []
+
+ sql.UpdateQuery = original
diff --git a/tests/modeltests/empty/models.py b/tests/modeltests/empty/models.py
index d57087134e..a6cdb0aa22 100644
--- a/tests/modeltests/empty/models.py
+++ b/tests/modeltests/empty/models.py
@@ -7,20 +7,6 @@ no fields.
from django.db import models
+
class Empty(models.Model):
pass
-
-__test__ = {'API_TESTS':"""
->>> m = Empty()
->>> m.id
->>> m.save()
->>> m2 = Empty()
->>> m2.save()
->>> len(Empty.objects.all())
-2
->>> m.id is not None
-True
->>> existing = Empty(m.id)
->>> existing.save()
-
-"""}
diff --git a/tests/modeltests/empty/tests.py b/tests/modeltests/empty/tests.py
new file mode 100644
index 0000000000..01fa1c58cd
--- /dev/null
+++ b/tests/modeltests/empty/tests.py
@@ -0,0 +1,15 @@
+from django.test import TestCase
+
+from models import Empty
+
+
+class EmptyModelTests(TestCase):
+ def test_empty(self):
+ m = Empty()
+ self.assertEqual(m.id, None)
+ m.save()
+ m2 = Empty.objects.create()
+ self.assertEqual(len(Empty.objects.all()), 2)
+ self.assertTrue(m.id is not None)
+ existing = Empty(m.id)
+ existing.save()
diff --git a/tests/modeltests/expressions/models.py b/tests/modeltests/expressions/models.py
index f6292f5d9b..b004408536 100644
--- a/tests/modeltests/expressions/models.py
+++ b/tests/modeltests/expressions/models.py
@@ -25,108 +25,3 @@ class Company(models.Model):
def __unicode__(self):
return self.name
-
-
-__test__ = {'API_TESTS': """
->>> from django.db.models import F
-
->>> Company(name='Example Inc.', num_employees=2300, num_chairs=5,
-... ceo=Employee.objects.create(firstname='Joe', lastname='Smith')).save()
->>> Company(name='Foobar Ltd.', num_employees=3, num_chairs=3,
-... ceo=Employee.objects.create(firstname='Frank', lastname='Meyer')).save()
->>> Company(name='Test GmbH', num_employees=32, num_chairs=1,
-... ceo=Employee.objects.create(firstname='Max', lastname='Mustermann')).save()
-
->>> company_query = Company.objects.values('name','num_employees','num_chairs').order_by('name','num_employees','num_chairs')
-
-# We can filter for companies where the number of employees is greater than the
-# number of chairs.
->>> company_query.filter(num_employees__gt=F('num_chairs'))
-[{'num_chairs': 5, 'name': u'Example Inc.', 'num_employees': 2300}, {'num_chairs': 1, 'name': u'Test GmbH', 'num_employees': 32}]
-
-# We can set one field to have the value of another field
-# Make sure we have enough chairs
->>> _ = company_query.update(num_chairs=F('num_employees'))
->>> company_query
-[{'num_chairs': 2300, 'name': u'Example Inc.', 'num_employees': 2300}, {'num_chairs': 3, 'name': u'Foobar Ltd.', 'num_employees': 3}, {'num_chairs': 32, 'name': u'Test GmbH', 'num_employees': 32}]
-
-# We can perform arithmetic operations in expressions
-# Make sure we have 2 spare chairs
->>> _ =company_query.update(num_chairs=F('num_employees')+2)
->>> company_query
-[{'num_chairs': 2302, 'name': u'Example Inc.', 'num_employees': 2300}, {'num_chairs': 5, 'name': u'Foobar Ltd.', 'num_employees': 3}, {'num_chairs': 34, 'name': u'Test GmbH', 'num_employees': 32}]
-
-# Law of order of operations is followed
->>> _ =company_query.update(num_chairs=F('num_employees') + 2 * F('num_employees'))
->>> company_query
-[{'num_chairs': 6900, 'name': u'Example Inc.', 'num_employees': 2300}, {'num_chairs': 9, 'name': u'Foobar Ltd.', 'num_employees': 3}, {'num_chairs': 96, 'name': u'Test GmbH', 'num_employees': 32}]
-
-# Law of order of operations can be overridden by parentheses
->>> _ =company_query.update(num_chairs=((F('num_employees') + 2) * F('num_employees')))
->>> company_query
-[{'num_chairs': 5294600, 'name': u'Example Inc.', 'num_employees': 2300}, {'num_chairs': 15, 'name': u'Foobar Ltd.', 'num_employees': 3}, {'num_chairs': 1088, 'name': u'Test GmbH', 'num_employees': 32}]
-
-# The relation of a foreign key can become copied over to an other foreign key.
->>> Company.objects.update(point_of_contact=F('ceo'))
-3
-
->>> [c.point_of_contact for c in Company.objects.all()]
-[<Employee: Joe Smith>, <Employee: Frank Meyer>, <Employee: Max Mustermann>]
-
->>> c = Company.objects.all()[0]
->>> c.point_of_contact = Employee.objects.create(firstname="Guido", lastname="van Rossum")
->>> c.save()
-
-# F Expressions can also span joins
->>> Company.objects.filter(ceo__firstname=F('point_of_contact__firstname')).distinct().order_by('name')
-[<Company: Foobar Ltd.>, <Company: Test GmbH>]
-
->>> _ = Company.objects.exclude(ceo__firstname=F('point_of_contact__firstname')).update(name='foo')
->>> Company.objects.exclude(ceo__firstname=F('point_of_contact__firstname')).get().name
-u'foo'
-
->>> _ = Company.objects.exclude(ceo__firstname=F('point_of_contact__firstname')).update(name=F('point_of_contact__lastname'))
-Traceback (most recent call last):
-...
-FieldError: Joined field references are not permitted in this query
-
-# F expressions can be used to update attributes on single objects
->>> test_gmbh = Company.objects.get(name='Test GmbH')
->>> test_gmbh.num_employees
-32
->>> test_gmbh.num_employees = F('num_employees') + 4
->>> test_gmbh.save()
->>> test_gmbh = Company.objects.get(pk=test_gmbh.pk)
->>> test_gmbh.num_employees
-36
-
-# F expressions cannot be used to update attributes which are foreign keys, or
-# attributes which involve joins.
->>> test_gmbh.point_of_contact = None
->>> test_gmbh.save()
->>> test_gmbh.point_of_contact is None
-True
->>> test_gmbh.point_of_contact = F('ceo')
-Traceback (most recent call last):
-...
-ValueError: Cannot assign "<django.db.models.expressions.F object at ...>": "Company.point_of_contact" must be a "Employee" instance.
-
->>> test_gmbh.point_of_contact = test_gmbh.ceo
->>> test_gmbh.save()
->>> test_gmbh.name = F('ceo__last_name')
->>> test_gmbh.save()
-Traceback (most recent call last):
-...
-FieldError: Joined field references are not permitted in this query
-
-# F expressions cannot be used to update attributes on objects which do not yet
-# exist in the database
->>> acme = Company(name='The Acme Widget Co.', num_employees=12, num_chairs=5,
-... ceo=test_gmbh.ceo)
->>> acme.num_employees = F('num_employees') + 16
->>> acme.save()
-Traceback (most recent call last):
-...
-TypeError: ...
-
-"""}
diff --git a/tests/modeltests/expressions/tests.py b/tests/modeltests/expressions/tests.py
new file mode 100644
index 0000000000..0a136ae5d8
--- /dev/null
+++ b/tests/modeltests/expressions/tests.py
@@ -0,0 +1,218 @@
+from django.core.exceptions import FieldError
+from django.db.models import F
+from django.test import TestCase
+
+from models import Company, Employee
+
+
+class ExpressionsTests(TestCase):
+ def test_filter(self):
+ Company.objects.create(
+ name="Example Inc.", num_employees=2300, num_chairs=5,
+ ceo=Employee.objects.create(firstname="Joe", lastname="Smith")
+ )
+ Company.objects.create(
+ name="Foobar Ltd.", num_employees=3, num_chairs=4,
+ ceo=Employee.objects.create(firstname="Frank", lastname="Meyer")
+ )
+ Company.objects.create(
+ name="Test GmbH", num_employees=32, num_chairs=1,
+ ceo=Employee.objects.create(firstname="Max", lastname="Mustermann")
+ )
+
+ company_query = Company.objects.values(
+ "name", "num_employees", "num_chairs"
+ ).order_by(
+ "name", "num_employees", "num_chairs"
+ )
+
+ # We can filter for companies where the number of employees is greater
+ # than the number of chairs.
+ self.assertQuerysetEqual(
+ company_query.filter(num_employees__gt=F("num_chairs")), [
+ {
+ "num_chairs": 5,
+ "name": "Example Inc.",
+ "num_employees": 2300,
+ },
+ {
+ "num_chairs": 1,
+ "name": "Test GmbH",
+ "num_employees": 32
+ },
+ ],
+ lambda o: o
+ )
+
+ # We can set one field to have the value of another field
+ # Make sure we have enough chairs
+ company_query.update(num_chairs=F("num_employees"))
+ self.assertQuerysetEqual(
+ company_query, [
+ {
+ "num_chairs": 2300,
+ "name": "Example Inc.",
+ "num_employees": 2300
+ },
+ {
+ "num_chairs": 3,
+ "name": "Foobar Ltd.",
+ "num_employees": 3
+ },
+ {
+ "num_chairs": 32,
+ "name": "Test GmbH",
+ "num_employees": 32
+ }
+ ],
+ lambda o: o
+ )
+
+ # We can perform arithmetic operations in expressions
+ # Make sure we have 2 spare chairs
+ company_query.update(num_chairs=F("num_employees")+2)
+ self.assertQuerysetEqual(
+ company_query, [
+ {
+ 'num_chairs': 2302,
+ 'name': u'Example Inc.',
+ 'num_employees': 2300
+ },
+ {
+ 'num_chairs': 5,
+ 'name': u'Foobar Ltd.',
+ 'num_employees': 3
+ },
+ {
+ 'num_chairs': 34,
+ 'name': u'Test GmbH',
+ 'num_employees': 32
+ }
+ ],
+ lambda o: o,
+ )
+
+ # Law of order of operations is followed
+ company_query.update(
+ num_chairs=F('num_employees') + 2 * F('num_employees')
+ )
+ self.assertQuerysetEqual(
+ company_query, [
+ {
+ 'num_chairs': 6900,
+ 'name': u'Example Inc.',
+ 'num_employees': 2300
+ },
+ {
+ 'num_chairs': 9,
+ 'name': u'Foobar Ltd.',
+ 'num_employees': 3
+ },
+ {
+ 'num_chairs': 96,
+ 'name': u'Test GmbH',
+ 'num_employees': 32
+ }
+ ],
+ lambda o: o,
+ )
+
+ # Law of order of operations can be overridden by parentheses
+ company_query.update(
+ num_chairs=((F('num_employees') + 2) * F('num_employees'))
+ )
+ self.assertQuerysetEqual(
+ company_query, [
+ {
+ 'num_chairs': 5294600,
+ 'name': u'Example Inc.',
+ 'num_employees': 2300
+ },
+ {
+ 'num_chairs': 15,
+ 'name': u'Foobar Ltd.',
+ 'num_employees': 3
+ },
+ {
+ 'num_chairs': 1088,
+ 'name': u'Test GmbH',
+ 'num_employees': 32
+ }
+ ],
+ lambda o: o,
+ )
+
+ # The relation of a foreign key can become copied over to an other
+ # foreign key.
+ self.assertEqual(
+ Company.objects.update(point_of_contact=F('ceo')),
+ 3
+ )
+ self.assertQuerysetEqual(
+ Company.objects.all(), [
+ "Joe Smith",
+ "Frank Meyer",
+ "Max Mustermann",
+ ],
+ lambda c: unicode(c.point_of_contact),
+ )
+
+ c = Company.objects.all()[0]
+ c.point_of_contact = Employee.objects.create(firstname="Guido", lastname="van Rossum")
+ c.save()
+
+ # F Expressions can also span joins
+ self.assertQuerysetEqual(
+ Company.objects.filter(ceo__firstname=F("point_of_contact__firstname")), [
+ "Foobar Ltd.",
+ "Test GmbH",
+ ],
+ lambda c: c.name
+ )
+
+ Company.objects.exclude(
+ ceo__firstname=F("point_of_contact__firstname")
+ ).update(name="foo")
+ self.assertEqual(
+ Company.objects.exclude(
+ ceo__firstname=F('point_of_contact__firstname')
+ ).get().name,
+ "foo",
+ )
+
+ self.assertRaises(FieldError,
+ lambda: Company.objects.exclude(
+ ceo__firstname=F('point_of_contact__firstname')
+ ).update(name=F('point_of_contact__lastname'))
+ )
+
+ # F expressions can be used to update attributes on single objects
+ test_gmbh = Company.objects.get(name="Test GmbH")
+ self.assertEqual(test_gmbh.num_employees, 32)
+ test_gmbh.num_employees = F("num_employees") + 4
+ test_gmbh.save()
+ test_gmbh = Company.objects.get(pk=test_gmbh.pk)
+ self.assertEqual(test_gmbh.num_employees, 36)
+
+ # F expressions cannot be used to update attributes which are foreign
+ # keys, or attributes which involve joins.
+ test_gmbh.point_of_contact = None
+ test_gmbh.save()
+ self.assertTrue(test_gmbh.point_of_contact is None)
+ def test():
+ test_gmbh.point_of_contact = F("ceo")
+ self.assertRaises(ValueError, test)
+
+ test_gmbh.point_of_contact = test_gmbh.ceo
+ test_gmbh.save()
+ test_gmbh.name = F("ceo__last_name")
+ self.assertRaises(FieldError, test_gmbh.save)
+
+ # F expressions cannot be used to update attributes on objects which do
+ # not yet exist in the database
+ acme = Company(
+ name="The Acme Widget Co.", num_employees=12, num_chairs=5,
+ ceo=test_gmbh.ceo
+ )
+ acme.num_employees = F("num_employees") + 16
+ self.assertRaises(TypeError, acme.save)
diff --git a/tests/modeltests/field_defaults/models.py b/tests/modeltests/field_defaults/models.py
index f258134147..0dd1f72934 100644
--- a/tests/modeltests/field_defaults/models.py
+++ b/tests/modeltests/field_defaults/models.py
@@ -19,41 +19,3 @@ class Article(models.Model):
def __unicode__(self):
return self.headline
-
-__test__ = {'API_TESTS': u"""
->>> from datetime import datetime
-
-# No articles are in the system yet.
->>> Article.objects.all()
-[]
-
-# Create an Article.
->>> a = Article(id=None)
-
-# Grab the current datetime it should be very close to the default that just
-# got saved as a.pub_date
->>> now = datetime.now()
-
-# Save it into the database. You have to call save() explicitly.
->>> a.save()
-
-# Now it has an ID. Note it's a long integer, as designated by the trailing "L".
->>> a.id
-1L
-
-# Access database columns via Python attributes.
->>> a.headline
-u'Default headline'
-
-# make sure the two dates are sufficiently close
->>> d = now - a.pub_date
->>> d.seconds < 5
-True
-
-# make sure that SafeString/SafeUnicode fields work
->>> from django.utils.safestring import SafeUnicode, SafeString
->>> a.headline = SafeUnicode(u'Iñtërnâtiônàlizætiøn1')
->>> a.save()
->>> a.headline = SafeString(u'Iñtërnâtiônàlizætiøn1'.encode('utf-8'))
->>> a.save()
-"""}
diff --git a/tests/modeltests/field_defaults/tests.py b/tests/modeltests/field_defaults/tests.py
new file mode 100644
index 0000000000..a23f64404a
--- /dev/null
+++ b/tests/modeltests/field_defaults/tests.py
@@ -0,0 +1,16 @@
+from datetime import datetime
+
+from django.test import TestCase
+
+from models import Article
+
+
+class DefaultTests(TestCase):
+ def test_field_defaults(self):
+ a = Article()
+ now = datetime.now()
+ a.save()
+
+ self.assertTrue(isinstance(a.id, (int, long)))
+ self.assertEqual(a.headline, "Default headline")
+ self.assertTrue((now - a.pub_date).seconds < 5)
diff --git a/tests/modeltests/field_subclassing/fields.py b/tests/modeltests/field_subclassing/fields.py
index a43714dcf5..1f9bdf5e0a 100644
--- a/tests/modeltests/field_subclassing/fields.py
+++ b/tests/modeltests/field_subclassing/fields.py
@@ -53,18 +53,18 @@ class SmallField(models.Field):
class JSONField(models.TextField):
__metaclass__ = models.SubfieldBase
-
+
description = ("JSONField automatically serializes and desializes values to "
"and from JSON.")
-
+
def to_python(self, value):
if not value:
return None
-
+
if isinstance(value, basestring):
value = json.loads(value)
return value
-
+
def get_db_prep_save(self, value):
if value is None:
return None
diff --git a/tests/modeltests/field_subclassing/models.py b/tests/modeltests/field_subclassing/models.py
index a9fe88fe77..4a55b72961 100644
--- a/tests/modeltests/field_subclassing/models.py
+++ b/tests/modeltests/field_subclassing/models.py
@@ -2,7 +2,6 @@
Tests for field subclassing.
"""
-from django.core import serializers
from django.db import models
from django.utils.encoding import force_unicode
@@ -18,56 +17,3 @@ class MyModel(models.Model):
class DataModel(models.Model):
data = JSONField()
-
-__test__ = {'API_TESTS': ur"""
-# Creating a model with custom fields is done as per normal.
->>> s = Small(1, 2)
->>> print s
-12
->>> m = MyModel(name='m', data=s)
->>> m.save()
-
-# Custom fields still have normal field's attributes.
->>> m._meta.get_field('data').verbose_name
-'small field'
-
-# The m.data attribute has been initialised correctly. It's a Small object.
->>> m.data.first, m.data.second
-(1, 2)
-
-# The data loads back from the database correctly and 'data' has the right type.
->>> m1 = MyModel.objects.get(pk=m.pk)
->>> isinstance(m1.data, Small)
-True
->>> print m1.data
-12
-
-# We can do normal filtering on the custom field (and will get an error when we
-# use a lookup type that does not make sense).
->>> s1 = Small(1, 3)
->>> s2 = Small('a', 'b')
->>> MyModel.objects.filter(data__in=[s, s1, s2])
-[<MyModel: m>]
->>> MyModel.objects.filter(data__lt=s)
-Traceback (most recent call last):
-...
-TypeError: Invalid lookup type: 'lt'
-
-# Serialization works, too.
->>> stream = serializers.serialize("json", MyModel.objects.all())
->>> stream
-'[{"pk": 1, "model": "field_subclassing.mymodel", "fields": {"data": "12", "name": "m"}}]'
->>> obj = list(serializers.deserialize("json", stream))[0]
->>> obj.object == m
-True
-
-# Test retrieving custom field data
->>> m.delete()
->>> m1 = MyModel(name="1", data=Small(1, 2))
->>> m1.save()
->>> m2 = MyModel(name="2", data=Small(2, 3))
->>> m2.save()
->>> for m in MyModel.objects.all(): print unicode(m.data)
-12
-23
-"""}
diff --git a/tests/modeltests/field_subclassing/tests.py b/tests/modeltests/field_subclassing/tests.py
index 731ab51d24..ba7148a654 100644
--- a/tests/modeltests/field_subclassing/tests.py
+++ b/tests/modeltests/field_subclassing/tests.py
@@ -1,21 +1,75 @@
+from django.core import serializers
from django.test import TestCase
-from models import DataModel
+from fields import Small
+from models import DataModel, MyModel
class CustomField(TestCase):
def test_defer(self):
d = DataModel.objects.create(data=[1, 2, 3])
-
+
self.assertTrue(isinstance(d.data, list))
-
+
d = DataModel.objects.get(pk=d.pk)
self.assertTrue(isinstance(d.data, list))
self.assertEqual(d.data, [1, 2, 3])
-
+
d = DataModel.objects.defer("data").get(pk=d.pk)
d.save()
-
+
d = DataModel.objects.get(pk=d.pk)
self.assertTrue(isinstance(d.data, list))
self.assertEqual(d.data, [1, 2, 3])
+
+ def test_custom_field(self):
+ # Creating a model with custom fields is done as per normal.
+ s = Small(1, 2)
+ self.assertEqual(str(s), "12")
+
+ m = MyModel.objects.create(name="m", data=s)
+ # Custom fields still have normal field's attributes.
+ self.assertEqual(m._meta.get_field("data").verbose_name, "small field")
+
+ # The m.data attribute has been initialised correctly. It's a Small
+ # object.
+ self.assertEqual((m.data.first, m.data.second), (1, 2))
+
+ # The data loads back from the database correctly and 'data' has the
+ # right type.
+ m1 = MyModel.objects.get(pk=m.pk)
+ self.assertTrue(isinstance(m1.data, Small))
+ self.assertEqual(str(m1.data), "12")
+
+ # We can do normal filtering on the custom field (and will get an error
+ # when we use a lookup type that does not make sense).
+ s1 = Small(1, 3)
+ s2 = Small("a", "b")
+ self.assertQuerysetEqual(
+ MyModel.objects.filter(data__in=[s, s1, s2]), [
+ "m",
+ ],
+ lambda m: m.name,
+ )
+ self.assertRaises(TypeError, lambda: MyModel.objects.filter(data__lt=s))
+
+ # Serialization works, too.
+ stream = serializers.serialize("json", MyModel.objects.all())
+ self.assertEqual(stream, '[{"pk": 1, "model": "field_subclassing.mymodel", "fields": {"data": "12", "name": "m"}}]')
+
+ obj = list(serializers.deserialize("json", stream))[0]
+ self.assertEqual(obj.object, m)
+
+ # Test retrieving custom field data
+ m.delete()
+
+ m1 = MyModel.objects.create(name="1", data=Small(1, 2))
+ m2 = MyModel.objects.create(name="2", data=Small(2, 3))
+
+ self.assertQuerysetEqual(
+ MyModel.objects.all(), [
+ "12",
+ "23",
+ ],
+ lambda m: str(m.data)
+ )
diff --git a/tests/modeltests/files/models.py b/tests/modeltests/files/models.py
index 67c27b54b5..f798f74df9 100644
--- a/tests/modeltests/files/models.py
+++ b/tests/modeltests/files/models.py
@@ -7,10 +7,12 @@ and where files should be stored.
import random
import tempfile
+
from django.db import models
from django.core.files.base import ContentFile
from django.core.files.storage import FileSystemStorage
+
temp_storage_location = tempfile.mkdtemp()
temp_storage = FileSystemStorage(location=temp_storage_location)
@@ -30,125 +32,3 @@ class Storage(models.Model):
custom = models.FileField(storage=temp_storage, upload_to=custom_upload_to)
random = models.FileField(storage=temp_storage, upload_to=random_upload_to)
default = models.FileField(storage=temp_storage, upload_to='tests', default='tests/default.txt')
-
-__test__ = {'API_TESTS':"""
-# Attempting to access a FileField from the class raises a descriptive error
->>> Storage.normal
-Traceback (most recent call last):
-...
-AttributeError: The 'normal' attribute can only be accessed from Storage instances.
-
-# An object without a file has limited functionality.
-
->>> obj1 = Storage()
->>> obj1.normal
-<FieldFile: None>
->>> obj1.normal.size
-Traceback (most recent call last):
-...
-ValueError: The 'normal' attribute has no file associated with it.
-
-# Saving a file enables full functionality.
-
->>> obj1.normal.save('django_test.txt', ContentFile('content'))
->>> obj1.normal
-<FieldFile: tests/django_test.txt>
->>> obj1.normal.size
-7
->>> obj1.normal.read()
-'content'
-
-# File objects can be assigned to FileField attributes, but shouldn't get
-# committed until the model it's attached to is saved.
-
->>> from django.core.files.uploadedfile import SimpleUploadedFile
->>> obj1.normal = SimpleUploadedFile('assignment.txt', 'content')
->>> dirs, files = temp_storage.listdir('tests')
->>> dirs
-[]
->>> files.sort()
->>> files == ['default.txt', 'django_test.txt']
-True
-
->>> obj1.save()
->>> dirs, files = temp_storage.listdir('tests')
->>> files.sort()
->>> files == ['assignment.txt', 'default.txt', 'django_test.txt']
-True
-
-# Files can be read in a little at a time, if necessary.
-
->>> obj1.normal.open()
->>> obj1.normal.read(3)
-'con'
->>> obj1.normal.read()
-'tent'
->>> '-'.join(obj1.normal.chunks(chunk_size=2))
-'co-nt-en-t'
-
-# Save another file with the same name.
-
->>> obj2 = Storage()
->>> obj2.normal.save('django_test.txt', ContentFile('more content'))
->>> obj2.normal
-<FieldFile: tests/django_test_1.txt>
->>> obj2.normal.size
-12
-
-# Push the objects into the cache to make sure they pickle properly
-
->>> from django.core.cache import cache
->>> cache.set('obj1', obj1)
->>> cache.set('obj2', obj2)
->>> cache.get('obj2').normal
-<FieldFile: tests/django_test_1.txt>
-
-# Deleting an object deletes the file it uses, if there are no other objects
-# still using that file.
-
->>> obj2.delete()
->>> obj2.normal.save('django_test.txt', ContentFile('more content'))
->>> obj2.normal
-<FieldFile: tests/django_test_1.txt>
-
-# Multiple files with the same name get _N appended to them.
-
->>> objs = [Storage() for i in range(3)]
->>> for o in objs:
-... o.normal.save('multiple_files.txt', ContentFile('Same Content'))
->>> [o.normal for o in objs]
-[<FieldFile: tests/multiple_files.txt>, <FieldFile: tests/multiple_files_1.txt>, <FieldFile: tests/multiple_files_2.txt>]
->>> for o in objs:
-... o.delete()
-
-# Default values allow an object to access a single file.
-
->>> obj3 = Storage.objects.create()
->>> obj3.default
-<FieldFile: tests/default.txt>
->>> obj3.default.read()
-'default content'
-
-# But it shouldn't be deleted, even if there are no more objects using it.
-
->>> obj3.delete()
->>> obj3 = Storage()
->>> obj3.default.read()
-'default content'
-
-# Verify the fix for #5655, making sure the directory is only determined once.
-
->>> obj4 = Storage()
->>> obj4.random.save('random_file', ContentFile('random content'))
->>> obj4.random
-<FieldFile: .../random_file>
-
-# Clean up the temporary files and dir.
->>> obj1.normal.delete()
->>> obj2.normal.delete()
->>> obj3.default.delete()
->>> obj4.random.delete()
-
->>> import shutil
->>> shutil.rmtree(temp_storage_location)
-"""}
diff --git a/tests/modeltests/files/tests.py b/tests/modeltests/files/tests.py
new file mode 100644
index 0000000000..025fcc574a
--- /dev/null
+++ b/tests/modeltests/files/tests.py
@@ -0,0 +1,100 @@
+import shutil
+
+from django.core.cache import cache
+from django.core.files.base import ContentFile
+from django.core.files.uploadedfile import SimpleUploadedFile
+from django.test import TestCase
+
+from models import Storage, temp_storage, temp_storage_location
+
+
+class FileTests(TestCase):
+ def tearDown(self):
+ shutil.rmtree(temp_storage_location)
+
+ def test_files(self):
+ # Attempting to access a FileField from the class raises a descriptive
+ # error
+ self.assertRaises(AttributeError, lambda: Storage.normal)
+
+ # An object without a file has limited functionality.
+ obj1 = Storage()
+ self.assertEqual(obj1.normal.name, "")
+ self.assertRaises(ValueError, lambda: obj1.normal.size)
+
+ # Saving a file enables full functionality.
+ obj1.normal.save("django_test.txt", ContentFile("content"))
+ self.assertEqual(obj1.normal.name, "tests/django_test.txt")
+ self.assertEqual(obj1.normal.size, 7)
+ self.assertEqual(obj1.normal.read(), "content")
+
+ # File objects can be assigned to FileField attributes, but shouldn't
+ # get committed until the model it's attached to is saved.
+ obj1.normal = SimpleUploadedFile("assignment.txt", "content")
+ dirs, files = temp_storage.listdir("tests")
+ self.assertEqual(dirs, [])
+ self.assertEqual(sorted(files), ["default.txt", "django_test.txt"])
+
+ obj1.save()
+ dirs, files = temp_storage.listdir("tests")
+ self.assertEqual(
+ sorted(files), ["assignment.txt", "default.txt", "django_test.txt"]
+ )
+
+ # Files can be read in a little at a time, if necessary.
+ obj1.normal.open()
+ self.assertEqual(obj1.normal.read(3), "con")
+ self.assertEqual(obj1.normal.read(), "tent")
+ self.assertEqual(list(obj1.normal.chunks(chunk_size=2)), ["co", "nt", "en", "t"])
+
+ # Save another file with the same name.
+ obj2 = Storage()
+ obj2.normal.save("django_test.txt", ContentFile("more content"))
+ self.assertEqual(obj2.normal.name, "tests/django_test_1.txt")
+ self.assertEqual(obj2.normal.size, 12)
+
+ # Push the objects into the cache to make sure they pickle properly
+ cache.set("obj1", obj1)
+ cache.set("obj2", obj2)
+ self.assertEqual(cache.get("obj2").normal.name, "tests/django_test_1.txt")
+
+ # Deleting an object deletes the file it uses, if there are no other
+ # objects still using that file.
+ obj2.delete()
+ obj2.normal.save("django_test.txt", ContentFile("more content"))
+ self.assertEqual(obj2.normal.name, "tests/django_test_1.txt")
+
+ # Multiple files with the same name get _N appended to them.
+ objs = [Storage() for i in range(3)]
+ for o in objs:
+ o.normal.save("multiple_files.txt", ContentFile("Same Content"))
+ self.assertEqual(
+ [o.normal.name for o in objs],
+ ["tests/multiple_files.txt", "tests/multiple_files_1.txt", "tests/multiple_files_2.txt"]
+ )
+ for o in objs:
+ o.delete()
+
+ # Default values allow an object to access a single file.
+ obj3 = Storage.objects.create()
+ self.assertEqual(obj3.default.name, "tests/default.txt")
+ self.assertEqual(obj3.default.read(), "default content")
+
+ # But it shouldn't be deleted, even if there are no more objects using
+ # it.
+ obj3.delete()
+ obj3 = Storage()
+ self.assertEqual(obj3.default.read(), "default content")
+
+ # Verify the fix for #5655, making sure the directory is only
+ # determined once.
+ obj4 = Storage()
+ obj4.random.save("random_file", ContentFile("random content"))
+ self.assertTrue(obj4.random.name.endswith("/random_file"))
+
+ # Clean up the temporary files and dir.
+ obj1.normal.delete()
+ obj2.normal.delete()
+ obj3.default.delete()
+ obj4.random.delete()
+
diff --git a/tests/modeltests/fixtures/models.py b/tests/modeltests/fixtures/models.py
index 46e07a5e6b..0035bcc7e0 100644
--- a/tests/modeltests/fixtures/models.py
+++ b/tests/modeltests/fixtures/models.py
@@ -72,6 +72,14 @@ class Person(models.Model):
def natural_key(self):
return (self.name,)
+class SpyManager(PersonManager):
+ def get_query_set(self):
+ return super(SpyManager, self).get_query_set().filter(cover_blown=False)
+
+class Spy(Person):
+ objects = SpyManager()
+ cover_blown = models.BooleanField(default=False)
+
class Visa(models.Model):
person = models.ForeignKey(Person)
permissions = models.ManyToManyField(Permission, blank=True)
@@ -90,230 +98,3 @@ class Book(models.Model):
class Meta:
ordering = ('name',)
-
-__test__ = {'API_TESTS': """
->>> from django.core import management
->>> from django.db.models import get_app
-
-# Reset the database representation of this app.
-# This will return the database to a clean initial state.
->>> management.call_command('flush', verbosity=0, interactive=False)
-
-# Syncdb introduces 1 initial data object from initial_data.json.
->>> Article.objects.all()
-[<Article: Python program becomes self aware>]
-
-# Load fixture 1. Single JSON file, with two objects.
->>> management.call_command('loaddata', 'fixture1.json', verbosity=0)
->>> Article.objects.all()
-[<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
-
-# Dump the current contents of the database as a JSON fixture
->>> management.call_command('dumpdata', 'fixtures', format='json')
-[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]
-
-# Try just dumping the contents of fixtures.Category
->>> management.call_command('dumpdata', 'fixtures.Category', format='json')
-[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}]
-
-# ...and just fixtures.Article
->>> management.call_command('dumpdata', 'fixtures.Article', format='json')
-[{"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]
-
-# ...and both
->>> management.call_command('dumpdata', 'fixtures.Category', 'fixtures.Article', format='json')
-[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]
-
-# Specify a specific model twice
->>> management.call_command('dumpdata', 'fixtures.Article', 'fixtures.Article', format='json')
-[{"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]
-
-# Specify a dump that specifies Article both explicitly and implicitly
->>> management.call_command('dumpdata', 'fixtures.Article', 'fixtures', format='json')
-[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]
-
-# Same again, but specify in the reverse order
->>> management.call_command('dumpdata', 'fixtures', 'fixtures.Article', format='json')
-[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]
-
-# Specify one model from one application, and an entire other application.
->>> management.call_command('dumpdata', 'fixtures.Category', 'sites', format='json')
-[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 1, "model": "sites.site", "fields": {"domain": "example.com", "name": "example.com"}}]
-
-# Load fixture 2. JSON file imported by default. Overwrites some existing objects
->>> management.call_command('loaddata', 'fixture2.json', verbosity=0)
->>> Article.objects.all()
-[<Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
-
-# Load fixture 3, XML format.
->>> management.call_command('loaddata', 'fixture3.xml', verbosity=0)
->>> Article.objects.all()
-[<Article: XML identified as leading cause of cancer>, <Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker on TV is great!>, <Article: Python program becomes self aware>]
-
-# Load fixture 6, JSON file with dynamic ContentType fields. Testing ManyToOne.
->>> management.call_command('loaddata', 'fixture6.json', verbosity=0)
->>> Tag.objects.all()
-[<Tag: <Article: Copyright is fine the way it is> tagged "copyright">, <Tag: <Article: Copyright is fine the way it is> tagged "law">]
-
-# Load fixture 7, XML file with dynamic ContentType fields. Testing ManyToOne.
->>> management.call_command('loaddata', 'fixture7.xml', verbosity=0)
->>> Tag.objects.all()
-[<Tag: <Article: Copyright is fine the way it is> tagged "copyright">, <Tag: <Article: Copyright is fine the way it is> tagged "legal">, <Tag: <Article: Django conquers world!> tagged "django">, <Tag: <Article: Django conquers world!> tagged "world domination">]
-
-# Load fixture 8, JSON file with dynamic Permission fields. Testing ManyToMany.
->>> management.call_command('loaddata', 'fixture8.json', verbosity=0)
->>> Visa.objects.all()
-[<Visa: Django Reinhardt Can add user, Can change user, Can delete user>, <Visa: Stephane Grappelli Can add user>, <Visa: Prince >]
-
-# Load fixture 9, XML file with dynamic Permission fields. Testing ManyToMany.
->>> management.call_command('loaddata', 'fixture9.xml', verbosity=0)
->>> Visa.objects.all()
-[<Visa: Django Reinhardt Can add user, Can change user, Can delete user>, <Visa: Stephane Grappelli Can add user, Can delete user>, <Visa: Artist formerly known as "Prince" Can change user>]
-
->>> Book.objects.all()
-[<Book: Music for all ages by Artist formerly known as "Prince" and Django Reinhardt>]
-
-# Load a fixture that doesn't exist
->>> management.call_command('loaddata', 'unknown.json', verbosity=0)
-
-# object list is unaffected
->>> Article.objects.all()
-[<Article: XML identified as leading cause of cancer>, <Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker on TV is great!>, <Article: Python program becomes self aware>]
-
-# By default, you get raw keys on dumpdata
->>> management.call_command('dumpdata', 'fixtures.book', format='json')
-[{"pk": 1, "model": "fixtures.book", "fields": {"name": "Music for all ages", "authors": [3, 1]}}]
-
-# But you can get natural keys if you ask for them and they are available
->>> management.call_command('dumpdata', 'fixtures.book', format='json', use_natural_keys=True)
-[{"pk": 1, "model": "fixtures.book", "fields": {"name": "Music for all ages", "authors": [["Artist formerly known as \\"Prince\\""], ["Django Reinhardt"]]}}]
-
-# Dump the current contents of the database as a JSON fixture
->>> management.call_command('dumpdata', 'fixtures', format='json', use_natural_keys=True)
-[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 5, "model": "fixtures.article", "fields": {"headline": "XML identified as leading cause of cancer", "pub_date": "2006-06-16 16:00:00"}}, {"pk": 4, "model": "fixtures.article", "fields": {"headline": "Django conquers world!", "pub_date": "2006-06-16 15:00:00"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Copyright is fine the way it is", "pub_date": "2006-06-16 14:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker on TV is great!", "pub_date": "2006-06-16 11:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}, {"pk": 1, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "copyright", "tagged_id": 3}}, {"pk": 2, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "legal", "tagged_id": 3}}, {"pk": 3, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "django", "tagged_id": 4}}, {"pk": 4, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "world domination", "tagged_id": 4}}, {"pk": 3, "model": "fixtures.person", "fields": {"name": "Artist formerly known as \\"Prince\\""}}, {"pk": 1, "model": "fixtures.person", "fields": {"name": "Django Reinhardt"}}, {"pk": 2, "model": "fixtures.person", "fields": {"name": "Stephane Grappelli"}}, {"pk": 1, "model": "fixtures.visa", "fields": {"person": ["Django Reinhardt"], "permissions": [["add_user", "auth", "user"], ["change_user", "auth", "user"], ["delete_user", "auth", "user"]]}}, {"pk": 2, "model": "fixtures.visa", "fields": {"person": ["Stephane Grappelli"], "permissions": [["add_user", "auth", "user"], ["delete_user", "auth", "user"]]}}, {"pk": 3, "model": "fixtures.visa", "fields": {"person": ["Artist formerly known as \\"Prince\\""], "permissions": [["change_user", "auth", "user"]]}}, {"pk": 1, "model": "fixtures.book", "fields": {"name": "Music for all ages", "authors": [["Artist formerly known as \\"Prince\\""], ["Django Reinhardt"]]}}]
-
-# Dump the current contents of the database as an XML fixture
->>> management.call_command('dumpdata', 'fixtures', format='xml', use_natural_keys=True)
-<?xml version="1.0" encoding="utf-8"?>
-<django-objects version="1.0"><object pk="1" model="fixtures.category"><field type="CharField" name="title">News Stories</field><field type="TextField" name="description">Latest news stories</field></object><object pk="5" model="fixtures.article"><field type="CharField" name="headline">XML identified as leading cause of cancer</field><field type="DateTimeField" name="pub_date">2006-06-16 16:00:00</field></object><object pk="4" model="fixtures.article"><field type="CharField" name="headline">Django conquers world!</field><field type="DateTimeField" name="pub_date">2006-06-16 15:00:00</field></object><object pk="3" model="fixtures.article"><field type="CharField" name="headline">Copyright is fine the way it is</field><field type="DateTimeField" name="pub_date">2006-06-16 14:00:00</field></object><object pk="2" model="fixtures.article"><field type="CharField" name="headline">Poker on TV is great!</field><field type="DateTimeField" name="pub_date">2006-06-16 11:00:00</field></object><object pk="1" model="fixtures.article"><field type="CharField" name="headline">Python program becomes self aware</field><field type="DateTimeField" name="pub_date">2006-06-16 11:00:00</field></object><object pk="1" model="fixtures.tag"><field type="CharField" name="name">copyright</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="2" model="fixtures.tag"><field type="CharField" name="name">legal</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="3" model="fixtures.tag"><field type="CharField" name="name">django</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">4</field></object><object pk="4" model="fixtures.tag"><field type="CharField" name="name">world domination</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">4</field></object><object pk="3" model="fixtures.person"><field type="CharField" name="name">Artist formerly known as "Prince"</field></object><object pk="1" model="fixtures.person"><field type="CharField" name="name">Django Reinhardt</field></object><object pk="2" model="fixtures.person"><field type="CharField" name="name">Stephane Grappelli</field></object><object pk="1" model="fixtures.visa"><field to="fixtures.person" name="person" rel="ManyToOneRel"><natural>Django Reinhardt</natural></field><field to="auth.permission" name="permissions" rel="ManyToManyRel"><object><natural>add_user</natural><natural>auth</natural><natural>user</natural></object><object><natural>change_user</natural><natural>auth</natural><natural>user</natural></object><object><natural>delete_user</natural><natural>auth</natural><natural>user</natural></object></field></object><object pk="2" model="fixtures.visa"><field to="fixtures.person" name="person" rel="ManyToOneRel"><natural>Stephane Grappelli</natural></field><field to="auth.permission" name="permissions" rel="ManyToManyRel"><object><natural>add_user</natural><natural>auth</natural><natural>user</natural></object><object><natural>delete_user</natural><natural>auth</natural><natural>user</natural></object></field></object><object pk="3" model="fixtures.visa"><field to="fixtures.person" name="person" rel="ManyToOneRel"><natural>Artist formerly known as "Prince"</natural></field><field to="auth.permission" name="permissions" rel="ManyToManyRel"><object><natural>change_user</natural><natural>auth</natural><natural>user</natural></object></field></object><object pk="1" model="fixtures.book"><field type="CharField" name="name">Music for all ages</field><field to="fixtures.person" name="authors" rel="ManyToManyRel"><object><natural>Artist formerly known as "Prince"</natural></object><object><natural>Django Reinhardt</natural></object></field></object></django-objects>
-
-"""}
-
-# Database flushing does not work on MySQL with the default storage engine
-# because it requires transaction support.
-if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.mysql':
- __test__['API_TESTS'] += \
-"""
-# Reset the database representation of this app. This will delete all data.
->>> management.call_command('flush', verbosity=0, interactive=False)
->>> Article.objects.all()
-[<Article: Python program becomes self aware>]
-
-# Load fixture 1 again, using format discovery
->>> management.call_command('loaddata', 'fixture1', verbosity=0)
->>> Article.objects.all()
-[<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
-
-# Try to load fixture 2 using format discovery; this will fail
-# because there are two fixture2's in the fixtures directory
->>> management.call_command('loaddata', 'fixture2', verbosity=0) # doctest: +ELLIPSIS
-Multiple fixtures named 'fixture2' in '...fixtures'. Aborting.
-
-# object list is unaffected
->>> Article.objects.all()
-[<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
-
-# Dump the current contents of the database as a JSON fixture
->>> management.call_command('dumpdata', 'fixtures', format='json')
-[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]
-
-# Load fixture 4 (compressed), using format discovery
->>> management.call_command('loaddata', 'fixture4', verbosity=0)
->>> Article.objects.all()
-[<Article: Django pets kitten>, <Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
-
->>> management.call_command('flush', verbosity=0, interactive=False)
-
-# Load fixture 4 (compressed), using format specification
->>> management.call_command('loaddata', 'fixture4.json', verbosity=0)
->>> Article.objects.all()
-[<Article: Django pets kitten>, <Article: Python program becomes self aware>]
-
->>> management.call_command('flush', verbosity=0, interactive=False)
-
-# Load fixture 5 (compressed), using format *and* compression specification
->>> management.call_command('loaddata', 'fixture5.json.zip', verbosity=0)
->>> Article.objects.all()
-[<Article: WoW subscribers now outnumber readers>, <Article: Python program becomes self aware>]
-
->>> management.call_command('flush', verbosity=0, interactive=False)
-
-# Load fixture 5 (compressed), only compression specification
->>> management.call_command('loaddata', 'fixture5.zip', verbosity=0)
->>> Article.objects.all()
-[<Article: WoW subscribers now outnumber readers>, <Article: Python program becomes self aware>]
-
->>> management.call_command('flush', verbosity=0, interactive=False)
-
-# Try to load fixture 5 using format and compression discovery; this will fail
-# because there are two fixture5's in the fixtures directory
->>> management.call_command('loaddata', 'fixture5', verbosity=0) # doctest: +ELLIPSIS
-Multiple fixtures named 'fixture5' in '...fixtures'. Aborting.
-
->>> management.call_command('flush', verbosity=0, interactive=False)
-
-# Load db fixtures 1 and 2. These will load using the 'default' database identifier implicitly
->>> management.call_command('loaddata', 'db_fixture_1', verbosity=0)
->>> management.call_command('loaddata', 'db_fixture_2', verbosity=0)
->>> Article.objects.all()
-[<Article: Who needs more than one database?>, <Article: Who needs to use compressed data?>, <Article: Python program becomes self aware>]
-
->>> management.call_command('flush', verbosity=0, interactive=False)
-
-# Load db fixtures 1 and 2. These will load using the 'default' database identifier explicitly
->>> management.call_command('loaddata', 'db_fixture_1', verbosity=0, using='default')
->>> management.call_command('loaddata', 'db_fixture_2', verbosity=0, using='default')
->>> Article.objects.all()
-[<Article: Who needs more than one database?>, <Article: Who needs to use compressed data?>, <Article: Python program becomes self aware>]
-
->>> management.call_command('flush', verbosity=0, interactive=False)
-
-# Try to load db fixture 3. This won't load because the database identifier doesn't match
->>> management.call_command('loaddata', 'db_fixture_3', verbosity=0)
->>> Article.objects.all()
-[<Article: Python program becomes self aware>]
-
->>> management.call_command('loaddata', 'db_fixture_3', verbosity=0, using='default')
->>> Article.objects.all()
-[<Article: Python program becomes self aware>]
-
->>> management.call_command('flush', verbosity=0, interactive=False)
-
-# Load back in fixture 1, we need the articles from it
->>> management.call_command('loaddata', 'fixture1', verbosity=0)
-
-# Try to load fixture 6 using format discovery
->>> management.call_command('loaddata', 'fixture6', verbosity=0)
->>> Tag.objects.all()
-[<Tag: <Article: Time to reform copyright> tagged "copyright">, <Tag: <Article: Time to reform copyright> tagged "law">]
-
-# Dump the current contents of the database as a JSON fixture
->>> management.call_command('dumpdata', 'fixtures', format='json', use_natural_keys=True)
-[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}, {"pk": 1, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "copyright", "tagged_id": 3}}, {"pk": 2, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "law", "tagged_id": 3}}, {"pk": 1, "model": "fixtures.person", "fields": {"name": "Django Reinhardt"}}, {"pk": 3, "model": "fixtures.person", "fields": {"name": "Prince"}}, {"pk": 2, "model": "fixtures.person", "fields": {"name": "Stephane Grappelli"}}]
-
-# Dump the current contents of the database as an XML fixture
->>> management.call_command('dumpdata', 'fixtures', format='xml', use_natural_keys=True)
-<?xml version="1.0" encoding="utf-8"?>
-<django-objects version="1.0"><object pk="1" model="fixtures.category"><field type="CharField" name="title">News Stories</field><field type="TextField" name="description">Latest news stories</field></object><object pk="3" model="fixtures.article"><field type="CharField" name="headline">Time to reform copyright</field><field type="DateTimeField" name="pub_date">2006-06-16 13:00:00</field></object><object pk="2" model="fixtures.article"><field type="CharField" name="headline">Poker has no place on ESPN</field><field type="DateTimeField" name="pub_date">2006-06-16 12:00:00</field></object><object pk="1" model="fixtures.article"><field type="CharField" name="headline">Python program becomes self aware</field><field type="DateTimeField" name="pub_date">2006-06-16 11:00:00</field></object><object pk="1" model="fixtures.tag"><field type="CharField" name="name">copyright</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="2" model="fixtures.tag"><field type="CharField" name="name">law</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="1" model="fixtures.person"><field type="CharField" name="name">Django Reinhardt</field></object><object pk="3" model="fixtures.person"><field type="CharField" name="name">Prince</field></object><object pk="2" model="fixtures.person"><field type="CharField" name="name">Stephane Grappelli</field></object></django-objects>
-
-"""
-
-from django.test import TestCase
-
-class SampleTestCase(TestCase):
- fixtures = ['fixture1.json', 'fixture2.json']
-
- def testClassFixtures(self):
- "Check that test case has installed 4 fixture objects"
- self.assertEqual(Article.objects.count(), 4)
- self.assertEquals(str(Article.objects.all()), "[<Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]")
diff --git a/tests/modeltests/fixtures/tests.py b/tests/modeltests/fixtures/tests.py
new file mode 100644
index 0000000000..799a7328da
--- /dev/null
+++ b/tests/modeltests/fixtures/tests.py
@@ -0,0 +1,336 @@
+import StringIO
+import sys
+
+from django.test import TestCase, TransactionTestCase
+from django.conf import settings
+from django.core import management
+from django.db import DEFAULT_DB_ALIAS
+
+from models import Article, Blog, Book, Category, Person, Spy, Tag, Visa
+
+class TestCaseFixtureLoadingTests(TestCase):
+ fixtures = ['fixture1.json', 'fixture2.json']
+
+ def testClassFixtures(self):
+ "Check that test case has installed 4 fixture objects"
+ self.assertEqual(Article.objects.count(), 4)
+ self.assertQuerysetEqual(Article.objects.all(), [
+ '<Article: Django conquers world!>',
+ '<Article: Copyright is fine the way it is>',
+ '<Article: Poker has no place on ESPN>',
+ '<Article: Python program becomes self aware>'
+ ])
+
+class FixtureLoadingTests(TestCase):
+
+ def _dumpdata_assert(self, args, output, format='json', natural_keys=False,
+ use_base_manager=False, exclude_list=[]):
+ new_io = StringIO.StringIO()
+ management.call_command('dumpdata', *args, **{'format':format,
+ 'stdout':new_io,
+ 'stderr':new_io,
+ 'use_natural_keys':natural_keys,
+ 'use_base_manager':use_base_manager,
+ 'exclude': exclude_list})
+ command_output = new_io.getvalue().strip()
+ self.assertEqual(command_output, output)
+
+ def test_initial_data(self):
+ # Syncdb introduces 1 initial data object from initial_data.json.
+ self.assertQuerysetEqual(Article.objects.all(), [
+ '<Article: Python program becomes self aware>'
+ ])
+
+ def test_loading_and_dumping(self):
+ new_io = StringIO.StringIO()
+
+ # Load fixture 1. Single JSON file, with two objects.
+ management.call_command('loaddata', 'fixture1.json', verbosity=0, commit=False)
+ self.assertQuerysetEqual(Article.objects.all(), [
+ '<Article: Time to reform copyright>',
+ '<Article: Poker has no place on ESPN>',
+ '<Article: Python program becomes self aware>'
+ ])
+
+ # Dump the current contents of the database as a JSON fixture
+ self._dumpdata_assert(['fixtures'], '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]')
+
+ # Try just dumping the contents of fixtures.Category
+ self._dumpdata_assert(['fixtures.Category'], '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}]')
+
+ # ...and just fixtures.Article
+ self._dumpdata_assert(['fixtures.Article'], '[{"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]')
+
+ # ...and both
+ self._dumpdata_assert(['fixtures.Category', 'fixtures.Article'], '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]')
+
+ # Specify a specific model twice
+ self._dumpdata_assert(['fixtures.Article', 'fixtures.Article'], '[{"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]')
+
+ # Specify a dump that specifies Article both explicitly and implicitly
+ self._dumpdata_assert(['fixtures.Article', 'fixtures'], '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]')
+
+ # Same again, but specify in the reverse order
+ self._dumpdata_assert(['fixtures'], '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]')
+
+ # Specify one model from one application, and an entire other application.
+ self._dumpdata_assert(['fixtures.Category', 'sites'], '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 1, "model": "sites.site", "fields": {"domain": "example.com", "name": "example.com"}}]')
+
+ # Load fixture 2. JSON file imported by default. Overwrites some existing objects
+ management.call_command('loaddata', 'fixture2.json', verbosity=0, commit=False)
+ self.assertQuerysetEqual(Article.objects.all(), [
+ '<Article: Django conquers world!>',
+ '<Article: Copyright is fine the way it is>',
+ '<Article: Poker has no place on ESPN>',
+ '<Article: Python program becomes self aware>'
+ ])
+
+ # Load fixture 3, XML format.
+ management.call_command('loaddata', 'fixture3.xml', verbosity=0, commit=False)
+ self.assertQuerysetEqual(Article.objects.all(), [
+ '<Article: XML identified as leading cause of cancer>',
+ '<Article: Django conquers world!>',
+ '<Article: Copyright is fine the way it is>',
+ '<Article: Poker on TV is great!>',
+ '<Article: Python program becomes self aware>'
+ ])
+
+ # Load fixture 6, JSON file with dynamic ContentType fields. Testing ManyToOne.
+ management.call_command('loaddata', 'fixture6.json', verbosity=0, commit=False)
+ self.assertQuerysetEqual(Tag.objects.all(), [
+ '<Tag: <Article: Copyright is fine the way it is> tagged "copyright">',
+ '<Tag: <Article: Copyright is fine the way it is> tagged "law">'
+ ])
+
+ # Load fixture 7, XML file with dynamic ContentType fields. Testing ManyToOne.
+ management.call_command('loaddata', 'fixture7.xml', verbosity=0, commit=False)
+ self.assertQuerysetEqual(Tag.objects.all(), [
+ '<Tag: <Article: Copyright is fine the way it is> tagged "copyright">',
+ '<Tag: <Article: Copyright is fine the way it is> tagged "legal">',
+ '<Tag: <Article: Django conquers world!> tagged "django">',
+ '<Tag: <Article: Django conquers world!> tagged "world domination">'
+ ])
+
+ # Load fixture 8, JSON file with dynamic Permission fields. Testing ManyToMany.
+ management.call_command('loaddata', 'fixture8.json', verbosity=0, commit=False)
+ self.assertQuerysetEqual(Visa.objects.all(), [
+ '<Visa: Django Reinhardt Can add user, Can change user, Can delete user>',
+ '<Visa: Stephane Grappelli Can add user>',
+ '<Visa: Prince >'
+ ])
+
+ # Load fixture 9, XML file with dynamic Permission fields. Testing ManyToMany.
+ management.call_command('loaddata', 'fixture9.xml', verbosity=0, commit=False)
+ self.assertQuerysetEqual(Visa.objects.all(), [
+ '<Visa: Django Reinhardt Can add user, Can change user, Can delete user>',
+ '<Visa: Stephane Grappelli Can add user, Can delete user>',
+ '<Visa: Artist formerly known as "Prince" Can change user>'
+ ])
+
+ self.assertQuerysetEqual(Book.objects.all(), [
+ '<Book: Music for all ages by Artist formerly known as "Prince" and Django Reinhardt>'
+ ])
+
+ # Load a fixture that doesn't exist
+ management.call_command('loaddata', 'unknown.json', verbosity=0, commit=False)
+
+ # object list is unaffected
+ self.assertQuerysetEqual(Article.objects.all(), [
+ '<Article: XML identified as leading cause of cancer>',
+ '<Article: Django conquers world!>',
+ '<Article: Copyright is fine the way it is>',
+ '<Article: Poker on TV is great!>',
+ '<Article: Python program becomes self aware>'
+ ])
+
+ # By default, you get raw keys on dumpdata
+ self._dumpdata_assert(['fixtures.book'], '[{"pk": 1, "model": "fixtures.book", "fields": {"name": "Music for all ages", "authors": [3, 1]}}]')
+
+ # But you can get natural keys if you ask for them and they are available
+ self._dumpdata_assert(['fixtures.book'], '[{"pk": 1, "model": "fixtures.book", "fields": {"name": "Music for all ages", "authors": [["Artist formerly known as \\"Prince\\""], ["Django Reinhardt"]]}}]', natural_keys=True)
+
+ # Dump the current contents of the database as a JSON fixture
+ self._dumpdata_assert(['fixtures'], '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 5, "model": "fixtures.article", "fields": {"headline": "XML identified as leading cause of cancer", "pub_date": "2006-06-16 16:00:00"}}, {"pk": 4, "model": "fixtures.article", "fields": {"headline": "Django conquers world!", "pub_date": "2006-06-16 15:00:00"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Copyright is fine the way it is", "pub_date": "2006-06-16 14:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker on TV is great!", "pub_date": "2006-06-16 11:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}, {"pk": 1, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "copyright", "tagged_id": 3}}, {"pk": 2, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "legal", "tagged_id": 3}}, {"pk": 3, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "django", "tagged_id": 4}}, {"pk": 4, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "world domination", "tagged_id": 4}}, {"pk": 3, "model": "fixtures.person", "fields": {"name": "Artist formerly known as \\"Prince\\""}}, {"pk": 1, "model": "fixtures.person", "fields": {"name": "Django Reinhardt"}}, {"pk": 2, "model": "fixtures.person", "fields": {"name": "Stephane Grappelli"}}, {"pk": 1, "model": "fixtures.visa", "fields": {"person": ["Django Reinhardt"], "permissions": [["add_user", "auth", "user"], ["change_user", "auth", "user"], ["delete_user", "auth", "user"]]}}, {"pk": 2, "model": "fixtures.visa", "fields": {"person": ["Stephane Grappelli"], "permissions": [["add_user", "auth", "user"], ["delete_user", "auth", "user"]]}}, {"pk": 3, "model": "fixtures.visa", "fields": {"person": ["Artist formerly known as \\"Prince\\""], "permissions": [["change_user", "auth", "user"]]}}, {"pk": 1, "model": "fixtures.book", "fields": {"name": "Music for all ages", "authors": [["Artist formerly known as \\"Prince\\""], ["Django Reinhardt"]]}}]', natural_keys=True)
+
+ # Dump the current contents of the database as an XML fixture
+ self._dumpdata_assert(['fixtures'], """<?xml version="1.0" encoding="utf-8"?>
+<django-objects version="1.0"><object pk="1" model="fixtures.category"><field type="CharField" name="title">News Stories</field><field type="TextField" name="description">Latest news stories</field></object><object pk="5" model="fixtures.article"><field type="CharField" name="headline">XML identified as leading cause of cancer</field><field type="DateTimeField" name="pub_date">2006-06-16 16:00:00</field></object><object pk="4" model="fixtures.article"><field type="CharField" name="headline">Django conquers world!</field><field type="DateTimeField" name="pub_date">2006-06-16 15:00:00</field></object><object pk="3" model="fixtures.article"><field type="CharField" name="headline">Copyright is fine the way it is</field><field type="DateTimeField" name="pub_date">2006-06-16 14:00:00</field></object><object pk="2" model="fixtures.article"><field type="CharField" name="headline">Poker on TV is great!</field><field type="DateTimeField" name="pub_date">2006-06-16 11:00:00</field></object><object pk="1" model="fixtures.article"><field type="CharField" name="headline">Python program becomes self aware</field><field type="DateTimeField" name="pub_date">2006-06-16 11:00:00</field></object><object pk="1" model="fixtures.tag"><field type="CharField" name="name">copyright</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="2" model="fixtures.tag"><field type="CharField" name="name">legal</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="3" model="fixtures.tag"><field type="CharField" name="name">django</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">4</field></object><object pk="4" model="fixtures.tag"><field type="CharField" name="name">world domination</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">4</field></object><object pk="3" model="fixtures.person"><field type="CharField" name="name">Artist formerly known as "Prince"</field></object><object pk="1" model="fixtures.person"><field type="CharField" name="name">Django Reinhardt</field></object><object pk="2" model="fixtures.person"><field type="CharField" name="name">Stephane Grappelli</field></object><object pk="1" model="fixtures.visa"><field to="fixtures.person" name="person" rel="ManyToOneRel"><natural>Django Reinhardt</natural></field><field to="auth.permission" name="permissions" rel="ManyToManyRel"><object><natural>add_user</natural><natural>auth</natural><natural>user</natural></object><object><natural>change_user</natural><natural>auth</natural><natural>user</natural></object><object><natural>delete_user</natural><natural>auth</natural><natural>user</natural></object></field></object><object pk="2" model="fixtures.visa"><field to="fixtures.person" name="person" rel="ManyToOneRel"><natural>Stephane Grappelli</natural></field><field to="auth.permission" name="permissions" rel="ManyToManyRel"><object><natural>add_user</natural><natural>auth</natural><natural>user</natural></object><object><natural>delete_user</natural><natural>auth</natural><natural>user</natural></object></field></object><object pk="3" model="fixtures.visa"><field to="fixtures.person" name="person" rel="ManyToOneRel"><natural>Artist formerly known as "Prince"</natural></field><field to="auth.permission" name="permissions" rel="ManyToManyRel"><object><natural>change_user</natural><natural>auth</natural><natural>user</natural></object></field></object><object pk="1" model="fixtures.book"><field type="CharField" name="name">Music for all ages</field><field to="fixtures.person" name="authors" rel="ManyToManyRel"><object><natural>Artist formerly known as "Prince"</natural></object><object><natural>Django Reinhardt</natural></object></field></object></django-objects>""", format='xml', natural_keys=True)
+
+ def test_dumpdata_with_excludes(self):
+ # Load fixture1 which has a site, two articles, and a category
+ management.call_command('loaddata', 'fixture1.json', verbosity=0, commit=False)
+
+ # Excluding fixtures app should only leave sites
+ self._dumpdata_assert(
+ ['sites', 'fixtures'],
+ '[{"pk": 1, "model": "sites.site", "fields": {"domain": "example.com", "name": "example.com"}}]',
+ exclude_list=['fixtures'])
+
+ # Excluding fixtures.Article should leave fixtures.Category
+ self._dumpdata_assert(
+ ['sites', 'fixtures'],
+ '[{"pk": 1, "model": "sites.site", "fields": {"domain": "example.com", "name": "example.com"}}, {"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}]',
+ exclude_list=['fixtures.Article'])
+
+ # Excluding fixtures and fixtures.Article should be a no-op
+ self._dumpdata_assert(
+ ['sites', 'fixtures'],
+ '[{"pk": 1, "model": "sites.site", "fields": {"domain": "example.com", "name": "example.com"}}, {"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}]',
+ exclude_list=['fixtures.Article'])
+
+ # Excluding sites and fixtures.Article should only leave fixtures.Category
+ self._dumpdata_assert(
+ ['sites', 'fixtures'],
+ '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}]',
+ exclude_list=['fixtures.Article', 'sites'])
+
+ # Excluding a bogus app should throw an error
+ self.assertRaises(SystemExit,
+ self._dumpdata_assert,
+ ['fixtures', 'sites'],
+ '',
+ exclude_list=['foo_app'])
+
+ # Excluding a bogus model should throw an error
+ self.assertRaises(SystemExit,
+ self._dumpdata_assert,
+ ['fixtures', 'sites'],
+ '',
+ exclude_list=['fixtures.FooModel'])
+
+ def test_dumpdata_with_filtering_manager(self):
+ Spy(name='Paul').save()
+ Spy(name='Alex', cover_blown=True).save()
+ self.assertQuerysetEqual(Spy.objects.all(),
+ ['<Spy: Paul>'])
+ # Use the default manager
+ self._dumpdata_assert(['fixtures.Spy'],'[{"pk": 1, "model": "fixtures.spy", "fields": {"cover_blown": false}}]')
+ # Dump using Django's base manager. Should return all objects,
+ # even those normally filtered by the manager
+ self._dumpdata_assert(['fixtures.Spy'], '[{"pk": 2, "model": "fixtures.spy", "fields": {"cover_blown": true}}, {"pk": 1, "model": "fixtures.spy", "fields": {"cover_blown": false}}]', use_base_manager=True)
+
+ def test_compress_format_loading(self):
+ # Load fixture 4 (compressed), using format specification
+ management.call_command('loaddata', 'fixture4.json', verbosity=0, commit=False)
+ self.assertQuerysetEqual(Article.objects.all(), [
+ '<Article: Django pets kitten>',
+ '<Article: Python program becomes self aware>'
+ ])
+
+ def test_compressed_specified_loading(self):
+ # Load fixture 5 (compressed), using format *and* compression specification
+ management.call_command('loaddata', 'fixture5.json.zip', verbosity=0, commit=False)
+ self.assertQuerysetEqual(Article.objects.all(), [
+ '<Article: WoW subscribers now outnumber readers>',
+ '<Article: Python program becomes self aware>'
+ ])
+
+ def test_compressed_loading(self):
+ # Load fixture 5 (compressed), only compression specification
+ management.call_command('loaddata', 'fixture5.zip', verbosity=0, commit=False)
+ self.assertQuerysetEqual(Article.objects.all(), [
+ '<Article: WoW subscribers now outnumber readers>',
+ '<Article: Python program becomes self aware>'
+ ])
+
+ def test_ambiguous_compressed_fixture(self):
+ # The name "fixture5" is ambigous, so loading it will raise an error
+ new_io = StringIO.StringIO()
+ management.call_command('loaddata', 'fixture5', verbosity=0, stderr=new_io, commit=False)
+ output = new_io.getvalue().strip().split('\n')
+ self.assertEqual(len(output), 1)
+ self.assertTrue(output[0].startswith("Multiple fixtures named 'fixture5'"))
+
+ def test_db_loading(self):
+ # Load db fixtures 1 and 2. These will load using the 'default' database identifier implicitly
+ management.call_command('loaddata', 'db_fixture_1', verbosity=0, commit=False)
+ management.call_command('loaddata', 'db_fixture_2', verbosity=0, commit=False)
+ self.assertQuerysetEqual(Article.objects.all(), [
+ '<Article: Who needs more than one database?>',
+ '<Article: Who needs to use compressed data?>',
+ '<Article: Python program becomes self aware>'
+ ])
+
+ def test_loading_using(self):
+ # Load db fixtures 1 and 2. These will load using the 'default' database identifier explicitly
+ management.call_command('loaddata', 'db_fixture_1', verbosity=0, using='default', commit=False)
+ management.call_command('loaddata', 'db_fixture_2', verbosity=0, using='default', commit=False)
+ self.assertQuerysetEqual(Article.objects.all(), [
+ '<Article: Who needs more than one database?>',
+ '<Article: Who needs to use compressed data?>',
+ '<Article: Python program becomes self aware>'
+ ])
+
+ def test_unmatched_identifier_loading(self):
+ # Try to load db fixture 3. This won't load because the database identifier doesn't match
+ management.call_command('loaddata', 'db_fixture_3', verbosity=0, commit=False)
+ self.assertQuerysetEqual(Article.objects.all(), [
+ '<Article: Python program becomes self aware>'
+ ])
+
+ management.call_command('loaddata', 'db_fixture_3', verbosity=0, using='default', commit=False)
+ self.assertQuerysetEqual(Article.objects.all(), [
+ '<Article: Python program becomes self aware>'
+ ])
+
+ def test_output_formats(self):
+ # Load back in fixture 1, we need the articles from it
+ management.call_command('loaddata', 'fixture1', verbosity=0, commit=False)
+
+ # Try to load fixture 6 using format discovery
+ management.call_command('loaddata', 'fixture6', verbosity=0, commit=False)
+ self.assertQuerysetEqual(Tag.objects.all(), [
+ '<Tag: <Article: Time to reform copyright> tagged "copyright">',
+ '<Tag: <Article: Time to reform copyright> tagged "law">'
+ ])
+
+ # Dump the current contents of the database as a JSON fixture
+ self._dumpdata_assert(['fixtures'], '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}, {"pk": 1, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "copyright", "tagged_id": 3}}, {"pk": 2, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "law", "tagged_id": 3}}, {"pk": 1, "model": "fixtures.person", "fields": {"name": "Django Reinhardt"}}, {"pk": 3, "model": "fixtures.person", "fields": {"name": "Prince"}}, {"pk": 2, "model": "fixtures.person", "fields": {"name": "Stephane Grappelli"}}]', natural_keys=True)
+
+ # Dump the current contents of the database as an XML fixture
+ self._dumpdata_assert(['fixtures'], """<?xml version="1.0" encoding="utf-8"?>
+<django-objects version="1.0"><object pk="1" model="fixtures.category"><field type="CharField" name="title">News Stories</field><field type="TextField" name="description">Latest news stories</field></object><object pk="3" model="fixtures.article"><field type="CharField" name="headline">Time to reform copyright</field><field type="DateTimeField" name="pub_date">2006-06-16 13:00:00</field></object><object pk="2" model="fixtures.article"><field type="CharField" name="headline">Poker has no place on ESPN</field><field type="DateTimeField" name="pub_date">2006-06-16 12:00:00</field></object><object pk="1" model="fixtures.article"><field type="CharField" name="headline">Python program becomes self aware</field><field type="DateTimeField" name="pub_date">2006-06-16 11:00:00</field></object><object pk="1" model="fixtures.tag"><field type="CharField" name="name">copyright</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="2" model="fixtures.tag"><field type="CharField" name="name">law</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="1" model="fixtures.person"><field type="CharField" name="name">Django Reinhardt</field></object><object pk="3" model="fixtures.person"><field type="CharField" name="name">Prince</field></object><object pk="2" model="fixtures.person"><field type="CharField" name="name">Stephane Grappelli</field></object></django-objects>""", format='xml', natural_keys=True)
+
+if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.mysql':
+ class FixtureTransactionTests(TransactionTestCase):
+ def _dumpdata_assert(self, args, output, format='json'):
+ new_io = StringIO.StringIO()
+ management.call_command('dumpdata', *args, **{'format':format, 'stdout':new_io})
+ command_output = new_io.getvalue().strip()
+ self.assertEqual(command_output, output)
+
+ def test_format_discovery(self):
+ # Load fixture 1 again, using format discovery
+ management.call_command('loaddata', 'fixture1', verbosity=0, commit=False)
+ self.assertQuerysetEqual(Article.objects.all(), [
+ '<Article: Time to reform copyright>',
+ '<Article: Poker has no place on ESPN>',
+ '<Article: Python program becomes self aware>'
+ ])
+
+ # Try to load fixture 2 using format discovery; this will fail
+ # because there are two fixture2's in the fixtures directory
+ new_io = StringIO.StringIO()
+ management.call_command('loaddata', 'fixture2', verbosity=0, stderr=new_io)
+ output = new_io.getvalue().strip().split('\n')
+ self.assertEqual(len(output), 1)
+ self.assertTrue(output[0].startswith("Multiple fixtures named 'fixture2'"))
+
+ # object list is unaffected
+ self.assertQuerysetEqual(Article.objects.all(), [
+ '<Article: Time to reform copyright>',
+ '<Article: Poker has no place on ESPN>',
+ '<Article: Python program becomes self aware>'
+ ])
+
+ # Dump the current contents of the database as a JSON fixture
+ self._dumpdata_assert(['fixtures'], '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]')
+
+ # Load fixture 4 (compressed), using format discovery
+ management.call_command('loaddata', 'fixture4', verbosity=0, commit=False)
+ self.assertQuerysetEqual(Article.objects.all(), [
+ '<Article: Django pets kitten>',
+ '<Article: Time to reform copyright>',
+ '<Article: Poker has no place on ESPN>',
+ '<Article: Python program becomes self aware>'
+ ])
diff --git a/tests/modeltests/fixtures_model_package/models/__init__.py b/tests/modeltests/fixtures_model_package/models/__init__.py
index 1581102b88..c0450b27bf 100644
--- a/tests/modeltests/fixtures_model_package/models/__init__.py
+++ b/tests/modeltests/fixtures_model_package/models/__init__.py
@@ -12,43 +12,3 @@ class Article(models.Model):
app_label = 'fixtures_model_package'
ordering = ('-pub_date', 'headline')
-__test__ = {'API_TESTS': """
->>> from django.core import management
->>> from django.db.models import get_app
-
-# Reset the database representation of this app.
-# This will return the database to a clean initial state.
->>> management.call_command('flush', verbosity=0, interactive=False)
-
-# Syncdb introduces 1 initial data object from initial_data.json.
->>> Article.objects.all()
-[<Article: Python program becomes self aware>]
-
-# Load fixture 1. Single JSON file, with two objects.
->>> management.call_command('loaddata', 'fixture1.json', verbosity=0)
->>> Article.objects.all()
-[<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
-
-# Load fixture 2. JSON file imported by default. Overwrites some existing objects
->>> management.call_command('loaddata', 'fixture2.json', verbosity=0)
->>> Article.objects.all()
-[<Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
-
-# Load a fixture that doesn't exist
->>> management.call_command('loaddata', 'unknown.json', verbosity=0)
-
-# object list is unaffected
->>> Article.objects.all()
-[<Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
-"""}
-
-
-from django.test import TestCase
-
-class SampleTestCase(TestCase):
- fixtures = ['fixture1.json', 'fixture2.json']
-
- def testClassFixtures(self):
- "Check that test case has installed 4 fixture objects"
- self.assertEqual(Article.objects.count(), 4)
- self.assertEquals(str(Article.objects.all()), "[<Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]")
diff --git a/tests/modeltests/fixtures_model_package/tests.py b/tests/modeltests/fixtures_model_package/tests.py
new file mode 100644
index 0000000000..1fae5ee807
--- /dev/null
+++ b/tests/modeltests/fixtures_model_package/tests.py
@@ -0,0 +1,71 @@
+from django.core import management
+from django.test import TestCase
+
+from models import Article
+
+
+class SampleTestCase(TestCase):
+ fixtures = ['fixture1.json', 'fixture2.json']
+
+ def testClassFixtures(self):
+ "Test cases can load fixture objects into models defined in packages"
+ self.assertEqual(Article.objects.count(), 4)
+ self.assertQuerysetEqual(
+ Article.objects.all(),[
+ "Django conquers world!",
+ "Copyright is fine the way it is",
+ "Poker has no place on ESPN",
+ "Python program becomes self aware"
+ ],
+ lambda a: a.headline
+ )
+
+
+class FixtureTestCase(TestCase):
+ def test_initial_data(self):
+ "Fixtures can load initial data into models defined in packages"
+ #Syncdb introduces 1 initial data object from initial_data.json
+ self.assertQuerysetEqual(
+ Article.objects.all(), [
+ "Python program becomes self aware"
+ ],
+ lambda a: a.headline
+ )
+
+ def test_loaddata(self):
+ "Fixtures can load data into models defined in packages"
+ # Load fixture 1. Single JSON file, with two objects
+ management.call_command("loaddata", "fixture1.json", verbosity=0, commit=False)
+ self.assertQuerysetEqual(
+ Article.objects.all(), [
+ "Time to reform copyright",
+ "Poker has no place on ESPN",
+ "Python program becomes self aware",
+ ],
+ lambda a: a.headline,
+ )
+
+ # Load fixture 2. JSON file imported by default. Overwrites some
+ # existing objects
+ management.call_command("loaddata", "fixture2.json", verbosity=0, commit=False)
+ self.assertQuerysetEqual(
+ Article.objects.all(), [
+ "Django conquers world!",
+ "Copyright is fine the way it is",
+ "Poker has no place on ESPN",
+ "Python program becomes self aware",
+ ],
+ lambda a: a.headline,
+ )
+
+ # Load a fixture that doesn't exist
+ management.call_command("loaddata", "unknown.json", verbosity=0, commit=False)
+ self.assertQuerysetEqual(
+ Article.objects.all(), [
+ "Django conquers world!",
+ "Copyright is fine the way it is",
+ "Poker has no place on ESPN",
+ "Python program becomes self aware",
+ ],
+ lambda a: a.headline,
+ )
diff --git a/tests/modeltests/force_insert_update/models.py b/tests/modeltests/force_insert_update/models.py
index 2489740e98..9516be7718 100644
--- a/tests/modeltests/force_insert_update/models.py
+++ b/tests/modeltests/force_insert_update/models.py
@@ -11,54 +11,3 @@ class Counter(models.Model):
class WithCustomPK(models.Model):
name = models.IntegerField(primary_key=True)
value = models.IntegerField()
-
-__test__ = {"API_TESTS": """
->>> c = Counter.objects.create(name="one", value=1)
-
-# The normal case
->>> c.value = 2
->>> c.save()
-
-# Same thing, via an update
->>> c.value = 3
->>> c.save(force_update=True)
-
-# Won't work because force_update and force_insert are mutually exclusive
->>> c.value = 4
->>> c.save(force_insert=True, force_update=True)
-Traceback (most recent call last):
-...
-ValueError: Cannot force both insert and updating in model saving.
-
-# Try to update something that doesn't have a primary key in the first place.
->>> c1 = Counter(name="two", value=2)
->>> c1.save(force_update=True)
-Traceback (most recent call last):
-...
-ValueError: Cannot force an update in save() with no primary key.
-
->>> c1.save(force_insert=True)
-
-# Won't work because we can't insert a pk of the same value.
->>> sid = transaction.savepoint()
->>> c.value = 5
->>> try:
-... c.save(force_insert=True)
-... except Exception, e:
-... if isinstance(e, IntegrityError):
-... print "Pass"
-... else:
-... print "Fail with %s" % type(e)
-Pass
->>> transaction.savepoint_rollback(sid)
-
-# Trying to update should still fail, even with manual primary keys, if the
-# data isn't in the database already.
->>> obj = WithCustomPK(name=1, value=1)
->>> obj.save(force_update=True)
-Traceback (most recent call last):
-...
-DatabaseError: ...
-
-"""
-}
diff --git a/tests/modeltests/force_insert_update/tests.py b/tests/modeltests/force_insert_update/tests.py
new file mode 100644
index 0000000000..bd3eb7dcf6
--- /dev/null
+++ b/tests/modeltests/force_insert_update/tests.py
@@ -0,0 +1,38 @@
+from django.db import transaction, IntegrityError, DatabaseError
+from django.test import TestCase
+
+from models import Counter, WithCustomPK
+
+
+class ForceTests(TestCase):
+ def test_force_update(self):
+ c = Counter.objects.create(name="one", value=1)
+ # The normal case
+
+ c.value = 2
+ c.save()
+ # Same thing, via an update
+ c.value = 3
+ c.save(force_update=True)
+
+ # Won't work because force_update and force_insert are mutually
+ # exclusive
+ c.value = 4
+ self.assertRaises(ValueError, c.save, force_insert=True, force_update=True)
+
+ # Try to update something that doesn't have a primary key in the first
+ # place.
+ c1 = Counter(name="two", value=2)
+ self.assertRaises(ValueError, c1.save, force_update=True)
+ c1.save(force_insert=True)
+
+ # Won't work because we can't insert a pk of the same value.
+ sid = transaction.savepoint()
+ c.value = 5
+ self.assertRaises(IntegrityError, c.save, force_insert=True)
+ transaction.savepoint_rollback(sid)
+
+ # Trying to update should still fail, even with manual primary keys, if
+ # the data isn't in the database already.
+ obj = WithCustomPK(name=1, value=1)
+ self.assertRaises(DatabaseError, obj.save, force_update=True)
diff --git a/tests/modeltests/get_latest/models.py b/tests/modeltests/get_latest/models.py
index 624f3a879a..1eeb299267 100644
--- a/tests/modeltests/get_latest/models.py
+++ b/tests/modeltests/get_latest/models.py
@@ -28,52 +28,3 @@ class Person(models.Model):
def __unicode__(self):
return self.name
-
-__test__ = {'API_TESTS':"""
-# Because no Articles exist yet, latest() raises ArticleDoesNotExist.
->>> Article.objects.latest()
-Traceback (most recent call last):
- ...
-DoesNotExist: Article matching query does not exist.
-
-# Create a couple of Articles.
->>> from datetime import datetime
->>> a1 = Article(headline='Article 1', pub_date=datetime(2005, 7, 26), expire_date=datetime(2005, 9, 1))
->>> a1.save()
->>> a2 = Article(headline='Article 2', pub_date=datetime(2005, 7, 27), expire_date=datetime(2005, 7, 28))
->>> a2.save()
->>> a3 = Article(headline='Article 3', pub_date=datetime(2005, 7, 27), expire_date=datetime(2005, 8, 27))
->>> a3.save()
->>> a4 = Article(headline='Article 4', pub_date=datetime(2005, 7, 28), expire_date=datetime(2005, 7, 30))
->>> a4.save()
-
-# Get the latest Article.
->>> Article.objects.latest()
-<Article: Article 4>
-
-# Get the latest Article that matches certain filters.
->>> Article.objects.filter(pub_date__lt=datetime(2005, 7, 27)).latest()
-<Article: Article 1>
-
-# Pass a custom field name to latest() to change the field that's used to
-# determine the latest object.
->>> Article.objects.latest('expire_date')
-<Article: Article 1>
-
->>> Article.objects.filter(pub_date__gt=datetime(2005, 7, 26)).latest('expire_date')
-<Article: Article 3>
-
-# You can still use latest() with a model that doesn't have "get_latest_by"
-# set -- just pass in the field name manually.
->>> p1 = Person(name='Ralph', birthday=datetime(1950, 1, 1))
->>> p1.save()
->>> p2 = Person(name='Stephanie', birthday=datetime(1960, 2, 3))
->>> p2.save()
->>> Person.objects.latest()
-Traceback (most recent call last):
- ...
-AssertionError: latest() requires either a field_name parameter or 'get_latest_by' in the model
-
->>> Person.objects.latest('birthday')
-<Person: Stephanie>
-"""}
diff --git a/tests/modeltests/get_latest/tests.py b/tests/modeltests/get_latest/tests.py
new file mode 100644
index 0000000000..3c3588bba0
--- /dev/null
+++ b/tests/modeltests/get_latest/tests.py
@@ -0,0 +1,53 @@
+from datetime import datetime
+
+from django.test import TestCase
+
+from models import Article, Person
+
+
+class LatestTests(TestCase):
+ def test_latest(self):
+ # Because no Articles exist yet, latest() raises ArticleDoesNotExist.
+ self.assertRaises(Article.DoesNotExist, Article.objects.latest)
+
+ a1 = Article.objects.create(
+ headline="Article 1", pub_date=datetime(2005, 7, 26),
+ expire_date=datetime(2005, 9, 1)
+ )
+ a2 = Article.objects.create(
+ headline="Article 2", pub_date=datetime(2005, 7, 27),
+ expire_date=datetime(2005, 7, 28)
+ )
+ a3 = Article.objects.create(
+ headline="Article 3", pub_date=datetime(2005, 7, 27),
+ expire_date=datetime(2005, 8, 27)
+ )
+ a4 = Article.objects.create(
+ headline="Article 4", pub_date=datetime(2005, 7, 28),
+ expire_date=datetime(2005, 7, 30)
+ )
+
+ # Get the latest Article.
+ self.assertEqual(Article.objects.latest(), a4)
+ # Get the latest Article that matches certain filters.
+ self.assertEqual(
+ Article.objects.filter(pub_date__lt=datetime(2005, 7, 27)).latest(),
+ a1
+ )
+
+ # Pass a custom field name to latest() to change the field that's used
+ # to determine the latest object.
+ self.assertEqual(Article.objects.latest('expire_date'), a1)
+ self.assertEqual(
+ Article.objects.filter(pub_date__gt=datetime(2005, 7, 26)).latest('expire_date'),
+ a3,
+ )
+
+ def test_latest_manual(self):
+ # You can still use latest() with a model that doesn't have
+ # "get_latest_by" set -- just pass in the field name manually.
+ p1 = Person.objects.create(name="Ralph", birthday=datetime(1950, 1, 1))
+ p2 = Person.objects.create(name="Stephanie", birthday=datetime(1960, 2, 3))
+ self.assertRaises(AssertionError, Person.objects.latest)
+
+ self.assertEqual(Person.objects.latest("birthday"), p2)
diff --git a/tests/modeltests/get_object_or_404/models.py b/tests/modeltests/get_object_or_404/models.py
index b2812e61e7..eb3cd8254d 100644
--- a/tests/modeltests/get_object_or_404/models.py
+++ b/tests/modeltests/get_object_or_404/models.py
@@ -32,76 +32,3 @@ class Article(models.Model):
def __unicode__(self):
return self.title
-
-__test__ = {'API_TESTS':"""
-# Create some Authors.
->>> a = Author.objects.create(name="Brave Sir Robin")
->>> a.save()
->>> a2 = Author.objects.create(name="Patsy")
->>> a2.save()
-
-# No Articles yet, so we should get a Http404 error.
->>> get_object_or_404(Article, title="Foo")
-Traceback (most recent call last):
-...
-Http404: No Article matches the given query.
-
-# Create an Article.
->>> article = Article.objects.create(title="Run away!")
->>> article.authors = [a, a2]
->>> article.save()
-
-# get_object_or_404 can be passed a Model to query.
->>> get_object_or_404(Article, title__contains="Run")
-<Article: Run away!>
-
-# We can also use the Article manager through an Author object.
->>> get_object_or_404(a.article_set, title__contains="Run")
-<Article: Run away!>
-
-# No articles containing "Camelot". This should raise a Http404 error.
->>> get_object_or_404(a.article_set, title__contains="Camelot")
-Traceback (most recent call last):
-...
-Http404: No Article matches the given query.
-
-# Custom managers can be used too.
->>> get_object_or_404(Article.by_a_sir, title="Run away!")
-<Article: Run away!>
-
-# QuerySets can be used too.
->>> get_object_or_404(Article.objects.all(), title__contains="Run")
-<Article: Run away!>
-
-# Just as when using a get() lookup, you will get an error if more than one
-# object is returned.
->>> get_object_or_404(Author.objects.all())
-Traceback (most recent call last):
-...
-MultipleObjectsReturned: get() returned more than one Author -- it returned ...! Lookup parameters were {}
-
-# Using an EmptyQuerySet raises a Http404 error.
->>> get_object_or_404(Article.objects.none(), title__contains="Run")
-Traceback (most recent call last):
-...
-Http404: No Article matches the given query.
-
-# get_list_or_404 can be used to get lists of objects
->>> get_list_or_404(a.article_set, title__icontains='Run')
-[<Article: Run away!>]
-
-# Http404 is returned if the list is empty.
->>> get_list_or_404(a.article_set, title__icontains='Shrubbery')
-Traceback (most recent call last):
-...
-Http404: No Article matches the given query.
-
-# Custom managers can be used too.
->>> get_list_or_404(Article.by_a_sir, title__icontains="Run")
-[<Article: Run away!>]
-
-# QuerySets can be used too.
->>> get_list_or_404(Article.objects.all(), title__icontains="Run")
-[<Article: Run away!>]
-
-"""}
diff --git a/tests/modeltests/get_object_or_404/tests.py b/tests/modeltests/get_object_or_404/tests.py
new file mode 100644
index 0000000000..b8c4f7510b
--- /dev/null
+++ b/tests/modeltests/get_object_or_404/tests.py
@@ -0,0 +1,80 @@
+from django.http import Http404
+from django.shortcuts import get_object_or_404, get_list_or_404
+from django.test import TestCase
+
+from models import Author, Article
+
+
+class GetObjectOr404Tests(TestCase):
+ def test_get_object_or_404(self):
+ a1 = Author.objects.create(name="Brave Sir Robin")
+ a2 = Author.objects.create(name="Patsy")
+
+ # No Articles yet, so we should get a Http404 error.
+ self.assertRaises(Http404, get_object_or_404, Article, title="Foo")
+
+ article = Article.objects.create(title="Run away!")
+ article.authors = [a1, a2]
+ # get_object_or_404 can be passed a Model to query.
+ self.assertEqual(
+ get_object_or_404(Article, title__contains="Run"),
+ article
+ )
+
+ # We can also use the Article manager through an Author object.
+ self.assertEqual(
+ get_object_or_404(a1.article_set, title__contains="Run"),
+ article
+ )
+
+ # No articles containing "Camelot". This should raise a Http404 error.
+ self.assertRaises(Http404,
+ get_object_or_404, a1.article_set, title__contains="Camelot"
+ )
+
+ # Custom managers can be used too.
+ self.assertEqual(
+ get_object_or_404(Article.by_a_sir, title="Run away!"),
+ article
+ )
+
+ # QuerySets can be used too.
+ self.assertEqual(
+ get_object_or_404(Article.objects.all(), title__contains="Run"),
+ article
+ )
+
+ # Just as when using a get() lookup, you will get an error if more than
+ # one object is returned.
+
+ self.assertRaises(Author.MultipleObjectsReturned,
+ get_object_or_404, Author.objects.all()
+ )
+
+ # Using an EmptyQuerySet raises a Http404 error.
+ self.assertRaises(Http404,
+ get_object_or_404, Article.objects.none(), title__contains="Run"
+ )
+
+ # get_list_or_404 can be used to get lists of objects
+ self.assertEqual(
+ get_list_or_404(a1.article_set, title__icontains="Run"),
+ [article]
+ )
+
+ # Http404 is returned if the list is empty.
+ self.assertRaises(Http404,
+ get_list_or_404, a1.article_set, title__icontains="Shrubbery"
+ )
+
+ # Custom managers can be used too.
+ self.assertEqual(
+ get_list_or_404(Article.by_a_sir, title__icontains="Run"),
+ [article]
+ )
+
+ # QuerySets can be used too.
+ self.assertEqual(
+ get_list_or_404(Article.objects.all(), title__icontains="Run"),
+ [article]
+ )
diff --git a/tests/modeltests/get_or_create/models.py b/tests/modeltests/get_or_create/models.py
index 56baa5c1ed..db5719b79e 100644
--- a/tests/modeltests/get_or_create/models.py
+++ b/tests/modeltests/get_or_create/models.py
@@ -19,65 +19,3 @@ class Person(models.Model):
class ManualPrimaryKeyTest(models.Model):
id = models.IntegerField(primary_key=True)
data = models.CharField(max_length=100)
-
-__test__ = {'API_TESTS':"""
-# Acting as a divine being, create an Person.
->>> from datetime import date
->>> p = Person(first_name='John', last_name='Lennon', birthday=date(1940, 10, 9))
->>> p.save()
-
-# Only one Person is in the database at this point.
->>> Person.objects.count()
-1
-
-# get_or_create() a person with similar first names.
->>> p, created = Person.objects.get_or_create(first_name='John', last_name='Lennon', defaults={'birthday': date(1940, 10, 9)})
-
-# get_or_create() didn't have to create an object.
->>> created
-False
-
-# There's still only one Person in the database.
->>> Person.objects.count()
-1
-
-# get_or_create() a Person with a different name.
->>> p, created = Person.objects.get_or_create(first_name='George', last_name='Harrison', defaults={'birthday': date(1943, 2, 25)})
->>> created
-True
->>> Person.objects.count()
-2
-
-# If we execute the exact same statement, it won't create a Person.
->>> p, created = Person.objects.get_or_create(first_name='George', last_name='Harrison', defaults={'birthday': date(1943, 2, 25)})
->>> created
-False
->>> Person.objects.count()
-2
-
-# If you don't specify a value or default value for all required fields, you
-# will get an error.
->>> try:
-... p, created = Person.objects.get_or_create(first_name='Tom', last_name='Smith')
-... except Exception, e:
-... if isinstance(e, IntegrityError):
-... print "Pass"
-... else:
-... print "Fail with %s" % type(e)
-Pass
-
-# If you specify an existing primary key, but different other fields, then you
-# will get an error and data will not be updated.
->>> m = ManualPrimaryKeyTest(id=1, data='Original')
->>> m.save()
->>> try:
-... m, created = ManualPrimaryKeyTest.objects.get_or_create(id=1, data='Different')
-... except Exception, e:
-... if isinstance(e, IntegrityError):
-... print "Pass"
-... else:
-... print "Fail with %s" % type(e)
-Pass
->>> ManualPrimaryKeyTest.objects.get(id=1).data == 'Original'
-True
-"""}
diff --git a/tests/modeltests/get_or_create/tests.py b/tests/modeltests/get_or_create/tests.py
new file mode 100644
index 0000000000..1999b20c76
--- /dev/null
+++ b/tests/modeltests/get_or_create/tests.py
@@ -0,0 +1,52 @@
+from datetime import date
+
+from django.db import IntegrityError
+from django.test import TransactionTestCase
+
+from models import Person, ManualPrimaryKeyTest
+
+
+class GetOrCreateTests(TransactionTestCase):
+ def test_get_or_create(self):
+ p = Person.objects.create(
+ first_name='John', last_name='Lennon', birthday=date(1940, 10, 9)
+ )
+
+ p, created = Person.objects.get_or_create(
+ first_name="John", last_name="Lennon", defaults={
+ "birthday": date(1940, 10, 9)
+ }
+ )
+ self.assertFalse(created)
+ self.assertEqual(Person.objects.count(), 1)
+
+ p, created = Person.objects.get_or_create(
+ first_name='George', last_name='Harrison', defaults={
+ 'birthday': date(1943, 2, 25)
+ }
+ )
+ self.assertTrue(created)
+ self.assertEqual(Person.objects.count(), 2)
+
+ # If we execute the exact same statement, it won't create a Person.
+ p, created = Person.objects.get_or_create(
+ first_name='George', last_name='Harrison', defaults={
+ 'birthday': date(1943, 2, 25)
+ }
+ )
+ self.assertFalse(created)
+ self.assertEqual(Person.objects.count(), 2)
+
+ # If you don't specify a value or default value for all required
+ # fields, you will get an error.
+ self.assertRaises(IntegrityError,
+ Person.objects.get_or_create, first_name="Tom", last_name="Smith"
+ )
+
+ # If you specify an existing primary key, but different other fields,
+ # then you will get an error and data will not be updated.
+ m = ManualPrimaryKeyTest.objects.create(id=1, data="Original")
+ self.assertRaises(IntegrityError,
+ ManualPrimaryKeyTest.objects.get_or_create, id=1, data="Different"
+ )
+ self.assertEqual(ManualPrimaryKeyTest.objects.get(id=1).data, "Original")
diff --git a/tests/modeltests/m2m_and_m2o/models.py b/tests/modeltests/m2m_and_m2o/models.py
index 0ab7a72d57..0fea1a2e7b 100644
--- a/tests/modeltests/m2m_and_m2o/models.py
+++ b/tests/modeltests/m2m_and_m2o/models.py
@@ -19,47 +19,3 @@ class Issue(models.Model):
class Meta:
ordering = ('num',)
-
-
-__test__ = {'API_TESTS':"""
->>> Issue.objects.all()
-[]
->>> r = User(username='russell')
->>> r.save()
->>> g = User(username='gustav')
->>> g.save()
-
->>> i = Issue(num=1)
->>> i.client = r
->>> i.save()
-
->>> i2 = Issue(num=2)
->>> i2.client = r
->>> i2.save()
->>> i2.cc.add(r)
-
->>> i3 = Issue(num=3)
->>> i3.client = g
->>> i3.save()
->>> i3.cc.add(r)
-
->>> from django.db.models.query import Q
-
->>> Issue.objects.filter(client=r.id)
-[<Issue: 1>, <Issue: 2>]
->>> Issue.objects.filter(client=g.id)
-[<Issue: 3>]
->>> Issue.objects.filter(cc__id__exact=g.id)
-[]
->>> Issue.objects.filter(cc__id__exact=r.id)
-[<Issue: 2>, <Issue: 3>]
-
-# These queries combine results from the m2m and the m2o relationships.
-# They're three ways of saying the same thing.
->>> Issue.objects.filter(Q(cc__id__exact=r.id) | Q(client=r.id))
-[<Issue: 1>, <Issue: 2>, <Issue: 3>]
->>> Issue.objects.filter(cc__id__exact=r.id) | Issue.objects.filter(client=r.id)
-[<Issue: 1>, <Issue: 2>, <Issue: 3>]
->>> Issue.objects.filter(Q(client=r.id) | Q(cc__id__exact=r.id))
-[<Issue: 1>, <Issue: 2>, <Issue: 3>]
-"""}
diff --git a/tests/modeltests/m2m_and_m2o/tests.py b/tests/modeltests/m2m_and_m2o/tests.py
new file mode 100644
index 0000000000..dedf9cdf26
--- /dev/null
+++ b/tests/modeltests/m2m_and_m2o/tests.py
@@ -0,0 +1,75 @@
+from django.db.models import Q
+from django.test import TestCase
+
+from models import Issue, User
+
+
+class RelatedObjectTests(TestCase):
+ def test_m2m_and_m2o(self):
+ r = User.objects.create(username="russell")
+ g = User.objects.create(username="gustav")
+
+ i1 = Issue(num=1)
+ i1.client = r
+ i1.save()
+
+ i2 = Issue(num=2)
+ i2.client = r
+ i2.save()
+ i2.cc.add(r)
+
+ i3 = Issue(num=3)
+ i3.client = g
+ i3.save()
+ i3.cc.add(r)
+
+ self.assertQuerysetEqual(
+ Issue.objects.filter(client=r.id), [
+ 1,
+ 2,
+ ],
+ lambda i: i.num
+ )
+ self.assertQuerysetEqual(
+ Issue.objects.filter(client=g.id), [
+ 3,
+ ],
+ lambda i: i.num
+ )
+ self.assertQuerysetEqual(
+ Issue.objects.filter(cc__id__exact=g.id), []
+ )
+ self.assertQuerysetEqual(
+ Issue.objects.filter(cc__id__exact=r.id), [
+ 2,
+ 3,
+ ],
+ lambda i: i.num
+ )
+
+ # These queries combine results from the m2m and the m2o relationships.
+ # They're three ways of saying the same thing.
+ self.assertQuerysetEqual(
+ Issue.objects.filter(Q(cc__id__exact = r.id) | Q(client=r.id)), [
+ 1,
+ 2,
+ 3,
+ ],
+ lambda i: i.num
+ )
+ self.assertQuerysetEqual(
+ Issue.objects.filter(cc__id__exact=r.id) | Issue.objects.filter(client=r.id), [
+ 1,
+ 2,
+ 3,
+ ],
+ lambda i: i.num
+ )
+ self.assertQuerysetEqual(
+ Issue.objects.filter(Q(client=r.id) | Q(cc__id__exact=r.id)), [
+ 1,
+ 2,
+ 3,
+ ],
+ lambda i: i.num
+ )
diff --git a/tests/modeltests/m2m_intermediary/models.py b/tests/modeltests/m2m_intermediary/models.py
index e9f964aa4e..8042a52b38 100644
--- a/tests/modeltests/m2m_intermediary/models.py
+++ b/tests/modeltests/m2m_intermediary/models.py
@@ -34,35 +34,3 @@ class Writer(models.Model):
def __unicode__(self):
return u'%s (%s)' % (self.reporter, self.position)
-__test__ = {'API_TESTS':"""
-# Create a few Reporters.
->>> r1 = Reporter(first_name='John', last_name='Smith')
->>> r1.save()
->>> r2 = Reporter(first_name='Jane', last_name='Doe')
->>> r2.save()
-
-# Create an Article.
->>> from datetime import datetime
->>> a = Article(headline='This is a test', pub_date=datetime(2005, 7, 27))
->>> a.save()
-
-# Create a few Writers.
->>> w1 = Writer(reporter=r1, article=a, position='Main writer')
->>> w1.save()
->>> w2 = Writer(reporter=r2, article=a, position='Contributor')
->>> w2.save()
-
-# Play around with the API.
->>> a.writer_set.select_related().order_by('-position')
-[<Writer: John Smith (Main writer)>, <Writer: Jane Doe (Contributor)>]
->>> w1.reporter
-<Reporter: John Smith>
->>> w2.reporter
-<Reporter: Jane Doe>
->>> w1.article
-<Article: This is a test>
->>> w2.article
-<Article: This is a test>
->>> r1.writer_set.all()
-[<Writer: John Smith (Main writer)>]
-"""}
diff --git a/tests/modeltests/m2m_intermediary/tests.py b/tests/modeltests/m2m_intermediary/tests.py
new file mode 100644
index 0000000000..5f357412a5
--- /dev/null
+++ b/tests/modeltests/m2m_intermediary/tests.py
@@ -0,0 +1,38 @@
+from datetime import datetime
+
+from django.test import TestCase
+
+from models import Reporter, Article, Writer
+
+
+class M2MIntermediaryTests(TestCase):
+ def test_intermeiary(self):
+ r1 = Reporter.objects.create(first_name="John", last_name="Smith")
+ r2 = Reporter.objects.create(first_name="Jane", last_name="Doe")
+
+ a = Article.objects.create(
+ headline="This is a test", pub_date=datetime(2005, 7, 27)
+ )
+
+ w1 = Writer.objects.create(reporter=r1, article=a, position="Main writer")
+ w2 = Writer.objects.create(reporter=r2, article=a, position="Contributor")
+
+ self.assertQuerysetEqual(
+ a.writer_set.select_related().order_by("-position"), [
+ ("John Smith", "Main writer"),
+ ("Jane Doe", "Contributor"),
+ ],
+ lambda w: (unicode(w.reporter), w.position)
+ )
+ self.assertEqual(w1.reporter, r1)
+ self.assertEqual(w2.reporter, r2)
+
+ self.assertEqual(w1.article, a)
+ self.assertEqual(w2.article, a)
+
+ self.assertQuerysetEqual(
+ r1.writer_set.all(), [
+ ("John Smith", "Main writer")
+ ],
+ lambda w: (unicode(w.reporter), w.position)
+ )
diff --git a/tests/modeltests/m2m_multiple/models.py b/tests/modeltests/m2m_multiple/models.py
index 42e74553d9..e53f840653 100644
--- a/tests/modeltests/m2m_multiple/models.py
+++ b/tests/modeltests/m2m_multiple/models.py
@@ -28,52 +28,3 @@ class Article(models.Model):
def __unicode__(self):
return self.headline
-__test__ = {'API_TESTS':"""
->>> from datetime import datetime
-
->>> c1 = Category(name='Sports')
->>> c1.save()
->>> c2 = Category(name='News')
->>> c2.save()
->>> c3 = Category(name='Crime')
->>> c3.save()
->>> c4 = Category(name='Life')
->>> c4.save()
-
->>> a1 = Article(headline='Area man steals', pub_date=datetime(2005, 11, 27))
->>> a1.save()
->>> a1.primary_categories.add(c2, c3)
->>> a1.secondary_categories.add(c4)
-
->>> a2 = Article(headline='Area man runs', pub_date=datetime(2005, 11, 28))
->>> a2.save()
->>> a2.primary_categories.add(c1, c2)
->>> a2.secondary_categories.add(c4)
-
->>> a1.primary_categories.all()
-[<Category: Crime>, <Category: News>]
-
->>> a2.primary_categories.all()
-[<Category: News>, <Category: Sports>]
-
->>> a1.secondary_categories.all()
-[<Category: Life>]
-
-
->>> c1.primary_article_set.all()
-[<Article: Area man runs>]
->>> c1.secondary_article_set.all()
-[]
->>> c2.primary_article_set.all()
-[<Article: Area man steals>, <Article: Area man runs>]
->>> c2.secondary_article_set.all()
-[]
->>> c3.primary_article_set.all()
-[<Article: Area man steals>]
->>> c3.secondary_article_set.all()
-[]
->>> c4.primary_article_set.all()
-[]
->>> c4.secondary_article_set.all()
-[<Article: Area man steals>, <Article: Area man runs>]
-"""}
diff --git a/tests/modeltests/m2m_multiple/tests.py b/tests/modeltests/m2m_multiple/tests.py
new file mode 100644
index 0000000000..1f4503a483
--- /dev/null
+++ b/tests/modeltests/m2m_multiple/tests.py
@@ -0,0 +1,84 @@
+from datetime import datetime
+
+from django.test import TestCase
+
+from models import Article, Category
+
+
+class M2MMultipleTests(TestCase):
+ def test_multiple(self):
+ c1, c2, c3, c4 = [
+ Category.objects.create(name=name)
+ for name in ["Sports", "News", "Crime", "Life"]
+ ]
+
+ a1 = Article.objects.create(
+ headline="Area man steals", pub_date=datetime(2005, 11, 27)
+ )
+ a1.primary_categories.add(c2, c3)
+ a1.secondary_categories.add(c4)
+
+ a2 = Article.objects.create(
+ headline="Area man runs", pub_date=datetime(2005, 11, 28)
+ )
+ a2.primary_categories.add(c1, c2)
+ a2.secondary_categories.add(c4)
+
+ self.assertQuerysetEqual(
+ a1.primary_categories.all(), [
+ "Crime",
+ "News",
+ ],
+ lambda c: c.name
+ )
+ self.assertQuerysetEqual(
+ a2.primary_categories.all(), [
+ "News",
+ "Sports",
+ ],
+ lambda c: c.name
+ )
+ self.assertQuerysetEqual(
+ a1.secondary_categories.all(), [
+ "Life",
+ ],
+ lambda c: c.name
+ )
+ self.assertQuerysetEqual(
+ c1.primary_article_set.all(), [
+ "Area man runs",
+ ],
+ lambda a: a.headline
+ )
+ self.assertQuerysetEqual(
+ c1.secondary_article_set.all(), []
+ )
+ self.assertQuerysetEqual(
+ c2.primary_article_set.all(), [
+ "Area man steals",
+ "Area man runs",
+ ],
+ lambda a: a.headline
+ )
+ self.assertQuerysetEqual(
+ c2.secondary_article_set.all(), []
+ )
+ self.assertQuerysetEqual(
+ c3.primary_article_set.all(), [
+ "Area man steals",
+ ],
+ lambda a: a.headline
+ )
+ self.assertQuerysetEqual(
+ c3.secondary_article_set.all(), []
+ )
+ self.assertQuerysetEqual(
+ c4.primary_article_set.all(), []
+ )
+ self.assertQuerysetEqual(
+ c4.secondary_article_set.all(), [
+ "Area man steals",
+ "Area man runs",
+ ],
+ lambda a: a.headline
+ )
diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
index 1087cf8795..7ded82bb7c 100644
--- a/tests/modeltests/model_forms/models.py
+++ b/tests/modeltests/model_forms/models.py
@@ -554,7 +554,7 @@ fields with the 'choices' attribute are represented by a ChoiceField.
<option value="1">Entertainment</option>
<option value="2">It&#39;s a test</option>
<option value="3">Third test</option>
-</select><br /> Hold down "Control", or "Command" on a Mac, to select more than one.</td></tr>
+</select><br /><span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></td></tr>
You can restrict a form to a subset of the complete list of fields
by providing a 'fields' argument. If you try to save a
@@ -579,7 +579,7 @@ inserted as 'initial' data in each Field.
... model = Writer
>>> f = RoykoForm(auto_id=False, instance=w)
>>> print f
-<tr><th>Name:</th><td><input type="text" name="name" value="Mike Royko" maxlength="50" /><br />Use both first and last names.</td></tr>
+<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(headline='Test article', slug='test-article', pub_date=datetime.date(1988, 1, 4), writer=w, article='Hello.')
>>> art.save()
@@ -609,7 +609,7 @@ inserted as 'initial' data in each Field.
<option value="1">Entertainment</option>
<option value="2">It&#39;s a test</option>
<option value="3">Third test</option>
-</select> Hold down "Control", or "Command" on a Mac, to select more than one.</li>
+</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
>>> f = TestArticleForm({'headline': u'Test headline', 'slug': 'test-headline', 'pub_date': u'1984-02-06', 'writer': unicode(w_royko.pk), 'article': 'Hello.'}, instance=art)
>>> f.errors
{}
@@ -672,7 +672,7 @@ Add some categories and test the many-to-many form output.
<option value="1" selected="selected">Entertainment</option>
<option value="2">It&#39;s a test</option>
<option value="3">Third test</option>
-</select> Hold down "Control", or "Command" on a Mac, to select more than one.</li>
+</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
Initial values can be provided for model forms
>>> f = TestArticleForm(auto_id=False, initial={'headline': 'Your headline here', 'categories': ['1','2']})
@@ -696,7 +696,7 @@ Initial values can be provided for model forms
<option value="1" selected="selected">Entertainment</option>
<option value="2" selected="selected">It&#39;s a test</option>
<option value="3">Third test</option>
-</select> Hold down "Control", or "Command" on a Mac, to select more than one.</li>
+</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
>>> f = TestArticleForm({'headline': u'New headline', 'slug': u'new-headline', 'pub_date': u'1988-01-04',
... 'writer': unicode(w_royko.pk), 'article': u'Hello.', 'categories': [u'1', u'2']}, instance=new_art)
@@ -812,7 +812,7 @@ the data in the database when the form is instantiated.
<option value="1">Entertainment</option>
<option value="2">It&#39;s a test</option>
<option value="3">Third</option>
-</select> Hold down "Control", or "Command" on a Mac, to select more than one.</li>
+</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
>>> Category.objects.create(name='Fourth', url='4th')
<Category: Fourth>
>>> Writer.objects.create(name='Carl Bernstein')
@@ -839,7 +839,7 @@ the data in the database when the form is instantiated.
<option value="2">It&#39;s a test</option>
<option value="3">Third</option>
<option value="4">Fourth</option>
-</select> Hold down "Control", or "Command" on a Mac, to select more than one.</li>
+</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
# ModelChoiceField ############################################################
diff --git a/tests/modeltests/model_forms/tests.py b/tests/modeltests/model_forms/tests.py
index 6a5f9395cc..c5647c714f 100644
--- a/tests/modeltests/model_forms/tests.py
+++ b/tests/modeltests/model_forms/tests.py
@@ -156,6 +156,10 @@ class UniqueTest(TestCase):
form = PostForm({'subtitle': "Finally", "title": "Django 1.0 is released",
"slug": "Django 1.0", 'posted': '2008-09-03'}, instance=p)
self.assertTrue(form.is_valid())
+ form = PostForm({'title': "Django 1.0 is released"})
+ self.assertFalse(form.is_valid())
+ self.assertEqual(len(form.errors), 1)
+ self.assertEqual(form.errors['posted'], [u'This field is required.'])
def test_inherited_unique_for_date(self):
p = Post.objects.create(title="Django 1.0 is released",
diff --git a/tests/modeltests/proxy_model_inheritance/tests.py b/tests/modeltests/proxy_model_inheritance/tests.py
index a07958a13b..d10d6a4ac1 100644
--- a/tests/modeltests/proxy_model_inheritance/tests.py
+++ b/tests/modeltests/proxy_model_inheritance/tests.py
@@ -23,9 +23,9 @@ class ProxyModelInheritanceTests(TransactionTestCase):
settings.INSTALLED_APPS = ('app1', 'app2')
map(load_app, settings.INSTALLED_APPS)
call_command('syncdb', verbosity=0)
+ global ProxyModel, NiceModel
from app1.models import ProxyModel
from app2.models import NiceModel
- global ProxyModel, NiceModel
def tearDown(self):
settings.INSTALLED_APPS = self.old_installed_apps
diff --git a/tests/modeltests/signals/models.py b/tests/modeltests/signals/models.py
index ea8137f657..8a500752be 100644
--- a/tests/modeltests/signals/models.py
+++ b/tests/modeltests/signals/models.py
@@ -3,6 +3,7 @@ Testing signals before/after saving and deleting.
"""
from django.db import models
+from django.dispatch import receiver
class Person(models.Model):
first_name = models.CharField(max_length=20)
@@ -11,6 +12,13 @@ class Person(models.Model):
def __unicode__(self):
return u"%s %s" % (self.first_name, self.last_name)
+class Car(models.Model):
+ make = models.CharField(max_length=20)
+ model = models.CharField(max_length=20)
+
+ def __unicode__(self):
+ return u"%s %s" % (self.make, self.model)
+
def pre_save_test(signal, sender, instance, **kwargs):
print 'pre_save signal,', instance
if kwargs.get('raw'):
@@ -52,22 +60,44 @@ __test__ = {'API_TESTS':"""
>>> models.signals.pre_delete.connect(pre_delete_test)
>>> models.signals.post_delete.connect(post_delete_test)
+# throw a decorator syntax receiver into the mix
+>>> @receiver(models.signals.pre_save)
+... def pre_save_decorator_test(signal, sender, instance, **kwargs):
+... print "pre_save signal decorator,", instance
+
+# throw a decorator syntax receiver into the mix
+>>> @receiver(models.signals.pre_save, sender=Car)
+... def pre_save_decorator_sender_test(signal, sender, instance, **kwargs):
+... print "pre_save signal decorator sender,", instance
+
>>> p1 = Person(first_name='John', last_name='Smith')
>>> p1.save()
pre_save signal, John Smith
+pre_save signal decorator, John Smith
post_save signal, John Smith
Is created
>>> p1.first_name = 'Tom'
>>> p1.save()
pre_save signal, Tom Smith
+pre_save signal decorator, Tom Smith
post_save signal, Tom Smith
Is updated
+# Car signal (sender defined)
+>>> c1 = Car(make="Volkswagon", model="Passat")
+>>> c1.save()
+pre_save signal, Volkswagon Passat
+pre_save signal decorator, Volkswagon Passat
+pre_save signal decorator sender, Volkswagon Passat
+post_save signal, Volkswagon Passat
+Is created
+
# Calling an internal method purely so that we can trigger a "raw" save.
>>> p1.save_base(raw=True)
pre_save signal, Tom Smith
Is raw
+pre_save signal decorator, Tom Smith
post_save signal, Tom Smith
Is updated
Is raw
@@ -82,12 +112,14 @@ instance.id is None: False
>>> p2.id = 99999
>>> p2.save()
pre_save signal, James Jones
+pre_save signal decorator, James Jones
post_save signal, James Jones
Is created
>>> p2.id = 99998
>>> p2.save()
pre_save signal, James Jones
+pre_save signal decorator, James Jones
post_save signal, James Jones
Is created
@@ -104,6 +136,8 @@ instance.id is None: False
>>> models.signals.pre_delete.disconnect(pre_delete_test)
>>> models.signals.post_save.disconnect(post_save_test)
>>> models.signals.pre_save.disconnect(pre_save_test)
+>>> models.signals.pre_save.disconnect(pre_save_decorator_test)
+>>> models.signals.pre_save.disconnect(pre_save_decorator_sender_test, sender=Car)
# Check that all our signals got disconnected properly.
>>> post_signals = (len(models.signals.pre_save.receivers),
diff --git a/tests/modeltests/test_client/models.py b/tests/modeltests/test_client/models.py
index c51323d843..30520082da 100644
--- a/tests/modeltests/test_client/models.py
+++ b/tests/modeltests/test_client/models.py
@@ -21,6 +21,7 @@ rather than the HTML rendered to the end-user.
"""
from django.test import Client, TestCase
+from django.conf import settings
from django.core import mail
class ClientTest(TestCase):
@@ -433,3 +434,26 @@ class ClientTest(TestCase):
self.assertEqual(mail.outbox[1].from_email, 'from@example.com')
self.assertEqual(mail.outbox[1].to[0], 'second@example.com')
self.assertEqual(mail.outbox[1].to[1], 'third@example.com')
+
+class CSRFEnabledClientTests(TestCase):
+ def setUp(self):
+ # Enable the CSRF middleware for this test
+ self.old_MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES
+ csrf_middleware_class = 'django.middleware.csrf.CsrfViewMiddleware'
+ if csrf_middleware_class not in settings.MIDDLEWARE_CLASSES:
+ settings.MIDDLEWARE_CLASSES += (csrf_middleware_class,)
+
+ def tearDown(self):
+ settings.MIDDLEWARE_CLASSES = self.old_MIDDLEWARE_CLASSES
+
+ def test_csrf_enabled_client(self):
+ "A client can be instantiated with CSRF checks enabled"
+ csrf_client = Client(enforce_csrf_checks=True)
+
+ # The normal client allows the post
+ response = self.client.post('/test_client/post_view/', {})
+ self.assertEqual(response.status_code, 200)
+
+ # The CSRF-enabled client rejects it
+ response = csrf_client.post('/test_client/post_view/', {})
+ self.assertEqual(response.status_code, 403)
diff --git a/tests/modeltests/validation/test_unique.py b/tests/modeltests/validation/test_unique.py
index 1b966390c4..fb77c4d28c 100644
--- a/tests/modeltests/validation/test_unique.py
+++ b/tests/modeltests/validation/test_unique.py
@@ -40,6 +40,15 @@ class GetUniqueCheckTests(unittest.TestCase):
), m._get_unique_checks()
)
+ def test_unique_for_date_exclusion(self):
+ m = UniqueForDateModel()
+ self.assertEqual((
+ [(UniqueForDateModel, ('id',))],
+ [(UniqueForDateModel, 'year', 'count', 'end_date'),
+ (UniqueForDateModel, 'month', 'order', 'end_date')]
+ ), m._get_unique_checks(exclude='start_date')
+ )
+
class PerformUniqueChecksTest(unittest.TestCase):
def setUp(self):
# Set debug to True to gain access to connection.queries.
diff --git a/tests/regressiontests/admin_changelist/tests.py b/tests/regressiontests/admin_changelist/tests.py
index b70d7c51f4..c8ad1ce8f6 100644
--- a/tests/regressiontests/admin_changelist/tests.py
+++ b/tests/regressiontests/admin_changelist/tests.py
@@ -1,10 +1,10 @@
-import unittest
from django.contrib import admin
from django.contrib.admin.views.main import ChangeList
from django.template import Context, Template
+from django.test import TransactionTestCase
from regressiontests.admin_changelist.models import Child, Parent
-class ChangeListTests(unittest.TestCase):
+class ChangeListTests(TransactionTestCase):
def test_select_related_preserved(self):
"""
Regression test for #10348: ChangeList.get_query_set() shouldn't
@@ -18,9 +18,8 @@ class ChangeListTests(unittest.TestCase):
def test_result_list_html(self):
"""
- Regression test for #11791: Inclusion tag result_list generates a
- table and this checks that the items are nested within the table
- element tags.
+ Verifies that inclusion tag result_list generates a table when with
+ default ModelAdmin settings.
"""
new_parent = Parent.objects.create(name='parent')
new_child = Child.objects.create(name='name', parent=new_parent)
@@ -29,16 +28,27 @@ class ChangeListTests(unittest.TestCase):
cl = ChangeList(request, Child, m.list_display, m.list_display_links,
m.list_filter, m.date_hierarchy, m.search_fields,
m.list_select_related, m.list_per_page, m.list_editable, m)
- FormSet = m.get_changelist_formset(request)
- cl.formset = FormSet(queryset=cl.result_list)
+ cl.formset = None
template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
context = Context({'cl': cl})
table_output = template.render(context)
- hidden_input_elem = '<input type="hidden" name="form-0-id" value="1" id="id_form-0-id" />'
- self.failIf(table_output.find(hidden_input_elem) == -1,
- 'Failed to find expected hidden input element in: %s' % table_output)
- self.failIf(table_output.find('<td>%s</td>' % hidden_input_elem) == -1,
- 'Hidden input element is not enclosed in <td> element.')
+ row_html = '<tbody><tr class="row1"><td><input type="checkbox" class="action-select" value="1" name="_selected_action" /></td><th><a href="1/">name</a></th><td>Parent object</td></tr></tbody>'
+ self.failIf(table_output.find(row_html) == -1,
+ 'Failed to find expected row element: %s' % table_output)
+
+ def test_result_list_editable_html(self):
+ """
+ Regression tests for #11791: Inclusion tag result_list generates a
+ table and this checks that the items are nested within the table
+ element tags.
+ Also a regression test for #13599, verifies that hidden fields
+ when list_editable is enabled are rendered in a div outside the
+ table.
+ """
+ new_parent = Parent.objects.create(name='parent')
+ new_child = Child.objects.create(name='name', parent=new_parent)
+ request = MockRequest()
+ m = ChildAdmin(Child, admin.site)
# Test with list_editable fields
m.list_display = ['id', 'name', 'parent']
@@ -52,10 +62,14 @@ class ChangeListTests(unittest.TestCase):
template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
context = Context({'cl': cl})
table_output = template.render(context)
- self.failIf(table_output.find(hidden_input_elem) == -1,
- 'Failed to find expected hidden input element in: %s' % table_output)
- self.failIf(table_output.find('<td>%s</td>' % hidden_input_elem) == -1,
- 'Hidden input element is not enclosed in <td> element.')
+ # make sure that hidden fields are in the correct place
+ hiddenfields_div = '<div class="hiddenfields"><input type="hidden" name="form-0-id" value="1" id="id_form-0-id" /></div>'
+ self.failIf(table_output.find(hiddenfields_div) == -1,
+ 'Failed to find hidden fields in: %s' % table_output)
+ # make sure that list editable fields are rendered in divs correctly
+ editable_name_field = '<input name="form-0-name" value="name" class="vTextField" maxlength="30" type="text" id="id_form-0-name" />'
+ self.failIf('<td>%s</td>' % editable_name_field == -1,
+ 'Failed to find "name" list_editable field in: %s' % table_output)
class ChildAdmin(admin.ModelAdmin):
list_display = ['name', 'parent']
diff --git a/tests/regressiontests/admin_scripts/tests.py b/tests/regressiontests/admin_scripts/tests.py
index 7ec2454561..3dd8ad5d13 100644
--- a/tests/regressiontests/admin_scripts/tests.py
+++ b/tests/regressiontests/admin_scripts/tests.py
@@ -133,7 +133,7 @@ class AdminScriptTestCase(unittest.TestCase):
return out, err
def run_django_admin(self, args, settings_file=None):
- bin_dir = os.path.dirname(bin.__file__)
+ bin_dir = os.path.abspath(os.path.dirname(bin.__file__))
return self.run_test(os.path.join(bin_dir,'django-admin.py'), args, settings_file)
def run_manage(self, args, settings_file=None):
diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py
index a2700ba747..b25a9b9a96 100644
--- a/tests/regressiontests/admin_views/models.py
+++ b/tests/regressiontests/admin_views/models.py
@@ -10,6 +10,7 @@ from django.core.mail import EmailMessage
from django.db import models
from django import forms
from django.forms.models import BaseModelFormSet
+from django.contrib.auth.models import User
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
@@ -579,6 +580,10 @@ class Pizza(models.Model):
class PizzaAdmin(admin.ModelAdmin):
readonly_fields = ('toppings',)
+class Album(models.Model):
+ owner = models.ForeignKey(User)
+ title = models.CharField(max_length=30)
+
admin.site.register(Article, ArticleAdmin)
admin.site.register(CustomArticle, CustomArticleAdmin)
admin.site.register(Section, save_as=True, inlines=[ArticleInline])
@@ -625,3 +630,4 @@ admin.site.register(Promo)
admin.site.register(ChapterXtra1)
admin.site.register(Pizza, PizzaAdmin)
admin.site.register(Topping)
+admin.site.register(Album)
diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
index 1385e5e0aa..725369a5b1 100644
--- a/tests/regressiontests/admin_views/tests.py
+++ b/tests/regressiontests/admin_views/tests.py
@@ -604,6 +604,28 @@ class AdminViewPermissionsTest(TestCase):
'Plural error message not found in response to post with multiple errors.')
self.client.get('/test_admin/admin/logout/')
+ def testConditionallyShowAddSectionLink(self):
+ """
+ The foreign key widget should only show the "add related" button if the
+ user has permission to add that related item.
+ """
+ # Set up and log in user.
+ url = '/test_admin/admin/admin_views/article/add/'
+ add_link_text = ' class="add-another"'
+ self.client.get('/test_admin/admin/')
+ self.client.post('/test_admin/admin/', self.adduser_login)
+ # The add user can't add sections yet, so they shouldn't see the "add
+ # section" link.
+ response = self.client.get(url)
+ self.assertNotContains(response, add_link_text)
+ # Allow the add user to add sections too. Now they can see the "add
+ # section" link.
+ add_user = User.objects.get(username='adduser')
+ perm = get_perm(Section, Section._meta.get_add_permission())
+ add_user.user_permissions.add(perm)
+ response = self.client.get(url)
+ self.assertContains(response, add_link_text)
+
def testCustomModelAdminTemplates(self):
self.client.get('/test_admin/admin/')
self.client.post('/test_admin/admin/', self.super_login)
@@ -1477,6 +1499,21 @@ class AdminActionsTest(TestCase):
response = self.client.post('/test_admin/admin/admin_views/externalsubscriber/', action_data)
self.failUnlessEqual(response.status_code, 302)
+ def test_default_redirect(self):
+ """
+ Test that actions which don't return an HttpResponse are redirected to
+ the same page, retaining the querystring (which may contain changelist
+ information).
+ """
+ action_data = {
+ ACTION_CHECKBOX_NAME: [1],
+ 'action' : 'external_mail',
+ 'index': 0,
+ }
+ url = '/test_admin/admin/admin_views/externalsubscriber/?ot=asc&o=1'
+ response = self.client.post(url, action_data)
+ self.assertRedirects(response, url)
+
def test_model_without_action(self):
"Tests a ModelAdmin without any action"
response = self.client.get('/test_admin/admin/admin_views/oldsubscriber/')
@@ -2113,11 +2150,9 @@ class ReadonlyTest(TestCase):
response = self.client.get('/test_admin/admin/admin_views/pizza/add/')
self.assertEqual(response.status_code, 200)
-class IncompleteFormTest(TestCase):
+class UserAdminTest(TestCase):
"""
- Tests validation of a ModelForm that doesn't explicitly have all data
- corresponding to model fields. Model validation shouldn't fail
- such a forms.
+ Tests user CRUD functionality.
"""
fixtures = ['admin-views-users.xml']
@@ -2128,6 +2163,7 @@ class IncompleteFormTest(TestCase):
self.client.logout()
def test_user_creation(self):
+ user_count = User.objects.count()
response = self.client.post('/test_admin/admin/auth/user/add/', {
'username': 'newuser',
'password1': 'newpassword',
@@ -2136,6 +2172,7 @@ class IncompleteFormTest(TestCase):
})
new_user = User.objects.order_by('-id')[0]
self.assertRedirects(response, '/test_admin/admin/auth/user/%s/' % new_user.pk)
+ self.assertEquals(User.objects.count(), user_count + 1)
self.assertNotEquals(new_user.password, UNUSABLE_PASSWORD)
def test_password_mismatch(self):
@@ -2149,3 +2186,71 @@ class IncompleteFormTest(TestCase):
self.assert_('password' not in adminform.form.errors)
self.assertEquals(adminform.form.errors['password2'],
[u"The two password fields didn't match."])
+
+ def test_user_fk_popup(self):
+ response = self.client.get('/test_admin/admin/admin_views/album/add/')
+ self.failUnlessEqual(response.status_code, 200)
+ self.assertContains(response, '/test_admin/admin/auth/user/add')
+ self.assertContains(response, 'class="add-another" id="add_id_owner" onclick="return showAddAnotherPopup(this);"')
+ response = self.client.get('/test_admin/admin/auth/user/add/?_popup=1')
+ self.assertNotContains(response, 'name="_continue"')
+
+ def test_user_add_another(self):
+ user_count = User.objects.count()
+ response = self.client.post('/test_admin/admin/auth/user/add/', {
+ 'username': 'newuser',
+ 'password1': 'newpassword',
+ 'password2': 'newpassword',
+ '_addanother': '1',
+ })
+ new_user = User.objects.order_by('-id')[0]
+ self.assertRedirects(response, '/test_admin/admin/auth/user/add/')
+ self.assertEquals(User.objects.count(), user_count + 1)
+ self.assertNotEquals(new_user.password, UNUSABLE_PASSWORD)
+
+try:
+ # If docutils isn't installed, skip the AdminDocs tests.
+ import docutils
+
+ class AdminDocsTest(TestCase):
+ fixtures = ['admin-views-users.xml']
+
+ def setUp(self):
+ self.client.login(username='super', password='secret')
+
+ def tearDown(self):
+ self.client.logout()
+
+ def test_tags(self):
+ response = self.client.get('/test_admin/admin/doc/tags/')
+
+ # The builtin tag group exists
+ self.assertContains(response, "<h2>Built-in tags</h2>", count=2)
+
+ # A builtin tag exists in both the index and detail
+ self.assertContains(response, '<h3 id="built_in-autoescape">autoescape</h3>')
+ self.assertContains(response, '<li><a href="#built_in-autoescape">autoescape</a></li>')
+
+ # An app tag exists in both the index and detail
+ self.assertContains(response, '<h3 id="flatpages-get_flatpages">get_flatpages</h3>')
+ self.assertContains(response, '<li><a href="#flatpages-get_flatpages">get_flatpages</a></li>')
+
+ # The admin list tag group exists
+ self.assertContains(response, "<h2>admin_list</h2>", count=2)
+
+ # An admin list tag exists in both the index and detail
+ self.assertContains(response, '<h3 id="admin_list-admin_actions">admin_actions</h3>')
+ self.assertContains(response, '<li><a href="#admin_list-admin_actions">admin_actions</a></li>')
+
+ def test_filters(self):
+ response = self.client.get('/test_admin/admin/doc/filters/')
+
+ # The builtin filter group exists
+ self.assertContains(response, "<h2>Built-in filters</h2>", count=2)
+
+ # A builtin filter exists in both the index and detail
+ self.assertContains(response, '<h3 id="built_in-add">add</h3>')
+ self.assertContains(response, '<li><a href="#built_in-add">add</a></li>')
+
+except ImportError:
+ pass
diff --git a/tests/regressiontests/admin_widgets/tests.py b/tests/regressiontests/admin_widgets/tests.py
index fd0c25ca1a..c445644335 100644
--- a/tests/regressiontests/admin_widgets/tests.py
+++ b/tests/regressiontests/admin_widgets/tests.py
@@ -1,3 +1,5 @@
+# encoding: utf-8
+
from django import forms
from django.contrib import admin
from django.contrib.admin import widgets
@@ -151,3 +153,13 @@ class AdminForeignKeyRawIdWidget(DjangoTestCase):
post_data)
self.assertContains(response,
'Select a valid choice. That choice is not one of the available choices.')
+
+ def test_invalid_target_id(self):
+
+ for test_str in ('Iñtërnâtiônàlizætiøn', "1234'", -1234):
+ # This should result in an error message, not a server exception.
+ response = self.client.post('%s/admin_widgets/event/add/' % self.admin_root,
+ {"band": test_str})
+
+ self.assertContains(response,
+ 'Select a valid choice. That choice is not one of the available choices.')
diff --git a/tests/regressiontests/aggregation_regress/models.py b/tests/regressiontests/aggregation_regress/models.py
index ba74357534..783c21956a 100644
--- a/tests/regressiontests/aggregation_regress/models.py
+++ b/tests/regressiontests/aggregation_regress/models.py
@@ -4,6 +4,7 @@ import pickle
from django.db import connection, models, DEFAULT_DB_ALIAS
from django.conf import settings
+
class Author(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
@@ -12,6 +13,7 @@ class Author(models.Model):
def __unicode__(self):
return self.name
+
class Publisher(models.Model):
name = models.CharField(max_length=255)
num_awards = models.IntegerField()
@@ -19,6 +21,7 @@ class Publisher(models.Model):
def __unicode__(self):
return self.name
+
class Book(models.Model):
isbn = models.CharField(max_length=9)
name = models.CharField(max_length=255)
@@ -36,6 +39,7 @@ class Book(models.Model):
def __unicode__(self):
return self.name
+
class Store(models.Model):
name = models.CharField(max_length=255)
books = models.ManyToManyField(Book)
@@ -45,334 +49,21 @@ class Store(models.Model):
def __unicode__(self):
return self.name
+
class Entries(models.Model):
EntryID = models.AutoField(primary_key=True, db_column='Entry ID')
Entry = models.CharField(unique=True, max_length=50)
Exclude = models.BooleanField()
+
class Clues(models.Model):
ID = models.AutoField(primary_key=True)
EntryID = models.ForeignKey(Entries, verbose_name='Entry', db_column = 'Entry ID')
Clue = models.CharField(max_length=150)
+
class HardbackBook(Book):
weight = models.FloatField()
def __unicode__(self):
return "%s (hardback): %s" % (self.name, self.weight)
-
-__test__ = {'API_TESTS': """
->>> from django.core import management
->>> from django.db.models import get_app, F
-
-# Reset the database representation of this app.
-# This will return the database to a clean initial state.
->>> management.call_command('flush', verbosity=0, interactive=False)
-
->>> from django.db.models import Avg, Sum, Count, Max, Min, StdDev, Variance
-
-# Ordering requests are ignored
->>> Author.objects.all().order_by('name').aggregate(Avg('age'))
-{'age__avg': 37.4...}
-
-# Implicit ordering is also ignored
->>> Book.objects.all().aggregate(Sum('pages'))
-{'pages__sum': 3703}
-
-# Baseline results
->>> Book.objects.all().aggregate(Sum('pages'), Avg('pages'))
-{'pages__sum': 3703, 'pages__avg': 617.1...}
-
-# Empty values query doesn't affect grouping or results
->>> Book.objects.all().values().aggregate(Sum('pages'), Avg('pages'))
-{'pages__sum': 3703, 'pages__avg': 617.1...}
-
-# Aggregate overrides extra selected column
->>> Book.objects.all().extra(select={'price_per_page' : 'price / pages'}).aggregate(Sum('pages'))
-{'pages__sum': 3703}
-
-# Annotations get combined with extra select clauses
->>> sorted((k,v) for k,v in Book.objects.all().annotate(mean_auth_age=Avg('authors__age')).extra(select={'manufacture_cost' : 'price * .5'}).get(pk=2).__dict__.items() if k != '_state')
-[('contact_id', 3), ('id', 2), ('isbn', u'067232959'), ('manufacture_cost', ...11.545...), ('mean_auth_age', 45.0), ('name', u'Sams Teach Yourself Django in 24 Hours'), ('pages', 528), ('price', Decimal("23.09")), ('pubdate', datetime.date(2008, 3, 3)), ('publisher_id', 2), ('rating', 3.0)]
-
-# Order of the annotate/extra in the query doesn't matter
->>> sorted((k,v) for k,v in Book.objects.all().extra(select={'manufacture_cost' : 'price * .5'}).annotate(mean_auth_age=Avg('authors__age')).get(pk=2).__dict__.items()if k != '_state')
-[('contact_id', 3), ('id', 2), ('isbn', u'067232959'), ('manufacture_cost', ...11.545...), ('mean_auth_age', 45.0), ('name', u'Sams Teach Yourself Django in 24 Hours'), ('pages', 528), ('price', Decimal("23.09")), ('pubdate', datetime.date(2008, 3, 3)), ('publisher_id', 2), ('rating', 3.0)]
-
-# Values queries can be combined with annotate and extra
->>> sorted((k,v) for k,v in Book.objects.all().annotate(mean_auth_age=Avg('authors__age')).extra(select={'manufacture_cost' : 'price * .5'}).values().get(pk=2).items()if k != '_state')
-[('contact_id', 3), ('id', 2), ('isbn', u'067232959'), ('manufacture_cost', ...11.545...), ('mean_auth_age', 45.0), ('name', u'Sams Teach Yourself Django in 24 Hours'), ('pages', 528), ('price', Decimal("23.09")), ('pubdate', datetime.date(2008, 3, 3)), ('publisher_id', 2), ('rating', 3.0)]
-
-# The order of the (empty) values, annotate and extra clauses doesn't matter
->>> sorted((k,v) for k,v in Book.objects.all().values().annotate(mean_auth_age=Avg('authors__age')).extra(select={'manufacture_cost' : 'price * .5'}).get(pk=2).items()if k != '_state')
-[('contact_id', 3), ('id', 2), ('isbn', u'067232959'), ('manufacture_cost', ...11.545...), ('mean_auth_age', 45.0), ('name', u'Sams Teach Yourself Django in 24 Hours'), ('pages', 528), ('price', Decimal("23.09")), ('pubdate', datetime.date(2008, 3, 3)), ('publisher_id', 2), ('rating', 3.0)]
-
-# If the annotation precedes the values clause, it won't be included
-# unless it is explicitly named
->>> sorted(Book.objects.all().annotate(mean_auth_age=Avg('authors__age')).extra(select={'price_per_page' : 'price / pages'}).values('name').get(pk=1).items())
-[('name', u'The Definitive Guide to Django: Web Development Done Right')]
-
->>> sorted(Book.objects.all().annotate(mean_auth_age=Avg('authors__age')).extra(select={'price_per_page' : 'price / pages'}).values('name','mean_auth_age').get(pk=1).items())
-[('mean_auth_age', 34.5), ('name', u'The Definitive Guide to Django: Web Development Done Right')]
-
-# If an annotation isn't included in the values, it can still be used in a filter
->>> Book.objects.annotate(n_authors=Count('authors')).values('name').filter(n_authors__gt=2)
-[{'name': u'Python Web Development with Django'}]
-
-# The annotations are added to values output if values() precedes annotate()
->>> sorted(Book.objects.all().values('name').annotate(mean_auth_age=Avg('authors__age')).extra(select={'price_per_page' : 'price / pages'}).get(pk=1).items())
-[('mean_auth_age', 34.5), ('name', u'The Definitive Guide to Django: Web Development Done Right')]
-
-# Check that all of the objects are getting counted (allow_nulls) and that values respects the amount of objects
->>> len(Author.objects.all().annotate(Avg('friends__age')).values())
-9
-
-# Check that consecutive calls to annotate accumulate in the query
->>> Book.objects.values('price').annotate(oldest=Max('authors__age')).order_by('oldest', 'price').annotate(Max('publisher__num_awards'))
-[{'price': Decimal("30..."), 'oldest': 35, 'publisher__num_awards__max': 3}, {'price': Decimal("29.69"), 'oldest': 37, 'publisher__num_awards__max': 7}, {'price': Decimal("23.09"), 'oldest': 45, 'publisher__num_awards__max': 1}, {'price': Decimal("75..."), 'oldest': 57, 'publisher__num_awards__max': 9}, {'price': Decimal("82.8..."), 'oldest': 57, 'publisher__num_awards__max': 7}]
-
-# Aggregates can be composed over annotations.
-# The return type is derived from the composed aggregate
->>> Book.objects.all().annotate(num_authors=Count('authors__id')).aggregate(Max('pages'), Max('price'), Sum('num_authors'), Avg('num_authors'))
-{'num_authors__sum': 10, 'num_authors__avg': 1.66..., 'pages__max': 1132, 'price__max': Decimal("82.80")}
-
-# Bad field requests in aggregates are caught and reported
->>> Book.objects.all().aggregate(num_authors=Count('foo'))
-Traceback (most recent call last):
-...
-FieldError: Cannot resolve keyword 'foo' into field. Choices are: authors, contact, hardbackbook, id, isbn, name, pages, price, pubdate, publisher, rating, store
-
->>> Book.objects.all().annotate(num_authors=Count('foo'))
-Traceback (most recent call last):
-...
-FieldError: Cannot resolve keyword 'foo' into field. Choices are: authors, contact, hardbackbook, id, isbn, name, pages, price, pubdate, publisher, rating, store
-
->>> Book.objects.all().annotate(num_authors=Count('authors__id')).aggregate(Max('foo'))
-Traceback (most recent call last):
-...
-FieldError: Cannot resolve keyword 'foo' into field. Choices are: authors, contact, hardbackbook, id, isbn, name, pages, price, pubdate, publisher, rating, store, num_authors
-
-# Old-style count aggregations can be mixed with new-style
->>> Book.objects.annotate(num_authors=Count('authors')).count()
-6
-
-# Non-ordinal, non-computed Aggregates over annotations correctly inherit
-# the annotation's internal type if the annotation is ordinal or computed
->>> Book.objects.annotate(num_authors=Count('authors')).aggregate(Max('num_authors'))
-{'num_authors__max': 3}
-
->>> Publisher.objects.annotate(avg_price=Avg('book__price')).aggregate(Max('avg_price'))
-{'avg_price__max': 75.0...}
-
-# Aliases are quoted to protected aliases that might be reserved names
->>> Book.objects.aggregate(number=Max('pages'), select=Max('pages'))
-{'number': 1132, 'select': 1132}
-
-# Regression for #10064: select_related() plays nice with aggregates
->>> sorted(Book.objects.select_related('publisher').annotate(num_authors=Count('authors')).values()[0].iteritems())
-[('contact_id', 8), ('id', 5), ('isbn', u'013790395'), ('name', u'Artificial Intelligence: A Modern Approach'), ('num_authors', 2), ('pages', 1132), ('price', Decimal("82.8...")), ('pubdate', datetime.date(1995, 1, 15)), ('publisher_id', 3), ('rating', 4.0)]
-
-# Regression for #10010: exclude on an aggregate field is correctly negated
->>> len(Book.objects.annotate(num_authors=Count('authors')))
-6
->>> len(Book.objects.annotate(num_authors=Count('authors')).filter(num_authors__gt=2))
-1
->>> len(Book.objects.annotate(num_authors=Count('authors')).exclude(num_authors__gt=2))
-5
-
->>> len(Book.objects.annotate(num_authors=Count('authors')).filter(num_authors__lt=3).exclude(num_authors__lt=2))
-2
->>> len(Book.objects.annotate(num_authors=Count('authors')).exclude(num_authors__lt=2).filter(num_authors__lt=3))
-2
-
-# Aggregates can be used with F() expressions
-# ... where the F() is pushed into the HAVING clause
->>> Publisher.objects.annotate(num_books=Count('book')).filter(num_books__lt=F('num_awards')/2).order_by('name').values('name','num_books','num_awards')
-[{'num_books': 1, 'name': u'Morgan Kaufmann', 'num_awards': 9}, {'num_books': 2, 'name': u'Prentice Hall', 'num_awards': 7}]
-
->>> Publisher.objects.annotate(num_books=Count('book')).exclude(num_books__lt=F('num_awards')/2).order_by('name').values('name','num_books','num_awards')
-[{'num_books': 2, 'name': u'Apress', 'num_awards': 3}, {'num_books': 0, 'name': u"Jonno's House of Books", 'num_awards': 0}, {'num_books': 1, 'name': u'Sams', 'num_awards': 1}]
-
-# ... and where the F() references an aggregate
->>> Publisher.objects.annotate(num_books=Count('book')).filter(num_awards__gt=2*F('num_books')).order_by('name').values('name','num_books','num_awards')
-[{'num_books': 1, 'name': u'Morgan Kaufmann', 'num_awards': 9}, {'num_books': 2, 'name': u'Prentice Hall', 'num_awards': 7}]
-
->>> Publisher.objects.annotate(num_books=Count('book')).exclude(num_books__lt=F('num_awards')/2).order_by('name').values('name','num_books','num_awards')
-[{'num_books': 2, 'name': u'Apress', 'num_awards': 3}, {'num_books': 0, 'name': u"Jonno's House of Books", 'num_awards': 0}, {'num_books': 1, 'name': u'Sams', 'num_awards': 1}]
-
-# Tests on fields with non-default table and column names.
->>> Clues.objects.values('EntryID__Entry').annotate(Appearances=Count('EntryID'), Distinct_Clues=Count('Clue', distinct=True))
-[]
-
->>> Entries.objects.annotate(clue_count=Count('clues__ID'))
-[]
-
-# Regression for #10089: Check handling of empty result sets with aggregates
->>> Book.objects.filter(id__in=[]).count()
-0
-
->>> Book.objects.filter(id__in=[]).aggregate(num_authors=Count('authors'), avg_authors=Avg('authors'), max_authors=Max('authors'), max_price=Max('price'), max_rating=Max('rating'))
-{'max_authors': None, 'max_rating': None, 'num_authors': 0, 'avg_authors': None, 'max_price': None}
-
->>> list(Publisher.objects.filter(pk=5).annotate(num_authors=Count('book__authors'), avg_authors=Avg('book__authors'), max_authors=Max('book__authors'), max_price=Max('book__price'), max_rating=Max('book__rating')).values()) == [{'max_authors': None, 'name': u"Jonno's House of Books", 'num_awards': 0, 'max_price': None, 'num_authors': 0, 'max_rating': None, 'id': 5, 'avg_authors': None}]
-True
-
-# Regression for #10113 - Fields mentioned in order_by() must be included in the GROUP BY.
-# This only becomes a problem when the order_by introduces a new join.
->>> Book.objects.annotate(num_authors=Count('authors')).order_by('publisher__name', 'name')
-[<Book: Practical Django Projects>, <Book: The Definitive Guide to Django: Web Development Done Right>, <Book: Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp>, <Book: Artificial Intelligence: A Modern Approach>, <Book: Python Web Development with Django>, <Book: Sams Teach Yourself Django in 24 Hours>]
-
-# Regression for #10127 - Empty select_related() works with annotate
->>> books = Book.objects.all().filter(rating__lt=4.5).select_related().annotate(Avg('authors__age'))
->>> sorted([(b.name, b.authors__age__avg, b.publisher.name, b.contact.name) for b in books])
-[(u'Artificial Intelligence: A Modern Approach', 51.5, u'Prentice Hall', u'Peter Norvig'), (u'Practical Django Projects', 29.0, u'Apress', u'James Bennett'), (u'Python Web Development with Django', 30.3..., u'Prentice Hall', u'Jeffrey Forcier'), (u'Sams Teach Yourself Django in 24 Hours', 45.0, u'Sams', u'Brad Dayley')]
-
-# Regression for #10132 - If the values() clause only mentioned extra(select=) columns, those columns are used for grouping
->>> Book.objects.extra(select={'pub':'publisher_id'}).values('pub').annotate(Count('id')).order_by('pub')
-[{'pub': 1, 'id__count': 2}, {'pub': 2, 'id__count': 1}, {'pub': 3, 'id__count': 2}, {'pub': 4, 'id__count': 1}]
-
->>> Book.objects.extra(select={'pub':'publisher_id','foo':'pages'}).values('pub').annotate(Count('id')).order_by('pub')
-[{'pub': 1, 'id__count': 2}, {'pub': 2, 'id__count': 1}, {'pub': 3, 'id__count': 2}, {'pub': 4, 'id__count': 1}]
-
-# Regression for #10182 - Queries with aggregate calls are correctly realiased when used in a subquery
->>> ids = Book.objects.filter(pages__gt=100).annotate(n_authors=Count('authors')).filter(n_authors__gt=2).order_by('n_authors')
->>> Book.objects.filter(id__in=ids)
-[<Book: Python Web Development with Django>]
-
-# Regression for #10197 -- Queries with aggregates can be pickled.
-# First check that pickling is possible at all. No crash = success
->>> qs = Book.objects.annotate(num_authors=Count('authors'))
->>> out = pickle.dumps(qs)
-
-# Then check that the round trip works.
->>> query = qs.query.get_compiler(qs.db).as_sql()[0]
->>> select_fields = qs.query.select_fields
->>> query2 = pickle.loads(pickle.dumps(qs))
->>> query2.query.get_compiler(query2.db).as_sql()[0] == query
-True
->>> query2.query.select_fields = select_fields
-
-# Regression for #10199 - Aggregate calls clone the original query so the original query can still be used
->>> books = Book.objects.all()
->>> _ = books.aggregate(Avg('authors__age'))
->>> books.all()
-[<Book: Artificial Intelligence: A Modern Approach>, <Book: Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp>, <Book: Practical Django Projects>, <Book: Python Web Development with Django>, <Book: Sams Teach Yourself Django in 24 Hours>, <Book: The Definitive Guide to Django: Web Development Done Right>]
-
-# Regression for #10248 - Annotations work with DateQuerySets
->>> Book.objects.annotate(num_authors=Count('authors')).filter(num_authors=2).dates('pubdate', 'day')
-[datetime.datetime(1995, 1, 15, 0, 0), datetime.datetime(2007, 12, 6, 0, 0)]
-
-# Regression for #10290 - extra selects with parameters can be used for
-# grouping.
->>> qs = Book.objects.all().annotate(mean_auth_age=Avg('authors__age')).extra(select={'sheets' : '(pages + %s) / %s'}, select_params=[1, 2]).order_by('sheets').values('sheets')
->>> [int(x['sheets']) for x in qs]
-[150, 175, 224, 264, 473, 566]
-
-# Regression for 10425 - annotations don't get in the way of a count() clause
->>> Book.objects.values('publisher').annotate(Count('publisher')).count()
-4
-
->>> Book.objects.annotate(Count('publisher')).values('publisher').count()
-6
-
->>> publishers = Publisher.objects.filter(id__in=(1,2))
->>> publishers
-[<Publisher: Apress>, <Publisher: Sams>]
-
->>> publishers = publishers.annotate(n_books=models.Count('book'))
->>> publishers[0].n_books
-2
-
->>> publishers
-[<Publisher: Apress>, <Publisher: Sams>]
-
->>> books = Book.objects.filter(publisher__in=publishers)
->>> books
-[<Book: Practical Django Projects>, <Book: Sams Teach Yourself Django in 24 Hours>, <Book: The Definitive Guide to Django: Web Development Done Right>]
-
->>> publishers
-[<Publisher: Apress>, <Publisher: Sams>]
-
-
-# Regression for 10666 - inherited fields work with annotations and aggregations
->>> HardbackBook.objects.aggregate(n_pages=Sum('book_ptr__pages'))
-{'n_pages': 2078}
-
->>> HardbackBook.objects.aggregate(n_pages=Sum('pages'))
-{'n_pages': 2078}
-
->>> HardbackBook.objects.annotate(n_authors=Count('book_ptr__authors')).values('name','n_authors')
-[{'n_authors': 2, 'name': u'Artificial Intelligence: A Modern Approach'}, {'n_authors': 1, 'name': u'Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp'}]
-
->>> HardbackBook.objects.annotate(n_authors=Count('authors')).values('name','n_authors')
-[{'n_authors': 2, 'name': u'Artificial Intelligence: A Modern Approach'}, {'n_authors': 1, 'name': u'Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp'}]
-
-# Regression for #10766 - Shouldn't be able to reference an aggregate fields in an an aggregate() call.
->>> Book.objects.all().annotate(mean_age=Avg('authors__age')).annotate(Avg('mean_age'))
-Traceback (most recent call last):
-...
-FieldError: Cannot compute Avg('mean_age'): 'mean_age' is an aggregate
-
-"""
-}
-
-def run_stddev_tests():
- """Check to see if StdDev/Variance tests should be run.
-
- Stddev and Variance are not guaranteed to be available for SQLite, and
- are not available for PostgreSQL before 8.2.
- """
- if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] == 'django.db.backends.sqlite3':
- return False
-
- class StdDevPop(object):
- sql_function = 'STDDEV_POP'
-
- try:
- connection.ops.check_aggregate_support(StdDevPop())
- except:
- return False
- return True
-
-if run_stddev_tests():
- __test__['API_TESTS'] += """
->>> Book.objects.aggregate(StdDev('pages'))
-{'pages__stddev': 311.46...}
-
->>> Book.objects.aggregate(StdDev('rating'))
-{'rating__stddev': 0.60...}
-
->>> Book.objects.aggregate(StdDev('price'))
-{'price__stddev': 24.16...}
-
-
->>> Book.objects.aggregate(StdDev('pages', sample=True))
-{'pages__stddev': 341.19...}
-
->>> Book.objects.aggregate(StdDev('rating', sample=True))
-{'rating__stddev': 0.66...}
-
->>> Book.objects.aggregate(StdDev('price', sample=True))
-{'price__stddev': 26.46...}
-
-
->>> Book.objects.aggregate(Variance('pages'))
-{'pages__variance': 97010.80...}
-
->>> Book.objects.aggregate(Variance('rating'))
-{'rating__variance': 0.36...}
-
->>> Book.objects.aggregate(Variance('price'))
-{'price__variance': 583.77...}
-
-
->>> Book.objects.aggregate(Variance('pages', sample=True))
-{'pages__variance': 116412.96...}
-
->>> Book.objects.aggregate(Variance('rating', sample=True))
-{'rating__variance': 0.44...}
-
->>> Book.objects.aggregate(Variance('price', sample=True))
-{'price__variance': 700.53...}
-
-"""
diff --git a/tests/regressiontests/aggregation_regress/tests.py b/tests/regressiontests/aggregation_regress/tests.py
index 3c4bdfa47d..51f439c5a1 100644
--- a/tests/regressiontests/aggregation_regress/tests.py
+++ b/tests/regressiontests/aggregation_regress/tests.py
@@ -1,12 +1,38 @@
+import datetime
+from decimal import Decimal
+
+from django.core.exceptions import FieldError
from django.conf import settings
-from django.test import TestCase
+from django.test import TestCase, Approximate
from django.db import DEFAULT_DB_ALIAS
-from django.db.models import Count, Max
+from django.db.models import Count, Max, Avg, Sum, StdDev, Variance, F
from regressiontests.aggregation_regress.models import *
+def run_stddev_tests():
+ """Check to see if StdDev/Variance tests should be run.
+
+ Stddev and Variance are not guaranteed to be available for SQLite, and
+ are not available for PostgreSQL before 8.2.
+ """
+ if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] == 'django.db.backends.sqlite3':
+ return False
+
+ class StdDevPop(object):
+ sql_function = 'STDDEV_POP'
+
+ try:
+ connection.ops.check_aggregate_support(StdDevPop())
+ except:
+ return False
+ return True
+
+
class AggregationTests(TestCase):
+ def assertObjectAttrs(self, obj, **kwargs):
+ for attr, value in kwargs.iteritems():
+ self.assertEqual(getattr(obj, attr), value)
def test_aggregates_in_where_clause(self):
"""
@@ -70,3 +96,593 @@ class AggregationTests(TestCase):
}).annotate(total_books=Count('book'))
# force execution of the query
list(qs)
+
+ def test_aggregate(self):
+ # Ordering requests are ignored
+ self.assertEqual(
+ Author.objects.order_by("name").aggregate(Avg("age")),
+ {"age__avg": Approximate(37.444, places=1)}
+ )
+
+ # Implicit ordering is also ignored
+ self.assertEqual(
+ Book.objects.aggregate(Sum("pages")),
+ {"pages__sum": 3703},
+ )
+
+ # Baseline results
+ self.assertEqual(
+ Book.objects.aggregate(Sum('pages'), Avg('pages')),
+ {'pages__sum': 3703, 'pages__avg': Approximate(617.166, places=2)}
+ )
+
+ # Empty values query doesn't affect grouping or results
+ self.assertEqual(
+ Book.objects.values().aggregate(Sum('pages'), Avg('pages')),
+ {'pages__sum': 3703, 'pages__avg': Approximate(617.166, places=2)}
+ )
+
+ # Aggregate overrides extra selected column
+ self.assertEqual(
+ Book.objects.extra(select={'price_per_page' : 'price / pages'}).aggregate(Sum('pages')),
+ {'pages__sum': 3703}
+ )
+
+ def test_annotation(self):
+ # Annotations get combined with extra select clauses
+ obj = Book.objects.annotate(mean_auth_age=Avg("authors__age")).extra(select={"manufacture_cost": "price * .5"}).get(pk=2)
+ self.assertObjectAttrs(obj,
+ contact_id=3,
+ id=2,
+ isbn=u'067232959',
+ mean_auth_age=45.0,
+ name='Sams Teach Yourself Django in 24 Hours',
+ pages=528,
+ price=Decimal("23.09"),
+ pubdate=datetime.date(2008, 3, 3),
+ publisher_id=2,
+ rating=3.0
+ )
+ # Different DB backends return different types for the extra select computation
+ self.assertTrue(obj.manufacture_cost == 11.545 or obj.manufacture_cost == Decimal('11.545'))
+
+ # Order of the annotate/extra in the query doesn't matter
+ obj = Book.objects.extra(select={'manufacture_cost' : 'price * .5'}).annotate(mean_auth_age=Avg('authors__age')).get(pk=2)
+ self.assertObjectAttrs(obj,
+ contact_id=3,
+ id=2,
+ isbn=u'067232959',
+ mean_auth_age=45.0,
+ name=u'Sams Teach Yourself Django in 24 Hours',
+ pages=528,
+ price=Decimal("23.09"),
+ pubdate=datetime.date(2008, 3, 3),
+ publisher_id=2,
+ rating=3.0
+ )
+ # Different DB backends return different types for the extra select computation
+ self.assertTrue(obj.manufacture_cost == 11.545 or obj.manufacture_cost == Decimal('11.545'))
+
+ # Values queries can be combined with annotate and extra
+ obj = Book.objects.annotate(mean_auth_age=Avg('authors__age')).extra(select={'manufacture_cost' : 'price * .5'}).values().get(pk=2)
+ manufacture_cost = obj['manufacture_cost']
+ self.assertTrue(manufacture_cost == 11.545 or manufacture_cost == Decimal('11.545'))
+ del obj['manufacture_cost']
+ self.assertEqual(obj, {
+ "contact_id": 3,
+ "id": 2,
+ "isbn": u"067232959",
+ "mean_auth_age": 45.0,
+ "name": u"Sams Teach Yourself Django in 24 Hours",
+ "pages": 528,
+ "price": Decimal("23.09"),
+ "pubdate": datetime.date(2008, 3, 3),
+ "publisher_id": 2,
+ "rating": 3.0,
+ })
+
+ # The order of the (empty) values, annotate and extra clauses doesn't
+ # matter
+ obj = Book.objects.values().annotate(mean_auth_age=Avg('authors__age')).extra(select={'manufacture_cost' : 'price * .5'}).get(pk=2)
+ manufacture_cost = obj['manufacture_cost']
+ self.assertTrue(manufacture_cost == 11.545 or manufacture_cost == Decimal('11.545'))
+ del obj['manufacture_cost']
+ self.assertEqual(obj, {
+ 'contact_id': 3,
+ 'id': 2,
+ 'isbn': u'067232959',
+ 'mean_auth_age': 45.0,
+ 'name': u'Sams Teach Yourself Django in 24 Hours',
+ 'pages': 528,
+ 'price': Decimal("23.09"),
+ 'pubdate': datetime.date(2008, 3, 3),
+ 'publisher_id': 2,
+ 'rating': 3.0
+ })
+
+ # If the annotation precedes the values clause, it won't be included
+ # unless it is explicitly named
+ obj = Book.objects.annotate(mean_auth_age=Avg('authors__age')).extra(select={'price_per_page' : 'price / pages'}).values('name').get(pk=1)
+ self.assertEqual(obj, {
+ "name": u'The Definitive Guide to Django: Web Development Done Right',
+ })
+
+ obj = Book.objects.annotate(mean_auth_age=Avg('authors__age')).extra(select={'price_per_page' : 'price / pages'}).values('name','mean_auth_age').get(pk=1)
+ self.assertEqual(obj, {
+ 'mean_auth_age': 34.5,
+ 'name': u'The Definitive Guide to Django: Web Development Done Right',
+ })
+
+ # If an annotation isn't included in the values, it can still be used
+ # in a filter
+ qs = Book.objects.annotate(n_authors=Count('authors')).values('name').filter(n_authors__gt=2)
+ self.assertQuerysetEqual(
+ qs, [
+ {"name": u'Python Web Development with Django'}
+ ],
+ lambda b: b,
+ )
+
+ # The annotations are added to values output if values() precedes
+ # annotate()
+ obj = Book.objects.values('name').annotate(mean_auth_age=Avg('authors__age')).extra(select={'price_per_page' : 'price / pages'}).get(pk=1)
+ self.assertEqual(obj, {
+ 'mean_auth_age': 34.5,
+ 'name': u'The Definitive Guide to Django: Web Development Done Right',
+ })
+
+ # Check that all of the objects are getting counted (allow_nulls) and
+ # that values respects the amount of objects
+ self.assertEqual(
+ len(Author.objects.annotate(Avg('friends__age')).values()),
+ 9
+ )
+
+ # Check that consecutive calls to annotate accumulate in the query
+ qs = Book.objects.values('price').annotate(oldest=Max('authors__age')).order_by('oldest', 'price').annotate(Max('publisher__num_awards'))
+ self.assertQuerysetEqual(
+ qs, [
+ {'price': Decimal("30"), 'oldest': 35, 'publisher__num_awards__max': 3},
+ {'price': Decimal("29.69"), 'oldest': 37, 'publisher__num_awards__max': 7},
+ {'price': Decimal("23.09"), 'oldest': 45, 'publisher__num_awards__max': 1},
+ {'price': Decimal("75"), 'oldest': 57, 'publisher__num_awards__max': 9},
+ {'price': Decimal("82.8"), 'oldest': 57, 'publisher__num_awards__max': 7}
+ ],
+ lambda b: b,
+ )
+
+ def test_aggrate_annotation(self):
+ # Aggregates can be composed over annotations.
+ # The return type is derived from the composed aggregate
+ vals = Book.objects.all().annotate(num_authors=Count('authors__id')).aggregate(Max('pages'), Max('price'), Sum('num_authors'), Avg('num_authors'))
+ self.assertEqual(vals, {
+ 'num_authors__sum': 10,
+ 'num_authors__avg': Approximate(1.666, places=2),
+ 'pages__max': 1132,
+ 'price__max': Decimal("82.80")
+ })
+
+ def test_field_error(self):
+ # Bad field requests in aggregates are caught and reported
+ self.assertRaises(
+ FieldError,
+ lambda: Book.objects.all().aggregate(num_authors=Count('foo'))
+ )
+
+ self.assertRaises(
+ FieldError,
+ lambda: Book.objects.all().annotate(num_authors=Count('foo'))
+ )
+
+ self.assertRaises(
+ FieldError,
+ lambda: Book.objects.all().annotate(num_authors=Count('authors__id')).aggregate(Max('foo'))
+ )
+
+ def test_more(self):
+ # Old-style count aggregations can be mixed with new-style
+ self.assertEqual(
+ Book.objects.annotate(num_authors=Count('authors')).count(),
+ 6
+ )
+
+ # Non-ordinal, non-computed Aggregates over annotations correctly
+ # inherit the annotation's internal type if the annotation is ordinal
+ # or computed
+ vals = Book.objects.annotate(num_authors=Count('authors')).aggregate(Max('num_authors'))
+ self.assertEqual(
+ vals,
+ {'num_authors__max': 3}
+ )
+
+ vals = Publisher.objects.annotate(avg_price=Avg('book__price')).aggregate(Max('avg_price'))
+ self.assertEqual(
+ vals,
+ {'avg_price__max': 75.0}
+ )
+
+ # Aliases are quoted to protected aliases that might be reserved names
+ vals = Book.objects.aggregate(number=Max('pages'), select=Max('pages'))
+ self.assertEqual(
+ vals,
+ {'number': 1132, 'select': 1132}
+ )
+
+ # Regression for #10064: select_related() plays nice with aggregates
+ obj = Book.objects.select_related('publisher').annotate(num_authors=Count('authors')).values()[0]
+ self.assertEqual(obj, {
+ 'contact_id': 8,
+ 'id': 5,
+ 'isbn': u'013790395',
+ 'name': u'Artificial Intelligence: A Modern Approach',
+ 'num_authors': 2,
+ 'pages': 1132,
+ 'price': Decimal("82.8"),
+ 'pubdate': datetime.date(1995, 1, 15),
+ 'publisher_id': 3,
+ 'rating': 4.0,
+ })
+
+ # Regression for #10010: exclude on an aggregate field is correctly
+ # negated
+ self.assertEqual(
+ len(Book.objects.annotate(num_authors=Count('authors'))),
+ 6
+ )
+ self.assertEqual(
+ len(Book.objects.annotate(num_authors=Count('authors')).filter(num_authors__gt=2)),
+ 1
+ )
+ self.assertEqual(
+ len(Book.objects.annotate(num_authors=Count('authors')).exclude(num_authors__gt=2)),
+ 5
+ )
+
+ self.assertEqual(
+ len(Book.objects.annotate(num_authors=Count('authors')).filter(num_authors__lt=3).exclude(num_authors__lt=2)),
+ 2
+ )
+ self.assertEqual(
+ len(Book.objects.annotate(num_authors=Count('authors')).exclude(num_authors__lt=2).filter(num_authors__lt=3)),
+ 2
+ )
+
+ def test_aggregate_fexpr(self):
+ # Aggregates can be used with F() expressions
+ # ... where the F() is pushed into the HAVING clause
+ qs = Publisher.objects.annotate(num_books=Count('book')).filter(num_books__lt=F('num_awards')/2).order_by('name').values('name','num_books','num_awards')
+ self.assertQuerysetEqual(
+ qs, [
+ {'num_books': 1, 'name': u'Morgan Kaufmann', 'num_awards': 9},
+ {'num_books': 2, 'name': u'Prentice Hall', 'num_awards': 7}
+ ],
+ lambda p: p,
+ )
+
+ qs = Publisher.objects.annotate(num_books=Count('book')).exclude(num_books__lt=F('num_awards')/2).order_by('name').values('name','num_books','num_awards')
+ self.assertQuerysetEqual(
+ qs, [
+ {'num_books': 2, 'name': u'Apress', 'num_awards': 3},
+ {'num_books': 0, 'name': u"Jonno's House of Books", 'num_awards': 0},
+ {'num_books': 1, 'name': u'Sams', 'num_awards': 1}
+ ],
+ lambda p: p,
+ )
+
+ # ... and where the F() references an aggregate
+ qs = Publisher.objects.annotate(num_books=Count('book')).filter(num_awards__gt=2*F('num_books')).order_by('name').values('name','num_books','num_awards')
+ self.assertQuerysetEqual(
+ qs, [
+ {'num_books': 1, 'name': u'Morgan Kaufmann', 'num_awards': 9},
+ {'num_books': 2, 'name': u'Prentice Hall', 'num_awards': 7}
+ ],
+ lambda p: p,
+ )
+
+ qs = Publisher.objects.annotate(num_books=Count('book')).exclude(num_books__lt=F('num_awards')/2).order_by('name').values('name','num_books','num_awards')
+ self.assertQuerysetEqual(
+ qs, [
+ {'num_books': 2, 'name': u'Apress', 'num_awards': 3},
+ {'num_books': 0, 'name': u"Jonno's House of Books", 'num_awards': 0},
+ {'num_books': 1, 'name': u'Sams', 'num_awards': 1}
+ ],
+ lambda p: p,
+ )
+
+ def test_db_col_table(self):
+ # Tests on fields with non-default table and column names.
+ qs = Clues.objects.values('EntryID__Entry').annotate(Appearances=Count('EntryID'), Distinct_Clues=Count('Clue', distinct=True))
+ self.assertQuerysetEqual(qs, [])
+
+ qs = Entries.objects.annotate(clue_count=Count('clues__ID'))
+ self.assertQuerysetEqual(qs, [])
+
+ def test_empty(self):
+ # Regression for #10089: Check handling of empty result sets with
+ # aggregates
+ self.assertEqual(
+ Book.objects.filter(id__in=[]).count(),
+ 0
+ )
+
+ vals = Book.objects.filter(id__in=[]).aggregate(num_authors=Count('authors'), avg_authors=Avg('authors'), max_authors=Max('authors'), max_price=Max('price'), max_rating=Max('rating'))
+ self.assertEqual(
+ vals,
+ {'max_authors': None, 'max_rating': None, 'num_authors': 0, 'avg_authors': None, 'max_price': None}
+ )
+
+ qs = Publisher.objects.filter(pk=5).annotate(num_authors=Count('book__authors'), avg_authors=Avg('book__authors'), max_authors=Max('book__authors'), max_price=Max('book__price'), max_rating=Max('book__rating')).values()
+ self.assertQuerysetEqual(
+ qs, [
+ {'max_authors': None, 'name': u"Jonno's House of Books", 'num_awards': 0, 'max_price': None, 'num_authors': 0, 'max_rating': None, 'id': 5, 'avg_authors': None}
+ ],
+ lambda p: p
+ )
+
+ def test_more_more(self):
+ # Regression for #10113 - Fields mentioned in order_by() must be
+ # included in the GROUP BY. This only becomes a problem when the
+ # order_by introduces a new join.
+ self.assertQuerysetEqual(
+ Book.objects.annotate(num_authors=Count('authors')).order_by('publisher__name', 'name'), [
+ "Practical Django Projects",
+ "The Definitive Guide to Django: Web Development Done Right",
+ "Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp",
+ "Artificial Intelligence: A Modern Approach",
+ "Python Web Development with Django",
+ "Sams Teach Yourself Django in 24 Hours",
+ ],
+ lambda b: b.name
+ )
+
+ # Regression for #10127 - Empty select_related() works with annotate
+ qs = Book.objects.filter(rating__lt=4.5).select_related().annotate(Avg('authors__age'))
+ self.assertQuerysetEqual(
+ qs, [
+ (u'Artificial Intelligence: A Modern Approach', 51.5, u'Prentice Hall', u'Peter Norvig'),
+ (u'Practical Django Projects', 29.0, u'Apress', u'James Bennett'),
+ (u'Python Web Development with Django', Approximate(30.333, places=2), u'Prentice Hall', u'Jeffrey Forcier'),
+ (u'Sams Teach Yourself Django in 24 Hours', 45.0, u'Sams', u'Brad Dayley')
+ ],
+ lambda b: (b.name, b.authors__age__avg, b.publisher.name, b.contact.name)
+ )
+
+ # Regression for #10132 - If the values() clause only mentioned extra
+ # (select=) columns, those columns are used for grouping
+ qs = Book.objects.extra(select={'pub':'publisher_id'}).values('pub').annotate(Count('id')).order_by('pub')
+ self.assertQuerysetEqual(
+ qs, [
+ {'pub': 1, 'id__count': 2},
+ {'pub': 2, 'id__count': 1},
+ {'pub': 3, 'id__count': 2},
+ {'pub': 4, 'id__count': 1}
+ ],
+ lambda b: b
+ )
+
+ qs = Book.objects.extra(select={'pub':'publisher_id', 'foo':'pages'}).values('pub').annotate(Count('id')).order_by('pub')
+ self.assertQuerysetEqual(
+ qs, [
+ {'pub': 1, 'id__count': 2},
+ {'pub': 2, 'id__count': 1},
+ {'pub': 3, 'id__count': 2},
+ {'pub': 4, 'id__count': 1}
+ ],
+ lambda b: b
+ )
+
+ # Regression for #10182 - Queries with aggregate calls are correctly
+ # realiased when used in a subquery
+ ids = Book.objects.filter(pages__gt=100).annotate(n_authors=Count('authors')).filter(n_authors__gt=2).order_by('n_authors')
+ self.assertQuerysetEqual(
+ Book.objects.filter(id__in=ids), [
+ "Python Web Development with Django",
+ ],
+ lambda b: b.name
+ )
+
+ def test_pickle(self):
+ # Regression for #10197 -- Queries with aggregates can be pickled.
+ # First check that pickling is possible at all. No crash = success
+ qs = Book.objects.annotate(num_authors=Count('authors'))
+ out = pickle.dumps(qs)
+
+ # Then check that the round trip works.
+ query = qs.query.get_compiler(qs.db).as_sql()[0]
+ qs2 = pickle.loads(pickle.dumps(qs))
+ self.assertEqual(
+ qs2.query.get_compiler(qs2.db).as_sql()[0],
+ query,
+ )
+
+ def test_more_more_more(self):
+ # Regression for #10199 - Aggregate calls clone the original query so
+ # the original query can still be used
+ books = Book.objects.all()
+ books.aggregate(Avg("authors__age"))
+ self.assertQuerysetEqual(
+ books.all(), [
+ u'Artificial Intelligence: A Modern Approach',
+ u'Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp',
+ u'Practical Django Projects',
+ u'Python Web Development with Django',
+ u'Sams Teach Yourself Django in 24 Hours',
+ u'The Definitive Guide to Django: Web Development Done Right'
+ ],
+ lambda b: b.name
+ )
+
+ # Regression for #10248 - Annotations work with DateQuerySets
+ qs = Book.objects.annotate(num_authors=Count('authors')).filter(num_authors=2).dates('pubdate', 'day')
+ self.assertQuerysetEqual(
+ qs, [
+ datetime.datetime(1995, 1, 15, 0, 0),
+ datetime.datetime(2007, 12, 6, 0, 0)
+ ],
+ lambda b: b
+ )
+
+ # Regression for #10290 - extra selects with parameters can be used for
+ # grouping.
+ qs = Book.objects.annotate(mean_auth_age=Avg('authors__age')).extra(select={'sheets' : '(pages + %s) / %s'}, select_params=[1, 2]).order_by('sheets').values('sheets')
+ self.assertQuerysetEqual(
+ qs, [
+ 150,
+ 175,
+ 224,
+ 264,
+ 473,
+ 566
+ ],
+ lambda b: int(b["sheets"])
+ )
+
+ # Regression for 10425 - annotations don't get in the way of a count()
+ # clause
+ self.assertEqual(
+ Book.objects.values('publisher').annotate(Count('publisher')).count(),
+ 4
+ )
+ self.assertEqual(
+ Book.objects.annotate(Count('publisher')).values('publisher').count(),
+ 6
+ )
+
+ publishers = Publisher.objects.filter(id__in=[1, 2])
+ self.assertQuerysetEqual(
+ publishers, [
+ "Apress",
+ "Sams"
+ ],
+ lambda p: p.name
+ )
+
+ publishers = publishers.annotate(n_books=Count("book"))
+ self.assertEqual(
+ publishers[0].n_books,
+ 2
+ )
+
+ self.assertQuerysetEqual(
+ publishers, [
+ "Apress",
+ "Sams",
+ ],
+ lambda p: p.name
+ )
+
+ books = Book.objects.filter(publisher__in=publishers)
+ self.assertQuerysetEqual(
+ books, [
+ "Practical Django Projects",
+ "Sams Teach Yourself Django in 24 Hours",
+ "The Definitive Guide to Django: Web Development Done Right",
+ ],
+ lambda b: b.name
+ )
+ self.assertQuerysetEqual(
+ publishers, [
+ "Apress",
+ "Sams",
+ ],
+ lambda p: p.name
+ )
+
+ # Regression for 10666 - inherited fields work with annotations and
+ # aggregations
+ self.assertEqual(
+ HardbackBook.objects.aggregate(n_pages=Sum('book_ptr__pages')),
+ {'n_pages': 2078}
+ )
+
+ self.assertEqual(
+ HardbackBook.objects.aggregate(n_pages=Sum('pages')),
+ {'n_pages': 2078},
+ )
+
+ qs = HardbackBook.objects.annotate(n_authors=Count('book_ptr__authors')).values('name', 'n_authors')
+ self.assertQuerysetEqual(
+ qs, [
+ {'n_authors': 2, 'name': u'Artificial Intelligence: A Modern Approach'},
+ {'n_authors': 1, 'name': u'Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp'}
+ ],
+ lambda h: h
+ )
+
+ qs = HardbackBook.objects.annotate(n_authors=Count('authors')).values('name', 'n_authors')
+ self.assertQuerysetEqual(
+ qs, [
+ {'n_authors': 2, 'name': u'Artificial Intelligence: A Modern Approach'},
+ {'n_authors': 1, 'name': u'Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp'}
+ ],
+ lambda h: h,
+ )
+
+ # Regression for #10766 - Shouldn't be able to reference an aggregate
+ # fields in an an aggregate() call.
+ self.assertRaises(
+ FieldError,
+ lambda: Book.objects.annotate(mean_age=Avg('authors__age')).annotate(Avg('mean_age'))
+ )
+
+ if run_stddev_tests():
+ def test_stddev(self):
+ self.assertEqual(
+ Book.objects.aggregate(StdDev('pages')),
+ {'pages__stddev': Approximate(311.46, 1)}
+ )
+
+ self.assertEqual(
+ Book.objects.aggregate(StdDev('rating')),
+ {'rating__stddev': Approximate(0.60, 1)}
+ )
+
+ self.assertEqual(
+ Book.objects.aggregate(StdDev('price')),
+ {'price__stddev': Approximate(24.16, 2)}
+ )
+
+ self.assertEqual(
+ Book.objects.aggregate(StdDev('pages', sample=True)),
+ {'pages__stddev': Approximate(341.19, 2)}
+ )
+
+ self.assertEqual(
+ Book.objects.aggregate(StdDev('rating', sample=True)),
+ {'rating__stddev': Approximate(0.66, 2)}
+ )
+
+ self.assertEqual(
+ Book.objects.aggregate(StdDev('price', sample=True)),
+ {'price__stddev': Approximate(26.46, 1)}
+ )
+
+ self.assertEqual(
+ Book.objects.aggregate(Variance('pages')),
+ {'pages__variance': Approximate(97010.80, 1)}
+ )
+
+ self.assertEqual(
+ Book.objects.aggregate(Variance('rating')),
+ {'rating__variance': Approximate(0.36, 1)}
+ )
+
+ self.assertEqual(
+ Book.objects.aggregate(Variance('price')),
+ {'price__variance': Approximate(583.77, 1)}
+ )
+
+ self.assertEqual(
+ Book.objects.aggregate(Variance('pages', sample=True)),
+ {'pages__variance': Approximate(116412.96, 1)}
+ )
+
+ self.assertEqual(
+ Book.objects.aggregate(Variance('rating', sample=True)),
+ {'rating__variance': Approximate(0.44, 2)}
+ )
+
+ self.assertEqual(
+ Book.objects.aggregate(Variance('price', sample=True)),
+ {'price__variance': Approximate(700.53, 2)}
+ )
diff --git a/tests/regressiontests/backends/models.py b/tests/regressiontests/backends/models.py
index 423bead1ad..7315408d49 100644
--- a/tests/regressiontests/backends/models.py
+++ b/tests/regressiontests/backends/models.py
@@ -1,5 +1,9 @@
+from django.contrib.contenttypes import generic
+from django.contrib.contenttypes.models import ContentType
+from django.conf import settings
from django.db import models
-from django.db import connection
+from django.db import connection, DEFAULT_DB_ALIAS
+
class Square(models.Model):
root = models.IntegerField()
@@ -8,6 +12,7 @@ class Square(models.Model):
def __unicode__(self):
return "%s ** 2 == %s" % (self.root, self.square)
+
class Person(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
@@ -15,11 +20,40 @@ class Person(models.Model):
def __unicode__(self):
return u'%s %s' % (self.first_name, self.last_name)
+
class SchoolClass(models.Model):
year = models.PositiveIntegerField()
day = models.CharField(max_length=9, blank=True)
last_updated = models.DateTimeField()
+# Unfortunately, the following model breaks MySQL hard.
+# Until #13711 is fixed, this test can't be run under MySQL.
+if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.mysql':
+ class VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ(models.Model):
+ class Meta:
+ # We need to use a short actual table name or
+ # we hit issue #8548 which we're not testing!
+ verbose_name = 'model_with_long_table_name'
+ primary_key_is_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.AutoField(primary_key=True)
+ charfield_is_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.CharField(max_length=100)
+ m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.ManyToManyField(Person,blank=True)
+
+
+class Tag(models.Model):
+ name = models.CharField(max_length=30)
+ content_type = models.ForeignKey(ContentType, related_name='backend_tags')
+ object_id = models.PositiveIntegerField()
+ content_object = generic.GenericForeignKey('content_type', 'object_id')
+
+
+class Post(models.Model):
+ name = models.CharField(max_length=30)
+ text = models.TextField()
+ tags = generic.GenericRelation('Tag')
+
+ class Meta:
+ db_table = 'CaseSensitive_Post'
+
qn = connection.ops.quote_name
__test__ = {'API_TESTS': """
diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
index 6a26a608eb..01764135fa 100644
--- a/tests/regressiontests/backends/tests.py
+++ b/tests/regressiontests/backends/tests.py
@@ -1,13 +1,18 @@
# -*- coding: utf-8 -*-
# Unit and doctests for specific database backends.
import datetime
-import models
import unittest
-from django.db import backend, connection, DEFAULT_DB_ALIAS
-from django.db.backends.signals import connection_created
+
from django.conf import settings
+from django.core import management
+from django.core.management.color import no_style
+from django.db import backend, connection, connections, DEFAULT_DB_ALIAS
+from django.db.backends.signals import connection_created
+from django.db.backends.postgresql import version as pg_version
from django.test import TestCase
+from regressiontests.backends import models
+
class Callproc(unittest.TestCase):
def test_dbms_session(self):
@@ -76,6 +81,7 @@ class DateQuotingTest(TestCase):
classes = models.SchoolClass.objects.filter(last_updated__day=20)
self.assertEqual(len(classes), 1)
+
class ParameterHandlingTest(TestCase):
def test_bad_parameter_count(self):
"An executemany call with too many/not enough parameters will raise an exception (Refs #12612)"
@@ -88,46 +94,95 @@ class ParameterHandlingTest(TestCase):
self.assertRaises(Exception, cursor.executemany, query, [(1,2,3),])
self.assertRaises(Exception, cursor.executemany, query, [(1,),])
+# Unfortunately, the following tests would be a good test to run on all
+# backends, but it breaks MySQL hard. Until #13711 is fixed, it can't be run
+# everywhere (although it would be an effective test of #13711).
+if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.mysql':
+ class LongNameTest(TestCase):
+ """Long primary keys and model names can result in a sequence name
+ that exceeds the database limits, which will result in truncation
+ on certain databases (e.g., Postgres). The backend needs to use
+ the correct sequence name in last_insert_id and other places, so
+ check it is. Refs #8901.
+ """
-def connection_created_test(sender, **kwargs):
- print 'connection_created signal'
-
-__test__ = {'API_TESTS': """
-# Check Postgres version parsing
->>> from django.db.backends.postgresql import version as pg_version
-
->>> pg_version._parse_version("PostgreSQL 8.3.1 on i386-apple-darwin9.2.2, compiled by GCC i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5478)")
-(8, 3, 1)
-
->>> pg_version._parse_version("PostgreSQL 8.3.6")
-(8, 3, 6)
-
->>> pg_version._parse_version("PostgreSQL 8.3")
-(8, 3, None)
-
->>> pg_version._parse_version("EnterpriseDB 8.3")
-(8, 3, None)
-
->>> pg_version._parse_version("PostgreSQL 8.3 beta4")
-(8, 3, None)
+ def test_sequence_name_length_limits_create(self):
+ """Test creation of model with long name and long pk name doesn't error. Ref #8901"""
+ models.VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ.objects.create()
+
+ def test_sequence_name_length_limits_m2m(self):
+ """Test an m2m save of a model with a long name and a long m2m field name doesn't error as on Django >=1.2 this now uses object saves. Ref #8901"""
+ obj = models.VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ.objects.create()
+ rel_obj = models.Person.objects.create(first_name='Django', last_name='Reinhardt')
+ obj.m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz.add(rel_obj)
+
+ def test_sequence_name_length_limits_flush(self):
+ """Test that sequence resetting as part of a flush with model with long name and long pk name doesn't error. Ref #8901"""
+ # A full flush is expensive to the full test, so we dig into the
+ # internals to generate the likely offending SQL and run it manually
+
+ # Some convenience aliases
+ VLM = models.VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
+ VLM_m2m = VLM.m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz.through
+ tables = [
+ VLM._meta.db_table,
+ VLM_m2m._meta.db_table,
+ ]
+ sequences = [
+ {
+ 'column': VLM._meta.pk.column,
+ 'table': VLM._meta.db_table
+ },
+ ]
+ cursor = connection.cursor()
+ for statement in connection.ops.sql_flush(no_style(), tables, sequences):
+ cursor.execute(statement)
->>> pg_version._parse_version("PostgreSQL 8.4beta1")
-(8, 4, None)
+class SequenceResetTest(TestCase):
+ def test_generic_relation(self):
+ "Sequence names are correct when resetting generic relations (Ref #13941)"
+ # Create an object with a manually specified PK
+ models.Post.objects.create(id=10, name='1st post', text='hello world')
-"""}
+ # Reset the sequences for the database
+ cursor = connection.cursor()
+ commands = connections[DEFAULT_DB_ALIAS].ops.sequence_reset_sql(no_style(), [models.Post])
+ for sql in commands:
+ cursor.execute(sql)
+
+ # If we create a new object now, it should have a PK greater
+ # than the PK we specified manually.
+ obj = models.Post.objects.create(name='New post', text='goodbye world')
+ self.assertTrue(obj.pk > 10)
+
+class PostgresVersionTest(TestCase):
+ def assert_parses(self, version_string, version):
+ self.assertEqual(pg_version._parse_version(version_string), version)
+
+ def test_parsing(self):
+ self.assert_parses("PostgreSQL 8.3 beta4", (8, 3, None))
+ self.assert_parses("PostgreSQL 8.3", (8, 3, None))
+ self.assert_parses("EnterpriseDB 8.3", (8, 3, None))
+ self.assert_parses("PostgreSQL 8.3.6", (8, 3, 6))
+ self.assert_parses("PostgreSQL 8.4beta1", (8, 4, None))
+ self.assert_parses("PostgreSQL 8.3.1 on i386-apple-darwin9.2.2, compiled by GCC i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5478)", (8, 3, 1))
# Unfortunately with sqlite3 the in-memory test database cannot be
# closed, and so it cannot be re-opened during testing, and so we
# sadly disable this test for now.
-if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.sqlite3':
- __test__['API_TESTS'] += """
->>> connection_created.connect(connection_created_test)
->>> connection.close() # Ensure the connection is closed
->>> cursor = connection.cursor()
-connection_created signal
->>> connection_created.disconnect(connection_created_test)
->>> cursor = connection.cursor()
-"""
-
-if __name__ == '__main__':
- unittest.main()
+if settings.DATABASES[DEFAULT_DB_ALIAS]["ENGINE"] != "django.db.backends.sqlite3":
+ class ConnectionCreatedSignalTest(TestCase):
+ def test_signal(self):
+ data = {}
+ def receiver(sender, connection, **kwargs):
+ data["connection"] = connection
+
+ connection_created.connect(receiver)
+ connection.close()
+ cursor = connection.cursor()
+ self.assertTrue(data["connection"] is connection)
+
+ connection_created.disconnect(receiver)
+ data.clear()
+ cursor = connection.cursor()
+ self.assertTrue(data == {})
diff --git a/tests/regressiontests/cache/liberal_backend.py b/tests/regressiontests/cache/liberal_backend.py
new file mode 100644
index 0000000000..5c7e312690
--- /dev/null
+++ b/tests/regressiontests/cache/liberal_backend.py
@@ -0,0 +1,9 @@
+from django.core.cache.backends.locmem import CacheClass as LocMemCacheClass
+
+class LiberalKeyValidationMixin(object):
+ def validate_key(self, key):
+ pass
+
+class CacheClass(LiberalKeyValidationMixin, LocMemCacheClass):
+ pass
+
diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
index 109374c46c..1e0a4046bb 100644
--- a/tests/regressiontests/cache/tests.py
+++ b/tests/regressiontests/cache/tests.py
@@ -8,11 +8,12 @@ import shutil
import tempfile
import time
import unittest
+import warnings
from django.conf import settings
from django.core import management
from django.core.cache import get_cache
-from django.core.cache.backends.base import InvalidCacheBackendError
+from django.core.cache.backends.base import InvalidCacheBackendError, CacheKeyWarning
from django.http import HttpResponse, HttpRequest
from django.middleware.cache import FetchFromCacheMiddleware, UpdateCacheMiddleware
from django.utils import translation
@@ -352,21 +353,68 @@ class BaseCacheTests(object):
self.assertEqual(self.cache.get('key3'), 'sausage')
self.assertEqual(self.cache.get('key4'), 'lobster bisque')
+ def perform_cull_test(self, initial_count, final_count):
+ """This is implemented as a utility method, because only some of the backends
+ implement culling. The culling algorithm also varies slightly, so the final
+ number of entries will vary between backends"""
+ # Create initial cache key entries. This will overflow the cache, causing a cull
+ for i in range(1, initial_count):
+ self.cache.set('cull%d' % i, 'value', 1000)
+ count = 0
+ # Count how many keys are left in the cache.
+ for i in range(1, initial_count):
+ if self.cache.has_key('cull%d' % i):
+ count = count + 1
+ self.assertEqual(count, final_count)
+
+ def test_invalid_keys(self):
+ """
+ All the builtin backends (except memcached, see below) should warn on
+ keys that would be refused by memcached. This encourages portable
+ caching code without making it too difficult to use production backends
+ with more liberal key rules. Refs #6447.
+
+ """
+ # On Python 2.6+ we could use the catch_warnings context
+ # manager to test this warning nicely. Since we can't do that
+ # yet, the cleanest option is to temporarily ask for
+ # CacheKeyWarning to be raised as an exception.
+ warnings.simplefilter("error", CacheKeyWarning)
+
+ # memcached does not allow whitespace or control characters in keys
+ self.assertRaises(CacheKeyWarning, self.cache.set, 'key with spaces', 'value')
+ # memcached limits key length to 250
+ self.assertRaises(CacheKeyWarning, self.cache.set, 'a' * 251, 'value')
+
+ # The warnings module has no public API for getting the
+ # current list of warning filters, so we can't save that off
+ # and reset to the previous value, we have to globally reset
+ # it. The effect will be the same, as long as the Django test
+ # runner doesn't add any global warning filters (it currently
+ # does not).
+ warnings.resetwarnings()
+
class DBCacheTests(unittest.TestCase, BaseCacheTests):
def setUp(self):
# Spaces are used in the table name to ensure quoting/escaping is working
self._table_name = 'test cache table'
management.call_command('createcachetable', self._table_name, verbosity=0, interactive=False)
- self.cache = get_cache('db://%s' % self._table_name)
+ self.cache = get_cache('db://%s?max_entries=30' % self._table_name)
def tearDown(self):
from django.db import connection
cursor = connection.cursor()
cursor.execute('DROP TABLE %s' % connection.ops.quote_name(self._table_name))
+ def test_cull(self):
+ self.perform_cull_test(50, 29)
+
class LocMemCacheTests(unittest.TestCase, BaseCacheTests):
def setUp(self):
- self.cache = get_cache('locmem://')
+ self.cache = get_cache('locmem://?max_entries=30')
+
+ def test_cull(self):
+ self.perform_cull_test(50, 29)
# memcached backend isn't guaranteed to be available.
# To check the memcached backend, the test settings file will
@@ -377,13 +425,29 @@ if settings.CACHE_BACKEND.startswith('memcached://'):
def setUp(self):
self.cache = get_cache(settings.CACHE_BACKEND)
+ def test_invalid_keys(self):
+ """
+ On memcached, we don't introduce a duplicate key validation
+ step (for speed reasons), we just let the memcached API
+ library raise its own exception on bad keys. Refs #6447.
+
+ In order to be memcached-API-library agnostic, we only assert
+ that a generic exception of some kind is raised.
+
+ """
+ # memcached does not allow whitespace or control characters in keys
+ self.assertRaises(Exception, self.cache.set, 'key with spaces', 'value')
+ # memcached limits key length to 250
+ self.assertRaises(Exception, self.cache.set, 'a' * 251, 'value')
+
+
class FileBasedCacheTests(unittest.TestCase, BaseCacheTests):
"""
Specific test cases for the file-based cache.
"""
def setUp(self):
self.dirname = tempfile.mkdtemp()
- self.cache = get_cache('file://%s' % self.dirname)
+ self.cache = get_cache('file://%s?max_entries=30' % self.dirname)
def test_hashing(self):
"""Test that keys are hashed into subdirectories correctly"""
@@ -406,6 +470,25 @@ class FileBasedCacheTests(unittest.TestCase, BaseCacheTests):
self.assert_(not os.path.exists(os.path.dirname(keypath)))
self.assert_(not os.path.exists(os.path.dirname(os.path.dirname(keypath))))
+ def test_cull(self):
+ self.perform_cull_test(50, 28)
+
+class CustomCacheKeyValidationTests(unittest.TestCase):
+ """
+ Tests for the ability to mixin a custom ``validate_key`` method to
+ a custom cache backend that otherwise inherits from a builtin
+ backend, and override the default key validation. Refs #6447.
+
+ """
+ def test_custom_key_validation(self):
+ cache = get_cache('regressiontests.cache.liberal_backend://')
+
+ # this key is both longer than 250 characters, and has spaces
+ key = 'some key with spaces' * 15
+ val = 'a value'
+ cache.set(key, val)
+ self.assertEqual(cache.get(key), val)
+
class CacheUtils(unittest.TestCase):
"""TestCase for django.utils.cache functions."""
diff --git a/tests/regressiontests/csrf_tests/tests.py b/tests/regressiontests/csrf_tests/tests.py
index 0a24522d9c..9030d397ab 100644
--- a/tests/regressiontests/csrf_tests/tests.py
+++ b/tests/regressiontests/csrf_tests/tests.py
@@ -3,7 +3,7 @@
from django.test import TestCase
from django.http import HttpRequest, HttpResponse
from django.middleware.csrf import CsrfMiddleware, CsrfViewMiddleware
-from django.views.decorators.csrf import csrf_exempt
+from django.views.decorators.csrf import csrf_exempt, csrf_view_exempt
from django.core.context_processors import csrf
from django.contrib.sessions.middleware import SessionMiddleware
from django.utils.importlib import import_module
@@ -12,8 +12,8 @@ from django.template import RequestContext, Template
# Response/views used for CsrfResponseMiddleware and CsrfViewMiddleware tests
def post_form_response():
- resp = HttpResponse(content="""
-<html><body><form method="post"><input type="text" /></form></body></html>
+ resp = HttpResponse(content=u"""
+<html><body><h1>\u00a1Unicode!<form method="post"><input type="text" /></form></body></html>
""", mimetype="text/html")
return resp
@@ -56,6 +56,9 @@ class TestingHttpRequest(HttpRequest):
return getattr(self, '_is_secure', False)
class CsrfMiddlewareTest(TestCase):
+ # The csrf token is potentially from an untrusted source, so could have
+ # characters that need dealing with.
+ _csrf_id_cookie = "<1>\xc2\xa1"
_csrf_id = "1"
# This is a valid session token for this ID and secret key. This was generated using
@@ -71,7 +74,7 @@ class CsrfMiddlewareTest(TestCase):
def _get_GET_csrf_cookie_request(self):
req = TestingHttpRequest()
- req.COOKIES[settings.CSRF_COOKIE_NAME] = self._csrf_id
+ req.COOKIES[settings.CSRF_COOKIE_NAME] = self._csrf_id_cookie
return req
def _get_POST_csrf_cookie_request(self):
@@ -123,6 +126,23 @@ class CsrfMiddlewareTest(TestCase):
# Check the Vary header got patched correctly
self.assert_('Cookie' in resp2.get('Vary',''))
+ def test_process_response_for_exempt_view(self):
+ """
+ Check that a view decorated with 'csrf_view_exempt' is still
+ post-processed to add the CSRF token.
+ """
+ req = self._get_GET_no_csrf_cookie_request()
+ CsrfMiddleware().process_view(req, csrf_view_exempt(post_form_view), (), {})
+
+ resp = post_form_response()
+ resp_content = resp.content # needed because process_response modifies resp
+ resp2 = CsrfMiddleware().process_response(req, resp)
+
+ csrf_cookie = resp2.cookies.get(settings.CSRF_COOKIE_NAME, False)
+ self.assertNotEqual(csrf_cookie, False)
+ self.assertNotEqual(resp_content, resp2.content)
+ self._check_token_present(resp2, csrf_cookie.value)
+
def test_process_response_no_csrf_cookie_view_only_get_token_used(self):
"""
When no prior CSRF cookie exists, check that the cookie is created, even
@@ -187,8 +207,11 @@ class CsrfMiddlewareTest(TestCase):
"""
Check that no post processing is done for an exempt view
"""
- req = self._get_POST_csrf_cookie_request()
- resp = csrf_exempt(post_form_view)(req)
+ req = self._get_GET_csrf_cookie_request()
+ view = csrf_exempt(post_form_view)
+ CsrfMiddleware().process_view(req, view, (), {})
+
+ resp = view(req)
resp_content = resp.content
resp2 = CsrfMiddleware().process_response(req, resp)
self.assertEquals(resp_content, resp2.content)
@@ -270,6 +293,17 @@ class CsrfMiddlewareTest(TestCase):
resp = token_view(req)
self.assertEquals(u"", resp.content)
+ def test_token_node_empty_csrf_cookie(self):
+ """
+ Check that we get a new token if the csrf_cookie is the empty string
+ """
+ req = self._get_GET_no_csrf_cookie_request()
+ req.COOKIES[settings.CSRF_COOKIE_NAME] = ""
+ CsrfViewMiddleware().process_view(req, token_view, (), {})
+ resp = token_view(req)
+
+ self.assertNotEqual(u"", resp.content)
+
def test_token_node_with_csrf_cookie(self):
"""
Check that CsrfTokenNode works when a CSRF cookie is set
@@ -279,6 +313,15 @@ class CsrfMiddlewareTest(TestCase):
resp = token_view(req)
self._check_token_present(resp)
+ def test_get_token_for_exempt_view(self):
+ """
+ Check that get_token still works for a view decorated with 'csrf_view_exempt'.
+ """
+ req = self._get_GET_csrf_cookie_request()
+ CsrfViewMiddleware().process_view(req, csrf_view_exempt(token_view), (), {})
+ resp = token_view(req)
+ self._check_token_present(resp)
+
def test_token_node_with_new_csrf_cookie(self):
"""
Check that CsrfTokenNode works when a CSRF cookie is created by
diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py
index 25555081e4..8341f83e36 100644
--- a/tests/regressiontests/defaultfilters/tests.py
+++ b/tests/regressiontests/defaultfilters/tests.py
@@ -476,6 +476,15 @@ u'1024.0 MB'
>>> filesizeformat(1024*1024*1024)
u'1.0 GB'
+>>> filesizeformat(1024*1024*1024*1024)
+u'1.0 TB'
+
+>>> filesizeformat(1024*1024*1024*1024*1024)
+u'1.0 PB'
+
+>>> filesizeformat(1024*1024*1024*1024*1024*2000)
+u'2000.0 PB'
+
>>> filesizeformat(complex(1,-1))
u'0 bytes'
diff --git a/tests/regressiontests/file_storage/tests.py b/tests/regressiontests/file_storage/tests.py
index 2c5f0f4551..a1470f9f05 100644
--- a/tests/regressiontests/file_storage/tests.py
+++ b/tests/regressiontests/file_storage/tests.py
@@ -230,3 +230,19 @@ if Image is not None:
finally:
del images.open
self.assert_(FileWrapper._closed)
+
+ class InconsistentGetImageDimensionsBug(TestCase):
+ """
+ Test that get_image_dimensions() works properly after various calls using a file handler (#11158)
+ """
+ def test_multiple_calls(self):
+ """
+ Multiple calls of get_image_dimensions() should return the same size.
+ """
+ from django.core.files.images import ImageFile
+ img_path = os.path.join(os.path.dirname(__file__), "test.png")
+ image = ImageFile(open(img_path))
+ image_pil = Image.open(img_path)
+ size_1, size_2 = get_image_dimensions(image), get_image_dimensions(image)
+ self.assertEqual(image_pil.size, size_1)
+ self.assertEqual(size_1, size_2)
diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py
index 990a9f74cf..e4f2c261c9 100644
--- a/tests/regressiontests/forms/fields.py
+++ b/tests/regressiontests/forms/fields.py
@@ -766,13 +766,13 @@ class FieldsTests(TestCase):
# FilePathField ###############################################################
def test_filepathfield_65(self):
- path = forms.__file__
+ path = os.path.abspath(forms.__file__)
path = os.path.dirname(path) + '/'
- assert fix_os_paths(path).endswith('/django/forms/')
+ self.assertTrue(fix_os_paths(path).endswith('/django/forms/'))
def test_filepathfield_66(self):
path = forms.__file__
- path = os.path.dirname(path) + '/'
+ path = os.path.dirname(os.path.abspath(path)) + '/'
f = FilePathField(path=path)
f.choices = [p for p in f.choices if p[0].endswith('.py')]
f.choices.sort()
@@ -787,13 +787,13 @@ class FieldsTests(TestCase):
]
for exp, got in zip(expected, fix_os_paths(f.choices)):
self.assertEqual(exp[1], got[1])
- assert got[0].endswith(exp[0])
+ self.assertTrue(got[0].endswith(exp[0]))
self.assertRaisesErrorWithMessage(ValidationError, "[u'Select a valid choice. fields.py is not one of the available choices.']", f.clean, 'fields.py')
assert fix_os_paths(f.clean(path + 'fields.py')).endswith('/django/forms/fields.py')
def test_filepathfield_67(self):
path = forms.__file__
- path = os.path.dirname(path) + '/'
+ path = os.path.dirname(os.path.abspath(path)) + '/'
f = FilePathField(path=path, match='^.*?\.py$')
f.choices.sort()
expected = [
@@ -807,10 +807,10 @@ class FieldsTests(TestCase):
]
for exp, got in zip(expected, fix_os_paths(f.choices)):
self.assertEqual(exp[1], got[1])
- assert got[0].endswith(exp[0])
+ self.assertTrue(got[0].endswith(exp[0]))
def test_filepathfield_68(self):
- path = forms.__file__
+ path = os.path.abspath(forms.__file__)
path = os.path.dirname(path) + '/'
f = FilePathField(path=path, recursive=True, match='^.*?\.py$')
f.choices.sort()
@@ -827,7 +827,7 @@ class FieldsTests(TestCase):
]
for exp, got in zip(expected, fix_os_paths(f.choices)):
self.assertEqual(exp[1], got[1])
- assert got[0].endswith(exp[0])
+ self.assertTrue(got[0].endswith(exp[0]))
# SplitDateTimeField ##########################################################
diff --git a/tests/regressiontests/forms/forms.py b/tests/regressiontests/forms/forms.py
index 58051fd133..91594139f2 100644
--- a/tests/regressiontests/forms/forms.py
+++ b/tests/regressiontests/forms/forms.py
@@ -522,6 +522,18 @@ tags.
<input type="hidden" name="composers" value="P" />
<input type="hidden" name="composers" value="J" />
+DateTimeField rendered as_hidden() is special too
+
+>>> class MessageForm(Form):
+... when = SplitDateTimeField()
+>>> f = MessageForm({'when_0': '1992-01-01', 'when_1': '01:01'})
+>>> print f.is_valid()
+True
+>>> print f['when']
+<input type="text" name="when_0" value="1992-01-01" id="id_when_0" /><input type="text" name="when_1" value="01:01" id="id_when_1" />
+>>> print f['when'].as_hidden()
+<input type="hidden" name="when_0" value="1992-01-01" id="id_when_0" /><input type="hidden" name="when_1" value="01:01" id="id_when_1" />
+
MultipleChoiceField can also be used with the CheckboxSelectMultiple widget.
>>> class SongForm(Form):
... name = CharField()
@@ -705,13 +717,13 @@ Form.clean() is required to return a dictionary of all clean data.
>>> print f.as_table()
<tr><td colspan="2"><ul class="errorlist"><li>Please make sure your passwords match.</li></ul></td></tr>
<tr><th>Username:</th><td><input type="text" name="username" value="adrian" maxlength="10" /></td></tr>
-<tr><th>Password1:</th><td><input type="password" name="password1" value="foo" /></td></tr>
-<tr><th>Password2:</th><td><input type="password" name="password2" value="bar" /></td></tr>
+<tr><th>Password1:</th><td><input type="password" name="password1" /></td></tr>
+<tr><th>Password2:</th><td><input type="password" name="password2" /></td></tr>
>>> print f.as_ul()
<li><ul class="errorlist"><li>Please make sure your passwords match.</li></ul></li>
<li>Username: <input type="text" name="username" value="adrian" maxlength="10" /></li>
-<li>Password1: <input type="password" name="password1" value="foo" /></li>
-<li>Password2: <input type="password" name="password2" value="bar" /></li>
+<li>Password1: <input type="password" name="password1" /></li>
+<li>Password2: <input type="password" name="password2" /></li>
>>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'}, auto_id=False)
>>> f.errors
{}
@@ -1258,20 +1270,20 @@ to a Field class. This help text is displayed when a Form is rendered.
... password = CharField(widget=PasswordInput, help_text='Choose wisely.')
>>> p = UserRegistration(auto_id=False)
>>> print p.as_ul()
-<li>Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</li>
-<li>Password: <input type="password" name="password" /> Choose wisely.</li>
+<li>Username: <input type="text" name="username" maxlength="10" /> <span class="helptext">e.g., user@example.com</span></li>
+<li>Password: <input type="password" name="password" /> <span class="helptext">Choose wisely.</span></li>
>>> print p.as_p()
-<p>Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</p>
-<p>Password: <input type="password" name="password" /> Choose wisely.</p>
+<p>Username: <input type="text" name="username" maxlength="10" /> <span class="helptext">e.g., user@example.com</span></p>
+<p>Password: <input type="password" name="password" /> <span class="helptext">Choose wisely.</span></p>
>>> print p.as_table()
-<tr><th>Username:</th><td><input type="text" name="username" maxlength="10" /><br />e.g., user@example.com</td></tr>
-<tr><th>Password:</th><td><input type="password" name="password" /><br />Choose wisely.</td></tr>
+<tr><th>Username:</th><td><input type="text" name="username" maxlength="10" /><br /><span class="helptext">e.g., user@example.com</span></td></tr>
+<tr><th>Password:</th><td><input type="password" name="password" /><br /><span class="helptext">Choose wisely.</span></td></tr>
The help text is displayed whether or not data is provided for the form.
>>> p = UserRegistration({'username': u'foo'}, auto_id=False)
>>> print p.as_ul()
-<li>Username: <input type="text" name="username" value="foo" maxlength="10" /> e.g., user@example.com</li>
-<li><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /> Choose wisely.</li>
+<li>Username: <input type="text" name="username" value="foo" maxlength="10" /> <span class="helptext">e.g., user@example.com</span></li>
+<li><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /> <span class="helptext">Choose wisely.</span></li>
help_text is not displayed for hidden fields. It can be used for documentation
purposes, though.
@@ -1281,7 +1293,7 @@ purposes, though.
... next = CharField(widget=HiddenInput, initial='/', help_text='Redirect destination')
>>> p = UserRegistration(auto_id=False)
>>> print p.as_ul()
-<li>Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</li>
+<li>Username: <input type="text" name="username" maxlength="10" /> <span class="helptext">e.g., user@example.com</span></li>
<li>Password: <input type="password" name="password" /><input type="hidden" name="next" value="/" /></li>
Help text can include arbitrary Unicode characters.
@@ -1289,7 +1301,7 @@ Help text can include arbitrary Unicode characters.
... username = CharField(max_length=10, help_text='ŠĐĆŽćžšđ')
>>> p = UserRegistration(auto_id=False)
>>> p.as_ul()
-u'<li>Username: <input type="text" name="username" maxlength="10" /> \u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111</li>'
+u'<li>Username: <input type="text" name="username" maxlength="10" /> <span class="helptext">\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111</span></li>'
# Subclassing forms ###########################################################
@@ -1589,8 +1601,8 @@ Case 2: POST with erroneous data (a redisplayed form, with errors).
<table>
<tr><td colspan="2"><ul class="errorlist"><li>Please make sure your passwords match.</li></ul></td></tr>
<tr><th>Username:</th><td><ul class="errorlist"><li>Ensure this value has at most 10 characters (it has 23).</li></ul><input type="text" name="username" value="this-is-a-long-username" maxlength="10" /></td></tr>
-<tr><th>Password1:</th><td><input type="password" name="password1" value="foo" /></td></tr>
-<tr><th>Password2:</th><td><input type="password" name="password2" value="bar" /></td></tr>
+<tr><th>Password1:</th><td><input type="password" name="password1" /></td></tr>
+<tr><th>Password2:</th><td><input type="password" name="password2" /></td></tr>
</table>
<input type="submit" />
</form>
@@ -1719,8 +1731,8 @@ the list of errors is empty). You can also use it in {% if %} statements.
>>> print t.render(Context({'form': UserRegistration({'username': 'django', 'password1': 'foo', 'password2': 'bar'}, auto_id=False)}))
<form action="">
<p><label>Your username: <input type="text" name="username" value="django" maxlength="10" /></label></p>
-<p><label>Password: <input type="password" name="password1" value="foo" /></label></p>
-<p><label>Password (again): <input type="password" name="password2" value="bar" /></label></p>
+<p><label>Password: <input type="password" name="password1" /></label></p>
+<p><label>Password (again): <input type="password" name="password2" /></label></p>
<input type="submit" />
</form>
>>> t = Template('''<form action="">
@@ -1734,8 +1746,8 @@ the list of errors is empty). You can also use it in {% if %} statements.
<form action="">
<ul class="errorlist"><li>Please make sure your passwords match.</li></ul>
<p><label>Your username: <input type="text" name="username" value="django" maxlength="10" /></label></p>
-<p><label>Password: <input type="password" name="password1" value="foo" /></label></p>
-<p><label>Password (again): <input type="password" name="password2" value="bar" /></label></p>
+<p><label>Password: <input type="password" name="password1" /></label></p>
+<p><label>Password (again): <input type="password" name="password2" /></label></p>
<input type="submit" />
</form>
diff --git a/tests/regressiontests/forms/input_formats.py b/tests/regressiontests/forms/input_formats.py
new file mode 100644
index 0000000000..498c6de9fb
--- /dev/null
+++ b/tests/regressiontests/forms/input_formats.py
@@ -0,0 +1,894 @@
+from datetime import time, date, datetime
+from unittest import TestCase
+
+from django import forms
+from django.conf import settings
+from django.utils.translation import activate, deactivate
+
+
+class LocalizedTimeTests(TestCase):
+ def setUp(self):
+ self.old_TIME_INPUT_FORMATS = settings.TIME_INPUT_FORMATS
+ self.old_USE_L10N = settings.USE_L10N
+
+ settings.TIME_INPUT_FORMATS = ["%I:%M:%S %p", "%I:%M %p"]
+ settings.USE_L10N = True
+
+ activate('de')
+
+ def tearDown(self):
+ settings.TIME_INPUT_FORMATS = self.old_TIME_INPUT_FORMATS
+ settings.USE_L10N = self.old_USE_L10N
+
+ deactivate()
+
+ def test_timeField(self):
+ "TimeFields can parse dates in the default format"
+ f = forms.TimeField()
+ # Parse a time in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM')
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('13:30:05')
+ self.assertEqual(result, time(13,30,5))
+
+ # Check that the parsed result does a round trip
+ text = f.widget._format_value(result)
+ self.assertEqual(text, '13:30:05')
+
+ # Parse a time in a valid, but non-default format, get a parsed result
+ result = f.clean('13:30')
+ self.assertEqual(result, time(13,30,0))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "13:30:00")
+
+ def test_localized_timeField(self):
+ "Localized TimeFields act as unlocalized widgets"
+ f = forms.TimeField(localize=True)
+ # Parse a time in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM')
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('13:30:05')
+ self.assertEqual(result, time(13,30,5))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, '13:30:05')
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('13:30')
+ self.assertEqual(result, time(13,30,0))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "13:30:00")
+
+ def test_timeField_with_inputformat(self):
+ "TimeFields with manually specified input formats can accept those formats"
+ f = forms.TimeField(input_formats=["%H.%M.%S", "%H.%M"])
+ # Parse a time in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM')
+ self.assertRaises(forms.ValidationError, f.clean, '13:30:05')
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('13.30.05')
+ self.assertEqual(result, time(13,30,5))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "13:30:05")
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('13.30')
+ self.assertEqual(result, time(13,30,0))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "13:30:00")
+
+ def test_localized_timeField_with_inputformat(self):
+ "Localized TimeFields with manually specified input formats can accept those formats"
+ f = forms.TimeField(input_formats=["%H.%M.%S", "%H.%M"], localize=True)
+ # Parse a time in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM')
+ self.assertRaises(forms.ValidationError, f.clean, '13:30:05')
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('13.30.05')
+ self.assertEqual(result, time(13,30,5))
+
+ # # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "13:30:05")
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('13.30')
+ self.assertEqual(result, time(13,30,0))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "13:30:00")
+
+
+class CustomTimeInputFormatsTests(TestCase):
+ def setUp(self):
+ self.old_TIME_INPUT_FORMATS = settings.TIME_INPUT_FORMATS
+ settings.TIME_INPUT_FORMATS = ["%I:%M:%S %p", "%I:%M %p"]
+
+ def tearDown(self):
+ settings.TIME_INPUT_FORMATS = self.old_TIME_INPUT_FORMATS
+
+ def test_timeField(self):
+ "TimeFields can parse dates in the default format"
+ f = forms.TimeField()
+ # Parse a time in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '13:30:05')
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('1:30:05 PM')
+ self.assertEqual(result, time(13,30,5))
+
+ # Check that the parsed result does a round trip
+ text = f.widget._format_value(result)
+ self.assertEqual(text, '01:30:05 PM')
+
+ # Parse a time in a valid, but non-default format, get a parsed result
+ result = f.clean('1:30 PM')
+ self.assertEqual(result, time(13,30,0))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "01:30:00 PM")
+
+ def test_localized_timeField(self):
+ "Localized TimeFields act as unlocalized widgets"
+ f = forms.TimeField(localize=True)
+ # Parse a time in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '13:30:05')
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('1:30:05 PM')
+ self.assertEqual(result, time(13,30,5))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, '01:30:05 PM')
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('01:30 PM')
+ self.assertEqual(result, time(13,30,0))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "01:30:00 PM")
+
+ def test_timeField_with_inputformat(self):
+ "TimeFields with manually specified input formats can accept those formats"
+ f = forms.TimeField(input_formats=["%H.%M.%S", "%H.%M"])
+ # Parse a time in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM')
+ self.assertRaises(forms.ValidationError, f.clean, '13:30:05')
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('13.30.05')
+ self.assertEqual(result, time(13,30,5))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "01:30:05 PM")
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('13.30')
+ self.assertEqual(result, time(13,30,0))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "01:30:00 PM")
+
+ def test_localized_timeField_with_inputformat(self):
+ "Localized TimeFields with manually specified input formats can accept those formats"
+ f = forms.TimeField(input_formats=["%H.%M.%S", "%H.%M"], localize=True)
+ # Parse a time in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM')
+ self.assertRaises(forms.ValidationError, f.clean, '13:30:05')
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('13.30.05')
+ self.assertEqual(result, time(13,30,5))
+
+ # # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "01:30:05 PM")
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('13.30')
+ self.assertEqual(result, time(13,30,0))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "01:30:00 PM")
+
+
+class SimpleTimeFormatTests(TestCase):
+ def test_timeField(self):
+ "TimeFields can parse dates in the default format"
+ f = forms.TimeField()
+ # Parse a time in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM')
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('13:30:05')
+ self.assertEqual(result, time(13,30,5))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "13:30:05")
+
+ # Parse a time in a valid, but non-default format, get a parsed result
+ result = f.clean('13:30')
+ self.assertEqual(result, time(13,30,0))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "13:30:00")
+
+ def test_localized_timeField(self):
+ "Localized TimeFields in a non-localized environment act as unlocalized widgets"
+ f = forms.TimeField()
+ # Parse a time in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM')
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('13:30:05')
+ self.assertEqual(result, time(13,30,5))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "13:30:05")
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('13:30')
+ self.assertEqual(result, time(13,30,0))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "13:30:00")
+
+ def test_timeField_with_inputformat(self):
+ "TimeFields with manually specified input formats can accept those formats"
+ f = forms.TimeField(input_formats=["%I:%M:%S %p", "%I:%M %p"])
+ # Parse a time in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '13:30:05')
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('1:30:05 PM')
+ self.assertEqual(result, time(13,30,5))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "13:30:05")
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('1:30 PM')
+ self.assertEqual(result, time(13,30,0))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "13:30:00")
+
+ def test_localized_timeField_with_inputformat(self):
+ "Localized TimeFields with manually specified input formats can accept those formats"
+ f = forms.TimeField(input_formats=["%I:%M:%S %p", "%I:%M %p"], localize=True)
+ # Parse a time in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '13:30:05')
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('1:30:05 PM')
+ self.assertEqual(result, time(13,30,5))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "13:30:05")
+
+ # Parse a time in a valid format, get a parsed result
+ result = f.clean('1:30 PM')
+ self.assertEqual(result, time(13,30,0))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "13:30:00")
+
+
+class LocalizedDateTests(TestCase):
+ def setUp(self):
+ self.old_DATE_INPUT_FORMATS = settings.DATE_INPUT_FORMATS
+ self.old_USE_L10N = settings.USE_L10N
+
+ settings.DATE_INPUT_FORMATS = ["%d/%m/%Y", "%d-%m-%Y"]
+ settings.USE_L10N = True
+
+ activate('de')
+
+ def tearDown(self):
+ settings.DATE_INPUT_FORMATS = self.old_DATE_INPUT_FORMATS
+ settings.USE_L10N = self.old_USE_L10N
+
+ deactivate()
+
+ def test_dateField(self):
+ "DateFields can parse dates in the default format"
+ f = forms.DateField()
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '21/12/2010')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('21.12.2010')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip
+ text = f.widget._format_value(result)
+ self.assertEqual(text, '21.12.2010')
+
+ # Parse a date in a valid, but non-default format, get a parsed result
+ result = f.clean('21.12.10')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "21.12.2010")
+
+ def test_localized_dateField(self):
+ "Localized DateFields act as unlocalized widgets"
+ f = forms.DateField(localize=True)
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '21/12/2010')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('21.12.2010')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, '21.12.2010')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('21.12.10')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "21.12.2010")
+
+ def test_dateField_with_inputformat(self):
+ "DateFields with manually specified input formats can accept those formats"
+ f = forms.DateField(input_formats=["%m.%d.%Y", "%m-%d-%Y"])
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '2010-12-21')
+ self.assertRaises(forms.ValidationError, f.clean, '21/12/2010')
+ self.assertRaises(forms.ValidationError, f.clean, '21.12.2010')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('12.21.2010')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "21.12.2010")
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('12-21-2010')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "21.12.2010")
+
+ def test_localized_dateField_with_inputformat(self):
+ "Localized DateFields with manually specified input formats can accept those formats"
+ f = forms.DateField(input_formats=["%m.%d.%Y", "%m-%d-%Y"], localize=True)
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '2010-12-21')
+ self.assertRaises(forms.ValidationError, f.clean, '21/12/2010')
+ self.assertRaises(forms.ValidationError, f.clean, '21.12.2010')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('12.21.2010')
+ self.assertEqual(result, date(2010,12,21))
+
+ # # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "21.12.2010")
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('12-21-2010')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "21.12.2010")
+
+class CustomDateInputFormatsTests(TestCase):
+ def setUp(self):
+ self.old_DATE_INPUT_FORMATS = settings.DATE_INPUT_FORMATS
+ settings.DATE_INPUT_FORMATS = ["%d.%m.%Y", "%d-%m-%Y"]
+
+ def tearDown(self):
+ settings.DATE_INPUT_FORMATS = self.old_DATE_INPUT_FORMATS
+
+ def test_dateField(self):
+ "DateFields can parse dates in the default format"
+ f = forms.DateField()
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '2010-12-21')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('21.12.2010')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip
+ text = f.widget._format_value(result)
+ self.assertEqual(text, '21.12.2010')
+
+ # Parse a date in a valid, but non-default format, get a parsed result
+ result = f.clean('21-12-2010')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "21.12.2010")
+
+ def test_localized_dateField(self):
+ "Localized DateFields act as unlocalized widgets"
+ f = forms.DateField(localize=True)
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '2010-12-21')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('21.12.2010')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, '21.12.2010')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('21-12-2010')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "21.12.2010")
+
+ def test_dateField_with_inputformat(self):
+ "DateFields with manually specified input formats can accept those formats"
+ f = forms.DateField(input_formats=["%m.%d.%Y", "%m-%d-%Y"])
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '21.12.2010')
+ self.assertRaises(forms.ValidationError, f.clean, '2010-12-21')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('12.21.2010')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "21.12.2010")
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('12-21-2010')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "21.12.2010")
+
+ def test_localized_dateField_with_inputformat(self):
+ "Localized DateFields with manually specified input formats can accept those formats"
+ f = forms.DateField(input_formats=["%m.%d.%Y", "%m-%d-%Y"], localize=True)
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '21.12.2010')
+ self.assertRaises(forms.ValidationError, f.clean, '2010-12-21')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('12.21.2010')
+ self.assertEqual(result, date(2010,12,21))
+
+ # # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "21.12.2010")
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('12-21-2010')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "21.12.2010")
+
+class SimpleDateFormatTests(TestCase):
+ def test_dateField(self):
+ "DateFields can parse dates in the default format"
+ f = forms.DateField()
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '21.12.2010')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('2010-12-21')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "2010-12-21")
+
+ # Parse a date in a valid, but non-default format, get a parsed result
+ result = f.clean('12/21/2010')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "2010-12-21")
+
+ def test_localized_dateField(self):
+ "Localized DateFields in a non-localized environment act as unlocalized widgets"
+ f = forms.DateField()
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '21.12.2010')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('2010-12-21')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "2010-12-21")
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('12/21/2010')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "2010-12-21")
+
+ def test_dateField_with_inputformat(self):
+ "DateFields with manually specified input formats can accept those formats"
+ f = forms.DateField(input_formats=["%d.%m.%Y", "%d-%m-%Y"])
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '2010-12-21')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('21.12.2010')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "2010-12-21")
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('21-12-2010')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "2010-12-21")
+
+ def test_localized_dateField_with_inputformat(self):
+ "Localized DateFields with manually specified input formats can accept those formats"
+ f = forms.DateField(input_formats=["%d.%m.%Y", "%d-%m-%Y"], localize=True)
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '2010-12-21')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('21.12.2010')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "2010-12-21")
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('21-12-2010')
+ self.assertEqual(result, date(2010,12,21))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "2010-12-21")
+
+class LocalizedDateTimeTests(TestCase):
+ def setUp(self):
+ self.old_DATETIME_INPUT_FORMATS = settings.DATETIME_INPUT_FORMATS
+ self.old_USE_L10N = settings.USE_L10N
+
+ settings.DATETIME_INPUT_FORMATS = ["%I:%M:%S %p %d/%m/%Y", "%I:%M %p %d-%m-%Y"]
+ settings.USE_L10N = True
+
+ activate('de')
+
+ def tearDown(self):
+ settings.DATETIME_INPUT_FORMATS = self.old_DATETIME_INPUT_FORMATS
+ settings.USE_L10N = self.old_USE_L10N
+
+ deactivate()
+
+ def test_dateTimeField(self):
+ "DateTimeFields can parse dates in the default format"
+ f = forms.DateTimeField()
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM 21/12/2010')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('21.12.2010 13:30:05')
+ self.assertEqual(result, datetime(2010,12,21,13,30,5))
+
+ # Check that the parsed result does a round trip
+ text = f.widget._format_value(result)
+ self.assertEqual(text, '21.12.2010 13:30:05')
+
+ # Parse a date in a valid, but non-default format, get a parsed result
+ result = f.clean('21.12.2010 13:30')
+ self.assertEqual(result, datetime(2010,12,21,13,30))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "21.12.2010 13:30:00")
+
+ def test_localized_dateTimeField(self):
+ "Localized DateTimeFields act as unlocalized widgets"
+ f = forms.DateTimeField(localize=True)
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM 21/12/2010')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('21.12.2010 13:30:05')
+ self.assertEqual(result, datetime(2010,12,21,13,30,5))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, '21.12.2010 13:30:05')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('21.12.2010 13:30')
+ self.assertEqual(result, datetime(2010,12,21,13,30))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "21.12.2010 13:30:00")
+
+ def test_dateTimeField_with_inputformat(self):
+ "DateTimeFields with manually specified input formats can accept those formats"
+ f = forms.DateTimeField(input_formats=["%H.%M.%S %m.%d.%Y", "%H.%M %m-%d-%Y"])
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '2010-12-21 13:30:05 13:30:05')
+ self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM 21/12/2010')
+ self.assertRaises(forms.ValidationError, f.clean, '13:30:05 21.12.2010')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('13.30.05 12.21.2010')
+ self.assertEqual(result, datetime(2010,12,21,13,30,5))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "21.12.2010 13:30:05")
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('13.30 12-21-2010')
+ self.assertEqual(result, datetime(2010,12,21,13,30))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "21.12.2010 13:30:00")
+
+ def test_localized_dateTimeField_with_inputformat(self):
+ "Localized DateTimeFields with manually specified input formats can accept those formats"
+ f = forms.DateTimeField(input_formats=["%H.%M.%S %m.%d.%Y", "%H.%M %m-%d-%Y"], localize=True)
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '2010-12-21 13:30:05')
+ self.assertRaises(forms.ValidationError, f.clean, '1:30:05 PM 21/12/2010')
+ self.assertRaises(forms.ValidationError, f.clean, '13:30:05 21.12.2010')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('13.30.05 12.21.2010')
+ self.assertEqual(result, datetime(2010,12,21,13,30,5))
+
+ # # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "21.12.2010 13:30:05")
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('13.30 12-21-2010')
+ self.assertEqual(result, datetime(2010,12,21,13,30))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "21.12.2010 13:30:00")
+
+
+class CustomDateTimeInputFormatsTests(TestCase):
+ def setUp(self):
+ self.old_DATETIME_INPUT_FORMATS = settings.DATETIME_INPUT_FORMATS
+ settings.DATETIME_INPUT_FORMATS = ["%I:%M:%S %p %d/%m/%Y", "%I:%M %p %d-%m-%Y"]
+
+ def tearDown(self):
+ settings.DATETIME_INPUT_FORMATS = self.old_DATETIME_INPUT_FORMATS
+
+ def test_dateTimeField(self):
+ "DateTimeFields can parse dates in the default format"
+ f = forms.DateTimeField()
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '2010-12-21 13:30:05')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('1:30:05 PM 21/12/2010')
+ self.assertEqual(result, datetime(2010,12,21,13,30,5))
+
+ # Check that the parsed result does a round trip
+ text = f.widget._format_value(result)
+ self.assertEqual(text, '01:30:05 PM 21/12/2010')
+
+ # Parse a date in a valid, but non-default format, get a parsed result
+ result = f.clean('1:30 PM 21-12-2010')
+ self.assertEqual(result, datetime(2010,12,21,13,30))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "01:30:00 PM 21/12/2010")
+
+ def test_localized_dateTimeField(self):
+ "Localized DateTimeFields act as unlocalized widgets"
+ f = forms.DateTimeField(localize=True)
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '2010-12-21 13:30:05')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('1:30:05 PM 21/12/2010')
+ self.assertEqual(result, datetime(2010,12,21,13,30,5))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, '01:30:05 PM 21/12/2010')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('1:30 PM 21-12-2010')
+ self.assertEqual(result, datetime(2010,12,21,13,30))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "01:30:00 PM 21/12/2010")
+
+ def test_dateTimeField_with_inputformat(self):
+ "DateTimeFields with manually specified input formats can accept those formats"
+ f = forms.DateTimeField(input_formats=["%m.%d.%Y %H:%M:%S", "%m-%d-%Y %H:%M"])
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '13:30:05 21.12.2010')
+ self.assertRaises(forms.ValidationError, f.clean, '2010-12-21 13:30:05')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('12.21.2010 13:30:05')
+ self.assertEqual(result, datetime(2010,12,21,13,30,5))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "01:30:05 PM 21/12/2010")
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('12-21-2010 13:30')
+ self.assertEqual(result, datetime(2010,12,21,13,30))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "01:30:00 PM 21/12/2010")
+
+ def test_localized_dateTimeField_with_inputformat(self):
+ "Localized DateTimeFields with manually specified input formats can accept those formats"
+ f = forms.DateTimeField(input_formats=["%m.%d.%Y %H:%M:%S", "%m-%d-%Y %H:%M"], localize=True)
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '13:30:05 21.12.2010')
+ self.assertRaises(forms.ValidationError, f.clean, '2010-12-21 13:30:05')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('12.21.2010 13:30:05')
+ self.assertEqual(result, datetime(2010,12,21,13,30,5))
+
+ # # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "01:30:05 PM 21/12/2010")
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('12-21-2010 13:30')
+ self.assertEqual(result, datetime(2010,12,21,13,30))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "01:30:00 PM 21/12/2010")
+
+class SimpleDateTimeFormatTests(TestCase):
+ def test_dateTimeField(self):
+ "DateTimeFields can parse dates in the default format"
+ f = forms.DateTimeField()
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '13:30:05 21.12.2010')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('2010-12-21 13:30:05')
+ self.assertEqual(result, datetime(2010,12,21,13,30,5))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "2010-12-21 13:30:05")
+
+ # Parse a date in a valid, but non-default format, get a parsed result
+ result = f.clean('12/21/2010 13:30:05')
+ self.assertEqual(result, datetime(2010,12,21,13,30,5))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "2010-12-21 13:30:05")
+
+ def test_localized_dateTimeField(self):
+ "Localized DateTimeFields in a non-localized environment act as unlocalized widgets"
+ f = forms.DateTimeField()
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '13:30:05 21.12.2010')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('2010-12-21 13:30:05')
+ self.assertEqual(result, datetime(2010,12,21,13,30,5))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "2010-12-21 13:30:05")
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('12/21/2010 13:30:05')
+ self.assertEqual(result, datetime(2010,12,21,13,30,5))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "2010-12-21 13:30:05")
+
+ def test_dateTimeField_with_inputformat(self):
+ "DateTimeFields with manually specified input formats can accept those formats"
+ f = forms.DateTimeField(input_formats=["%I:%M:%S %p %d.%m.%Y", "%I:%M %p %d-%m-%Y"])
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '2010-12-21 13:30:05')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('1:30:05 PM 21.12.2010')
+ self.assertEqual(result, datetime(2010,12,21,13,30,5))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "2010-12-21 13:30:05")
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('1:30 PM 21-12-2010')
+ self.assertEqual(result, datetime(2010,12,21,13,30))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "2010-12-21 13:30:00")
+
+ def test_localized_dateTimeField_with_inputformat(self):
+ "Localized DateTimeFields with manually specified input formats can accept those formats"
+ f = forms.DateTimeField(input_formats=["%I:%M:%S %p %d.%m.%Y", "%I:%M %p %d-%m-%Y"], localize=True)
+ # Parse a date in an unaccepted format; get an error
+ self.assertRaises(forms.ValidationError, f.clean, '2010-12-21 13:30:05')
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('1:30:05 PM 21.12.2010')
+ self.assertEqual(result, datetime(2010,12,21,13,30,5))
+
+ # Check that the parsed result does a round trip to the same format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "2010-12-21 13:30:05")
+
+ # Parse a date in a valid format, get a parsed result
+ result = f.clean('1:30 PM 21-12-2010')
+ self.assertEqual(result, datetime(2010,12,21,13,30))
+
+ # Check that the parsed result does a round trip to default format
+ text = f.widget._format_value(result)
+ self.assertEqual(text, "2010-12-21 13:30:00")
diff --git a/tests/regressiontests/forms/localflavor/au.py b/tests/regressiontests/forms/localflavor/au.py
index fd4c0d6980..cda782094a 100644
--- a/tests/regressiontests/forms/localflavor/au.py
+++ b/tests/regressiontests/forms/localflavor/au.py
@@ -50,7 +50,7 @@ u''
## AUPhoneNumberField ########################################################
A field that accepts a 10 digit Australian phone number.
-llows spaces and parentheses around area code.
+Allows spaces and parentheses around area code.
>>> from django.contrib.localflavor.au.forms import AUPhoneNumberField
>>> f = AUPhoneNumberField()
diff --git a/tests/regressiontests/forms/models.py b/tests/regressiontests/forms/models.py
index 229c50556e..028ff9bad2 100644
--- a/tests/regressiontests/forms/models.py
+++ b/tests/regressiontests/forms/models.py
@@ -38,11 +38,28 @@ class ChoiceOptionModel(models.Model):
Can't reuse ChoiceModel because error_message tests require that it have no instances."""
name = models.CharField(max_length=10)
+ class Meta:
+ ordering = ('name',)
+
+ def __unicode__(self):
+ return u'ChoiceOption %d' % self.pk
+
class ChoiceFieldModel(models.Model):
"""Model with ForeignKey to another model, for testing ModelForm
generation with ModelChoiceField."""
choice = models.ForeignKey(ChoiceOptionModel, blank=False,
- default=lambda: ChoiceOptionModel.objects.all()[0])
+ default=lambda: ChoiceOptionModel.objects.get(name='default'))
+ choice_int = models.ForeignKey(ChoiceOptionModel, blank=False, related_name='choice_int',
+ default=lambda: 1)
+
+ multi_choice = models.ManyToManyField(ChoiceOptionModel, blank=False, related_name='multi_choice',
+ default=lambda: ChoiceOptionModel.objects.filter(name='default'))
+ multi_choice_int = models.ManyToManyField(ChoiceOptionModel, blank=False, related_name='multi_choice_int',
+ default=lambda: [1])
+
+class ChoiceFieldForm(django_forms.ModelForm):
+ class Meta:
+ model = ChoiceFieldModel
class FileModel(models.Model):
file = models.FileField(storage=temp_storage, upload_to='tests')
@@ -74,6 +91,74 @@ class TestTicket12510(TestCase):
# only one query is required to pull the model from DB
self.assertEqual(initial_queries+1, len(connection.queries))
+class ModelFormCallableModelDefault(TestCase):
+ def test_no_empty_option(self):
+ "If a model's ForeignKey has blank=False and a default, no empty option is created (Refs #10792)."
+ option = ChoiceOptionModel.objects.create(name='default')
+
+ choices = list(ChoiceFieldForm().fields['choice'].choices)
+ self.assertEquals(len(choices), 1)
+ self.assertEquals(choices[0], (option.pk, unicode(option)))
+
+ def test_callable_initial_value(self):
+ "The initial value for a callable default returning a queryset is the pk (refs #13769)"
+ obj1 = ChoiceOptionModel.objects.create(id=1, name='default')
+ obj2 = ChoiceOptionModel.objects.create(id=2, name='option 2')
+ obj3 = ChoiceOptionModel.objects.create(id=3, name='option 3')
+ self.assertEquals(ChoiceFieldForm().as_p(), """<p><label for="id_choice">Choice:</label> <select name="choice" id="id_choice">
+<option value="1" selected="selected">ChoiceOption 1</option>
+<option value="2">ChoiceOption 2</option>
+<option value="3">ChoiceOption 3</option>
+</select><input type="hidden" name="initial-choice" value="1" id="initial-id_choice" /></p>
+<p><label for="id_choice_int">Choice int:</label> <select name="choice_int" id="id_choice_int">
+<option value="1" selected="selected">ChoiceOption 1</option>
+<option value="2">ChoiceOption 2</option>
+<option value="3">ChoiceOption 3</option>
+</select><input type="hidden" name="initial-choice_int" value="1" id="initial-id_choice_int" /></p>
+<p><label for="id_multi_choice">Multi choice:</label> <select multiple="multiple" name="multi_choice" id="id_multi_choice">
+<option value="1" selected="selected">ChoiceOption 1</option>
+<option value="2">ChoiceOption 2</option>
+<option value="3">ChoiceOption 3</option>
+</select><input type="hidden" name="initial-multi_choice" value="1" id="initial-id_multi_choice_0" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>
+<p><label for="id_multi_choice_int">Multi choice int:</label> <select multiple="multiple" name="multi_choice_int" id="id_multi_choice_int">
+<option value="1" selected="selected">ChoiceOption 1</option>
+<option value="2">ChoiceOption 2</option>
+<option value="3">ChoiceOption 3</option>
+</select><input type="hidden" name="initial-multi_choice_int" value="1" id="initial-id_multi_choice_int_0" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>""")
+
+ def test_initial_instance_value(self):
+ "Initial instances for model fields may also be instances (refs #7287)"
+ obj1 = ChoiceOptionModel.objects.create(id=1, name='default')
+ obj2 = ChoiceOptionModel.objects.create(id=2, name='option 2')
+ obj3 = ChoiceOptionModel.objects.create(id=3, name='option 3')
+ self.assertEquals(ChoiceFieldForm(initial={
+ 'choice': obj2,
+ 'choice_int': obj2,
+ 'multi_choice': [obj2,obj3],
+ 'multi_choice_int': ChoiceOptionModel.objects.exclude(name="default"),
+ }).as_p(), """<p><label for="id_choice">Choice:</label> <select name="choice" id="id_choice">
+<option value="1">ChoiceOption 1</option>
+<option value="2" selected="selected">ChoiceOption 2</option>
+<option value="3">ChoiceOption 3</option>
+</select><input type="hidden" name="initial-choice" value="2" id="initial-id_choice" /></p>
+<p><label for="id_choice_int">Choice int:</label> <select name="choice_int" id="id_choice_int">
+<option value="1">ChoiceOption 1</option>
+<option value="2" selected="selected">ChoiceOption 2</option>
+<option value="3">ChoiceOption 3</option>
+</select><input type="hidden" name="initial-choice_int" value="2" id="initial-id_choice_int" /></p>
+<p><label for="id_multi_choice">Multi choice:</label> <select multiple="multiple" name="multi_choice" id="id_multi_choice">
+<option value="1">ChoiceOption 1</option>
+<option value="2" selected="selected">ChoiceOption 2</option>
+<option value="3" selected="selected">ChoiceOption 3</option>
+</select><input type="hidden" name="initial-multi_choice" value="2" id="initial-id_multi_choice_0" />
+<input type="hidden" name="initial-multi_choice" value="3" id="initial-id_multi_choice_1" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>
+<p><label for="id_multi_choice_int">Multi choice int:</label> <select multiple="multiple" name="multi_choice_int" id="id_multi_choice_int">
+<option value="1">ChoiceOption 1</option>
+<option value="2" selected="selected">ChoiceOption 2</option>
+<option value="3" selected="selected">ChoiceOption 3</option>
+</select><input type="hidden" name="initial-multi_choice_int" value="2" id="initial-id_multi_choice_int_0" />
+<input type="hidden" name="initial-multi_choice_int" value="3" id="initial-id_multi_choice_int_1" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>""")
+
__test__ = {'API_TESTS': """
>>> from django.forms.models import ModelForm
@@ -155,18 +240,5 @@ u'class default value'
datetime.date(1999, 3, 2)
>>> shutil.rmtree(temp_storage_location)
-In a ModelForm with a ModelChoiceField, if the model's ForeignKey has blank=False and a default,
-no empty option is created (regression test for #10792).
-
-First we need at least one instance of ChoiceOptionModel:
-
->>> ChoiceOptionModel.objects.create(name='default')
-<ChoiceOptionModel: ChoiceOptionModel object>
-
->>> class ChoiceFieldForm(ModelForm):
-... class Meta:
-... model = ChoiceFieldModel
->>> list(ChoiceFieldForm().fields['choice'].choices)
-[(1, u'ChoiceOptionModel object')]
"""}
diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py
index 8757e799a9..7a91cb701e 100644
--- a/tests/regressiontests/forms/tests.py
+++ b/tests/regressiontests/forms/tests.py
@@ -41,6 +41,8 @@ from fields import FieldsTests
from validators import TestFieldWithValidators
from widgets import WidgetTests
+from input_formats import *
+
__test__ = {
'extra_tests': extra_tests,
'form_tests': form_tests,
diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py
index 39d7d569a3..59b33d5210 100644
--- a/tests/regressiontests/forms/widgets.py
+++ b/tests/regressiontests/forms/widgets.py
@@ -62,6 +62,17 @@ u'<input type="text" class="special" name="email" />'
u'<input type="password" name="email" />'
>>> w.render('email', None)
u'<input type="password" name="email" />'
+>>> w.render('email', 'secret')
+u'<input type="password" name="email" />'
+
+The render_value argument lets you specify whether the widget should render
+its value. For security reasons, this is off by default.
+
+>>> w = PasswordInput(render_value=True)
+>>> w.render('email', '')
+u'<input type="password" name="email" />'
+>>> w.render('email', None)
+u'<input type="password" name="email" />'
>>> w.render('email', 'test@example.com')
u'<input type="password" name="email" value="test@example.com" />'
>>> w.render('email', 'some "quoted" & ampersanded value')
@@ -70,36 +81,20 @@ u'<input type="password" name="email" value="some &quot;quoted&quot; &amp; amper
u'<input type="password" name="email" value="test@example.com" class="fun" />'
You can also pass 'attrs' to the constructor:
->>> w = PasswordInput(attrs={'class': 'fun'})
+>>> w = PasswordInput(attrs={'class': 'fun'}, render_value=True)
>>> w.render('email', '')
u'<input type="password" class="fun" name="email" />'
>>> w.render('email', 'foo@example.com')
u'<input type="password" class="fun" value="foo@example.com" name="email" />'
'attrs' passed to render() get precedence over those passed to the constructor:
->>> w = PasswordInput(attrs={'class': 'pretty'})
+>>> w = PasswordInput(attrs={'class': 'pretty'}, render_value=True)
>>> w.render('email', '', attrs={'class': 'special'})
u'<input type="password" class="special" name="email" />'
>>> w.render('email', 'ŠĐĆŽćžšđ', attrs={'class': 'fun'})
u'<input type="password" class="fun" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" name="email" />'
-The render_value argument lets you specify whether the widget should render
-its value. You may want to do this for security reasons.
->>> w = PasswordInput(render_value=True)
->>> w.render('email', 'secret')
-u'<input type="password" name="email" value="secret" />'
->>> w = PasswordInput(render_value=False)
->>> w.render('email', '')
-u'<input type="password" name="email" />'
->>> w.render('email', None)
-u'<input type="password" name="email" />'
->>> w.render('email', 'secret')
-u'<input type="password" name="email" />'
->>> w = PasswordInput(attrs={'class': 'fun'}, render_value=False)
->>> w.render('email', 'secret')
-u'<input type="password" class="fun" name="email" />'
-
# HiddenInput Widget ############################################################
>>> w = HiddenInput()
@@ -1286,7 +1281,7 @@ class SelectAndTextWidget(forms.MultiWidget):
forms.TextInput
]
super(SelectAndTextWidget, self).__init__(widgets)
-
+
def _set_choices(self, choices):
"""
When choices are set for this widget, we want to pass those along to the Select widget
@@ -1310,3 +1305,21 @@ class WidgetTests(TestCase):
# w2 ought to be independent of w1, since MultiWidget ought
# to make a copy of its sub-widgets when it is copied.
self.assertEqual(w1.choices, [1,2,3])
+
+ def test_13390(self):
+ # See ticket #13390
+ class SplitDateForm(forms.Form):
+ field = forms.DateTimeField(widget=forms.SplitDateTimeWidget, required=False)
+
+ form = SplitDateForm({'field': ''})
+ self.assertTrue(form.is_valid())
+ form = SplitDateForm({'field': ['', '']})
+ self.assertTrue(form.is_valid())
+
+ class SplitDateRequiredForm(forms.Form):
+ field = forms.DateTimeField(widget=forms.SplitDateTimeWidget, required=True)
+
+ form = SplitDateRequiredForm({'field': ''})
+ self.assertFalse(form.is_valid())
+ form = SplitDateRequiredForm({'field': ['', '']})
+ self.assertFalse(form.is_valid())
diff --git a/tests/regressiontests/httpwrappers/tests.py b/tests/regressiontests/httpwrappers/tests.py
index 132472ca37..d3991aad86 100644
--- a/tests/regressiontests/httpwrappers/tests.py
+++ b/tests/regressiontests/httpwrappers/tests.py
@@ -1,476 +1,237 @@
-"""
-###################
-# Empty QueryDict #
-###################
-
->>> q = QueryDict('')
-
->>> q['foo']
-Traceback (most recent call last):
-...
-MultiValueDictKeyError: "Key 'foo' not found in <QueryDict: {}>"
-
->>> q['something'] = 'bar'
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.get('foo', 'default')
-'default'
-
->>> q.getlist('foo')
-[]
-
->>> q.setlist('foo', ['bar', 'baz'])
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.appendlist('foo', ['bar'])
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.has_key('foo')
-False
-
->>> 'foo' in q
-False
-
->>> q.items()
-[]
-
->>> q.lists()
-[]
-
->>> q.keys()
-[]
-
->>> q.values()
-[]
-
->>> len(q)
-0
-
->>> q.update({'foo': 'bar'})
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.pop('foo')
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.popitem()
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.clear()
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.setdefault('foo', 'bar')
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.urlencode()
-''
-
-###################################
-# Mutable copy of empty QueryDict #
-###################################
-
->>> q = q.copy()
-
->>> q['foo']
-Traceback (most recent call last):
-...
-MultiValueDictKeyError: "Key 'foo' not found in <QueryDict: {}>"
-
->>> q['name'] = 'john'
-
->>> q['name']
-u'john'
-
->>> del q['name']
->>> 'name' in q
-False
-
->>> q['name'] = 'john'
-
->>> q.get('foo', 'default')
-'default'
-
->>> q.get('name', 'default')
-u'john'
-
->>> q.getlist('name')
-[u'john']
-
->>> q.getlist('foo')
-[]
-
->>> q.setlist('foo', ['bar', 'baz'])
-
->>> q.get('foo', 'default')
-u'baz'
-
->>> q.getlist('foo')
-[u'bar', u'baz']
-
->>> q.appendlist('foo', 'another')
-
->>> q.getlist('foo')
-[u'bar', u'baz', u'another']
-
->>> q['foo']
-u'another'
-
->>> q.has_key('foo')
-True
-
->>> 'foo' in q
-True
-
->>> q.items()
-[(u'foo', u'another'), (u'name', u'john')]
-
->>> q.lists()
-[(u'foo', [u'bar', u'baz', u'another']), (u'name', [u'john'])]
-
->>> q.keys()
-[u'foo', u'name']
-
->>> q.values()
-[u'another', u'john']
-
->>> len(q)
-2
-
->>> q.update({'foo': 'hello'})
-
-# Displays last value
->>> q['foo']
-u'hello'
-
->>> q.get('foo', 'not available')
-u'hello'
-
->>> q.getlist('foo')
-[u'bar', u'baz', u'another', u'hello']
-
->>> q.pop('foo')
-[u'bar', u'baz', u'another', u'hello']
-
->>> q.pop('foo', 'not there')
-'not there'
-
->>> q.get('foo', 'not there')
-'not there'
-
->>> q.setdefault('foo', 'bar')
-u'bar'
-
->>> q['foo']
-u'bar'
-
->>> q.getlist('foo')
-[u'bar']
-
->>> q.urlencode()
-'foo=bar&name=john'
-
->>> q.clear()
-
->>> len(q)
-0
-
-#####################################
-# QueryDict with one key/value pair #
-#####################################
-
->>> q = QueryDict('foo=bar')
-
->>> q['foo']
-u'bar'
-
->>> q['bar']
-Traceback (most recent call last):
-...
-MultiValueDictKeyError: "Key 'bar' not found in <QueryDict: {u'foo': [u'bar']}>"
-
->>> q['something'] = 'bar'
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.get('foo', 'default')
-u'bar'
-
->>> q.get('bar', 'default')
-'default'
-
->>> q.getlist('foo')
-[u'bar']
-
->>> q.getlist('bar')
-[]
-
->>> q.setlist('foo', ['bar', 'baz'])
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.appendlist('foo', ['bar'])
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.has_key('foo')
-True
-
->>> 'foo' in q
-True
-
->>> q.has_key('bar')
-False
-
->>> 'bar' in q
-False
-
->>> q.items()
-[(u'foo', u'bar')]
-
->>> q.lists()
-[(u'foo', [u'bar'])]
-
->>> q.keys()
-[u'foo']
-
->>> q.values()
-[u'bar']
-
->>> len(q)
-1
-
->>> q.update({'foo': 'bar'})
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.pop('foo')
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.popitem()
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.clear()
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.setdefault('foo', 'bar')
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.urlencode()
-'foo=bar'
-
-#####################################################
-# QueryDict with two key/value pairs with same keys #
-#####################################################
-
->>> q = QueryDict('vote=yes&vote=no')
-
->>> q['vote']
-u'no'
-
->>> q['something'] = 'bar'
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.get('vote', 'default')
-u'no'
-
->>> q.get('foo', 'default')
-'default'
-
->>> q.getlist('vote')
-[u'yes', u'no']
-
->>> q.getlist('foo')
-[]
-
->>> q.setlist('foo', ['bar', 'baz'])
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.appendlist('foo', ['bar'])
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.has_key('vote')
-True
-
->>> 'vote' in q
-True
-
->>> q.has_key('foo')
-False
-
->>> 'foo' in q
-False
-
->>> q.items()
-[(u'vote', u'no')]
-
->>> q.lists()
-[(u'vote', [u'yes', u'no'])]
-
->>> q.keys()
-[u'vote']
-
->>> q.values()
-[u'no']
-
->>> len(q)
-1
-
->>> q.update({'foo': 'bar'})
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.pop('foo')
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.popitem()
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.clear()
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.setdefault('foo', 'bar')
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
->>> q.urlencode()
-'vote=yes&vote=no'
-
->>> del q['vote']
-Traceback (most recent call last):
-...
-AttributeError: This QueryDict instance is immutable
-
-# QueryDicts must be able to handle invalid input encoding (in this case, bad
-# UTF-8 encoding).
->>> q = QueryDict('foo=bar&foo=\xff')
-
->>> q['foo']
-u'\ufffd'
-
->>> q.getlist('foo')
-[u'bar', u'\ufffd']
-
-
-########################
-# Pickling a QueryDict #
-########################
->>> import pickle
->>> q = QueryDict('')
->>> q1 = pickle.loads(pickle.dumps(q, 2))
->>> q == q1
-True
->>> q = QueryDict('a=b&c=d')
->>> q1 = pickle.loads(pickle.dumps(q, 2))
->>> q == q1
-True
->>> q = QueryDict('a=b&c=d&a=1')
->>> q1 = pickle.loads(pickle.dumps(q, 2))
->>> q == q1
-True
-
-######################################
-# HttpResponse with Unicode headers #
-######################################
-
->>> r = HttpResponse()
-
-If we insert a unicode value it will be converted to an ascii
-string. This makes sure we comply with the HTTP specifications.
-
->>> r['value'] = u'test value'
->>> isinstance(r['value'], str)
-True
-
-An error is raised When a unicode object with non-ascii is assigned.
-
->>> r['value'] = u't\xebst value' # doctest:+ELLIPSIS
-Traceback (most recent call last):
-...
-UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
-
-The response also converts unicode keys to strings.
-
->>> r[u'test'] = 'testing key'
->>> l = list(r.items())
->>> l.sort()
->>> l[1]
-('test', 'testing key')
-
-It will also raise errors for keys with non-ascii data.
-
->>> r[u't\xebst'] = 'testing key' # doctest:+ELLIPSIS
-Traceback (most recent call last):
-...
-UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
-
-# Bug #10188: Do not allow newlines in headers (CR or LF)
->>> r['test\\rstr'] = 'test'
-Traceback (most recent call last):
-...
-BadHeaderError: Header values can't contain newlines (got 'test\\rstr')
-
->>> r['test\\nstr'] = 'test'
-Traceback (most recent call last):
-...
-BadHeaderError: Header values can't contain newlines (got 'test\\nstr')
-
-#
-# Regression test for #8278: QueryDict.update(QueryDict)
-#
->>> x = QueryDict("a=1&a=2", mutable=True)
->>> y = QueryDict("a=3&a=4")
->>> x.update(y)
->>> x.getlist('a')
-[u'1', u'2', u'3', u'4']
-"""
-
-from django.http import QueryDict, HttpResponse, CompatCookie
-from django.test import TestCase
-
-
-class Cookies(TestCase):
-
+import copy
+import pickle
+import unittest
+from django.http import QueryDict, HttpResponse, CompatCookie, BadHeaderError
+
+class QueryDictTests(unittest.TestCase):
+ def test_missing_key(self):
+ q = QueryDict('')
+ self.assertRaises(KeyError, q.__getitem__, 'foo')
+
+ def test_immutability(self):
+ q = QueryDict('')
+ self.assertRaises(AttributeError, q.__setitem__, 'something', 'bar')
+ self.assertRaises(AttributeError, q.setlist, 'foo', ['bar'])
+ self.assertRaises(AttributeError, q.appendlist, 'foo', ['bar'])
+ self.assertRaises(AttributeError, q.update, {'foo': 'bar'})
+ self.assertRaises(AttributeError, q.pop, 'foo')
+ self.assertRaises(AttributeError, q.popitem)
+ self.assertRaises(AttributeError, q.clear)
+
+ def test_immutable_get_with_default(self):
+ q = QueryDict('')
+ self.assertEqual(q.get('foo', 'default'), 'default')
+
+ def test_immutable_basic_operations(self):
+ q = QueryDict('')
+ self.assertEqual(q.getlist('foo'), [])
+ self.assertEqual(q.has_key('foo'), False)
+ self.assertEqual('foo' in q, False)
+ self.assertEqual(q.items(), [])
+ self.assertEqual(q.lists(), [])
+ self.assertEqual(q.items(), [])
+ self.assertEqual(q.keys(), [])
+ self.assertEqual(q.values(), [])
+ self.assertEqual(len(q), 0)
+ self.assertEqual(q.urlencode(), '')
+
+ def test_single_key_value(self):
+ """Test QueryDict with one key/value pair"""
+
+ q = QueryDict('foo=bar')
+ self.assertEqual(q['foo'], 'bar')
+ self.assertRaises(KeyError, q.__getitem__, 'bar')
+ self.assertRaises(AttributeError, q.__setitem__, 'something', 'bar')
+
+ self.assertEqual(q.get('foo', 'default'), 'bar')
+ self.assertEqual(q.get('bar', 'default'), 'default')
+ self.assertEqual(q.getlist('foo'), ['bar'])
+ self.assertEqual(q.getlist('bar'), [])
+
+ self.assertRaises(AttributeError, q.setlist, 'foo', ['bar'])
+ self.assertRaises(AttributeError, q.appendlist, 'foo', ['bar'])
+
+ self.failUnless(q.has_key('foo'))
+ self.failUnless('foo' in q)
+ self.failIf(q.has_key('bar'))
+ self.failIf('bar' in q)
+
+ self.assertEqual(q.items(), [(u'foo', u'bar')])
+ self.assertEqual(q.lists(), [(u'foo', [u'bar'])])
+ self.assertEqual(q.keys(), ['foo'])
+ self.assertEqual(q.values(), ['bar'])
+ self.assertEqual(len(q), 1)
+
+ self.assertRaises(AttributeError, q.update, {'foo': 'bar'})
+ self.assertRaises(AttributeError, q.pop, 'foo')
+ self.assertRaises(AttributeError, q.popitem)
+ self.assertRaises(AttributeError, q.clear)
+ self.assertRaises(AttributeError, q.setdefault, 'foo', 'bar')
+
+ self.assertEqual(q.urlencode(), 'foo=bar')
+
+ def test_mutable_copy(self):
+ """A copy of a QueryDict is mutable."""
+ q = QueryDict('').copy()
+ self.assertRaises(KeyError, q.__getitem__, "foo")
+ q['name'] = 'john'
+ self.assertEqual(q['name'], 'john')
+
+ def test_mutable_delete(self):
+ q = QueryDict('').copy()
+ q['name'] = 'john'
+ del q['name']
+ self.failIf('name' in q)
+
+ def test_basic_mutable_operations(self):
+ q = QueryDict('').copy()
+ q['name'] = 'john'
+ self.assertEqual(q.get('foo', 'default'), 'default')
+ self.assertEqual(q.get('name', 'default'), 'john')
+ self.assertEqual(q.getlist('name'), ['john'])
+ self.assertEqual(q.getlist('foo'), [])
+
+ q.setlist('foo', ['bar', 'baz'])
+ self.assertEqual(q.get('foo', 'default'), 'baz')
+ self.assertEqual(q.getlist('foo'), ['bar', 'baz'])
+
+ q.appendlist('foo', 'another')
+ self.assertEqual(q.getlist('foo'), ['bar', 'baz', 'another'])
+ self.assertEqual(q['foo'], 'another')
+ self.failUnless(q.has_key('foo'))
+ self.failUnless('foo' in q)
+
+ self.assertEqual(q.items(), [(u'foo', u'another'), (u'name', u'john')])
+ self.assertEqual(q.lists(), [(u'foo', [u'bar', u'baz', u'another']), (u'name', [u'john'])])
+ self.assertEqual(q.keys(), [u'foo', u'name'])
+ self.assertEqual(q.values(), [u'another', u'john'])
+ self.assertEqual(len(q), 2)
+
+ q.update({'foo': 'hello'})
+ self.assertEqual(q['foo'], 'hello')
+ self.assertEqual(q.get('foo', 'not available'), 'hello')
+ self.assertEqual(q.getlist('foo'), [u'bar', u'baz', u'another', u'hello'])
+ self.assertEqual(q.pop('foo'), [u'bar', u'baz', u'another', u'hello'])
+ self.assertEqual(q.pop('foo', 'not there'), 'not there')
+ self.assertEqual(q.get('foo', 'not there'), 'not there')
+ self.assertEqual(q.setdefault('foo', 'bar'), 'bar')
+ self.assertEqual(q['foo'], 'bar')
+ self.assertEqual(q.getlist('foo'), ['bar'])
+ self.assertEqual(q.urlencode(), 'foo=bar&name=john')
+
+ q.clear()
+ self.assertEqual(len(q), 0)
+
+ def test_multiple_keys(self):
+ """Test QueryDict with two key/value pairs with same keys."""
+
+ q = QueryDict('vote=yes&vote=no')
+
+ self.assertEqual(q['vote'], u'no')
+ self.assertRaises(AttributeError, q.__setitem__, 'something', 'bar')
+
+ self.assertEqual(q.get('vote', 'default'), u'no')
+ self.assertEqual(q.get('foo', 'default'), 'default')
+ self.assertEqual(q.getlist('vote'), [u'yes', u'no'])
+ self.assertEqual(q.getlist('foo'), [])
+
+ self.assertRaises(AttributeError, q.setlist, 'foo', ['bar', 'baz'])
+ self.assertRaises(AttributeError, q.setlist, 'foo', ['bar', 'baz'])
+ self.assertRaises(AttributeError, q.appendlist, 'foo', ['bar'])
+
+ self.assertEqual(q.has_key('vote'), True)
+ self.assertEqual('vote' in q, True)
+ self.assertEqual(q.has_key('foo'), False)
+ self.assertEqual('foo' in q, False)
+ self.assertEqual(q.items(), [(u'vote', u'no')])
+ self.assertEqual(q.lists(), [(u'vote', [u'yes', u'no'])])
+ self.assertEqual(q.keys(), [u'vote'])
+ self.assertEqual(q.values(), [u'no'])
+ self.assertEqual(len(q), 1)
+
+ self.assertRaises(AttributeError, q.update, {'foo': 'bar'})
+ self.assertRaises(AttributeError, q.pop, 'foo')
+ self.assertRaises(AttributeError, q.popitem)
+ self.assertRaises(AttributeError, q.clear)
+ self.assertRaises(AttributeError, q.setdefault, 'foo', 'bar')
+ self.assertRaises(AttributeError, q.__delitem__, 'vote')
+
+ def test_invalid_input_encoding(self):
+ """
+ QueryDicts must be able to handle invalid input encoding (in this
+ case, bad UTF-8 encoding).
+ """
+ q = QueryDict('foo=bar&foo=\xff')
+ self.assertEqual(q['foo'], u'\ufffd')
+ self.assertEqual(q.getlist('foo'), [u'bar', u'\ufffd'])
+
+ def test_pickle(self):
+ q = QueryDict('')
+ q1 = pickle.loads(pickle.dumps(q, 2))
+ self.assertEqual(q == q1, True)
+ q = QueryDict('a=b&c=d')
+ q1 = pickle.loads(pickle.dumps(q, 2))
+ self.assertEqual(q == q1, True)
+ q = QueryDict('a=b&c=d&a=1')
+ q1 = pickle.loads(pickle.dumps(q, 2))
+ self.assertEqual(q == q1 , True)
+
+ def test_update_from_querydict(self):
+ """Regression test for #8278: QueryDict.update(QueryDict)"""
+ x = QueryDict("a=1&a=2", mutable=True)
+ y = QueryDict("a=3&a=4")
+ x.update(y)
+ self.assertEqual(x.getlist('a'), [u'1', u'2', u'3', u'4'])
+
+ def test_non_default_encoding(self):
+ """#13572 - QueryDict with a non-default encoding"""
+ q = QueryDict('sbb=one', encoding='rot_13')
+ self.assertEqual(q.encoding , 'rot_13' )
+ self.assertEqual(q.items() , [(u'foo', u'bar')] )
+ self.assertEqual(q.urlencode() , 'sbb=one' )
+ q = q.copy()
+ self.assertEqual(q.encoding , 'rot_13' )
+ self.assertEqual(q.items() , [(u'foo', u'bar')] )
+ self.assertEqual(q.urlencode() , 'sbb=one' )
+ self.assertEqual(copy.copy(q).encoding , 'rot_13' )
+ self.assertEqual(copy.deepcopy(q).encoding , 'rot_13')
+
+class HttpResponseTests(unittest.TestCase):
+ def test_unicode_headers(self):
+ r = HttpResponse()
+
+ # If we insert a unicode value it will be converted to an ascii
+ r['value'] = u'test value'
+ self.failUnless(isinstance(r['value'], str))
+
+ # An error is raised ~hen a unicode object with non-ascii is assigned.
+ self.assertRaises(UnicodeEncodeError, r.__setitem__, 'value', u't\xebst value')
+
+ # An error is raised when a unicode object with non-ASCII format is
+ # passed as initial mimetype or content_type.
+ self.assertRaises(UnicodeEncodeError, HttpResponse,
+ mimetype=u't\xebst value')
+
+ # HttpResponse headers must be convertible to ASCII.
+ self.assertRaises(UnicodeEncodeError, HttpResponse,
+ content_type=u't\xebst value')
+
+ # The response also converts unicode keys to strings.)
+ r[u'test'] = 'testing key'
+ l = list(r.items())
+ l.sort()
+ self.assertEqual(l[1], ('test', 'testing key'))
+
+ # It will also raise errors for keys with non-ascii data.
+ self.assertRaises(UnicodeEncodeError, r.__setitem__, u't\xebst key', 'value')
+
+ def test_newlines_in_headers(self):
+ # Bug #10188: Do not allow newlines in headers (CR or LF)
+ r = HttpResponse()
+ self.assertRaises(BadHeaderError, r.__setitem__, 'test\rstr', 'test')
+ self.assertRaises(BadHeaderError, r.__setitem__, 'test\nstr', 'test')
+
+class CookieTests(unittest.TestCase):
def test_encode(self):
"""
Test that we don't output tricky characters in encoded value
@@ -502,7 +263,3 @@ class Cookies(TestCase):
c2 = CompatCookie()
c2.load(c.output())
self.assertEqual(c['test'].value, c2['test'].value)
-
-if __name__ == "__main__":
- import doctest
- doctest.testmod()
diff --git a/tests/regressiontests/i18n/models.py b/tests/regressiontests/i18n/models.py
index 8b142b36bc..75cd996f83 100644
--- a/tests/regressiontests/i18n/models.py
+++ b/tests/regressiontests/i18n/models.py
@@ -10,9 +10,3 @@ class Company(models.Model):
date_added = models.DateTimeField(default=datetime(1799,1,31,23,59,59,0))
cents_payed = models.DecimalField(max_digits=4, decimal_places=2)
products_delivered = models.IntegerField()
-
-__test__ = {'API_TESTS': '''
->>> tm = TestModel()
->>> tm.save()
-'''
-}
diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
index 32c991a436..7e2e9f8228 100644
--- a/tests/regressiontests/i18n/tests.py
+++ b/tests/regressiontests/i18n/tests.py
@@ -5,14 +5,17 @@ import os
import sys
import pickle
-from django.template import Template, Context
from django.conf import settings
+from django.template import Template, Context
+from django.test import TestCase
from django.utils.formats import get_format, date_format, time_format, localize, localize_input
from django.utils.numberformat import format as nformat
-from django.test import TestCase
+from django.utils.safestring import mark_safe, SafeString, SafeUnicode
from django.utils.translation import ugettext, ugettext_lazy, activate, deactivate, gettext_lazy, to_locale
+
from forms import I18nForm, SelectDateForm, SelectDateWidget, CompanyForm
+from models import Company, TestModel
class TranslationTests(TestCase):
@@ -59,7 +62,6 @@ class TranslationTests(TestCase):
"""
Translating a string requiring no auto-escaping shouldn't change the "safe" status.
"""
- from django.utils.safestring import mark_safe, SafeString, SafeUnicode
s = mark_safe('Password')
self.assertEqual(SafeString, type(s))
activate('de')
@@ -170,14 +172,14 @@ class FormattingTests(TestCase):
self.assertEqual(u'desembre 2009', date_format(self.d, 'YEAR_MONTH_FORMAT'))
self.assertEqual(u'12/31/2009 8:50 p.m.', date_format(self.dt, 'SHORT_DATETIME_FORMAT'))
self.assertEqual('No localizable', localize('No localizable'))
- self.assertEqual(decimal.Decimal('66666.666'), localize(self.n))
- self.assertEqual(99999.999, localize(self.f))
- self.assertEqual(datetime.date(2009, 12, 31), localize(self.d))
- self.assertEqual(datetime.datetime(2009, 12, 31, 20, 50), localize(self.dt))
+ self.assertEqual('66666.666', localize(self.n))
+ self.assertEqual('99999.999', localize(self.f))
+ self.assertEqual(u'des. 31, 2009', localize(self.d))
+ self.assertEqual(u'des. 31, 2009, 8:50 p.m.', localize(self.dt))
self.assertEqual(u'66666.666', Template('{{ n }}').render(self.ctxt))
self.assertEqual(u'99999.999', Template('{{ f }}').render(self.ctxt))
- self.assertEqual(u'2009-12-31', Template('{{ d }}').render(self.ctxt))
- self.assertEqual(u'2009-12-31 20:50:00', Template('{{ dt }}').render(self.ctxt))
+ self.assertEqual(u'des. 31, 2009', Template('{{ d }}').render(self.ctxt))
+ self.assertEqual(u'des. 31, 2009, 8:50 p.m.', Template('{{ dt }}').render(self.ctxt))
self.assertEqual(u'66666.67', Template('{{ n|floatformat:2 }}').render(self.ctxt))
self.assertEqual(u'100000.0', Template('{{ f|floatformat }}').render(self.ctxt))
self.assertEqual(u'10:15 a.m.', Template('{{ t|time:"TIME_FORMAT" }}').render(self.ctxt))
@@ -620,3 +622,16 @@ class DjangoFallbackResolutionOrderI18NTests(ResolutionOrderI18NTests):
def test_django_fallback(self):
self.assertUgettext('Date/time', 'Datum/Zeit')
+
+
+class TestModels(TestCase):
+ def test_lazy(self):
+ tm = TestModel()
+ tm.save()
+
+ def test_safestr(self):
+ c = Company(cents_payed=12, products_delivered=1)
+ c.name = SafeUnicode(u'Iñtërnâtiônàlizætiøn1')
+ c.save()
+ c.name = SafeString(u'Iñtërnâtiônàlizætiøn1'.encode('utf-8'))
+ c.save()
diff --git a/tests/regressiontests/localflavor/models.py b/tests/regressiontests/localflavor/models.py
index f74a5051d4..e69de29bb2 100644
--- a/tests/regressiontests/localflavor/models.py
+++ b/tests/regressiontests/localflavor/models.py
@@ -1,8 +0,0 @@
-from django.db import models
-from django.contrib.localflavor.us.models import USStateField
-
-class Place(models.Model):
- state = USStateField(blank=True)
- state_req = USStateField()
- state_default = USStateField(default="CA", blank=True)
- name = models.CharField(max_length=20)
diff --git a/tests/regressiontests/localflavor/tests.py b/tests/regressiontests/localflavor/tests.py
index 0ea3c52568..69682360d3 100644
--- a/tests/regressiontests/localflavor/tests.py
+++ b/tests/regressiontests/localflavor/tests.py
@@ -1,83 +1,5 @@
+import unittest
from django.test import TestCase
-from models import Place
-from forms import PlaceForm
-class USLocalflavorTests(TestCase):
- def setUp(self):
- self.form = PlaceForm({'state':'GA', 'state_req':'NC', 'name':'impossible'})
-
- def test_get_display_methods(self):
- """Test that the get_*_display() methods are added to the model instances."""
- place = self.form.save()
- self.assertEqual(place.get_state_display(), 'Georgia')
- self.assertEqual(place.get_state_req_display(), 'North Carolina')
-
- def test_required(self):
- """Test that required USStateFields throw appropriate errors."""
- form = PlaceForm({'state':'GA', 'name':'Place in GA'})
- self.assertFalse(form.is_valid())
- self.assertEqual(form.errors['state_req'], [u'This field is required.'])
-
- def test_field_blank_option(self):
- """Test that the empty option is there."""
- state_select_html = """\
-<select name="state" id="id_state">
-<option value="">---------</option>
-<option value="AL">Alabama</option>
-<option value="AK">Alaska</option>
-<option value="AS">American Samoa</option>
-<option value="AZ">Arizona</option>
-<option value="AR">Arkansas</option>
-<option value="CA">California</option>
-<option value="CO">Colorado</option>
-<option value="CT">Connecticut</option>
-<option value="DE">Delaware</option>
-<option value="DC">District of Columbia</option>
-<option value="FL">Florida</option>
-<option value="GA" selected="selected">Georgia</option>
-<option value="GU">Guam</option>
-<option value="HI">Hawaii</option>
-<option value="ID">Idaho</option>
-<option value="IL">Illinois</option>
-<option value="IN">Indiana</option>
-<option value="IA">Iowa</option>
-<option value="KS">Kansas</option>
-<option value="KY">Kentucky</option>
-<option value="LA">Louisiana</option>
-<option value="ME">Maine</option>
-<option value="MD">Maryland</option>
-<option value="MA">Massachusetts</option>
-<option value="MI">Michigan</option>
-<option value="MN">Minnesota</option>
-<option value="MS">Mississippi</option>
-<option value="MO">Missouri</option>
-<option value="MT">Montana</option>
-<option value="NE">Nebraska</option>
-<option value="NV">Nevada</option>
-<option value="NH">New Hampshire</option>
-<option value="NJ">New Jersey</option>
-<option value="NM">New Mexico</option>
-<option value="NY">New York</option>
-<option value="NC">North Carolina</option>
-<option value="ND">North Dakota</option>
-<option value="MP">Northern Mariana Islands</option>
-<option value="OH">Ohio</option>
-<option value="OK">Oklahoma</option>
-<option value="OR">Oregon</option>
-<option value="PA">Pennsylvania</option>
-<option value="PR">Puerto Rico</option>
-<option value="RI">Rhode Island</option>
-<option value="SC">South Carolina</option>
-<option value="SD">South Dakota</option>
-<option value="TN">Tennessee</option>
-<option value="TX">Texas</option>
-<option value="UT">Utah</option>
-<option value="VT">Vermont</option>
-<option value="VI">Virgin Islands</option>
-<option value="VA">Virginia</option>
-<option value="WA">Washington</option>
-<option value="WV">West Virginia</option>
-<option value="WI">Wisconsin</option>
-<option value="WY">Wyoming</option>
-</select>"""
- self.assertEqual(str(self.form['state']), state_select_html)
+# just import your tests here
+from us.tests import *
diff --git a/tests/regressiontests/localflavor/us/__init__.py b/tests/regressiontests/localflavor/us/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/regressiontests/localflavor/us/__init__.py
diff --git a/tests/regressiontests/localflavor/forms.py b/tests/regressiontests/localflavor/us/forms.py
index 2dd1da6dd0..9b77e1078b 100644
--- a/tests/regressiontests/localflavor/forms.py
+++ b/tests/regressiontests/localflavor/us/forms.py
@@ -1,7 +1,7 @@
from django.forms import ModelForm
-from models import Place
+from models import USPlace
-class PlaceForm(ModelForm):
+class USPlaceForm(ModelForm):
"""docstring for PlaceForm"""
class Meta:
- model = Place
+ model = USPlace
diff --git a/tests/regressiontests/localflavor/us/models.py b/tests/regressiontests/localflavor/us/models.py
new file mode 100644
index 0000000000..a8a4cf0f50
--- /dev/null
+++ b/tests/regressiontests/localflavor/us/models.py
@@ -0,0 +1,13 @@
+from django.db import models
+from django.contrib.localflavor.us.models import USStateField
+
+# When creating models you need to remember to add a app_label as
+# 'localflavor', so your model can be found
+
+class USPlace(models.Model):
+ state = USStateField(blank=True)
+ state_req = USStateField()
+ state_default = USStateField(default="CA", blank=True)
+ name = models.CharField(max_length=20)
+ class Meta:
+ app_label = 'localflavor'
diff --git a/tests/regressiontests/localflavor/us/tests.py b/tests/regressiontests/localflavor/us/tests.py
new file mode 100644
index 0000000000..07fe057833
--- /dev/null
+++ b/tests/regressiontests/localflavor/us/tests.py
@@ -0,0 +1,82 @@
+from django.test import TestCase
+from forms import USPlaceForm
+
+class USLocalflavorTests(TestCase):
+ def setUp(self):
+ self.form = USPlaceForm({'state':'GA', 'state_req':'NC', 'name':'impossible'})
+
+ def test_get_display_methods(self):
+ """Test that the get_*_display() methods are added to the model instances."""
+ place = self.form.save()
+ self.assertEqual(place.get_state_display(), 'Georgia')
+ self.assertEqual(place.get_state_req_display(), 'North Carolina')
+
+ def test_required(self):
+ """Test that required USStateFields throw appropriate errors."""
+ form = USPlaceForm({'state':'GA', 'name':'Place in GA'})
+ self.assertFalse(form.is_valid())
+ self.assertEqual(form.errors['state_req'], [u'This field is required.'])
+
+ def test_field_blank_option(self):
+ """Test that the empty option is there."""
+ state_select_html = """\
+<select name="state" id="id_state">
+<option value="">---------</option>
+<option value="AL">Alabama</option>
+<option value="AK">Alaska</option>
+<option value="AS">American Samoa</option>
+<option value="AZ">Arizona</option>
+<option value="AR">Arkansas</option>
+<option value="CA">California</option>
+<option value="CO">Colorado</option>
+<option value="CT">Connecticut</option>
+<option value="DE">Delaware</option>
+<option value="DC">District of Columbia</option>
+<option value="FL">Florida</option>
+<option value="GA" selected="selected">Georgia</option>
+<option value="GU">Guam</option>
+<option value="HI">Hawaii</option>
+<option value="ID">Idaho</option>
+<option value="IL">Illinois</option>
+<option value="IN">Indiana</option>
+<option value="IA">Iowa</option>
+<option value="KS">Kansas</option>
+<option value="KY">Kentucky</option>
+<option value="LA">Louisiana</option>
+<option value="ME">Maine</option>
+<option value="MD">Maryland</option>
+<option value="MA">Massachusetts</option>
+<option value="MI">Michigan</option>
+<option value="MN">Minnesota</option>
+<option value="MS">Mississippi</option>
+<option value="MO">Missouri</option>
+<option value="MT">Montana</option>
+<option value="NE">Nebraska</option>
+<option value="NV">Nevada</option>
+<option value="NH">New Hampshire</option>
+<option value="NJ">New Jersey</option>
+<option value="NM">New Mexico</option>
+<option value="NY">New York</option>
+<option value="NC">North Carolina</option>
+<option value="ND">North Dakota</option>
+<option value="MP">Northern Mariana Islands</option>
+<option value="OH">Ohio</option>
+<option value="OK">Oklahoma</option>
+<option value="OR">Oregon</option>
+<option value="PA">Pennsylvania</option>
+<option value="PR">Puerto Rico</option>
+<option value="RI">Rhode Island</option>
+<option value="SC">South Carolina</option>
+<option value="SD">South Dakota</option>
+<option value="TN">Tennessee</option>
+<option value="TX">Texas</option>
+<option value="UT">Utah</option>
+<option value="VT">Vermont</option>
+<option value="VI">Virgin Islands</option>
+<option value="VA">Virginia</option>
+<option value="WA">Washington</option>
+<option value="WV">West Virginia</option>
+<option value="WI">Wisconsin</option>
+<option value="WY">Wyoming</option>
+</select>"""
+ self.assertEqual(str(self.form['state']), state_select_html)
diff --git a/tests/regressiontests/m2m_through_regress/models.py b/tests/regressiontests/m2m_through_regress/models.py
index 56aecd6975..ec87985765 100644
--- a/tests/regressiontests/m2m_through_regress/models.py
+++ b/tests/regressiontests/m2m_through_regress/models.py
@@ -1,8 +1,10 @@
from datetime import datetime
+
from django.contrib.auth.models import User
from django.core import management
from django.db import models
+
# Forward declared intermediate model
class Membership(models.Model):
person = models.ForeignKey('Person')
@@ -51,159 +53,3 @@ class Through(ThroughBase):
class B(models.Model):
b_text = models.CharField(max_length=20)
a_list = models.ManyToManyField(A, through=Through)
-
-
-__test__ = {'API_TESTS':"""
-# Create some dummy data
->>> bob = Person.objects.create(name='Bob')
->>> jim = Person.objects.create(name='Jim')
-
->>> rock = Group.objects.create(name='Rock')
->>> roll = Group.objects.create(name='Roll')
-
->>> frank = User.objects.create_user('frank','frank@example.com','password')
->>> jane = User.objects.create_user('jane','jane@example.com','password')
-
-# Now test that the forward declared Membership works
->>> Membership.objects.create(person=bob, group=rock)
-<Membership: Bob is a member of Rock>
-
->>> Membership.objects.create(person=bob, group=roll)
-<Membership: Bob is a member of Roll>
-
->>> Membership.objects.create(person=jim, group=rock)
-<Membership: Jim is a member of Rock>
-
->>> bob.group_set.all()
-[<Group: Rock>, <Group: Roll>]
-
->>> roll.members.all()
-[<Person: Bob>]
-
-# Error messages use the model name, not repr of the class name
->>> bob.group_set = []
-Traceback (most recent call last):
-...
-AttributeError: Cannot set values on a ManyToManyField which specifies an intermediary model. Use m2m_through_regress.Membership's Manager instead.
-
->>> roll.members = []
-Traceback (most recent call last):
-...
-AttributeError: Cannot set values on a ManyToManyField which specifies an intermediary model. Use m2m_through_regress.Membership's Manager instead.
-
->>> rock.members.create(name='Anne')
-Traceback (most recent call last):
-...
-AttributeError: Cannot use create() on a ManyToManyField which specifies an intermediary model. Use m2m_through_regress.Membership's Manager instead.
-
->>> bob.group_set.create(name='Funk')
-Traceback (most recent call last):
-...
-AttributeError: Cannot use create() on a ManyToManyField which specifies an intermediary model. Use m2m_through_regress.Membership's Manager instead.
-
-# Now test that the intermediate with a relationship outside
-# the current app (i.e., UserMembership) workds
->>> UserMembership.objects.create(user=frank, group=rock)
-<UserMembership: frank is a user and member of Rock>
-
->>> UserMembership.objects.create(user=frank, group=roll)
-<UserMembership: frank is a user and member of Roll>
-
->>> UserMembership.objects.create(user=jane, group=rock)
-<UserMembership: jane is a user and member of Rock>
-
->>> frank.group_set.all()
-[<Group: Rock>, <Group: Roll>]
-
->>> roll.user_members.all()
-[<User: frank>]
-
-# Regression test for #8134 --
-# m2m-through models shouldn't be serialized as m2m fields on the model.
-
-# First, clean up a lot of objects we don't need.
-# The serialization test only requires three objects to work -
-# one for each end of the m2m, plus the through model.
-
->>> User.objects.all().delete()
->>> UserMembership.objects.all().delete()
->>> frank.delete()
->>> rock.delete()
->>> jim.delete()
-
-# Dump the current contents of the database as a JSON fixture
->>> management.call_command('dumpdata', 'm2m_through_regress', format='json', indent=2)
-[
- {
- "pk": 2,
- "model": "m2m_through_regress.membership",
- "fields": {
- "person": 1,
- "price": 100,
- "group": 2
- }
- },
- {
- "pk": 1,
- "model": "m2m_through_regress.person",
- "fields": {
- "name": "Bob"
- }
- },
- {
- "pk": 2,
- "model": "m2m_through_regress.group",
- "fields": {
- "name": "Roll"
- }
- }
-]
-
-# Check the XML serializer too, since it doesn't use the common implementation
->>> management.call_command('dumpdata', 'm2m_through_regress', format='xml', indent=2)
-<?xml version="1.0" encoding="utf-8"?>
-<django-objects version="1.0">
- <object pk="2" model="m2m_through_regress.membership">
- <field to="m2m_through_regress.person" name="person" rel="ManyToOneRel">1</field>
- <field to="m2m_through_regress.group" name="group" rel="ManyToOneRel">2</field>
- <field type="IntegerField" name="price">100</field>
- </object>
- <object pk="1" model="m2m_through_regress.person">
- <field type="CharField" name="name">Bob</field>
- </object>
- <object pk="2" model="m2m_through_regress.group">
- <field type="CharField" name="name">Roll</field>
- </object>
-</django-objects>
-
-## Regression test for #8046:
-Check that we don't involve too many copies of the intermediate table when
-doing a join.
-
->>> bob = Person.objects.create(name='Bob')
->>> jim = Person.objects.create(name='Jim')
->>> rock = Group.objects.create(name='Rock')
->>> roll = Group.objects.create(name='Roll')
->>> _ = Membership.objects.create(person=bob, group=rock)
->>> _ = Membership.objects.create(person=jim, group=rock, price=50)
->>> _ = Membership.objects.create(person=bob, group=roll, price=50)
->>> rock.members.filter(membership__price=50)
-[<Person: Jim>]
-
-## Regression test for #8254
->>> bob.group_set.filter(membership__price=50)
-[<Group: Roll>]
-
-## Regression test for #9804
-# Flush the database, just to make sure we can.
->>> management.call_command('flush', verbosity=0, interactive=False)
-
-## Regression test for #11107
-Ensure that sequences on m2m_through tables are being created for the through
-model, not for a phantom auto-generated m2m table.
-
->>> management.call_command('loaddata', 'm2m_through', verbosity=0)
->>> management.call_command('dumpdata', 'm2m_through_regress', format='json')
-[{"pk": 1, "model": "m2m_through_regress.usermembership", "fields": {"price": 100, "group": 1, "user": 1}}, {"pk": 1, "model": "m2m_through_regress.person", "fields": {"name": "Guido"}}, {"pk": 1, "model": "m2m_through_regress.group", "fields": {"name": "Python Core Group"}}]
-
-"""}
diff --git a/tests/regressiontests/m2m_through_regress/tests.py b/tests/regressiontests/m2m_through_regress/tests.py
new file mode 100644
index 0000000000..406851acfd
--- /dev/null
+++ b/tests/regressiontests/m2m_through_regress/tests.py
@@ -0,0 +1,128 @@
+try:
+ from cStringIO import StringIO
+except ImportError:
+ from StringIO import StringIO
+
+from django.core import management
+from django.contrib.auth.models import User
+from django.test import TestCase
+
+from models import Person, Group, Membership, UserMembership
+
+
+class M2MThroughTestCase(TestCase):
+ def test_everything(self):
+ bob = Person.objects.create(name="Bob")
+ jim = Person.objects.create(name="Jim")
+
+ rock = Group.objects.create(name="Rock")
+ roll = Group.objects.create(name="Roll")
+
+ frank = User.objects.create_user("frank", "frank@example.com", "password")
+ jane = User.objects.create_user("jane", "jane@example.com", "password")
+
+ Membership.objects.create(person=bob, group=rock)
+ Membership.objects.create(person=bob, group=roll)
+ Membership.objects.create(person=jim, group=rock)
+
+ self.assertQuerysetEqual(
+ bob.group_set.all(), [
+ "<Group: Rock>",
+ "<Group: Roll>",
+ ]
+ )
+
+ self.assertQuerysetEqual(
+ roll.members.all(), [
+ "<Person: Bob>",
+ ]
+ )
+
+ self.assertRaises(AttributeError, setattr, bob, "group_set", [])
+ self.assertRaises(AttributeError, setattr, roll, "members", [])
+
+ self.assertRaises(AttributeError, rock.members.create, name="Anne")
+ self.assertRaises(AttributeError, bob.group_set.create, name="Funk")
+
+ UserMembership.objects.create(user=frank, group=rock)
+ UserMembership.objects.create(user=frank, group=roll)
+ UserMembership.objects.create(user=jane, group=rock)
+
+ self.assertQuerysetEqual(
+ frank.group_set.all(), [
+ "<Group: Rock>",
+ "<Group: Roll>",
+ ]
+ )
+
+ self.assertQuerysetEqual(
+ roll.user_members.all(), [
+ "<User: frank>",
+ ]
+ )
+
+ def test_serialization(self):
+ "m2m-through models aren't serialized as m2m fields. Refs #8134"
+
+ p = Person.objects.create(name="Bob")
+ g = Group.objects.create(name="Roll")
+ m =Membership.objects.create(person=p, group=g)
+
+ pks = {"p_pk": p.pk, "g_pk": g.pk, "m_pk": m.pk}
+
+ out = StringIO()
+ management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out)
+ self.assertEqual(out.getvalue().strip(), """[{"pk": %(m_pk)s, "model": "m2m_through_regress.membership", "fields": {"person": %(p_pk)s, "price": 100, "group": %(g_pk)s}}, {"pk": %(p_pk)s, "model": "m2m_through_regress.person", "fields": {"name": "Bob"}}, {"pk": %(g_pk)s, "model": "m2m_through_regress.group", "fields": {"name": "Roll"}}]""" % pks)
+
+ out = StringIO()
+ management.call_command("dumpdata", "m2m_through_regress", format="xml",
+ indent=2, stdout=out)
+ self.assertEqual(out.getvalue().strip(), """
+<?xml version="1.0" encoding="utf-8"?>
+<django-objects version="1.0">
+ <object pk="%(m_pk)s" model="m2m_through_regress.membership">
+ <field to="m2m_through_regress.person" name="person" rel="ManyToOneRel">%(p_pk)s</field>
+ <field to="m2m_through_regress.group" name="group" rel="ManyToOneRel">%(g_pk)s</field>
+ <field type="IntegerField" name="price">100</field>
+ </object>
+ <object pk="%(p_pk)s" model="m2m_through_regress.person">
+ <field type="CharField" name="name">Bob</field>
+ </object>
+ <object pk="%(g_pk)s" model="m2m_through_regress.group">
+ <field type="CharField" name="name">Roll</field>
+ </object>
+</django-objects>
+ """.strip() % pks)
+
+ def test_join_trimming(self):
+ "Check that we don't involve too many copies of the intermediate table when doing a join. Refs #8046, #8254"
+ bob = Person.objects.create(name="Bob")
+ jim = Person.objects.create(name="Jim")
+
+ rock = Group.objects.create(name="Rock")
+ roll = Group.objects.create(name="Roll")
+
+ Membership.objects.create(person=bob, group=rock)
+ Membership.objects.create(person=jim, group=rock, price=50)
+ Membership.objects.create(person=bob, group=roll, price=50)
+
+ self.assertQuerysetEqual(
+ rock.members.filter(membership__price=50), [
+ "<Person: Jim>",
+ ]
+ )
+
+ self.assertQuerysetEqual(
+ bob.group_set.filter(membership__price=50), [
+ "<Group: Roll>",
+ ]
+ )
+
+class ThroughLoadDataTestCase(TestCase):
+ fixtures = ["m2m_through"]
+
+ def test_sequence_creation(self):
+ "Check that sequences on an m2m_through are created for the through model, not a phantom auto-generated m2m table. Refs #11107"
+ out = StringIO()
+ management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out)
+ self.assertEqual(out.getvalue().strip(), """[{"pk": 1, "model": "m2m_through_regress.usermembership", "fields": {"price": 100, "group": 1, "user": 1}}, {"pk": 1, "model": "m2m_through_regress.person", "fields": {"name": "Guido"}}, {"pk": 1, "model": "m2m_through_regress.group", "fields": {"name": "Python Core Group"}}]""")
diff --git a/tests/regressiontests/model_forms_regress/models.py b/tests/regressiontests/model_forms_regress/models.py
index 871bb6f815..4f9811a963 100644
--- a/tests/regressiontests/model_forms_regress/models.py
+++ b/tests/regressiontests/model_forms_regress/models.py
@@ -54,3 +54,6 @@ class Author(models.Model):
class Author1(models.Model):
publication = models.OneToOneField(Publication, null=False)
full_name = models.CharField(max_length=255)
+
+class Homepage(models.Model):
+ url = models.URLField(verify_exists=False)
diff --git a/tests/regressiontests/model_forms_regress/tests.py b/tests/regressiontests/model_forms_regress/tests.py
index 5a7a83bc0e..baf769c02a 100644
--- a/tests/regressiontests/model_forms_regress/tests.py
+++ b/tests/regressiontests/model_forms_regress/tests.py
@@ -5,8 +5,10 @@ from django import forms
from django.forms.models import modelform_factory, ModelChoiceField
from django.conf import settings
from django.test import TestCase
+from django.core.exceptions import FieldError
-from models import Person, RealPerson, Triple, FilePathModel, Article, Publication, CustomFF, Author, Author1
+from models import Person, RealPerson, Triple, FilePathModel, Article, \
+ Publication, CustomFF, Author, Author1, Homepage
class ModelMultipleChoiceFieldTests(TestCase):
@@ -128,7 +130,7 @@ class ManyToManyCallableInitialTests(TestCase):
<option value="1" selected="selected">First Book</option>
<option value="2" selected="selected">Second Book</option>
<option value="3">Third Book</option>
-</select> Hold down "Control", or "Command" on a Mac, to select more than one.</li>""")
+</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>""")
class CFFForm(forms.ModelForm):
class Meta:
@@ -212,7 +214,122 @@ class TestTicket11183(TestCase):
def test_11183(self):
form1 = ModelChoiceForm()
field1 = form1.fields['person']
- # To allow the widget to change the queryset of field1.widget.choices correctly,
+ # To allow the widget to change the queryset of field1.widget.choices correctly,
# without affecting other forms, the following must hold:
self.assert_(field1 is not ModelChoiceForm.base_fields['person'])
self.assert_(field1.widget.choices.field is field1)
+
+class HomepageForm(forms.ModelForm):
+ class Meta:
+ model = Homepage
+
+class URLFieldTests(TestCase):
+ def test_url_on_modelform(self):
+ "Check basic URL field validation on model forms"
+ self.assertFalse(HomepageForm({'url': 'foo'}).is_valid())
+ self.assertFalse(HomepageForm({'url': 'http://'}).is_valid())
+ self.assertFalse(HomepageForm({'url': 'http://example'}).is_valid())
+ self.assertFalse(HomepageForm({'url': 'http://example.'}).is_valid())
+ self.assertFalse(HomepageForm({'url': 'http://com.'}).is_valid())
+
+ self.assertTrue(HomepageForm({'url': 'http://localhost'}).is_valid())
+ self.assertTrue(HomepageForm({'url': 'http://example.com'}).is_valid())
+ self.assertTrue(HomepageForm({'url': 'http://www.example.com'}).is_valid())
+ self.assertTrue(HomepageForm({'url': 'http://www.example.com:8000'}).is_valid())
+ self.assertTrue(HomepageForm({'url': 'http://www.example.com/test'}).is_valid())
+ self.assertTrue(HomepageForm({'url': 'http://www.example.com:8000/test'}).is_valid())
+ self.assertTrue(HomepageForm({'url': 'http://example.com/foo/bar'}).is_valid())
+
+ def test_http_prefixing(self):
+ "If the http:// prefix is omitted on form input, the field adds it again. (Refs #13613)"
+ form = HomepageForm({'url': 'example.com'})
+ form.is_valid()
+ # self.assertTrue(form.is_valid())
+ # self.assertEquals(form.cleaned_data['url'], 'http://example.com/')
+
+ form = HomepageForm({'url': 'example.com/test'})
+ form.is_valid()
+ # self.assertTrue(form.is_valid())
+ # self.assertEquals(form.cleaned_data['url'], 'http://example.com/test')
+
+
+class FormFieldCallbackTests(TestCase):
+
+ def test_baseform_with_widgets_in_meta(self):
+ """Regression for #13095: Using base forms with widgets defined in Meta should not raise errors."""
+ widget = forms.Textarea()
+
+ class BaseForm(forms.ModelForm):
+ class Meta:
+ model = Person
+ widgets = {'name': widget}
+
+ Form = modelform_factory(Person, form=BaseForm)
+ self.assertTrue(Form.base_fields['name'].widget is widget)
+
+ def test_custom_callback(self):
+ """Test that a custom formfield_callback is used if provided"""
+
+ callback_args = []
+
+ def callback(db_field, **kwargs):
+ callback_args.append((db_field, kwargs))
+ return db_field.formfield(**kwargs)
+
+ widget = forms.Textarea()
+
+ class BaseForm(forms.ModelForm):
+ class Meta:
+ model = Person
+ widgets = {'name': widget}
+
+ _ = modelform_factory(Person, form=BaseForm,
+ formfield_callback=callback)
+ id_field, name_field = Person._meta.fields
+
+ self.assertEqual(callback_args,
+ [(id_field, {}), (name_field, {'widget': widget})])
+
+ def test_bad_callback(self):
+ # A bad callback provided by user still gives an error
+ self.assertRaises(TypeError, modelform_factory, Person,
+ formfield_callback='not a function or callable')
+
+
+class InvalidFieldAndFactory(TestCase):
+ """ Tests for #11905 """
+
+ def test_extra_field_model_form(self):
+ try:
+ class ExtraPersonForm(forms.ModelForm):
+ """ ModelForm with an extra field """
+
+ age = forms.IntegerField()
+
+ class Meta:
+ model = Person
+ fields = ('name', 'no-field')
+ except FieldError, e:
+ # Make sure the exception contains some reference to the
+ # field responsible for the problem.
+ self.assertTrue('no-field' in e.args[0])
+ else:
+ self.fail('Invalid "no-field" field not caught')
+
+ def test_extra_declared_field_model_form(self):
+ try:
+ class ExtraPersonForm(forms.ModelForm):
+ """ ModelForm with an extra field """
+
+ age = forms.IntegerField()
+
+ class Meta:
+ model = Person
+ fields = ('name', 'age')
+ except FieldError:
+ self.fail('Declarative field raised FieldError incorrectly')
+
+ def test_extra_field_modelform_factory(self):
+ self.assertRaises(FieldError, modelform_factory,
+ Person, fields=['no-field', 'name'])
+
diff --git a/tests/regressiontests/model_formsets_regress/tests.py b/tests/regressiontests/model_formsets_regress/tests.py
index 61bc514324..ee2a26f6c2 100644
--- a/tests/regressiontests/model_formsets_regress/tests.py
+++ b/tests/regressiontests/model_formsets_regress/tests.py
@@ -1,8 +1,10 @@
-from django.forms.models import modelform_factory, inlineformset_factory
+from django import forms
+from django.forms.models import modelform_factory, inlineformset_factory, modelformset_factory
from django.test import TestCase
from models import User, UserSite, Restaurant, Manager
+
class InlineFormsetTests(TestCase):
def test_formset_over_to_field(self):
"A formset over a ForeignKey with a to_field can be saved. Regression for #10243"
@@ -156,3 +158,61 @@ class InlineFormsetTests(TestCase):
# you can create a formset with an instance of None
form = Form(instance=None)
formset = FormSet(instance=None)
+
+
+class CustomWidget(forms.CharField):
+ pass
+
+
+class UserSiteForm(forms.ModelForm):
+ class Meta:
+ model = UserSite
+ widgets = {'data': CustomWidget}
+
+
+class Callback(object):
+
+ def __init__(self):
+ self.log = []
+
+ def __call__(self, db_field, **kwargs):
+ self.log.append((db_field, kwargs))
+ return db_field.formfield(**kwargs)
+
+
+class FormfieldCallbackTests(TestCase):
+ """
+ Regression for #13095: Using base forms with widgets
+ defined in Meta should not raise errors.
+ """
+
+ def test_inlineformset_factory_default(self):
+ Formset = inlineformset_factory(User, UserSite, form=UserSiteForm)
+ form = Formset({}).forms[0]
+ self.assertTrue(isinstance(form['data'].field.widget, CustomWidget))
+
+ def test_modelformset_factory_default(self):
+ Formset = modelformset_factory(UserSite, form=UserSiteForm)
+ form = Formset({}).forms[0]
+ self.assertTrue(isinstance(form['data'].field.widget, CustomWidget))
+
+ def assertCallbackCalled(self, callback):
+ id_field, user_field, data_field = UserSite._meta.fields
+ expected_log = [
+ (id_field, {}),
+ (user_field, {}),
+ (data_field, {'widget': CustomWidget}),
+ ]
+ self.assertEqual(callback.log, expected_log)
+
+ def test_inlineformset_custom_callback(self):
+ callback = Callback()
+ inlineformset_factory(User, UserSite, form=UserSiteForm,
+ formfield_callback=callback)
+ self.assertCallbackCalled(callback)
+
+ def test_modelformset_custom_callback(self):
+ callback = Callback()
+ modelformset_factory(UserSite, form=UserSiteForm,
+ formfield_callback=callback)
+ self.assertCallbackCalled(callback)
diff --git a/tests/regressiontests/multiple_database/fixtures/pets.json b/tests/regressiontests/multiple_database/fixtures/pets.json
new file mode 100644
index 0000000000..89756a3e5b
--- /dev/null
+++ b/tests/regressiontests/multiple_database/fixtures/pets.json
@@ -0,0 +1,18 @@
+[
+ {
+ "pk": 1,
+ "model": "multiple_database.pet",
+ "fields": {
+ "name": "Mr Bigglesworth",
+ "owner": 1
+ }
+ },
+ {
+ "pk": 2,
+ "model": "multiple_database.pet",
+ "fields": {
+ "name": "Spot",
+ "owner": 2
+ }
+ }
+] \ No newline at end of file
diff --git a/tests/regressiontests/multiple_database/tests.py b/tests/regressiontests/multiple_database/tests.py
index 7bde8bf037..7f66ea3bcf 100644
--- a/tests/regressiontests/multiple_database/tests.py
+++ b/tests/regressiontests/multiple_database/tests.py
@@ -7,6 +7,7 @@ from django.conf import settings
from django.contrib.auth.models import User
from django.core import management
from django.db import connections, router, DEFAULT_DB_ALIAS
+from django.db.models import signals
from django.db.utils import ConnectionRouter
from django.test import TestCase
@@ -883,7 +884,13 @@ class QueryTestCase(TestCase):
self.assertRaises(ValueError, str, qs.query)
# Evaluating the query shouldn't work, either
- self.assertRaises(ValueError, list, qs)
+ try:
+ for obj in qs:
+ pass
+ self.fail('Iterating over query should raise ValueError')
+ except ValueError:
+ pass
+
class TestRouter(object):
# A test router. The behaviour is vaguely master/slave, but the
@@ -1491,19 +1498,10 @@ class AuthTestCase(TestCase):
self.old_routers = router.routers
router.routers = [AuthRouter()]
- # Redirect stdout to a buffer so we can test
- # the output of a management command
- self.old_stdout = sys.stdout
- self.stdout = StringIO()
- sys.stdout = self.stdout
-
def tearDown(self):
# Restore the 'other' database as an independent database
router.routers = self.old_routers
- # Restore stdout
- sys.stdout = self.old_stdout
-
def test_auth_manager(self):
"The methods on the auth manager obey database hints"
# Create one user using default allocation policy
@@ -1539,14 +1537,16 @@ class AuthTestCase(TestCase):
# Check that dumping the default database doesn't try to include auth
# because allow_syncdb prohibits auth on default
- self.stdout.flush()
- management.call_command('dumpdata', 'auth', format='json', database='default')
- self.assertEquals(self.stdout.getvalue(), '[]\n')
+ new_io = StringIO()
+ management.call_command('dumpdata', 'auth', format='json', database='default', stdout=new_io)
+ command_output = new_io.getvalue().strip()
+ self.assertEqual(command_output, '[]')
# Check that dumping the other database does include auth
- self.stdout.flush()
- management.call_command('dumpdata', 'auth', format='json', database='other')
- self.assertTrue('alice@example.com' in self.stdout.getvalue())
+ new_io = StringIO()
+ management.call_command('dumpdata', 'auth', format='json', database='other', stdout=new_io)
+ command_output = new_io.getvalue().strip()
+ self.assertTrue('"email": "alice@example.com",' in command_output)
class UserProfileTestCase(TestCase):
def setUp(self):
@@ -1570,11 +1570,31 @@ class UserProfileTestCase(TestCase):
self.assertEquals(alice.get_profile().flavor, 'chocolate')
self.assertEquals(bob.get_profile().flavor, 'crunchy frog')
+class AntiPetRouter(object):
+ # A router that only expresses an opinion on syncdb,
+ # passing pets to the 'other' database
+
+ def allow_syncdb(self, db, model):
+ "Make sure the auth app only appears on the 'other' db"
+ if db == 'other':
+ return model._meta.object_name == 'Pet'
+ else:
+ return model._meta.object_name != 'Pet'
+ return None
class FixtureTestCase(TestCase):
multi_db = True
fixtures = ['multidb-common', 'multidb']
+ def setUp(self):
+ # Install the anti-pet router
+ self.old_routers = router.routers
+ router.routers = [AntiPetRouter()]
+
+ def tearDown(self):
+ # Restore the 'other' database as an independent database
+ router.routers = self.old_routers
+
def test_fixture_loading(self):
"Multi-db fixtures are loaded correctly"
# Check that "Pro Django" exists on the default database, but not on other database
@@ -1612,6 +1632,14 @@ class FixtureTestCase(TestCase):
except Book.DoesNotExist:
self.fail('"The Definitive Guide to Django" should exist on both databases')
+ def test_pseudo_empty_fixtures(self):
+ "A fixture can contain entries, but lead to nothing in the database; this shouldn't raise an error (ref #14068)"
+ new_io = StringIO()
+ management.call_command('loaddata', 'pets', stdout=new_io, stderr=new_io)
+ command_output = new_io.getvalue().strip()
+ # No objects will actually be loaded
+ self.assertEqual(command_output, "Installed 0 object(s) (of 2) from 1 fixture(s)")
+
class PickleQuerySetTestCase(TestCase):
multi_db = True
@@ -1620,3 +1648,114 @@ class PickleQuerySetTestCase(TestCase):
Book.objects.using(db).create(title='Dive into Python', published=datetime.date(2009, 5, 4))
qs = Book.objects.all()
self.assertEqual(qs.db, pickle.loads(pickle.dumps(qs)).db)
+
+
+class DatabaseReceiver(object):
+ """
+ Used in the tests for the database argument in signals (#13552)
+ """
+ def __call__(self, signal, sender, **kwargs):
+ self._database = kwargs['using']
+
+class WriteToOtherRouter(object):
+ """
+ A router that sends all writes to the other database.
+ """
+ def db_for_write(self, model, **hints):
+ return "other"
+
+class SignalTests(TestCase):
+ multi_db = True
+
+ def setUp(self):
+ self.old_routers = router.routers
+
+ def tearDown(self):
+ router.routser = self.old_routers
+
+ def _write_to_other(self):
+ "Sends all writes to 'other'."
+ router.routers = [WriteToOtherRouter()]
+
+ def _write_to_default(self):
+ "Sends all writes to the default DB"
+ router.routers = self.old_routers
+
+ def test_database_arg_save_and_delete(self):
+ """
+ Tests that the pre/post_save signal contains the correct database.
+ (#13552)
+ """
+ # Make some signal receivers
+ pre_save_receiver = DatabaseReceiver()
+ post_save_receiver = DatabaseReceiver()
+ pre_delete_receiver = DatabaseReceiver()
+ post_delete_receiver = DatabaseReceiver()
+ # Make model and connect receivers
+ signals.pre_save.connect(sender=Person, receiver=pre_save_receiver)
+ signals.post_save.connect(sender=Person, receiver=post_save_receiver)
+ signals.pre_delete.connect(sender=Person, receiver=pre_delete_receiver)
+ signals.post_delete.connect(sender=Person, receiver=post_delete_receiver)
+ p = Person.objects.create(name='Darth Vader')
+ # Save and test receivers got calls
+ p.save()
+ self.assertEqual(pre_save_receiver._database, DEFAULT_DB_ALIAS)
+ self.assertEqual(post_save_receiver._database, DEFAULT_DB_ALIAS)
+ # Delete, and test
+ p.delete()
+ self.assertEqual(pre_delete_receiver._database, DEFAULT_DB_ALIAS)
+ self.assertEqual(post_delete_receiver._database, DEFAULT_DB_ALIAS)
+ # Save again to a different database
+ p.save(using="other")
+ self.assertEqual(pre_save_receiver._database, "other")
+ self.assertEqual(post_save_receiver._database, "other")
+ # Delete, and test
+ p.delete(using="other")
+ self.assertEqual(pre_delete_receiver._database, "other")
+ self.assertEqual(post_delete_receiver._database, "other")
+
+ def test_database_arg_m2m(self):
+ """
+ Test that the m2m_changed signal has a correct database arg (#13552)
+ """
+ # Make a receiver
+ receiver = DatabaseReceiver()
+ # Connect it, and make the models
+ signals.m2m_changed.connect(receiver=receiver)
+
+ b = Book.objects.create(title="Pro Django",
+ published=datetime.date(2008, 12, 16))
+
+ p = Person.objects.create(name="Marty Alchin")
+
+ # Test addition
+ b.authors.add(p)
+ self.assertEqual(receiver._database, DEFAULT_DB_ALIAS)
+ self._write_to_other()
+ b.authors.add(p)
+ self._write_to_default()
+ self.assertEqual(receiver._database, "other")
+
+ # Test removal
+ b.authors.remove(p)
+ self.assertEqual(receiver._database, DEFAULT_DB_ALIAS)
+ self._write_to_other()
+ b.authors.remove(p)
+ self._write_to_default()
+ self.assertEqual(receiver._database, "other")
+
+ # Test addition in reverse
+ p.book_set.add(b)
+ self.assertEqual(receiver._database, DEFAULT_DB_ALIAS)
+ self._write_to_other()
+ p.book_set.add(b)
+ self._write_to_default()
+ self.assertEqual(receiver._database, "other")
+
+ # Test clearing
+ b.authors.clear()
+ self.assertEqual(receiver._database, DEFAULT_DB_ALIAS)
+ self._write_to_other()
+ b.authors.clear()
+ self._write_to_default()
+ self.assertEqual(receiver._database, "other")
diff --git a/tests/regressiontests/requests/tests.py b/tests/regressiontests/requests/tests.py
index 1615a73406..22bc88c172 100644
--- a/tests/regressiontests/requests/tests.py
+++ b/tests/regressiontests/requests/tests.py
@@ -1,5 +1,5 @@
"""
->>> from django.http import HttpRequest
+>>> from django.http import HttpRequest, HttpResponse
>>> print repr(HttpRequest())
<HttpRequest
GET:{},
@@ -44,4 +44,27 @@ https://www.example.com/asdf
>>> request.path = ''
>>> print request.build_absolute_uri(location="/path/with:colons")
http://www.example.com/path/with:colons
+
+
+# Test cookie datetime expiration logic
+>>> from datetime import datetime, timedelta
+>>> delta = timedelta(seconds=10)
+>>> response = HttpResponse()
+>>> response.set_cookie('datetime', expires=datetime.utcnow()+delta)
+>>> datetime_cookie = response.cookies['datetime']
+>>> datetime_cookie['max-age']
+10
+>>> response.set_cookie('datetime', expires=datetime(2028, 1, 1, 4, 5, 6))
+>>> response.cookies['datetime']['expires']
+'Sat, 01-Jan-2028 04:05:06 GMT'
+
+# Test automatically setting cookie expires if only max_age is provided
+>>> response.set_cookie('max_age', max_age=10)
+>>> max_age_cookie = response.cookies['max_age']
+>>> max_age_cookie['max-age']
+10
+>>> from django.utils.http import cookie_date
+>>> import time
+>>> max_age_cookie['expires'] == cookie_date(time.time()+10)
+True
"""
diff --git a/tests/regressiontests/serializers_regress/tests.py b/tests/regressiontests/serializers_regress/tests.py
index 84e90ff7e1..be920c6920 100644
--- a/tests/regressiontests/serializers_regress/tests.py
+++ b/tests/regressiontests/serializers_regress/tests.py
@@ -10,14 +10,16 @@ forward, backwards and self references.
import datetime
import decimal
-import unittest
-from cStringIO import StringIO
+try:
+ from cStringIO import StringIO
+except ImportError:
+ from StringIO import StringIO
-from django.utils.functional import curry
-from django.core import serializers
-from django.db import transaction, DEFAULT_DB_ALIAS
-from django.core import management
from django.conf import settings
+from django.core import serializers, management
+from django.db import transaction, DEFAULT_DB_ALIAS
+from django.test import TestCase
+from django.utils.functional import curry
from models import *
@@ -59,10 +61,10 @@ def im2m_create(pk, klass, data):
def im_create(pk, klass, data):
instance = klass(id=pk)
- setattr(instance, 'right_id', data['right'])
- setattr(instance, 'left_id', data['left'])
+ instance.right_id = data['right']
+ instance.left_id = data['left']
if 'extra' in data:
- setattr(instance, 'extra', data['extra'])
+ instance.extra = data['extra']
models.Model.save_base(instance, raw=True)
return [instance]
@@ -96,7 +98,9 @@ def inherited_create(pk, klass, data):
def data_compare(testcase, pk, klass, data):
instance = klass.objects.get(id=pk)
testcase.assertEqual(data, instance.data,
- "Objects with PK=%d not equal; expected '%s' (%s), got '%s' (%s)" % (pk,data, type(data), instance.data, type(instance.data)))
+ "Objects with PK=%d not equal; expected '%s' (%s), got '%s' (%s)" % (
+ pk, data, type(data), instance.data, type(instance.data))
+ )
def generic_compare(testcase, pk, klass, data):
instance = klass.objects.get(id=pk)
@@ -348,28 +352,16 @@ if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.mysql':
# Dynamically create serializer tests to ensure that all
# registered serializers are automatically tested.
-class SerializerTests(unittest.TestCase):
+class SerializerTests(TestCase):
pass
def serializerTest(format, self):
- # Clear the database first
- management.call_command('flush', verbosity=0, interactive=False)
# Create all the objects defined in the test data
objects = []
instance_count = {}
- transaction.enter_transaction_management()
- try:
- transaction.managed(True)
- for (func, pk, klass, datum) in test_data:
- objects.extend(func[0](pk, klass, datum))
- instance_count[klass] = 0
- transaction.commit()
- except:
- transaction.rollback()
- transaction.leave_transaction_management()
- raise
- transaction.leave_transaction_management()
+ for (func, pk, klass, datum) in test_data:
+ objects.extend(func[0](pk, klass, datum))
# Get a count of the number of objects created for each class
for klass in instance_count:
@@ -381,19 +373,8 @@ def serializerTest(format, self):
# Serialize the test database
serialized_data = serializers.serialize(format, objects, indent=2)
- # Flush the database and recreate from the serialized data
- management.call_command('flush', verbosity=0, interactive=False)
- transaction.enter_transaction_management()
- try:
- transaction.managed(True)
- for obj in serializers.deserialize(format, serialized_data):
- obj.save()
- transaction.commit()
- except:
- transaction.rollback()
- transaction.leave_transaction_management()
- raise
- transaction.leave_transaction_management()
+ for obj in serializers.deserialize(format, serialized_data):
+ obj.save()
# Assert that the deserialized data is the same
# as the original source
@@ -406,10 +387,7 @@ def serializerTest(format, self):
self.assertEquals(count, klass.objects.count())
def fieldsTest(format, self):
- # Clear the database first
- management.call_command('flush', verbosity=0, interactive=False)
-
- obj = ComplexModel(field1='first',field2='second',field3='third')
+ obj = ComplexModel(field1='first', field2='second', field3='third')
obj.save_base(raw=True)
# Serialize then deserialize the test database
@@ -422,9 +400,6 @@ def fieldsTest(format, self):
self.assertEqual(result.object.field3, 'third')
def streamTest(format, self):
- # Clear the database first
- management.call_command('flush', verbosity=0, interactive=False)
-
obj = ComplexModel(field1='first',field2='second',field3='third')
obj.save_base(raw=True)
@@ -440,7 +415,7 @@ def streamTest(format, self):
stream.close()
for format in serializers.get_serializer_formats():
- setattr(SerializerTests, 'test_'+format+'_serializer', curry(serializerTest, format))
- setattr(SerializerTests, 'test_'+format+'_serializer_fields', curry(fieldsTest, format))
+ setattr(SerializerTests, 'test_' + format + '_serializer', curry(serializerTest, format))
+ setattr(SerializerTests, 'test_' + format + '_serializer_fields', curry(fieldsTest, format))
if format != 'python':
- setattr(SerializerTests, 'test_'+format+'_serializer_stream', curry(streamTest, format))
+ setattr(SerializerTests, 'test_' + format + '_serializer_stream', curry(streamTest, format))
diff --git a/tests/regressiontests/templates/filters.py b/tests/regressiontests/templates/filters.py
index 3d6284e881..af34c58a9f 100644
--- a/tests/regressiontests/templates/filters.py
+++ b/tests/regressiontests/templates/filters.py
@@ -328,7 +328,12 @@ def get_filter_tests():
'join03': (r'{{ a|join:" &amp; " }}', {'a': ['alpha', 'beta & me']}, 'alpha &amp; beta &amp; me'),
'join04': (r'{% autoescape off %}{{ a|join:" &amp; " }}{% endautoescape %}', {'a': ['alpha', 'beta & me']}, 'alpha &amp; beta & me'),
-
+ # Test that joining with unsafe joiners don't result in unsafe strings (#11377)
+ 'join05': (r'{{ a|join:var }}', {'a': ['alpha', 'beta & me'], 'var': ' & '}, 'alpha &amp; beta &amp; me'),
+ 'join06': (r'{{ a|join:var }}', {'a': ['alpha', 'beta & me'], 'var': mark_safe(' & ')}, 'alpha & beta &amp; me'),
+ 'join07': (r'{{ a|join:var|lower }}', {'a': ['Alpha', 'Beta & me'], 'var': ' & ' }, 'alpha &amp; beta &amp; me'),
+ 'join08': (r'{{ a|join:var|lower }}', {'a': ['Alpha', 'Beta & me'], 'var': mark_safe(' & ')}, 'alpha & beta &amp; me'),
+
'date01': (r'{{ d|date:"m" }}', {'d': datetime(2008, 1, 1)}, '01'),
'date02': (r'{{ d|date }}', {'d': datetime(2008, 1, 1)}, 'Jan. 1, 2008'),
#Ticket 9520: Make sure |date doesn't blow up on non-dates
@@ -341,5 +346,5 @@ def get_filter_tests():
'add04': (r'{{ i|add:"16" }}', {'i': 'not_an_int'}, 'not_an_int16'),
'add05': (r'{{ l1|add:l2 }}', {'l1': [1, 2], 'l2': [3, 4]}, '[1, 2, 3, 4]'),
'add06': (r'{{ t1|add:t2 }}', {'t1': (3, 4), 't2': (1, 2)}, '(3, 4, 1, 2)'),
- 'add07': (r'{{ d|add:t }}', {'d': date(2000, 1, 1), 't': timedelta(10)}, '2000-01-11'),
+ 'add07': (r'{{ d|add:t }}', {'d': date(2000, 1, 1), 't': timedelta(10)}, 'Jan. 11, 2000'),
}
diff --git a/tests/regressiontests/templates/loaders.py b/tests/regressiontests/templates/loaders.py
index 64a0dc6505..caa3faa6bc 100644
--- a/tests/regressiontests/templates/loaders.py
+++ b/tests/regressiontests/templates/loaders.py
@@ -15,9 +15,11 @@ import pkg_resources
import imp
import StringIO
import os.path
+import warnings
from django.template import TemplateDoesNotExist, Context
from django.template.loaders.eggs import load_template_source as lts_egg
+from django.template.loaders.eggs import Loader as EggLoader
from django.template import loader
# Mock classes and objects for pkg_resources functions.
@@ -53,7 +55,33 @@ def create_egg(name, resources):
egg._resources = resources
sys.modules[name] = egg
-class EggLoader(unittest.TestCase):
+class DeprecatedEggLoaderTest(unittest.TestCase):
+ "Test the deprecated load_template_source interface to the egg loader"
+ def setUp(self):
+ pkg_resources._provider_factories[MockLoader] = MockProvider
+
+ self.empty_egg = create_egg("egg_empty", {})
+ self.egg_1 = create_egg("egg_1", {
+ os.path.normcase('templates/y.html') : StringIO.StringIO("y"),
+ os.path.normcase('templates/x.txt') : StringIO.StringIO("x"),
+ })
+ self._old_installed_apps = settings.INSTALLED_APPS
+ settings.INSTALLED_APPS = []
+ warnings.simplefilter("ignore", PendingDeprecationWarning)
+
+ def tearDown(self):
+ settings.INSTALLED_APPS = self._old_installed_apps
+ warnings.resetwarnings()
+
+ def test_existing(self):
+ "A template can be loaded from an egg"
+ settings.INSTALLED_APPS = ['egg_1']
+ contents, template_name = lts_egg("y.html")
+ self.assertEqual(contents, "y")
+ self.assertEqual(template_name, "egg:egg_1:templates/y.html")
+
+
+class EggLoaderTest(unittest.TestCase):
def setUp(self):
pkg_resources._provider_factories[MockLoader] = MockProvider
@@ -71,24 +99,28 @@ class EggLoader(unittest.TestCase):
def test_empty(self):
"Loading any template on an empty egg should fail"
settings.INSTALLED_APPS = ['egg_empty']
- self.assertRaises(TemplateDoesNotExist, lts_egg, "not-existing.html")
+ egg_loader = EggLoader()
+ self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "not-existing.html")
def test_non_existing(self):
"Template loading fails if the template is not in the egg"
settings.INSTALLED_APPS = ['egg_1']
- self.assertRaises(TemplateDoesNotExist, lts_egg, "not-existing.html")
+ egg_loader = EggLoader()
+ self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "not-existing.html")
def test_existing(self):
"A template can be loaded from an egg"
settings.INSTALLED_APPS = ['egg_1']
- contents, template_name = lts_egg("y.html")
+ egg_loader = EggLoader()
+ contents, template_name = egg_loader.load_template_source("y.html")
self.assertEqual(contents, "y")
self.assertEqual(template_name, "egg:egg_1:templates/y.html")
def test_not_installed(self):
"Loading an existent template from an egg not included in INSTALLED_APPS should fail"
settings.INSTALLED_APPS = []
- self.assertRaises(TemplateDoesNotExist, lts_egg, "y.html")
+ egg_loader = EggLoader()
+ self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "y.html")
class CachedLoader(unittest.TestCase):
def setUp(self):
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
index 5902e8d5e7..bbbcae30eb 100644
--- a/tests/regressiontests/templates/tests.py
+++ b/tests/regressiontests/templates/tests.py
@@ -506,6 +506,17 @@ class Templates(unittest.TestCase):
'basic-syntax28': ("{{ a.b }}", {'a': SilentGetItemClass()}, ('', 'INVALID')),
'basic-syntax29': ("{{ a.b }}", {'a': SilentAttrClass()}, ('', 'INVALID')),
+ # Something that starts like a number but has an extra lookup works as a lookup.
+ 'basic-syntax30': ("{{ 1.2.3 }}", {"1": {"2": {"3": "d"}}}, "d"),
+ 'basic-syntax31': ("{{ 1.2.3 }}", {"1": {"2": ("a", "b", "c", "d")}}, "d"),
+ 'basic-syntax32': ("{{ 1.2.3 }}", {"1": (("x", "x", "x", "x"), ("y", "y", "y", "y"), ("a", "b", "c", "d"))}, "d"),
+ 'basic-syntax33': ("{{ 1.2.3 }}", {"1": ("xxxx", "yyyy", "abcd")}, "d"),
+ 'basic-syntax34': ("{{ 1.2.3 }}", {"1": ({"x": "x"}, {"y": "y"}, {"z": "z", "3": "d"})}, "d"),
+
+ # Numbers are numbers even if their digits are in the context.
+ 'basic-syntax35': ("{{ 1 }}", {"1": "abc"}, "1"),
+ 'basic-syntax36': ("{{ 1.2 }}", {"1": "abc"}, "1.2"),
+
# List-index syntax allows a template to access a certain item of a subscriptable object.
'list-index01': ("{{ var.1 }}", {"var": ["first item", "second item"]}, "second item"),
@@ -592,7 +603,7 @@ class Templates(unittest.TestCase):
#filters should accept empty string constants
'filter-syntax20': ('{{ ""|default_if_none:"was none" }}', {}, ""),
-
+
### COMMENT SYNTAX ########################################################
'comment-syntax01': ("{# this is hidden #}hello", {}, "hello"),
'comment-syntax02': ("{# this is hidden #}hello{# foo #}", {}, "hello"),
@@ -690,6 +701,7 @@ class Templates(unittest.TestCase):
'for-tag-unpack11': ("{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, ("one:1,/two:2,/", "one:1,INVALID/two:2,INVALID/")),
'for-tag-unpack12': ("{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}", {"items": (('one', 1, 'carrot'), ('two', 2))}, ("one:1,carrot/two:2,/", "one:1,carrot/two:2,INVALID/")),
'for-tag-unpack13': ("{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}", {"items": (('one', 1, 'carrot'), ('two', 2, 'cheese'))}, ("one:1,carrot/two:2,cheese/", "one:1,carrot/two:2,cheese/")),
+ 'for-tag-unpack14': ("{% for x,y in items %}{{ x }}:{{ y }}/{% endfor %}", {"items": (1, 2)}, (":/:/", "INVALID:INVALID/INVALID:INVALID/")),
'for-tag-empty01': ("{% for val in values %}{{ val }}{% empty %}empty text{% endfor %}", {"values": [1, 2, 3]}, "123"),
'for-tag-empty02': ("{% for val in values %}{{ val }}{% empty %}values array empty{% endfor %}", {"values": []}, "values array empty"),
'for-tag-empty03': ("{% for val in values %}{{ val }}{% empty %}values array not found{% endfor %}", {}, "values array not found"),
@@ -1285,7 +1297,8 @@ class Templates(unittest.TestCase):
# Regression test for #11270.
'cache17': ('{% load cache %}{% cache 10 long_cache_key poem %}Some Content{% endcache %}', {'poem': 'Oh freddled gruntbuggly/Thy micturations are to me/As plurdled gabbleblotchits/On a lurgid bee/That mordiously hath bitled out/Its earted jurtles/Into a rancid festering/Or else I shall rend thee in the gobberwarts with my blurglecruncheon/See if I dont.'}, 'Some Content'),
-
+
+
### AUTOESCAPE TAG ##############################################
'autoescape-tag01': ("{% autoescape off %}hello{% endautoescape %}", {}, "hello"),
'autoescape-tag02': ("{% autoescape off %}{{ first }}{% endautoescape %}", {"first": "<b>hello</b>"}, "<b>hello</b>"),
@@ -1314,6 +1327,23 @@ class Templates(unittest.TestCase):
# implementation details (fortunately, the (no)autoescape block
# tags can be used in those cases)
'autoescape-filtertag01': ("{{ first }}{% filter safe %}{{ first }} x<y{% endfilter %}", {"first": "<a>"}, template.TemplateSyntaxError),
+
+ # ifqeual compares unescaped vales.
+ 'autoescape-ifequal01': ('{% ifequal var "this & that" %}yes{% endifequal %}', { "var": "this & that" }, "yes" ),
+
+ # Arguments to filters are 'safe' and manipulate their input unescaped.
+ 'autoescape-filters01': ('{{ var|cut:"&" }}', { "var": "this & that" }, "this that" ),
+ 'autoescape-filters02': ('{{ var|join:" & \" }}', { "var": ("Tom", "Dick", "Harry") }, "Tom & Dick & Harry" ),
+
+ # Literal strings are safe.
+ 'autoescape-literals01': ('{{ "this & that" }}',{}, "this & that" ),
+
+ # Iterating over strings outputs safe characters.
+ 'autoescape-stringiterations01': ('{% for l in var %}{{ l }},{% endfor %}', {'var': 'K&R'}, "K,&amp;,R," ),
+
+ # Escape requirement survives lookup.
+ 'autoescape-lookup01': ('{{ var.key }}', { "var": {"key": "this & that" }}, "this &amp; that" ),
+
}
diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py
index 6da7ae4445..22b59e54a9 100644
--- a/tests/regressiontests/test_client_regress/models.py
+++ b/tests/regressiontests/test_client_regress/models.py
@@ -11,6 +11,7 @@ from django.core.urlresolvers import reverse
from django.core.exceptions import SuspiciousOperation
from django.template import TemplateDoesNotExist, TemplateSyntaxError, Context
from django.template import loader
+from django.test.client import encode_file
class AssertContainsTests(TestCase):
def setUp(self):
@@ -34,20 +35,20 @@ class AssertContainsTests(TestCase):
try:
self.assertContains(response, 'text', status_code=999)
except AssertionError, e:
- self.assertEquals(str(e), "Couldn't retrieve page: Response code was 200 (expected 999)")
+ self.assertEquals(str(e), "Couldn't retrieve content: Response code was 200 (expected 999)")
try:
self.assertContains(response, 'text', status_code=999, msg_prefix='abc')
except AssertionError, e:
- self.assertEquals(str(e), "abc: Couldn't retrieve page: Response code was 200 (expected 999)")
+ self.assertEquals(str(e), "abc: Couldn't retrieve content: Response code was 200 (expected 999)")
try:
self.assertNotContains(response, 'text', status_code=999)
except AssertionError, e:
- self.assertEquals(str(e), "Couldn't retrieve page: Response code was 200 (expected 999)")
+ self.assertEquals(str(e), "Couldn't retrieve content: Response code was 200 (expected 999)")
try:
self.assertNotContains(response, 'text', status_code=999, msg_prefix='abc')
except AssertionError, e:
- self.assertEquals(str(e), "abc: Couldn't retrieve page: Response code was 200 (expected 999)")
+ self.assertEquals(str(e), "abc: Couldn't retrieve content: Response code was 200 (expected 999)")
try:
self.assertNotContains(response, 'once')
@@ -619,6 +620,7 @@ class ContextTests(TestCase):
"Context variables can be retrieved from a single context"
response = self.client.get("/test_client_regress/request_data/", data={'foo':'whiz'})
self.assertEqual(response.context.__class__, Context)
+ self.assertTrue('get-foo' in response.context)
self.assertEqual(response.context['get-foo'], 'whiz')
self.assertEqual(response.context['request-foo'], 'whiz')
self.assertEqual(response.context['data'], 'sausage')
@@ -634,6 +636,7 @@ class ContextTests(TestCase):
response = self.client.get("/test_client_regress/request_data_extended/", data={'foo':'whiz'})
self.assertEqual(response.context.__class__, ContextList)
self.assertEqual(len(response.context), 2)
+ self.assertTrue('get-foo' in response.context)
self.assertEqual(response.context['get-foo'], 'whiz')
self.assertEqual(response.context['request-foo'], 'whiz')
self.assertEqual(response.context['data'], 'bacon')
@@ -821,3 +824,40 @@ class UnicodePayloadTests(TestCase):
response = self.client.post("/test_client_regress/parse_unicode_json/", json,
content_type="application/json; charset=koi8-r")
self.assertEqual(response.content, json.encode('koi8-r'))
+
+class DummyFile(object):
+ def __init__(self, filename):
+ self.name = filename
+ def read(self):
+ return 'TEST_FILE_CONTENT'
+
+class UploadedFileEncodingTest(TestCase):
+ def test_file_encoding(self):
+ encoded_file = encode_file('TEST_BOUNDARY', 'TEST_KEY', DummyFile('test_name.bin'))
+ self.assertEqual('--TEST_BOUNDARY', encoded_file[0])
+ self.assertEqual('Content-Disposition: form-data; name="TEST_KEY"; filename="test_name.bin"', encoded_file[1])
+ self.assertEqual('TEST_FILE_CONTENT', encoded_file[-1])
+
+ def test_guesses_content_type_on_file_encoding(self):
+ self.assertEqual('Content-Type: application/octet-stream',
+ encode_file('IGNORE', 'IGNORE', DummyFile("file.bin"))[2])
+ self.assertEqual('Content-Type: text/plain',
+ encode_file('IGNORE', 'IGNORE', DummyFile("file.txt"))[2])
+ self.assertEqual('Content-Type: application/zip',
+ encode_file('IGNORE', 'IGNORE', DummyFile("file.zip"))[2])
+ self.assertEqual('Content-Type: application/octet-stream',
+ encode_file('IGNORE', 'IGNORE', DummyFile("file.unknown"))[2])
+
+class RequestHeadersTest(TestCase):
+ def test_client_headers(self):
+ "A test client can receive custom headers"
+ response = self.client.get("/test_client_regress/check_headers/", HTTP_X_ARG_CHECK='Testing 123')
+ self.assertEquals(response.content, "HTTP_X_ARG_CHECK: Testing 123")
+ self.assertEquals(response.status_code, 200)
+
+ def test_client_headers_redirect(self):
+ "Test client headers are preserved through redirects"
+ response = self.client.get("/test_client_regress/check_headers_redirect/", follow=True, HTTP_X_ARG_CHECK='Testing 123')
+ self.assertEquals(response.content, "HTTP_X_ARG_CHECK: Testing 123")
+ self.assertRedirects(response, '/test_client_regress/check_headers/',
+ status_code=301, target_status_code=200)
diff --git a/tests/regressiontests/test_client_regress/urls.py b/tests/regressiontests/test_client_regress/urls.py
index 99eb7b70be..650d80b909 100644
--- a/tests/regressiontests/test_client_regress/urls.py
+++ b/tests/regressiontests/test_client_regress/urls.py
@@ -24,4 +24,6 @@ urlpatterns = patterns('',
(r'^request_methods/$', views.request_methods_view),
(r'^check_unicode/$', views.return_unicode),
(r'^parse_unicode_json/$', views.return_json_file),
+ (r'^check_headers/$', views.check_headers),
+ (r'^check_headers_redirect/$', redirect_to, {'url': '/test_client_regress/check_headers/'}),
)
diff --git a/tests/regressiontests/test_client_regress/views.py b/tests/regressiontests/test_client_regress/views.py
index 5ba7cc30a9..40aa61fca7 100644
--- a/tests/regressiontests/test_client_regress/views.py
+++ b/tests/regressiontests/test_client_regress/views.py
@@ -86,3 +86,8 @@ def return_json_file(request):
mimetype='application/json; charset=' + charset)
response['Content-Disposition'] = 'attachment; filename=testfile.json'
return response
+
+def check_headers(request):
+ "A view that responds with value of the X-ARG-CHECK header"
+ return HttpResponse('HTTP_X_ARG_CHECK: %s' % request.META.get('HTTP_X_ARG_CHECK', 'Undefined'))
+
diff --git a/tests/regressiontests/urlpatterns_reverse/included_named_urls.py b/tests/regressiontests/urlpatterns_reverse/included_named_urls.py
new file mode 100644
index 0000000000..b3f7903b41
--- /dev/null
+++ b/tests/regressiontests/urlpatterns_reverse/included_named_urls.py
@@ -0,0 +1,10 @@
+from django.conf.urls.defaults import *
+from views import empty_view
+
+urlpatterns = patterns('',
+ url(r'^$', empty_view, name="named-url3"),
+ url(r'^extra/(?P<extra>\w+)/$', empty_view, name="named-url4"),
+ url(r'^(?P<one>\d+)|(?P<two>\d+)/$', empty_view),
+ (r'^included/', include('regressiontests.urlpatterns_reverse.included_named_urls2')),
+)
+
diff --git a/tests/regressiontests/urlpatterns_reverse/included_named_urls2.py b/tests/regressiontests/urlpatterns_reverse/included_named_urls2.py
new file mode 100644
index 0000000000..96c42c3e4d
--- /dev/null
+++ b/tests/regressiontests/urlpatterns_reverse/included_named_urls2.py
@@ -0,0 +1,9 @@
+from django.conf.urls.defaults import *
+from views import empty_view
+
+urlpatterns = patterns('',
+ url(r'^$', empty_view, name="named-url5"),
+ url(r'^extra/(?P<extra>\w+)/$', empty_view, name="named-url6"),
+ url(r'^(?P<one>\d+)|(?P<two>\d+)/$', empty_view),
+)
+
diff --git a/tests/regressiontests/urlpatterns_reverse/included_namespace_urls.py b/tests/regressiontests/urlpatterns_reverse/included_namespace_urls.py
index 073190657c..16887e2a9b 100644
--- a/tests/regressiontests/urlpatterns_reverse/included_namespace_urls.py
+++ b/tests/regressiontests/urlpatterns_reverse/included_namespace_urls.py
@@ -1,5 +1,6 @@
from django.conf.urls.defaults import *
from namespace_urls import URLObject
+from views import view_class_instance
testobj3 = URLObject('testapp', 'test-ns3')
@@ -7,7 +8,13 @@ urlpatterns = patterns('regressiontests.urlpatterns_reverse.views',
url(r'^normal/$', 'empty_view', name='inc-normal-view'),
url(r'^normal/(?P<arg1>\d+)/(?P<arg2>\d+)/$', 'empty_view', name='inc-normal-view'),
+ url(r'^mixed_args/(\d+)/(?P<arg2>\d+)/$', 'empty_view', name='inc-mixed-args'),
+ url(r'^no_kwargs/(\d+)/(\d+)/$', 'empty_view', name='inc-no-kwargs'),
+
+ url(r'^view_class/(?P<arg1>\d+)/(?P<arg2>\d+)/$', view_class_instance, name='inc-view-class'),
+
(r'^test3/', include(testobj3.urls)),
(r'^ns-included3/', include('regressiontests.urlpatterns_reverse.included_urls', namespace='inc-ns3')),
+ (r'^ns-included4/', include('regressiontests.urlpatterns_reverse.namespace_urls', namespace='inc-ns4')),
)
diff --git a/tests/regressiontests/urlpatterns_reverse/named_urls.py b/tests/regressiontests/urlpatterns_reverse/named_urls.py
new file mode 100644
index 0000000000..d8a61a106c
--- /dev/null
+++ b/tests/regressiontests/urlpatterns_reverse/named_urls.py
@@ -0,0 +1,9 @@
+from django.conf.urls.defaults import *
+from views import empty_view
+
+urlpatterns = patterns('',
+ url(r'^$', empty_view, name="named-url1"),
+ url(r'^extra/(?P<extra>\w+)/$', empty_view, name="named-url2"),
+ url(r'^(?P<one>\d+)|(?P<two>\d+)/$', empty_view),
+ (r'^included/', include('regressiontests.urlpatterns_reverse.included_named_urls')),
+)
diff --git a/tests/regressiontests/urlpatterns_reverse/namespace_urls.py b/tests/regressiontests/urlpatterns_reverse/namespace_urls.py
index 27cc7f7a22..3d34049932 100644
--- a/tests/regressiontests/urlpatterns_reverse/namespace_urls.py
+++ b/tests/regressiontests/urlpatterns_reverse/namespace_urls.py
@@ -1,4 +1,5 @@
from django.conf.urls.defaults import *
+from views import view_class_instance
class URLObject(object):
def __init__(self, app_name, namespace):
@@ -23,6 +24,14 @@ urlpatterns = patterns('regressiontests.urlpatterns_reverse.views',
url(r'^normal/$', 'empty_view', name='normal-view'),
url(r'^normal/(?P<arg1>\d+)/(?P<arg2>\d+)/$', 'empty_view', name='normal-view'),
+ url(r'^mixed_args/(\d+)/(?P<arg2>\d+)/$', 'empty_view', name='mixed-args'),
+ url(r'^no_kwargs/(\d+)/(\d+)/$', 'empty_view', name='no-kwargs'),
+
+ url(r'^view_class/(?P<arg1>\d+)/(?P<arg2>\d+)/$', view_class_instance, name='view-class'),
+
+ (r'^unnamed/normal/(?P<arg1>\d+)/(?P<arg2>\d+)/$', 'empty_view'),
+ (r'^unnamed/view_class/(?P<arg1>\d+)/(?P<arg2>\d+)/$', view_class_instance),
+
(r'^test1/', include(testobj1.urls)),
(r'^test2/', include(testobj2.urls)),
(r'^default/', include(default_testobj.urls)),
diff --git a/tests/regressiontests/urlpatterns_reverse/tests.py b/tests/regressiontests/urlpatterns_reverse/tests.py
index 3fcc935da2..a0b98a88d3 100644
--- a/tests/regressiontests/urlpatterns_reverse/tests.py
+++ b/tests/regressiontests/urlpatterns_reverse/tests.py
@@ -18,7 +18,9 @@ import unittest
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
-from django.core.urlresolvers import reverse, resolve, NoReverseMatch, Resolver404
+from django.core.urlresolvers import reverse, resolve, NoReverseMatch,\
+ Resolver404, ResolverMatch,\
+ RegexURLResolver, RegexURLPattern
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
from django.shortcuts import redirect
from django.test import TestCase
@@ -26,6 +28,41 @@ from django.test import TestCase
import urlconf_outer
import urlconf_inner
import middleware
+import views
+
+resolve_test_data = (
+ # These entries are in the format: (path, url_name, app_name, namespace, view_func, args, kwargs)
+ # Simple case
+ ('/normal/42/37/', 'normal-view', None, '', views.empty_view, tuple(), {'arg1': '42', 'arg2': '37'}),
+ ('/view_class/42/37/', 'view-class', None, '', views.view_class_instance, tuple(), {'arg1': '42', 'arg2': '37'}),
+ ('/included/normal/42/37/', 'inc-normal-view', None, '', views.empty_view, tuple(), {'arg1': '42', 'arg2': '37'}),
+ ('/included/view_class/42/37/', 'inc-view-class', None, '', views.view_class_instance, tuple(), {'arg1': '42', 'arg2': '37'}),
+
+ # Unnamed args are dropped if you have *any* kwargs in a pattern
+ ('/mixed_args/42/37/', 'mixed-args', None, '', views.empty_view, tuple(), {'arg2': '37'}),
+ ('/included/mixed_args/42/37/', 'inc-mixed-args', None, '', views.empty_view, tuple(), {'arg2': '37'}),
+
+ # Unnamed views will be resolved to the function/class name
+ ('/unnamed/normal/42/37/', 'regressiontests.urlpatterns_reverse.views.empty_view', None, '', views.empty_view, tuple(), {'arg1': '42', 'arg2': '37'}),
+ ('/unnamed/view_class/42/37/', 'regressiontests.urlpatterns_reverse.views.ViewClass', None, '', views.view_class_instance, tuple(), {'arg1': '42', 'arg2': '37'}),
+
+ # If you have no kwargs, you get an args list.
+ ('/no_kwargs/42/37/', 'no-kwargs', None, '', views.empty_view, ('42','37'), {}),
+ ('/included/no_kwargs/42/37/', 'inc-no-kwargs', None, '', views.empty_view, ('42','37'), {}),
+
+ # Namespaces
+ ('/test1/inner/42/37/', 'urlobject-view', 'testapp', 'test-ns1', 'empty_view', tuple(), {'arg1': '42', 'arg2': '37'}),
+ ('/included/test3/inner/42/37/', 'urlobject-view', 'testapp', 'test-ns3', 'empty_view', tuple(), {'arg1': '42', 'arg2': '37'}),
+ ('/ns-included1/normal/42/37/', 'inc-normal-view', None, 'inc-ns1', views.empty_view, tuple(), {'arg1': '42', 'arg2': '37'}),
+ ('/included/test3/inner/42/37/', 'urlobject-view', 'testapp', 'test-ns3', 'empty_view', tuple(), {'arg1': '42', 'arg2': '37'}),
+ ('/default/inner/42/37/', 'urlobject-view', 'testapp', 'testapp', 'empty_view', tuple(), {'arg1': '42', 'arg2': '37'}),
+ ('/other2/inner/42/37/', 'urlobject-view', 'nodefault', 'other-ns2', 'empty_view', tuple(), {'arg1': '42', 'arg2': '37'}),
+ ('/other1/inner/42/37/', 'urlobject-view', 'nodefault', 'other-ns1', 'empty_view', tuple(), {'arg1': '42', 'arg2': '37'}),
+
+ # Nested namespaces
+ ('/ns-included1/test3/inner/42/37/', 'urlobject-view', 'testapp', 'inc-ns1:test-ns3', 'empty_view', tuple(), {'arg1': '42', 'arg2': '37'}),
+ ('/ns-included1/ns-included4/ns-included2/test3/inner/42/37/', 'urlobject-view', 'testapp', 'inc-ns1:inc-ns4:inc-ns2:test-ns3', 'empty_view', tuple(), {'arg1': '42', 'arg2': '37'}),
+)
test_data = (
('places', '/places/3/', [3], {}),
@@ -119,6 +156,10 @@ class URLPatternReverse(TestCase):
else:
self.assertEquals(got, expected)
+ def test_reverse_none(self):
+ # Reversing None should raise an error, not return the last un-named view.
+ self.assertRaises(NoReverseMatch, reverse, None)
+
class ResolverTests(unittest.TestCase):
def test_non_regex(self):
"""
@@ -133,6 +174,42 @@ class ResolverTests(unittest.TestCase):
self.assertRaises(Resolver404, resolve, 'a')
self.assertRaises(Resolver404, resolve, '\\')
self.assertRaises(Resolver404, resolve, '.')
+
+ def test_404_tried_urls_have_names(self):
+ """
+ Verifies that the list of URLs that come back from a Resolver404
+ exception contains a list in the right format for printing out in
+ the DEBUG 404 page with both the patterns and URL names, if available.
+ """
+ urls = 'regressiontests.urlpatterns_reverse.named_urls'
+ # this list matches the expected URL types and names returned when
+ # you try to resolve a non-existent URL in the first level of included
+ # URLs in named_urls.py (e.g., '/included/non-existent-url')
+ url_types_names = [
+ [{'type': RegexURLPattern, 'name': 'named-url1'}],
+ [{'type': RegexURLPattern, 'name': 'named-url2'}],
+ [{'type': RegexURLPattern, 'name': None}],
+ [{'type': RegexURLResolver}, {'type': RegexURLPattern, 'name': 'named-url3'}],
+ [{'type': RegexURLResolver}, {'type': RegexURLPattern, 'name': 'named-url4'}],
+ [{'type': RegexURLResolver}, {'type': RegexURLPattern, 'name': None}],
+ [{'type': RegexURLResolver}, {'type': RegexURLResolver}],
+ ]
+ try:
+ resolve('/included/non-existent-url', urlconf=urls)
+ self.fail('resolve did not raise a 404')
+ except Resolver404, e:
+ # make sure we at least matched the root ('/') url resolver:
+ self.assertTrue('tried' in e.args[0])
+ tried = e.args[0]['tried']
+ self.assertEqual(len(e.args[0]['tried']), len(url_types_names), 'Wrong number of tried URLs returned. Expected %s, got %s.' % (len(url_types_names), len(e.args[0]['tried'])))
+ for tried, expected in zip(e.args[0]['tried'], url_types_names):
+ for t, e in zip(tried, expected):
+ self.assertTrue(isinstance(t, e['type']), '%s is not an instance of %s' % (t, e['type']))
+ if 'name' in e:
+ if not e['name']:
+ self.assertTrue(t.name is None, 'Expected no URL name but found %s.' % t.name)
+ else:
+ self.assertEqual(t.name, e['name'], 'Wrong URL name. Expected "%s", got "%s".' % (e['name'], t.name))
class ReverseShortcutTests(TestCase):
urls = 'regressiontests.urlpatterns_reverse.urls'
@@ -229,6 +306,12 @@ class NamespaceTests(TestCase):
self.assertEquals('/ns-included1/test3/inner/37/42/', reverse('inc-ns1:test-ns3:urlobject-view', args=[37,42]))
self.assertEquals('/ns-included1/test3/inner/42/37/', reverse('inc-ns1:test-ns3:urlobject-view', kwargs={'arg1':42, 'arg2':37}))
+ def test_nested_namespace_pattern(self):
+ "Namespaces can be nested"
+ self.assertEquals('/ns-included1/ns-included4/ns-included1/test3/inner/', reverse('inc-ns1:inc-ns4:inc-ns1:test-ns3:urlobject-view'))
+ self.assertEquals('/ns-included1/ns-included4/ns-included1/test3/inner/37/42/', reverse('inc-ns1:inc-ns4:inc-ns1:test-ns3:urlobject-view', args=[37,42]))
+ self.assertEquals('/ns-included1/ns-included4/ns-included1/test3/inner/42/37/', reverse('inc-ns1:inc-ns4:inc-ns1:test-ns3:urlobject-view', kwargs={'arg1':42, 'arg2':37}))
+
def test_app_lookup_object(self):
"A default application namespace can be used for lookup"
self.assertEquals('/default/inner/', reverse('testapp:urlobject-view'))
@@ -311,9 +394,51 @@ class ErrorHandlerResolutionTests(TestCase):
self.assertEqual(self.callable_resolver.resolve404(), handler)
self.assertEqual(self.callable_resolver.resolve500(), handler)
+class DefaultErrorHandlerTests(TestCase):
+ urls = 'regressiontests.urlpatterns_reverse.urls_without_full_import'
+
+ def test_default_handler(self):
+ "If the urls.py doesn't specify handlers, the defaults are used"
+ try:
+ response = self.client.get('/test/')
+ self.assertEquals(response.status_code, 404)
+ except AttributeError:
+ self.fail("Shouldn't get an AttributeError due to undefined 404 handler")
+
+ try:
+ self.assertRaises(ValueError, self.client.get, '/bad_view/')
+ except AttributeError:
+ self.fail("Shouldn't get an AttributeError due to undefined 500 handler")
+
class NoRootUrlConfTests(TestCase):
"""Tests for handler404 and handler500 if urlconf is None"""
urls = None
def test_no_handler_exception(self):
self.assertRaises(ImproperlyConfigured, self.client.get, '/test/me/')
+
+class ResolverMatchTests(TestCase):
+ urls = 'regressiontests.urlpatterns_reverse.namespace_urls'
+
+ def test_urlpattern_resolve(self):
+ for path, name, app_name, namespace, func, args, kwargs in resolve_test_data:
+ # Test legacy support for extracting "function, args, kwargs"
+ match_func, match_args, match_kwargs = resolve(path)
+ self.assertEqual(match_func, func)
+ self.assertEqual(match_args, args)
+ self.assertEqual(match_kwargs, kwargs)
+
+ # Test ResolverMatch capabilities.
+ match = resolve(path)
+ self.assertEqual(match.__class__, ResolverMatch)
+ self.assertEqual(match.url_name, name)
+ self.assertEqual(match.args, args)
+ self.assertEqual(match.kwargs, kwargs)
+ self.assertEqual(match.app_name, app_name)
+ self.assertEqual(match.namespace, namespace)
+ self.assertEqual(match.func, func)
+
+ # ... and for legacy purposes:
+ self.assertEquals(match[0], func)
+ self.assertEquals(match[1], args)
+ self.assertEquals(match[2], kwargs)
diff --git a/tests/regressiontests/urlpatterns_reverse/urls_without_full_import.py b/tests/regressiontests/urlpatterns_reverse/urls_without_full_import.py
new file mode 100644
index 0000000000..75a195ed14
--- /dev/null
+++ b/tests/regressiontests/urlpatterns_reverse/urls_without_full_import.py
@@ -0,0 +1,10 @@
+# A URLs file that doesn't use the default
+# from django.conf.urls.defaults import *
+# import pattern.
+from django.conf.urls.defaults import patterns, url
+from views import empty_view, bad_view
+
+urlpatterns = patterns('',
+ url(r'^test_view/$', empty_view, name="test_view"),
+ url(r'^bad_view/$', bad_view, name="bad_view"),
+)
diff --git a/tests/regressiontests/urlpatterns_reverse/views.py b/tests/regressiontests/urlpatterns_reverse/views.py
index 99c00bde70..fdd742382c 100644
--- a/tests/regressiontests/urlpatterns_reverse/views.py
+++ b/tests/regressiontests/urlpatterns_reverse/views.py
@@ -1,8 +1,19 @@
+from django.http import HttpResponse
+
def empty_view(request, *args, **kwargs):
- pass
+ return HttpResponse('')
def kwargs_view(request, arg1=1, arg2=2):
- pass
+ return HttpResponse('')
def absolute_kwargs_view(request, arg1=1, arg2=2):
- pass
+ return HttpResponse('')
+
+class ViewClass(object):
+ def __call__(self, request, *args, **kwargs):
+ return HttpResponse('')
+
+view_class_instance = ViewClass()
+
+def bad_view(request, *args, **kwargs):
+ raise ValueError("I don't think I'm getting good value for this view")
diff --git a/tests/regressiontests/utils/timesince.py b/tests/regressiontests/utils/timesince.py
index 04878b272a..5a54bf4c8c 100644
--- a/tests/regressiontests/utils/timesince.py
+++ b/tests/regressiontests/utils/timesince.py
@@ -88,11 +88,11 @@ u'0 minutes'
u'0 minutes'
# Timesince should work with both date objects (#9672)
->>> today = datetime.date.today()
->>> timeuntil(today+oneday, today)
-u'1 day'
->>> timeuntil(today-oneday, today)
-u'0 minutes'
->>> timeuntil(today+oneweek, today)
-u'1 week'
+>>> today = datetime.date.today()
+>>> timeuntil(today+oneday, today)
+u'1 day'
+>>> timeuntil(today-oneday, today)
+u'0 minutes'
+>>> timeuntil(today+oneweek, today)
+u'1 week'
"""
diff --git a/tests/regressiontests/views/tests/__init__.py b/tests/regressiontests/views/tests/__init__.py
index 697968ee52..edd533e175 100644
--- a/tests/regressiontests/views/tests/__init__.py
+++ b/tests/regressiontests/views/tests/__init__.py
@@ -2,6 +2,7 @@ from debug import *
from defaults import *
from generic.create_update import *
from generic.date_based import *
+from generic.simple import *
from i18n import *
from specials import *
from static import *
diff --git a/tests/regressiontests/views/tests/generic/simple.py b/tests/regressiontests/views/tests/generic/simple.py
new file mode 100644
index 0000000000..f94b3da439
--- /dev/null
+++ b/tests/regressiontests/views/tests/generic/simple.py
@@ -0,0 +1,38 @@
+# coding: utf-8
+
+from django.test import TestCase
+
+class RedirectToTest(TestCase):
+ def test_redirect_to_returns_permanent_redirect(self):
+ "simple.redirect_to returns a permanent redirect (301) by default"
+ response = self.client.get('/views/simple/redirect_to/')
+ self.assertEqual(response.status_code, 301)
+ self.assertEqual('http://testserver/views/simple/target/', response['Location'])
+
+ def test_redirect_to_can_return_a_temporary_redirect(self):
+ "simple.redirect_to returns a temporary redirect (302) when explicitely asked to"
+ response = self.client.get('/views/simple/redirect_to_temp/')
+ self.assertEqual(response.status_code, 302)
+ self.assertEqual('http://testserver/views/simple/target/', response['Location'])
+
+ def test_redirect_to_on_empty_url_returns_gone(self):
+ "simple.redirect_to returns resource gone (410) when given a None url"
+ response = self.client.get('/views/simple/redirect_to_none/')
+ self.assertEqual(response.status_code, 410)
+
+ def test_redirect_to_allows_formatted_url_string(self):
+ "simple.redirect_to uses string interpolation on target url for keyword args"
+ response = self.client.get('/views/simple/redirect_to_arg/42/')
+ self.assertEqual(response.status_code, 301)
+ self.assertEqual('http://testserver/views/simple/target_arg/42/', response['Location'])
+
+ def test_redirect_to_allows_query_string_to_be_passed(self):
+ "simple.redirect_to configured with query_string=True passes on any query string"
+ # the default is to not forward the query string
+ response = self.client.get('/views/simple/redirect_to/?param1=foo&param2=bar')
+ self.assertEqual(response.status_code, 301)
+ self.assertEqual('http://testserver/views/simple/target/', response['Location'])
+ # views configured with query_string=True however passes the query string along
+ response = self.client.get('/views/simple/redirect_to_query/?param1=foo&param2=bar')
+ self.assertEqual(response.status_code, 301)
+ self.assertEqual('http://testserver/views/simple/target/?param1=foo&param2=bar', response['Location'])
diff --git a/tests/regressiontests/views/urls.py b/tests/regressiontests/views/urls.py
index f5675d0e28..b42700baeb 100644
--- a/tests/regressiontests/views/urls.py
+++ b/tests/regressiontests/views/urls.py
@@ -77,7 +77,6 @@ urlpatterns += patterns('django.views.generic.date_based',
)
# crud generic views.
-
urlpatterns += patterns('django.views.generic.create_update',
(r'^create_update/member/create/article/$', 'create_object',
dict(login_required=True, model=Article)),
@@ -123,3 +122,12 @@ urlpatterns += patterns('regressiontests.views.views',
url(r'view_exception/(?P<n>\d+)/$', 'view_exception', name='view_exception'),
url(r'template_exception/(?P<n>\d+)/$', 'template_exception', name='template_exception'),
)
+
+# simple generic views.
+urlpatterns += patterns('django.views.generic.simple',
+ (r'^simple/redirect_to/$', 'redirect_to', dict(url='/views/simple/target/')),
+ (r'^simple/redirect_to_temp/$', 'redirect_to', dict(url='/views/simple/target/', permanent=False)),
+ (r'^simple/redirect_to_none/$', 'redirect_to', dict(url=None)),
+ (r'^simple/redirect_to_arg/(?P<id>\d+)/$', 'redirect_to', dict(url='/views/simple/target_arg/%(id)s/')),
+ (r'^simple/redirect_to_query/$', 'redirect_to', dict(url='/views/simple/target/', query_string=True)),
+)
diff --git a/tests/runtests.py b/tests/runtests.py
index 77bc1481d1..e733569212 100755
--- a/tests/runtests.py
+++ b/tests/runtests.py
@@ -27,6 +27,7 @@ ALWAYS_INSTALLED_APPS = [
'django.contrib.messages',
'django.contrib.comments',
'django.contrib.admin',
+ 'django.contrib.admindocs',
]
def get_test_models():
@@ -119,22 +120,19 @@ def django_tests(verbosity, interactive, failfast, test_labels):
from django.db.models.loading import load_app
# Load all the test model apps.
+ test_labels_set = set([label.split('.')[0] for label in test_labels])
for model_dir, model_name in get_test_models():
model_label = '.'.join([model_dir, model_name])
- try:
- # if the model was named on the command line, or
- # no models were named (i.e., run all), import
- # this model and add it to the list to test.
- if not test_labels or model_name in set([label.split('.')[0] for label in test_labels]):
- if verbosity >= 1:
- print "Importing model %s" % model_name
- mod = load_app(model_label)
- if mod:
- if model_label not in settings.INSTALLED_APPS:
- settings.INSTALLED_APPS.append(model_label)
- except Exception, e:
- sys.stderr.write("Error while importing %s:" % model_name + ''.join(traceback.format_exception(*sys.exc_info())[1:]))
- continue
+ # if the model was named on the command line, or
+ # no models were named (i.e., run all), import
+ # this model and add it to the list to test.
+ if not test_labels or model_name in test_labels_set:
+ if verbosity >= 2:
+ print "Importing model %s" % model_name
+ mod = load_app(model_label)
+ if mod:
+ if model_label not in settings.INSTALLED_APPS:
+ settings.INSTALLED_APPS.append(model_label)
# Add tests for invalid models.
extra_tests = []
@@ -185,8 +183,8 @@ if __name__ == "__main__":
from optparse import OptionParser
usage = "%prog [options] [model model model ...]"
parser = OptionParser(usage=usage)
- parser.add_option('-v','--verbosity', action='store', dest='verbosity', default='0',
- type='choice', choices=['0', '1', '2'],
+ parser.add_option('-v','--verbosity', action='store', dest='verbosity', default='1',
+ type='choice', choices=['0', '1', '2', '3'],
help='Verbosity level; 0=minimal output, 1=normal output, 2=all output')
parser.add_option('--noinput', action='store_false', dest='interactive', default=True,
help='Tells Django to NOT prompt the user for input of any kind.')