Пример #1
0
    def test_result_query_without_params(self):
        """ Test API Result query"""
        result = self.create_result(n_answers=10)
        res = self.app.get('/api/result')
        results = json.loads(res.data)
        assert len(results) == 1, results
        result = results[0]
        assert result['info'] is None, result
        assert len(result['task_run_ids']) == 10, result
        assert result['task_run_ids'] == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], result
        assert result['project_id'] == 1, result
        assert result['task_id'] == 1, result
        assert result['created'] is not None, result

        result = self.create_result(n_answers=10)
        result = result_repo.get(2)
        result.created = '2119-01-01T14:37:30.642119'
        result_repo.update(result)

        url = '/api/result?desc=true'
        res = self.app.get(url)
        data = json.loads(res.data)
        err_msg = "It should get the last item first."
        assert data[0]['created'] == '2119-01-01T14:37:30.642119', err_msg

        url = '/api/result'
        res = self.app.get(url)
        data = json.loads(res.data)
        err_msg = "It should get not the last item first."
        assert data[0]['created'] != '2119-01-01T14:37:30.642119', err_msg

        # The output should have a mime-type: application/json
        assert res.mimetype == 'application/json', res
Пример #2
0
    def test_result_query_without_params(self):
        """ Test API Result query"""
        result = self.create_result(n_answers=10)
        res = self.app.get("/api/result")
        results = json.loads(res.data)
        assert len(results) == 1, results
        result = results[0]
        assert result["info"] is None, result
        assert len(result["task_run_ids"]) == 10, result
        assert result["task_run_ids"] == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], result
        assert result["project_id"] == 1, result
        assert result["task_id"] == 1, result
        assert result["created"] is not None, result

        result = self.create_result(n_answers=10)
        result = result_repo.get(2)
        result.created = "2119-01-01T14:37:30.642119"
        result_repo.update(result)

        url = "/api/result?desc=true"
        res = self.app.get(url)
        data = json.loads(res.data)
        err_msg = "It should get the last item first."
        assert data[0]["created"] == "2119-01-01T14:37:30.642119", err_msg

        url = "/api/result"
        res = self.app.get(url)
        data = json.loads(res.data)
        err_msg = "It should get not the last item first."
        assert data[0]["created"] != "2119-01-01T14:37:30.642119", err_msg

        # The output should have a mime-type: application/json
        assert res.mimetype == "application/json", res
Пример #3
0
 def _set_result_has_children(self, result):
     """Add has_children key to a result."""
     from pybossa.core import result_repo
     new_info = result.info.copy()
     new_info['has_children'] = True
     result.info = new_info
     result_repo.update(result)
Пример #4
0
 def test_correct_data_written_to_xml(self):
     TaskRunFactory.create(project=self.project, task=self.task)
     result = result_repo.filter_by(project_id=self.project.id)[0]
     result.info = {'n': 42}
     result_repo.update(result)
     resp = self.app.get('/data/project/xml_export', follow_redirects=True)
     xml = ('<?xml version="1.0" encoding="UTF-8" ?><record-group>'
            '<record><n>42</n></record></record-group>')
     dom = parseString(xml)
     expected = dom.toprettyxml()
     assert resp.data == expected, resp.data
Пример #5
0
 def test_correct_data_written_to_csv(self, mock_writer):
     TaskRunFactory.create(project=self.project, task=self.task)
     result = result_repo.filter_by(project_id=self.project.id)[0]
     result.info = {'n': 42}
     result_repo.update(result)
     res = self.app.get('/data/project/csv_export', follow_redirects=True)
     expected_headers = ['info', 'task_id', 'created', 'last_version',
                         'task_run_ids', 'project_id', 'id', 'info_n']
     expected_row = result.dictize().values() + [42]
     headers = mock_writer.call_args_list[0][0][0]
     row = mock_writer.call_args_list[1][0][0]
     assert sorted(headers) == sorted(expected_headers)
     assert sorted(row) == sorted(expected_row)
Пример #6
0
    def test_project_built_from_valid_parent_template(self, mock_count,
                                                      mock_create_tasks):
        """Test that import data modified for IIIF child projects."""
        self.register()
        user = user_repo.get(1)
        vol = dict(id='123abc',
                   name='My Volume',
                   importer='iiif',
                   data=dict(manifest_uri=self.manifest_uri))
        category = CategoryFactory()
        tmpl_fixtures = TemplateFixtures(category)
        select_task = tmpl_fixtures.iiif_select_tmpl
        parent_tmpl = tmpl_fixtures.create(task_tmpl=select_task)
        child_tmpl = tmpl_fixtures.create(task_tmpl=select_task)
        child_tmpl['parent_template_id'] = parent_tmpl['id']

        category.info = dict(presenter='iiif-annotation',
                             volumes=[vol],
                             templates=[parent_tmpl, child_tmpl])
        project_repo.update_category(category)

        parent = ProjectFactory(id=42,
                                owner=user,
                                category=category,
                                info=dict(template_id=parent_tmpl['id'],
                                          volume_id=vol['id']))

        n_tasks = 3
        tasks = TaskFactory.create_batch(n_tasks, n_answers=1, project=parent)
        for task in tasks:
            TaskRunFactory.create(user=user, project=parent, task=task)
        results = result_repo.filter_by(project_id=parent.id)
        for result in results:
            result.info = 'foo'
            result_repo.update(result)

        endpoint = '/lc/projects/{}/new'.format(category.short_name)
        form_data = dict(name='foo',
                         short_name='bar',
                         template_id=child_tmpl['id'],
                         volume_id=vol['id'])

        self.app_post_json(endpoint, data=form_data)

        expected_call = call(task_repo,
                             1,
                             type='iiif-enhanced',
                             manifest_uri=self.manifest_uri,
                             parent_id=parent.id)
        assert_equal(mock_create_tasks.call_args_list, [expected_call])
Пример #7
0
    def notest_n_results_returns_number_of_total_results(self):
        project = ProjectFactory.create()
        task = TaskFactory.create(n_answers=1, project=project)
        TaskRunFactory.create(task=task, project=project)

        results = cached_projects.n_results(project.id)

        assert results == 0, results

        result = result_repo.get_by(project_id=project.id)

        result.info = dict(foo='bar')

        result_repo.update(result)

        results = cached_projects.n_results(project.id)

        assert results == 1, results
Пример #8
0
    def test_n_results_returns_number_of_total_results(self):
        project = ProjectFactory.create()
        task = TaskFactory.create(n_answers=1, project=project)
        TaskRunFactory.create(task=task, project=project)

        results = cached_projects.n_results(project.id)

        assert results == 0, results

        result = result_repo.get_by(project_id=project.id)

        result.info = dict(foo='bar')

        result_repo.update(result)

        results = cached_projects.n_results(project.id)

        assert results == 1, results
Пример #9
0
    def test_result_query_without_params(self):
        """ Test API Result query"""
        result = self.create_result(n_answers=10)
        res = self.app.get('/api/result')
        results = json.loads(res.data)
        assert len(results) == 1, results
        result = results[0]
        assert result['info'] is None, result
        assert len(result['task_run_ids']) == 10, result
        assert result['task_run_ids'] == [1, 2, 3, 4, 5, 6, 7, 8, 9,
                                          10], result
        assert result['project_id'] == 1, result
        assert result['task_id'] == 1, result
        assert result['created'] is not None, result

        # Related
        res = self.app.get('/api/result?related=True')
        results = json.loads(res.data)
        assert len(results) == 1, results
        result = results[0]
        assert result['info'] is None, result
        assert len(result['task_run_ids']) == 10, result
        assert result['task_run_ids'] == [1, 2, 3, 4, 5, 6, 7, 8, 9,
                                          10], result
        assert result['project_id'] == 1, result
        assert result['task_id'] == 1, result
        assert result['created'] is not None, result
        assert result['task']['id'] == result['task_id'], result
        assert len(result['task_runs']) == 10, result
        for tr in result['task_runs']:
            assert tr['task_id'] == result['task_id'], tr
            url = '/api/taskrun?id=%s&related=True' % tr['id']
            taskrun = self.app.get(url)
            taskrun = json.loads(taskrun.data)[0]
            assert taskrun['result']['id'] == result['id'], taskrun['result']
            assert taskrun['task']['id'] == result['task_id'], taskrun['task']
        url = '/api/task?id=%s&related=True' % result['task_id']
        task = self.app.get(url)
        task = json.loads(task.data)[0]
        assert task['result']['id'] == result['id'], task['result']
        for tr in task['task_runs']:
            assert tr['id'] in result['task_run_ids'], task['task']

        result = self.create_result(n_answers=10)
        result = result_repo.get(2)
        result.created = '2119-01-01T14:37:30.642119'
        result_repo.update(result)

        url = '/api/result?orderby=created&desc=true'
        res = self.app.get(url)
        data = json.loads(res.data)
        print data
        err_msg = "It should get the last item first."
        assert data[0]['created'] == '2119-01-01T14:37:30.642119', err_msg

        url = '/api/result?orderby=id&desc=false'
        res = self.app.get(url)
        data = json.loads(res.data)
        err_msg = "It should be sorted by id."
        assert data[1]['id'] == result.id, err_msg

        url = '/api/result?orderby=wrongattribute'
        res = self.app.get(url)
        data = json.loads(res.data)
        err_msg = "It should be 415."
        assert data['status'] == 'failed', data
        assert data['status_code'] == 415, data
        assert 'has no attribute' in data['exception_msg'], data

        url = '/api/result'
        res = self.app.get(url)
        data = json.loads(res.data)
        err_msg = "It should get not the last item first."
        assert data[0]['created'] != '2119-01-01T14:37:30.642119', err_msg

        # The output should have a mime-type: application/json
        assert res.mimetype == 'application/json', res
Пример #10
0
    def test_result_query_without_params(self):
        """ Test API Result query"""
        result = self.create_result(n_answers=10)
        res = self.app.get('/api/result')
        results = json.loads(res.data)
        assert len(results) == 1, results
        result = results[0]
        assert result['info'] is None, result
        assert len(result['task_run_ids']) == 10, result
        assert result['task_run_ids'] == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], result
        assert result['project_id'] == 1, result
        assert result['task_id'] == 1, result
        assert result['created'] is not None, result

        # Related
        res = self.app.get('/api/result?related=True')
        results = json.loads(res.data)
        assert len(results) == 1, results
        result = results[0]
        assert result['info'] is None, result
        assert len(result['task_run_ids']) == 10, result
        assert result['task_run_ids'] == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], result
        assert result['project_id'] == 1, result
        assert result['task_id'] == 1, result
        assert result['created'] is not None, result
        assert result['task']['id'] == result['task_id'], result
        assert len(result['task_runs']) == 10, result
        for tr in result['task_runs']:
            assert tr['task_id'] == result['task_id'], tr
            url = '/api/taskrun?id=%s&related=True' % tr['id']
            taskrun = self.app.get(url)
            taskrun = json.loads(taskrun.data)[0]
            assert taskrun['result']['id'] == result['id'], taskrun['result']
            assert taskrun['task']['id'] == result['task_id'], taskrun['task']
        url = '/api/task?id=%s&related=True' % result['task_id']
        task = self.app.get(url)
        task = json.loads(task.data)[0]
        assert task['result']['id'] == result['id'], task['result']
        for tr in task['task_runs']:
            assert tr['id'] in result['task_run_ids'], task['task']

        result = self.create_result(n_answers=10)
        result = result_repo.get(2)
        result.created = '2119-01-01T14:37:30.642119'
        result_repo.update(result)

        url = '/api/result?orderby=created&desc=true'
        res = self.app.get(url)
        data = json.loads(res.data)
        print data
        err_msg = "It should get the last item first."
        assert data[0]['created'] == '2119-01-01T14:37:30.642119', err_msg

        url = '/api/result?orderby=id&desc=false'
        res = self.app.get(url)
        data = json.loads(res.data)
        err_msg = "It should be sorted by id."
        assert data[1]['id'] == result.id, err_msg

        url = '/api/result?orderby=wrongattribute'
        res = self.app.get(url)
        data = json.loads(res.data)
        err_msg = "It should be 415."
        assert data['status'] == 'failed', data
        assert data['status_code'] == 415, data
        assert 'has no attribute' in data['exception_msg'], data


        url = '/api/result'
        res = self.app.get(url)
        data = json.loads(res.data)
        err_msg = "It should get not the last item first."
        assert data[0]['created'] != '2119-01-01T14:37:30.642119', err_msg

        # The output should have a mime-type: application/json
        assert res.mimetype == 'application/json', res