def test_metadata_unicode(self): meta = Metadata('<test>') meta.add('alpha', 'is \u03b1') val = self.dump_to_string(meta) self.assertEqual(val, 'alpha: is \u03b1\n') source = """# Metadata test file alpha: is α """ fl = io.StringIO(source) meta = Metadata('<unit test>', fl) fl.close() self.assertEqual(meta.get_one('alpha'), 'is \u03b1')
def test_metadata_dump(self): meta = Metadata('<test>') val = self.dump_to_string(meta) self.assertEqual(val, '') val = self.dump_to_string(meta, 'I am a comment.') self.assertEqual(val, '# I am a comment.\n\n') val = self.dump_to_string(meta, ['one', 'two']) self.assertEqual(val, '# one\n# two\n\n') meta.add('one', 'two') val = self.dump_to_string(meta) self.assertEqual(val, 'one: two\n') meta.add('one', 'three') val = self.dump_to_string(meta) self.assertEqual(val, 'one: two\none: three\n')
def test_metadata_modify(self): meta = Metadata('<test>') meta.add('one', 'two') meta2 = meta.clone() meta2.add('one', 'four') self.assertEqual(meta2.get_all('one'), ['two', 'four']) meta.add('one', 'three') self.assertEqual(meta.get_all('one'), ['two', 'three']) self.assertEqual(meta2.get_all('one'), ['two', 'four']) meta.delete_all('one') meta.delete_all('nonexistent') self.assertEqual(meta.get_all('one'), []) self.assertEqual(meta2.get_all('one'), ['two', 'four'])
def test_metadata_empty(self): meta = Metadata('<test>') self.assertEqual(len(meta), 0) self.assertEqual(meta.get_all('foo'), []) self.assertEqual(list(meta.keys()), [])
def test_metadata(self): source = """# Metadata test file one: 1 two: 2 dc.three: 3 one: 11 four : Four """ fl = io.StringIO(source) meta = Metadata('<unit test>', fl) fl.close() self.assertEqual(meta.get_one('one'), '1') self.assertEqual(meta.get_one('dc.three'), '3') self.assertEqual(meta.get_one('four'), 'Four') self.assertEqual(meta.get_one('four', 'def'), 'Four') self.assertEqual(meta.get_one('missing'), None) self.assertEqual(meta.get_one('missing', 'def'), 'def') self.assertEqual(meta.get_all('two'), ['2']) self.assertEqual(meta.get_all('one'), ['1', '11']) self.assertEqual(meta.get_all('missing'), []) ls = list(meta.keys()) self.assertEqual(len(ls), len(meta)) ls.sort() self.assertEqual(ls, ['dc.three', 'four', 'one', 'two']) meta2 = meta.clone() self.assertEqual(len(meta2), len(meta)) for key in list(meta.keys()): self.assertEqual(meta.get_all(key), meta2.get_all(key))