Page MenuHomec4science

test_main.py
No OneTemporary

File Metadata

Created
Sat, Nov 9, 06:11

test_main.py

import unittest
import os
from os.path import isfile, isdir, dirname, sep, realpath
from datetime import datetime
import shutil
import tempfile
import xml.etree.ElementTree as ET
import lxml.etree as LET
from rdflib import Graph, URIRef, Literal, BNode, OWL, RDF, RDFS, XSD
from xmldiff import main
import sys
sys.path.append('svgscripts')
from datatypes.page import Page
sys.path.append('shared_util')
try:
from myxmlwriter import attach_dict_to_xml_node, dict2xml, lock_xml_tree, update_metadata, write_pretty, test_lock, xml_has_type,\
FILE_TYPE_SVG_WORD_POSITION, FILE_TYPE_XML_DICT, get_dictionary_from_node, xml2dict, parse_xml_of_type
except ImportError:
sys.path.append(dirname(dirname(realpath(__file__))))
from shared_util.myxmlwriter import attach_dict_to_xml_node, dict2xml, lock_xml_tree, update_metadata, write_pretty, test_lock, xml_has_type,\
FILE_TYPE_SVG_WORD_POSITION, FILE_TYPE_XML_DICT, get_dictionary_from_node, xml2dict, parse_xml_of_type
class TestPrettyWriter(unittest.TestCase):
def setUp(self):
self.test_dir = tempfile.mkdtemp()
self.title = 'ASDF'
DATADIR = dirname(__file__) + sep + 'test_data'
self.page = DATADIR + sep + 'N_VII_1_page001.xml'
self.mydict = { 'asdf': { 'b': { 'a': 1, 'b': 'c' , 'c': URIRef('adf')}},\
'str': 'test' }
def test_attach_dict_to_xml_node(self):
xml_tree = LET.Element('root')
attach_dict_to_xml_node(self.mydict, LET.SubElement(xml_tree, 'dict'))
#print(LET.dump(xml_tree))
self.assertEqual(xml_tree.xpath('//asdf/b/a/@type')[0], 'int')
self.assertEqual(xml_tree.xpath('//asdf/b/b/@type')[0], 'str')
self.assertEqual(xml_tree.xpath('//asdf/b/c/@type')[0], URIRef.__name__)
def test_dict2xml(self):
test_file = self.test_dir + sep + 'new_test.xml'
dict2xml(self.mydict, test_file)
self.assertEqual(isfile(test_file), True)
def test_get_dictionary_from_node(self):
test_file = self.test_dir + sep + 'source.xml'
dict2xml(self.mydict, test_file)
xml_tree = LET.parse(test_file)
self.assertEqual(len(xml_tree.xpath('/root/dict')[0].getchildren()), len(self.mydict.keys()))
for index, key in enumerate(self.mydict.keys()):
mydict = get_dictionary_from_node(xml_tree.xpath('/root/dict')[0].getchildren()[index])
self.assertEqual(key in mydict.keys(), True)
if type(self.mydict[key]) == dict:
self.assertEqual(mydict[key].keys(), self.mydict[key].keys())
def test_update_metadata(self):
test_tree = LET.ElementTree(LET.Element('page', attrib={"title": self.title}))
update_metadata(test_tree, __file__)
self.assertEqual(test_tree.find('./metadata').find('./createdBy').find('./script').text, __file__)
update_metadata(test_tree, __file__)
self.assertEqual(len(test_tree.find('./metadata').findall('./modifiedBy[@script="{}"]'.format(__file__))), 1)
update_metadata(test_tree, __file__)
self.assertEqual(len(test_tree.find('./metadata').findall('./modifiedBy[@script="{}"]'.format(__file__))), 1)
def test_write_pretty(self):
et_file = self.test_dir + os.sep + 'et_file.xml'
pretty_file = self.test_dir + os.sep + 'pretty_file.xml'
manuscript_tree = ET.ElementTree(ET.Element('page', attrib={"title": self.title}))
metadata = ET.SubElement(manuscript_tree.getroot(), 'metadata')
ET.SubElement(metadata, 'type').text = 'xmlManuscriptFile'
createdBy = ET.SubElement(metadata, 'createdBy')
manuscript_tree.write(et_file, xml_declaration=True, encoding='utf-8')
write_pretty(xml_string=ET.tostring(manuscript_tree.getroot()), file_name=pretty_file)
self.assertEqual(main.diff_files(et_file, pretty_file), [])
write_pretty(xml_element_tree=manuscript_tree, file_name=pretty_file)
self.assertEqual(main.diff_files(et_file, pretty_file), [])
def test_lock(self):
page = Page(self.page)
locker_dict = { 'reference_file': 'asdf.txt', 'message': 'locked on this file'}
lock_xml_tree(page.page_tree, **locker_dict)
self.assertEqual(page.is_locked(), True)
#test_lock(page.page_tree)
def test_xml2dict(self):
test_file = self.test_dir + sep + 'source.xml'
dict2xml(self.mydict, test_file)
mydict = xml2dict(test_file)
self.assertEqual(mydict, self.mydict)
def test_xml_has_type(self):
self.assertEqual(xml_has_type(FILE_TYPE_SVG_WORD_POSITION, xml_source_file=self.page), True)
self.assertEqual(xml_has_type(FILE_TYPE_XML_DICT, xml_source_file=self.page), False)
with self.assertRaises(Exception):
parse_xml_of_type(self.page, FILE_TYPE_XML_DICT)
def tearDown(self):
isdir(self.test_dir) and shutil.rmtree(self.test_dir)
if __name__ == "__main__":
unittest.main()

Event Timeline