예제 #1
0
    def test_merge_task(self):
        file = data_mocking.create_file({'project_id': self.project.id},
                                        self.session)
        original_file = data_mocking.create_file(
            {'project_id': self.project.id}, self.session)
        completion_dir = data_mocking.create_directory(
            {
                'project': self.project,
                'user': self.project_data['users'][0],
                'files': [original_file]
            }, self.session)
        job = data_mocking.create_job(
            {
                'name': 'my-test-job-{}'.format(1),
                'project': self.project,
                'completion_directory_id': completion_dir.id
            }, self.session)

        task = data_mocking.create_task(
            {
                'name': 'test task',
                'file': file,
                'job': job,
                'file_original': original_file
            }, self.session)
        print('aasadsd', task.job, task.job.completion_directory_id)
        task_complete.merge_task(self.session, job, task)
        self.session.commit()
        file_link = self.session.query(WorkingDirFileLink).filter(
            WorkingDirFileLink.working_dir_id ==
            task.job.completion_directory_id,
            WorkingDirFileLink.file_id == task.file_id).all()
        self.assertEqual(len(file_link), 1)
예제 #2
0
    def test_send_task_to_scale_ai(self):
        task_file_type = None
        task = data_mocking.create_task({'name': 'test task'}, self.session)
        attachment = None
        # Unsupported file types: label
        task_file_type = 'image'
        meta_data = {
            'label_file_list': [],
            'diffgram_task_id': task.id,
            'source_id': -1  # TODO: add source when ready.
        }

        objects_to_annotate = []
        if 'label_file_list_serialized' in task.label_dict:
            for elm in task.label_dict['label_file_list_serialized']:
                if elm['type'] == 'label':
                    objects_to_annotate.append(elm['label']['name'])

        with patch.object(self.scaleaiconn,
                          'put_data',
                          return_value={'result': ScaleAITask(param_dict={'task_id': None}, client=self.scaleaiconn.connection_client)}):
            result = self.scaleaiconn.put_data({
                'action_type': 'bounding_box',
                'project': 'test',
                'instruction': 'Please Draw a box on the image.',
                'callback_url': 'http://diffgram.com/scaleai/task-completed',
                'attachment_type': task_file_type,
                'attachment': 'https://thumbor.forbes.com/thumbor/250x382/https://blogs-images.forbes.com/dorothypomerantz/files/2011/09/Spongebob-squarepants.jpg?width=960',
                'objects_to_annotate': objects_to_annotate,
                'with_labels': True,
                'metadata': meta_data,
            })
        print('RESSS', result)
        self.assertEqual(type(result['result']).__name__, ScaleAITask.__name__)
        self.session.close()
예제 #3
0
    def test_task_complete(self):
        # Create mock tasks
        file = data_mocking.create_file({'project_id': self.project.id},
                                        self.session)
        task = data_mocking.create_task({
            'name': 'test task',
            'file': file
        }, self.session)
        task.job.stat_count_tasks = 1
        task_complete.task_complete(session=self.session,
                                    task=task,
                                    new_file=task.file,
                                    project=self.project)

        self.assertEqual(task.status, 'complete')
        # Now job should have +1 in completed col
        self.assertEqual(task.job.stat_count_complete, 1)
        self.assertEqual(task.job.status, 'complete')

        task_complete.task_complete(session=self.session,
                                    task=task,
                                    new_file=task.file,
                                    project=self.project)
        # Count should still be one since task has already been completed.
        self.assertEqual(task.job.stat_count_complete, 1)
예제 #4
0
    def test_task_next_issue(self):
        # Create mock tasks
        num_tasks = 5
        job = data_mocking.create_job(
            {
                'name': 'my-test-job-{}'.format(1),
                'project': self.project
            }, self.session)
        all_tasks = []
        for i in range(0, num_tasks):
            task = data_mocking.create_task(
                {
                    'name': 'task{}'.format(i),
                    'job': job
                }, self.session)
            all_tasks.append(task)

        issue1 = data_mocking.create_discussion(
            {
                'title': 'test',
                'description': 'test',
                'project_id': self.project.id
            }, self.session)
        issue1.attach_element(session=self.session,
                              element={
                                  'type': 'task',
                                  'id': all_tasks[2].id
                              })
        issue2 = data_mocking.create_discussion(
            {
                'title': 'test2',
                'description': 'test2',
                'project_id': self.project.id
            }, self.session)
        issue2.attach_element(session=self.session,
                              element={
                                  'type': 'task',
                                  'id': all_tasks[4].id
                              })

        with self.client.session_transaction() as session:
            endpoint = "/api/v1/task/{}/next-task-with-issues".format(
                all_tasks[0].id)
            credentials = b64encode("{}:{}".format(
                self.auth_api.client_id,
                self.auth_api.client_secret).encode()).decode('utf-8')
            session['Authorization'] = credentials
            common_actions.add_auth_to_session(session, self.project.users[0])
        response = self.client.post(endpoint,
                                    data=json.dumps({}),
                                    headers={
                                        'directory_id':
                                        str(self.project.directory_default_id),
                                        'Authorization':
                                        'Basic {}'.format(credentials)
                                    })
        data = response.json
        self.assertEqual(response.status_code, 200)
        self.assertEqual(data['task_id'], all_tasks[2].id)
예제 #5
0
    def test_issue_new_web(self):
        # Create mock tasks
        # Create mock job.
        job = data_mocking.create_job(
            {
                'name': 'my-test-job',
                'project': self.project
            }, self.session)

        file = data_mocking.create_file(
            {
                'project_id': job.project.id,
                'job_id': job.id
            }, self.session)
        task = data_mocking.create_task(
            {
                'name': 'task{}'.format(1),
                'job': job,
                'file': file,
            }, self.session)
        discussion_title = 'new_issue'
        discussion_description = 'new_issue_description'
        request_data = {
            'title':
            discussion_title,
            'description':
            discussion_description,
            'attached_elements': [{
                'type': 'job',
                'id': job.id
            }, {
                'type': 'file',
                'id': file.id
            }, {
                'type': 'task',
                'id': task.id
            }]
        }

        endpoint = "/api/v1/project/" + job.project.project_string_id + "/issues/new"
        auth_api = common_actions.create_project_auth(project=job.project,
                                                      session=self.session)
        credentials = b64encode(
            "{}:{}".format(auth_api.client_id,
                           auth_api.client_secret).encode()).decode('utf-8')
        response = self.client.post(endpoint,
                                    data=json.dumps(request_data),
                                    headers={
                                        'directory_id':
                                        str(job.project.directory_default_id),
                                        'Authorization':
                                        'Basic {}'.format(credentials)
                                    })
        data = response.json
        self.assertEqual(response.status_code, 200)
        self.assertEqual(data['issue']['title'], discussion_title)
        self.assertEqual(data['issue']['description'], discussion_description)
예제 #6
0
    def test_get_video_frame_from_task(self):
        # Create mock task
        job = data_mocking.create_job({
            'name': 'my-test-job-{}'.format(1),
            'project': self.project
        }, self.session)
        video_file = data_mocking.create_file({
            'project_id': self.project.id,
            'type': 'video'
        }, self.session)
        frames_list = []
        # Mock Frames
        for i in range(0, 10):
            frame = data_mocking.create_file({
                'project_id': self.project.id,
                'video_parent_file_id': video_file.id,
                'frame_number': i,
                'type': 'frame'
            }, self.session)
            frames_list.append(frame)

        task = data_mocking.create_task({
            'name': 'tasktest',
            'job': job,
            'file': video_file
        }, self.session)

        request_data = {
            'frame_list': [1, 2, 3],
            'project_string_id': self.project.project_string_id,
            'mode_data': 'list'
        }

        endpoint = "/api/v1/task/{}/video/single/{}/frame-list/".format(task.id, video_file.id)
        with self.client.session_transaction() as session:
            auth_api = common_actions.create_project_auth(project = self.project, session = self.session)
            credentials = b64encode("{}:{}".format(auth_api.client_id, auth_api.client_secret).encode()).decode(
                'utf-8')
            session['Authorization'] = credentials
            common_actions.add_auth_to_session(session, self.project.users[0])
        response = self.client.post(
            endpoint,
            data = json.dumps(request_data),
            headers = {
                'directory_id': str(job.project.directory_default_id),
                'Authorization': 'Basic {}'.format(credentials)
            }
        )
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.json['url_list']), 3)
        i = 1
        for elm in response.json['url_list']:
            self.assertEqual(elm['frame_number'], i)
            i += 1
예제 #7
0
 def test_update_tasks(self):
     job = data_mocking.create_job({'name': 'my-test-job'}, self.session)
     task_1 = data_mocking.create_task({
         'name': 'task1',
         'job': job
     }, self.session)
     task_2 = data_mocking.create_task({
         'name': 'task2',
         'job': job
     }, self.session)
     self.session.commit()
     test_dict = {
         'test': {},
         'test_2': {},
         'test_3': {},
     }
     job.label_dict = test_dict
     log = {'error': {}, 'info': {}}
     job_new_or_update.update_tasks(job, self.session, log)
     self.session.commit()
     task1_updated = Task.get_by_id(self.session, task_1.id)
     task2_updated = Task.get_by_id(self.session, task_2.id)
     self.assertEqual(task1_updated.label_dict, test_dict)
     self.assertEqual(task2_updated.label_dict, test_dict)
예제 #8
0
    def test_notify_task_completion(self):
        project = self.project_data['project']
        new_dir = data_mocking.create_directory(
            {
                'project': project,
                'user': self.project_data['users'][0],
            }, self.session)
        old_dir = data_mocking.create_directory(
            {
                'project': project,
                'user': self.project_data['users'][0],
            }, self.session)

        job = data_mocking.create_job(
            {
                'project': project,
                'completion_directory_id': new_dir.id,
                'output_dir_action': 'copy'
            },
            session=self.session)
        file = data_mocking.create_file({'project_id': project.id},
                                        self.session)
        task_1 = data_mocking.create_task(
            {
                'name': 'task1',
                'job': job,
                'file': file,
                'incoming_directory_id': old_dir.id
            }, self.session)
        with patch.object(
                regular_methods,
                'transmit_interservice_request_after_commit') as mock:
            job_observable = task_file_observers.JobObservable(
                session=self.session,
                log=regular_log.default(),
                job=job,
                task=task_1)
            dir_observer = task_file_observers.DirectoryJobObserver(
                session=self.session,
                log=regular_log.default(),
                directory=new_dir,
                job_observable=job_observable)
            job_observable.add_new_directory_observer(dir_observer)
            job_observable.notify_all_observers(defer=False)
            self.session.commit()
            file_link = self.session.query(WorkingDirFileLink).filter(
                WorkingDirFileLink.working_dir_id == new_dir.id).all()
            mock.assert_called_once()
예제 #9
0
    def test_test_task_next_issue_core(self):
        # Create mock tasks
        # Create mock tasks
        num_tasks = 5
        job = data_mocking.create_job(
            {
                'name': 'my-test-job-{}'.format(1),
                'project': self.project
            }, self.session)
        all_tasks = []
        for i in range(0, num_tasks):
            task = data_mocking.create_task(
                {
                    'name': 'task{}'.format(i),
                    'job': job
                }, self.session)
            all_tasks.append(task)

        issue1 = data_mocking.create_discussion(
            {
                'title': 'test',
                'description': 'test',
                'project_id': self.project.id
            }, self.session)
        issue1.attach_element(session=self.session,
                              element={
                                  'type': 'task',
                                  'id': all_tasks[2].id
                              })
        issue2 = data_mocking.create_discussion(
            {
                'title': 'test2',
                'description': 'test2',
                'project_id': self.project.id
            }, self.session)
        issue2.attach_element(session=self.session,
                              element={
                                  'type': 'task',
                                  'id': all_tasks[4].id
                              })

        result = task_next_issue.task_next_issue_core(session=self.session,
                                                      task_id=all_tasks[0].id)
예제 #10
0
    def test_new_issue_core(self):
        # Create mock tasks
        # Create mock job.
        job = data_mocking.create_job({'name': 'my-test-job'}, self.session)

        file = data_mocking.create_file(
            {
                'project_id': job.project.id,
                'job_id': job.id
            }, self.session)
        task = data_mocking.create_task(
            {
                'name': 'task{}'.format(1),
                'job': job,
                'file': file,
            }, self.session)
        issue_title = 'new_issue'
        issue_description = 'new_issue_description'
        # Success Case
        result, log = discussion_new.new_discussion_core(
            session=self.session,
            log=regular_log.default(),
            member=None,
            project=job.project,
            title=issue_title,
            description=issue_description)

        self.assertIsNotNone(result)
        self.assertEqual(len(log['error'].keys()), 0)
        # Error Case
        result, log = discussion_new.new_discussion_core(
            session=self.session,
            log=regular_log.default(),
            member=None,
            project=job.project.project_string_id,
            title=None,
            description=issue_description)

        self.assertIsNone(result)
        self.assertEqual(len(log['error'].keys()), 1)
        self.assertIsNotNone(log['error'].get('title'))
예제 #11
0
    def test_issue_list_web(self):
        # Create mock tasks
        # Create mock job.
        discussion = data_mocking.create_discussion(
            {
                'project_id': self.project.id,
                'name': 'test',
                'title': 'test',
            },
            self.session,
        )
        issue2 = data_mocking.create_discussion(
            {
                'project_id': self.project.id,
                'name': 'test',
                'title': 'test',
            },
            self.session,
        )
        issue3 = data_mocking.create_discussion(
            {
                'project_id': self.project.id,
                'name': 'test',
                'title': 'test',
            },
            self.session,
        )
        job = data_mocking.create_job({
            'name': 'my-test-job',
            'project': self.project
        }, self.session)

        discussion.attach_element(
            session = self.session,
            element = {'type': 'job', 'id': job.id}
        )
        issue2.attach_element(
            session = self.session,
            element = {'type': 'job', 'id': job.id}
        )
        file = data_mocking.create_file({'project_id': job.project.id, 'job_id': job.id}, self.session)

        discussion.attach_element(
            session = self.session,
            element = {'type': 'file', 'id': file.id}
        )

        task = data_mocking.create_task({
            'name': 'task{}'.format(1),
            'job': job,
            'file': file,
        }, self.session)

        discussion.attach_element(
            session = self.session,
            element = {'type': 'task', 'id': task.id}
        )
        regular_methods.commit_with_rollback(self.session)

        rels = self.session.query(DiscussionRelation).all()
        request_data = {
            'project_id': self.project.id,
            'task_id': task.id,
        }

        endpoint = "/api/v1/project/" + self.project.project_string_id + "/discussions/list"
        auth_api = common_actions.create_project_auth(project=job.project, session=self.session)
        credentials = b64encode("{}:{}".format(auth_api.client_id, auth_api.client_secret).encode()).decode('utf-8')
        response_with_task_id = self.client.post(
            endpoint,
            data=json.dumps(request_data),
            headers={
                'directory_id': str(job.project.directory_default_id),
                'Authorization': 'Basic {}'.format(credentials)
            }
        )
        data = response_with_task_id.json
        self.assertEqual(response_with_task_id.status_code, 200)
        self.assertEqual(len(data['issues']), 1)
        request_data = {
            'project_id': self.project.id,
            'job_id': job.id,
        }
        response_with_job_id = self.client.post(
            endpoint,
            data=json.dumps(request_data),
            headers={
                'directory_id': str(job.project.directory_default_id),
                'Authorization': 'Basic {}'.format(credentials)
            }
        )

        data = response_with_job_id.json
        self.assertEqual(response_with_task_id.status_code, 200)
        self.assertEqual(len(data['issues']), 2)

        request_data = {
            'project_id': self.project.id,
        }
        response_project = self.client.post(
            endpoint,
            data=json.dumps(request_data),
            headers={
                'directory_id': str(job.project.directory_default_id),
                'Authorization': 'Basic {}'.format(credentials)
            }
        )

        data = response_project.json
        self.assertEqual(response_with_task_id.status_code, 200)
        self.assertEqual(len(data['issues']), 3)