aboutsummaryrefslogblamecommitdiffstats
path: root/test_generate_html.py
blob: d302da308880f5b309b6cf9486dd4973d0f6e973 (plain) (tree)
1
2
3
4
5
6
7
8
9
                  
               
                                        
                 
                                  
                


                                            

                                                          








                                                    
 



























                                                                                 



                                                                         






                                                                   


                                                      
                                                    
                                            


                                                
                                                      




                                                                 
                                                      
                                                    
                                                
                                                                     

                                                                    
                                                          
                                                           

                                                                    



                          
#!/usr/bin/python3
import unittest
import generate_html_cs as generate_html
import subprocess
import xml.etree.ElementTree 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 TestMethods(unittest.TestCase):
    def test_translate_book_abbrs(self):
        """
        Example XML piece:
        Description of bug #78:
            Czech biblical references are not understood by crosswire
            website. So, when I ask for J 6:39, I get a book of Joshua,
            not the gospel of John.
        """
        test_element_str = '''
         <LOSUNG d="24" m="10">
          <TL>Středa 24. října 2012</TL>
          <OT>
           <S b="Ez" ch="34" v="16"/>
           <L>Ztracenou vypátrám, zaběhlou přivedu zpět.</L>
           <SL>Ezechiel 34,16</SL>
          </OT>
          <NT>
           <S b="J" ch="6" v="39"/>
           <L>Jeho vůle jest, abych neztratil nikoho z těch, které mi dal,</L>
           <L>ale vzkřísil je v poslední den.</L>
           <SL>Jan 6,39</SL>
          </NT>
          <SR><SL>1.Mojžíšova 24,54b-67</SL></SR>
          <CR><SL>2.Korintským 4,1-6</SL></CR>
         </LOSUNG>
        '''
        test_element = et.fromstring(test_element_str)
        wword = generate_html.parse_losung(test_element)
        self.assertEqual(wword[1]['watchwords'][1]['int_ref_id']['book'],
                         'John')


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())
        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), 2)
        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()