|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | +from django.core.management import call_command |
| 5 | +from django.core.validators import MaxValueValidator |
| 6 | +from django.core.validators import MinValueValidator |
| 7 | +from django.db import connection |
| 8 | +from django.db.utils import DataError |
| 9 | +from django.test import TestCase |
| 10 | +from django.test import TransactionTestCase |
| 11 | +from django.test import override_settings |
| 12 | + |
| 13 | +from tests.testapp.models import TinyIntegerModel |
| 14 | + |
| 15 | + |
| 16 | +class TestSaveLoad(TestCase): |
| 17 | + def test_success(self): |
| 18 | + TinyIntegerModel.objects.create(tiny_signed=-128, tiny_unsigned=0) |
| 19 | + TinyIntegerModel.objects.create(tiny_signed=127, tiny_unsigned=255) |
| 20 | + |
| 21 | + def test_invalid_too_long_signed(self): |
| 22 | + with pytest.raises(DataError) as excinfo: |
| 23 | + TinyIntegerModel.objects.create(tiny_signed=128) |
| 24 | + |
| 25 | + assert excinfo.value.args == ( |
| 26 | + 1264, |
| 27 | + "Out of range value for column 'tiny_signed' at row 1", |
| 28 | + ) |
| 29 | + |
| 30 | + def test_invalid_too_long_unsigned(self): |
| 31 | + with pytest.raises(DataError) as excinfo: |
| 32 | + TinyIntegerModel.objects.create(tiny_unsigned=256) |
| 33 | + |
| 34 | + assert excinfo.value.args == ( |
| 35 | + 1264, |
| 36 | + "Out of range value for column 'tiny_unsigned' at row 1", |
| 37 | + ) |
| 38 | + |
| 39 | + def test_invalid_too_short_signed(self): |
| 40 | + with pytest.raises(DataError) as excinfo: |
| 41 | + TinyIntegerModel.objects.create(tiny_signed=-129) |
| 42 | + |
| 43 | + assert excinfo.value.args == ( |
| 44 | + 1264, |
| 45 | + "Out of range value for column 'tiny_signed' at row 1", |
| 46 | + ) |
| 47 | + |
| 48 | + def test_invalid_too_short_unsigned(self): |
| 49 | + with pytest.raises(DataError) as excinfo: |
| 50 | + TinyIntegerModel.objects.create(tiny_unsigned=-1) |
| 51 | + |
| 52 | + assert excinfo.value.args == ( |
| 53 | + 1264, |
| 54 | + "Out of range value for column 'tiny_unsigned' at row 1", |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +class TestMigrations(TransactionTestCase): |
| 59 | + @override_settings( |
| 60 | + MIGRATION_MODULES={"testapp": "tests.testapp.tinyinteger_default_migrations"} |
| 61 | + ) |
| 62 | + def test_adding_field_with_default(self): |
| 63 | + table_name = "testapp_tinyintegerdefaultmodel" |
| 64 | + table_names = connection.introspection.table_names |
| 65 | + with connection.cursor() as cursor: |
| 66 | + assert table_name not in table_names(cursor) |
| 67 | + |
| 68 | + call_command( |
| 69 | + "migrate", "testapp", verbosity=0, skip_checks=True, interactive=False |
| 70 | + ) |
| 71 | + with connection.cursor() as cursor: |
| 72 | + assert table_name in table_names(cursor) |
| 73 | + |
| 74 | + call_command( |
| 75 | + "migrate", |
| 76 | + "testapp", |
| 77 | + "zero", |
| 78 | + verbosity=0, |
| 79 | + skip_checks=True, |
| 80 | + interactive=False, |
| 81 | + ) |
| 82 | + with connection.cursor() as cursor: |
| 83 | + assert table_name not in table_names(cursor) |
| 84 | + |
| 85 | + |
| 86 | +class TestFormValidation(TestCase): |
| 87 | + def test_signed_validators(self): |
| 88 | + validators = TinyIntegerModel._meta.get_field("tiny_signed").validators |
| 89 | + assert len(validators) == 2 |
| 90 | + assert isinstance(validators[0], MinValueValidator) |
| 91 | + assert validators[0].limit_value == -128 |
| 92 | + assert isinstance(validators[1], MaxValueValidator) |
| 93 | + assert validators[1].limit_value == 127 |
| 94 | + |
| 95 | + def test_unsigned_validators(self): |
| 96 | + validators = TinyIntegerModel._meta.get_field("tiny_unsigned").validators |
| 97 | + assert len(validators) == 2 |
| 98 | + assert isinstance(validators[0], MinValueValidator) |
| 99 | + assert validators[0].limit_value == 0 |
| 100 | + assert isinstance(validators[1], MaxValueValidator) |
| 101 | + assert validators[1].limit_value == 255 |
0 commit comments