def test_calling_task_with_no_blocks(self, mock_update): with self.assertNumQueries(1): aggregation_tasks.update_aggregators( username='******', course_key='course-v1:OpenCraft+Onboarding+2018') mock_update.assert_called_once_with(self.user, self.course_key, frozenset(), False)
def test_multiple_partial_updates(self): completion = BlockCompletion.objects.create( user=self.user, context_key=self.course_key, block_key=self.blocks[4], completion=0.75, ) ''' Queries are for the following table * Select - auth_user (fetch user details) - completion_aggregator_aggregator (user specific for specific course) - completion_blockcompletion (user specific) * Insert or Update Query - completion_aggregator_aggregator (insert aggregation data) * Update query - completion_aggregator_stalecompletion (user specific) ''' # pylint: disable=pointless-string-statement with self.assertNumQueries(5): aggregation_tasks.update_aggregators( self.user.username, six.text_type(self.course_key), {six.text_type(completion.block_key)}) new_completions = [ BlockCompletion.objects.create( user=self.user, context_key=self.course_key, block_key=self.blocks[5], completion=1.0, ), BlockCompletion.objects.create( user=self.user, context_key=self.course_key, block_key=self.blocks[6], completion=0.5, ), ] with self.assertNumQueries(5): aggregation_tasks.update_aggregators( username=self.user.username, course_key=six.text_type(self.course_key), block_keys=[ six.text_type(comp.block_key) for comp in new_completions ]) course_agg = Aggregator.objects.get(course_key=self.course_key, block_key=self.blocks[0]) chap1_agg = Aggregator.objects.get(course_key=self.course_key, block_key=self.blocks[1]) chap2_agg = Aggregator.objects.get(course_key=self.course_key, block_key=self.blocks[2]) self.assertEqual(chap1_agg.earned, 0.75) self.assertEqual(chap1_agg.last_modified, completion.modified) self.assertEqual(chap2_agg.earned, 1.5) self.assertEqual(chap2_agg.last_modified, new_completions[1].modified) self.assertEqual(course_agg.earned, 2.25) self.assertEqual(course_agg.last_modified, new_completions[1].modified)
def test_unexpected_updater_errors(self): # Verify that no exception is bubbled up when the constructor errors, but that the update method is not called. with mock.patch.object(AggregationUpdater, '__init__') as mock_update_constructor: mock_update_constructor.side_effect = RuntimeError('test') with pytest.raises(RuntimeError): aggregation_tasks.update_aggregators( username='******', course_key='course-v1:OpenCraft+Onboarding+2018')
def test_task_with_unknown_user(self): StaleCompletion.objects.create(username='******', course_key='course-v1:edx+course+test', resolved=False) with mock.patch('completion_aggregator.core.update_aggregators' ) as mock_update_handler: aggregation_tasks.update_aggregators( username='******', course_key='course-v1:edx+course+test') assert StaleCompletion.objects.get(username='******').resolved mock_update_handler.assert_not_called()
def test_expected_updater_errors(self, exception_class): # Verify that no exception is bubbled up when the constructor errors, but that the update method is not called. with mock.patch.object(AggregationUpdater, '__init__') as mock_update_constructor: mock_update_constructor.side_effect = exception_class('test') with mock.patch.object(AggregationUpdater, 'update') as mock_update_action: aggregation_tasks.update_aggregators( username='******', course_key='course-v1:OpenCraft+Onboarding+2018' ) assert not mock_update_action.called
def test_calling_task_with_changed_blocks(self, mock_update): with self.assertNumQueries(1): aggregation_tasks.update_aggregators( username='******', course_key='course-v1:OpenCraft+Onboarding+2018', block_keys=[ 'block-v1:OpenCraft+Onboarding+2018+type@html+block@course-chapter-html0', 'block-v1:OpenCraft+Onboarding+2018+type@html+block@course-chapter-html1', ], ) mock_update.assert_called_once_with( self.user, self.course_key, self.block_keys, False, )
def test_end_to_end_task_calling(self): ''' Queries are for the following table * Select - auth_user (fetch user details) - completion_aggregator_aggregator (user specific for specific course) - completion_blockcompletion (user specific) - auth user (fetch user details) * Insert or Update Query - completion_aggregator_aggregator (insert aggregation data) * Update query - completion_aggregator_stalecompletion (user specific) ''' with self.assertNumQueries(6): aggregation_tasks.update_aggregators(username='******', course_key='course-v1:edx+course+test') self.agg.refresh_from_db() assert self.agg.last_modified > self.agg_modified assert self.agg.earned == 1.0 assert self.agg.possible == 5.0