Skip to content

Commit

Permalink
feat: add testing options
Browse files Browse the repository at this point in the history
  • Loading branch information
johanseto committed Nov 5, 2024
1 parent 606b4eb commit 00bfa5b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
38 changes: 38 additions & 0 deletions eox_nelp/admin/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from unittest.mock import MagicMock, patch

from ddt import data, ddt, unpack
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.test import RequestFactory, TestCase, override_settings

from eox_nelp.admin import (
Expand All @@ -19,6 +21,8 @@
pearson_update_ead_action,
)

User = get_user_model()


@ddt
class TestPearsonAction(TestCase):
Expand Down Expand Up @@ -149,3 +153,37 @@ def test_actions(self, admin_action):
- pearson_real_time_action method is in model actions.
"""
self.assertIn(admin_action, self.modeladmin.actions)


class TestNelpUserAdmin(TestCase):
"""
Unit tests for the NelpUserAdmin class.
"""

def test_load_module(self):
"""
Test loading module of NelpUserAdmin when eox_support condition is bypassed.
Expected behavior:
- Test NelpUserAdmin is loaded in the user model key of admin registry.
- user_national_id is in list_display
- UserExtraInfoInline in inlines
"""
class ProtoAdminUserSupportModule:
"""Proto class to simulate module"""
class SupportUserAdmin(admin.ModelAdmin):
"""Base User Model admin for testing purposes. With empty tuple to add the nelp values"""
list_display = ((),)
search_fields = ((),)
inlines = ((),)
readonly_fields = ((),)

with patch.dict("sys.modules", {"eox_support.admin.user": ProtoAdminUserSupportModule}):
from eox_nelp.admin.user import ( # pylint: disable=import-outside-toplevel
NelpUserAdmin,
UserExtraInfoInline,
)

self.assertIsInstance(admin.site._registry[User], NelpUserAdmin) # pylint: disable=protected-access
self.assertIn("user_national_id", NelpUserAdmin.list_display)
self.assertIn(UserExtraInfoInline, NelpUserAdmin.inlines)
1 change: 0 additions & 1 deletion eox_nelp/admin/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class NelpUserAdmin(SupportUserAdmin):
list_display = SupportUserAdmin.list_display[:2] + ('user_national_id',) + SupportUserAdmin.list_display[2:]
search_fields = SupportUserAdmin.search_fields + ('extrainfo__national_id',)
inlines = SupportUserAdmin.inlines + (UserExtraInfoInline,)
readonly_fields = SupportUserAdmin.readonly_fields + ('user_national_id',)

def user_national_id(self, instance):
"""Return national_id associated with the user extra_info instance."""
Expand Down

0 comments on commit 00bfa5b

Please sign in to comment.