Skip to content

Commit

Permalink
Added: tests for admin actions
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Jul 27, 2023
1 parent d6b4234 commit 67d39fc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
3 changes: 1 addition & 2 deletions recipes/admin/admin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from typing import Any

from django.contrib import admin
from django.forms import ModelChoiceField
from django.http import HttpRequest
from django.db.models import ForeignKey

from ..models import *
from .actions import comment_accept, comment_reject
Expand Down
Empty file added recipes/tests/admin/__init__.py
Empty file.
42 changes: 42 additions & 0 deletions recipes/tests/admin/test_actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from django.contrib.admin.sites import AdminSite
from django.contrib.auth import get_user_model
from django.test import Client, TestCase
from unittest.mock import Mock


from recipes.admin.admin import CommentAdmin
from recipes.models import Comment


class CommentsAdminTest(TestCase):
fixtures = ["categories.json", "programs.json", "recipes.json", "comments.json"]

def setUp(self):
self.client = Client()
self.site = AdminSite()
self.user = get_user_model().objects.create_user(
username="testuser", password="testpass", is_staff=True, is_superuser=True
)
self.client.login(username="testuser", password="testpass")
self.model_admin = CommentAdmin(model=Comment, admin_site=self.site)

def test_action_accept(self):
request = Mock(user=self.user, GET={})
actions = self.model_admin.get_actions(request)

expected_results = [
("comment_accept", Comment.STATE_ACCEPTED),
("comment_reject", Comment.STATE_REJECTED),
]
queryset = Comment.objects.all()
comments_count = queryset.count()

for action_name, state in expected_results:
action_meta = actions.get(action_name)
self.assertIsNotNone(action_meta)

action = action_meta[0]

action(self.model_admin, request, queryset)

self.assertEqual(queryset.filter(state=state).count(), comments_count)

0 comments on commit 67d39fc

Please sign in to comment.