1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 19:26:48 +00:00
webvirtcloud/appsettings/tests/test_get_settings.py
catborise c55f52f025 test: each app keeps its tests in a tests package
131 test modules sat next to the code they test; instances alone had
51 of them beside views.py and utils.py. They move into a
tests/ package per app, and the JavaScript checks into
webvirtcloud/tests/js. Each non-empty tests.py becomes tests/test_views.py;
the empty generated stubs are removed.

Imports of the app's own modules become absolute, paths built from
__file__ go one level up, the live helper is instances.tests.livetest,
and coverage omits */tests/*. The suite discovers the same 960 tests as
before.
2026-10-12 15:17:30 +03:00

36 lines
1.6 KiB
Python

"""get_settings() runs on every request (middleware). A database error must not
leave the loop reading an unbound variable (the old bare except did), and must
not crash the request chain over a transient read."""
from unittest.mock import patch
from django.db import DatabaseError
from django.test import TestCase
from appsettings.models import AppSettings
from appsettings.settings import app_settings, get_settings
class GetSettingsTestCase(TestCase):
def test_a_database_error_does_not_raise(self):
app_settings.MARKER = "kept"
with patch("appsettings.settings.AppSettings.objects") as objects:
objects.all.side_effect = DatabaseError("db down")
get_settings() # must not raise UnboundLocalError or DatabaseError
self.assertEqual(app_settings.MARKER, "kept")
def test_settings_are_loaded_when_the_database_works(self):
AppSettings.objects.update_or_create(key="TEST_MARKER_KEY", defaults={"name": "m", "value": "from-db"})
try:
get_settings()
self.assertEqual(app_settings.TEST_MARKER_KEY, "from-db")
finally:
if hasattr(app_settings, "TEST_MARKER_KEY"):
delattr(app_settings, "TEST_MARKER_KEY")
def test_a_query_that_fails_during_iteration_does_not_raise(self):
app_settings.MARKER2 = "kept"
with patch("appsettings.settings.AppSettings.objects") as objects:
objects.all.return_value.__iter__.side_effect = DatabaseError("cursor died")
get_settings()
self.assertEqual(app_settings.MARKER2, "kept")