summaryrefslogtreecommitdiff
path: root/tests/model_fields/test_autofield.py
blob: 73220ae62b61c29963221725d3fe343455d08dab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from django.test import TestCase

from .models import AutoModel, BigAutoModel, SmallAutoModel


class AutoFieldTests(TestCase):
    model = AutoModel

    def test_invalid_value(self):
        tests = [
            (TypeError, ()),
            (TypeError, []),
            (TypeError, {}),
            (TypeError, set()),
            (TypeError, object()),
            (TypeError, complex()),
            (ValueError, 'non-numeric string'),
            (ValueError, b'non-numeric byte-string'),
        ]
        for exception, value in tests:
            with self.subTest(value=value):
                msg = "Field 'value' expected a number but got %r." % (value,)
                with self.assertRaisesMessage(exception, msg):
                    self.model.objects.create(value=value)


class BigAutoFieldTests(AutoFieldTests):
    model = BigAutoModel


class SmallAutoFieldTests(AutoFieldTests):
    model = SmallAutoModel