def test_verification_default_ordered_list_mock(self, unordered_set_mock: MagicMock): verified_list_data = {'items': [1, 2, 3]} task = AbstractTask(info={'url': 'https://bla.pl'}) unordered_set_mock.side_effect = [([1, 2, 3], 1)] task.cross_check([verified_list_data, verified_list_data]) unordered_set_mock.assert_called_with([[1, 2, 3], [1, 2, 3]])
def test_verification_default_equals_true(self): verified_dict_data = {'fld': 'val1'} task = AbstractTask(info={'url': 'https://bla.pl'}) (result, confidence) = task.cross_check( [verified_dict_data, verified_dict_data]) self.assertEquals(result, verified_dict_data)
def test_verification_default_equals_false(self, after_save_mock: MagicMock, save_verified_data_mock: MagicMock): task = AbstractTask(info={'url': 'https://bla.pl'}) decision = task.verify_and_save([{'fld': 'val1'}, {'fld': 'whatever'}]) self.assertEqual(decision, False) after_save_mock.assert_not_called() save_verified_data_mock.assert_not_called()
def test_verification_default_ordered_list_true(self): verified_list_data = {'items': [1, 2, 3]} task = AbstractTask(info={'url': 'https://bla.pl'}) (result, confidence) = task.cross_check( [verified_list_data, verified_list_data]) self.assertEquals(result, verified_list_data)
def test_verification_default_equals_mock(self, equals_mock: MagicMock): verified_dict_data = {'fld': 'val1'} task = AbstractTask(info={'url': 'https://bla.pl'}) equals_mock.return_value = (1, verified_dict_data) task.cross_check([verified_dict_data, verified_dict_data]) equals_mock.assert_called_with(['val1', 'val1'])
def test_verification_default_complex_true(self): verified_dict_data = {'cars': [{'model': 'A', 'year': 2011}, {'model': 'B', 'year': 2012}]} task = AbstractTask(info={'url': 'https://bla.pl'}) (result, confidence) = task.cross_check([verified_dict_data, verified_dict_data]) self.assertEquals(confidence, 1) self.assertEquals(result, verified_dict_data)
def test_verification_default_complex(self, equals_mock: MagicMock): verified_dict_data = {'cars': [{'model': 'A', 'year': 2011}, {'model': 'B', 'year': 2012}]} task = AbstractTask(info={'url': 'https://bla.pl'}) equals_mock.side_effect = lambda values: (values[0], 1) task.cross_check([verified_dict_data, verified_dict_data]) # equals_mock.assert_not_called_with([verified_dict_data, verified_dict_data]) calls = [call(['A', 'A']), call([2011, 2011]), call(['B', 'B']), call([2012, 2012])] equals_mock.assert_has_calls(calls, any_order=True)
def test_flow_of_verified(self, save_verified_data_mock: MagicMock, after_save_mock: MagicMock): verified_data = {'fld': 'val1'} task = AbstractTask(info={'url': 'https://bla.pl'}) # TODO test verification on one input task.verify_and_save([verified_data, verified_data]) save_verified_data_mock.assert_called_with(verified_data) after_save_mock.assert_called_with(verified_data)
def test_all_same_num(self): entry = [1, 2, 3, 4] task = AbstractTask(info={'url': 'https://bla.pl'}) (result, confidence) = OrderedListVerifier(task, '')([entry, entry, entry]) self.assertEquals(result, entry) self.assertEquals(confidence, 1)
def test_all_same_text(self): entry = ['val1', 'val2', 'val3', 'val4'] task = AbstractTask(info={'url': 'https://bla.pl'}) (result, confidence) = OrderedListVerifier(task, '')([entry, entry, entry]) self.assertEquals(result, entry) self.assertEquals(confidence, 1)
def test_ordering(self): task = AbstractTask(info={'url': 'https://bla.pl'}) (result, confidence) = OrderedListVerifier(task, '')([[1, 2, 3, 4], [4, 3, 2, 1]]) self.assertLess(confidence, 1) self.assertGreaterEqual(confidence, 0)
def test_different_length2(self): entry = [1, 2, 3, 4] task = AbstractTask(info={'url': 'https://bla.pl'}) (result, confidence) = OrderedListVerifier(task, '')([entry + [5], entry, entry]) self.assertEquals(result, entry) self.assertGreaterEqual(confidence, 0) self.assertLess(confidence, 1)
def test_no_standing_out(self): task = AbstractTask(info={'url': 'https://bla.pl'}) (result, confidence) = OrderedListVerifier(task, '')([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) self.assertLess(confidence, 1) self.assertGreaterEqual(confidence, 0)
def test_get_context_data(self, initialize_task_data_mock: MagicMock, _get_task_mock: MagicMock): request = self.factory.get(self.fake_path) view = TaskView() view = setup_view(view, request) view.task = AbstractTask() context = view.get_context_data() self.assertIsInstance(context['task'], AbstractTask) self.assertEqual(context['task'], view.task)
def test_get_context_data_presenter_typeerror( self, get_presenter_mock: MagicMock, initialize_task_data_mock: MagicMock, _get_task_mock: MagicMock): get_presenter_mock.side_effect = TypeError request = self.factory.get(self.fake_path) view = TaskView() view = setup_view(view, request) view.task = AbstractTask() with self.assertRaises(PresenterNotDefined): view.get_context_data()
def test_initialize_task_data(self, klass_from_name_mock: MagicMock): del self.task_data['info']['task_form'] request = self.factory.get(self.fake_path) view = TaskView() view = setup_view(view, request) view.task = AbstractTask(**self.task_data) view.initialize_task_data() # TODO: divide to 2 test cases # self.assertEqual(view.template_name, self.task_data.get('info').get('template_name')) self.assertEqual(view.template_name, self.task_data.get('info').get('template_name'))
def test_get_context_data(self, initialize_task_data_mock: MagicMock, _get_task_mock: MagicMock): request = self.factory.get(self.fake_path) view = TaskView() view = setup_view(view, request) view.task = AbstractTask() context = view.get_context_data() self.assertIsInstance(context['task'], AbstractTask) self.assertEqual(context['task'], view.task) self.assertEqual(context['project_id'], PYBOSSA_PROJECT_ID) from moonsheep.settings import PYBOSSA_BASE_URL self.assertEqual(context['pybossa_url'], PYBOSSA_BASE_URL)
def test_get_no_tasks(self, get_context_data_mock: MagicMock, _get_form_class_data_mock: MagicMock, _get_task_mock: MagicMock): _get_task_mock.side_effect = NoTasksLeft request = self.factory.get(self.fake_path) view = TaskView() view = setup_view(view, request, **self.task_data) view.task = AbstractTask() view.template_name = self.template_name response = view.get(request) self.assertEqual(view.task, None) self.assertEqual(view.error_message, 'Broker returned no tasks') self.assertEqual(view.error_template, 'error-messages/no-tasks.html') self.assertEqual(response.status_code, 200) _get_task_mock.assert_any_call() _get_form_class_data_mock.assert_not_called() get_context_data_mock.assert_any_call()
def test_get_presenter_not_defined(self, get_context_data_mock: MagicMock, _get_form_class_data_mock: MagicMock, _get_task_mock: MagicMock): _get_task_mock.side_effect = PresenterNotDefined request = self.factory.get(self.fake_path) view = TaskView() view = setup_view(view, request, **self.task_data) view.task = AbstractTask() view.template_name = self.template_name response = view.get(request) self.assertEqual(view.task, None) self.assertEqual(view.error_message, 'Presenter not defined') self.assertEqual(view.error_template, 'error-messages/presenter-not-defined.html') self.assertEqual(response.status_code, 200) _get_task_mock.assert_any_call() _get_form_class_data_mock.assert_not_called() get_context_data_mock.assert_any_call()
def test_get_improperly_configured(self, get_context_data_mock: MagicMock, _get_form_class_data_mock: MagicMock, _get_task_mock: MagicMock): _get_task_mock.side_effect = ImproperlyConfigured request = self.factory.get(self.fake_path) view = TaskView() view = setup_view(view, request, **self.task_data) view.task = AbstractTask() view.template_name = self.template_name response = view.get(request) self.assertEqual(view.task, None) self.assertEqual(view.error_message, 'Improperly configured PyBossa') self.assertEqual(view.error_template, 'error-messages/improperly-configured.html') self.assertEqual(response.status_code, 200) _get_task_mock.assert_any_call() _get_form_class_data_mock.assert_not_called() get_context_data_mock.assert_any_call()
def update_total_progress(task: models.Task): """ Update total progress of given task and all of its parents # TODO in the future this should be run effectively by some backend job regularly #138 :param task: :return: """ from moonsheep.tasks import AbstractTask while True: # Update total progress of given task # total_progress = ( average(subtasks progress) * average_number_of_subtasks(configured in task) + own_progress) # / (average_number_of_subtasks(configured in task) + 1) if task.own_progress < 100: # it has not subtasks yet, guess its number average_number_of_subtasks = AbstractTask.create_task_instance(task).average_subtasks_count() task.total_progress = task.own_progress / (average_number_of_subtasks + 1) else: # own_progress == 100, so there might be subtasks subtasks = Task.objects.filter(parent=task) \ .aggregate(average_progress=Avg('total_progress'), count=Count('id')) task.total_progress = Decimal( subtasks['average_progress'] * subtasks['count'] + 100) / ( subtasks['count'] + 1) task.save() # Update total progress of all of its parents if task.parent_id: task = task.parent else: break # now it must be a doc-descendant task, so compute progress of a document # Progress of a document is an average total progress of all of tasks created on such document. doc_progress = Task.objects.filter(doc_id=task.doc_id, parent=None) \ .aggregate(Avg('total_progress'))['total_progress__avg'] Document = MOONSHEEP['DOCUMENT_MODEL'] Document.objects.filter(id=task.doc_id).update(progress=doc_progress)
def test_create_task_instance(self): task = AbstractTask.create_task_instance( 'moonsheep.tasks.AbstractTask', info={'url': 'https://bla.pl'}) self.assertIsInstance(task, AbstractTask)