diff options
author | Florian Apolloner <florian@apolloner.eu> | 2013-02-26 09:53:47 +0100 |
---|---|---|
committer | Florian Apolloner <florian@apolloner.eu> | 2013-02-26 14:36:57 +0100 |
commit | 89f40e36246100df6a11316c31a76712ebc6c501 (patch) | |
tree | 6e65639683ddaf2027908d1ecb1739e0e2ff853b /tests/model_regress | |
parent | b3d2ccb5bfbaf6e7fe1f98843baaa48c35a70950 (diff) | |
download | django-89f40e36246100df6a11316c31a76712ebc6c501.tar.gz |
Merged regressiontests and modeltests into the test root.
Diffstat (limited to 'tests/model_regress')
-rw-r--r-- | tests/model_regress/__init__.py | 0 | ||||
-rw-r--r-- | tests/model_regress/models.py | 84 | ||||
-rw-r--r-- | tests/model_regress/tests.py | 217 |
3 files changed, 301 insertions, 0 deletions
diff --git a/tests/model_regress/__init__.py b/tests/model_regress/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/model_regress/__init__.py diff --git a/tests/model_regress/models.py b/tests/model_regress/models.py new file mode 100644 index 0000000000..82efb9c783 --- /dev/null +++ b/tests/model_regress/models.py @@ -0,0 +1,84 @@ +# coding: utf-8 +from django.db import models +from django.utils.encoding import python_2_unicode_compatible + + +CHOICES = ( + (1, 'first'), + (2, 'second'), +) + + +@python_2_unicode_compatible +class Article(models.Model): + headline = models.CharField(max_length=100, default='Default headline') + pub_date = models.DateTimeField() + status = models.IntegerField(blank=True, null=True, choices=CHOICES) + misc_data = models.CharField(max_length=100, blank=True) + article_text = models.TextField() + + class Meta: + ordering = ('pub_date', 'headline') + # A utf-8 verbose name (Ångström's Articles) to test they are valid. + verbose_name = "\xc3\x85ngstr\xc3\xb6m's Articles" + + def __str__(self): + return self.headline + + +class Movie(models.Model): + #5218: Test models with non-default primary keys / AutoFields + movie_id = models.AutoField(primary_key=True) + name = models.CharField(max_length=60) + + +class Party(models.Model): + when = models.DateField(null=True) + + +class Event(models.Model): + when = models.DateTimeField() + + +@python_2_unicode_compatible +class Department(models.Model): + id = models.PositiveIntegerField(primary_key=True) + name = models.CharField(max_length=200) + + def __str__(self): + return self.name + + +@python_2_unicode_compatible +class Worker(models.Model): + department = models.ForeignKey(Department) + name = models.CharField(max_length=200) + + def __str__(self): + return self.name + + +@python_2_unicode_compatible +class BrokenUnicodeMethod(models.Model): + name = models.CharField(max_length=7) + + def __str__(self): + # Intentionally broken (invalid start byte in byte string). + return b'Name\xff: %s'.decode() % self.name + + +class NonAutoPK(models.Model): + name = models.CharField(max_length=10, primary_key=True) + + +#18432: Chained foreign keys with to_field produce incorrect query +class Model1(models.Model): + pkey = models.IntegerField(unique=True, db_index=True) + + +class Model2(models.Model): + model1 = models.ForeignKey(Model1, unique=True, to_field='pkey') + + +class Model3(models.Model): + model2 = models.ForeignKey(Model2, unique=True, to_field='model1') diff --git a/tests/model_regress/tests.py b/tests/model_regress/tests.py new file mode 100644 index 0000000000..e2d30f0ecc --- /dev/null +++ b/tests/model_regress/tests.py @@ -0,0 +1,217 @@ +from __future__ import absolute_import, unicode_literals + +import datetime +from operator import attrgetter + +from django.core.exceptions import ValidationError +from django.test import TestCase, skipUnlessDBFeature +from django.utils import six +from django.utils import tzinfo + +from .models import (Worker, Article, Party, Event, Department, + BrokenUnicodeMethod, NonAutoPK, Model1, Model2, Model3) + + +class ModelTests(TestCase): + # The bug is that the following queries would raise: + # "TypeError: Related Field has invalid lookup: gte" + def test_related_gte_lookup(self): + """ + Regression test for #10153: foreign key __gte lookups. + """ + Worker.objects.filter(department__gte=0) + + def test_related_lte_lookup(self): + """ + Regression test for #10153: foreign key __lte lookups. + """ + 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, '') + self.assertIs(type(a.misc_data), six.text_type) + + 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)) + Party.objects.create(when=datetime.datetime(1, 3, 3)) + 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"), + ordered=False + ) + 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"), + ordered=False + ) + self.assertQuerysetEqual( + Party.objects.filter(when__month="12"), [ + datetime.date(1999, 12, 31), + datetime.date(1998, 12, 31), + ], + attrgetter("when"), + ordered=False + ) + self.assertQuerysetEqual( + Party.objects.filter(when__year="1998"), [ + datetime.date(1998, 12, 31), + ], + attrgetter("when") + ) + + # Regression test for #18969 + self.assertQuerysetEqual( + Party.objects.filter(when__year=1), [ + datetime.date(1, 3, 3), + ], + attrgetter("when") + ) + self.assertQuerysetEqual( + Party.objects.filter(when__year='1'), [ + datetime.date(1, 3, 3), + ], + 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(six.text_type(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 + ) + + def test_chained_fks(self): + """ + Regression for #18432: Chained foreign keys with to_field produce incorrect query + """ + + m1 = Model1.objects.create(pkey=1000) + m2 = Model2.objects.create(model1=m1) + m3 = Model3.objects.create(model2=m2) + + # this is the actual test for #18432 + m3 = Model3.objects.get(model2=1000) + m3.model2 + + +class ModelValidationTest(TestCase): + def test_pk_validation(self): + one = NonAutoPK.objects.create(name="one") + again = NonAutoPK(name="one") + self.assertRaises(ValidationError, again.validate_unique) + + +class EvaluateMethodTest(TestCase): + """ + Regression test for #13640: cannot filter by objects with 'evaluate' attr + """ + + def test_model_with_evaluate_method(self): + """ + Ensures that you can filter by objects that have an 'evaluate' attr + """ + dept = Department.objects.create(pk=1, name='abc') + dept.evaluate = 'abc' + Worker.objects.filter(department=dept) |