2017-09-04 16:17:51 +02:00
|
|
|
from datetime import datetime
|
2020-10-25 22:41:06 +01:00
|
|
|
from unittest import mock
|
2017-09-04 16:17:51 +02:00
|
|
|
|
2019-08-20 09:54:41 +02:00
|
|
|
from django.db.models import DateTimeField, Value
|
2020-10-25 22:41:06 +01:00
|
|
|
from django.db.models.lookups import Lookup, YearLookup
|
2017-09-04 16:17:51 +02:00
|
|
|
from django.test import SimpleTestCase
|
|
|
|
|
|
|
|
|
2020-10-25 22:41:06 +01:00
|
|
|
class CustomLookup(Lookup):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class LookupTests(SimpleTestCase):
|
|
|
|
def test_equality(self):
|
|
|
|
lookup = Lookup(Value(1), Value(2))
|
|
|
|
self.assertEqual(lookup, lookup)
|
|
|
|
self.assertEqual(lookup, Lookup(lookup.lhs, lookup.rhs))
|
|
|
|
self.assertEqual(lookup, mock.ANY)
|
|
|
|
self.assertNotEqual(lookup, Lookup(lookup.lhs, Value(3)))
|
|
|
|
self.assertNotEqual(lookup, Lookup(Value(3), lookup.rhs))
|
|
|
|
self.assertNotEqual(lookup, CustomLookup(lookup.lhs, lookup.rhs))
|
|
|
|
|
2021-05-11 06:02:25 +02:00
|
|
|
def test_repr(self):
|
|
|
|
tests = [
|
|
|
|
(Lookup(Value(1), Value("a")), "Lookup(Value(1), Value('a'))"),
|
|
|
|
(
|
|
|
|
YearLookup(
|
|
|
|
Value(datetime(2010, 1, 1, 0, 0, 0)),
|
|
|
|
Value(datetime(2010, 1, 1, 23, 59, 59)),
|
|
|
|
),
|
|
|
|
"YearLookup("
|
|
|
|
"Value(datetime.datetime(2010, 1, 1, 0, 0)), "
|
|
|
|
"Value(datetime.datetime(2010, 1, 1, 23, 59, 59)))",
|
|
|
|
),
|
|
|
|
]
|
|
|
|
for lookup, expected in tests:
|
|
|
|
with self.subTest(lookup=lookup):
|
|
|
|
self.assertEqual(repr(lookup), expected)
|
|
|
|
|
2020-10-25 22:41:06 +01:00
|
|
|
def test_hash(self):
|
|
|
|
lookup = Lookup(Value(1), Value(2))
|
|
|
|
self.assertEqual(hash(lookup), hash(lookup))
|
|
|
|
self.assertEqual(hash(lookup), hash(Lookup(lookup.lhs, lookup.rhs)))
|
|
|
|
self.assertNotEqual(hash(lookup), hash(Lookup(lookup.lhs, Value(3))))
|
|
|
|
self.assertNotEqual(hash(lookup), hash(Lookup(Value(3), lookup.rhs)))
|
|
|
|
self.assertNotEqual(hash(lookup), hash(CustomLookup(lookup.lhs, lookup.rhs)))
|
|
|
|
|
|
|
|
|
2019-05-21 01:58:11 +02:00
|
|
|
class YearLookupTests(SimpleTestCase):
|
|
|
|
def test_get_bound_params(self):
|
|
|
|
look_up = YearLookup(
|
2017-09-04 16:17:51 +02:00
|
|
|
lhs=Value(datetime(2010, 1, 1, 0, 0, 0), output_field=DateTimeField()),
|
|
|
|
rhs=Value(datetime(2010, 1, 1, 23, 59, 59), output_field=DateTimeField()),
|
|
|
|
)
|
2019-05-21 01:58:11 +02:00
|
|
|
msg = "subclasses of YearLookup must provide a get_bound_params() method"
|
2017-09-04 16:17:51 +02:00
|
|
|
with self.assertRaisesMessage(NotImplementedError, msg):
|
2019-05-21 01:58:11 +02:00
|
|
|
look_up.get_bound_params(
|
|
|
|
datetime(2010, 1, 1, 0, 0, 0), datetime(2010, 1, 1, 23, 59, 59)
|
2022-02-03 20:24:19 +01:00
|
|
|
)
|