| 1 |
from datetime import datetime |
| 2 |
from unittest import mock |
| 3 |
|
| 4 |
from django.db.models import DateTimeField, Value |
| 5 |
from django.db.models.lookups import Lookup, YearLookup |
| 6 |
from django.test import SimpleTestCase |
| 7 |
|
| 8 |
|
| 9 |
class CustomLookup(Lookup): |
| 10 |
pass |
| 11 |
|
| 12 |
|
| 13 |
class LookupTests(SimpleTestCase): |
| 14 |
def test_equality(self): |
| 15 |
lookup = Lookup(Value(1), Value(2)) |
| 16 |
self.assertEqual(lookup, lookup) |
| 17 |
self.assertEqual(lookup, Lookup(lookup.lhs, lookup.rhs)) |
| 18 |
self.assertEqual(lookup, mock.ANY) |
| 19 |
self.assertNotEqual(lookup, Lookup(lookup.lhs, Value(3))) |
| 20 |
self.assertNotEqual(lookup, Lookup(Value(3), lookup.rhs)) |
| 21 |
self.assertNotEqual(lookup, CustomLookup(lookup.lhs, lookup.rhs)) |
| 22 |
|
| 23 |
def test_repr(self): |
| 24 |
tests = [ |
| 25 |
(Lookup(Value(1), Value("a")), "Lookup(Value(1), Value('a'))"), |
| 26 |
( |
| 27 |
YearLookup( |
| 28 |
Value(datetime(2010, 1, 1, 0, 0, 0)), |
| 29 |
Value(datetime(2010, 1, 1, 23, 59, 59)), |
| 30 |
), |
| 31 |
"YearLookup(" |
| 32 |
"Value(datetime.datetime(2010, 1, 1, 0, 0)), " |
| 33 |
"Value(datetime.datetime(2010, 1, 1, 23, 59, 59)))", |
| 34 |
), |
| 35 |
] |
| 36 |
for lookup, expected in tests: |
| 37 |
with self.subTest(lookup=lookup): |
| 38 |
self.assertEqual(repr(lookup), expected) |
| 39 |
|
| 40 |
def test_hash(self): |
| 41 |
lookup = Lookup(Value(1), Value(2)) |
| 42 |
self.assertEqual(hash(lookup), hash(lookup)) |
| 43 |
self.assertEqual(hash(lookup), hash(Lookup(lookup.lhs, lookup.rhs))) |
| 44 |
self.assertNotEqual(hash(lookup), hash(Lookup(lookup.lhs, Value(3)))) |
| 45 |
self.assertNotEqual(hash(lookup), hash(Lookup(Value(3), lookup.rhs))) |
| 46 |
self.assertNotEqual(hash(lookup), hash(CustomLookup(lookup.lhs, lookup.rhs))) |
| 47 |
|
| 48 |
|
| 49 |
class YearLookupTests(SimpleTestCase): |
| 50 |
def test_get_bound_params(self): |
| 51 |
look_up = YearLookup( |
| 52 |
lhs=Value(datetime(2010, 1, 1, 0, 0, 0), output_field=DateTimeField()), |
| 53 |
rhs=Value(datetime(2010, 1, 1, 23, 59, 59), output_field=DateTimeField()), |
| 54 |
) |
| 55 |
msg = "subclasses of YearLookup must provide a get_bound_params() method" |
| 56 |
with self.assertRaisesMessage(NotImplementedError, msg): |
| 57 |
look_up.get_bound_params( |
| 58 |
datetime(2010, 1, 1, 0, 0, 0), datetime(2010, 1, 1, 23, 59, 59) |
| 59 |
) |