aboutsummaryrefslogtreecommitdiffstats
path: root/test_generate_html.py
blob: 405ecf021672359cee6f059f026a47617cd4b63e (plain) (blame)
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
#!/usr/bin/python3
import unittest
import generate_html
import subprocess
import lxml.etree as et
import lxml.html

TEST_FILE = "hes12kni.tab"
test_dict = {'no': '1', 'de_name': '1.Mose',
    'cs_name': '1.Mojžíšova', 'de_abbr': 'Gn',
    'test': '-', 'cs_abbr': 'Gn'}
TEST_LOSUNGEN = "hes12-01.xml"


class TestProcessCSV(unittest.TestCase):
    def test_generate_dict(self):
        los_dict = generate_html.csv2dict(TEST_FILE)
        self.assertEqual(len(los_dict), 78)
        self.assertEqual(los_dict['Gn'], test_dict)


class TestProcessLosungen(unittest.TestCase):
    def setUp(self):
        self.los_elements = generate_html.parse_file(TEST_LOSUNGEN)

    def test_parse_losungen(self):
        proc = subprocess.Popen(["tidy", "-e", "-f", "/dev/null"],
                stdin=subprocess.PIPE, bufsize=-1, close_fds=True)
        proc.communicate(self.los_elements.encode("utf-8"))
        self.assertEqual(proc.returncode, 0)

    def test_attributes_on_articles(self):
        # Every article has to have id attribute
        root = lxml.html.fromstring(self.los_elements)
        empty_articles = root.xpath("//article[not(@id)]")
        self.assertEqual(len(empty_articles), 0)

    def test_appropriate_elements(self):
        # We need one <script> and one <style> elements in <head>
        root = lxml.html.fromstring(self.los_elements)
        script_element = root.xpath("//head/script")
        self.assertEqual(len(script_element), 1)
        self.assertEqual(script_element[0].attrib["src"], "hesla.js")
        style_element = root.xpath("//head/link[@rel='stylesheet']")
        self.assertEqual(len(style_element), 1)
        meta_element = root.xpath("//head/meta[@content]")
        self.assertEqual(meta_element[0].attrib["content"],
                "width=device-width, initial-scale=1.0, " + \
                " maximum-scale=2.0, user-scalable=yes")


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