Page MenuHomec4science

test_positional_object.py
No OneTemporary

File Metadata

Created
Wed, May 8, 10:50

test_positional_object.py

import unittest
from os import sep, path
import lxml.etree as ET
import sys
sys.path.append('svgscripts')
from datatypes.positional_object import PositionalObject
from datatypes.matrix import Matrix
class TestObject(PositionalObject):
def __init__(self, id=0, height=0.0, width=0.0, x=0.0, y=0.0, msg='Hello World', matrix=None, tag='test-object'):
super(TestObject,self).__init__(id=id, height=height, width=width, x=x, y=y, matrix=matrix, tag=tag)
self.msg = msg
self.stringKeys.append('msg')
self.obj = None
class TestPositionalObject(unittest.TestCase):
def test_init(self):
faksimile_tag = 'faksimile-word'
po = PositionalObject(id=1, height=10, width=10, x=0, y=10, tag=faksimile_tag)
self.assertEqual(po.height, 10)
self.assertEqual(po.tag, faksimile_tag)
def test_attach_object_to_tree(self):
matrix = Matrix('matrix(0 0 0 0 0 0)')
tag = 'word-position'
po = PositionalObject(id=1, height=10, width=10, x=0, y=10, matrix=matrix, tag=tag)
empty_tree = ET.ElementTree(ET.Element('page'))
po.attach_object_to_tree(empty_tree)
self.assertEqual(po.tag, tag)
for node in empty_tree.getroot().xpath('//' + po.tag):
self.assertEqual(node.get('id'), '1')
self.assertEqual(node.get('bottom'), '20')
self.assertEqual(node.get('transform'), matrix.toString())
def test_init_node(self):
matrix = Matrix('matrix(0 0 0 0 0 0)')
tag = 'word-position'
poA = PositionalObject(id=1, height=10, width=10, x=0, y=10, matrix=matrix, tag=tag)
empty_tree = ET.ElementTree(ET.Element('page'))
poA.attach_object_to_tree(empty_tree)
poB = PositionalObject(node=empty_tree.getroot().find('./' + poA.tag))
self.assertEqual(poB.id, '1')
self.assertEqual(poB.height, 10)
self.assertEqual(poB.transform.toString(), matrix.toString())
self.assertEqual(poB.tag, tag)
def test_inheritance(self):
test = TestObject(id=1, height=10, width=10, x=0, y=10)
testB = TestObject(id=1, height=10, width=10, x=0, y=10)
test.obj = testB
test.attachable_objects.append(test.obj)
self.assertEqual(test.id, '1')
self.assertEqual(test.tag, 'test-object')
empty_tree = ET.ElementTree(ET.Element('page'))
test.attach_object_to_tree(empty_tree)
#print(ET.dump(empty_tree.getroot()))
for node in empty_tree.getroot().xpath('//' + test.tag):
self.assertEqual(node.get('id'), '1')
self.assertEqual(node.get('bottom'), '20')
self.assertEqual(node.get('msg'), 'Hello World')
def test_POSITIONS_OVERLAP_HORIZONTALLY(self):
pa = PositionalObject(id=0, height=10, width=10, x=0)
pb = PositionalObject(id=1, height=10, width=10, x=1)
pc = PositionalObject(id=2, height=10, width=10, x=10)
px = PositionalObject(id=9, height=1, width=1)
self.assertEqual(PositionalObject.POSITIONS_OVERLAP_HORIZONTALLY(pa, pb), True)
self.assertEqual(PositionalObject.POSITIONS_OVERLAP_HORIZONTALLY(pa, pc), False)
self.assertEqual(PositionalObject.POSITIONS_OVERLAP_HORIZONTALLY(pb, pc), True)
self.assertEqual(PositionalObject.POSITIONS_OVERLAP_HORIZONTALLY(pa, px), True)
def test_POSITIONS_OVERLAP_VERTICALLY(self):
pa = PositionalObject(id=0, height=10, width=10, y=0)
pb = PositionalObject(id=1, height=10, width=10, y=1)
pc = PositionalObject(id=2, height=10, width=10, y=10)
self.assertEqual(PositionalObject.POSITIONS_OVERLAP_VERTICALLY(pa, pb), True)
self.assertEqual(PositionalObject.POSITIONS_OVERLAP_VERTICALLY(pa, pc), False)
self.assertEqual(PositionalObject.POSITIONS_OVERLAP_VERTICALLY(pb, pc), True)
def test_POSITIONS_ARE_STACKED(self):
pa = PositionalObject(id=0, height=10, width=10)
pb = PositionalObject(id=1, height=10, width=10, x=10)
pc = PositionalObject(id=2, height=10, width=10, x=10, y=10)
px = PositionalObject(id=9, height=10, width=10, x=10, y=6)
self.assertEqual(PositionalObject.POSITIONS_ARE_STACKED(pa, pb), False)
self.assertEqual(PositionalObject.POSITIONS_ARE_STACKED(pa, pc), False)
self.assertEqual(PositionalObject.POSITIONS_ARE_STACKED(pb, pc), True)
self.assertEqual(PositionalObject.POSITIONS_ARE_STACKED(pb, px), True)
def test_get_semanticAndDataDict(self):
matrix = Matrix('matrix(0 0 0 0 0 0)')
po = PositionalObject(id=1, height=10, width=10, x=0, y=10, matrix=matrix)
empty_tree = ET.ElementTree(ET.Element('page'))
po.attach_object_to_tree(empty_tree)
dict = PositionalObject.get_semantic_dictionary()
for key in dict['properties'].keys():
cls, cardinality, xpath = dict['properties'].get(key)
results = empty_tree.xpath(xpath)
self.assertEqual(len(results), 1)
#print('{}: {}'.format(key, results[0]))
if __name__ == "__main__":
unittest.main()

Event Timeline