test_epicollect_importer.py
4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2015 SciFabric LTD.
#
# PyBossa is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa. If not, see <http://www.gnu.org/licenses/>.
import json
from mock import patch
from nose.tools import assert_raises
from pybossa.importers import BulkImportException
from pybossa.importers.epicollect import BulkTaskEpiCollectPlusImport
from default import FakeResponse
@patch('pybossa.importers.epicollect.requests.get')
class TestBulkTaskEpiCollectPlusImport(object):
epicollect = {'epicollect_project': 'fakeproject',
'epicollect_form': 'fakeform'}
importer = BulkTaskEpiCollectPlusImport(**epicollect)
def test_count_tasks_raises_exception_if_file_forbidden(self, request):
forbidden_request = FakeResponse(text='Forbidden', status_code=403,
headers={'content-type': 'text/json'},
encoding='utf-8')
request.return_value = forbidden_request
msg = "Oops! It looks like you don't have permission to access the " \
"EpiCollect Plus project"
assert_raises(BulkImportException, self.importer.count_tasks)
try:
self.importer.count_tasks()
except BulkImportException as e:
assert e[0] == msg, e
def test_tasks_raises_exception_if_file_forbidden(self, request):
forbidden_request = FakeResponse(text='Forbidden', status_code=403,
headers={'content-type': 'text/json'},
encoding='utf-8')
request.return_value = forbidden_request
msg = "Oops! It looks like you don't have permission to access the " \
"EpiCollect Plus project"
assert_raises(BulkImportException, self.importer.tasks)
try:
self.importer.tasks()
except BulkImportException as e:
assert e[0] == msg, e
def test_count_tasks_raises_exception_if_not_json(self, request):
html_request = FakeResponse(text='Not an application/json',
status_code=200,
headers={'content-type': 'text/html'},
encoding='utf-8')
request.return_value = html_request
msg = "Oops! That project and form do not look like the right one."
assert_raises(BulkImportException, self.importer.count_tasks)
try:
self.importer.count_tasks()
except BulkImportException as e:
assert e[0] == msg, e
def test_tasks_raises_exception_if_not_json(self, request):
html_request = FakeResponse(text='Not an application/json',
status_code=200,
headers={'content-type': 'text/html'},
encoding='utf-8')
request.return_value = html_request
msg = "Oops! That project and form do not look like the right one."
assert_raises(BulkImportException, self.importer.tasks)
try:
self.importer.tasks()
except BulkImportException as e:
assert e[0] == msg, e
def test_count_tasks_returns_number_of_tasks_in_project(self, request):
data = [dict(DeviceID=23), dict(DeviceID=24)]
response = FakeResponse(text=json.dumps(data), status_code=200,
headers={'content-type': 'application/json'},
encoding='utf-8')
request.return_value = response
number_of_tasks = self.importer.count_tasks()
assert number_of_tasks is 2, number_of_tasks
def test_tasks_returns_tasks_in_project(self, request):
data = [dict(DeviceID=23), dict(DeviceID=24)]
response = FakeResponse(text=json.dumps(data), status_code=200,
headers={'content-type': 'application/json'},
encoding='utf-8')
request.return_value = response
task = self.importer.tasks().next()
assert task == {'info': {u'DeviceID': 23}}, task