Skip to content

Commit 2127719

Browse files
committed
Test with psycopg2 and psycopg3
1 parent 633a6f4 commit 2127719

File tree

6 files changed

+25
-20
lines changed

6 files changed

+25
-20
lines changed

tests/requirements.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/test_project/test_project/settings/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
DATABASES = {
1515
"default": {
16-
"ENGINE": "django.db.backends.postgresql_psycopg2", # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
16+
"ENGINE": "django.db.backends.postgresql", # Add 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
1717
"NAME": "django_pgviews", # Or path to database file if using sqlite3.
1818
# The following settings are not used with sqlite3:
1919
"USER": "django_pgviews",
@@ -22,7 +22,7 @@
2222
"PORT": "", # Set to empty string for default.
2323
},
2424
"weather_db": {
25-
"ENGINE": "django.db.backends.postgresql_psycopg2", # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
25+
"ENGINE": "django.db.backends.postgresql", # Add 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
2626
"NAME": "django_pgviews_weatherdb", # Or path to database file if using sqlite3.
2727
# The following settings are not used with sqlite3:
2828
"USER": "django_pgviews",

tests/test_project/test_project/settings/ci.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
DATABASES = {
77
"default": {
8-
"ENGINE": "django.db.backends.postgresql_psycopg2",
8+
"ENGINE": "django.db.backends.postgresql",
99
"NAME": os.environ.get("DB_NAME", "circle_test"),
1010
"USER": os.environ.get("DB_USER", "postgres"),
1111
"PASSWORD": os.environ.get("DB_PASSWORD", "testing"),
1212
"HOST": "localhost",
1313
"PORT": "5432",
1414
},
1515
"weather_db": {
16-
"ENGINE": "django.db.backends.postgresql_psycopg2",
16+
"ENGINE": "django.db.backends.postgresql",
1717
"NAME": os.environ.get("DB_NAME_WEATHER", "weatherdb"),
1818
"USER": os.environ.get("DB_USER", "postgres"),
1919
"PASSWORD": os.environ.get("DB_PASSWORD", "testing"),

tests/test_project/test_project/viewtest/models.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from datetime import timedelta
22

3-
from django.db import models
3+
from django.db import models, connections
4+
from django.db.models import signals
5+
from django.dispatch import receiver
46
from django.utils import timezone
57

68
from django_pgviews import view
@@ -92,3 +94,10 @@ class MaterializedRelatedViewWithNoData(view.ReadOnlyMaterializedView):
9294
sql = """SELECT id AS model_id, id FROM viewtest_testmodel"""
9395
model = models.ForeignKey(TestModel, on_delete=models.DO_NOTHING)
9496
with_data = False
97+
98+
99+
@receiver(signals.post_migrate)
100+
def create_test_schema(sender, app_config, using, **kwargs):
101+
command = "CREATE SCHEMA IF NOT EXISTS {};".format("test_schema")
102+
with connections[using].cursor() as cursor:
103+
cursor.execute(command)

tests/test_project/test_project/viewtest/tests.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,25 @@
33
from contextlib import closing
44
from datetime import timedelta
55

6-
import psycopg2.errors
76
from django.conf import settings
87
from django.contrib import auth
98
from django.contrib.auth.models import User
109
from django.core.management import call_command
11-
from django.db import connection, connections, DEFAULT_DB_ALIAS
10+
from django.db import connection, DEFAULT_DB_ALIAS
1211
from django.db.utils import OperationalError
13-
from django.db.models import signals
1412
from django.dispatch import receiver
1513
from django.test import TestCase
1614
from django.utils import timezone
1715

1816
from django_pgviews.signals import view_synced, all_views_synced
1917
from django_pgviews.view import _schema_and_name
20-
2118
from . import models
2219
from .models import LatestSuperusers
2320

24-
25-
@receiver(signals.post_migrate)
26-
def create_test_schema(sender, app_config, using, **kwargs):
27-
command = "CREATE SCHEMA IF NOT EXISTS {};".format("test_schema")
28-
with connections[using].cursor() as cursor:
29-
cursor.execute(command)
21+
try:
22+
from psycopg.errors import UndefinedTable
23+
except ImportError:
24+
from psycopg2.errors import UndefinedTable
3025

3126

3227
def get_list_of_indexes(cursor, cls):
@@ -145,7 +140,7 @@ def test_refresh_missing(self):
145140
with connection.cursor() as cursor:
146141
cursor.execute("DROP MATERIALIZED VIEW viewtest_materializedrelatedview CASCADE;")
147142

148-
with self.assertRaises(psycopg2.errors.UndefinedTable):
143+
with self.assertRaises(UndefinedTable):
149144
models.MaterializedRelatedView.refresh()
150145

151146
def test_materialized_view_indexes(self):

tox.ini

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
[tox]
22
envlist =
3-
py{37,38,39,310}-dj{32},
4-
py{38,39,310,311}-dj{40,41,42}
3+
py{37,38,39,310}-dj{32}-pg2
4+
py{38,39,310,311}-dj{40,41}-pg2
5+
py{38,39,310,311}-dj{42}-pg{2,3}
56

67
[gh]
78
python =
@@ -17,7 +18,8 @@ setenv =
1718
DJANGO_SETTINGS_MODULE = test_project.settings.ci
1819
changedir = {toxinidir}/tests/test_project
1920
deps=
20-
-rtests/requirements.txt
21+
pg2: psycopg2>2.9
22+
pg3: psycopg>3.1
2123
dj32: https://github.com/django/django/archive/stable/3.2.x.tar.gz#egg=django
2224
dj40: https://github.com/django/django/archive/stable/4.0.x.tar.gz#egg=django
2325
dj41: https://github.com/django/django/archive/stable/4.1.x.tar.gz#egg=django

0 commit comments

Comments
 (0)