test_googledocs_importer.py
6.86 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# -*- 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/>.
from mock import patch
from nose.tools import assert_raises
from pybossa.importers import BulkImportException
from pybossa.importers.csv import BulkTaskGDImport
from default import FakeResponse
@patch('pybossa.importers.csv.requests.get')
class TestBulkTaskGDImport(object):
def setUp(self):
url = 'http://drive.google.com'
self.importer = BulkTaskGDImport(googledocs_url=url)
def test_count_tasks_returns_0_if_no_rows_other_than_header(self, request):
empty_file = FakeResponse(text='CSV,with,no,content\n', status_code=200,
headers={'content-type': 'text/plain'},
encoding='utf-8')
request.return_value = empty_file
number_of_tasks = self.importer.count_tasks()
assert number_of_tasks is 0, number_of_tasks
def test_count_tasks_returns_1_for_CSV_with_one_valid_row(self, request):
valid_file = FakeResponse(text='Foo,Bar,Baz\n1,2,3', status_code=200,
headers={'content-type': 'text/plain'},
encoding='utf-8')
request.return_value = valid_file
number_of_tasks = self.importer.count_tasks()
assert number_of_tasks is 1, number_of_tasks
def test_count_tasks_raises_exception_if_file_forbidden(self, request):
forbidden_request = FakeResponse(text='Forbidden', status_code=403,
headers={'content-type': 'text/plain'},
encoding='utf-8')
request.return_value = forbidden_request
msg = "Oops! It looks like you don't have permission to access that file"
assert_raises(BulkImportException, self.importer.count_tasks)
try:
self.importer.count_tasks()
except BulkImportException as e:
assert e[0] == msg, e
def test_count_tasks_raises_exception_if_not_CSV_file(self, request):
html_request = FakeResponse(text='Not a CSV', status_code=200,
headers={'content-type': 'text/html'},
encoding='utf-8')
request.return_value = html_request
msg = "Oops! That file doesn't look like the right file."
assert_raises(BulkImportException, self.importer.count_tasks)
try:
self.importer.count_tasks()
except BulkImportException as e:
assert e[0] == msg, e
def test_count_tasks_raises_exception_if_dup_header(self, request):
empty_file = FakeResponse(text='Foo,Bar,Foo\n1,2,3', status_code=200,
headers={'content-type': 'text/plain'},
encoding='utf-8')
request.return_value = empty_file
msg = "The file you uploaded has two headers with the same name."
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/plain'},
encoding='utf-8')
request.return_value = forbidden_request
msg = "Oops! It looks like you don't have permission to access that file"
assert_raises(BulkImportException, self.importer.tasks)
try:
self.importer.tasks()
except BulkImportException as e:
assert e[0] == msg, e
def test_tasks_raises_exception_if_not_CSV_file(self, request):
html_request = FakeResponse(text='Not a CSV', status_code=200,
headers={'content-type': 'text/html'},
encoding='utf-8')
request.return_value = html_request
msg = "Oops! That file doesn't look like the right file."
assert_raises(BulkImportException, self.importer.tasks)
try:
self.importer.tasks()
except BulkImportException as e:
assert e[0] == msg, e
def test_tasks_raises_exception_if_dup_header(self, request):
csv_file = FakeResponse(text='Foo,Bar,Foo\n1,2,3', status_code=200,
headers={'content-type': 'text/plain'},
encoding='utf-8')
request.return_value = csv_file
msg = "The file you uploaded has two headers with the same name."
raised = False
try:
self.importer.tasks().next()
except BulkImportException as e:
assert e[0] == msg, e
raised = True
finally:
assert raised, "Exception not raised"
def test_tasks_return_tasks_with_only_info_fields(self, request):
csv_file = FakeResponse(text='Foo,Bar,Baz\n1,2,3', status_code=200,
headers={'content-type': 'text/plain'},
encoding='utf-8')
request.return_value = csv_file
tasks = self.importer.tasks()
task = tasks.next()
assert task == {"info": {u'Bar': u'2', u'Foo': u'1', u'Baz': u'3'}}, task
def test_tasks_return_tasks_with_non_info_fields_too(self, request):
csv_file = FakeResponse(text='Foo,Bar,priority_0\n1,2,3',
status_code=200,
headers={'content-type': 'text/plain'},
encoding='utf-8')
request.return_value = csv_file
tasks = self.importer.tasks()
task = tasks.next()
assert task == {'info': {u'Foo': u'1', u'Bar': u'2'},
u'priority_0': u'3'}, task
def test_tasks_works_with_encodings_other_than_utf8(self, request):
csv_file = FakeResponse(text=u'Foo\nM\xc3\xbcnchen', status_code=200,
headers={'content-type': 'text/plain'},
encoding='ISO-8859-1')
request.return_value = csv_file
tasks = self.importer.tasks()
task = tasks.next()
assert csv_file.encoding == 'utf-8'