1. import collections.abc
    
  2. from datetime import datetime
    
  3. from math import ceil
    
  4. from operator import attrgetter
    
  5. from unittest import skipUnless
    
  6. 
    
  7. from django.core.exceptions import FieldError
    
  8. from django.db import connection, models
    
  9. from django.db.models import (
    
  10.     BooleanField,
    
  11.     Case,
    
  12.     Exists,
    
  13.     ExpressionWrapper,
    
  14.     F,
    
  15.     Max,
    
  16.     OuterRef,
    
  17.     Q,
    
  18.     Subquery,
    
  19.     Value,
    
  20.     When,
    
  21. )
    
  22. from django.db.models.functions import Cast, Substr
    
  23. from django.db.models.lookups import (
    
  24.     Exact,
    
  25.     GreaterThan,
    
  26.     GreaterThanOrEqual,
    
  27.     IsNull,
    
  28.     LessThan,
    
  29.     LessThanOrEqual,
    
  30. )
    
  31. from django.test import TestCase, skipUnlessDBFeature
    
  32. from django.test.utils import isolate_apps
    
  33. 
    
  34. from .models import (
    
  35.     Article,
    
  36.     Author,
    
  37.     Freebie,
    
  38.     Game,
    
  39.     IsNullWithNoneAsRHS,
    
  40.     Player,
    
  41.     Product,
    
  42.     Season,
    
  43.     Stock,
    
  44.     Tag,
    
  45. )
    
  46. 
    
  47. 
    
  48. class LookupTests(TestCase):
    
  49.     @classmethod
    
  50.     def setUpTestData(cls):
    
  51.         # Create a few Authors.
    
  52.         cls.au1 = Author.objects.create(name="Author 1", alias="a1")
    
  53.         cls.au2 = Author.objects.create(name="Author 2", alias="a2")
    
  54.         # Create a few Articles.
    
  55.         cls.a1 = Article.objects.create(
    
  56.             headline="Article 1",
    
  57.             pub_date=datetime(2005, 7, 26),
    
  58.             author=cls.au1,
    
  59.             slug="a1",
    
  60.         )
    
  61.         cls.a2 = Article.objects.create(
    
  62.             headline="Article 2",
    
  63.             pub_date=datetime(2005, 7, 27),
    
  64.             author=cls.au1,
    
  65.             slug="a2",
    
  66.         )
    
  67.         cls.a3 = Article.objects.create(
    
  68.             headline="Article 3",
    
  69.             pub_date=datetime(2005, 7, 27),
    
  70.             author=cls.au1,
    
  71.             slug="a3",
    
  72.         )
    
  73.         cls.a4 = Article.objects.create(
    
  74.             headline="Article 4",
    
  75.             pub_date=datetime(2005, 7, 28),
    
  76.             author=cls.au1,
    
  77.             slug="a4",
    
  78.         )
    
  79.         cls.a5 = Article.objects.create(
    
  80.             headline="Article 5",
    
  81.             pub_date=datetime(2005, 8, 1, 9, 0),
    
  82.             author=cls.au2,
    
  83.             slug="a5",
    
  84.         )
    
  85.         cls.a6 = Article.objects.create(
    
  86.             headline="Article 6",
    
  87.             pub_date=datetime(2005, 8, 1, 8, 0),
    
  88.             author=cls.au2,
    
  89.             slug="a6",
    
  90.         )
    
  91.         cls.a7 = Article.objects.create(
    
  92.             headline="Article 7",
    
  93.             pub_date=datetime(2005, 7, 27),
    
  94.             author=cls.au2,
    
  95.             slug="a7",
    
  96.         )
    
  97.         # Create a few Tags.
    
  98.         cls.t1 = Tag.objects.create(name="Tag 1")
    
  99.         cls.t1.articles.add(cls.a1, cls.a2, cls.a3)
    
  100.         cls.t2 = Tag.objects.create(name="Tag 2")
    
  101.         cls.t2.articles.add(cls.a3, cls.a4, cls.a5)
    
  102.         cls.t3 = Tag.objects.create(name="Tag 3")
    
  103.         cls.t3.articles.add(cls.a5, cls.a6, cls.a7)
    
  104. 
    
  105.     def test_exists(self):
    
  106.         # We can use .exists() to check that there are some
    
  107.         self.assertTrue(Article.objects.exists())
    
  108.         for a in Article.objects.all():
    
  109.             a.delete()
    
  110.         # There should be none now!
    
  111.         self.assertFalse(Article.objects.exists())
    
  112. 
    
  113.     def test_lookup_int_as_str(self):
    
  114.         # Integer value can be queried using string
    
  115.         self.assertSequenceEqual(
    
  116.             Article.objects.filter(id__iexact=str(self.a1.id)),
    
  117.             [self.a1],
    
  118.         )
    
  119. 
    
  120.     @skipUnlessDBFeature("supports_date_lookup_using_string")
    
  121.     def test_lookup_date_as_str(self):
    
  122.         # A date lookup can be performed using a string search
    
  123.         self.assertSequenceEqual(
    
  124.             Article.objects.filter(pub_date__startswith="2005"),
    
  125.             [self.a5, self.a6, self.a4, self.a2, self.a3, self.a7, self.a1],
    
  126.         )
    
  127. 
    
  128.     def test_iterator(self):
    
  129.         # Each QuerySet gets iterator(), which is a generator that "lazily"
    
  130.         # returns results using database-level iteration.
    
  131.         self.assertIsInstance(Article.objects.iterator(), collections.abc.Iterator)
    
  132. 
    
  133.         self.assertQuerysetEqual(
    
  134.             Article.objects.iterator(),
    
  135.             [
    
  136.                 "Article 5",
    
  137.                 "Article 6",
    
  138.                 "Article 4",
    
  139.                 "Article 2",
    
  140.                 "Article 3",
    
  141.                 "Article 7",
    
  142.                 "Article 1",
    
  143.             ],
    
  144.             transform=attrgetter("headline"),
    
  145.         )
    
  146.         # iterator() can be used on any QuerySet.
    
  147.         self.assertQuerysetEqual(
    
  148.             Article.objects.filter(headline__endswith="4").iterator(),
    
  149.             ["Article 4"],
    
  150.             transform=attrgetter("headline"),
    
  151.         )
    
  152. 
    
  153.     def test_count(self):
    
  154.         # count() returns the number of objects matching search criteria.
    
  155.         self.assertEqual(Article.objects.count(), 7)
    
  156.         self.assertEqual(
    
  157.             Article.objects.filter(pub_date__exact=datetime(2005, 7, 27)).count(), 3
    
  158.         )
    
  159.         self.assertEqual(
    
  160.             Article.objects.filter(headline__startswith="Blah blah").count(), 0
    
  161.         )
    
  162. 
    
  163.         # count() should respect sliced query sets.
    
  164.         articles = Article.objects.all()
    
  165.         self.assertEqual(articles.count(), 7)
    
  166.         self.assertEqual(articles[:4].count(), 4)
    
  167.         self.assertEqual(articles[1:100].count(), 6)
    
  168.         self.assertEqual(articles[10:100].count(), 0)
    
  169. 
    
  170.         # Date and date/time lookups can also be done with strings.
    
  171.         self.assertEqual(
    
  172.             Article.objects.filter(pub_date__exact="2005-07-27 00:00:00").count(), 3
    
  173.         )
    
  174. 
    
  175.     def test_in_bulk(self):
    
  176.         # in_bulk() takes a list of IDs and returns a dictionary mapping IDs to objects.
    
  177.         arts = Article.objects.in_bulk([self.a1.id, self.a2.id])
    
  178.         self.assertEqual(arts[self.a1.id], self.a1)
    
  179.         self.assertEqual(arts[self.a2.id], self.a2)
    
  180.         self.assertEqual(
    
  181.             Article.objects.in_bulk(),
    
  182.             {
    
  183.                 self.a1.id: self.a1,
    
  184.                 self.a2.id: self.a2,
    
  185.                 self.a3.id: self.a3,
    
  186.                 self.a4.id: self.a4,
    
  187.                 self.a5.id: self.a5,
    
  188.                 self.a6.id: self.a6,
    
  189.                 self.a7.id: self.a7,
    
  190.             },
    
  191.         )
    
  192.         self.assertEqual(Article.objects.in_bulk([self.a3.id]), {self.a3.id: self.a3})
    
  193.         self.assertEqual(Article.objects.in_bulk({self.a3.id}), {self.a3.id: self.a3})
    
  194.         self.assertEqual(
    
  195.             Article.objects.in_bulk(frozenset([self.a3.id])), {self.a3.id: self.a3}
    
  196.         )
    
  197.         self.assertEqual(Article.objects.in_bulk((self.a3.id,)), {self.a3.id: self.a3})
    
  198.         self.assertEqual(Article.objects.in_bulk([1000]), {})
    
  199.         self.assertEqual(Article.objects.in_bulk([]), {})
    
  200.         self.assertEqual(
    
  201.             Article.objects.in_bulk(iter([self.a1.id])), {self.a1.id: self.a1}
    
  202.         )
    
  203.         self.assertEqual(Article.objects.in_bulk(iter([])), {})
    
  204.         with self.assertRaises(TypeError):
    
  205.             Article.objects.in_bulk(headline__startswith="Blah")
    
  206. 
    
  207.     def test_in_bulk_lots_of_ids(self):
    
  208.         test_range = 2000
    
  209.         max_query_params = connection.features.max_query_params
    
  210.         expected_num_queries = (
    
  211.             ceil(test_range / max_query_params) if max_query_params else 1
    
  212.         )
    
  213.         Author.objects.bulk_create(
    
  214.             [Author() for i in range(test_range - Author.objects.count())]
    
  215.         )
    
  216.         authors = {author.pk: author for author in Author.objects.all()}
    
  217.         with self.assertNumQueries(expected_num_queries):
    
  218.             self.assertEqual(Author.objects.in_bulk(authors), authors)
    
  219. 
    
  220.     def test_in_bulk_with_field(self):
    
  221.         self.assertEqual(
    
  222.             Article.objects.in_bulk(
    
  223.                 [self.a1.slug, self.a2.slug, self.a3.slug], field_name="slug"
    
  224.             ),
    
  225.             {
    
  226.                 self.a1.slug: self.a1,
    
  227.                 self.a2.slug: self.a2,
    
  228.                 self.a3.slug: self.a3,
    
  229.             },
    
  230.         )
    
  231. 
    
  232.     def test_in_bulk_meta_constraint(self):
    
  233.         season_2011 = Season.objects.create(year=2011)
    
  234.         season_2012 = Season.objects.create(year=2012)
    
  235.         Season.objects.create(year=2013)
    
  236.         self.assertEqual(
    
  237.             Season.objects.in_bulk(
    
  238.                 [season_2011.year, season_2012.year],
    
  239.                 field_name="year",
    
  240.             ),
    
  241.             {season_2011.year: season_2011, season_2012.year: season_2012},
    
  242.         )
    
  243. 
    
  244.     def test_in_bulk_non_unique_field(self):
    
  245.         msg = "in_bulk()'s field_name must be a unique field but 'author' isn't."
    
  246.         with self.assertRaisesMessage(ValueError, msg):
    
  247.             Article.objects.in_bulk([self.au1], field_name="author")
    
  248. 
    
  249.     @skipUnlessDBFeature("can_distinct_on_fields")
    
  250.     def test_in_bulk_distinct_field(self):
    
  251.         self.assertEqual(
    
  252.             Article.objects.order_by("headline")
    
  253.             .distinct("headline")
    
  254.             .in_bulk(
    
  255.                 [self.a1.headline, self.a5.headline],
    
  256.                 field_name="headline",
    
  257.             ),
    
  258.             {self.a1.headline: self.a1, self.a5.headline: self.a5},
    
  259.         )
    
  260. 
    
  261.     @skipUnlessDBFeature("can_distinct_on_fields")
    
  262.     def test_in_bulk_multiple_distinct_field(self):
    
  263.         msg = "in_bulk()'s field_name must be a unique field but 'pub_date' isn't."
    
  264.         with self.assertRaisesMessage(ValueError, msg):
    
  265.             Article.objects.order_by("headline", "pub_date").distinct(
    
  266.                 "headline",
    
  267.                 "pub_date",
    
  268.             ).in_bulk(field_name="pub_date")
    
  269. 
    
  270.     @isolate_apps("lookup")
    
  271.     def test_in_bulk_non_unique_meta_constaint(self):
    
  272.         class Model(models.Model):
    
  273.             ean = models.CharField(max_length=100)
    
  274.             brand = models.CharField(max_length=100)
    
  275.             name = models.CharField(max_length=80)
    
  276. 
    
  277.             class Meta:
    
  278.                 constraints = [
    
  279.                     models.UniqueConstraint(
    
  280.                         fields=["ean"],
    
  281.                         name="partial_ean_unique",
    
  282.                         condition=models.Q(is_active=True),
    
  283.                     ),
    
  284.                     models.UniqueConstraint(
    
  285.                         fields=["brand", "name"],
    
  286.                         name="together_brand_name_unique",
    
  287.                     ),
    
  288.                 ]
    
  289. 
    
  290.         msg = "in_bulk()'s field_name must be a unique field but '%s' isn't."
    
  291.         for field_name in ["brand", "ean"]:
    
  292.             with self.subTest(field_name=field_name):
    
  293.                 with self.assertRaisesMessage(ValueError, msg % field_name):
    
  294.                     Model.objects.in_bulk(field_name=field_name)
    
  295. 
    
  296.     def test_in_bulk_sliced_queryset(self):
    
  297.         msg = "Cannot use 'limit' or 'offset' with in_bulk()."
    
  298.         with self.assertRaisesMessage(TypeError, msg):
    
  299.             Article.objects.all()[0:5].in_bulk([self.a1.id, self.a2.id])
    
  300. 
    
  301.     def test_values(self):
    
  302.         # values() returns a list of dictionaries instead of object instances --
    
  303.         # and you can specify which fields you want to retrieve.
    
  304.         self.assertSequenceEqual(
    
  305.             Article.objects.values("headline"),
    
  306.             [
    
  307.                 {"headline": "Article 5"},
    
  308.                 {"headline": "Article 6"},
    
  309.                 {"headline": "Article 4"},
    
  310.                 {"headline": "Article 2"},
    
  311.                 {"headline": "Article 3"},
    
  312.                 {"headline": "Article 7"},
    
  313.                 {"headline": "Article 1"},
    
  314.             ],
    
  315.         )
    
  316.         self.assertSequenceEqual(
    
  317.             Article.objects.filter(pub_date__exact=datetime(2005, 7, 27)).values("id"),
    
  318.             [{"id": self.a2.id}, {"id": self.a3.id}, {"id": self.a7.id}],
    
  319.         )
    
  320.         self.assertSequenceEqual(
    
  321.             Article.objects.values("id", "headline"),
    
  322.             [
    
  323.                 {"id": self.a5.id, "headline": "Article 5"},
    
  324.                 {"id": self.a6.id, "headline": "Article 6"},
    
  325.                 {"id": self.a4.id, "headline": "Article 4"},
    
  326.                 {"id": self.a2.id, "headline": "Article 2"},
    
  327.                 {"id": self.a3.id, "headline": "Article 3"},
    
  328.                 {"id": self.a7.id, "headline": "Article 7"},
    
  329.                 {"id": self.a1.id, "headline": "Article 1"},
    
  330.             ],
    
  331.         )
    
  332.         # You can use values() with iterator() for memory savings,
    
  333.         # because iterator() uses database-level iteration.
    
  334.         self.assertSequenceEqual(
    
  335.             list(Article.objects.values("id", "headline").iterator()),
    
  336.             [
    
  337.                 {"headline": "Article 5", "id": self.a5.id},
    
  338.                 {"headline": "Article 6", "id": self.a6.id},
    
  339.                 {"headline": "Article 4", "id": self.a4.id},
    
  340.                 {"headline": "Article 2", "id": self.a2.id},
    
  341.                 {"headline": "Article 3", "id": self.a3.id},
    
  342.                 {"headline": "Article 7", "id": self.a7.id},
    
  343.                 {"headline": "Article 1", "id": self.a1.id},
    
  344.             ],
    
  345.         )
    
  346.         # The values() method works with "extra" fields specified in extra(select).
    
  347.         self.assertSequenceEqual(
    
  348.             Article.objects.extra(select={"id_plus_one": "id + 1"}).values(
    
  349.                 "id", "id_plus_one"
    
  350.             ),
    
  351.             [
    
  352.                 {"id": self.a5.id, "id_plus_one": self.a5.id + 1},
    
  353.                 {"id": self.a6.id, "id_plus_one": self.a6.id + 1},
    
  354.                 {"id": self.a4.id, "id_plus_one": self.a4.id + 1},
    
  355.                 {"id": self.a2.id, "id_plus_one": self.a2.id + 1},
    
  356.                 {"id": self.a3.id, "id_plus_one": self.a3.id + 1},
    
  357.                 {"id": self.a7.id, "id_plus_one": self.a7.id + 1},
    
  358.                 {"id": self.a1.id, "id_plus_one": self.a1.id + 1},
    
  359.             ],
    
  360.         )
    
  361.         data = {
    
  362.             "id_plus_one": "id+1",
    
  363.             "id_plus_two": "id+2",
    
  364.             "id_plus_three": "id+3",
    
  365.             "id_plus_four": "id+4",
    
  366.             "id_plus_five": "id+5",
    
  367.             "id_plus_six": "id+6",
    
  368.             "id_plus_seven": "id+7",
    
  369.             "id_plus_eight": "id+8",
    
  370.         }
    
  371.         self.assertSequenceEqual(
    
  372.             Article.objects.filter(id=self.a1.id).extra(select=data).values(*data),
    
  373.             [
    
  374.                 {
    
  375.                     "id_plus_one": self.a1.id + 1,
    
  376.                     "id_plus_two": self.a1.id + 2,
    
  377.                     "id_plus_three": self.a1.id + 3,
    
  378.                     "id_plus_four": self.a1.id + 4,
    
  379.                     "id_plus_five": self.a1.id + 5,
    
  380.                     "id_plus_six": self.a1.id + 6,
    
  381.                     "id_plus_seven": self.a1.id + 7,
    
  382.                     "id_plus_eight": self.a1.id + 8,
    
  383.                 }
    
  384.             ],
    
  385.         )
    
  386.         # You can specify fields from forward and reverse relations, just like filter().
    
  387.         self.assertSequenceEqual(
    
  388.             Article.objects.values("headline", "author__name"),
    
  389.             [
    
  390.                 {"headline": self.a5.headline, "author__name": self.au2.name},
    
  391.                 {"headline": self.a6.headline, "author__name": self.au2.name},
    
  392.                 {"headline": self.a4.headline, "author__name": self.au1.name},
    
  393.                 {"headline": self.a2.headline, "author__name": self.au1.name},
    
  394.                 {"headline": self.a3.headline, "author__name": self.au1.name},
    
  395.                 {"headline": self.a7.headline, "author__name": self.au2.name},
    
  396.                 {"headline": self.a1.headline, "author__name": self.au1.name},
    
  397.             ],
    
  398.         )
    
  399.         self.assertSequenceEqual(
    
  400.             Author.objects.values("name", "article__headline").order_by(
    
  401.                 "name", "article__headline"
    
  402.             ),
    
  403.             [
    
  404.                 {"name": self.au1.name, "article__headline": self.a1.headline},
    
  405.                 {"name": self.au1.name, "article__headline": self.a2.headline},
    
  406.                 {"name": self.au1.name, "article__headline": self.a3.headline},
    
  407.                 {"name": self.au1.name, "article__headline": self.a4.headline},
    
  408.                 {"name": self.au2.name, "article__headline": self.a5.headline},
    
  409.                 {"name": self.au2.name, "article__headline": self.a6.headline},
    
  410.                 {"name": self.au2.name, "article__headline": self.a7.headline},
    
  411.             ],
    
  412.         )
    
  413.         self.assertSequenceEqual(
    
  414.             (
    
  415.                 Author.objects.values(
    
  416.                     "name", "article__headline", "article__tag__name"
    
  417.                 ).order_by("name", "article__headline", "article__tag__name")
    
  418.             ),
    
  419.             [
    
  420.                 {
    
  421.                     "name": self.au1.name,
    
  422.                     "article__headline": self.a1.headline,
    
  423.                     "article__tag__name": self.t1.name,
    
  424.                 },
    
  425.                 {
    
  426.                     "name": self.au1.name,
    
  427.                     "article__headline": self.a2.headline,
    
  428.                     "article__tag__name": self.t1.name,
    
  429.                 },
    
  430.                 {
    
  431.                     "name": self.au1.name,
    
  432.                     "article__headline": self.a3.headline,
    
  433.                     "article__tag__name": self.t1.name,
    
  434.                 },
    
  435.                 {
    
  436.                     "name": self.au1.name,
    
  437.                     "article__headline": self.a3.headline,
    
  438.                     "article__tag__name": self.t2.name,
    
  439.                 },
    
  440.                 {
    
  441.                     "name": self.au1.name,
    
  442.                     "article__headline": self.a4.headline,
    
  443.                     "article__tag__name": self.t2.name,
    
  444.                 },
    
  445.                 {
    
  446.                     "name": self.au2.name,
    
  447.                     "article__headline": self.a5.headline,
    
  448.                     "article__tag__name": self.t2.name,
    
  449.                 },
    
  450.                 {
    
  451.                     "name": self.au2.name,
    
  452.                     "article__headline": self.a5.headline,
    
  453.                     "article__tag__name": self.t3.name,
    
  454.                 },
    
  455.                 {
    
  456.                     "name": self.au2.name,
    
  457.                     "article__headline": self.a6.headline,
    
  458.                     "article__tag__name": self.t3.name,
    
  459.                 },
    
  460.                 {
    
  461.                     "name": self.au2.name,
    
  462.                     "article__headline": self.a7.headline,
    
  463.                     "article__tag__name": self.t3.name,
    
  464.                 },
    
  465.             ],
    
  466.         )
    
  467.         # However, an exception FieldDoesNotExist will be thrown if you specify
    
  468.         # a nonexistent field name in values() (a field that is neither in the
    
  469.         # model nor in extra(select)).
    
  470.         msg = (
    
  471.             "Cannot resolve keyword 'id_plus_two' into field. Choices are: "
    
  472.             "author, author_id, headline, id, id_plus_one, pub_date, slug, tag"
    
  473.         )
    
  474.         with self.assertRaisesMessage(FieldError, msg):
    
  475.             Article.objects.extra(select={"id_plus_one": "id + 1"}).values(
    
  476.                 "id", "id_plus_two"
    
  477.             )
    
  478.         # If you don't specify field names to values(), all are returned.
    
  479.         self.assertSequenceEqual(
    
  480.             Article.objects.filter(id=self.a5.id).values(),
    
  481.             [
    
  482.                 {
    
  483.                     "id": self.a5.id,
    
  484.                     "author_id": self.au2.id,
    
  485.                     "headline": "Article 5",
    
  486.                     "pub_date": datetime(2005, 8, 1, 9, 0),
    
  487.                     "slug": "a5",
    
  488.                 }
    
  489.             ],
    
  490.         )
    
  491. 
    
  492.     def test_values_list(self):
    
  493.         # values_list() is similar to values(), except that the results are
    
  494.         # returned as a list of tuples, rather than a list of dictionaries.
    
  495.         # Within each tuple, the order of the elements is the same as the order
    
  496.         # of fields in the values_list() call.
    
  497.         self.assertSequenceEqual(
    
  498.             Article.objects.values_list("headline"),
    
  499.             [
    
  500.                 ("Article 5",),
    
  501.                 ("Article 6",),
    
  502.                 ("Article 4",),
    
  503.                 ("Article 2",),
    
  504.                 ("Article 3",),
    
  505.                 ("Article 7",),
    
  506.                 ("Article 1",),
    
  507.             ],
    
  508.         )
    
  509.         self.assertSequenceEqual(
    
  510.             Article.objects.values_list("id").order_by("id"),
    
  511.             [
    
  512.                 (self.a1.id,),
    
  513.                 (self.a2.id,),
    
  514.                 (self.a3.id,),
    
  515.                 (self.a4.id,),
    
  516.                 (self.a5.id,),
    
  517.                 (self.a6.id,),
    
  518.                 (self.a7.id,),
    
  519.             ],
    
  520.         )
    
  521.         self.assertSequenceEqual(
    
  522.             Article.objects.values_list("id", flat=True).order_by("id"),
    
  523.             [
    
  524.                 self.a1.id,
    
  525.                 self.a2.id,
    
  526.                 self.a3.id,
    
  527.                 self.a4.id,
    
  528.                 self.a5.id,
    
  529.                 self.a6.id,
    
  530.                 self.a7.id,
    
  531.             ],
    
  532.         )
    
  533.         self.assertSequenceEqual(
    
  534.             Article.objects.extra(select={"id_plus_one": "id+1"})
    
  535.             .order_by("id")
    
  536.             .values_list("id"),
    
  537.             [
    
  538.                 (self.a1.id,),
    
  539.                 (self.a2.id,),
    
  540.                 (self.a3.id,),
    
  541.                 (self.a4.id,),
    
  542.                 (self.a5.id,),
    
  543.                 (self.a6.id,),
    
  544.                 (self.a7.id,),
    
  545.             ],
    
  546.         )
    
  547.         self.assertSequenceEqual(
    
  548.             Article.objects.extra(select={"id_plus_one": "id+1"})
    
  549.             .order_by("id")
    
  550.             .values_list("id_plus_one", "id"),
    
  551.             [
    
  552.                 (self.a1.id + 1, self.a1.id),
    
  553.                 (self.a2.id + 1, self.a2.id),
    
  554.                 (self.a3.id + 1, self.a3.id),
    
  555.                 (self.a4.id + 1, self.a4.id),
    
  556.                 (self.a5.id + 1, self.a5.id),
    
  557.                 (self.a6.id + 1, self.a6.id),
    
  558.                 (self.a7.id + 1, self.a7.id),
    
  559.             ],
    
  560.         )
    
  561.         self.assertSequenceEqual(
    
  562.             Article.objects.extra(select={"id_plus_one": "id+1"})
    
  563.             .order_by("id")
    
  564.             .values_list("id", "id_plus_one"),
    
  565.             [
    
  566.                 (self.a1.id, self.a1.id + 1),
    
  567.                 (self.a2.id, self.a2.id + 1),
    
  568.                 (self.a3.id, self.a3.id + 1),
    
  569.                 (self.a4.id, self.a4.id + 1),
    
  570.                 (self.a5.id, self.a5.id + 1),
    
  571.                 (self.a6.id, self.a6.id + 1),
    
  572.                 (self.a7.id, self.a7.id + 1),
    
  573.             ],
    
  574.         )
    
  575.         args = ("name", "article__headline", "article__tag__name")
    
  576.         self.assertSequenceEqual(
    
  577.             Author.objects.values_list(*args).order_by(*args),
    
  578.             [
    
  579.                 (self.au1.name, self.a1.headline, self.t1.name),
    
  580.                 (self.au1.name, self.a2.headline, self.t1.name),
    
  581.                 (self.au1.name, self.a3.headline, self.t1.name),
    
  582.                 (self.au1.name, self.a3.headline, self.t2.name),
    
  583.                 (self.au1.name, self.a4.headline, self.t2.name),
    
  584.                 (self.au2.name, self.a5.headline, self.t2.name),
    
  585.                 (self.au2.name, self.a5.headline, self.t3.name),
    
  586.                 (self.au2.name, self.a6.headline, self.t3.name),
    
  587.                 (self.au2.name, self.a7.headline, self.t3.name),
    
  588.             ],
    
  589.         )
    
  590.         with self.assertRaises(TypeError):
    
  591.             Article.objects.values_list("id", "headline", flat=True)
    
  592. 
    
  593.     def test_get_next_previous_by(self):
    
  594.         # Every DateField and DateTimeField creates get_next_by_FOO() and
    
  595.         # get_previous_by_FOO() methods. In the case of identical date values,
    
  596.         # these methods will use the ID as a fallback check. This guarantees
    
  597.         # that no records are skipped or duplicated.
    
  598.         self.assertEqual(repr(self.a1.get_next_by_pub_date()), "<Article: Article 2>")
    
  599.         self.assertEqual(repr(self.a2.get_next_by_pub_date()), "<Article: Article 3>")
    
  600.         self.assertEqual(
    
  601.             repr(self.a2.get_next_by_pub_date(headline__endswith="6")),
    
  602.             "<Article: Article 6>",
    
  603.         )
    
  604.         self.assertEqual(repr(self.a3.get_next_by_pub_date()), "<Article: Article 7>")
    
  605.         self.assertEqual(repr(self.a4.get_next_by_pub_date()), "<Article: Article 6>")
    
  606.         with self.assertRaises(Article.DoesNotExist):
    
  607.             self.a5.get_next_by_pub_date()
    
  608.         self.assertEqual(repr(self.a6.get_next_by_pub_date()), "<Article: Article 5>")
    
  609.         self.assertEqual(repr(self.a7.get_next_by_pub_date()), "<Article: Article 4>")
    
  610. 
    
  611.         self.assertEqual(
    
  612.             repr(self.a7.get_previous_by_pub_date()), "<Article: Article 3>"
    
  613.         )
    
  614.         self.assertEqual(
    
  615.             repr(self.a6.get_previous_by_pub_date()), "<Article: Article 4>"
    
  616.         )
    
  617.         self.assertEqual(
    
  618.             repr(self.a5.get_previous_by_pub_date()), "<Article: Article 6>"
    
  619.         )
    
  620.         self.assertEqual(
    
  621.             repr(self.a4.get_previous_by_pub_date()), "<Article: Article 7>"
    
  622.         )
    
  623.         self.assertEqual(
    
  624.             repr(self.a3.get_previous_by_pub_date()), "<Article: Article 2>"
    
  625.         )
    
  626.         self.assertEqual(
    
  627.             repr(self.a2.get_previous_by_pub_date()), "<Article: Article 1>"
    
  628.         )
    
  629. 
    
  630.     def test_escaping(self):
    
  631.         # Underscores, percent signs and backslashes have special meaning in the
    
  632.         # underlying SQL code, but Django handles the quoting of them automatically.
    
  633.         a8 = Article.objects.create(
    
  634.             headline="Article_ with underscore", pub_date=datetime(2005, 11, 20)
    
  635.         )
    
  636. 
    
  637.         self.assertSequenceEqual(
    
  638.             Article.objects.filter(headline__startswith="Article"),
    
  639.             [a8, self.a5, self.a6, self.a4, self.a2, self.a3, self.a7, self.a1],
    
  640.         )
    
  641.         self.assertSequenceEqual(
    
  642.             Article.objects.filter(headline__startswith="Article_"),
    
  643.             [a8],
    
  644.         )
    
  645.         a9 = Article.objects.create(
    
  646.             headline="Article% with percent sign", pub_date=datetime(2005, 11, 21)
    
  647.         )
    
  648.         self.assertSequenceEqual(
    
  649.             Article.objects.filter(headline__startswith="Article"),
    
  650.             [a9, a8, self.a5, self.a6, self.a4, self.a2, self.a3, self.a7, self.a1],
    
  651.         )
    
  652.         self.assertSequenceEqual(
    
  653.             Article.objects.filter(headline__startswith="Article%"),
    
  654.             [a9],
    
  655.         )
    
  656.         a10 = Article.objects.create(
    
  657.             headline="Article with \\ backslash", pub_date=datetime(2005, 11, 22)
    
  658.         )
    
  659.         self.assertSequenceEqual(
    
  660.             Article.objects.filter(headline__contains="\\"),
    
  661.             [a10],
    
  662.         )
    
  663. 
    
  664.     def test_exclude(self):
    
  665.         pub_date = datetime(2005, 11, 20)
    
  666.         a8 = Article.objects.create(
    
  667.             headline="Article_ with underscore", pub_date=pub_date
    
  668.         )
    
  669.         a9 = Article.objects.create(
    
  670.             headline="Article% with percent sign", pub_date=pub_date
    
  671.         )
    
  672.         a10 = Article.objects.create(
    
  673.             headline="Article with \\ backslash", pub_date=pub_date
    
  674.         )
    
  675.         # exclude() is the opposite of filter() when doing lookups:
    
  676.         self.assertSequenceEqual(
    
  677.             Article.objects.filter(headline__contains="Article").exclude(
    
  678.                 headline__contains="with"
    
  679.             ),
    
  680.             [self.a5, self.a6, self.a4, self.a2, self.a3, self.a7, self.a1],
    
  681.         )
    
  682.         self.assertSequenceEqual(
    
  683.             Article.objects.exclude(headline__startswith="Article_"),
    
  684.             [a10, a9, self.a5, self.a6, self.a4, self.a2, self.a3, self.a7, self.a1],
    
  685.         )
    
  686.         self.assertSequenceEqual(
    
  687.             Article.objects.exclude(headline="Article 7"),
    
  688.             [a10, a9, a8, self.a5, self.a6, self.a4, self.a2, self.a3, self.a1],
    
  689.         )
    
  690. 
    
  691.     def test_none(self):
    
  692.         # none() returns a QuerySet that behaves like any other QuerySet object
    
  693.         self.assertQuerysetEqual(Article.objects.none(), [])
    
  694.         self.assertQuerysetEqual(
    
  695.             Article.objects.none().filter(headline__startswith="Article"), []
    
  696.         )
    
  697.         self.assertQuerysetEqual(
    
  698.             Article.objects.filter(headline__startswith="Article").none(), []
    
  699.         )
    
  700.         self.assertEqual(Article.objects.none().count(), 0)
    
  701.         self.assertEqual(
    
  702.             Article.objects.none().update(headline="This should not take effect"), 0
    
  703.         )
    
  704.         self.assertQuerysetEqual(Article.objects.none().iterator(), [])
    
  705. 
    
  706.     def test_in(self):
    
  707.         self.assertSequenceEqual(
    
  708.             Article.objects.exclude(id__in=[]),
    
  709.             [self.a5, self.a6, self.a4, self.a2, self.a3, self.a7, self.a1],
    
  710.         )
    
  711. 
    
  712.     def test_in_empty_list(self):
    
  713.         self.assertSequenceEqual(Article.objects.filter(id__in=[]), [])
    
  714. 
    
  715.     def test_in_different_database(self):
    
  716.         with self.assertRaisesMessage(
    
  717.             ValueError,
    
  718.             "Subqueries aren't allowed across different databases. Force the "
    
  719.             "inner query to be evaluated using `list(inner_query)`.",
    
  720.         ):
    
  721.             list(Article.objects.filter(id__in=Article.objects.using("other").all()))
    
  722. 
    
  723.     def test_in_keeps_value_ordering(self):
    
  724.         query = (
    
  725.             Article.objects.filter(slug__in=["a%d" % i for i in range(1, 8)])
    
  726.             .values("pk")
    
  727.             .query
    
  728.         )
    
  729.         self.assertIn(" IN (a1, a2, a3, a4, a5, a6, a7) ", str(query))
    
  730. 
    
  731.     def test_in_ignore_none(self):
    
  732.         with self.assertNumQueries(1) as ctx:
    
  733.             self.assertSequenceEqual(
    
  734.                 Article.objects.filter(id__in=[None, self.a1.id]),
    
  735.                 [self.a1],
    
  736.             )
    
  737.         sql = ctx.captured_queries[0]["sql"]
    
  738.         self.assertIn("IN (%s)" % self.a1.pk, sql)
    
  739. 
    
  740.     def test_in_ignore_solo_none(self):
    
  741.         with self.assertNumQueries(0):
    
  742.             self.assertSequenceEqual(Article.objects.filter(id__in=[None]), [])
    
  743. 
    
  744.     def test_in_ignore_none_with_unhashable_items(self):
    
  745.         class UnhashableInt(int):
    
  746.             __hash__ = None
    
  747. 
    
  748.         with self.assertNumQueries(1) as ctx:
    
  749.             self.assertSequenceEqual(
    
  750.                 Article.objects.filter(id__in=[None, UnhashableInt(self.a1.id)]),
    
  751.                 [self.a1],
    
  752.             )
    
  753.         sql = ctx.captured_queries[0]["sql"]
    
  754.         self.assertIn("IN (%s)" % self.a1.pk, sql)
    
  755. 
    
  756.     def test_error_messages(self):
    
  757.         # Programming errors are pointed out with nice error messages
    
  758.         with self.assertRaisesMessage(
    
  759.             FieldError,
    
  760.             "Cannot resolve keyword 'pub_date_year' into field. Choices are: "
    
  761.             "author, author_id, headline, id, pub_date, slug, tag",
    
  762.         ):
    
  763.             Article.objects.filter(pub_date_year="2005").count()
    
  764. 
    
  765.     def test_unsupported_lookups(self):
    
  766.         with self.assertRaisesMessage(
    
  767.             FieldError,
    
  768.             "Unsupported lookup 'starts' for CharField or join on the field "
    
  769.             "not permitted, perhaps you meant startswith or istartswith?",
    
  770.         ):
    
  771.             Article.objects.filter(headline__starts="Article")
    
  772. 
    
  773.         with self.assertRaisesMessage(
    
  774.             FieldError,
    
  775.             "Unsupported lookup 'is_null' for DateTimeField or join on the field "
    
  776.             "not permitted, perhaps you meant isnull?",
    
  777.         ):
    
  778.             Article.objects.filter(pub_date__is_null=True)
    
  779. 
    
  780.         with self.assertRaisesMessage(
    
  781.             FieldError,
    
  782.             "Unsupported lookup 'gobbledygook' for DateTimeField or join on the field "
    
  783.             "not permitted.",
    
  784.         ):
    
  785.             Article.objects.filter(pub_date__gobbledygook="blahblah")
    
  786. 
    
  787.     def test_relation_nested_lookup_error(self):
    
  788.         # An invalid nested lookup on a related field raises a useful error.
    
  789.         msg = "Related Field got invalid lookup: editor"
    
  790.         with self.assertRaisesMessage(FieldError, msg):
    
  791.             Article.objects.filter(author__editor__name="James")
    
  792.         msg = "Related Field got invalid lookup: foo"
    
  793.         with self.assertRaisesMessage(FieldError, msg):
    
  794.             Tag.objects.filter(articles__foo="bar")
    
  795. 
    
  796.     def test_regex(self):
    
  797.         # Create some articles with a bit more interesting headlines for
    
  798.         # testing field lookups.
    
  799.         Article.objects.all().delete()
    
  800.         now = datetime.now()
    
  801.         Article.objects.bulk_create(
    
  802.             [
    
  803.                 Article(pub_date=now, headline="f"),
    
  804.                 Article(pub_date=now, headline="fo"),
    
  805.                 Article(pub_date=now, headline="foo"),
    
  806.                 Article(pub_date=now, headline="fooo"),
    
  807.                 Article(pub_date=now, headline="hey-Foo"),
    
  808.                 Article(pub_date=now, headline="bar"),
    
  809.                 Article(pub_date=now, headline="AbBa"),
    
  810.                 Article(pub_date=now, headline="baz"),
    
  811.                 Article(pub_date=now, headline="baxZ"),
    
  812.             ]
    
  813.         )
    
  814.         # zero-or-more
    
  815.         self.assertQuerysetEqual(
    
  816.             Article.objects.filter(headline__regex=r"fo*"),
    
  817.             Article.objects.filter(headline__in=["f", "fo", "foo", "fooo"]),
    
  818.         )
    
  819.         self.assertQuerysetEqual(
    
  820.             Article.objects.filter(headline__iregex=r"fo*"),
    
  821.             Article.objects.filter(headline__in=["f", "fo", "foo", "fooo", "hey-Foo"]),
    
  822.         )
    
  823.         # one-or-more
    
  824.         self.assertQuerysetEqual(
    
  825.             Article.objects.filter(headline__regex=r"fo+"),
    
  826.             Article.objects.filter(headline__in=["fo", "foo", "fooo"]),
    
  827.         )
    
  828.         # wildcard
    
  829.         self.assertQuerysetEqual(
    
  830.             Article.objects.filter(headline__regex=r"fooo?"),
    
  831.             Article.objects.filter(headline__in=["foo", "fooo"]),
    
  832.         )
    
  833.         # leading anchor
    
  834.         self.assertQuerysetEqual(
    
  835.             Article.objects.filter(headline__regex=r"^b"),
    
  836.             Article.objects.filter(headline__in=["bar", "baxZ", "baz"]),
    
  837.         )
    
  838.         self.assertQuerysetEqual(
    
  839.             Article.objects.filter(headline__iregex=r"^a"),
    
  840.             Article.objects.filter(headline="AbBa"),
    
  841.         )
    
  842.         # trailing anchor
    
  843.         self.assertQuerysetEqual(
    
  844.             Article.objects.filter(headline__regex=r"z$"),
    
  845.             Article.objects.filter(headline="baz"),
    
  846.         )
    
  847.         self.assertQuerysetEqual(
    
  848.             Article.objects.filter(headline__iregex=r"z$"),
    
  849.             Article.objects.filter(headline__in=["baxZ", "baz"]),
    
  850.         )
    
  851.         # character sets
    
  852.         self.assertQuerysetEqual(
    
  853.             Article.objects.filter(headline__regex=r"ba[rz]"),
    
  854.             Article.objects.filter(headline__in=["bar", "baz"]),
    
  855.         )
    
  856.         self.assertQuerysetEqual(
    
  857.             Article.objects.filter(headline__regex=r"ba.[RxZ]"),
    
  858.             Article.objects.filter(headline="baxZ"),
    
  859.         )
    
  860.         self.assertQuerysetEqual(
    
  861.             Article.objects.filter(headline__iregex=r"ba[RxZ]"),
    
  862.             Article.objects.filter(headline__in=["bar", "baxZ", "baz"]),
    
  863.         )
    
  864. 
    
  865.         # and more articles:
    
  866.         Article.objects.bulk_create(
    
  867.             [
    
  868.                 Article(pub_date=now, headline="foobar"),
    
  869.                 Article(pub_date=now, headline="foobaz"),
    
  870.                 Article(pub_date=now, headline="ooF"),
    
  871.                 Article(pub_date=now, headline="foobarbaz"),
    
  872.                 Article(pub_date=now, headline="zoocarfaz"),
    
  873.                 Article(pub_date=now, headline="barfoobaz"),
    
  874.                 Article(pub_date=now, headline="bazbaRFOO"),
    
  875.             ]
    
  876.         )
    
  877. 
    
  878.         # alternation
    
  879.         self.assertQuerysetEqual(
    
  880.             Article.objects.filter(headline__regex=r"oo(f|b)"),
    
  881.             Article.objects.filter(
    
  882.                 headline__in=[
    
  883.                     "barfoobaz",
    
  884.                     "foobar",
    
  885.                     "foobarbaz",
    
  886.                     "foobaz",
    
  887.                 ]
    
  888.             ),
    
  889.         )
    
  890.         self.assertQuerysetEqual(
    
  891.             Article.objects.filter(headline__iregex=r"oo(f|b)"),
    
  892.             Article.objects.filter(
    
  893.                 headline__in=[
    
  894.                     "barfoobaz",
    
  895.                     "foobar",
    
  896.                     "foobarbaz",
    
  897.                     "foobaz",
    
  898.                     "ooF",
    
  899.                 ]
    
  900.             ),
    
  901.         )
    
  902.         self.assertQuerysetEqual(
    
  903.             Article.objects.filter(headline__regex=r"^foo(f|b)"),
    
  904.             Article.objects.filter(headline__in=["foobar", "foobarbaz", "foobaz"]),
    
  905.         )
    
  906. 
    
  907.         # greedy matching
    
  908.         self.assertQuerysetEqual(
    
  909.             Article.objects.filter(headline__regex=r"b.*az"),
    
  910.             Article.objects.filter(
    
  911.                 headline__in=[
    
  912.                     "barfoobaz",
    
  913.                     "baz",
    
  914.                     "bazbaRFOO",
    
  915.                     "foobarbaz",
    
  916.                     "foobaz",
    
  917.                 ]
    
  918.             ),
    
  919.         )
    
  920.         self.assertQuerysetEqual(
    
  921.             Article.objects.filter(headline__iregex=r"b.*ar"),
    
  922.             Article.objects.filter(
    
  923.                 headline__in=[
    
  924.                     "bar",
    
  925.                     "barfoobaz",
    
  926.                     "bazbaRFOO",
    
  927.                     "foobar",
    
  928.                     "foobarbaz",
    
  929.                 ]
    
  930.             ),
    
  931.         )
    
  932. 
    
  933.     @skipUnlessDBFeature("supports_regex_backreferencing")
    
  934.     def test_regex_backreferencing(self):
    
  935.         # grouping and backreferences
    
  936.         now = datetime.now()
    
  937.         Article.objects.bulk_create(
    
  938.             [
    
  939.                 Article(pub_date=now, headline="foobar"),
    
  940.                 Article(pub_date=now, headline="foobaz"),
    
  941.                 Article(pub_date=now, headline="ooF"),
    
  942.                 Article(pub_date=now, headline="foobarbaz"),
    
  943.                 Article(pub_date=now, headline="zoocarfaz"),
    
  944.                 Article(pub_date=now, headline="barfoobaz"),
    
  945.                 Article(pub_date=now, headline="bazbaRFOO"),
    
  946.             ]
    
  947.         )
    
  948.         self.assertQuerysetEqual(
    
  949.             Article.objects.filter(headline__regex=r"b(.).*b\1").values_list(
    
  950.                 "headline", flat=True
    
  951.             ),
    
  952.             ["barfoobaz", "bazbaRFOO", "foobarbaz"],
    
  953.         )
    
  954. 
    
  955.     def test_regex_null(self):
    
  956.         """
    
  957.         A regex lookup does not fail on null/None values
    
  958.         """
    
  959.         Season.objects.create(year=2012, gt=None)
    
  960.         self.assertQuerysetEqual(Season.objects.filter(gt__regex=r"^$"), [])
    
  961. 
    
  962.     def test_regex_non_string(self):
    
  963.         """
    
  964.         A regex lookup does not fail on non-string fields
    
  965.         """
    
  966.         s = Season.objects.create(year=2013, gt=444)
    
  967.         self.assertQuerysetEqual(Season.objects.filter(gt__regex=r"^444$"), [s])
    
  968. 
    
  969.     def test_regex_non_ascii(self):
    
  970.         """
    
  971.         A regex lookup does not trip on non-ASCII characters.
    
  972.         """
    
  973.         Player.objects.create(name="\u2660")
    
  974.         Player.objects.get(name__regex="\u2660")
    
  975. 
    
  976.     def test_nonfield_lookups(self):
    
  977.         """
    
  978.         A lookup query containing non-fields raises the proper exception.
    
  979.         """
    
  980.         msg = (
    
  981.             "Unsupported lookup 'blahblah' for CharField or join on the field not "
    
  982.             "permitted."
    
  983.         )
    
  984.         with self.assertRaisesMessage(FieldError, msg):
    
  985.             Article.objects.filter(headline__blahblah=99)
    
  986.         with self.assertRaisesMessage(FieldError, msg):
    
  987.             Article.objects.filter(headline__blahblah__exact=99)
    
  988.         msg = (
    
  989.             "Cannot resolve keyword 'blahblah' into field. Choices are: "
    
  990.             "author, author_id, headline, id, pub_date, slug, tag"
    
  991.         )
    
  992.         with self.assertRaisesMessage(FieldError, msg):
    
  993.             Article.objects.filter(blahblah=99)
    
  994. 
    
  995.     def test_lookup_collision(self):
    
  996.         """
    
  997.         Genuine field names don't collide with built-in lookup types
    
  998.         ('year', 'gt', 'range', 'in' etc.) (#11670).
    
  999.         """
    
  1000.         # 'gt' is used as a code number for the year, e.g. 111=>2009.
    
  1001.         season_2009 = Season.objects.create(year=2009, gt=111)
    
  1002.         season_2009.games.create(home="Houston Astros", away="St. Louis Cardinals")
    
  1003.         season_2010 = Season.objects.create(year=2010, gt=222)
    
  1004.         season_2010.games.create(home="Houston Astros", away="Chicago Cubs")
    
  1005.         season_2010.games.create(home="Houston Astros", away="Milwaukee Brewers")
    
  1006.         season_2010.games.create(home="Houston Astros", away="St. Louis Cardinals")
    
  1007.         season_2011 = Season.objects.create(year=2011, gt=333)
    
  1008.         season_2011.games.create(home="Houston Astros", away="St. Louis Cardinals")
    
  1009.         season_2011.games.create(home="Houston Astros", away="Milwaukee Brewers")
    
  1010.         hunter_pence = Player.objects.create(name="Hunter Pence")
    
  1011.         hunter_pence.games.set(Game.objects.filter(season__year__in=[2009, 2010]))
    
  1012.         pudge = Player.objects.create(name="Ivan Rodriquez")
    
  1013.         pudge.games.set(Game.objects.filter(season__year=2009))
    
  1014.         pedro_feliz = Player.objects.create(name="Pedro Feliz")
    
  1015.         pedro_feliz.games.set(Game.objects.filter(season__year__in=[2011]))
    
  1016.         johnson = Player.objects.create(name="Johnson")
    
  1017.         johnson.games.set(Game.objects.filter(season__year__in=[2011]))
    
  1018. 
    
  1019.         # Games in 2010
    
  1020.         self.assertEqual(Game.objects.filter(season__year=2010).count(), 3)
    
  1021.         self.assertEqual(Game.objects.filter(season__year__exact=2010).count(), 3)
    
  1022.         self.assertEqual(Game.objects.filter(season__gt=222).count(), 3)
    
  1023.         self.assertEqual(Game.objects.filter(season__gt__exact=222).count(), 3)
    
  1024. 
    
  1025.         # Games in 2011
    
  1026.         self.assertEqual(Game.objects.filter(season__year=2011).count(), 2)
    
  1027.         self.assertEqual(Game.objects.filter(season__year__exact=2011).count(), 2)
    
  1028.         self.assertEqual(Game.objects.filter(season__gt=333).count(), 2)
    
  1029.         self.assertEqual(Game.objects.filter(season__gt__exact=333).count(), 2)
    
  1030.         self.assertEqual(Game.objects.filter(season__year__gt=2010).count(), 2)
    
  1031.         self.assertEqual(Game.objects.filter(season__gt__gt=222).count(), 2)
    
  1032. 
    
  1033.         # Games played in 2010 and 2011
    
  1034.         self.assertEqual(Game.objects.filter(season__year__in=[2010, 2011]).count(), 5)
    
  1035.         self.assertEqual(Game.objects.filter(season__year__gt=2009).count(), 5)
    
  1036.         self.assertEqual(Game.objects.filter(season__gt__in=[222, 333]).count(), 5)
    
  1037.         self.assertEqual(Game.objects.filter(season__gt__gt=111).count(), 5)
    
  1038. 
    
  1039.         # Players who played in 2009
    
  1040.         self.assertEqual(
    
  1041.             Player.objects.filter(games__season__year=2009).distinct().count(), 2
    
  1042.         )
    
  1043.         self.assertEqual(
    
  1044.             Player.objects.filter(games__season__year__exact=2009).distinct().count(), 2
    
  1045.         )
    
  1046.         self.assertEqual(
    
  1047.             Player.objects.filter(games__season__gt=111).distinct().count(), 2
    
  1048.         )
    
  1049.         self.assertEqual(
    
  1050.             Player.objects.filter(games__season__gt__exact=111).distinct().count(), 2
    
  1051.         )
    
  1052. 
    
  1053.         # Players who played in 2010
    
  1054.         self.assertEqual(
    
  1055.             Player.objects.filter(games__season__year=2010).distinct().count(), 1
    
  1056.         )
    
  1057.         self.assertEqual(
    
  1058.             Player.objects.filter(games__season__year__exact=2010).distinct().count(), 1
    
  1059.         )
    
  1060.         self.assertEqual(
    
  1061.             Player.objects.filter(games__season__gt=222).distinct().count(), 1
    
  1062.         )
    
  1063.         self.assertEqual(
    
  1064.             Player.objects.filter(games__season__gt__exact=222).distinct().count(), 1
    
  1065.         )
    
  1066. 
    
  1067.         # Players who played in 2011
    
  1068.         self.assertEqual(
    
  1069.             Player.objects.filter(games__season__year=2011).distinct().count(), 2
    
  1070.         )
    
  1071.         self.assertEqual(
    
  1072.             Player.objects.filter(games__season__year__exact=2011).distinct().count(), 2
    
  1073.         )
    
  1074.         self.assertEqual(
    
  1075.             Player.objects.filter(games__season__gt=333).distinct().count(), 2
    
  1076.         )
    
  1077.         self.assertEqual(
    
  1078.             Player.objects.filter(games__season__year__gt=2010).distinct().count(), 2
    
  1079.         )
    
  1080.         self.assertEqual(
    
  1081.             Player.objects.filter(games__season__gt__gt=222).distinct().count(), 2
    
  1082.         )
    
  1083. 
    
  1084.     def test_chain_date_time_lookups(self):
    
  1085.         self.assertCountEqual(
    
  1086.             Article.objects.filter(pub_date__month__gt=7),
    
  1087.             [self.a5, self.a6],
    
  1088.         )
    
  1089.         self.assertCountEqual(
    
  1090.             Article.objects.filter(pub_date__day__gte=27),
    
  1091.             [self.a2, self.a3, self.a4, self.a7],
    
  1092.         )
    
  1093.         self.assertCountEqual(
    
  1094.             Article.objects.filter(pub_date__hour__lt=8),
    
  1095.             [self.a1, self.a2, self.a3, self.a4, self.a7],
    
  1096.         )
    
  1097.         self.assertCountEqual(
    
  1098.             Article.objects.filter(pub_date__minute__lte=0),
    
  1099.             [self.a1, self.a2, self.a3, self.a4, self.a5, self.a6, self.a7],
    
  1100.         )
    
  1101. 
    
  1102.     def test_exact_none_transform(self):
    
  1103.         """Transforms are used for __exact=None."""
    
  1104.         Season.objects.create(year=1, nulled_text_field="not null")
    
  1105.         self.assertFalse(Season.objects.filter(nulled_text_field__isnull=True))
    
  1106.         self.assertTrue(Season.objects.filter(nulled_text_field__nulled__isnull=True))
    
  1107.         self.assertTrue(Season.objects.filter(nulled_text_field__nulled__exact=None))
    
  1108.         self.assertTrue(Season.objects.filter(nulled_text_field__nulled=None))
    
  1109. 
    
  1110.     def test_exact_sliced_queryset_limit_one(self):
    
  1111.         self.assertCountEqual(
    
  1112.             Article.objects.filter(author=Author.objects.all()[:1]),
    
  1113.             [self.a1, self.a2, self.a3, self.a4],
    
  1114.         )
    
  1115. 
    
  1116.     def test_exact_sliced_queryset_limit_one_offset(self):
    
  1117.         self.assertCountEqual(
    
  1118.             Article.objects.filter(author=Author.objects.all()[1:2]),
    
  1119.             [self.a5, self.a6, self.a7],
    
  1120.         )
    
  1121. 
    
  1122.     def test_exact_sliced_queryset_not_limited_to_one(self):
    
  1123.         msg = (
    
  1124.             "The QuerySet value for an exact lookup must be limited to one "
    
  1125.             "result using slicing."
    
  1126.         )
    
  1127.         with self.assertRaisesMessage(ValueError, msg):
    
  1128.             list(Article.objects.filter(author=Author.objects.all()[:2]))
    
  1129.         with self.assertRaisesMessage(ValueError, msg):
    
  1130.             list(Article.objects.filter(author=Author.objects.all()[1:]))
    
  1131. 
    
  1132.     @skipUnless(connection.vendor == "mysql", "MySQL-specific workaround.")
    
  1133.     def test_exact_booleanfield(self):
    
  1134.         # MySQL ignores indexes with boolean fields unless they're compared
    
  1135.         # directly to a boolean value.
    
  1136.         product = Product.objects.create(name="Paper", qty_target=5000)
    
  1137.         Stock.objects.create(product=product, short=False, qty_available=5100)
    
  1138.         stock_1 = Stock.objects.create(product=product, short=True, qty_available=180)
    
  1139.         qs = Stock.objects.filter(short=True)
    
  1140.         self.assertSequenceEqual(qs, [stock_1])
    
  1141.         self.assertIn(
    
  1142.             "%s = True" % connection.ops.quote_name("short"),
    
  1143.             str(qs.query),
    
  1144.         )
    
  1145. 
    
  1146.     @skipUnless(connection.vendor == "mysql", "MySQL-specific workaround.")
    
  1147.     def test_exact_booleanfield_annotation(self):
    
  1148.         # MySQL ignores indexes with boolean fields unless they're compared
    
  1149.         # directly to a boolean value.
    
  1150.         qs = Author.objects.annotate(
    
  1151.             case=Case(
    
  1152.                 When(alias="a1", then=True),
    
  1153.                 default=False,
    
  1154.                 output_field=BooleanField(),
    
  1155.             )
    
  1156.         ).filter(case=True)
    
  1157.         self.assertSequenceEqual(qs, [self.au1])
    
  1158.         self.assertIn(" = True", str(qs.query))
    
  1159. 
    
  1160.         qs = Author.objects.annotate(
    
  1161.             wrapped=ExpressionWrapper(Q(alias="a1"), output_field=BooleanField()),
    
  1162.         ).filter(wrapped=True)
    
  1163.         self.assertSequenceEqual(qs, [self.au1])
    
  1164.         self.assertIn(" = True", str(qs.query))
    
  1165.         # EXISTS(...) shouldn't be compared to a boolean value.
    
  1166.         qs = Author.objects.annotate(
    
  1167.             exists=Exists(Author.objects.filter(alias="a1", pk=OuterRef("pk"))),
    
  1168.         ).filter(exists=True)
    
  1169.         self.assertSequenceEqual(qs, [self.au1])
    
  1170.         self.assertNotIn(" = True", str(qs.query))
    
  1171. 
    
  1172.     def test_custom_field_none_rhs(self):
    
  1173.         """
    
  1174.         __exact=value is transformed to __isnull=True if Field.get_prep_value()
    
  1175.         converts value to None.
    
  1176.         """
    
  1177.         season = Season.objects.create(year=2012, nulled_text_field=None)
    
  1178.         self.assertTrue(
    
  1179.             Season.objects.filter(pk=season.pk, nulled_text_field__isnull=True)
    
  1180.         )
    
  1181.         self.assertTrue(Season.objects.filter(pk=season.pk, nulled_text_field=""))
    
  1182. 
    
  1183.     def test_pattern_lookups_with_substr(self):
    
  1184.         a = Author.objects.create(name="John Smith", alias="Johx")
    
  1185.         b = Author.objects.create(name="Rhonda Simpson", alias="sonx")
    
  1186.         tests = (
    
  1187.             ("startswith", [a]),
    
  1188.             ("istartswith", [a]),
    
  1189.             ("contains", [a, b]),
    
  1190.             ("icontains", [a, b]),
    
  1191.             ("endswith", [b]),
    
  1192.             ("iendswith", [b]),
    
  1193.         )
    
  1194.         for lookup, result in tests:
    
  1195.             with self.subTest(lookup=lookup):
    
  1196.                 authors = Author.objects.filter(
    
  1197.                     **{"name__%s" % lookup: Substr("alias", 1, 3)}
    
  1198.                 )
    
  1199.                 self.assertCountEqual(authors, result)
    
  1200. 
    
  1201.     def test_custom_lookup_none_rhs(self):
    
  1202.         """Lookup.can_use_none_as_rhs=True allows None as a lookup value."""
    
  1203.         season = Season.objects.create(year=2012, nulled_text_field=None)
    
  1204.         query = Season.objects.get_queryset().query
    
  1205.         field = query.model._meta.get_field("nulled_text_field")
    
  1206.         self.assertIsInstance(
    
  1207.             query.build_lookup(["isnull_none_rhs"], field, None), IsNullWithNoneAsRHS
    
  1208.         )
    
  1209.         self.assertTrue(
    
  1210.             Season.objects.filter(pk=season.pk, nulled_text_field__isnull_none_rhs=True)
    
  1211.         )
    
  1212. 
    
  1213.     def test_exact_exists(self):
    
  1214.         qs = Article.objects.filter(pk=OuterRef("pk"))
    
  1215.         seasons = Season.objects.annotate(
    
  1216.             pk_exists=Exists(qs),
    
  1217.         ).filter(
    
  1218.             pk_exists=Exists(qs),
    
  1219.         )
    
  1220.         self.assertCountEqual(seasons, Season.objects.all())
    
  1221. 
    
  1222.     def test_nested_outerref_lhs(self):
    
  1223.         tag = Tag.objects.create(name=self.au1.alias)
    
  1224.         tag.articles.add(self.a1)
    
  1225.         qs = Tag.objects.annotate(
    
  1226.             has_author_alias_match=Exists(
    
  1227.                 Article.objects.annotate(
    
  1228.                     author_exists=Exists(
    
  1229.                         Author.objects.filter(alias=OuterRef(OuterRef("name")))
    
  1230.                     ),
    
  1231.                 ).filter(author_exists=True)
    
  1232.             ),
    
  1233.         )
    
  1234.         self.assertEqual(qs.get(has_author_alias_match=True), tag)
    
  1235. 
    
  1236.     def test_exact_query_rhs_with_selected_columns(self):
    
  1237.         newest_author = Author.objects.create(name="Author 2")
    
  1238.         authors_max_ids = (
    
  1239.             Author.objects.filter(
    
  1240.                 name="Author 2",
    
  1241.             )
    
  1242.             .values(
    
  1243.                 "name",
    
  1244.             )
    
  1245.             .annotate(
    
  1246.                 max_id=Max("id"),
    
  1247.             )
    
  1248.             .values("max_id")
    
  1249.         )
    
  1250.         authors = Author.objects.filter(id=authors_max_ids[:1])
    
  1251.         self.assertEqual(authors.get(), newest_author)
    
  1252. 
    
  1253.     def test_isnull_non_boolean_value(self):
    
  1254.         msg = "The QuerySet value for an isnull lookup must be True or False."
    
  1255.         tests = [
    
  1256.             Author.objects.filter(alias__isnull=1),
    
  1257.             Article.objects.filter(author__isnull=1),
    
  1258.             Season.objects.filter(games__isnull=1),
    
  1259.             Freebie.objects.filter(stock__isnull=1),
    
  1260.         ]
    
  1261.         for qs in tests:
    
  1262.             with self.subTest(qs=qs):
    
  1263.                 with self.assertRaisesMessage(ValueError, msg):
    
  1264.                     qs.exists()
    
  1265. 
    
  1266.     def test_lookup_rhs(self):
    
  1267.         product = Product.objects.create(name="GME", qty_target=5000)
    
  1268.         stock_1 = Stock.objects.create(product=product, short=True, qty_available=180)
    
  1269.         stock_2 = Stock.objects.create(product=product, short=False, qty_available=5100)
    
  1270.         Stock.objects.create(product=product, short=False, qty_available=4000)
    
  1271.         self.assertCountEqual(
    
  1272.             Stock.objects.filter(short=Q(qty_available__lt=F("product__qty_target"))),
    
  1273.             [stock_1, stock_2],
    
  1274.         )
    
  1275.         self.assertCountEqual(
    
  1276.             Stock.objects.filter(
    
  1277.                 short=ExpressionWrapper(
    
  1278.                     Q(qty_available__lt=F("product__qty_target")),
    
  1279.                     output_field=BooleanField(),
    
  1280.                 )
    
  1281.             ),
    
  1282.             [stock_1, stock_2],
    
  1283.         )
    
  1284. 
    
  1285. 
    
  1286. class LookupQueryingTests(TestCase):
    
  1287.     @classmethod
    
  1288.     def setUpTestData(cls):
    
  1289.         cls.s1 = Season.objects.create(year=1942, gt=1942)
    
  1290.         cls.s2 = Season.objects.create(year=1842, gt=1942, nulled_text_field="text")
    
  1291.         cls.s3 = Season.objects.create(year=2042, gt=1942)
    
  1292. 
    
  1293.     def test_annotate(self):
    
  1294.         qs = Season.objects.annotate(equal=Exact(F("year"), 1942))
    
  1295.         self.assertCountEqual(
    
  1296.             qs.values_list("year", "equal"),
    
  1297.             ((1942, True), (1842, False), (2042, False)),
    
  1298.         )
    
  1299. 
    
  1300.     def test_alias(self):
    
  1301.         qs = Season.objects.alias(greater=GreaterThan(F("year"), 1910))
    
  1302.         self.assertCountEqual(qs.filter(greater=True), [self.s1, self.s3])
    
  1303. 
    
  1304.     def test_annotate_value_greater_than_value(self):
    
  1305.         qs = Season.objects.annotate(greater=GreaterThan(Value(40), Value(30)))
    
  1306.         self.assertCountEqual(
    
  1307.             qs.values_list("year", "greater"),
    
  1308.             ((1942, True), (1842, True), (2042, True)),
    
  1309.         )
    
  1310. 
    
  1311.     def test_annotate_field_greater_than_field(self):
    
  1312.         qs = Season.objects.annotate(greater=GreaterThan(F("year"), F("gt")))
    
  1313.         self.assertCountEqual(
    
  1314.             qs.values_list("year", "greater"),
    
  1315.             ((1942, False), (1842, False), (2042, True)),
    
  1316.         )
    
  1317. 
    
  1318.     def test_annotate_field_greater_than_value(self):
    
  1319.         qs = Season.objects.annotate(greater=GreaterThan(F("year"), Value(1930)))
    
  1320.         self.assertCountEqual(
    
  1321.             qs.values_list("year", "greater"),
    
  1322.             ((1942, True), (1842, False), (2042, True)),
    
  1323.         )
    
  1324. 
    
  1325.     def test_annotate_field_greater_than_literal(self):
    
  1326.         qs = Season.objects.annotate(greater=GreaterThan(F("year"), 1930))
    
  1327.         self.assertCountEqual(
    
  1328.             qs.values_list("year", "greater"),
    
  1329.             ((1942, True), (1842, False), (2042, True)),
    
  1330.         )
    
  1331. 
    
  1332.     def test_annotate_literal_greater_than_field(self):
    
  1333.         qs = Season.objects.annotate(greater=GreaterThan(1930, F("year")))
    
  1334.         self.assertCountEqual(
    
  1335.             qs.values_list("year", "greater"),
    
  1336.             ((1942, False), (1842, True), (2042, False)),
    
  1337.         )
    
  1338. 
    
  1339.     def test_annotate_less_than_float(self):
    
  1340.         qs = Season.objects.annotate(lesser=LessThan(F("year"), 1942.1))
    
  1341.         self.assertCountEqual(
    
  1342.             qs.values_list("year", "lesser"),
    
  1343.             ((1942, True), (1842, True), (2042, False)),
    
  1344.         )
    
  1345. 
    
  1346.     def test_annotate_greater_than_or_equal(self):
    
  1347.         qs = Season.objects.annotate(greater=GreaterThanOrEqual(F("year"), 1942))
    
  1348.         self.assertCountEqual(
    
  1349.             qs.values_list("year", "greater"),
    
  1350.             ((1942, True), (1842, False), (2042, True)),
    
  1351.         )
    
  1352. 
    
  1353.     def test_annotate_greater_than_or_equal_float(self):
    
  1354.         qs = Season.objects.annotate(greater=GreaterThanOrEqual(F("year"), 1942.1))
    
  1355.         self.assertCountEqual(
    
  1356.             qs.values_list("year", "greater"),
    
  1357.             ((1942, False), (1842, False), (2042, True)),
    
  1358.         )
    
  1359. 
    
  1360.     def test_combined_lookups(self):
    
  1361.         expression = Exact(F("year"), 1942) | GreaterThan(F("year"), 1942)
    
  1362.         qs = Season.objects.annotate(gte=expression)
    
  1363.         self.assertCountEqual(
    
  1364.             qs.values_list("year", "gte"),
    
  1365.             ((1942, True), (1842, False), (2042, True)),
    
  1366.         )
    
  1367. 
    
  1368.     def test_lookup_in_filter(self):
    
  1369.         qs = Season.objects.filter(GreaterThan(F("year"), 1910))
    
  1370.         self.assertCountEqual(qs, [self.s1, self.s3])
    
  1371. 
    
  1372.     def test_isnull_lookup_in_filter(self):
    
  1373.         self.assertSequenceEqual(
    
  1374.             Season.objects.filter(IsNull(F("nulled_text_field"), False)),
    
  1375.             [self.s2],
    
  1376.         )
    
  1377.         self.assertCountEqual(
    
  1378.             Season.objects.filter(IsNull(F("nulled_text_field"), True)),
    
  1379.             [self.s1, self.s3],
    
  1380.         )
    
  1381. 
    
  1382.     def test_filter_lookup_lhs(self):
    
  1383.         qs = Season.objects.annotate(before_20=LessThan(F("year"), 2000)).filter(
    
  1384.             before_20=LessThan(F("year"), 1900),
    
  1385.         )
    
  1386.         self.assertCountEqual(qs, [self.s2, self.s3])
    
  1387. 
    
  1388.     def test_filter_wrapped_lookup_lhs(self):
    
  1389.         qs = (
    
  1390.             Season.objects.annotate(
    
  1391.                 before_20=ExpressionWrapper(
    
  1392.                     Q(year__lt=2000),
    
  1393.                     output_field=BooleanField(),
    
  1394.                 )
    
  1395.             )
    
  1396.             .filter(before_20=LessThan(F("year"), 1900))
    
  1397.             .values_list("year", flat=True)
    
  1398.         )
    
  1399.         self.assertCountEqual(qs, [1842, 2042])
    
  1400. 
    
  1401.     def test_filter_exists_lhs(self):
    
  1402.         qs = Season.objects.annotate(
    
  1403.             before_20=Exists(
    
  1404.                 Season.objects.filter(pk=OuterRef("pk"), year__lt=2000),
    
  1405.             )
    
  1406.         ).filter(before_20=LessThan(F("year"), 1900))
    
  1407.         self.assertCountEqual(qs, [self.s2, self.s3])
    
  1408. 
    
  1409.     def test_filter_subquery_lhs(self):
    
  1410.         qs = Season.objects.annotate(
    
  1411.             before_20=Subquery(
    
  1412.                 Season.objects.filter(pk=OuterRef("pk")).values(
    
  1413.                     lesser=LessThan(F("year"), 2000),
    
  1414.                 ),
    
  1415.             )
    
  1416.         ).filter(before_20=LessThan(F("year"), 1900))
    
  1417.         self.assertCountEqual(qs, [self.s2, self.s3])
    
  1418. 
    
  1419.     def test_combined_lookups_in_filter(self):
    
  1420.         expression = Exact(F("year"), 1942) | GreaterThan(F("year"), 1942)
    
  1421.         qs = Season.objects.filter(expression)
    
  1422.         self.assertCountEqual(qs, [self.s1, self.s3])
    
  1423. 
    
  1424.     def test_combined_annotated_lookups_in_filter(self):
    
  1425.         expression = Exact(F("year"), 1942) | GreaterThan(F("year"), 1942)
    
  1426.         qs = Season.objects.annotate(gte=expression).filter(gte=True)
    
  1427.         self.assertCountEqual(qs, [self.s1, self.s3])
    
  1428. 
    
  1429.     def test_combined_annotated_lookups_in_filter_false(self):
    
  1430.         expression = Exact(F("year"), 1942) | GreaterThan(F("year"), 1942)
    
  1431.         qs = Season.objects.annotate(gte=expression).filter(gte=False)
    
  1432.         self.assertSequenceEqual(qs, [self.s2])
    
  1433. 
    
  1434.     def test_lookup_in_order_by(self):
    
  1435.         qs = Season.objects.order_by(LessThan(F("year"), 1910), F("year"))
    
  1436.         self.assertSequenceEqual(qs, [self.s1, self.s3, self.s2])
    
  1437. 
    
  1438.     @skipUnlessDBFeature("supports_boolean_expr_in_select_clause")
    
  1439.     def test_aggregate_combined_lookup(self):
    
  1440.         expression = Cast(GreaterThan(F("year"), 1900), models.IntegerField())
    
  1441.         qs = Season.objects.aggregate(modern=models.Sum(expression))
    
  1442.         self.assertEqual(qs["modern"], 2)
    
  1443. 
    
  1444.     def test_conditional_expression(self):
    
  1445.         qs = Season.objects.annotate(
    
  1446.             century=Case(
    
  1447.                 When(
    
  1448.                     GreaterThan(F("year"), 1900) & LessThanOrEqual(F("year"), 2000),
    
  1449.                     then=Value("20th"),
    
  1450.                 ),
    
  1451.                 default=Value("other"),
    
  1452.             )
    
  1453.         ).values("year", "century")
    
  1454.         self.assertCountEqual(
    
  1455.             qs,
    
  1456.             [
    
  1457.                 {"year": 1942, "century": "20th"},
    
  1458.                 {"year": 1842, "century": "other"},
    
  1459.                 {"year": 2042, "century": "other"},
    
  1460.             ],
    
  1461.         )