|
| 1 | +import django |
| 2 | +import pytest |
| 3 | + |
| 4 | +from django.test import TestCase |
| 5 | + |
| 6 | +from localized_fields.fields import LocalizedField |
| 7 | +from localized_fields.value import LocalizedValue |
| 8 | + |
| 9 | +from .fake_model import get_fake_model |
| 10 | + |
| 11 | + |
| 12 | +class LocalizedIsNullLookupsTestCase(TestCase): |
| 13 | + """Tests whether ref lookups properly work with.""" |
| 14 | + |
| 15 | + TestModel1 = None |
| 16 | + |
| 17 | + @classmethod |
| 18 | + def setUpClass(cls): |
| 19 | + """Creates the test model in the database.""" |
| 20 | + super(LocalizedIsNullLookupsTestCase, cls).setUpClass() |
| 21 | + cls.TestModel = get_fake_model( |
| 22 | + {"text": LocalizedField(null=True, required=[])} |
| 23 | + ) |
| 24 | + cls.TestModel.objects.create( |
| 25 | + text=LocalizedValue(dict(en="text_en", ro="text_ro", nl="text_nl")) |
| 26 | + ) |
| 27 | + cls.TestModel.objects.create( |
| 28 | + text=None, |
| 29 | + ) |
| 30 | + |
| 31 | + def test_isnull_lookup_valid_values(self): |
| 32 | + """Test whether isnull properly works with valid values.""" |
| 33 | + assert self.TestModel.objects.filter(text__isnull=True).exists() |
| 34 | + assert self.TestModel.objects.filter(text__isnull=False).exists() |
| 35 | + |
| 36 | + def test_isnull_lookup_null(self): |
| 37 | + """Test whether isnull crashes with None as value.""" |
| 38 | + |
| 39 | + with pytest.raises(ValueError): |
| 40 | + assert self.TestModel.objects.filter(text__isnull=None).exists() |
| 41 | + |
| 42 | + def test_isnull_lookup_string(self): |
| 43 | + """Test whether isnull properly works with string values on the |
| 44 | + corresponding Django version.""" |
| 45 | + if django.VERSION < (4, 0): |
| 46 | + assert self.TestModel.objects.filter(text__isnull="True").exists() |
| 47 | + else: |
| 48 | + with pytest.raises(ValueError): |
| 49 | + assert self.TestModel.objects.filter( |
| 50 | + text__isnull="True" |
| 51 | + ).exists() |
0 commit comments