Skip to content
This repository has been archived by the owner on Dec 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #12 from snok/tests
Browse files Browse the repository at this point in the history
Adds config tests
  • Loading branch information
Sondre Lillebø Gundersen authored Jun 29, 2020
2 parents 4b6d164 + b458ab4 commit 87109dd
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 35 deletions.
31 changes: 0 additions & 31 deletions .github/workflows/publish_test_pypi.yml

This file was deleted.

Binary file modified chromedriver.exe
Binary file not shown.
57 changes: 55 additions & 2 deletions demo/tests.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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()
4 changes: 2 additions & 2 deletions js_logger/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down

0 comments on commit 87109dd

Please sign in to comment.