import unittest import re from resync.resource import Resource from resync.source_description import SourceDescription class TestSourceDescription(unittest.TestCase): def test01_empty(self): rsd = SourceDescription() rsd.describedby = "http://example.org/about" self.assertEqual(len(rsd), 0) rsd.md_at = None self.assertEqual(rsd.as_xml(), '\n') def test02_add(self): rsd = SourceDescription() self.assertEqual(len(rsd), 0) rsd.add(Resource("http://example.org/u1")) rsd.add([Resource("http://example.org/u2"), Resource("http://example.org/u3")]) self.assertEqual(rsd.uris(), ['http://example.org/u1', 'http://example.org/u2', 'http://example.org/u3']) def test03_one_caplist(self): rsd = SourceDescription() rsd.describedby = "http://example.org/about" self.assertEqual(len(rsd), 0) rsd.md_at = None rsd.add_capability_list("http://example.org/ds1/cl.xml") self.assertEqual(len(rsd), 1) self.assertEqual(rsd.as_xml(), '\nhttp://example.org/ds1/cl.xml') def test04_a_bunch(self): rsd = SourceDescription() rsd.describedby = "http://example.org/about" self.assertEqual(len(rsd), 0) rsd.add_capability_list("http://example.org/ds1/cl.xml") rsd.add_capability_list("http://example.org/ds2/cl.xml") rsd.add_capability_list("http://example.org/ds3/cl.xml") self.assertEqual(len(rsd), 3) self.assertEqual(rsd.as_xml(), '\nhttp://example.org/ds1/cl.xmlhttp://example.org/ds2/cl.xmlhttp://example.org/ds3/cl.xml') def test05_parse(self): xml = '\nhttp://example.org/ds1/cl.xmlhttp://example.org/ds2/cl.xmlhttp://example.org/ds3/cl.xml' sd = SourceDescription() sd.parse(str_data=xml) self.assertEqual(sd.link_href('describedby'), 'http://example.org/about', 'describedby link') self.assertEqual(sd.capability, 'description') self.assertEqual(len(sd.resources), 3, 'got 3 capacility lists') [r1, r2, r3] = sd.resources self.assertEqual(r1.uri, 'http://example.org/ds1/cl.xml') self.assertEqual(r1.capability, 'capabilitylist')