Page MenuHomeSoftware Heritage

D8195.id29572.diff
No OneTemporary

D8195.id29572.diff

diff --git a/swh/deposit/config.py b/swh/deposit/config.py
--- a/swh/deposit/config.py
+++ b/swh/deposit/config.py
@@ -4,7 +4,7 @@
# See top-level LICENSE file for more information
import os
-from typing import Any, Dict
+from typing import Any, Dict, Optional
from swh.core import config
from swh.deposit import __version__
@@ -58,36 +58,36 @@
}
-def setup_django_for(platform=None, config_file=None):
- """Setup function for command line tools (swh.deposit.create_user) to
- initialize the needed db access.
+def setup_django_for(platform: Optional[str] = None, config_file: Optional[str] = None):
+ """Setup function for command line tools (e.g. swh.deposit.create_user) to
+ initialize the needed db access.
Note:
Do not import any django related module prior to this function
- call. Otherwise, this will raise an
- django.core.exceptions.ImproperlyConfigured error message.
+ call. Otherwise, this will raise a django.core.exceptions.ImproperlyConfigured
+ error message.
Args:
- platform (str): the platform the scheduling is running
- config_file (str): Extra configuration file (typically for the
- production platform)
+ platform: the platform the cli is running for
+ config_file: Extra configuration file (typically for the production platform)
Raises:
- ValueError in case of wrong platform inputs.
+ ValueError in case of wrong platform inputs
+ ValueError if the SWH_CONFIG_FILENAME environment variable is not set.
"""
if platform is not None:
if platform not in AUTHORIZED_PLATFORMS:
- raise ValueError("Platform should be one of %s" % AUTHORIZED_PLATFORMS)
+ raise ValueError(f"Platform should be one of {AUTHORIZED_PLATFORMS}")
if "DJANGO_SETTINGS_MODULE" not in os.environ:
- os.environ["DJANGO_SETTINGS_MODULE"] = "swh.deposit.settings.%s" % platform
+ os.environ["DJANGO_SETTINGS_MODULE"] = f"swh.deposit.settings.{platform}"
if config_file:
os.environ.setdefault("SWH_CONFIG_FILENAME", config_file)
- import django
+ from django import setup
- django.setup()
+ setup()
class APIConfig:
diff --git a/swh/deposit/tests/test_config.py b/swh/deposit/tests/test_config.py
new file mode 100644
--- /dev/null
+++ b/swh/deposit/tests/test_config.py
@@ -0,0 +1,19 @@
+# Copyright (C) 2022 The Software Heritage developers
+# See the AUTHORS file at the top-level directory of this distribution
+# License: GNU General Public License version 3, or any later version
+# See top-level LICENSE file for more information
+
+import pytest
+
+from swh.deposit.config import setup_django_for
+
+
+def test_setup_django_for_raise_unknown_platform(monkeypatch):
+ """Unknown platform should make the function setup raise"""
+ with pytest.raises(ValueError, match="Platform should be"):
+ setup_django_for(platform="unknown")
+
+
+def test_setup_django_for_ok(deposit_config_path):
+ """Everything is fine, moving along"""
+ setup_django_for(platform="testing")

File Metadata

Mime Type
text/plain
Expires
Fri, Jun 20, 6:16 PM (2 w, 14 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3229709

Event Timeline