def test_instantiation(self): PKG_INFO = os.path.join(os.path.dirname(__file__), 'PKG-INFO') with open(PKG_INFO, 'r', encoding='utf-8') as f: contents = f.read() fp = StringIO(contents) m = Metadata() self.assertRaises(MetadataUnrecognizedVersionError, m.items) m = Metadata(PKG_INFO) self.assertEqual(len(m.items()), 22) m = Metadata(fileobj=fp) self.assertEqual(len(m.items()), 22) m = Metadata(mapping=dict(name='Test', version='1.0')) self.assertEqual(len(m.items()), 11) d = dict(m.items()) self.assertRaises(TypeError, Metadata, PKG_INFO, fileobj=fp) self.assertRaises(TypeError, Metadata, PKG_INFO, mapping=d) self.assertRaises(TypeError, Metadata, fileobj=fp, mapping=d) self.assertRaises(TypeError, Metadata, PKG_INFO, mapping=m, fileobj=fp)
def test_write_metadata(self): # check support of non-ASCII values tmp_dir = self.mkdtemp() my_file = os.path.join(tmp_dir, 'f') metadata = Metadata(mapping={'author': 'Mister Café', 'name': 'my.project', 'author': 'Café Junior', 'summary': 'Café torréfié', 'description': 'Héhéhé', 'keywords': ['café', 'coffee']}) metadata.write(my_file) # the file should use UTF-8 metadata2 = Metadata() with open(my_file, encoding='utf-8') as fp: metadata2.read_file(fp) # XXX when keywords are not defined, metadata will have # 'Keywords': [] but metadata2 will have 'Keywords': [''] # because of a value.split(',') in Metadata.get self.assertEqual(metadata.items(), metadata2.items()) # ASCII also works, it's a subset of UTF-8 metadata = Metadata(mapping={'author': 'Mister Cafe', 'name': 'my.project', 'author': 'Cafe Junior', 'summary': 'Cafe torrefie', 'description': 'Hehehe'}) metadata.write(my_file) metadata2 = Metadata() with open(my_file, encoding='utf-8') as fp: metadata2.read_file(fp)
def test_mapping_api(self): PKG_INFO = os.path.join(os.path.dirname(__file__), 'PKG-INFO') with open(PKG_INFO, 'r', encoding='utf-8') as f: content = f.read() % sys.platform metadata = Metadata(fileobj=StringIO(content)) self.assertIn('Version', metadata.keys()) self.assertIn('0.5', metadata.values()) self.assertIn(('Version', '0.5'), metadata.items()) metadata.update({'version': '0.6'}) self.assertEqual(metadata['Version'], '0.6') metadata.update([('version', '0.7')]) self.assertEqual(metadata['Version'], '0.7') # make sure update method checks values like the set method does metadata.update({'version': '1--2'}) self.assertEqual(len(self.get_logs()), 1) # XXX caveat: the keys method and friends are not 3.x-style views # should be changed or documented self.assertEqual(list(metadata), metadata.keys())