class TemplateTagsTestCase(TestCase): def setUp(self): self.m1 = Machine(url='http://dspace.wrlc.org', name='DSpace Server') self.m1.save() self.m2 = Machine(url='dspace.wrlc.org', name='Backup DSpace Server') self.m2.save() self.collection = Collection(id='testcoll', name='TestCollection') self.collection.save() self.project = Project(id='testproj', name='TestProject', collection=self.collection) self.project.save() self.item = Item(id='testItem', title='TestItem', collection=self.collection, project=self.project) self.item.save() self.bag1 = Bag(bagname='testBag1', item=self.item, machine=self.m1, bag_type='1', absolute_filesystem_path='/tmp/fakebag') self.bag1.payload = """/data/METADATA/0123456789-dc.xml 2655 /data/METADATA/0123456789-MRC.xml 3256 /data/IMAGES/0123456789_pg1.jp2 1778740 /data/IMAGES/0123456789_pg2.jp2 1878756 /data/IMAGES/0123456789_pg3.jp2 1915879 /data/IMAGES/0123456789_pg1.tiff 1778740 /data/IMAGES/0123456789_pg2.tiff 1878756 /data/IMAGES/0123456789_pg3.tiff 1915879 """ self.bag1.save() self.bag2 = Bag(bagname='testBag2', item=self.item, machine=self.m2, bag_type='1', absolute_filesystem_path='/tmp/fakebag') self.bag2.payload = """/data/METADATA/0123456789-dc.xml 2655 /data/METADATA/0123456789-MRC.xml 3256 /data/IMAGES/0123456789_pg1.jp2 1778740 /data/IMAGES/0123456789_pg2.jp2 1878756 /data/IMAGES/0123456789_pg3.jp2 1915879 /data/IMAGES/0123456789_pg1.tiff 1778740 /data/IMAGES/0123456789_pg2.tiff 1878756 /data/IMAGES/0123456789_pg3.tiff 1915879 """ self.bag2.save() def test_urlize_with_label_templatetag(self): bag1_files = self.bag1.list_payload() bag1_url = invapp_extras.urlize_with_label( urlize(self.bag1.access_url() + bag1_files[0][0]), bag1_files[0][0]) self.assertEqual(bag1_url, '<a href="http://dspace.wrlc.org/tmp/' 'fakebag/data/METADATA/0123456789-dc.xml">' '/data/METADATA/0123456789-dc.xml</a>') bag2_files = self.bag2.list_payload() bag2_url = invapp_extras.urlize_with_label( urlize(self.bag2.access_url() + bag2_files[0][0]), bag2_files[0][0]) self.assertEqual(bag2_url, '/data/METADATA/0123456789-dc.xml')
class ModelTestCase(TestCase): def setUp(self): # set up hierarchy of fake objects for bag self.c1 = Collection(id='collection_1', name='test-collection-1', created=now()) self.c1.save() self.p1 = Project(id='project_1', name='test-project-1', collection=self.c1, created=now()) self.p1.save() self.i1 = Item(id='item_1', title='test-item-1', project=self.p1, created=now(), original_item_type='1') self.i1.save() self.m1 = Machine(name='test-machine-1', url='http://test.url.com', www_root='/bags/') self.m1.save() # load bag with raw data self.bag = Bag(bagname='test-bag-11', created=now(), item=self.i1, machine=self.m1, absolute_filesystem_path='/bags/partition1/test-bag-11', bag_type='1') self.bag.stats = self.bag.collect_stats() self.bag.save() def test_payload(self): self.assertEqual(self.bag.stats['total_size'], 0) self.assertEqual(self.bag.stats['total_count'], 0) self.assertEqual(len(self.bag.stats['types'].keys()), 0) # now add payload data self.bag.payload = """/data/METADATA/0123456789-dc.xml 2655 /data/METADATA/0123456789-MRC.xml 3256 /data/IMAGES/0123456789_pg1.jp2 1778740 /data/IMAGES/0123456789_pg2.jp2 1878756 /data/IMAGES/0123456789_pg3.jp2 1915879 /data/IMAGES/0123456789_pg1.tiff 1778740 /data/IMAGES/0123456789_pg2.tiff 1878756 /data/IMAGES/0123456789_pg3.tiff 1915879 """ # compare output of parsing method with expected result expect = { 'total_count': 8, 'total_size': 11152661, 'types': { 'xml': {'count': 2, 'size': 5911}, 'jp2': {'count': 3, 'size': 5573375}, 'tiff': {'count': 3, 'size': 5573375} } } self.bag.stats = self.bag.collect_stats() self.bag.save() self.assertEqual(expect['total_size'], self.bag.stats['total_size']) self.assertEqual(expect['total_count'], self.bag.stats['total_count']) for t in expect['types'].keys(): self.assertEqual(expect['types'][t]['count'], self.bag.stats['types'][t]['count']) self.assertEqual(expect['types'][t]['size'], self.bag.stats['types'][t]['size']) def test_bag_access_path(self): self.assertEqual(self.bag.access_url(), 'http://test.url.com/partition1/test-bag-11') self.m1.www_root = '/bags' self.assertEqual(self.bag.access_url(), 'http://test.url.com/partition1/test-bag-11') self.m1.www_root = 'bags' self.assertEqual(self.bag.access_url(), 'http://test.url.com/partition1/test-bag-11') @skipIf(not settings.TEST_IDSERVICE.get('url') or not settings.TEST_IDSERVICE.get('requester') or not settings.TEST_IDSERVICE.get('minter'), 'Test IDService not set') def test_auto_id_creation(self): # test collection c1 = Collection(name='Test Collection autoID') c1.save() self.assertTrue(c1.id) self.assertTrue(c1.created) # test project p1 = Project(name='Test Project autoID', collection=c1) p1.save() self.assertTrue(p1.id) self.assertTrue(p1.created) # test item i1 = Item(title='Test Item autoID', collection=c1, project=p1, original_item_type='1') i1.save() self.assertTrue(i1.id) self.assertTrue(i1.created) # test bag + machine m1 = Machine(name='test machine zzz', url='zzz.gwu.edu') m1.save() b1 = Bag(created=now(), item=i1, machine=m1, absolute_filesystem_path='blah/blah', bag_type='1') b1.save() # test copy numbers for duplicate bags b2 = Bag(created=now(), item=i1, machine=m1, absolute_filesystem_path='blah/blah', bag_type='1') b2.save() self.assertNotEqual(b1.bagname, b2.bagname) self.assertTrue(b1.bagname.endswith('1')) self.assertTrue(b2.bagname.endswith('2')) parts1 = b1.bagname.split('_') parts2 = b2.bagname.split('_') for x, val in enumerate(parts1): if x != len(parts1) - 1: self.assertEqual(val, parts2[x]) else: self.assertTrue(int(parts2[x]) - int(val) == 1) # make sure bagname does not contain forward slash i1.id = '12345/123456789' i1.save() b3 = Bag(created=now(), item=i1, machine=m1, absolute_filesystem_path='xxxx', bag_type='1') b3.save() self.assertFalse('/' in b3.bagname)