##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Test loading of Dublin Core metadata from the XML representation.
$Id: test_xmlmetadata.py 66902 2006-04-12 20:16:30Z philikon $
"""
import unittest
from zope.dublincore import dcterms
from zope.dublincore.xmlmetadata import dumpString, parseString
class XMLDublinCoreLoadingTests(unittest.TestCase):
# Note: We're not using the 'traditional' namespace prefixes in
# the tests since we want to make sure we're doing the right thing
# in the content handler. Also, we should use something we're not
# using in zope.dublincore.dcterms.
_prefix = ("\n"
"\n"
% (dcterms.DC_NS, dcterms.DCTERMS_NS, dcterms.XSI_NS))
_suffix = "\n"
def parse(self, text):
return parseString("%s%s%s" % (self._prefix, text, self._suffix))
def check1(self, text, name, value, generic=None):
expected = {name: (value,)}
m = self.parse(text)
self.assertEqual(m, expected)
m = self.parse("%s" % text)
self.assertEqual(m, expected)
if generic:
m = self.parse("<%s>%s%s>" % (generic, text, generic))
self.assertEqual(m, expected)
m = self.parse("<%s>%s%s>"
% (generic, text, generic))
self.assertEqual(m, expected)
# tests with acceptable input
def test_empty(self):
m = parseString("")
self.assertEqual(m, {})
# core elements and related refinements
def test_simple_title(self):
self.check1("Foo", "Title", u"Foo")
def test_two_titles(self):
m = self.parse("Foo"
"Bar")
self.assertEqual(m, {"Title": (u"Foo", u"Bar")})
def test_alternative_title(self):
m = self.parse("Foo"
"Bar")
self.assertEqual(m, {"Title": (u"Foo",),
"Title.Alternative": (u"Bar",)})
def test_creator(self):
self.check1("somebody",
"Creator", "somebody")
def test_subject(self):
self.check1("something",
"Subject", "something")
self.check1("something",
"Subject.LCSH", "something")
self.check1("something",
"Subject.MESH", "something")
self.check1("something",
"Subject.DDC", "something")
self.check1("something",
"Subject.LCC", "something")
self.check1("something",
"Subject.UDC", "something")
def test_description(self):
self.check1("foo",
"Description", "foo")
self.check1("foo",
"Description.Abstract", "foo", generic="d:description")
self.check1("foo",
"Description.Table Of Contents", "foo",
generic="d:description")
def test_publisher(self):
self.check1("pub",
"Publisher", "pub")
def test_contributor(self):
self.check1("somebody",
"Contributor", "somebody")
def test_date(self):
self.check1("2003-08-20",
"Date", "2003-08-20")
# refinements used by Zope
self.check1("2003-08-20",
"Date.Created", "2003-08-20", generic="d:date")
self.check1("2003-08-20",
"Date.Modified", "2003-08-20", generic="d:date")
# other refinements
self.check1("2003-08-20",
"Date.Accepted", "2003-08-20", generic="d:date")
self.check1("2003-08-20",
"Date.Available", "2003-08-20", generic="d:date")
self.check1("2003-08-20",
"Date.Copyrighted", "2003-08-20", generic="d:date")
self.check1("2003-08-20",
"Date.Issued", "2003-08-20", generic="d:date")
self.check1("2003-08-20",
"Date.Submitted", "2003-08-20", generic="d:date")
self.check1("2003-08-20",
"Date.Valid", "2003-08-20", generic="d:date")
def test_type(self):
self.check1("some type",
"Type", "some type")
self.check1("Collection",
"Type.DCMIType", "Collection")
def test_format(self):
self.check1("some format",
"Format", "some format")
self.check1("text/xml",
"Format.IMT", "text/xml")
self.check1("1 hour",
"Format.Extent", "1 hour", generic="d:format")
self.check1("70mm IMAX celluloid",
"Format.Medium", "70mm IMAX celluloid", generic="d:format")
def test_identifier(self):
self.check1("ident",
"Identifier", "ident")
self.check1(""
" citation "
"",
"Identifier.Bibliographic Citation", "citation",
generic="d:identifier")
def test_source(self):
self.check1("src",
"Source", "src")
self.check1("http://example.com/",
"Source.URI", "http://example.com/")
def test_language(self):
self.check1("Klingon",
"Language", "Klingon")
self.check1("abc",
"Language.ISO639-2", "abc")
self.check1("en",
"Language.RFC1766", "en")
self.check1("en-GB-oed",
"Language.RFC3066", "en-GB-oed")
def test_relation(self):
self.check1("rel",
"Relation", "rel")
self.check1("that",
"Relation.Is Version Of", "that", generic="d:relation")
self.check1("that",
"Relation.Has Version", "that", generic="d:relation")
self.check1("that",
"Relation.Is Replaced By", "that", generic="d:relation")
self.check1("that",
"Relation.Replaces", "that", generic="d:relation")
self.check1("that",
"Relation.Is Required By", "that", generic="d:relation")
self.check1("that",
"Relation.Requires", "that", generic="d:relation")
self.check1("that",
"Relation.Is Part Of", "that", generic="d:relation")
self.check1("that",
"Relation.Has Part", "that", generic="d:relation")
self.check1("that",
"Relation.Is Referenced By", "that", generic="d:relation")
self.check1("that",
"Relation.References", "that", generic="d:relation")
self.check1("that",
"Relation.Is Format Of", "that", generic="d:relation")
self.check1("that",
"Relation.Has Format", "that", generic="d:relation")
self.check1("that",
"Relation.Conforms To", "that", generic="d:relation")
def test_coverage(self):
self.check1("how much",
"Coverage", "how much")
self.check1("where",
"Coverage.Spatial", "where", generic="d:coverage")
self.check1("when",
"Coverage.Temporal", "when", generic="d:coverage")
self.check1(""
" name=Period Name; start=1812; end=2112; "
"",
"Coverage.Temporal.Period",
"name=Period Name; start=1812; end=2112;",
generic="d:coverage")
self.check1("2003-08-20",
"Coverage.Temporal.W3CDTF", "2003-08-20",
generic="d:coverage")
def test_rights(self):
self.check1("rights",
"Rights", "rights")
self.check1("rights",
"Rights.Access Rights", "rights", generic="d:rights")
# non-core elements
def test_audience(self):
# Audience is the only DCMI element not in the core
self.check1("people",
"Audience", "people")
self.check1("people",
"Audience.Education Level", "people", generic="t:audience")
self.check1("people",
"Audience.Mediator", "people", generic="t:audience")
def test_nested_refinement(self):
# direct nesting
self.check1((""
"Foo"
""),
"Title.Alternative", u"Foo")
# nesting with an intermediate element
self.check1((""
"Foo"
""),
"Title.Alternative", u"Foo")
# tests with errors in the input
def test_invalid_nested_refinement(self):
self.assertRaises(ValueError, self.parse,
(""
"Title"
""))
self.assertRaises(ValueError, self.parse,
(""
"Title"
""))
def test_invalid_type(self):
self.assertRaises(ValueError, self.parse,
"x")
def test_invalid_dcmitype(self):
self.assertRaises(ValueError, self.parse,
"flub")
class XMLDublinCoreSerializationTests(unittest.TestCase):
def roundtrip(self, mapping):
text = dumpString(mapping)
parsed = parseString(text)
self.assertEqual(parsed, mapping)
def test_serialize_empty(self):
self.roundtrip({})
def test_single_entry(self):
self.roundtrip({"Title.Alternative": (u"Foo",)})
def test_two_titles(self):
self.roundtrip({"Title": (u"Foo", u"Bar")})
def test_suite():
suite = unittest.makeSuite(XMLDublinCoreLoadingTests)
suite.addTest(unittest.makeSuite(XMLDublinCoreSerializationTests))
return suite
if __name__ == "__main__":
unittest.main(defaultTest="test_suite")