本文整理汇总了Python中tests.utils.mock.patch函数的典型用法代码示例。如果您正苦于以下问题:Python patch函数的具体用法?Python patch怎么用?Python patch使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了patch函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_set_up_new_course_failed_to_delete_repo
def test_set_up_new_course_failed_to_delete_repo(self):
resp = self.get_set_up_new_course()
self.assertTrue(resp.status_code, 403)
def force_remove_path_side_effect(path):
# we need to delete the path, or tests followed will fail
force_remove_path(path)
raise OSError("my os error")
data = SINGLE_COURSE_SETUP_LIST[0]["course"]
with mock.patch(
"dulwich.client.GitClient.fetch"
) as mock_fetch, mock.patch(
'relate.utils.force_remove_path'
)as mock_force_remove_path:
mock_fetch.side_effect = RuntimeError("my fetch error")
mock_force_remove_path.side_effect = force_remove_path_side_effect
resp = self.post_create_course(data, raise_error=False)
self.assertTrue(resp.status_code, 200)
self.assertEqual(Course.objects.count(), 0)
self.assertAddMessageCallCount(2)
self.assertAddMessageCalledWith(
"Failed to delete unused repository directory", reset=False)
self.assertAddMessageCalledWith(
"Course creation failed: RuntimeError: my fetch error")
开发者ID:inducer,项目名称:relate,代码行数:28,代码来源:test_versioning.py
示例2: test_set_up_new_course
def test_set_up_new_course(self):
# In this test, we use client instead of request factory to simplify
# the logic.
with self.temporarily_switch_to_user(self.instructor):
# the permission is cached, need to repopulated from db
resp = self.get_set_up_new_course()
self.assertTrue(resp.status_code, 200)
with mock.patch("dulwich.client.GitClient.fetch",
return_value={b"HEAD": b"some_commit_sha"}), \
mock.patch("course.versioning.transfer_remote_refs",
return_value=None), \
mock.patch("course.validation.validate_course_content",
return_value=None):
data = self.get_set_up_new_course_form_data()
resp = self.post_create_course(data, raise_error=False,
login_superuser=False)
self.assertTrue(resp.status_code, 200)
self.assertEqual(Course.objects.count(), 1)
self.assertEqual(Participation.objects.count(), 1)
self.assertEqual(Participation.objects.first().user.username,
"test_instructor")
self.assertAddMessageCalledWith(
"Course content validated, creation succeeded.")
from course.enrollment import get_participation_role_identifiers
# the user who setup the course has role instructor
self.assertTrue(
get_participation_role_identifiers(
Course.objects.first(),
Participation.objects.first()),
"instructor")
开发者ID:inducer,项目名称:relate,代码行数:35,代码来源:test_versioning.py
示例3: test_validate_point_count_called
def test_validate_point_count_called(self):
import random
with mock.patch("course.page.base.validate_point_count")\
as mock_validate_point_count,\
mock.patch("course.page.base.get_auto_feedback")\
as mock_get_auto_feedback:
mock_validate_point_count.side_effect = lambda x: x
mock_get_auto_feedback.side_effect = lambda x: x
for i in range(10):
correctness = random.uniform(0, 15)
feedback = "some feedback"
AnswerFeedback(correctness, feedback)
mock_validate_point_count.assert_called_once_with(correctness)
# because feedback is not None
self.assertEqual(mock_get_auto_feedback.call_count, 0)
mock_validate_point_count.reset_mock()
for i in range(10):
correctness = random.uniform(0, 15)
AnswerFeedback(correctness)
# because get_auto_feedback is mocked, the call_count of
# mock_validate_point_count is only once
mock_validate_point_count.assert_called_once_with(correctness)
mock_validate_point_count.reset_mock()
# because feedback is None
self.assertEqual(mock_get_auto_feedback.call_count, 1)
mock_get_auto_feedback.reset_mock()
AnswerFeedback(correctness=None)
mock_validate_point_count.assert_called_once_with(None)
开发者ID:ishitatsuyuki,项目名称:relate,代码行数:34,代码来源:test_generic.py
示例4: test_success_test_restrict_to_first_attempt_invalid
def test_success_test_restrict_to_first_attempt_invalid(self):
with mock.patch(
"course.analytics.make_grade_histogram"
) as mock_make_g_his, mock.patch(
"course.analytics.make_page_answer_stats_list"
) as mock_make_stats_list, mock.patch(
"course.analytics.make_time_histogram"
) as mock_make_t_his, mock.patch(
"course.analytics.count_participants"
) as mock_count_particpt:
resp = self.get_flow_analytics_view(flow_id=self.flow_id,
restrict_to_first_attempt="foo")
self.assertEqual(resp.status_code, 200)
self.assertResponseContextEqual(
resp, "flow_identifier", self.flow_id)
# make_page_answer_stats_list is called
# using restrict_to_first_attempt = 0
self.assertIn(0, mock_make_stats_list.call_args[0])
self.assertResponseContextEqual(resp, "restrict_to_first_attempt", 0)
self.assertEqual(mock_make_g_his.call_count, 1)
self.assertEqual(mock_make_stats_list.call_count, 1)
self.assertEqual(mock_make_t_his.call_count, 1)
self.assertEqual(mock_count_particpt.call_count, 1)
开发者ID:inducer,项目名称:relate,代码行数:25,代码来源:test_analytics.py
示例5: test_post_form_deprecated
def test_post_form_deprecated(self):
page_id = "half"
with mock.patch(
"course.page.text.TextQuestionBase.process_form_post",
autospec=True
) as mock_process_form_post, mock.patch(
"course.page.text.TextQuestionBase.post_form",
autospec=True) as mock_post_form, mock.patch(
"warnings.warn") as mock_warn:
mock_process_form_post.side_effect = process_form_post_side_effect_super
mock_post_form.side_effect = post_form_side_effect
self.post_answer_by_page_id(
page_id, answer_data={"answer": "1/2"})
self.assertTrue(mock_warn.call_count >= 1)
expected_warn_msg = (
"TextQuestion is using the post_form compatiblity hook, "
"which is deprecated.")
warned_with_expected_msg = False
for args in mock_warn.call_args_list:
if expected_warn_msg in args[0]:
warned_with_expected_msg = True
break
if not warned_with_expected_msg:
self.fail("'%s' is not warned as expected" % expected_warn_msg)
self.assertEqual(self.end_flow().status_code, 200)
self.assertSessionScoreEqual(5)
开发者ID:inducer,项目名称:relate,代码行数:35,代码来源:test_base.py
示例6: test_markup_body_for_title_not_implemented
def test_markup_body_for_title_not_implemented(self):
with mock.patch("course.page.static.Page.markup_body_for_title")\
as mock_markup_body_for_title,\
mock.patch("warnings.warn") as mock_warn:
mock_markup_body_for_title.side_effect = NotImplementedError
mock_warn.side_effect = [None, None, None]
markdown = (
PAGE_WITH_TITLE_MARKDOWN_PATTERN
% {"attr_title": "",
"content_title": ""})
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxNotHasValidPage(resp)
self.assertResponseContextContains(
resp, PAGE_ERRORS,
"no title found in body or title attribute")
# There are other warnings besides this expected warning
self.assertTrue(mock_warn.call_count >= 1)
warned_with_expected_msg = False
expected_warn_msg = ("PageBaseWithTitle subclass 'Page' does not "
"implement markup_body_for_title()")
for args in mock_warn.call_args_list:
if expected_warn_msg in args[0]:
warned_with_expected_msg = True
break
if not warned_with_expected_msg:
self.fail("%s is not warned as expected" % expected_warn_msg)
开发者ID:inducer,项目名称:relate,代码行数:31,代码来源:test_base.py
示例7: test_deprecated_make_page_data_has_warning
def test_deprecated_make_page_data_has_warning(self):
with mock.patch("course.page.text.TextQuestionBase.make_page_data",
autospec=True) as mock_make_page_data, mock.patch(
"warnings.warn") as mock_warn:
mock_make_page_data.side_effect = make_page_data_side_effect_has_data
resp = self.get_page_sandbox_preview_response(TEXT_QUESTION_MARKDOWN)
self.assertEqual(resp.status_code, 200)
self.assertSandboxHasValidPage(resp)
self.assertSandboxWarningTextContain(resp, None)
# There are other warnings besides this expected warning
self.assertTrue(mock_warn.call_count >= 1)
expected_warn_msg = (
"TextQuestion is using the make_page_data compatiblity "
"hook, which is deprecated.")
warned_with_expected_msg = False
for args in mock_warn.call_args_list:
if expected_warn_msg in args[0]:
warned_with_expected_msg = True
break
if not warned_with_expected_msg:
self.fail("'%s' is not warned as expected" % expected_warn_msg)
开发者ID:inducer,项目名称:relate,代码行数:27,代码来源:test_base.py
示例8: setUp
def setUp(self):
super(FlowRuleExceptionTest, self).setUp()
user = factories.UserFactory()
self.participation = factories.ParticipationFactory(
course=self.course,
user=user)
fake_get_course_repo = mock.patch("course.content.get_course_repo")
self.mock_get_course_repo = fake_get_course_repo.start()
self.mock_get_course_repo.return_value = mock.MagicMock()
self.addCleanup(fake_get_course_repo.stop)
fake_get_flow_desc = mock.patch("course.content.get_flow_desc")
self.mock_get_flow_desc = fake_get_flow_desc.start()
self.addCleanup(fake_get_flow_desc.stop)
fake_validate_session_start_rule = mock.patch(
"course.validation.validate_session_start_rule")
self.mock_validate_session_start_rule = (
fake_validate_session_start_rule.start())
self.addCleanup(fake_validate_session_start_rule.stop)
fake_validate_session_access_rule = mock.patch(
"course.validation.validate_session_access_rule")
self.mock_validate_session_access_rule = (
fake_validate_session_access_rule.start())
self.addCleanup(fake_validate_session_access_rule.stop)
fake_validate_session_grading_rule = mock.patch(
"course.validation.validate_session_grading_rule")
self.mock_validate_session_grading_rule = (
fake_validate_session_grading_rule.start())
self.addCleanup(fake_validate_session_grading_rule.stop)
开发者ID:inducer,项目名称:relate,代码行数:32,代码来源:test_models.py
示例9: test_purge_page_view_superuser
def test_purge_page_view_superuser(self):
with self.temporarily_switch_to_user(self.superuser):
resp = self.get_purget_page_view()
self.assertEqual(resp.status_code, 200)
self.assertEqual(
resp.context.get("form").fields["course"].queryset.count(), 2)
with mock.patch("celery.app.task.Task.delay") \
as mocked_delay,\
mock.patch("course.views.monitor_task"):
# post without "submit"
resp = self.post_purget_page_view(self.course1.pk, add_submit=False)
# Nothing happened
self.assertEqual(resp.status_code, 200)
self.assertEqual(mocked_delay.call_count, 0)
# post with "submit"
# Manually fake an async result id
faked_async_result_id = "64907302-3166-43d8-b822"
mocked_delay.return_value.id = faked_async_result_id
resp = self.post_purget_page_view(self.course1.pk)
self.assertRedirects(resp,
reverse("relate-monitor_task",
args=[faked_async_result_id]),
fetch_redirect_response=False)
self.assertEqual(mocked_delay.call_count, 1)
self.assertTrue(self.course1.id in mocked_delay.call_args[0])
开发者ID:inducer,项目名称:relate,代码行数:30,代码来源:test_purge_page_view_data.py
示例10: setUp
def setUp(self):
super(RunCourseUpdateCommandTest, self).setUp()
self.course = factories.CourseFactory(
active_git_commit_sha=self.default_old_sha)
user = factories.UserFactory()
instructor_role = factories.ParticipationRoleFactory(
course=self.course,
identifier="instructor"
)
self.participation = factories.ParticipationFactory(
course=self.course,
preview_git_commit_sha=None,
user=user)
self.participation.roles.set([instructor_role])
self.request = mock.MagicMock()
self.request.user = user
self.pctx = mock.MagicMock()
self.pctx.course = self.course
self.pctx.participation = self.participation
self.repo = mock.MagicMock()
self.content_repo = self.repo
fake_get_dulwich_client_and_remote_path_from_course = mock.patch(
"course.versioning.get_dulwich_client_and_remote_path_from_course")
self.mock_get_dulwich_client_and_remote_path_from_course = (
fake_get_dulwich_client_and_remote_path_from_course.start()
)
self.mock_client = mock.MagicMock()
remote_path = "/remote/path"
self.mock_get_dulwich_client_and_remote_path_from_course.return_value = (
self.mock_client, remote_path
)
self.mock_client.fetch.return_value = {
b"HEAD": self.default_switch_to_sha.encode()}
self.addCleanup(fake_get_dulwich_client_and_remote_path_from_course.stop)
fake_transfer_remote_refs = mock.patch(
"course.versioning.transfer_remote_refs")
self.mock_transfer_remote_refs = fake_transfer_remote_refs.start()
self.addCleanup(fake_transfer_remote_refs.stop)
fake_is_parent_commit = mock.patch("course.versioning.is_parent_commit")
self.mock_is_parent_commit = fake_is_parent_commit.start()
self.mock_is_parent_commit.return_value = False
self.addCleanup(fake_is_parent_commit.stop)
fake_validate_course_content = mock.patch(
"course.validation.validate_course_content")
self.mock_validate_course_content = fake_validate_course_content.start()
self.mock_validate_course_content.return_value = []
self.addCleanup(fake_validate_course_content.stop)
开发者ID:inducer,项目名称:relate,代码行数:57,代码来源:test_versioning.py
示例11: test_form_not_valid
def test_form_not_valid(self):
with mock.patch(
"course.versioning.GitUpdateForm.is_valid"
) as mock_form_valid, mock.patch(
"course.versioning.run_course_update_command"
) as mock_run_update:
mock_form_valid.return_value = False
resp = self.post_update_course_content(
"some_commit_sha", command="update")
self.assertEqual(resp.status_code, 200)
self.assertEqual(mock_run_update.call_count, 0)
开发者ID:inducer,项目名称:relate,代码行数:11,代码来源:test_versioning.py
示例12: test_attribute_error2
def test_attribute_error2(self):
dtime = datetime.datetime(2019, 1, 1)
with mock.patch(
"django.utils.formats.date_format"
) as mock_date_format, mock.patch(
"django.utils.dateformat.format"
) as mock_format:
mock_date_format.side_effect = date_format_side_effect
mock_format.side_effect = format_side_effectformat
result = format_datetime_local(
dtime, format="foo")
self.assertEqual(
result, date_format(dtime, format=get_format("DATETIME_FORMAT")))
开发者ID:inducer,项目名称:relate,代码行数:13,代码来源:test_misc.py
示例13: test_set_up_new_course_git_source_invalid
def test_set_up_new_course_git_source_invalid(self):
data = self.get_set_up_new_course_form_data()
request = self.rf.post(self.get_set_up_new_course_url(), data=data)
request.user = self.instructor
with mock.patch("dulwich.client.GitClient.fetch",
return_value=None), \
mock.patch("course.models.Course.save") as mock_save, \
mock.patch("course.versioning.render"):
resp = versioning.set_up_new_course(request)
self.assertTrue(resp.status_code, 200)
self.assertEqual(mock_save.call_count, 0)
self.assertAddMessageCalledWith(
"No refs found in remote repository")
开发者ID:inducer,项目名称:relate,代码行数:13,代码来源:test_versioning.py
示例14: verify_result_with_configure
def verify_result_with_configure(self, my_site_name):
# home page
with mock.patch(REAL_TRANSLATION_FUNCTION_TO_MOCK) as mock_gettext:
mock_gettext.side_effect = real_trans_side_effect
resp = self.c.get("/")
self.assertEqual(resp.status_code, 200)
self.assertContains(resp, "<title>%s</title>" % my_site_name, html=True)
# Three translations in nav_bar brand, html title and
# "Welcome to RELATE", respectively
self.assertEqual(
self.get_translation_count(mock_gettext, my_site_name), 3)
mock_gettext.reset_mock()
# course page
resp = self.c.get(self.get_course_page_url())
self.assertEqual(resp.status_code, 200)
test_site_name_re = re.compile(
".+<title>.+-.+%s.+</title>.+" % my_site_name, re.DOTALL)
self.assertRegex(resp.content.decode(), test_site_name_re)
# One translation in html title
self.assertEqual(
self.get_translation_count(mock_gettext, my_site_name), 1)
# email
with override_settings(RELATE_REGISTRATION_ENABLED=True, USE_I18N=True):
# render() is mocked so as to count string translated in email rendering
with mock.patch(REAL_TRANSLATION_FUNCTION_TO_MOCK) as mock_gettext,\
mock.patch("course.auth._") as mock_ugettext,\
mock.patch('course.auth.messages'),\
mock.patch('course.auth.render'):
mock_gettext.return_value = "foo"
with self.temporarily_switch_to_user(None):
resp = self.post_sign_up(
data={"username": "Jack", "email": "[email protected]"},
follow=False
)
self.assertTrue(resp.status_code, 200)
self.assertEqual(len(mail.outbox), 1)
# In the view, tranlating RELATE for email title.
self.assertEqual(
self.get_translation_count(mock_ugettext, my_site_name), 1)
# Three RELATE in the email template
self.assertEqual(
self.get_translation_count(mock_gettext, my_site_name), 3)
开发者ID:ishitatsuyuki,项目名称:relate,代码行数:49,代码来源:test_misc.py
示例15: test_regrade_with_access_rules_tag
def test_regrade_with_access_rules_tag(self):
with mock.patch("course.flow.regrade_session") as mock_regrade:
regrade_flow_sessions(self.gopp.course_id,
self.gopp.flow_id,
access_rules_tag="None exist tag",
inprog_value=None
)
mock_regrade.return_value = None
# no regrade happened
self.assertEqual(mock_regrade.call_count, 0)
first_session = models.FlowSession.objects.first()
first_session.access_rules_tag = "some tag"
first_session.save()
regrade_flow_sessions(self.gopp.course_id,
self.gopp.flow_id,
access_rules_tag="some tag",
inprog_value=None
)
self.assertEqual(mock_regrade.call_count, 1)
self.assertIn(first_session, mock_regrade.call_args[0])
self.assertEqual(self.mock_update_state.call_count, 1)
开发者ID:inducer,项目名称:relate,代码行数:27,代码来源:test_tasks.py
示例16: test_finish_in_progress_sessions_past_due_only_dued
def test_finish_in_progress_sessions_past_due_only_dued(self):
# now_datetime > grading_rule.due
fake_grading_rule = self.get_hacked_session_grading_rule(
due=now() + timedelta(days=1))
with mock.patch("course.flow.get_session_grading_rule") as \
mock_get_grading_rule:
mock_get_grading_rule.return_value = fake_grading_rule
finish_in_progress_sessions(
self.gopp.course_id, self.gopp.flow_id,
rule_tag=None, now_datetime=now()+timedelta(days=3),
past_due_only=True)
self.assertEqual(
models.FlowSession.objects.filter(in_progress=True).count(),
0)
self.assertEqual(
models.FlowSession.objects.filter(in_progress=False).count(),
self.all_sessions_count)
self.assertEqual(
models.FlowPageVisitGrade.objects.filter(
visit__flow_session__in=self.ended_sessions).count(),
0)
for ended_session in self.in_progress_sessions:
self.assertTrue(
models.FlowPageVisitGrade.objects.filter(
visit__flow_session=ended_session).count() > 0)
self.assertEqual(
self.mock_update_state.call_count, self.in_progress_sessions_count)
开发者ID:inducer,项目名称:relate,代码行数:32,代码来源:test_tasks.py
示例17: test_expire_in_progress_sessions_past_due_only_not_dued
def test_expire_in_progress_sessions_past_due_only_not_dued(self):
# now_datetime <= grading_rule.due
fake_grading_rule = self.get_hacked_session_grading_rule(
due=now() + timedelta(days=1))
with mock.patch("course.flow.get_session_grading_rule") as \
mock_get_grading_rule:
mock_get_grading_rule.return_value = fake_grading_rule
expire_in_progress_sessions(
self.gopp.course.id, self.gopp.flow_id,
rule_tag=None, now_datetime=now(),
past_due_only=True)
self.assertEqual(
models.FlowSession.objects.filter(in_progress=True).count(),
self.in_progress_sessions_count)
self.assertEqual(
models.FlowSession.objects.filter(in_progress=False).count(),
self.ended_sessions_count)
self.assertEqual(self.mock_update_state.call_count,
self.in_progress_sessions_count)
self.assertEqual(
self.mock_update_state.call_count, self.in_progress_sessions_count)
开发者ID:inducer,项目名称:relate,代码行数:26,代码来源:test_tasks.py
示例18: test_update_course_no_requested
def test_update_course_no_requested(self):
with mock.patch(HANDLE_ENROLLMENT_PATH) as mock_handle_enrollment:
mock_handle_enrollment.return_value = None
self.course.listed = not self.course.listed
self.course.save()
self.assertEqual(mock_handle_enrollment.call_count, 0)
开发者ID:inducer,项目名称:relate,代码行数:7,代码来源:test_receivers.py
示例19: test_form_invalid
def test_form_invalid(self):
with mock.patch("course.exam.ExamCheckInForm.is_valid"
) as mock_is_valid:
mock_is_valid.return_value = False
resp = self.post_check_in_for_exam_view(data=self.get_post_data())
self.assertEqual(resp.status_code, 200)
self.assertEqual(self.ticket.state, constants.exam_ticket_states.valid)
开发者ID:inducer,项目名称:relate,代码行数:7,代码来源:test_exam.py
示例20: setUp
def setUp(self):
self.requset = mock.MagicMock()
self.requset.relate_facilities = ["fa1", "fa2"]
fake_get_facilities_config = mock.patch(
"course.utils.get_facilities_config")
self.mock_get_facilities_config = fake_get_facilities_config.start()
self.addCleanup(fake_get_facilities_config.stop)
开发者ID:inducer,项目名称:relate,代码行数:7,代码来源:test_exam.py
注:本文中的tests.utils.mock.patch函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论