PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/modeltests/or_lookups/tests.py

https://code.google.com/p/mango-py/
Python | 232 lines | 191 code | 28 blank | 13 comment | 0 complexity | 3ebe2805e83431a7952800a92ec411d0 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from datetime import datetime
  2. from operator import attrgetter
  3. from django.db.models import Q
  4. from django.test import TestCase
  5. from models import Article
  6. class OrLookupsTests(TestCase):
  7. def setUp(self):
  8. self.a1 = Article.objects.create(
  9. headline='Hello', pub_date=datetime(2005, 11, 27)
  10. ).pk
  11. self.a2 = Article.objects.create(
  12. headline='Goodbye', pub_date=datetime(2005, 11, 28)
  13. ).pk
  14. self.a3 = Article.objects.create(
  15. headline='Hello and goodbye', pub_date=datetime(2005, 11, 29)
  16. ).pk
  17. def test_filter_or(self):
  18. self.assertQuerysetEqual(
  19. Article.objects.filter(headline__startswith='Hello') | Article.objects.filter(headline__startswith='Goodbye'), [
  20. 'Hello',
  21. 'Goodbye',
  22. 'Hello and goodbye'
  23. ],
  24. attrgetter("headline")
  25. )
  26. self.assertQuerysetEqual(
  27. Article.objects.filter(headline__contains='Hello') | Article.objects.filter(headline__contains='bye'), [
  28. 'Hello',
  29. 'Goodbye',
  30. 'Hello and goodbye'
  31. ],
  32. attrgetter("headline")
  33. )
  34. self.assertQuerysetEqual(
  35. Article.objects.filter(headline__iexact='Hello') | Article.objects.filter(headline__contains='ood'), [
  36. 'Hello',
  37. 'Goodbye',
  38. 'Hello and goodbye'
  39. ],
  40. attrgetter("headline")
  41. )
  42. self.assertQuerysetEqual(
  43. Article.objects.filter(Q(headline__startswith='Hello') | Q(headline__startswith='Goodbye')), [
  44. 'Hello',
  45. 'Goodbye',
  46. 'Hello and goodbye'
  47. ],
  48. attrgetter("headline")
  49. )
  50. def test_stages(self):
  51. # You can shorten this syntax with code like the following, which is
  52. # especially useful if building the query in stages:
  53. articles = Article.objects.all()
  54. self.assertQuerysetEqual(
  55. articles.filter(headline__startswith='Hello') & articles.filter(headline__startswith='Goodbye'),
  56. []
  57. )
  58. self.assertQuerysetEqual(
  59. articles.filter(headline__startswith='Hello') & articles.filter(headline__contains='bye'), [
  60. 'Hello and goodbye'
  61. ],
  62. attrgetter("headline")
  63. )
  64. def test_pk_q(self):
  65. self.assertQuerysetEqual(
  66. Article.objects.filter(Q(pk=self.a1) | Q(pk=self.a2)), [
  67. 'Hello',
  68. 'Goodbye'
  69. ],
  70. attrgetter("headline")
  71. )
  72. self.assertQuerysetEqual(
  73. Article.objects.filter(Q(pk=self.a1) | Q(pk=self.a2) | Q(pk=self.a3)), [
  74. 'Hello',
  75. 'Goodbye',
  76. 'Hello and goodbye'
  77. ],
  78. attrgetter("headline"),
  79. )
  80. def test_pk_in(self):
  81. self.assertQuerysetEqual(
  82. Article.objects.filter(pk__in=[self.a1, self.a2, self.a3]), [
  83. 'Hello',
  84. 'Goodbye',
  85. 'Hello and goodbye'
  86. ],
  87. attrgetter("headline"),
  88. )
  89. self.assertQuerysetEqual(
  90. Article.objects.filter(pk__in=(self.a1, self.a2, self.a3)), [
  91. 'Hello',
  92. 'Goodbye',
  93. 'Hello and goodbye'
  94. ],
  95. attrgetter("headline"),
  96. )
  97. self.assertQuerysetEqual(
  98. Article.objects.filter(pk__in=[self.a1, self.a2, self.a3, 40000]), [
  99. 'Hello',
  100. 'Goodbye',
  101. 'Hello and goodbye'
  102. ],
  103. attrgetter("headline"),
  104. )
  105. def test_q_negated(self):
  106. # Q objects can be negated
  107. self.assertQuerysetEqual(
  108. Article.objects.filter(Q(pk=self.a1) | ~Q(pk=self.a2)), [
  109. 'Hello',
  110. 'Hello and goodbye'
  111. ],
  112. attrgetter("headline")
  113. )
  114. self.assertQuerysetEqual(
  115. Article.objects.filter(~Q(pk=self.a1) & ~Q(pk=self.a2)), [
  116. 'Hello and goodbye'
  117. ],
  118. attrgetter("headline"),
  119. )
  120. # This allows for more complex queries than filter() and exclude()
  121. # alone would allow
  122. self.assertQuerysetEqual(
  123. Article.objects.filter(Q(pk=self.a1) & (~Q(pk=self.a2) | Q(pk=self.a3))), [
  124. 'Hello'
  125. ],
  126. attrgetter("headline"),
  127. )
  128. def test_complex_filter(self):
  129. # The 'complex_filter' method supports framework features such as
  130. # 'limit_choices_to' which normally take a single dictionary of lookup
  131. # arguments but need to support arbitrary queries via Q objects too.
  132. self.assertQuerysetEqual(
  133. Article.objects.complex_filter({'pk': self.a1}), [
  134. 'Hello'
  135. ],
  136. attrgetter("headline"),
  137. )
  138. self.assertQuerysetEqual(
  139. Article.objects.complex_filter(Q(pk=self.a1) | Q(pk=self.a2)), [
  140. 'Hello',
  141. 'Goodbye'
  142. ],
  143. attrgetter("headline"),
  144. )
  145. def test_empty_in(self):
  146. # Passing "in" an empty list returns no results ...
  147. self.assertQuerysetEqual(
  148. Article.objects.filter(pk__in=[]),
  149. []
  150. )
  151. # ... but can return results if we OR it with another query.
  152. self.assertQuerysetEqual(
  153. Article.objects.filter(Q(pk__in=[]) | Q(headline__icontains='goodbye')), [
  154. 'Goodbye',
  155. 'Hello and goodbye'
  156. ],
  157. attrgetter("headline"),
  158. )
  159. def test_q_and(self):
  160. # Q arg objects are ANDed
  161. self.assertQuerysetEqual(
  162. Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye')), [
  163. 'Hello and goodbye'
  164. ],
  165. attrgetter("headline")
  166. )
  167. # Q arg AND order is irrelevant
  168. self.assertQuerysetEqual(
  169. Article.objects.filter(Q(headline__contains='bye'), headline__startswith='Hello'), [
  170. 'Hello and goodbye'
  171. ],
  172. attrgetter("headline"),
  173. )
  174. self.assertQuerysetEqual(
  175. Article.objects.filter(Q(headline__startswith='Hello') & Q(headline__startswith='Goodbye')),
  176. []
  177. )
  178. def test_q_exclude(self):
  179. self.assertQuerysetEqual(
  180. Article.objects.exclude(Q(headline__startswith='Hello')), [
  181. 'Goodbye'
  182. ],
  183. attrgetter("headline")
  184. )
  185. def test_other_arg_queries(self):
  186. # Try some arg queries with operations other than filter.
  187. self.assertEqual(
  188. Article.objects.get(Q(headline__startswith='Hello'), Q(headline__contains='bye')).headline,
  189. 'Hello and goodbye'
  190. )
  191. self.assertEqual(
  192. Article.objects.filter(Q(headline__startswith='Hello') | Q(headline__contains='bye')).count(),
  193. 3
  194. )
  195. self.assertQuerysetEqual(
  196. Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye')).values(), [
  197. {"headline": "Hello and goodbye", "id": self.a3, "pub_date": datetime(2005, 11, 29)},
  198. ],
  199. lambda o: o,
  200. )
  201. self.assertEqual(
  202. Article.objects.filter(Q(headline__startswith='Hello')).in_bulk([self.a1, self.a2]),
  203. {self.a1: Article.objects.get(pk=self.a1)}
  204. )