~wgrant/openlp/openlyrics_test

« back to all changes in this revision

Viewing changes to testing/test_openlyrics.py

  • Committer: Martin Zibricky
  • Date: 2011-09-21 21:48:58 UTC
  • Revision ID: mzibr.public@gmail.com-20110921214858-ye6zh502b8f5ej0p
Readding openlyrics tests after openlyrics branch merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
 
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.                              #
 
18
#                                                                             #
 
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    #
 
22
# more details.                                                               #
 
23
#                                                                             #
 
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
###############################################################################
 
28
 
 
29
"""
 
30
OpenLyrics import/export tests
 
31
"""
 
32
 
 
33
from lxml import etree
 
34
 
 
35
from openlp.plugins.songs.lib.db import Song
 
36
from openlp.plugins.songs.lib import OpenLyrics, SongXML
 
37
 
 
38
 
 
39
# Import from xml containing openlyrics 0.7
 
40
IMPORTED_VERSES_0_7 = [
 
41
    [
 
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'
 
46
        u'z Tvé milosti.'
 
47
    ],
 
48
    [
 
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'
 
53
        u'z Tvé milosti.'
 
54
    ],
 
55
]
 
56
IMPORTED_VERSES = [
 
57
    [
 
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}.'
 
63
    ],
 
64
    [
 
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.'
 
70
    ],
 
71
    [
 
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}'
 
77
    ],
 
78
    [
 
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'
 
83
        u'z Tvé milosti.'
 
84
    ],
 
85
    [
 
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'
 
90
        u'k té milosti!'
 
91
    ],
 
92
    [
 
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}'
 
98
    ]
 
99
]
 
100
 
 
101
 
 
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']
 
106
        # compare lyrics
 
107
        assert a[1] == b[1]
 
108
 
 
109
 
 
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='):
 
114
            continue
 
115
        assert l1 == l2
 
116
 
 
117
 
 
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))
 
122
    o = OpenLyrics(db)
 
123
    song = o.xml_to_song(xml)
 
124
    return song
 
125
 
 
126
 
 
127
def _export_xml(db, song, xml_file):
 
128
    o = OpenLyrics(db)
 
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,
 
132
        pretty_print=True)
 
133
 
 
134
 
 
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
 
139
    sxml = SongXML()
 
140
    verses = sxml.get_verses(song.lyrics)
 
141
    _cmp_verses(verses, IMPORTED_VERSES)
 
142
 
 
143
 
 
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
 
148
    sxml = SongXML()
 
149
    verses = sxml.get_verses(song.lyrics)
 
150
    print verses
 
151
    _cmp_verses(verses, IMPORTED_VERSES_0_7)
 
152
 
 
153
 
 
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)
 
160
    # validate file
 
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'))
 
164
 
 
165
 
 
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)
 
173
    # compare files
 
174
    _cmp_xml_files(f, f_exp)