def test_resolve_ref(self): # item has no reference attribute b1 = Item(id='b1') self.assertRaises(KeyError, b1.resolve_ref, 'alignment') # has a reference attribute, but is not contained by a tier b1.alignment = 'a1' self.assertRaises(XigtStructureError, b1.resolve_ref, 'alignment') # item in tier, but tier has no reference attribute t_b = Tier(id='b', items=[b1]) self.assertRaises(KeyError, b1.resolve_ref, 'alignment') # tier has reference attribute, but is not contained by an Igt t_b.alignment = 'a' self.assertRaises(XigtStructureError, b1.resolve_ref, 'alignment') # item in IGT, but referred tier doesn't exist igt = Igt(tiers=[t_b]) self.assertRaises(XigtStructureError, b1.resolve_ref, 'alignment') # referred tier exists, but has no item referred by item's alignment t_a = Tier(id='a') igt.append(t_a) self.assertRaises(XigtStructureError, b1.resolve_ref, 'alignment') # referred item exists, but has no value (which resolves to '') a1 = Item(id='a1') t_a.append(a1) self.assertEqual(b1.resolve_ref('alignment'), '') # referred item has a value a1.text = 'text' self.assertEqual(b1.resolve_ref('alignment'), 'text') # stored item tests self.assertRaises(KeyError, self.i1.resolve_ref, 'alignment') self.assertRaises(KeyError, self.i2.resolve_ref, 'alignment') self.assertEqual(self.i_ac.resolve_ref('alignment'), 'text') self.assertEqual(self.i_ac.resolve_ref('content'), 'te') self.assertEqual(self.i_s.resolve_ref('segmentation'), 'xt') self.assertEqual(self.i_t.resolve_ref('content'), 'text')
class TestItem(unittest.TestCase): def setUp(self): # empty self.i1 = Item() # basic info self.i2 = Item( id='i2', type='basic', attributes={'attr':'val'}, text='text' ) # alignment and content refs self.i_ac = Item( id='i_ac', alignment='i2', content='i2[0:2]' ) # segmentation ref self.i_s = Item( id='i_s', segmentation='i2[2:4]' ) # override content ref with text self.i_t = Item( id='i_t', content='i2', text='something else' ) # contextual structure self.t_a = Tier(id='t_a', items=[self.i2]) self.t_b = Tier(id='t_b', items=[self.i_ac, self.i_t], alignment='t_a', content='t_a') self.t_c = Tier(id='t_c', items=[self.i_s], segmentation='t_a') self.igt = Igt(tiers=[self.t_a, self.t_b, self.t_c]) self.xc = XigtCorpus(igts=[self.igt]) def test_init(self): self.assertRaises(ValueError, Item, id='1') # invalid id def test_id(self): self.assertIs(self.i1.id, None) self.assertEqual(self.i2.id, 'i2') self.assertEqual(self.i_ac.id, 'i_ac') self.assertEqual(self.i_s.id, 'i_s') self.assertEqual(self.i_t.id, 'i_t') def test_type(self): self.assertIs(self.i1.type, None) self.assertEqual(self.i2.type, 'basic') self.assertIs(self.i_ac.type, None) self.assertIs(self.i_s.type, None) self.assertIs(self.i_t.type, None) def test_parents(self): self.assertIs(self.i1.tier, None) self.assertIs(self.i1.igt, None) self.assertIs(self.i1.corpus, None) self.assertIs(self.i2.tier, self.t_a) self.assertIs(self.i2.igt, self.igt) self.assertIs(self.i2.corpus, self.xc) self.assertEqual(self.i_ac.tier, self.t_b) self.assertEqual(self.i_ac.igt, self.igt) self.assertEqual(self.i_ac.corpus, self.xc) self.assertEqual(self.i_s.tier, self.t_c) self.assertEqual(self.i_s.igt, self.igt) self.assertEqual(self.i_s.corpus, self.xc) self.assertEqual(self.i_t.tier, self.t_b) self.assertEqual(self.i_t.igt, self.igt) self.assertEqual(self.i_t.corpus, self.xc) def test_attributes(self): self.assertEqual(self.i1.attributes, dict()) self.assertEqual(self.i2.attributes, {'attr':'val'}) self.assertEqual(self.i_ac.attributes, {'alignment': 'i2', 'content': 'i2[0:2]'}) self.assertEqual(self.i_s.attributes, {'segmentation': 'i2[2:4]'}) self.assertEqual(self.i_t.attributes, {'content': 'i2'}) def test_reference_attributes(self): # segmentation cannot co-occur with alignment or content self.assertRaises(XigtError, Item, alignment='a1', segmentation='b1') self.assertRaises(XigtError, Item, content='a1', segmentation='b1') self.assertIs(self.i1.alignment, None) self.assertIs(self.i1.content, None) self.assertIs(self.i1.segmentation, None) self.assertIs(self.i2.alignment, None) self.assertIs(self.i2.content, None) self.assertIs(self.i2.segmentation, None) self.assertEqual(self.i_ac.alignment, 'i2') self.assertEqual(self.i_ac.content, 'i2[0:2]') self.assertIs(self.i_ac.segmentation, None) self.assertIs(self.i_s.alignment, None) self.assertIs(self.i_s.content, None) self.assertEqual(self.i_s.segmentation, 'i2[2:4]') self.assertEqual(self.i_t.alignment, None) self.assertEqual(self.i_t.content, 'i2') self.assertEqual(self.i_t.segmentation, None) def test_text(self): self.assertIs(self.i1.text, None) self.assertEqual(self.i2.text, 'text') self.assertIs(self.i_ac.text, None) self.assertIs(self.i_s.text, None) self.assertEqual(self.i_t.text, 'something else') def test_value(self): self.assertIs(self.i1.value(), None) self.assertEqual(self.i2.value(), 'text') self.assertEqual(self.i_ac.value(), 'te') self.assertEqual(self.i_s.value(), 'xt') self.assertEqual(self.i_t.value(), 'something else') def test_resolve_ref(self): # item has no reference attribute b1 = Item(id='b1') self.assertRaises(KeyError, b1.resolve_ref, 'alignment') # has a reference attribute, but is not contained by a tier b1.alignment = 'a1' self.assertRaises(XigtStructureError, b1.resolve_ref, 'alignment') # item in tier, but tier has no reference attribute t_b = Tier(id='b', items=[b1]) self.assertRaises(KeyError, b1.resolve_ref, 'alignment') # tier has reference attribute, but is not contained by an Igt t_b.alignment = 'a' self.assertRaises(XigtStructureError, b1.resolve_ref, 'alignment') # item in IGT, but referred tier doesn't exist igt = Igt(tiers=[t_b]) self.assertRaises(XigtStructureError, b1.resolve_ref, 'alignment') # referred tier exists, but has no item referred by item's alignment t_a = Tier(id='a') igt.append(t_a) self.assertRaises(XigtStructureError, b1.resolve_ref, 'alignment') # referred item exists, but has no value (which resolves to '') a1 = Item(id='a1') t_a.append(a1) self.assertEqual(b1.resolve_ref('alignment'), '') # referred item has a value a1.text = 'text' self.assertEqual(b1.resolve_ref('alignment'), 'text') # stored item tests self.assertRaises(KeyError, self.i1.resolve_ref, 'alignment') self.assertRaises(KeyError, self.i2.resolve_ref, 'alignment') self.assertEqual(self.i_ac.resolve_ref('alignment'), 'text') self.assertEqual(self.i_ac.resolve_ref('content'), 'te') self.assertEqual(self.i_s.resolve_ref('segmentation'), 'xt') self.assertEqual(self.i_t.resolve_ref('content'), 'text') def test_span(self): # sub-spans of null content is also null content self.assertIs(self.i1.span(0,1), None) self.assertEqual(self.i2.span(0,1), 't') self.assertEqual(self.i_ac.span(1,2), 'e') self.assertEqual(self.i_s.span(1,2), 't') self.assertEqual(self.i_t.span(1,2), 'o') def test_get_attribute(self): i = Item(id='i1') self.assertEqual(i.get_attribute('attr'), None) self.assertEqual(i.get_attribute('attr', 1), 1) i.attributes['attr'] = 'val' self.assertEqual(i.get_attribute('attr', 1), 'val') self.assertEqual(i.get_attribute('abc', inherit=True), None) t = Tier(id='t', items=[i], attributes={'abc': 'def'}) self.assertEqual(i.get_attribute('abc', inherit=True), 'def') self.assertEqual(self.i1.get_attribute('attr'), None) self.assertEqual(self.i1.get_attribute('attr', 1), 1) self.assertEqual(self.i2.get_attribute('attr'), 'val') self.assertEqual(self.i2.get_attribute('attr', 1), 'val') self.assertEqual(self.i_ac.get_attribute('alignment'), 'i2')