Skip to content

Commit

Permalink
⚡✅🚨✔️ tests: Add tests for validators
Browse files Browse the repository at this point in the history
pass all tests with 100% coverage
Closes #108
  • Loading branch information
MEHRSHAD-MIRSHEKARY committed Oct 4, 2024
1 parent 8a7214e commit db56c2c
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 4 deletions.
2 changes: 1 addition & 1 deletion django_logging/tests/settings/test_checks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from typing import Generator, List
from typing import List
from unittest.mock import patch

import pytest
Expand Down
98 changes: 96 additions & 2 deletions django_logging/tests/validators/test_config_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
import pytest

from django_logging.constants.config_types import LogLevels
from django_logging.tests.constants import PYTHON_VERSION, PYTHON_VERSION_REASON
from django_logging.validators.config_validators import (
validate_boolean_setting,
validate_date_format,
validate_directory,
validate_email_notifier,
validate_extra_log_files,
validate_format_option,
validate_format_string,
validate_log_file_format_types,
validate_log_levels,
)
from django_logging.tests.constants import PYTHON_VERSION, PYTHON_VERSION_REASON

pytestmark = [
pytest.mark.validators,
Expand Down Expand Up @@ -240,7 +242,7 @@ def test_validate_format_option_failure(self) -> None:
-------
- Appropriate errors are returned for invalid format options.
"""
format_option = 15
format_option = 25
errors = validate_format_option(format_option, "log_format_option")
assert len(errors) == 1
assert errors[0].id == "django_logging.E012_log_format_option"
Expand Down Expand Up @@ -379,3 +381,95 @@ def test_validate_email_notifier_invalid_type(self) -> None:
errors = validate_email_notifier(notifier_config) # type: ignore
assert len(errors) == 1
assert errors[0].id == "django_logging.E018_LOG_EMAIL_NOTIFIER['ENABLE']"

def test_validate_log_file_format_types_invalid_type(self) -> None:
"""
Test validation of log file format types with an invalid data type.
Args:
----
format_types (Dict): The format types to validate.
config_name (str): The name of the configuration being validated.
valid_levels (List[str]): Valid logging levels.
valid_formats (List[str]): Valid format types.
Asserts:
-------
- Errors are returned when format types are not a dictionary.
"""
errors = validate_log_file_format_types(
"INVALID_TYPE", "log_file_format_types", # type: ignore
["DEBUG", "INFO"], ["JSON", "XML"]
)
assert len(errors) == 1
assert errors[0].id == "django_logging.E022_log_file_format_types"

def test_validate_log_file_format_types_invalid_level_and_format(self) -> None:
"""
Test validation of log file format types with invalid log levels and formats.
Args:
----
format_types (Dict): The format types to validate.
config_name (str): The name of the configuration being validated.
valid_levels (List[str]): Valid logging levels.
valid_formats (List[str]): Valid format types.
Asserts:
-------
- Errors are returned for invalid log levels.
- Errors are returned for invalid format types.
"""
errors = validate_log_file_format_types(
{"INVALID_LEVEL": "JSON", "DEBUG": "INVALID_FORMAT"},
"log_file_format_types",
["DEBUG", "INFO", "WARNING"],
["JSON", "XML"],
)
assert len(errors) == 2
assert errors[0].id == "django_logging.E023_log_file_format_types"
assert errors[1].id == "django_logging.E024_log_file_format_types"

def test_validate_extra_log_files_invalid_type(self) -> None:
"""
Test validation of extra log files with an invalid data type.
Args:
----
extra_log_files (Dict): The extra log files configuration to validate.
config_name (str): The name of the configuration being validated.
valid_levels (List[str]): Valid logging levels.
Asserts:
-------
- Errors are returned when extra log files are not a dictionary.
"""
errors = validate_extra_log_files(
"INVALID_TYPE", "extra_log_files", ["DEBUG", "INFO"] # type: ignore
)
assert len(errors) == 1
assert errors[0].id == "django_logging.E025_extra_log_files"

def test_validate_extra_log_files_invalid_level_and_value(self) -> None:
"""
Test validation of extra log files with invalid log levels and values.
Args:
----
extra_log_files (Dict): The extra log files configuration to validate.
config_name (str): The name of the configuration being validated.
valid_levels (List[str]): Valid logging levels.
Asserts:
-------
- Errors are returned for invalid log levels.
- Errors are returned for non-boolean values.
"""
errors = validate_extra_log_files(
{"INVALID_LEVEL": True, "DEBUG": "INVALID_VALUE"},
"extra_log_files",
["DEBUG", "INFO", "WARNING"],
)
assert len(errors) == 2
assert errors[0].id == "django_logging.E026_extra_log_files"
assert errors[1].id == "django_logging.E027_extra_log_files"
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import pytest
from django.conf import settings

from django_logging.validators.email_settings_validator import check_email_settings
from django_logging.tests.constants import PYTHON_VERSION, PYTHON_VERSION_REASON
from django_logging.validators.email_settings_validator import check_email_settings

pytestmark = [
pytest.mark.validators,
Expand Down

0 comments on commit db56c2c

Please sign in to comment.