Missed a py2 file

This commit is contained in:
Philip Ridout 2013-09-07 17:02:28 +00:00
parent dc9d3cd74e
commit bc30365e0e
1 changed files with 113 additions and 113 deletions

View File

@ -1,113 +1,113 @@
""" """
The :mod:`songfileimporthelper` modules provides a helper class and methods to easily enable testing the import of The :mod:`songfileimporthelper` modules provides a helper class and methods to easily enable testing the import of
song files from third party applications. song files from third party applications.
""" """
import json import json
from unittest import TestCase from unittest import TestCase
from mock import patch, MagicMock from mock import patch, MagicMock
class SongImportTestHelper(TestCase): class SongImportTestHelper(TestCase):
""" """
This class is designed to be a helper class to reduce repition when testing the import of song files. This class is designed to be a helper class to reduce repition when testing the import of song files.
""" """
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.importer_module = __import__( self.importer_module = __import__(
u'openlp.plugins.songs.lib.%s' % self.importer_module_name, fromlist=[self.importer_class_name]) 'openlp.plugins.songs.lib.%s' % self.importer_module_name, fromlist=[self.importer_class_name])
self.importer_class = getattr(self.importer_module, self.importer_class_name) self.importer_class = getattr(self.importer_module, self.importer_class_name)
TestCase.__init__(self, *args, **kwargs) TestCase.__init__(self, *args, **kwargs)
def setUp(self): def setUp(self):
""" """
Patch and set up the mocks required. Patch and set up the mocks required.
""" """
self.add_copyright_patcher = patch( self.add_copyright_patcher = patch(
u'openlp.plugins.songs.lib.%s.%s.addCopyright' % (self.importer_module_name, self.importer_class_name)) 'openlp.plugins.songs.lib.%s.%s.addCopyright' % (self.importer_module_name, self.importer_class_name))
self.add_verse_patcher = patch( self.add_verse_patcher = patch(
u'openlp.plugins.songs.lib.%s.%s.addVerse' % (self.importer_module_name, self.importer_class_name)) 'openlp.plugins.songs.lib.%s.%s.addVerse' % (self.importer_module_name, self.importer_class_name))
self.finish_patcher = patch( self.finish_patcher = patch(
u'openlp.plugins.songs.lib.%s.%s.finish' % (self.importer_module_name, self.importer_class_name)) 'openlp.plugins.songs.lib.%s.%s.finish' % (self.importer_module_name, self.importer_class_name))
self.parse_author_patcher = patch( self.parse_author_patcher = patch(
u'openlp.plugins.songs.lib.%s.%s.parse_author' % (self.importer_module_name, self.importer_class_name)) 'openlp.plugins.songs.lib.%s.%s.parse_author' % (self.importer_module_name, self.importer_class_name))
self.song_import_patcher = patch(u'openlp.plugins.songs.lib.%s.SongImport' % self.importer_module_name) self.song_import_patcher = patch('openlp.plugins.songs.lib.%s.SongImport' % self.importer_module_name)
self.mocked_add_copyright = self.add_copyright_patcher.start() self.mocked_add_copyright = self.add_copyright_patcher.start()
self.mocked_add_verse = self.add_verse_patcher.start() self.mocked_add_verse = self.add_verse_patcher.start()
self.mocked_finish = self.finish_patcher.start() self.mocked_finish = self.finish_patcher.start()
self.mocked_parse_author = self.parse_author_patcher.start() self.mocked_parse_author = self.parse_author_patcher.start()
self.mocked_song_importer = self.song_import_patcher.start() self.mocked_song_importer = self.song_import_patcher.start()
self.mocked_manager = MagicMock() self.mocked_manager = MagicMock()
self.mocked_import_wizard = MagicMock() self.mocked_import_wizard = MagicMock()
self.mocked_finish.return_value = True self.mocked_finish.return_value = True
def tearDown(self): def tearDown(self):
""" """
Clean up Clean up
""" """
self.add_copyright_patcher.stop() self.add_copyright_patcher.stop()
self.add_verse_patcher.stop() self.add_verse_patcher.stop()
self.finish_patcher.stop() self.finish_patcher.stop()
self.parse_author_patcher.stop() self.parse_author_patcher.stop()
self.song_import_patcher.stop() self.song_import_patcher.stop()
def load_external_result_data(self, file_name): def load_external_result_data(self, file_name):
""" """
A method to load and return an object containing the song data from an external file. A method to load and return an object containing the song data from an external file.
""" """
result_file = open(file_name, 'rb') result_file = open(file_name, 'rb')
return json.loads(result_file.read()) return json.loads(result_file.read())
def file_import(self, source_file_name, result_data): def file_import(self, source_file_name, result_data):
""" """
Import the given file and check that it has imported correctly Import the given file and check that it has imported correctly
""" """
importer = self.importer_class(self.mocked_manager) importer = self.importer_class(self.mocked_manager)
importer.import_wizard = self.mocked_import_wizard importer.import_wizard = self.mocked_import_wizard
importer.stop_import_flag = False importer.stop_import_flag = False
importer.topics = [] importer.topics = []
# WHEN: Importing the source file # WHEN: Importing the source file
importer.import_source = [source_file_name] importer.import_source = [source_file_name]
add_verse_calls = self.get_data(result_data, u'verses') add_verse_calls = self.get_data(result_data, 'verses')
author_calls = self.get_data(result_data, u'authors') author_calls = self.get_data(result_data, 'authors')
ccli_number = self.get_data(result_data, u'ccli_number') ccli_number = self.get_data(result_data, 'ccli_number')
comments = self.get_data(result_data, u'comments') comments = self.get_data(result_data, 'comments')
song_book_name = self.get_data(result_data, u'song_book_name') song_book_name = self.get_data(result_data, 'song_book_name')
song_copyright = self.get_data(result_data, u'copyright') song_copyright = self.get_data(result_data, 'copyright')
song_number = self.get_data(result_data, u'song_number') song_number = self.get_data(result_data, 'song_number')
title = self.get_data(result_data, u'title') title = self.get_data(result_data, 'title')
topics = self.get_data(result_data, u'topics') topics = self.get_data(result_data, 'topics')
verse_order_list = self.get_data(result_data, u'verse_order_list') verse_order_list = self.get_data(result_data, 'verse_order_list')
# THEN: doImport should return none, the song data should be as expected, and finish should have been # THEN: doImport should return none, the song data should be as expected, and finish should have been
# called. # called.
self.assertIsNone(importer.doImport(), u'doImport should return None when it has completed') self.assertIsNone(importer.doImport(), 'doImport should return None when it has completed')
self.assertEquals(importer.title, title, u'title for %s should be "%s"' % (source_file_name, title)) self.assertEquals(importer.title, title, 'title for %s should be "%s"' % (source_file_name, title))
for author in author_calls: for author in author_calls:
self.mocked_parse_author.assert_any_call(author) self.mocked_parse_author.assert_any_call(author)
if song_copyright: if song_copyright:
self.mocked_add_copyright.assert_called_with(song_copyright) self.mocked_add_copyright.assert_called_with(song_copyright)
if ccli_number: if ccli_number:
self.assertEquals(importer.ccliNumber, ccli_number, u'ccliNumber for %s should be %s' self.assertEquals(importer.ccliNumber, ccli_number, 'ccliNumber for %s should be %s'
% (source_file_name, ccli_number)) % (source_file_name, ccli_number))
for verse_text, verse_tag in add_verse_calls: for verse_text, verse_tag in add_verse_calls:
self.mocked_add_verse.assert_any_call(verse_text, verse_tag) self.mocked_add_verse.assert_any_call(verse_text, verse_tag)
if topics: if topics:
self.assertEquals(importer.topics, topics, u'topics for %s should be %s' % (source_file_name, topics)) self.assertEquals(importer.topics, topics, 'topics for %s should be %s' % (source_file_name, topics))
if comments: if comments:
self.assertEquals(importer.comments, comments, u'comments for %s should be "%s"' self.assertEquals(importer.comments, comments, 'comments for %s should be "%s"'
% (source_file_name, comments)) % (source_file_name, comments))
if song_book_name: if song_book_name:
self.assertEquals(importer.songBookName, song_book_name, u'songBookName for %s should be "%s"' self.assertEquals(importer.songBookName, song_book_name, 'songBookName for %s should be "%s"'
% (source_file_name, song_book_name)) % (source_file_name, song_book_name))
if song_number: if song_number:
self.assertEquals(importer.songNumber, song_number, u'songNumber for %s should be %s' self.assertEquals(importer.songNumber, song_number, 'songNumber for %s should be %s'
% (source_file_name, song_number)) % (source_file_name, song_number))
if verse_order_list: if verse_order_list:
self.assertEquals(importer.verseOrderList, [], u'verseOrderList for %s should be %s' self.assertEquals(importer.verseOrderList, [], 'verseOrderList for %s should be %s'
% (source_file_name, verse_order_list)) % (source_file_name, verse_order_list))
self.mocked_finish.assert_called_with() self.mocked_finish.assert_called_with()
def get_data(self, data, key): def get_data(self, data, key):
if key in data: if key in data:
return data[key] return data[key]
return u'' return ''