~tomasgroth/openlp/portable-path

« back to all changes in this revision

Viewing changes to tests/functional/openlp_plugins/bibles/test_csvimport.py

  • Committer: Tomas Groth
  • Date: 2019-04-30 19:02:42 UTC
  • mfrom: (2829.2.32 openlp)
  • Revision ID: tomasgroth@yahoo.dk-20190430190242-6zwjk8724tyux70m
trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
###############################################################################
5
5
# OpenLP - Open Source Lyrics Projection                                      #
6
6
# --------------------------------------------------------------------------- #
7
 
# Copyright (c) 2008-2018 OpenLP Developers                                   #
 
7
# Copyright (c) 2008-2019 OpenLP Developers                                   #
8
8
# --------------------------------------------------------------------------- #
9
9
# This program is free software; you can redistribute it and/or modify it     #
10
10
# under the terms of the GNU General Public License as published by the Free  #
25
25
import csv
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
29
29
 
30
30
from openlp.core.common.path import Path
31
31
from openlp.core.lib.exceptions import ValidationError
34
34
from tests.utils import load_external_result_data
35
35
from tests.utils.constants import RESOURCE_PATH
36
36
 
 
37
 
37
38
TEST_PATH = RESOURCE_PATH / 'bibles'
38
39
 
39
40
 
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
134
138
 
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:
140
143
 
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)
143
146
 
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='"')
149
152
 
150
153
    def test_parse_csv_file_oserror(self):
151
154
        """
152
155
        Test the parse_csv_file() handles an OSError correctly
153
156
        """
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()
 
161
 
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}):
158
164
 
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'
164
170
 
165
171
    def test_parse_csv_file_csverror(self):
167
173
        Test the parse_csv_file() handles an csv.Error correctly
168
174
        """
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'
 
178
 
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):
174
182
 
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'
180
188
 
181
189
    def test_process_books_stopped_import(self):