import unittest try: # python2 # Must try this first as io also exists in python2 # but in the wrong one! import StringIO as io except ImportError: # python3 import io import re from resync.resource import Resource from resync.change_dump import ChangeDump from resync.sitemap import SitemapParseError class TestChangeDump(unittest.TestCase): def test01_as_xml(self): rd = ChangeDump() rd.add(Resource('a.zip', timestamp=1)) rd.add(Resource('b.zip', timestamp=2)) xml = rd.as_xml() # print(xml) self.assertTrue( re.search(r'a.zip1970-01-01T00:00:01Z', xml), 'XML has resource a') def test10_parse(self): xml = '\n\ \ \ http://example.com/a.zip2012-03-14T18:37:36Z\ http://example.com/b.zip2012-03-14T18:37:36Z\ ' rd = ChangeDump() rd.parse(fh=io.StringIO(xml)) self.assertEqual(len(rd.resources), 2, 'got 2 resource dumps') self.assertEqual(rd.md['capability'], 'changedump', 'capability set') self.assertEqual(rd.md_from, '2013-01-01') self.assertTrue('http://example.com/a.zip' in rd.resources) self.assertTrue(rd.resources['http://example.com/a.zip'].length, 12345) self.assertTrue('http://example.com/b.zip' in rd.resources) self.assertTrue(rd.resources['http://example.com/b.zip'].length, 56789) def test11_parse_no_capability(self): # For a resource dump this should be an error xml = '\n\ \ \ http://example.com/a.zip2012-03-14T18:37:36Z\ ' rd = ChangeDump() self.assertRaises(SitemapParseError, rd.parse, fh=io.StringIO(xml)) def test12_parse_bad_capability(self): # the \n\ \ \ http://example.com/bad_res_12012-03-14T18:37:36Z\ ' rd = ChangeDump() self.assertRaises(SitemapParseError, rd.parse, fh=io.StringIO(xml))