def test_remove(self): md = Metadata(metas=[Meta(id='m1'), Meta(id='m2')]) assert len(md) == 2 md.remove(md[0]) assert len(md) == 1 assert md[0].id == 'm2' with pytest.raises(KeyError): md['m1']
class DelMetaTests(TestCase): def setUp(self): self.t = Tier() self.md = Metadata() self.t.metadata = [self.md] self.m1 = Meta(text='foo') self.m2 = Meta(type='foobar', attributes={'foo':'bar'}) def no_meta_remains_test(self): self.md.append(self.m2) self.assertEqual(len(self.t.metadata), 1) self.assertEqual(len(self.md), 1) del_meta_attr(self.t, 'foobar', 'foo') self.assertEqual(len(self.md), 0) self.assertEqual(len(self.t.metadata), 0) def meta_remains_test(self): self.md.append(self.m1) self.md.append(self.m2) self.assertEqual(len(self.t.metadata), 1) self.assertEqual(len(self.md), 2) del_meta_attr(self.t, 'foobar', 'foo') self.assertEqual(len(self.t.metadata), 1)
def setUp(self): self.md1 = Metadata() self.md2 = Metadata( id='md2', type='basic', attributes={'attr':'val'}, metas=[Meta(id='meta1', text='meta')] )
def test_append(self): md = Metadata() self.assertRaises(XigtStructureError, md.append, Item()) self.assertRaises(XigtStructureError, md.append, Tier()) self.assertRaises(XigtStructureError, md.append, Igt()) self.assertRaises(XigtStructureError, md.append, XigtCorpus()) self.assertRaises(XigtStructureError, md.append, Metadata()) self.assertEqual(len(md), 0) md.append(Meta(id='meta1')) self.assertEqual(len(md), 1) self.assertRaises(XigtError, md.append, Meta(id='meta1')) md.append(Meta(id='meta2')) self.assertEqual(len(md), 2) self.assertEqual(md[0].id, 'meta1') self.assertEqual(md[1].id, 'meta2')
def test_insert(self): md = Metadata() assert len(md) == 0 md.insert(0, Meta(id='meta1')) assert len(md) == 1 with pytest.raises(XigtError): md.insert(0, Meta(id='meta1')) md.insert(0, Meta(id='meta2')) md.insert(100, Meta(id='meta3')) assert len(md) == 3 assert md[0].id == 'meta2' assert md[1].id == 'meta1' assert md[2].id == 'meta3'
def setUp(self): self.t = Tier() self.md = Metadata() self.t.metadata = [self.md] self.m1 = Meta(text='foo') self.m2 = Meta(type='foobar', attributes={'foo':'bar'})
def test_clear(self): md = Metadata() md.extend([Meta(id='meta1'), Meta(id='meta2'), Meta(id='meta3')]) self.assertEqual(len(md), 3) md.clear() self.assertEqual(len(md), 0) self.assertIs(md.get(0), None) self.assertIs(md.get('meta1'), None)
def test_clear(self): md = Metadata() md.extend([Meta(id='meta1'), Meta(id='meta2'), Meta(id='meta3')]) assert len(md) == 3 md.clear() assert len(md) == 0 assert md.get(0) is None assert md.get('meta1') is None
def make_igt_metadata(block, options): md = Metadata() mi = 1 if options["keep_headers"] and block.get("header_lines"): md.append(Meta(id="meta{}".format(mi), type="odin-header", text="\n".join(block.get("header_lines", [])))) mi += 1 lg_code = block.get("iso-639-3") if lg_code is None: lg_code = "und" # undetermined language = (block.get("language") or "").strip() subj = '\n <dc:subject xsi:type="olac:language" ' 'olac:code="{}">{}</dc:subject>'.format( lg_code.strip(), language ) lang = '\n <dc:language xsi:type="olac:language" ' 'olac:code="en">English</dc:language>' md.append(Meta(id="meta{}".format(mi), children=[subj, lang])) return md
def test_get_attribute(self): md = Metadata( attributes={'one': 1, 'two': 2, '{http://namespace.org}three': 4}, nsmap={'pre': 'http://namespace.org'} ) igt = Igt(metadata=[md], attributes={'three': 3}) assert md.get_attribute('one') == 1 assert md.get_attribute('two') == 2 assert md.get_attribute('three') is None assert md.get_attribute('three', namespace='http://namespace.org') == 4 assert md.get_attribute('three', namespace='pre') == 4 assert md.get_attribute('three', inherit=True) == 3 assert md.get_attribute('three', namespace='pre', inherit=True) == 4 assert md.get_attribute('three', default=5) == 5
def make_igt_metadata(block, options): md = Metadata() mi = 1 if options['keep_headers'] and block.get('header_lines'): md.append( Meta(id='meta{}'.format(mi), type='odin-header', text='\n'.join(block.get('header_lines', [])))) mi += 1 lg_code = block.get('iso-639-3') if lg_code is None: lg_code = 'und' # undetermined # should we title-case language? # language = (block.get('language') or '').strip().title() language = (block.get('language') or '').strip() subj = MetaChild('subject', attributes={ qattrname('type', 'xsi'): 'olac:language', qattrname('code', 'olac'): lg_code.strip() }, text=language.title(), namespace=_nsmap['dc']) lang = MetaChild('language', attributes={ qattrname('type', 'xsi'): 'olac:language', qattrname('code', 'olac'): 'en' }, text='English', namespace=_nsmap['dc']) md.append(Meta(id='meta{}'.format(mi), children=[subj, lang])) return md
def make_igt_metadata(block, options): md = Metadata() mi = 1 if options['keep_headers'] and block.get('header_lines'): md.append(Meta(id='meta{}'.format(mi), type='odin-header', text='\n'.join(block.get('header_lines', [])) )) mi += 1 lg_code = block.get('iso-639-3') if lg_code is None: lg_code = 'und' # undetermined # should we title-case language? # language = (block.get('language') or '').strip().title() language = (block.get('language') or '').strip() subj = MetaChild( 'subject', attributes={ qattrname('type', 'xsi'): 'olac:language', qattrname('code', 'olac'): lg_code.strip() }, text=language.title(), namespace=_nsmap['dc'] ) lang = MetaChild( 'language', attributes={ qattrname('type', 'xsi'): 'olac:language', qattrname('code', 'olac'): 'en' }, text='English', namespace=_nsmap['dc'] ) md.append(Meta(id='meta{}'.format(mi), children=[subj, lang])) return md
def test_extend(self): md = Metadata() assert len(md) == 0 md.extend([Meta(id='meta1')]) assert len(md) == 1 md.extend([]) assert len(md) == 1 md.extend([Meta(id='meta2'), Meta(id='meta3')]) assert len(md) == 3 assert md[0].id == 'meta1' assert md[1].id == 'meta2' assert md[2].id == 'meta3'
def test_extend(self): md = Metadata() self.assertEqual(len(md), 0) md.extend([Meta(id='meta1')]) self.assertEqual(len(md), 1) md.extend([]) self.assertEqual(len(md), 1) md.extend([Meta(id='meta2'), Meta(id='meta3')]) self.assertEqual(len(md), 3) self.assertEqual(md[0].id, 'meta1') self.assertEqual(md[1].id, 'meta2') self.assertEqual(md[2].id, 'meta3')
def test_insert(self): md = Metadata() self.assertEqual(len(md), 0) md.insert(0, Meta(id='meta1')) self.assertEqual(len(md), 1) self.assertRaises(XigtError, md.insert, 0, Meta(id='meta1')) md.insert(0, Meta(id='meta2')) md.insert(100, Meta(id='meta3')) self.assertEqual(len(md), 3) self.assertEqual(md[0].id, 'meta2') self.assertEqual(md[1].id, 'meta1') self.assertEqual(md[2].id, 'meta3')
def default_decode_metadata(elem): if elem is None: return None ns, tag = _qname_split(elem.tag) assert tag == 'metadata' metadata = Metadata( id=elem.get('id'), type=elem.get('type'), attributes=get_attributes(elem, ignore=('id', 'type')), metas=[decode_meta(meta) for meta in elem.findall('meta')], namespace=ns, nsmap=elem.attrib.nsmap) elem.clear() return metadata
def test_get_attribute(self): md = Metadata( attributes={'one': 1, 'two': 2, '{http://namespace.org}three': 4}, nsmap={'pre': 'http://namespace.org'} ) igt = Igt(metadata=[md], attributes={'three': 3}) self.assertEqual(md.get_attribute('one'), 1) self.assertEqual(md.get_attribute('two'), 2) self.assertIs(md.get_attribute('three'), None) self.assertEqual( md.get_attribute('three', namespace='http://namespace.org'), 4 ) self.assertEqual( md.get_attribute('three', namespace='pre'), 4 ) self.assertEqual(md.get_attribute('three', inherit=True), 3) self.assertEqual( md.get_attribute('three', namespace='pre', inherit=True), 4 ) self.assertEqual(md.get_attribute('three', default=5), 5)
def test_append(self): md = Metadata() with pytest.raises(XigtStructureError): md.append(Item()) with pytest.raises(XigtStructureError): md.append(Tier()) with pytest.raises(XigtStructureError): md.append(Igt()) with pytest.raises(XigtStructureError): md.append(XigtCorpus()) with pytest.raises(XigtStructureError): md.append(Metadata()) assert len(md) == 0 md.append(Meta(id='meta1')) assert len(md) == 1 with pytest.raises(XigtError): md.append(Meta(id='meta1')) md.append(Meta(id='meta2')) assert len(md) == 2 assert md[0].id == 'meta1' assert md[1].id == 'meta2'
class TestMetadata(unittest.TestCase): def setUp(self): self.md1 = Metadata() self.md2 = Metadata( id='md2', type='basic', attributes={'attr':'val'}, metas=[Meta(id='meta1', text='meta')] ) def test_init(self): self.assertRaises(ValueError, Metadata, id='1') # invalid id def test_id(self): self.assertIs(self.md1.id, None) self.assertIs(self.md2.id, 'md2') def test_type(self): self.assertIs(self.md1.type, None) self.assertEqual(self.md2.type, 'basic') def test_metas(self): self.assertEqual(self.md1.metas, []) self.assertEqual(len(self.md2.metas), 1) self.assertEqual(self.md2[0].text, 'meta') def test_attributes(self): self.assertEqual(self.md1.attributes, dict()) self.assertEqual(self.md2.attributes, {'attr':'val'}) def test_get(self): self.assertIs(self.md1.get(0), None) self.assertIs(self.md1.get('meta1'), None) self.assertEqual(self.md1.get('meta1', default=1), 1) self.assertEqual(self.md2.get(0).id, 'meta1') self.assertIs(self.md2.get(1), None) self.assertEqual(self.md2.get('meta1').id, 'meta1') self.assertEqual( self.md2.get('meta1', default=Meta(id='meta2')).id, 'meta1' ) def test_append(self): md = Metadata() self.assertRaises(XigtStructureError, md.append, Item()) self.assertRaises(XigtStructureError, md.append, Tier()) self.assertRaises(XigtStructureError, md.append, Igt()) self.assertRaises(XigtStructureError, md.append, XigtCorpus()) self.assertRaises(XigtStructureError, md.append, Metadata()) self.assertEqual(len(md), 0) md.append(Meta(id='meta1')) self.assertEqual(len(md), 1) self.assertRaises(XigtError, md.append, Meta(id='meta1')) md.append(Meta(id='meta2')) self.assertEqual(len(md), 2) self.assertEqual(md[0].id, 'meta1') self.assertEqual(md[1].id, 'meta2') def test_insert(self): md = Metadata() self.assertEqual(len(md), 0) md.insert(0, Meta(id='meta1')) self.assertEqual(len(md), 1) self.assertRaises(XigtError, md.insert, 0, Meta(id='meta1')) md.insert(0, Meta(id='meta2')) md.insert(100, Meta(id='meta3')) self.assertEqual(len(md), 3) self.assertEqual(md[0].id, 'meta2') self.assertEqual(md[1].id, 'meta1') self.assertEqual(md[2].id, 'meta3') def test_extend(self): md = Metadata() self.assertEqual(len(md), 0) md.extend([Meta(id='meta1')]) self.assertEqual(len(md), 1) md.extend([]) self.assertEqual(len(md), 1) md.extend([Meta(id='meta2'), Meta(id='meta3')]) self.assertEqual(len(md), 3) self.assertEqual(md[0].id, 'meta1') self.assertEqual(md[1].id, 'meta2') self.assertEqual(md[2].id, 'meta3') def test_clear(self): md = Metadata() md.extend([Meta(id='meta1'), Meta(id='meta2'), Meta(id='meta3')]) self.assertEqual(len(md), 3) md.clear() self.assertEqual(len(md), 0) self.assertIs(md.get(0), None) self.assertIs(md.get('meta1'), None) def test_get_attribute(self): md = Metadata( attributes={'one': 1, 'two': 2, '{http://namespace.org}three': 4}, nsmap={'pre': 'http://namespace.org'} ) igt = Igt(metadata=[md], attributes={'three': 3}) self.assertEqual(md.get_attribute('one'), 1) self.assertEqual(md.get_attribute('two'), 2) self.assertIs(md.get_attribute('three'), None) self.assertEqual( md.get_attribute('three', namespace='http://namespace.org'), 4 ) self.assertEqual( md.get_attribute('three', namespace='pre'), 4 ) self.assertEqual(md.get_attribute('three', inherit=True), 3) self.assertEqual( md.get_attribute('three', namespace='pre', inherit=True), 4 ) self.assertEqual(md.get_attribute('three', default=5), 5)