Skip to content

Commit

Permalink
tests: add tests to check loading of data for ClassLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
SukiCZ committed Oct 5, 2024
1 parent 4fe2ce0 commit ea5fac4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
25 changes: 25 additions & 0 deletions weblate/utils/tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@

from django.core.cache import cache
from django.test import SimpleTestCase
from django.test.utils import override_settings

from weblate.addons.base import BaseAddon
from weblate.utils.apps import check_class_loader
from weblate.utils.celery import is_celery_queue_long
from weblate.utils.classloader import ClassLoader


class CeleryQueueTest(SimpleTestCase):
Expand Down Expand Up @@ -56,3 +60,24 @@ def test_translate(self) -> None:
self.assertFalse(is_celery_queue_long())
self.set_cache({int(time.time() / 3600) - 1: {"translate": 2000}})
self.assertTrue(is_celery_queue_long())


class ClassLoaderCheckTestCase(SimpleTestCase):
@override_settings(TEST_ADDONS=("weblate.addons.cleanup.CleanupAddon",))
def test_load(self) -> None:
loader = ClassLoader("TEST_ADDONS", construct=False, base_class=BaseAddon)
loader.load_data()
self.assertEqual(len(list(loader.keys())), 1)

@override_settings(TEST_ADDONS=("weblate.addons.cleanup.CleanupAddon"))
def test_invalid(self) -> None:
loader = ClassLoader("TEST_ADDONS", construct=False, base_class=BaseAddon) # noqa: F841
errors = check_class_loader(app_configs=None, databases=None)
self.assertEqual(len(errors), 1)

@override_settings(TEST_ADDONS=("weblate.addons.not_found",))
def test_not_found(self) -> None:
loader = ClassLoader("TEST_ADDONS", construct=False, base_class=BaseAddon) # noqa: F841
errors = check_class_loader(app_configs=None, databases=None)
self.assertEqual(len(errors), 1)
self.assertIn("does not define a 'not_found' class", errors[0].msg)
8 changes: 8 additions & 0 deletions weblate/utils/tests/test_classloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ def test_invalid(self) -> None:
):
loader.load_data()

@override_settings(TEST_SERVICES=("weblate.addons.not_found",))
def test_not_found(self) -> None:
loader = ClassLoader("TEST_SERVICES", construct=False, base_class=BaseAddon)
with self.assertRaisesRegex(
ImproperlyConfigured, "does not define a 'not_found' class"
):
loader.load_data()

@override_settings(TEST_SERVICES=("weblate.addons.cleanup",))
def test_module(self) -> None:
loader = ClassLoader("TEST_SERVICES", construct=False, base_class=BaseAddon)
Expand Down

0 comments on commit ea5fac4

Please sign in to comment.