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