mirror of https://github.com/django/django.git
Converted the model_regress doctests into unittests. We have always been at war with doctests.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14614 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
008f333bac
commit
3ede430b9a
|
@ -1,9 +1,6 @@
|
||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
import datetime
|
from django.db import models
|
||||||
|
|
||||||
from django.conf import settings
|
|
||||||
from django.db import models, DEFAULT_DB_ALIAS, connection
|
|
||||||
from django.utils import tzinfo
|
|
||||||
|
|
||||||
CHOICES = (
|
CHOICES = (
|
||||||
(1, 'first'),
|
(1, 'first'),
|
||||||
|
@ -60,112 +57,3 @@ class BrokenUnicodeMethod(models.Model):
|
||||||
|
|
||||||
class NonAutoPK(models.Model):
|
class NonAutoPK(models.Model):
|
||||||
name = models.CharField(max_length=10, primary_key=True)
|
name = models.CharField(max_length=10, primary_key=True)
|
||||||
|
|
||||||
__test__ = {'API_TESTS': """
|
|
||||||
(NOTE: Part of the regression test here is merely parsing the model
|
|
||||||
declaration. The verbose_name, in particular, did not always work.)
|
|
||||||
|
|
||||||
An empty choice field should return None for the display name.
|
|
||||||
|
|
||||||
>>> from datetime import datetime
|
|
||||||
>>> a = Article(headline="Look at me!", pub_date=datetime.now())
|
|
||||||
>>> a.save()
|
|
||||||
>>> a.get_status_display() is None
|
|
||||||
True
|
|
||||||
|
|
||||||
Empty strings should be returned as Unicode
|
|
||||||
>>> a2 = Article.objects.get(pk=a.id)
|
|
||||||
>>> a2.misc_data
|
|
||||||
u''
|
|
||||||
|
|
||||||
# TextFields can hold more than 4000 characters (this was broken in Oracle).
|
|
||||||
>>> a3 = Article(headline="Really, really big", pub_date=datetime.now())
|
|
||||||
>>> a3.article_text = "ABCDE" * 1000
|
|
||||||
>>> a3.save()
|
|
||||||
>>> a4 = Article.objects.get(pk=a3.id)
|
|
||||||
>>> len(a4.article_text)
|
|
||||||
5000
|
|
||||||
|
|
||||||
# Regression test for #659
|
|
||||||
>>> import datetime
|
|
||||||
>>> p = Party.objects.create(when = datetime.datetime(1999, 12, 31))
|
|
||||||
>>> p = Party.objects.create(when = datetime.datetime(1998, 12, 31))
|
|
||||||
>>> p = Party.objects.create(when = datetime.datetime(1999, 1, 1))
|
|
||||||
>>> [p.when for p in Party.objects.filter(when__month=2)]
|
|
||||||
[]
|
|
||||||
>>> [p.when for p in Party.objects.filter(when__month=1)]
|
|
||||||
[datetime.date(1999, 1, 1)]
|
|
||||||
>>> [p.when for p in Party.objects.filter(when__month=12)]
|
|
||||||
[datetime.date(1999, 12, 31), datetime.date(1998, 12, 31)]
|
|
||||||
>>> [p.when for p in Party.objects.filter(when__year=1998)]
|
|
||||||
[datetime.date(1998, 12, 31)]
|
|
||||||
|
|
||||||
# Regression test for #8510
|
|
||||||
>>> [p.when for p in Party.objects.filter(when__day='31')]
|
|
||||||
[datetime.date(1999, 12, 31), datetime.date(1998, 12, 31)]
|
|
||||||
>>> [p.when for p in Party.objects.filter(when__month='12')]
|
|
||||||
[datetime.date(1999, 12, 31), datetime.date(1998, 12, 31)]
|
|
||||||
>>> [p.when for p in Party.objects.filter(when__year='1998')]
|
|
||||||
[datetime.date(1998, 12, 31)]
|
|
||||||
|
|
||||||
# Date filtering was failing with NULL date values in SQLite (regression test
|
|
||||||
# for #3501, amongst other things).
|
|
||||||
>>> _ = Party.objects.create()
|
|
||||||
>>> p = Party.objects.filter(when__month=1)[0]
|
|
||||||
>>> p.when
|
|
||||||
datetime.date(1999, 1, 1)
|
|
||||||
>>> l = Party.objects.filter(pk=p.pk).dates("when", "month")
|
|
||||||
>>> l[0].month == 1
|
|
||||||
True
|
|
||||||
|
|
||||||
# Check that get_next_by_FIELD and get_previous_by_FIELD don't crash when we
|
|
||||||
# have usecs values stored on the database
|
|
||||||
#
|
|
||||||
# [It crashed after the Field.get_db_prep_* refactor, because on most backends
|
|
||||||
# DateTimeFields supports usecs, but DateTimeField.to_python didn't recognize
|
|
||||||
# them. (Note that Model._get_next_or_previous_by_FIELD coerces values to
|
|
||||||
# strings)]
|
|
||||||
#
|
|
||||||
>>> e = Event.objects.create(when = datetime.datetime(2000, 1, 1, 16, 0, 0))
|
|
||||||
>>> e = Event.objects.create(when = datetime.datetime(2000, 1, 1, 6, 1, 1))
|
|
||||||
>>> e = Event.objects.create(when = datetime.datetime(2000, 1, 1, 13, 1, 1))
|
|
||||||
>>> e = Event.objects.create(when = datetime.datetime(2000, 1, 1, 12, 0, 20, 24))
|
|
||||||
>>> e.get_next_by_when().when
|
|
||||||
datetime.datetime(2000, 1, 1, 13, 1, 1)
|
|
||||||
>>> e.get_previous_by_when().when
|
|
||||||
datetime.datetime(2000, 1, 1, 6, 1, 1)
|
|
||||||
|
|
||||||
# Check Department and Worker
|
|
||||||
>>> d = Department(id=10, name='IT')
|
|
||||||
>>> d.save()
|
|
||||||
>>> w = Worker(department=d, name='Full-time')
|
|
||||||
>>> w.save()
|
|
||||||
>>> w
|
|
||||||
<Worker: Full-time>
|
|
||||||
|
|
||||||
# Models with broken unicode methods should still have a printable repr
|
|
||||||
>>> b = BrokenUnicodeMethod(name="Jerry")
|
|
||||||
>>> b.save()
|
|
||||||
>>> BrokenUnicodeMethod.objects.all()
|
|
||||||
[<BrokenUnicodeMethod: [Bad Unicode data]>]
|
|
||||||
|
|
||||||
"""}
|
|
||||||
|
|
||||||
if connection.features.supports_timezones:
|
|
||||||
__test__["timezone-tests"] = """
|
|
||||||
# Saving an updating with timezone-aware datetime Python objects. Regression
|
|
||||||
# test for #10443.
|
|
||||||
|
|
||||||
# The idea is that all these creations and saving should work without crashing.
|
|
||||||
# It's not rocket science.
|
|
||||||
>>> Article.objects.all().delete()
|
|
||||||
>>> dt1 = datetime.datetime(2008, 8, 31, 16, 20, tzinfo=tzinfo.FixedOffset(600))
|
|
||||||
>>> dt2 = datetime.datetime(2008, 8, 31, 17, 20, tzinfo=tzinfo.FixedOffset(600))
|
|
||||||
>>> obj = Article.objects.create(headline="A headline", pub_date=dt1, article_text="foo")
|
|
||||||
|
|
||||||
>>> obj.pub_date = dt2
|
|
||||||
>>> obj.save()
|
|
||||||
>>> Article.objects.filter(headline="A headline").update(pub_date=dt1)
|
|
||||||
1
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
|
@ -1,23 +1,166 @@
|
||||||
from django.core.exceptions import ValidationError
|
import datetime
|
||||||
from django.test import TestCase
|
from operator import attrgetter
|
||||||
|
|
||||||
from models import Worker, NonAutoPK
|
from django.test import TestCase, skipUnlessDBFeature
|
||||||
|
from django.utils import tzinfo
|
||||||
|
|
||||||
|
from models import (Worker, Article, Party, Event, Department,
|
||||||
|
BrokenUnicodeMethod, NonAutoPK)
|
||||||
|
|
||||||
|
|
||||||
class RelatedModelOrderedLookupTest(TestCase):
|
|
||||||
"""
|
|
||||||
Regression test for #10153: foreign key __gte and __lte lookups.
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
class ModelTests(TestCase):
|
||||||
# The bug is that the following queries would raise:
|
# The bug is that the following queries would raise:
|
||||||
# "TypeError: Related Field has invalid lookup: gte"
|
# "TypeError: Related Field has invalid lookup: gte"
|
||||||
|
|
||||||
def test_related_gte_lookup(self):
|
def test_related_gte_lookup(self):
|
||||||
|
"""
|
||||||
|
Regression test for #10153: foreign key __gte lookups.
|
||||||
|
"""
|
||||||
Worker.objects.filter(department__gte=0)
|
Worker.objects.filter(department__gte=0)
|
||||||
|
|
||||||
def test_related_lte_lookup(self):
|
def test_related_lte_lookup(self):
|
||||||
|
"""
|
||||||
|
Regression test for #10153: foreign key __lte lookups.
|
||||||
|
"""
|
||||||
Worker.objects.filter(department__lte=0)
|
Worker.objects.filter(department__lte=0)
|
||||||
|
|
||||||
|
def test_empty_choice(self):
|
||||||
|
# NOTE: Part of the regression test here is merely parsing the model
|
||||||
|
# declaration. The verbose_name, in particular, did not always work.
|
||||||
|
a = Article.objects.create(
|
||||||
|
headline="Look at me!", pub_date=datetime.datetime.now()
|
||||||
|
)
|
||||||
|
# An empty choice field should return None for the display name.
|
||||||
|
self.assertIs(a.get_status_display(), None)
|
||||||
|
|
||||||
|
# Empty strings should be returned as Unicode
|
||||||
|
a = Article.objects.get(pk=a.pk)
|
||||||
|
self.assertEqual(a.misc_data, u'')
|
||||||
|
self.assertIs(type(a.misc_data), unicode)
|
||||||
|
|
||||||
|
def test_long_textfield(self):
|
||||||
|
# TextFields can hold more than 4000 characters (this was broken in
|
||||||
|
# Oracle).
|
||||||
|
a = Article.objects.create(
|
||||||
|
headline="Really, really big",
|
||||||
|
pub_date=datetime.datetime.now(),
|
||||||
|
article_text = "ABCDE" * 1000
|
||||||
|
)
|
||||||
|
a = Article.objects.get(pk=a.pk)
|
||||||
|
self.assertEqual
|
||||||
|
(len(a.article_text), 5000)
|
||||||
|
|
||||||
|
def test_date_lookup(self):
|
||||||
|
# Regression test for #659
|
||||||
|
Party.objects.create(when=datetime.datetime(1999, 12, 31))
|
||||||
|
Party.objects.create(when=datetime.datetime(1998, 12, 31))
|
||||||
|
Party.objects.create(when=datetime.datetime(1999, 1, 1))
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
Party.objects.filter(when__month=2), []
|
||||||
|
)
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
Party.objects.filter(when__month=1), [
|
||||||
|
datetime.date(1999, 1, 1)
|
||||||
|
],
|
||||||
|
attrgetter("when")
|
||||||
|
)
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
Party.objects.filter(when__month=12), [
|
||||||
|
datetime.date(1999, 12, 31),
|
||||||
|
datetime.date(1998, 12, 31),
|
||||||
|
],
|
||||||
|
attrgetter("when")
|
||||||
|
)
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
Party.objects.filter(when__year=1998), [
|
||||||
|
datetime.date(1998, 12, 31),
|
||||||
|
],
|
||||||
|
attrgetter("when")
|
||||||
|
)
|
||||||
|
# Regression test for #8510
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
Party.objects.filter(when__day="31"), [
|
||||||
|
datetime.date(1999, 12, 31),
|
||||||
|
datetime.date(1998, 12, 31),
|
||||||
|
],
|
||||||
|
attrgetter("when")
|
||||||
|
)
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
Party.objects.filter(when__month="12"), [
|
||||||
|
datetime.date(1999, 12, 31),
|
||||||
|
datetime.date(1998, 12, 31),
|
||||||
|
],
|
||||||
|
attrgetter("when")
|
||||||
|
)
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
Party.objects.filter(when__year="1998"), [
|
||||||
|
datetime.date(1998, 12, 31),
|
||||||
|
],
|
||||||
|
attrgetter("when")
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_date_filter_null(self):
|
||||||
|
# Date filtering was failing with NULL date values in SQLite
|
||||||
|
# (regression test for #3501, amongst other things).
|
||||||
|
Party.objects.create(when=datetime.datetime(1999, 1, 1))
|
||||||
|
Party.objects.create()
|
||||||
|
p = Party.objects.filter(when__month=1)[0]
|
||||||
|
self.assertEqual(p.when, datetime.date(1999, 1, 1))
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
Party.objects.filter(pk=p.pk).dates("when", "month"), [
|
||||||
|
1
|
||||||
|
],
|
||||||
|
attrgetter("month")
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_get_next_prev_by_field(self):
|
||||||
|
# Check that get_next_by_FIELD and get_previous_by_FIELD don't crash
|
||||||
|
# when we have usecs values stored on the database
|
||||||
|
#
|
||||||
|
# It crashed after the Field.get_db_prep_* refactor, because on most
|
||||||
|
# backends DateTimeFields supports usecs, but DateTimeField.to_python
|
||||||
|
# didn't recognize them. (Note that
|
||||||
|
# Model._get_next_or_previous_by_FIELD coerces values to strings)
|
||||||
|
Event.objects.create(when=datetime.datetime(2000, 1, 1, 16, 0, 0))
|
||||||
|
Event.objects.create(when=datetime.datetime(2000, 1, 1, 6, 1, 1))
|
||||||
|
Event.objects.create(when=datetime.datetime(2000, 1, 1, 13, 1, 1))
|
||||||
|
e = Event.objects.create(when=datetime.datetime(2000, 1, 1, 12, 0, 20, 24))
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
e.get_next_by_when().when, datetime.datetime(2000, 1, 1, 13, 1, 1)
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
e.get_previous_by_when().when, datetime.datetime(2000, 1, 1, 6, 1, 1)
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_primary_key_foreign_key_types(self):
|
||||||
|
# Check Department and Worker (non-default PK type)
|
||||||
|
d = Department.objects.create(id=10, name="IT")
|
||||||
|
w = Worker.objects.create(department=d, name="Full-time")
|
||||||
|
self.assertEqual(unicode(w), "Full-time")
|
||||||
|
|
||||||
|
def test_broken_unicode(self):
|
||||||
|
# Models with broken unicode methods should still have a printable repr
|
||||||
|
b = BrokenUnicodeMethod.objects.create(name="Jerry")
|
||||||
|
self.assertEqual(repr(b), "<BrokenUnicodeMethod: [Bad Unicode data]>")
|
||||||
|
|
||||||
|
@skipUnlessDBFeature("supports_timezones")
|
||||||
|
def test_timezones(self):
|
||||||
|
# Saving an updating with timezone-aware datetime Python objects.
|
||||||
|
# Regression test for #10443.
|
||||||
|
# The idea is that all these creations and saving should work without
|
||||||
|
# crashing. It's not rocket science.
|
||||||
|
dt1 = datetime.datetime(2008, 8, 31, 16, 20, tzinfo=tzinfo.FixedOffset(600))
|
||||||
|
dt2 = datetime.datetime(2008, 8, 31, 17, 20, tzinfo=tzinfo.FixedOffset(600))
|
||||||
|
obj = Article.objects.create(
|
||||||
|
headline="A headline", pub_date=dt1, article_text="foo"
|
||||||
|
)
|
||||||
|
obj.pub_date = dt2
|
||||||
|
obj.save()
|
||||||
|
self.assertEqual(
|
||||||
|
Article.objects.filter(headline="A headline").update(pub_date=dt1),
|
||||||
|
1
|
||||||
|
)
|
||||||
|
|
||||||
class ModelValidationTest(TestCase):
|
class ModelValidationTest(TestCase):
|
||||||
def test_pk_validation(self):
|
def test_pk_validation(self):
|
||||||
|
|
Loading…
Reference in New Issue