2
# -*- coding: utf-8 -*-
3
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
5
###############################################################################
6
# OpenLP - Open Source Lyrics Projection #
7
# --------------------------------------------------------------------------- #
8
# Copyright (c) 2008-2011 Raoul Snyman #
9
# Portions copyright (c) 2008-2011 Tim Bentley, Gerald Britton, Jonathan #
10
# Corwin, Michael Gorven, Scott Guerrieri, Matthias Hub, Meinert Jordan, #
11
# Armin Köhler, Joshua Millar, Stevan Pettit, Andreas Preikschat, Mattias #
12
# Põldaru, Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
13
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Frode Woldsund #
14
# --------------------------------------------------------------------------- #
15
# This program is free software; you can redistribute it and/or modify it #
16
# under the terms of the GNU General Public License as published by the Free #
17
# Software Foundation; version 2 of the License. #
19
# This program is distributed in the hope that it will be useful, but WITHOUT #
20
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
21
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
24
# You should have received a copy of the GNU General Public License along #
25
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
26
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
27
###############################################################################
30
OpenLyrics import/export tests
33
from lxml import etree
35
from openlp.plugins.songs.lib.db import Song
36
from openlp.plugins.songs.lib import OpenLyrics, SongXML
39
# Import from xml containing openlyrics 0.7
40
IMPORTED_VERSES_0_7 = [
42
{'type': 'v', 'label': '1'},
43
u'Jezu Kriste, štědrý kněže,\n'
44
u's Otcem, Duchem jeden Bože,\n'
45
u'štědrost Tvá je naše zboží,\n'
49
{'type': 'v', 'label': '2'},
50
u'Ty jsi v světě, bydlil s námi,\n'
51
u'Tvé tělo trpělo rány\n'
52
u'za nás za hříšné křesťany,\n'
58
{'type': 'v', 'label': '1'},
59
u'{r}Jezu Kriste{/r}, štědrý kněže,\n'
60
u's {bl}Otcem, Duchem{/bl} jeden {y}Bože{/y},\n'
61
u'štědrost Tvá je naše zboží,\n'
62
u'z {o}{st}Tvé{/st}{/o} {it}milosti{/it}.'
65
{'type': 'v', 'label': '2'},
66
u'{bl}Ty{/bl} jsi v světě, bydlil s námi,\n'
67
u'Tvé tělo trpělo rány\n'
68
u'za nás za hříšné křesťany,\n'
69
u'z {bl}Tvé{/bl} milosti.'
72
{'type': 'v', 'label': '3'},
73
u'Ó, {g}Tvá dobroto{/g} důstojná\n'
74
u'a k nám milosti přehojná!\n'
75
u'Dáváš nám bohatství mnohá\n'
76
u'{st}{y}z Tvé milosti.{/y}{/st}'
79
{'type': 'v', 'label': '4'},
80
u'Ráčils nás sám zastoupiti,\n'
81
u'{it}život za nás položiti,{/it}\n'
82
u'tak smrt věčnou zahladiti,\n'
86
{'type': 'v', 'label': '5'},
87
u'{st}Ó, {r}křesťané{/r}, z bludů{/st} vstaňme,\n'
88
u'dané dobro nám poznejme,\n'
89
u'k Synu Božímu chvátejme,\n'
93
{'type': 'v', 'label': '6'},
94
u'Chvála{br}budiž{br}Bohu{br}Otci,\n'
95
u'{st}Synu jeho téže moci,\n'
96
u'Duchu jeho rovné moci,\n'
97
u'z též milosti!{/st}'
102
def _cmp_verses(lyrics1, lyrics2):
103
for a, b in zip(lyrics1, lyrics2):
104
assert a[0]['type'] == b[0]['type']
105
assert a[0]['label'] == b[0]['label']
110
def _cmp_xml_files(file1, file2):
111
for l1, l2 in zip(file1.readlines(), file2.readlines()):
112
# skip line with item modifiedDate - it is unique everytime
113
if l1.startswith('<song xmlns='):
118
def _import_xml(db, xml_file):
119
parser = etree.XMLParser(remove_blank_text=True)
120
parsed_file = etree.parse(open(xml_file.strpath, u'r'), parser)
121
xml = unicode(etree.tostring(parsed_file))
123
song = o.xml_to_song(xml)
127
def _export_xml(db, song, xml_file):
129
xml = o.song_to_xml(song)
130
tree = etree.ElementTree(etree.fromstring(xml))
131
tree.write(open(xml_file.strpath, u'w'), encoding=u'utf-8', xml_declaration=True,
135
def test_openlyrics_import(openlp_runner, pth, tmpdir):
136
f = pth.songs.join('openlyrics_formatting_tags.xml')
137
song = _import_xml(openlp_runner.get_songs_db(empty=True), f)
138
# compare verses and their content
140
verses = sxml.get_verses(song.lyrics)
141
_cmp_verses(verses, IMPORTED_VERSES)
144
def test_openlyrics_import_0_7(openlp_runner, pth, tmpdir):
145
f = pth.songs.join('openlyrics_0.7_import.xml')
146
song = _import_xml(openlp_runner.get_songs_db(empty=True), f)
147
# compare verses and their content
149
verses = sxml.get_verses(song.lyrics)
151
_cmp_verses(verses, IMPORTED_VERSES_0_7)
154
def test_openlyrics_export(openlp_runner, openlyrics_validator, pth, tmpdir):
155
# export song to file
156
db = openlp_runner.get_songs_db()
157
f = tmpdir.join('out.xml')
158
s = db.get_all_objects(Song)[0]
159
_export_xml(db, s, f)
161
assert openlyrics_validator.validate(f.strpath) == True
162
# string comparison with original file line by line
163
_cmp_xml_files(f, pth.songs.join('openlyrics_formatting_tags.xml'))
166
def test_openlyrics_import_and_export(openlp_runner, pth, tmpdir):
167
# import song, then export it and compare with original song.
168
f = pth.songs.join('openlyrics_formatting_tags.xml')
169
f_exp = tmpdir.join('out.xml')
170
db = openlp_runner.get_songs_db(empty=True)
171
song = _import_xml(db, f)
172
_export_xml(db, song, f_exp)
174
_cmp_xml_files(f, f_exp)