def test_Metadata_create_element_dict(): """Test a UNTL Python object converts to dictionary.""" metadata = us.Metadata() title_text = u'An Official Title' series_text = u'The Series Title' name_text = u'Oudrid, C. (Cristóbal), 1825-1877.' title = us.Title(content=title_text, qualifier=u'officialtitle') series_title = us.Title(content=series_text, qualifier=u'seriestitle') contributor = us.Contributor(qualifier=u'cmp') type_ = us.Type(content=u'per') name = us.Name(content=name_text) contributor.add_child(type_) contributor.add_child(name) metadata.add_child(title) metadata.add_child(series_title) metadata.add_child(contributor) untl_dict = metadata.create_element_dict() assert untl_dict == { 'contributor': [ {'content': {'name': name_text, 'type': u'per'}, 'qualifier': u'cmp'} ], 'title': [ {'content': title_text, 'qualifier': u'officialtitle'}, {'content': series_text, 'qualifier': u'seriestitle'} ] }
def test_FormGenerator(_): """Test same UNTLElement subclasses are grouped together. Official and series titles should be in same group. A hidden group should be created if it doesn't exist. """ creator = us.Creator(qualifier='') official_title = us.Title(content='A Title', qualifier='officialtitle') series_title = us.Title(content='Series', qualifier='seriestitle') children = [official_title, series_title, creator] form_elements = us.FormGenerator(children=children, sort_order=['title', 'creator', 'hidden']) # Check there is a title group with both official and series elements # a group for the creator, and a separate hidden group was created. assert len(form_elements.element_groups) == 3 title_group = form_elements.element_groups[0].group_list assert len(title_group) == 2 for title in title_group: assert isinstance(title, us.Title) creator_group = form_elements.element_groups[1].group_list assert len(creator_group) == 1 assert isinstance(creator_group[0], us.Creator) hidden_group = form_elements.element_groups[2].group_list assert len(hidden_group) == 1 assert isinstance(hidden_group[0], us.Meta)
def test_Metadata_create_xml_file_ascii_hex(tmpdir): """Test the UNTL XML file is created correctly. Uses pytest tmdir fixture for temporary file. """ metadata = us.Metadata() title = us.Title(content=u'Colecci\xf3n', qualifier=u'seriestitle') description = us.Description(content=u'"Fortuna te d\xe9 Dios, hijo"', qualifier=u'content') contributor = us.Contributor(qualifier=u'cmp') type_ = us.Type(content=u'per') name = us.Name(content=u'Oudrid, C. (Crist\xf3bal), 1825-1877.') contributor.add_child(type_) contributor.add_child(name) metadata.add_child(title) metadata.add_child(description) metadata.add_child(contributor) xml_file = tmpdir.join('untl.xml') metadata.create_xml_file(xml_file.strpath) assert xml_file.read() == ( '<?xml version="1.0" encoding="UTF-8"?>\n' '<metadata>\n' ' <title qualifier="seriestitle">Colección</title>\n' ' <contributor qualifier="cmp">\n' ' <type>per</type>\n' ' <name>Oudrid, C. (Cristóbal), 1825-1877.</name>\n' ' </contributor>\n' ' <description qualifier="content">"Fortuna te dé Dios, hijo"</description>\n' '</metadata>\n' )
def test_Metadata_create_xml(): """Test the metadata ElementTree representation.""" metadata = us.Metadata() title_text = u'Colección' description_text = u'Adaption of "Fortuna te dé Dios, hijo"' name_text = u'Oudrid, C. (Cristóbal), 1825-1877.' title = us.Title(content=title_text, qualifier=u'seriestitle') description = us.Description(content=description_text, qualifier=u'content') contributor = us.Contributor(qualifier=u'cmp') type_ = us.Type(content=u'per') name = us.Name(content=name_text) contributor.add_child(type_) contributor.add_child(name) metadata.add_child(title) metadata.add_child(description) metadata.add_child(contributor) root = metadata.create_xml(useNamespace=False) assert root.tag == 'metadata' # Check are children are there in sorted order. assert root[0].tag == u'title' assert root[0].text == title_text assert root[0].get('qualifier') == u'seriestitle' assert root[1].tag == u'contributor' assert root[1].get('qualifier') == u'cmp' assert root[2].tag == u'description' assert root[2].text == description_text assert root[2].get('qualifier') == u'content' # Check that the contributor children are there. assert root[1][0].tag == 'type' assert root[1][1].tag == 'name' assert root[1][1].text == name_text
def test_untlpy2dict(): title = us.Title(qualifier='serialtitle', content='The Bronco') elements = us.Metadata() elements.add_child(title) untl_dict = untldoc.untlpy2dict(elements) assert untl_dict == { 'title': [{ 'qualifier': 'serialtitle', 'content': 'The Bronco' }] }
def test_untl_to_hash_dict(meaningful_meta, expected_hash_dict): title = us.Title(qualifier='serialtitle', content='The Bronco') meta_modifier = us.Meta(qualifier='metadataModifier', content='Daniel') meta_modification = us.Meta(qualifier='metadataModificationDate', content='2007-09-20, 13:46:15') meta_object = us.Meta(qualifier='objectType', content='simple') elements = us.Metadata() elements.add_child(title) elements.add_child(meta_modification) elements.add_child(meta_modifier) elements.add_child(meta_object) hash_dict = untldoc.untl_to_hash_dict(elements, meaningful_meta) assert hash_dict == expected_hash_dict
def test_Metadata_create_xml_string(): """Test our metadata xml is written as expected string.""" metadata = us.Metadata() title = us.Title(content=u'Colección', qualifier=u'seriestitle') description = us.Description(content=u'Adaption of "Fortuna te dé Dios, hijo"', qualifier=u'content') metadata.children = [title, description] expected_text = """<?xml version="1.0" encoding="UTF-8"?> <metadata> <title qualifier="seriestitle">Colección</title> <description qualifier="content">Adaption of "Fortuna te dé Dios, hijo"</description> </metadata>\n""" assert metadata.create_xml_string() == expected_text.encode()
def test_add_missing_children(): """Test adding expected children that don't exist for a form.""" required = ['title', 'format', 'publisher'] parent = us.UNTLElement() parent.contained_children = required title_child = us.Title(content='A title', qualifier='officialtitle') children = [title_child] parent.children = children padded_children = us.add_missing_children(required, children) # Now there are Format and Publisher elements after Title. assert len(padded_children) == 3 assert isinstance(padded_children[1], us.Format) assert isinstance(padded_children[2], us.Publisher)
def test_Metadata_create_xml_file(tmpdir): """Test the UNTL XML file is created correctly. Uses pytest tmdir fixture for temporary file. """ metadata = us.Metadata() title = us.Title(content=u'Colección', qualifier=u'seriestitle') description = us.Description(content=u'"Fortuna te dé Dios, hijo"', qualifier=u'content') contributor = us.Contributor(qualifier=u'cmp') type_ = us.Type(content=u'per') name = us.Name(content=u'Oudrid, C. (Cristóbal), 1825-1877.') contributor.add_child(type_) contributor.add_child(name) metadata.add_child(title) metadata.add_child(description) metadata.add_child(contributor) xml_file = tmpdir.join('untl.xml') metadata.create_xml_file(xml_file.strpath) assert xml_file.read() == """<?xml version="1.0" encoding="UTF-8"?>
def test_py2dict(): """Test UNTL Elements are converted to a dictionary.""" title = us.Title(qualifier='serialtitle', content='The Bronco') name = us.Name(content='Case, J.') creator = us.Creator(qualifier='aut') creator.add_child(name) elements = us.Metadata() elements.add_child(title) elements.add_child(creator) metadata_dict = mg.py2dict(elements) assert metadata_dict == { 'title': [{ 'qualifier': 'serialtitle', 'content': 'The Bronco' }], 'creator': [{ 'qualifier': 'aut', 'content': { 'name': 'Case, J.' } }] }
def test_generate_untl_json(input_indent, json_output): title = us.Title(qualifier='serialtitle', content='The Bronco') elements = us.Metadata() elements.add_child(title) untl_json = untldoc.generate_untl_json(elements, input_indent) assert untl_json == json_output