Add test for multiple top-level rs:md elements

This commit is contained in:
Simeon Warner 2014-01-20 11:51:33 -05:00
parent ab6c74527d
commit 35692e81e8
2 changed files with 22 additions and 10 deletions

View File

@ -158,7 +158,8 @@ class Sitemap(object):
# have what we expect, read it
in_preamble = True
self.resources_created=0
self.resources_created = 0
seen_top_level_md = False
for e in etree.getroot().getchildren():
# look for <rs:md> and <rs:ln>, first <url> ends
# then look for resources in <url> blocks
@ -172,18 +173,20 @@ class Sitemap(object):
self.resources_created+=1
elif (e.tag == "{"+RS_NS+"}md"):
if (in_preamble):
# FIXME - more then one <rs:md> should be error
resources.md = self.md_from_etree(e,'preamble')
if (seen_top_level_md):
raise SitemapParseError("Multiple <rs:md> at top level of sitemap")
else:
resources.md = self.md_from_etree(e,'preamble')
seen_top_level_md = True
else:
raise SitemapParseError("Found <rs:md> after first <url> in sitemap")
elif (e.tag == "{"+RS_NS+"}ln"):
if (in_preamble):
resources.ln.append(self.ln_from_etree(e,'preamble'))
else:
raise SitemapParseError("Found <rs:md> after first <url> in sitemap")
raise SitemapParseError("Found <rs:ln> after first <url> in sitemap")
else:
# element we don't recognize, ignore
# FIXME - might add check for debug?
pass
# check that we read to right capability document
if (capability is not None):

View File

@ -132,22 +132,31 @@ class TestSitemap(unittest.TestCase):
self.assertEqual( s.resources_created, 1 )
self.assertEqual( i.resources[0].lastmod, None )
def test_14_parse_illformed(self):
def test_14_parse_multi(self):
xml_start='<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n\
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:rs="http://www.openarchives.org/rs/terms/">'
xml_end='<url><loc>uri:a</loc></url></urlset>'
s=Sitemap()
two_md='<rs:md capability="resourcelist"/><rs:md capability="resourcelist"/>'
self.assertRaises( SitemapParseError, s.parse_xml,
StringIO.StringIO(xml_start+two_md+xml_end))
def test_15_parse_illformed(self):
s=Sitemap()
# ExpatError in python2.6, ParserError in 2.7
self.assertRaises( etree_error_class, s.parse_xml, StringIO.StringIO('not xml') )
self.assertRaises( etree_error_class, s.parse_xml, StringIO.StringIO('<urlset><url>something</urlset>') )
def test_15_parse_valid_xml_but_other(self):
def test_16_parse_valid_xml_but_other(self):
s=Sitemap()
self.assertRaises( SitemapParseError, s.parse_xml, StringIO.StringIO('<urlset xmlns="http://example.org/other_namespace"> </urlset>') )
self.assertRaises( SitemapParseError, s.parse_xml, StringIO.StringIO('<other xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> </other>') )
def test_16_parse_sitemapindex_as_sitemap(self):
def test_17_parse_sitemapindex_as_sitemap(self):
s=Sitemap()
self.assertRaises( SitemapIndexError, s.parse_xml, StringIO.StringIO('<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> </sitemapindex>'), sitemapindex=False )
def test_17_parse_with_rs_ln_on_resource(self):
def test_18_parse_with_rs_ln_on_resource(self):
xml='<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n\
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:rs="http://www.openarchives.org/rs/terms/">\
<rs:md capability=\"resourcelist\"/>\
@ -177,7 +186,7 @@ class TestSitemap(unittest.TestCase):
self.assertEqual( r1.ln[0]['pri'], 1 )
self.assertEqual( r2.uri, 'http://example.com/file_b' )
def test_18_parse_with_bad_rs_ln(self):
def test_19_parse_with_bad_rs_ln(self):
xmlstart='<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n\
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:rs="http://www.openarchives.org/rs/terms/">\
<rs:md capability="resourcelist"/>\