2022-12-01 08:39:46 +00:00
|
|
|
import unittest
|
2021-11-04 09:36:11 +00:00
|
|
|
from decimal import Decimal
|
|
|
|
|
2022-12-01 08:39:46 +00:00
|
|
|
from django.db import connection
|
2018-11-12 16:15:48 +00:00
|
|
|
from django.db.backends.signals import connection_created
|
2019-01-10 18:06:14 +00:00
|
|
|
from django.db.migrations.writer import MigrationWriter
|
2022-12-01 08:39:46 +00:00
|
|
|
from django.test import TestCase
|
2023-09-24 09:02:50 +00:00
|
|
|
from django.test.utils import CaptureQueriesContext, modify_settings, override_settings
|
2018-11-12 16:15:48 +00:00
|
|
|
|
2019-01-10 18:06:14 +00:00
|
|
|
try:
|
|
|
|
from django.contrib.postgres.fields import (
|
2021-11-04 09:36:11 +00:00
|
|
|
DateRangeField,
|
|
|
|
DateTimeRangeField,
|
|
|
|
DecimalRangeField,
|
|
|
|
IntegerRangeField,
|
2019-01-10 18:06:14 +00:00
|
|
|
)
|
2023-09-24 09:02:50 +00:00
|
|
|
from django.contrib.postgres.signals import get_hstore_oids
|
2022-12-12 07:25:05 +00:00
|
|
|
from django.db.backends.postgresql.psycopg_any import (
|
|
|
|
DateRange,
|
|
|
|
DateTimeRange,
|
|
|
|
DateTimeTZRange,
|
|
|
|
NumericRange,
|
2022-12-01 19:23:43 +00:00
|
|
|
is_psycopg3,
|
2022-12-12 07:25:05 +00:00
|
|
|
)
|
2019-01-10 18:06:14 +00:00
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2018-11-12 16:15:48 +00:00
|
|
|
|
2022-12-01 08:39:46 +00:00
|
|
|
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific tests")
|
|
|
|
class PostgresConfigTests(TestCase):
|
2023-09-24 09:02:50 +00:00
|
|
|
def test_install_app_no_warning(self):
|
|
|
|
# Clear cache to force queries when (re)initializing the
|
|
|
|
# "django.contrib.postgres" app.
|
|
|
|
get_hstore_oids.cache_clear()
|
|
|
|
with CaptureQueriesContext(connection) as captured_queries:
|
|
|
|
with override_settings(INSTALLED_APPS=["django.contrib.postgres"]):
|
|
|
|
pass
|
|
|
|
self.assertGreaterEqual(len(captured_queries), 1)
|
|
|
|
|
2018-11-12 16:15:48 +00:00
|
|
|
def test_register_type_handlers_connection(self):
|
|
|
|
from django.contrib.postgres.signals import register_type_handlers
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2018-11-12 16:15:48 +00:00
|
|
|
self.assertNotIn(
|
2020-11-07 10:19:20 +00:00
|
|
|
register_type_handlers, connection_created._live_receivers(None)[0]
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2018-11-12 16:15:48 +00:00
|
|
|
with modify_settings(INSTALLED_APPS={"append": "django.contrib.postgres"}):
|
|
|
|
self.assertIn(
|
2020-11-07 10:19:20 +00:00
|
|
|
register_type_handlers, connection_created._live_receivers(None)[0]
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2018-11-12 16:15:48 +00:00
|
|
|
self.assertNotIn(
|
2020-11-07 10:19:20 +00:00
|
|
|
register_type_handlers, connection_created._live_receivers(None)[0]
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2019-01-10 18:06:14 +00:00
|
|
|
|
|
|
|
def test_register_serializer_for_migrations(self):
|
|
|
|
tests = (
|
|
|
|
(DateRange(empty=True), DateRangeField),
|
|
|
|
(DateTimeRange(empty=True), DateRangeField),
|
|
|
|
(DateTimeTZRange(None, None, "[]"), DateTimeRangeField),
|
2021-11-04 09:36:11 +00:00
|
|
|
(NumericRange(Decimal("1.0"), Decimal("5.0"), "()"), DecimalRangeField),
|
2019-01-10 18:06:14 +00: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-01 19:23:43 +00:00
|
|
|
import_name = "psycopg.types.range" if is_psycopg3 else "psycopg2.extras"
|
2019-01-10 18:06:14 +00: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-01 19:23:43 +00:00
|
|
|
f"import {import_name}",
|
2019-01-10 18:06:14 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
self.assertIn(
|
2022-12-01 19:23:43 +00:00
|
|
|
f"{field.__module__}.{field.__class__.__name__}"
|
|
|
|
f"(default={import_name}.{default!r})",
|
2019-01-10 18:06:14 +00:00
|
|
|
serialized_field,
|
|
|
|
)
|
|
|
|
assertNotSerializable()
|