2022-12-01 16:39:46 +08:00
|
|
|
import unittest
|
2021-11-04 17:36:11 +08:00
|
|
|
from decimal import Decimal
|
|
|
|
|
2022-12-01 16:39:46 +08:00
|
|
|
from django.db import connection
|
2018-11-13 00:15:48 +08:00
|
|
|
from django.db.backends.signals import connection_created
|
2019-01-11 02:06:14 +08:00
|
|
|
from django.db.migrations.writer import MigrationWriter
|
2022-12-01 16:39:46 +08:00
|
|
|
from django.test import TestCase
|
2018-11-13 00:15:48 +08:00
|
|
|
from django.test.utils import modify_settings
|
|
|
|
|
2019-01-11 02:06:14 +08:00
|
|
|
try:
|
|
|
|
from django.contrib.postgres.fields import (
|
2021-11-04 17:36:11 +08:00
|
|
|
DateRangeField,
|
|
|
|
DateTimeRangeField,
|
|
|
|
DecimalRangeField,
|
|
|
|
IntegerRangeField,
|
2019-01-11 02:06:14 +08:00
|
|
|
)
|
2022-12-12 15:25:05 +08:00
|
|
|
from django.db.backends.postgresql.psycopg_any import (
|
|
|
|
DateRange,
|
|
|
|
DateTimeRange,
|
|
|
|
DateTimeTZRange,
|
|
|
|
NumericRange,
|
2022-12-02 03:23:43 +08:00
|
|
|
is_psycopg3,
|
2022-12-12 15:25:05 +08:00
|
|
|
)
|
2019-01-11 02:06:14 +08:00
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2018-11-13 00:15:48 +08:00
|
|
|
|
2022-12-01 16:39:46 +08:00
|
|
|
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific tests")
|
|
|
|
class PostgresConfigTests(TestCase):
|
2018-11-13 00:15:48 +08:00
|
|
|
def test_register_type_handlers_connection(self):
|
|
|
|
from django.contrib.postgres.signals import register_type_handlers
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2018-11-13 00:15:48 +08:00
|
|
|
self.assertNotIn(
|
|
|
|
register_type_handlers, connection_created._live_receivers(None)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2018-11-13 00:15:48 +08:00
|
|
|
with modify_settings(INSTALLED_APPS={"append": "django.contrib.postgres"}):
|
|
|
|
self.assertIn(
|
|
|
|
register_type_handlers, connection_created._live_receivers(None)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2018-11-13 00:15:48 +08:00
|
|
|
self.assertNotIn(
|
|
|
|
register_type_handlers, connection_created._live_receivers(None)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2019-01-11 02:06:14 +08:00
|
|
|
|
|
|
|
def test_register_serializer_for_migrations(self):
|
|
|
|
tests = (
|
|
|
|
(DateRange(empty=True), DateRangeField),
|
|
|
|
(DateTimeRange(empty=True), DateRangeField),
|
|
|
|
(DateTimeTZRange(None, None, "[]"), DateTimeRangeField),
|
2021-11-04 17:36:11 +08:00
|
|
|
(NumericRange(Decimal("1.0"), Decimal("5.0"), "()"), DecimalRangeField),
|
2019-01-11 02:06:14 +08:00
|
|
|
(NumericRange(1, 10), IntegerRangeField),
|
|
|
|
)
|
|
|
|
|
|
|
|
def assertNotSerializable():
|
|
|
|
for default, test_field in tests:
|
|
|
|
with self.subTest(default=default):
|
|
|
|
field = test_field(default=default)
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ValueError, "Cannot serialize: %s" % default.__class__.__name__
|
|
|
|
):
|
|
|
|
MigrationWriter.serialize(field)
|
|
|
|
|
|
|
|
assertNotSerializable()
|
2022-12-02 03:23:43 +08:00
|
|
|
import_name = "psycopg.types.range" if is_psycopg3 else "psycopg2.extras"
|
2019-01-11 02:06:14 +08:00
|
|
|
with self.modify_settings(INSTALLED_APPS={"append": "django.contrib.postgres"}):
|
|
|
|
for default, test_field in tests:
|
|
|
|
with self.subTest(default=default):
|
|
|
|
field = test_field(default=default)
|
|
|
|
serialized_field, imports = MigrationWriter.serialize(field)
|
|
|
|
self.assertEqual(
|
|
|
|
imports,
|
|
|
|
{
|
|
|
|
"import django.contrib.postgres.fields.ranges",
|
2022-12-02 03:23:43 +08:00
|
|
|
f"import {import_name}",
|
2019-01-11 02:06:14 +08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
self.assertIn(
|
2022-12-02 03:23:43 +08:00
|
|
|
f"{field.__module__}.{field.__class__.__name__}"
|
|
|
|
f"(default={import_name}.{default!r})",
|
2019-01-11 02:06:14 +08:00
|
|
|
serialized_field,
|
|
|
|
)
|
|
|
|
assertNotSerializable()
|