Skip to content

Commit

Permalink
refactor: used ddt in tests to make it concise
Browse files Browse the repository at this point in the history
  • Loading branch information
AhtishamShahid committed Aug 4, 2023
1 parent c19b388 commit 7415d40
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 32 deletions.
42 changes: 12 additions & 30 deletions lms/djangoapps/discussion/rest_api/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import datetime, timedelta
from unittest.mock import Mock

import ddt
from django.conf import settings
from httpretty import httpretty
from pytz import UTC
Expand Down Expand Up @@ -310,6 +311,7 @@ def test_comment_creators_own_response(self):
self.assertEqual(args_comment.app_name, 'discussion')


@ddt.ddt
class TestBlackoutDates(ForumsEnableMixin, CommentsServiceMockMixin, ModuleStoreTestCase):
"""
Test for the is_posting_allowed function
Expand Down Expand Up @@ -354,41 +356,21 @@ def _check_posting_allowed(self, posting_restriction):
self.course.get_discussion_blackout_datetimes()
)

def test_posting_disabled(self):
@ddt.data(
(PostingRestriction.DISABLED, True),
(PostingRestriction.ENABLED, False),
(PostingRestriction.SCHEDULED, False),
)
@ddt.unpack
def test_blackout_dates(self, restriction, state):
"""
Test posting when the posting restriction is disabled.
Assertion:
Posting should be allowed.
"""
date_ranges = self._get_date_ranges()
self._set_discussion_blackouts(date_ranges)

posting_allowed = self._check_posting_allowed(PostingRestriction.DISABLED)
self.assertTrue(posting_allowed)

def test_posting_enabled(self):
"""
Test posting when the posting restriction is enabled.
Assertion:
Posting should not be allowed.
Test is_posting_allowed function with the misc posting restriction
"""
date_ranges = self._get_date_ranges()
self._set_discussion_blackouts(date_ranges)

posting_allowed = self._check_posting_allowed(PostingRestriction.ENABLED)
self.assertFalse(posting_allowed)

def test_posting_scheduled(self):
"""
Test posting when the posting restriction is scheduled.
Assertion:
Posting should not be allowed.
"""
date_ranges = self._get_date_ranges()
self._set_discussion_blackouts(date_ranges)

posting_allowed = self._check_posting_allowed(PostingRestriction.SCHEDULED)
self.assertFalse(posting_allowed)
posting_allowed = self._check_posting_allowed(restriction)
self.assertEqual(state, posting_allowed)

def test_posting_scheduled_future(self):
"""
Expand Down
7 changes: 5 additions & 2 deletions lms/djangoapps/discussion/rest_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ def discussion_open_for_user(course, user):
course: Course to check discussions for
user: User to check for privileges in course
"""
discussions_config = DiscussionsConfiguration.get(course.id).posting_restrictions
discussions_posting_restrictions = DiscussionsConfiguration.get(course.id).posting_restrictions
blackout_dates = course.get_discussion_blackout_datetimes()
return is_posting_allowed(discussions_config, blackout_dates) or has_discussion_privileges(user, course.id)
return (
is_posting_allowed(discussions_posting_restrictions, blackout_dates) or
has_discussion_privileges(user, course.id)
)


def set_attribute(threads, attribute, value):
Expand Down

0 comments on commit 7415d40

Please sign in to comment.