Skip to content

Commit

Permalink
⚡🚨 Update(tests) to modify new changes from configs
Browse files Browse the repository at this point in the history
  • Loading branch information
MEHRSHAD-MIRSHEKARY committed Sep 26, 2024
1 parent e599544 commit b886af0
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 38 deletions.
28 changes: 14 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ repos:
files: ^(docs/(.*/)*.*\.rst)
additional_dependencies: [ Sphinx==6.2.1 ]

# - repo: local
# hooks:
- repo: local
hooks:
# - id: pytest
# name: Pytest
# entry: poetry run pytest -v
Expand All @@ -94,15 +94,15 @@ repos:
# pass_filenames: false
# always_run: true

# - id: pylint
# name: pylint
# entry: pylint
# language: system
# types: [ python ]
# require_serial: true
# args:
# - "-rn"
# - "-sn"
# - "--rcfile=pyproject.toml"
# files: ^django_notification/
# exclude: (migrations/|tests/|docs/).*
- id: pylint
name: pylint
entry: pylint
language: system
types: [ python ]
require_serial: true
args:
- "-rn"
- "-sn"
- "--rcfile=pyproject.toml"
files: ^django_notification/
exclude: (migrations/|tests/|docs/).*
3 changes: 3 additions & 0 deletions django_notification/tests/api/serializers/test_group.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import sys
from unittest.mock import patch

import pytest
from django.contrib.auth.models import Group
from rest_framework.exceptions import ValidationError
from django_notification.api.serializers.group import GroupSerializer
from django_notification.settings.conf import config
from django_notification.utils.serialization.field_filters import (
filter_non_empty_fields,
)
Expand All @@ -23,6 +25,7 @@ class TestGroupSerializer:
Test the GroupSerializer and PermissionSerializer functionality.
"""

@patch.object(config, "exclude_serializer_null_fields", False)
def test_group_serializer_with_valid_data(self, group_with_perm: Group) -> None:
"""
Test that the GroupSerializer correctly serializes a group with permissions.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import sys
from typing import Dict, Any
from unittest.mock import patch

import pytest

from django_notification.api.serializers.simple_notification import (
SimpleNotificationSerializer,
)
from django_notification.settings.conf import config
from django_notification.utils.serialization.field_filters import (
filter_non_empty_fields,
)
Expand Down Expand Up @@ -52,6 +54,7 @@ def test_serializer_fields(self, notification_dict: Dict[str, Any]) -> None:
serializer = SimpleNotificationSerializer(notification_dict)
assert set(serializer.data.keys()) == set(expected_fields)

@patch.object(config, "exclude_serializer_null_fields", False)
@mark.django_db
def test_title_generation(self, notification_dict: Dict[str, Any]) -> None:
"""
Expand Down
1 change: 1 addition & 0 deletions django_notification/tests/api/views/test_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def setup_method(self) -> None:
"""
self.client = APIClient()

@patch.object(config, "exclude_serializer_null_fields", False)
def test_get_queryset_for_staff(
self, admin_user: Type[User], notification: Notification
) -> None:
Expand Down
4 changes: 2 additions & 2 deletions django_notification/tests/constants.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
PYTHON_VERSION = (3, 8)
PYTHON_VERSION_REASON = "Requires Python 3.8 or higher"
PYTHON_VERSION = (3, 9)
PYTHON_VERSION_REASON = "Requires Python 3.9 or higher"
61 changes: 39 additions & 22 deletions django_notification/tests/settings/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def test_valid_settings(self, mock_config: MagicMock) -> None:
mock_config.admin_has_change_permission = False
mock_config.admin_has_delete_permission = False
mock_config.include_serializer_full_details = True
mock_config.exclude_serializer_null_fields = True
mock_config.api_allow_list = True
mock_config.api_allow_retrieve = False
mock_config.user_serializer_fields = ["id", "username"]
Expand Down Expand Up @@ -66,7 +67,8 @@ def test_invalid_boolean_settings(self, mock_config: MagicMock) -> None:
mock_config.admin_has_add_permission = "not_boolean"
mock_config.admin_has_change_permission = "not_boolean"
mock_config.admin_has_delete_permission = "not_boolean"
mock_config.include_serializer_full_details = True
mock_config.include_serializer_full_details = "not_bool"
mock_config.exclude_serializer_null_fields = "not_boolean"
mock_config.user_serializer_fields = ["id", "username"]
mock_config.api_ordering_fields = ["created_at"]
mock_config.api_search_fields = ["title"]
Expand All @@ -78,31 +80,39 @@ def test_invalid_boolean_settings(self, mock_config: MagicMock) -> None:

errors = check_notification_settings(None)

# Expect 6 errors for invalid boolean values
assert len(errors) == 6
# Expect 8 errors for invalid boolean values
assert len(errors) == 8
assert (
errors[0].id
== "django_notification.E001_DJANGO_NOTIFICATION_API_INCLUDE_SOFT_DELETE"
== f"django_notification.E001_{mock_config.prefix}API_INCLUDE_SOFT_DELETE"
)
assert (
errors[1].id
== "django_notification.E001_DJANGO_NOTIFICATION_API_INCLUDE_HARD_DELETE"
== f"django_notification.E001_{mock_config.prefix}API_INCLUDE_HARD_DELETE"
)
assert (
errors[2].id
== "django_notification.E001_DJANGO_NOTIFICATION_ADMIN_HAS_ADD_PERMISSION"
== f"django_notification.E001_{mock_config.prefix}ADMIN_HAS_ADD_PERMISSION"
)
assert (
errors[3].id
== "django_notification.E001_DJANGO_NOTIFICATION_ADMIN_HAS_CHANGE_PERMISSION"
== f"django_notification.E001_{mock_config.prefix}ADMIN_HAS_CHANGE_PERMISSION"
)
assert (
errors[4].id
== "django_notification.E001_DJANGO_NOTIFICATION_ADMIN_HAS_DELETE_PERMISSION"
== f"django_notification.E001_{mock_config.prefix}ADMIN_HAS_DELETE_PERMISSION"
)
assert (
errors[5].id
== "django_notification.E001_DJANGO_NOTIFICATION_API_ALLOW_LIST"
errors[5].id
== f"django_notification.E001_{mock_config.prefix}SERIALIZER_INCLUDE_FULL_DETAILS"
)
assert (
errors[6].id
== f"django_notification.E001_{mock_config.prefix}SERIALIZER_EXCLUDE_NULL_FIELDS"
)
assert (
errors[7].id
== f"django_notification.E001_{mock_config.prefix}API_ALLOW_LIST"
)

@patch("django_notification.settings.checks.config")
Expand All @@ -125,6 +135,7 @@ def test_invalid_list_settings(self, mock_config: MagicMock) -> None:
mock_config.admin_has_change_permission = False
mock_config.admin_has_delete_permission = False
mock_config.include_serializer_full_details = True
mock_config.exclude_serializer_null_fields = True
mock_config.api_allow_list = True
mock_config.api_allow_retrieve = False
mock_config.user_serializer_fields = []
Expand All @@ -140,15 +151,15 @@ def test_invalid_list_settings(self, mock_config: MagicMock) -> None:
assert len(errors) == 3
assert (
errors[0].id
== "django_notification.E003_DJANGO_NOTIFICATION_USER_SERIALIZER_FIELDS"
== f"django_notification.E003_{mock_config.prefix}USER_SERIALIZER_FIELDS"
)
assert (
errors[1].id
== "django_notification.E003_DJANGO_NOTIFICATION_API_ORDERING_FIELDS"
== f"django_notification.E003_{mock_config.prefix}API_ORDERING_FIELDS"
)
assert (
errors[2].id
== "django_notification.E004_DJANGO_NOTIFICATION_API_SEARCH_FIELDS"
== f"django_notification.E004_{mock_config.prefix}API_SEARCH_FIELDS"
)

@patch("django_notification.settings.checks.config")
Expand All @@ -171,6 +182,7 @@ def test_invalid_throttle_rate(self, mock_config: MagicMock) -> None:
mock_config.admin_has_change_permission = False
mock_config.admin_has_delete_permission = False
mock_config.include_serializer_full_details = True
mock_config.exclude_serializer_null_fields = True
mock_config.api_allow_list = True
mock_config.api_allow_retrieve = False
mock_config.user_serializer_fields = ["id", "username"]
Expand Down Expand Up @@ -207,6 +219,7 @@ def test_invalid_class_import(self, mock_config: MagicMock) -> None:
mock_config.admin_has_change_permission = False
mock_config.admin_has_delete_permission = False
mock_config.include_serializer_full_details = True
mock_config.exclude_serializer_null_fields = True
mock_config.api_allow_list = True
mock_config.api_allow_retrieve = False
mock_config.user_serializer_fields = ["id", "username"]
Expand All @@ -220,33 +233,37 @@ def test_invalid_class_import(self, mock_config: MagicMock) -> None:

errors = check_notification_settings(None)

# Expect 7 errors for invalid class imports
assert len(errors) == 7
# Expect 8 errors for invalid class imports
assert len(errors) == 8
assert (
errors[0].id
== "django_notification.E010_DJANGO_NOTIFICATION_USER_SERIALIZER_CLASS"
== f"django_notification.E010_{mock_config.prefix}USER_SERIALIZER_CLASS"
)
assert (
errors[1].id
== "django_notification.E010_DJANGO_NOTIFICATION_GROUP_SERIALIZER_CLASS"
== f"django_notification.E010_{mock_config.prefix}GROUP_SERIALIZER_CLASS"
)
assert (
errors[2].id
== "django_notification.E010_DJANGO_NOTIFICATION_API_THROTTLE_CLASS"
== f"django_notification.E010_{mock_config.prefix}API_THROTTLE_CLASS"
)
assert (
errors[3].id
== "django_notification.E010_DJANGO_NOTIFICATION_API_PAGINATION_CLASS"
== f"django_notification.E010_{mock_config.prefix}API_PAGINATION_CLASS"
)
assert (
errors[4].id
== "django_notification.E011_DJANGO_NOTIFICATION_API_PARSER_CLASSES"
== f"django_notification.E011_{mock_config.prefix}API_PARSER_CLASSES"
)
assert (
errors[5].id
== "django_notification.E010_DJANGO_NOTIFICATION_API_FILTERSET_CLASS"
== f"django_notification.E010_{mock_config.prefix}API_FILTERSET_CLASS"
)
assert (
errors[6].id
== "django_notification.E010_DJANGO_NOTIFICATION_API_EXTRA_PERMISSION_CLASS"
== f"django_notification.E010_{mock_config.prefix}API_EXTRA_PERMISSION_CLASS"
)
assert (
errors[7].id
== f"django_notification.E010_{mock_config.prefix}ADMIN_SITE_CLASS"
)
1 change: 1 addition & 0 deletions django_notification/tests/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def configure_django_settings() -> None:
DJANGO_NOTIFICATION_ADMIN_HAS_ADD_PERMISSION=False,
DJANGO_NOTIFICATION_ADMIN_HAS_CHANGE_PERMISSION=False,
DJANGO_NOTIFICATION_ADMIN_HAS_DELETE_PERMISSION=False,
DJANGO_NOTIFICATION_SERIALIZER_EXCLUDE_NULL_FIELDS=True,
DJANGO_NOTIFICATION_API_ALLOW_LIST=True,
DJANGO_NOTIFICATION_API_ALLOW_RETRIEVE=True,
DJANGO_NOTIFICATION_AUTHENTICATED_USER_THROTTLE_RATE="20/minute",
Expand Down

0 comments on commit b886af0

Please sign in to comment.