26
26
from collections import namedtuple
27
27
from unittest import TestCase
28
from unittest.mock import ANY, MagicMock, PropertyMock, call, patch
28
from unittest.mock import MagicMock, PropertyMock, call, patch
30
30
from openlp.core.common.path import Path
31
31
from openlp.core.lib.exceptions import ValidationError
131
132
# GIVEN: A mocked csv.reader which returns an iterator with test data
132
133
test_data = [['1', 'Line 1', 'Data 1'], ['2', 'Line 2', 'Data 2'], ['3', 'Line 3', 'Data 3']]
133
134
TestTuple = namedtuple('TestTuple', 'line_no line_description line_data')
135
mocked_csv_file = MagicMock()
136
mocked_enter_file = MagicMock()
137
mocked_csv_file.open.return_value.__enter__.return_value = mocked_enter_file
135
139
with patch('openlp.plugins.bibles.lib.importers.csvbible.get_file_encoding',
136
return_value={'encoding': 'utf-8', 'confidence': 0.99}),\
137
patch('openlp.plugins.bibles.lib.importers.csvbible.Path.open', create=True) as mocked_open,\
140
return_value={'encoding': 'utf-8', 'confidence': 0.99}), \
138
141
patch('openlp.plugins.bibles.lib.importers.csvbible.csv.reader',
139
142
return_value=iter(test_data)) as mocked_reader:
141
144
# WHEN: Calling the CSVBible parse_csv_file method with a file name and TestTuple
142
result = CSVBible.parse_csv_file(Path('file.csv'), TestTuple)
145
result = CSVBible.parse_csv_file(mocked_csv_file, TestTuple)
144
147
# THEN: A list of TestTuple instances with the parsed data should be returned
145
148
assert result == [TestTuple('1', 'Line 1', 'Data 1'), TestTuple('2', 'Line 2', 'Data 2'),
146
149
TestTuple('3', 'Line 3', 'Data 3')]
147
mocked_open.assert_called_once_with('r', encoding='utf-8', newline='')
148
mocked_reader.assert_called_once_with(ANY, delimiter=',', quotechar='"')
150
mocked_csv_file.open.assert_called_once_with('r', encoding='utf-8', newline='')
151
mocked_reader.assert_called_once_with(mocked_enter_file, delimiter=',', quotechar='"')
150
153
def test_parse_csv_file_oserror(self):
152
155
Test the parse_csv_file() handles an OSError correctly
154
157
# GIVEN: Mocked a mocked open object which raises an OSError
158
mocked_csv_file = MagicMock()
159
mocked_csv_file.__str__.return_value = 'file.csv'
160
mocked_csv_file.open.side_effect = OSError()
155
162
with patch('openlp.plugins.bibles.lib.importers.csvbible.get_file_encoding',
156
return_value={'encoding': 'utf-8', 'confidence': 0.99}),\
157
patch('openlp.plugins.bibles.lib.importers.csvbible.Path.open', side_effect=OSError, create=True):
163
return_value={'encoding': 'utf-8', 'confidence': 0.99}):
159
165
# WHEN: Calling CSVBible.parse_csv_file
160
166
# THEN: A ValidationError should be raised
161
167
with self.assertRaises(ValidationError) as context:
162
CSVBible.parse_csv_file(Path('file.csv'), None)
168
CSVBible.parse_csv_file(mocked_csv_file, None)
163
169
assert context.exception.msg == 'Parsing "file.csv" failed'
165
171
def test_parse_csv_file_csverror(self):
167
173
Test the parse_csv_file() handles an csv.Error correctly
169
175
# GIVEN: Mocked a csv.reader which raises an csv.Error
176
mocked_csv_file = MagicMock()
177
mocked_csv_file.__str__.return_value = 'file.csv'
170
179
with patch('openlp.plugins.bibles.lib.importers.csvbible.get_file_encoding',
171
180
return_value={'encoding': 'utf-8', 'confidence': 0.99}),\
172
patch('openlp.plugins.bibles.lib.importers.csvbible.Path.open', create=True),\
173
181
patch('openlp.plugins.bibles.lib.importers.csvbible.csv.reader', side_effect=csv.Error):
175
183
# WHEN: Calling CSVBible.parse_csv_file
176
184
# THEN: A ValidationError should be raised
177
185
with self.assertRaises(ValidationError) as context:
178
CSVBible.parse_csv_file(Path('file.csv'), None)
186
CSVBible.parse_csv_file(mocked_csv_file, None)
179
187
assert context.exception.msg == 'Parsing "file.csv" failed'
181
189
def test_process_books_stopped_import(self):