From b264ae171d282ea421ce417b9aaea754201a65b2 Mon Sep 17 00:00:00 2001 From: Simeon Warner Date: Tue, 5 Apr 2016 14:55:02 -0400 Subject: [PATCH] More tests --- resync/capability_list.py | 2 ++ tests/test_capability_list.py | 26 ++++++++++++-- tests/test_change_list.py | 55 ++++++++++++++++------------- tests/test_list_base.py | 14 +++++++- tests/testdata/unknown/unknown1.xml | 6 ++++ 5 files changed, 75 insertions(+), 28 deletions(-) create mode 100644 tests/testdata/unknown/unknown1.xml diff --git a/resync/capability_list.py b/resync/capability_list.py index e598821..1630522 100644 --- a/resync/capability_list.py +++ b/resync/capability_list.py @@ -85,6 +85,8 @@ class CapabilityList(ListBase): Will throw a ValueError if the resource (ie. same uri) already exists in the capability_list, unless replace=True. + + See add_capability() for normal method of adding capabilities. """ if isinstance(resource, collections.Iterable): for r in resource: diff --git a/tests/test_capability_list.py b/tests/test_capability_list.py index b821683..d8bb1fd 100644 --- a/tests/test_capability_list.py +++ b/tests/test_capability_list.py @@ -2,12 +2,32 @@ import unittest import re from resync.resource import Resource from resync.resource_list import ResourceList +from resync.resource_set import ResourceSetDupeError from resync.change_list import ChangeList from resync.capability_list import CapabilityList class TestCapabilityList(unittest.TestCase): - def test01_resourcelist(self): + def test01_add(self): + # one + caps = CapabilityList() + r1 = Resource( uri='http://example.org/r1' ) + caps.add( r1 ) + self.assertEqual( len(caps), 1 ) + # dupe + self.assertRaises( ResourceSetDupeError, caps.add, r1 ) + self.assertEqual( len(caps), 1 ) + # dupe with replace + caps = CapabilityList() + caps.add( [r1,r1], replace=True ) + self.assertEqual( len(caps), 1 ) + # diff + caps = CapabilityList() + r2 = ChangeList( uri='http://example.org/r2' ) + caps.add( [r1,r2] ) + self.assertEqual( len(caps), 2 ) + + def test02_resourcelist(self): rl = ResourceList() caps = CapabilityList() caps.add_capability( rl, "http://example.org/resourcelist.xml" ) @@ -15,7 +35,7 @@ class TestCapabilityList(unittest.TestCase): self.assertEqual( len(caps), 1 ) self.assertEqual( caps.as_xml(), '\nhttp://example.org/resourcelist.xml' ) - def test02_multiple(self): + def test03_multiple(self): caps = CapabilityList() rl = ResourceList() caps.add_capability( rl, "rl.xml" ) @@ -26,7 +46,7 @@ class TestCapabilityList(unittest.TestCase): self.assertTrue( re.search( r'rl.xml', xml ) ) self.assertTrue( re.search( r'cl.xml', xml) ) - def test03_parse(self): + def test04_parse(self): xml='\nhttp://example.org/resourcelist.xml' cl=CapabilityList() cl.parse(str_data=xml) diff --git a/tests/test_change_list.py b/tests/test_change_list.py index 2c23d11..fcef6a1 100644 --- a/tests/test_change_list.py +++ b/tests/test_change_list.py @@ -8,22 +8,29 @@ except ImportError: #python3 import re from resync.resource import Resource -from resync.change_list import ChangeList +from resync.change_list import ChangeList, ChangeTypeError from resync.resource_list import ResourceList from resync.sitemap import SitemapParseError class TestChangeList(unittest.TestCase): - def test1_set_with_repeats(self): - src = ChangeList() - src.add( Resource('a',timestamp=1,change='updated') ) - src.add( Resource('b',timestamp=1,change='created') ) - src.add( Resource('c',timestamp=1,change='deleted') ) - src.add( Resource('a',timestamp=2,change='deleted') ) - src.add( Resource('b',timestamp=2,change='updated') ) - self.assertEqual(len(src), 5, "5 changes in change_list") + def test01_add_if_changed(self): + cl = ChangeList() + cl.add_if_changed( Resource('a',timestamp=1,change='updated') ) + self.assertEqual( len(cl), 1 ) + self.assertRaises( ChangeTypeError, cl.add_if_changed, + Resource('c',timestamp=3) ) - def test2_with_repeats_again(self): + def test02_set_with_repeats(self): + cl = ChangeList() + cl.add( Resource('a',timestamp=1,change='updated') ) + cl.add( Resource('b',timestamp=1,change='created') ) + cl.add( Resource('c',timestamp=1,change='deleted') ) + cl.add( Resource('a',timestamp=2,change='deleted') ) + cl.add( Resource('b',timestamp=2,change='updated') ) + self.assertEqual(len(cl), 5, "5 changes in change_list") + + def test03_with_repeats_again(self): r1 = Resource(uri='a',length=1,change='created') r2 = Resource(uri='b',length=2,change='created') i = ChangeList() @@ -35,16 +42,16 @@ class TestChangeList(unittest.TestCase): i.add(r1d) self.assertEqual( len(i), 3 ) - def test3_change_list(self): - src = ChangeList() - src.add( Resource('a',timestamp=1,change='created') ) - src.add( Resource('b',timestamp=2,change='created') ) - src.add( Resource('c',timestamp=3,change='created') ) - src.add( Resource('d',timestamp=4,change='created') ) - src.add( Resource('e',timestamp=5,change='created') ) - self.assertEqual(len(src), 5, "5 things in src") + def test04_change_list(self): + cl = ChangeList() + cl.add( Resource('a',timestamp=1,change='created') ) + cl.add( Resource('b',timestamp=2,change='created') ) + cl.add( Resource('c',timestamp=3,change='created') ) + cl.add( Resource('d',timestamp=4,change='created') ) + cl.add( Resource('e',timestamp=5,change='created') ) + self.assertEqual(len(cl), 5, "5 things in src") - def test4_iter(self): + def test05_iter(self): i = ChangeList() i.add( Resource('a',timestamp=1,change='created') ) i.add( Resource('b',timestamp=2,change='created') ) @@ -57,7 +64,7 @@ class TestChangeList(unittest.TestCase): self.assertEqual( resources[0].uri, 'a') self.assertEqual( resources[3].uri, 'd') - def test5_add_changed_resources(self): + def test06_add_changed_resources(self): added = ResourceList() added.add( Resource('a',timestamp=1,change='created') ) added.add( Resource('d',timestamp=4,change='created') ) @@ -91,7 +98,7 @@ class TestChangeList(unittest.TestCase): self.assertEqual(dst.resources['d'].timestamp, 4) self.assertEqual(dst.resources['d'].change, 'created') - def test20_as_xml(self): + def test07_as_xml(self): cl = ChangeList() cl.md_from = '1970-01-01T00:00:00Z' cl.add( Resource('a',timestamp=1,change='updated') ) @@ -101,7 +108,7 @@ class TestChangeList(unittest.TestCase): self.assertTrue( re.search(r'a1970-01-01T00:00:01Z', xml), 'XML has resource a' ) - def test30_parse(self): + def test08_parse(self): xml='\n\ \ \ @@ -114,7 +121,7 @@ class TestChangeList(unittest.TestCase): self.assertEqual( cl.md['capability'], 'changelist', 'capability set' ) self.assertEqual( cl.md['md_from'], '2013-01-01' ) - def test31_parse_no_capability(self): + def test09_parse_no_capability(self): # missing capability is an error for changelist xml='\n\ \ @@ -123,7 +130,7 @@ class TestChangeList(unittest.TestCase): cl=ChangeList() self.assertRaises( SitemapParseError, cl.parse, fh=io.StringIO(xml) ) - def test32_parse_bad_capability(self): + def test10_parse_bad_capability(self): # the \n\ \ diff --git a/tests/test_list_base.py b/tests/test_list_base.py index 7eea9a4..acc3e8f 100644 --- a/tests/test_list_base.py +++ b/tests/test_list_base.py @@ -43,7 +43,13 @@ class TestListBase(TestCase): x = lb.as_xml() self.assertEqual( x, '\na2001-01-01T00:00:00Zb2002-02-02T00:00:00Zc2003-03-03T00:00:00Z' ) - def test03_parse(self): + def test03_read(self): + # just does parse + lb=ListBase() + lb.read(uri='tests/testdata/unknown/unknown1.xml') + self.assertEqual( len(lb.resources), 2 ) + + def test04_parse(self): xml = '\n\ \ \ @@ -62,6 +68,12 @@ class TestListBase(TestCase): lb3=ListBase() lb3.parse(str=xml) self.assertEqual( len(lb3.resources), 2, 'got 2 resources') + # file that doesn't exits + lb4 = ListBase() + self.assertRaises( Exception, lb4.parse, + uri='tests/testdata/does_not_exist' ) + # nothing + self.assertRaises( Exception, lb4.parse ) def test04_write(self): lb = ListBase(capability_name = 'special') diff --git a/tests/testdata/unknown/unknown1.xml b/tests/testdata/unknown/unknown1.xml new file mode 100644 index 0000000..8a9b3f8 --- /dev/null +++ b/tests/testdata/unknown/unknown1.xml @@ -0,0 +1,6 @@ + + + +r12012-03-14T18:37:36Z +r22012-03-14T18:37:36Z + \ No newline at end of file