Skip to content

Commit

Permalink
Resolve issues with running morango integration tests on postres
Browse files Browse the repository at this point in the history
  • Loading branch information
bjester committed Jul 20, 2022
1 parent adf987c commit 1395b53
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ lint:
flake8 kolibri

test:
pytest
python -O -m pytest

test-all:
tox
Expand Down
69 changes: 37 additions & 32 deletions kolibri/core/auth/test/sync_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import uuid

import requests
from django.conf import settings
from django.db import connection
from django.db import connections
from django.utils.functional import wraps
Expand Down Expand Up @@ -44,7 +45,7 @@ def __init__(
self.env = os.environ.copy()
self.env["KOLIBRI_HOME"] = kolibri_home or tempfile.mkdtemp()
self.env["DJANGO_SETTINGS_MODULE"] = settings
self.env["POSTGRES_DB"] = db_name
self.env["KOLIBRI_DATABASE_NAME"] = db_name
self.env["KOLIBRI_RUN_MODE"] = self.env.get("KOLIBRI_RUN_MODE", "") + "-testing"
self.env["KOLIBRI_ZIP_CONTENT_PORT"] = str(get_free_tcp_port())
if env is not None:
Expand Down Expand Up @@ -193,10 +194,8 @@ def __init__(self, count=2, **server_kwargs):
]

def __enter__(self):

# spin up the servers
if "sqlite" in connection.vendor:

tempserver = KolibriServer(
autostart=False,
kolibri_home=os.environ.get("KOLIBRI_TEST_PRESEEDED_HOME"),
Expand All @@ -214,46 +213,48 @@ def __enter__(self):
]

# calculate the DATABASE settings
connections.databases = {
server.db_alias: {
for server in self.servers:
settings.DATABASES[server.db_alias] = connections.databases[
server.db_alias
] = {
"ENGINE": "django.db.backends.sqlite3",
"NAME": server.db_path,
"OPTIONS": {"timeout": 100},
}
for server in self.servers
}

if "postgresql" in connection.vendor:

if self.server_count == 3:
self.servers = [
KolibriServer(
settings="kolibri.deployment.default.settings.postgres_test",
db_name="eco_test" + str(i + 1),
**self.server_kwargs[i]
)
for i in range(self.server_count)
]

if self.server_count == 5:
self.servers = [
KolibriServer(
settings="kolibri.deployment.default.settings.postgres_test",
db_name="eco2_test" + str(i + 1),
**self.server_kwargs[i]
)
for i in range(self.server_count)
]
self.servers = [
KolibriServer(
autostart=False,
db_name="eco_test" + str(i + 1),
**self.server_kwargs[i]
)
for i in range(self.server_count)
]

# calculate the DATABASE settings
connections.databases = {
server.db_alias: {
for server in self.servers:
settings.DATABASES[server.db_alias] = connections.databases[
server.db_alias
] = {
"ENGINE": "django.db.backends.postgresql",
"USER": "postgres",
"NAME": server.env["POSTGRES_DB"],
"PASSWORD": "postgres",
"NAME": server.env["KOLIBRI_DATABASE_NAME"],
"HOST": "localhost",
"PORT": "5432",
"TEST": {"NAME": server.env["KOLIBRI_DATABASE_NAME"]},
}
for server in self.servers
}

for server in self.servers:
server_conn = connections[server.db_alias]
# We don't use `create_test_db` here but instead the internal method which does the
# magic we want, since `create_test_db` also attempts to sync and migrate the
# database and that raises errors. When the Kolibri server starts it will run
# migrations automatically
server_conn.creation._create_test_db(verbosity=2, autoclobber=True)
server_conn.close()
server.start()

return self.servers

Expand All @@ -262,6 +263,10 @@ def __exit__(self, typ, val, traceback):
# make sure all the servers are shut down
for server in self.servers:
server.kill()
# destroy the test databases
server_conn = connections[server.db_alias]
server_conn.creation.destroy_test_db()
server_conn.close()

def __call__(self, f):
@wraps(f)
Expand Down
2 changes: 1 addition & 1 deletion kolibri/core/auth/test/test_deprovisioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def count_instances(models):


@override_settings(MORANGO_TEST_POSTGRESQL=True)
class UserImportCommandTestCase(TestCase):
class DeprovisionCommandTestCase(TestCase):
"""
Tests for the deprovision command.
"""
Expand Down

0 comments on commit 1395b53

Please sign in to comment.