Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/testing/testexport.py
blob: 9e1c9e1a3a3961e5a8477f8034c1818045f01756 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
Tests website and scorm exports.
"""
# ===========================================================================
# testexport
# Copyright 2005, University of Auckland
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# ===========================================================================

import unittest
from exe.engine.package       import Package
from exe.engine.path          import Path, TempDirPath
from exe.export.websiteexport import WebsiteExport
from exe.export.scormexport   import ScormExport
from zipfile                  import ZipFile
from sets                     import Set

class TestWebsiteExport(unittest.TestCase):

    def testExport(self):
        # Delete the output dir
        outdir = TempDirPath()
        # Load a package
        package = Package.load('testPackage.elp')
        # Do the export
        exporter = WebsiteExport('../exe/webui/style/default', outdir,
                                 '../exe/webui/images', '../exe/webui/scripts')
        exporter.export(package)
        # Check that it all exists now
        assert outdir.isdir()
        assert (outdir / 'index.html').isfile()
        # Check that the style sheets have been copied
        for filename in Path('../exe/webui/style/default').files():
            assert ((outdir / filename.basename()).exists(),
                    'Style file "%s" not copied' % (outdir / filename.basename()))

        # Check that each node in the package has had a page made
        pagenodes  = Set([p.node for p in exporter.pages])
        othernodes = Set(self._getNodes([], package.root))
        assert pagenodes == othernodes

        for page in exporter.pages:
            self._testPage(page, outdir)

        
    def _getNodes(self, nodes, node):
        nodes.append(node)
        for child in node.children:
            self._getNodes(nodes, child)
        return nodes 


    def _testPage(self, page, outdir):
        """
        Tests for more or less correct creation of webpages
        """
        node = page.node
        pageName = outdir/page.name+'.html'
        assert pageName.exists(), 'HTML file "%s" not created' % pageName
        html = open(pageName).read()
        assert (node.title in html,
                'Node title (%s) not found in "%s"' % (node.title, pageName))


class BaseTestScormExport(unittest.TestCase):
    
    def doTest(self, withMeta):
        """Exports a package with meta data"""
        # Load our test package
        package = Package.load('testPackage.elp')
        # Do the export
        outFilename = Path('scormtest.zip')
        class MyConfig:
            def __init__(self):
                self.exePath = ""
        exporter = ScormExport(MyConfig(),
                               '../exe/webui/style/default', 
                               '../exe/webui/scripts', 
                               outFilename)
        exporter.export(package, withMeta)
        # Check that it made a nice zip file
        assert outFilename.exists()
        # See if the manifest file was created
        zipped = ZipFile(outFilename)
        filenames = zipped.namelist()
        assert 'imsmanifest.xml' in filenames, filenames
        self._testManifest(zipped.read('imsmanifest.xml'))

        # Test that all the node's html files have been generated
        pagenodes  = Set([p.node for p in exporter.pages])
        othernodes = Set(self._getNodes([], package.root))
        assert pagenodes == othernodes

        for page in exporter.pages:
            self._testPage(page, zipped)

        # Clean up
        zipped.close()

    def _testManifest(self, content):
        """Override this func to test different types of manifest files"""
        
    def _getNodes(self, nodes, node):
        nodes.append(node)
        for child in node.children:
            self._getNodes(nodes, child)
        return nodes 

    def _testPage(self, page, zipped):
        """Tests for more or less correct creation
        of webpages"""
        node     = page.node
        filename = page.name + '.html'
        assert (filename in zipped.namelist(), 
                'HTML file "%s" not created' % filename)
        html = zipped.read(filename)
        assert (node.title in html,
                'Node title (%s) not found in "%s"' % (node.title, filename))
        

class TestScormMetaExport(BaseTestScormExport):
    
    def testMeta(self):
        """Tests exporting of scorm packages with meta data"""
        self.doTest(True)

    def _testManifest(self, content):
        """Checks that there is meta data in 'content'"""
        assert '<metadata>' in content
        assert '</metadata>' in content
        assert 'xmlns:dc' in content
        assert 'dc:title' in content
        assert 'dc:creator' in content
        assert 'dc:description' in content
        assert 'dc:language' in content
        
class TestScormNoMetaExport(BaseTestScormExport):
    
    def testMeta(self):
        """Tests exporting of scorm packages without meta data"""
        self.doTest(False)

    def _testManifest(self, content):
        """Checks that there is meta data in 'content'"""
        assert '<metadata>' in content
        assert '</metadata>' in content
        assert 'xmlns:dc' not in content
        assert 'dc:title' not in content
        assert 'dc:creator' not in content
        assert 'dc:description' not in content
        assert 'dc:language' not in content
        



if __name__ == "__main__":
    unittest.main()