diff --git a/.github/workflows/publish_test_pypi.yml b/.github/workflows/publish_test_pypi.yml deleted file mode 100644 index 97b14f8..0000000 --- a/.github/workflows/publish_test_pypi.yml +++ /dev/null @@ -1,31 +0,0 @@ -#name: publish package to test-pypi -# -#on: -# push: -# branches: -# - master -# -#jobs: -# build-and-publish: -# name: Build and publish -# runs-on: ubuntu-latest -# steps: -# - uses: actions/checkout@master -# - name: Set up Python 3.8 -# uses: actions/setup-python@v1 -# with: -# python-version: 3.8 -# - name: Install poetry -# run: | -# python -m pip install --upgrade pip -# pip install poetry -# - name: Install dependencies -# run: | -# poetry install -# - name: Configure private repository -# run: | -# poetry config repositories.test https://test.pypi.org/legacy/ -# - name: Build and publish -# run: | -# poetry config pypi-token.test ${{ secrets.TEST_PYPI_TOKEN }} -# poetry publish --build --no-interaction --repository test diff --git a/chromedriver.exe b/chromedriver.exe index d263d4d..4850c8d 100644 Binary files a/chromedriver.exe and b/chromedriver.exe differ diff --git a/demo/tests.py b/demo/tests.py index 98e6c2b..e563e54 100644 --- a/demo/tests.py +++ b/demo/tests.py @@ -1,10 +1,17 @@ +from unittest.mock import patch + +from django.conf import settings from django.contrib.staticfiles.testing import StaticLiveServerTestCase -from selenium.webdriver import Chrome +from django.core.exceptions import ImproperlyConfigured +from django.test import SimpleTestCase -driver = Chrome() +from js_logger.config import get_logger, Settings +from selenium.webdriver import Chrome class TestLogging(StaticLiveServerTestCase): + driver = Chrome() + @classmethod def setUpClass(cls): super().setUpClass() @@ -37,3 +44,49 @@ def test_console_logging(self): button.click() self.selenium.implicitly_wait(2) # <-- race condition assert 'test' in log.output[0] + + +class TestConfig(SimpleTestCase): + def test_get_logger(self): + import logging + + self.assertEqual(get_logger('DEBUG', 'test'), logging.getLogger('test').debug) + self.assertEqual(get_logger('INFO', 'test'), logging.getLogger('test').info) + self.assertEqual(get_logger('WARNING', 'test'), logging.getLogger('test').warning) + self.assertEqual(get_logger('ERROR', 'test'), logging.getLogger('test').error) + self.assertEqual(get_logger('EXCEPTION', 'test'), logging.getLogger('test').exception) + self.assertEqual(get_logger('CRITICAL', 'test'), logging.getLogger('test').critical) + for i in ['', 's', 0, [], {}, None]: + with self.assertRaisesMessage( + ImproperlyConfigured, + expected_message=f'The log level for the `test` logger was set as `{i}` which is not a valid log level.', + ): + get_logger(i, 'test') + + valid_log_levels = [ + 'DEBUG', + 'INFO', + 'WARNING', + 'ERROR', + 'EXCEPTION', + 'CRITICAL', + ] + + @patch.object(settings, 'JS_LOGGER', None) + def test_missing_settings(self): + self.assertEqual(Settings().CONSOLE_LOG_LEVEL, 'INFO') + self.assertEqual(Settings().CONSOLE_ERROR_LEVEL, 'WARNING') + + def test_set_log_levels(self): + for level in self.valid_log_levels: + with patch.object(settings, 'JS_LOGGER', {'CONSOLE_LOG_LEVEL': level, 'CONSOLE_ERROR_LEVEL': level}): + js_settings = Settings() + self.assertEqual(js_settings.CONSOLE_LOG_LEVEL, level) + self.assertEqual(js_settings.CONSOLE_ERROR_LEVEL, level) + + @patch.object(settings, 'JS_LOGGER', {'bad setting': ''}) + def test_invalid_setting(self): + with self.assertRaisesMessage( + ImproperlyConfigured, expected_message=f'`bad setting` is not a valid setting for the django-js-logger module' + ): + Settings() diff --git a/js_logger/config.py b/js_logger/config.py index adfa9c8..479346d 100644 --- a/js_logger/config.py +++ b/js_logger/config.py @@ -26,7 +26,7 @@ def get_logger(level: str, logger_name: str) -> Callable: elif level == 'CRITICAL': return logging.getLogger(logger_name).critical else: - raise ImproperlyConfigured(f'The log level for the `{logger_name}` ' f'logger was set as `{level}` which is not a valid log level.') + raise ImproperlyConfigured(f'The log level for the `{logger_name}` logger was set as `{level}` ' f'which is not a valid log level.') class Settings(object): @@ -41,7 +41,7 @@ def __init__(self) -> None: self.CONSOLE_LOG_LEVEL = 'INFO' self.CONSOLE_ERROR_LEVEL = 'WARNING' - if not hasattr(django_settings, 'JS_LOGGER'): + if not hasattr(django_settings, 'JS_LOGGER') or not django_settings.JS_LOGGER: return package_settings = django_settings.JS_LOGGER