PageRenderTime 123ms CodeModel.GetById 60ms app.highlight 20ms RepoModel.GetById 40ms app.codeStats 0ms

/tests/regressiontests/model_regress/tests.py

https://code.google.com/p/mango-py/
Python | 170 lines | 133 code | 13 blank | 24 comment | 0 complexity | 5da5064eb5bfbf54708884856fb3f221 MD5 | raw file
  1import datetime
  2from operator import attrgetter
  3
  4from django.core.exceptions import ValidationError
  5from django.test import TestCase, skipUnlessDBFeature
  6from django.utils import tzinfo
  7
  8from models import (Worker, Article, Party, Event, Department,
  9    BrokenUnicodeMethod, NonAutoPK)
 10
 11
 12
 13class ModelTests(TestCase):
 14    # The bug is that the following queries would raise:
 15    # "TypeError: Related Field has invalid lookup: gte"
 16    def test_related_gte_lookup(self):
 17        """
 18        Regression test for #10153: foreign key __gte lookups.
 19        """
 20        Worker.objects.filter(department__gte=0)
 21
 22    def test_related_lte_lookup(self):
 23        """
 24        Regression test for #10153: foreign key __lte lookups.
 25        """
 26        Worker.objects.filter(department__lte=0)
 27
 28    def test_empty_choice(self):
 29        # NOTE: Part of the regression test here is merely parsing the model
 30        # declaration. The verbose_name, in particular, did not always work.
 31        a = Article.objects.create(
 32            headline="Look at me!", pub_date=datetime.datetime.now()
 33        )
 34        # An empty choice field should return None for the display name.
 35        self.assertIs(a.get_status_display(), None)
 36
 37        # Empty strings should be returned as Unicode
 38        a = Article.objects.get(pk=a.pk)
 39        self.assertEqual(a.misc_data, u'')
 40        self.assertIs(type(a.misc_data), unicode)
 41
 42    def test_long_textfield(self):
 43        # TextFields can hold more than 4000 characters (this was broken in
 44        # Oracle).
 45        a = Article.objects.create(
 46            headline="Really, really big",
 47            pub_date=datetime.datetime.now(),
 48            article_text = "ABCDE" * 1000
 49        )
 50        a = Article.objects.get(pk=a.pk)
 51        self.assertEqual
 52        (len(a.article_text), 5000)
 53
 54    def test_date_lookup(self):
 55        # Regression test for #659
 56        Party.objects.create(when=datetime.datetime(1999, 12, 31))
 57        Party.objects.create(when=datetime.datetime(1998, 12, 31))
 58        Party.objects.create(when=datetime.datetime(1999, 1, 1))
 59        self.assertQuerysetEqual(
 60            Party.objects.filter(when__month=2), []
 61        )
 62        self.assertQuerysetEqual(
 63            Party.objects.filter(when__month=1), [
 64                datetime.date(1999, 1, 1)
 65            ],
 66            attrgetter("when")
 67        )
 68        self.assertQuerysetEqual(
 69            Party.objects.filter(when__month=12), [
 70                datetime.date(1999, 12, 31),
 71                datetime.date(1998, 12, 31),
 72            ],
 73            attrgetter("when")
 74        )
 75        self.assertQuerysetEqual(
 76            Party.objects.filter(when__year=1998), [
 77                datetime.date(1998, 12, 31),
 78            ],
 79            attrgetter("when")
 80        )
 81        # Regression test for #8510
 82        self.assertQuerysetEqual(
 83            Party.objects.filter(when__day="31"), [
 84                datetime.date(1999, 12, 31),
 85                datetime.date(1998, 12, 31),
 86            ],
 87            attrgetter("when")
 88        )
 89        self.assertQuerysetEqual(
 90            Party.objects.filter(when__month="12"), [
 91                datetime.date(1999, 12, 31),
 92                datetime.date(1998, 12, 31),
 93            ],
 94            attrgetter("when")
 95        )
 96        self.assertQuerysetEqual(
 97            Party.objects.filter(when__year="1998"), [
 98                datetime.date(1998, 12, 31),
 99            ],
100            attrgetter("when")
101        )
102
103    def test_date_filter_null(self):
104        # Date filtering was failing with NULL date values in SQLite
105        # (regression test for #3501, amongst other things).
106        Party.objects.create(when=datetime.datetime(1999, 1, 1))
107        Party.objects.create()
108        p = Party.objects.filter(when__month=1)[0]
109        self.assertEqual(p.when, datetime.date(1999, 1, 1))
110        self.assertQuerysetEqual(
111            Party.objects.filter(pk=p.pk).dates("when", "month"), [
112                1
113            ],
114            attrgetter("month")
115        )
116
117    def test_get_next_prev_by_field(self):
118        # Check that get_next_by_FIELD and get_previous_by_FIELD don't crash
119        # when we have usecs values stored on the database
120        #
121        # It crashed after the Field.get_db_prep_* refactor, because on most
122        # backends DateTimeFields supports usecs, but DateTimeField.to_python
123        # didn't recognize them. (Note that
124        # Model._get_next_or_previous_by_FIELD coerces values to strings)
125        Event.objects.create(when=datetime.datetime(2000, 1, 1, 16, 0, 0))
126        Event.objects.create(when=datetime.datetime(2000, 1, 1, 6, 1, 1))
127        Event.objects.create(when=datetime.datetime(2000, 1, 1, 13, 1, 1))
128        e = Event.objects.create(when=datetime.datetime(2000, 1, 1, 12, 0, 20, 24))
129
130        self.assertEqual(
131            e.get_next_by_when().when, datetime.datetime(2000, 1, 1, 13, 1, 1)
132        )
133        self.assertEqual(
134            e.get_previous_by_when().when, datetime.datetime(2000, 1, 1, 6, 1, 1)
135        )
136
137    def test_primary_key_foreign_key_types(self):
138        # Check Department and Worker (non-default PK type)
139        d = Department.objects.create(id=10, name="IT")
140        w = Worker.objects.create(department=d, name="Full-time")
141        self.assertEqual(unicode(w), "Full-time")
142
143    def test_broken_unicode(self):
144        # Models with broken unicode methods should still have a printable repr
145        b = BrokenUnicodeMethod.objects.create(name="Jerry")
146        self.assertEqual(repr(b), "<BrokenUnicodeMethod: [Bad Unicode data]>")
147
148    @skipUnlessDBFeature("supports_timezones")
149    def test_timezones(self):
150        # Saving an updating with timezone-aware datetime Python objects.
151        # Regression test for #10443.
152        # The idea is that all these creations and saving should work without
153        # crashing. It's not rocket science.
154        dt1 = datetime.datetime(2008, 8, 31, 16, 20, tzinfo=tzinfo.FixedOffset(600))
155        dt2 = datetime.datetime(2008, 8, 31, 17, 20, tzinfo=tzinfo.FixedOffset(600))
156        obj = Article.objects.create(
157            headline="A headline", pub_date=dt1, article_text="foo"
158        )
159        obj.pub_date = dt2
160        obj.save()
161        self.assertEqual(
162            Article.objects.filter(headline="A headline").update(pub_date=dt1),
163            1
164        )
165
166class ModelValidationTest(TestCase):
167    def test_pk_validation(self):
168        one = NonAutoPK.objects.create(name="one")
169        again = NonAutoPK(name="one")
170        self.assertRaises(ValidationError, again.validate_unique)