summaryrefslogtreecommitdiff
path: root/tests/expressions
diff options
context:
space:
mode:
authorTobias Bengfort <tobias.bengfort@posteo.de>2021-04-06 18:14:16 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-04-20 11:44:41 +0200
commit54e94640ace261b14cf8cdb1fae3dc6f068a5f87 (patch)
tree12e432a1a539303dd51ba2691e87b3aaba03a338 /tests/expressions
parent9e1ccd7283e8544d86cba35c820a7d741f5d2712 (diff)
downloaddjango-54e94640ace261b14cf8cdb1fae3dc6f068a5f87.tar.gz
Refs #25287 -- Added support for multiplying and dividing DurationField by scalar values on SQLite.
Diffstat (limited to 'tests/expressions')
-rw-r--r--tests/expressions/models.py1
-rw-r--r--tests/expressions/tests.py30
2 files changed, 31 insertions, 0 deletions
diff --git a/tests/expressions/models.py b/tests/expressions/models.py
index 02836e653e..938e623d60 100644
--- a/tests/expressions/models.py
+++ b/tests/expressions/models.py
@@ -60,6 +60,7 @@ class Experiment(models.Model):
estimated_time = models.DurationField()
start = models.DateTimeField()
end = models.DateTimeField()
+ scalar = models.IntegerField(null=True)
class Meta:
db_table = 'expressions_ExPeRiMeNt'
diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py
index 9b88c94d7b..0585805a8b 100644
--- a/tests/expressions/tests.py
+++ b/tests/expressions/tests.py
@@ -1530,6 +1530,36 @@ class FTimeDeltaTests(TestCase):
))
self.assertIsNone(queryset.first().shifted)
+ def test_durationfield_multiply_divide(self):
+ Experiment.objects.update(scalar=2)
+ tests = [
+ (Decimal('2'), 2),
+ (F('scalar'), 2),
+ (2, 2),
+ (3.2, 3.2),
+ ]
+ for expr, scalar in tests:
+ with self.subTest(expr=expr):
+ qs = Experiment.objects.annotate(
+ multiplied=ExpressionWrapper(
+ expr * F('estimated_time'),
+ output_field=DurationField(),
+ ),
+ divided=ExpressionWrapper(
+ F('estimated_time') / expr,
+ output_field=DurationField(),
+ ),
+ )
+ for experiment in qs:
+ self.assertEqual(
+ experiment.multiplied,
+ experiment.estimated_time * scalar,
+ )
+ self.assertEqual(
+ experiment.divided,
+ experiment.estimated_time / scalar,
+ )
+
def test_duration_expressions(self):
for delta in self.deltas:
qs = Experiment.objects.annotate(duration=F('estimated_time') + delta)