summaryrefslogtreecommitdiff
path: root/tests/postgres_tests/test_introspection.py
blob: 1ae940fdb91a987ef26f89d3c8827050099be06a (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from io import StringIO

from django.core.management import call_command

from . import PostgreSQLTestCase


class InspectDBTests(PostgreSQLTestCase):
    def assertFieldsInModel(self, model, field_outputs):
        out = StringIO()
        call_command(
            "inspectdb",
            table_name_filter=lambda tn: tn.startswith(model),
            stdout=out,
        )
        output = out.getvalue()
        for field_output in field_outputs:
            self.assertIn(field_output, output)

    def test_range_fields(self):
        self.assertFieldsInModel(
            "postgres_tests_rangesmodel",
            [
                "ints = django.contrib.postgres.fields.IntegerRangeField(blank=True, "
                "null=True)",
                "bigints = django.contrib.postgres.fields.BigIntegerRangeField("
                "blank=True, null=True)",
                "decimals = django.contrib.postgres.fields.DecimalRangeField("
                "blank=True, null=True)",
                "timestamps = django.contrib.postgres.fields.DateTimeRangeField("
                "blank=True, null=True)",
                "dates = django.contrib.postgres.fields.DateRangeField(blank=True, "
                "null=True)",
            ],
        )

    def test_hstore_field(self):
        from django.db.backends.postgresql.base import psycopg_version

        if psycopg_version() < (3, 2):
            self.skipTest("psycopg 3.2+ is required.")
        self.assertFieldsInModel(
            "postgres_tests_hstoremodel",
            [
                "field = django.contrib.postgres.fields.HStoreField(blank=True, "
                "null=True)",
            ],
        )