Merge pull request #16009 from edx/jmbowman/pytest_unit_tests

PLAT-1677 Switch to pytest for unit tests
This commit is contained in:
Brian Beggs
2017-09-25 08:57:01 -04:00
committed by GitHub
30 changed files with 293 additions and 200 deletions

View File

@@ -1,11 +1,11 @@
from contextlib import contextmanager
from datetime import datetime, timedelta
import itertools
from unittest import TestCase
import ddt
import pytz
import waffle
from django.test import TestCase
from course_modes.models import CourseMode
from openedx.core.djangoapps.certificates import api

View File

@@ -549,11 +549,15 @@ class CreditProviderCallbackViewTests(UserMixin, TestCase):
self.assertEqual(response.status_code, 403)
@ddt.data(
to_timestamp(datetime.datetime.now(pytz.UTC) - datetime.timedelta(0, 60 * 15 + 1)),
-datetime.timedelta(0, 60 * 15 + 1),
'invalid'
)
def test_post_with_invalid_timestamp(self, timestamp):
def test_post_with_invalid_timestamp(self, timedelta):
""" Verify HTTP 400 is returned for requests with an invalid timestamp. """
if timedelta == 'invalid':
timestamp = timedelta
else:
timestamp = to_timestamp(datetime.datetime.now(pytz.UTC) + timedelta)
request_uuid = self._create_credit_request_and_get_uuid()
response = self._credit_provider_callback(request_uuid, 'approved', timestamp=timestamp)
self.assertEqual(response.status_code, 400)

View File

@@ -1,5 +1,6 @@
import datetime
import itertools
from copy import deepcopy
from unittest import skipUnless
import attr
@@ -201,10 +202,8 @@ class TestSendRecurringNudge(CacheIsolationTestCase):
@ddt.data(*itertools.product((1, 10, 100), (3, 10)))
@ddt.unpack
@override_settings()
def test_templates(self, message_count, day):
settings.TEMPLATES[0]['OPTIONS']['string_if_invalid'] = "TEMPLATE WARNING - MISSING VARIABLE [%s]"
user = UserFactory.create()
schedules = [
ScheduleFactory.create(
@@ -226,20 +225,23 @@ class TestSendRecurringNudge(CacheIsolationTestCase):
sent_messages = []
with patch.object(tasks, '_recurring_nudge_schedule_send') as mock_schedule_send:
mock_schedule_send.apply_async = lambda args, *_a, **_kw: sent_messages.append(args)
templates_override = deepcopy(settings.TEMPLATES)
templates_override[0]['OPTIONS']['string_if_invalid'] = "TEMPLATE WARNING - MISSING VARIABLE [%s]"
with self.settings(TEMPLATES=templates_override):
with patch.object(tasks, '_recurring_nudge_schedule_send') as mock_schedule_send:
mock_schedule_send.apply_async = lambda args, *_a, **_kw: sent_messages.append(args)
with self.assertNumQueries(2):
tasks.recurring_nudge_schedule_hour(
self.site_config.site.id, day, test_time_str, [schedules[0].enrollment.course.org],
)
with self.assertNumQueries(2):
tasks.recurring_nudge_schedule_hour(
self.site_config.site.id, day, test_time_str, [schedules[0].enrollment.course.org],
)
self.assertEqual(len(sent_messages), 1)
self.assertEqual(len(sent_messages), 1)
for args in sent_messages:
tasks._recurring_nudge_schedule_send(*args)
for args in sent_messages:
tasks._recurring_nudge_schedule_send(*args)
self.assertEqual(mock_channel.deliver.call_count, 1)
for (_name, (_msg, email), _kwargs) in mock_channel.deliver.mock_calls:
for template in attr.astuple(email):
self.assertNotIn("TEMPLATE WARNING", template)
self.assertEqual(mock_channel.deliver.call_count, 1)
for (_name, (_msg, email), _kwargs) in mock_channel.deliver.mock_calls:
for template in attr.astuple(email):
self.assertNotIn("TEMPLATE WARNING", template)