Skip to content

Commit

Permalink
Use patch to prevent side effects from connection overrides.
Browse files Browse the repository at this point in the history
  • Loading branch information
rtibbles committed Jul 17, 2024
1 parent 062fae3 commit be57772
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions kolibri/core/deviceadmin/tests/test_dbrestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest
from django.conf import settings
from django.core.management import call_command
from django.db import ConnectionHandler
from django.test.utils import override_settings
from mock import patch

Expand Down Expand Up @@ -174,14 +175,13 @@ def test_restore_from_file_to_memory():

# Restore it into a new test database setting
with override_settings(DATABASES=MOCK_DATABASES):
from django import db

# Destroy current connections and create new ones:
db.connections.close_all()
db.connections = db.ConnectionHandler()
call_command("dbrestore", backup)
# Test that the user has been restored!
assert Facility.objects.filter(name="test file", kind=FACILITY).count() == 1
with patch("django.db.connections", ConnectionHandler()):
call_command("dbrestore", backup)
# Test that the user has been restored!
assert (
Facility.objects.filter(name="test file", kind=FACILITY).count()
== 1
)
_clear_backups(dest_folder)


Expand Down Expand Up @@ -210,15 +210,13 @@ def test_restore_from_file_to_file():

# Restore it into a new test database setting
with override_settings(DATABASES=MOCK_DATABASES_FILE):
# Destroy current connections and create new ones:
db.connections.close_all()
db.connections = db.ConnectionHandler()
# Purposefully destroy the connection pointer, which is the default
# state of an unopened connection
db.connections["default"].connection = None
call_command("dbrestore", backup)
# Test that the user has been restored!
assert Facility.objects.filter(name="test file", kind=FACILITY).count() == 1
with patch("django.db.connections", ConnectionHandler()):
call_command("dbrestore", backup)
# Test that the user has been restored!
assert (
Facility.objects.filter(name="test file", kind=FACILITY).count()
== 1
)
_clear_backups(dest_folder)


Expand Down

0 comments on commit be57772

Please sign in to comment.