diff options
| author | Simon Charette <charette.s@gmail.com> | 2021-04-22 13:44:25 -0400 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-04-23 15:38:32 +0200 |
| commit | 170b006ce82b0ecf26dc088f832538b747ca0115 (patch) | |
| tree | d46af32220ce9ad7246fad075f0244a300d6473c /tests/lookup | |
| parent | 67bb1f516cf507feb141fd4ef746456e1ef67c4a (diff) | |
Fixed #32673 -- Fixed lookups crash when comparing against lookups on PostgreSQL.
Regression in 3a505c70e7b228bf1212c067a8f38271ca86ce09.
Nonlitteral right-hand-sides of lookups need to be wrapped in
parentheses to avoid operator precedence ambiguities.
Thanks Charles Lirsac for the detailed report.
Diffstat (limited to 'tests/lookup')
| -rw-r--r-- | tests/lookup/models.py | 1 | ||||
| -rw-r--r-- | tests/lookup/tests.py | 24 |
2 files changed, 23 insertions, 2 deletions
diff --git a/tests/lookup/models.py b/tests/lookup/models.py index 6a0afaacde..2343c850c1 100644 --- a/tests/lookup/models.py +++ b/tests/lookup/models.py @@ -94,6 +94,7 @@ class Product(models.Model): class Stock(models.Model): product = models.ForeignKey(Product, models.CASCADE) + short = models.BooleanField(default=False) qty_available = models.DecimalField(max_digits=6, decimal_places=2) diff --git a/tests/lookup/tests.py b/tests/lookup/tests.py index 28ddc13aa0..e38eae087c 100644 --- a/tests/lookup/tests.py +++ b/tests/lookup/tests.py @@ -5,13 +5,16 @@ from operator import attrgetter from django.core.exceptions import FieldError from django.db import connection, models -from django.db.models import Exists, Max, OuterRef +from django.db.models import ( + BooleanField, Exists, ExpressionWrapper, F, Max, OuterRef, Q, +) from django.db.models.functions import Substr from django.test import TestCase, skipUnlessDBFeature from django.test.utils import isolate_apps from .models import ( - Article, Author, Freebie, Game, IsNullWithNoneAsRHS, Player, Season, Tag, + Article, Author, Freebie, Game, IsNullWithNoneAsRHS, Player, Product, + Season, Stock, Tag, ) @@ -1000,3 +1003,20 @@ class LookupTests(TestCase): with self.subTest(qs=qs): with self.assertRaisesMessage(ValueError, msg): qs.exists() + + def test_lookup_rhs(self): + product = Product.objects.create(name='GME', qty_target=5000) + stock_1 = Stock.objects.create(product=product, short=True, qty_available=180) + stock_2 = Stock.objects.create(product=product, short=False, qty_available=5100) + Stock.objects.create(product=product, short=False, qty_available=4000) + self.assertCountEqual( + Stock.objects.filter(short=Q(qty_available__lt=F('product__qty_target'))), + [stock_1, stock_2], + ) + self.assertCountEqual( + Stock.objects.filter(short=ExpressionWrapper( + Q(qty_available__lt=F('product__qty_target')), + output_field=BooleanField(), + )), + [stock_1, stock_2], + ) |
