def test_stage_state(self, review_stage_state, visited, expected_stage_state): self.block.visited = visited patched_review_subjects = mock.PropertyMock() with patch_obj(self.block_to_test, 'review_status') as patched_review_status, \ patch_obj(self.block_to_test, 'review_subjects', patched_review_subjects): patched_review_status.return_value = review_stage_state patched_review_subjects.return_value = [{'id': 1}, {'id': 2}] self.assertEqual(self.block.get_stage_state(), expected_stage_state)
def test_stage_state_no_reviews_assigned(self, review_stage_state, visited): self.block.visited = visited patched_review_subjects = mock.PropertyMock() with patch_obj(self.block_to_test, 'review_status') as patched_review_status, \ patch_obj(self.block_to_test, 'review_subjects', patched_review_subjects): patched_review_status.return_value = review_stage_state patched_review_subjects.return_value = [] self.assertEqual(self.block.get_stage_state(), StageState.NOT_STARTED)
def test_stage_state_no_reviews_assigned(self, review_stage_state, visited): self.block.visited = visited patched_review_subjects = mock.PropertyMock() with patch_obj(self.block_to_test, 'review_status') as patched_review_status, \ patch_obj(self.block_to_test, 'review_subjects', patched_review_subjects): patched_review_status.return_value = review_stage_state patched_review_subjects.return_value = [] self.assertEqual(self.block.get_stage_state(), StageState.NOT_STARTED)
def test_review_status(self, peers_to_review, questions, reviews, expected_result): self.project_api_mock.get_peer_review_items_for_group.return_value = reviews with patch_obj(self.block_to_test, 'review_subjects', mock.PropertyMock()) as patched_review_subjects, \ patch_obj(self.block_to_test, 'required_questions', mock.PropertyMock()) as patched_questions: patched_review_subjects.return_value = [ReducedUserDetails(id=rev_id) for rev_id in peers_to_review] patched_questions.return_value = [make_question(q_id, 'irrelevant') for q_id in questions] self.assertEqual(self.block.review_status(), expected_result) self.project_api_mock.get_peer_review_items_for_group.assert_called_once_with( self.workgroup_data.id, self.activity_mock.content_id )
def test_review_status(self, peers_to_review, questions, reviews, expected_result): self.project_api_mock.get_peer_review_items_for_group.return_value = reviews with patch_obj(self.block_to_test, 'review_subjects', mock.PropertyMock()) as patched_review_subjects, \ patch_obj(self.block_to_test, 'required_questions', mock.PropertyMock()) as patched_questions: patched_review_subjects.return_value = [ReducedUserDetails(id=rev_id) for rev_id in peers_to_review] patched_questions.return_value = [make_question(q_id, 'irrelevant') for q_id in questions] self.assertEqual(self.block.review_status(), expected_result) self.project_api_mock.get_peer_review_items_for_group.assert_called_once_with( self.workgroup_data.id, self.activity_mock.content_id )
def test_marks_visited_on_student_view(self, can_mark_complete, should_set_visited): can_mark_mock = mock.PropertyMock(return_value=can_mark_complete) self.assertFalse(self.block.visited) # precondition check with patch_obj(self.block_to_test, 'can_mark_complete', can_mark_mock): self.block.student_view({}) self.assertEqual(self.block.visited, should_set_visited)
def assert_group_completion(self, group, questions, expected_result): with patch_obj(self.block_to_test, 'required_questions', mock.PropertyMock()) as patched_questions: patched_questions.return_value = [make_question(q_id, 'irrelevant') for q_id in questions] group_completion = self.block.get_external_group_status(group) self.assertEqual(group_completion, expected_result)
def __enter__(self): def _has_child_of_category(child_category): return child_category in self._child_categories has_child_mock = mock.Mock() patch_has_child = patch_obj(self._block, 'has_child_of_category', has_child_mock) has_child_mock.side_effect = _has_child_of_category self._patchers.append(patch_has_child) questions_mock = mock.PropertyMock() questions_mock.return_value = self._questions patch_questions = patch_obj(type(self._block), 'questions', questions_mock) self._patchers.append(patch_questions) for patch in self._patchers: patch.start()
def test_marks_complete_on_student_view(self, can_mark_complete, should_call_mark_complete): can_mark_mock = mock.PropertyMock(return_value=can_mark_complete) with patch_obj(self.block_to_test, 'can_mark_complete', can_mark_mock): self.block.student_view({}) if should_call_mark_complete: self.runtime_mock.publish.assert_called_with(self.block, 'progress', {'user_id': self.block.user_id}) else: self.assertFalse(self.runtime_mock.publish.called)
def test_review_status(self, groups, questions, reviews, expected_result): def get_reviews(group_id, _component_id): return reviews.get(group_id, []) expected_calls = [ mock.call(group_id, self.block.activity_content_id) for group_id in groups ] self.project_api_mock.get_workgroup_review_items_for_group.side_effect = get_reviews with patch_obj(self.block_to_test, 'review_subjects', mock.PropertyMock()) as patched_review_subjects, \ patch_obj(self.block_to_test, 'required_questions', mock.PropertyMock()) as patched_questions: patched_review_subjects.return_value = [WorkgroupDetails(id=rev_id) for rev_id in groups] patched_questions.return_value = [make_question(q_id, 'irrelevant') for q_id in questions] self.assertEqual(self.block.review_status(), expected_result) self.assertEqual(self.project_api_mock.get_workgroup_review_items_for_group.mock_calls, expected_calls)
def test_can_mark_compete_suite(self, reviewers, questions, reviews, expected_result): self.get_reviewer_ids_mock.return_value = reviewers self.get_reviews_mock.return_value = reviews with patch_obj( self.block_to_test, 'required_questions', mock.PropertyMock() ) as patched_required_questions: patched_required_questions.return_value = questions self.assertEqual(self.block.can_mark_complete, expected_result)
def test_can_mark_compete_suite(self, reviewers, questions, reviews, expected_result): self.get_reviewer_ids_mock.return_value = reviewers self.get_reviews_mock.return_value = reviews with patch_obj(self.block_to_test, 'required_questions', mock.PropertyMock()) as patched_required_questions: patched_required_questions.return_value = questions self.assertEqual(self.block.can_mark_complete, expected_result)
def test_marks_complete_on_student_view(self, can_mark_complete, should_call_mark_complete): can_mark_mock = mock.PropertyMock(return_value=can_mark_complete) with patch_obj(self.block_to_test, 'can_mark_complete', can_mark_mock): self.block.student_view({}) if should_call_mark_complete: self.runtime_mock.publish.assert_called_with( self.block, 'progress', {'user_id': self.block.user_id}) else: self.assertFalse(self.runtime_mock.publish.called)
def assert_users_completion(self, expected_result, questions, target_users, workgroups=None): target_workgroups = workgroups if workgroups else ['irrelevant'] expected_completed, expected_partially_completed = expected_result with patch_obj(self.block_to_test, 'required_questions', mock.PropertyMock()) as patched_questions: patched_questions.return_value = [make_question(q_id, 'irrelevant') for q_id in questions] completed, partially_completed = self.block.get_users_completion( target_workgroups, [ReducedUserDetails(id=uid) for uid in target_users] ) self.assertEqual(completed, expected_completed) self.assertEqual(partially_completed, expected_partially_completed)
def test_review_status(self, groups, questions, reviews, expected_result): def get_reviews(group_id, _component_id): return reviews.get(group_id, []) expected_calls = [ mock.call(group_id, self.block.activity_content_id) for group_id in groups ] self.project_api_mock.get_workgroup_review_items_for_group.side_effect = get_reviews with patch_obj(self.block_to_test, 'review_subjects', mock.PropertyMock()) as patched_review_subjects, \ patch_obj(self.block_to_test, 'required_questions', mock.PropertyMock()) as patched_questions: patched_review_subjects.return_value = [ WorkgroupDetails(id=rev_id) for rev_id in groups ] patched_questions.return_value = [ make_question(q_id, 'irrelevant') for q_id in questions ] self.assertEqual(self.block.review_status(), expected_result) self.assertEqual( self.project_api_mock.get_workgroup_review_items_for_group. mock_calls, expected_calls)
def test_ta_get_external_group_status(self, ta_reviewers, questions, review_items, expected_result): group_to_review = mk_wg(GROUP_ID, [{"id": 1}]) self.activity_mock.is_ta_graded = True self._set_project_api_responses( group_to_review, { group_to_review.id: [self._parse_review_item_string(item) for item in items] for group_id, items in review_items.iteritems() } ) with patch_obj(self.block, 'is_user_ta') as patched_outsider_allowed: patched_outsider_allowed.side_effect = lambda user_id, _course_id: user_id in ta_reviewers self.assert_group_completion(group_to_review, questions, expected_result)
def test_ta_get_external_group_status(self, ta_reviewers, questions, review_items, expected_result): group_to_review = mk_wg(GROUP_ID, [{"id": 1}]) self.activity_mock.is_ta_graded = True self._set_project_api_responses( group_to_review, { group_to_review.id: [self._parse_review_item_string(item) for item in items] for group_id, items in review_items.iteritems() }) with patch_obj(self.block, 'is_user_ta') as patched_outsider_allowed: patched_outsider_allowed.side_effect = lambda user_id, _course_id: user_id in ta_reviewers self.assert_group_completion(group_to_review, questions, expected_result)
def test_can_mark_complete_admin_grader(self, available_now): with patch_obj(self.block_to_test, 'is_admin_grader', mock.PropertyMock(return_value=True)), \ patch_obj(self.block_to_test, 'available_now', mock.PropertyMock(return_value=available_now)): self.assertEqual(self.block.can_mark_complete, True)
def test_can_mark_complete_no_questions_returns_true(self): with patch_obj(self.block_to_test, 'required_questions') as patched_required_questions: patched_required_questions.return_value = [] self.assertTrue(self.block.can_mark_complete)
def test_can_mark_complete_admin_grader(self, available_now): with patch_obj(self.block_to_test, 'is_admin_grader', mock.PropertyMock(return_value=True)), \ patch_obj(self.block_to_test, 'available_now', mock.PropertyMock(return_value=available_now)): self.assertEqual(self.block.can_mark_complete, True)
def test_can_mark_complete_no_questions_returns_true(self): with patch_obj(self.block_to_test, 'required_questions') as patched_required_questions: patched_required_questions.return_value = [] self.assertTrue(self.block.can_mark_complete)