Esempio n. 1
0
 def test11_update(self):
     """Test update method."""
     tempdir = tempfile.mkdtemp(prefix='test_update')
     oo = Object()
     # First create and object
     oo.id = 'uri:wumpus'
     objdir = os.path.join(tempdir, '1')
     oo.digest_algorithm = 'sha256'
     oo.create(srcdir='fixtures/1.0/content/spec-ex-minimal/v1',
               metadata=VersionMetadata(),
               objdir=objdir)
     self.assertEqual(
         set(os.listdir(objdir)),
         set([
             '0=ocfl_object_1.0', 'inventory.json', 'inventory.json.sha256',
             'v1'
         ]))
     # Now update
     oo.digest_algorithm = 'sha512'
     oo.update(objdir=objdir, metadata=VersionMetadata())
     self.assertEqual(
         set(os.listdir(objdir)),
         set([
             '0=ocfl_object_1.0', 'inventory.json', 'inventory.json.sha512',
             'v1', 'v2'
         ]))
Esempio n. 2
0
 def test09_build(self):
     """Test write method."""
     tempdir = tempfile.mkdtemp(prefix='test_write')
     oo = Object()
     self.assertRaises(ObjectException,
                       oo.build,
                       srcdir='fixtures/1.0/content/spec-ex-full')
     oo.id = 'uri:firkin'
     objdir = os.path.join(tempdir, '1')
     oo.build(srcdir='fixtures/1.0/content/spec-ex-full',
              metadata=VersionMetadata(),
              objdir=objdir)
     self.assertEqual(
         set(os.listdir(objdir)),
         set([
             '0=ocfl_object_1.0', 'inventory.json', 'inventory.json.sha512',
             'v1', 'v2', 'v3'
         ]))
     # If objdir is None the output is just a log saying what would have been written
     log_io = io.StringIO()
     oo.log.addHandler(logging.StreamHandler(log_io))
     oo.build(srcdir='fixtures/1.0/content/spec-ex-full',
              metadata=VersionMetadata(),
              objdir=None)
     log_out = log_io.getvalue()
     self.assertIn('### Inventory for v1', log_out)
     self.assertIn('"id": "uri:firkin",', log_out)
     self.assertIn('### Inventory for v2', log_out)
     self.assertIn('### Inventory for v3', log_out)
Esempio n. 3
0
 def test_bag_extracted_version(self):
     """Test bag_extracted_version method."""
     # Write bag with no metadata
     tempdir = tempfile.mkdtemp(prefix='test_bag1')
     with open(os.path.join(tempdir, 'my_file'), 'w', encoding="utf-8") as fh:
         fh.write("Something\n")
     metadata = VersionMetadata()
     bag_extracted_version(tempdir, metadata)
     with open(os.path.join(tempdir, 'bag-info.txt'), 'r', encoding="utf-8") as fh:
         info = fh.read()
     self.assertNotIn('Contact-Email', info)
     self.assertNotIn('Contact-Name', info)
     self.assertNotIn('External-Description', info)
     self.assertNotIn('External-Identifier', info)
     self.assertIn('Payload-Oxum', info)
     # Write bag with all metadata
     tempdir = tempfile.mkdtemp(prefix='test_bag2')
     with open(os.path.join(tempdir, 'my_file2'), 'w', encoding="utf-8") as fh:
         fh.write("Something else\n")
     metadata = VersionMetadata()
     metadata.message = "hello"
     metadata.name = "A Person"
     metadata.address = "mailto:[email protected]"
     metadata.id = 'info:a-bag-2'
     bag_extracted_version(tempdir, metadata)
     with open(os.path.join(tempdir, 'bag-info.txt'), 'r', encoding="utf-8") as fh:
         info = fh.read()
     self.assertIn('Contact-Email: [email protected]', info)
     self.assertIn('Contact-Name: A Person', info)
     self.assertIn('External-Description: hello', info)
     self.assertIn('External-Identifier: info:a-bag-2', info)
     self.assertIn('Payload-Oxum', info)
Esempio n. 4
0
 def test02_init(self):
     """Test VersionMetadata init method."""
     args = argparse.Namespace(created='a',
                               message='b',
                               name='c',
                               address='d')
     m = VersionMetadata(args=args)
     d = m.as_dict(extra='x')
     self.assertEqual(d['created'], 'a')
     self.assertEqual(d['message'], 'b')
     self.assertEqual(d['user'], {'name': 'c', 'address': 'd'})
     self.assertEqual(d['extra'], 'x')
     # with load from file
     with open('tests/testdata/inventories/inv_1_good.json', 'r', encoding="utf-8") as fh:
         inventory = json.load(fh)
     m = VersionMetadata(inventory=inventory, version='v1')
     self.assertEqual(m.version, 'v1')
     self.assertEqual(m.created, '2018-10-02T12:00:00Z')
Esempio n. 5
0
 def test_bag_as_source(self):
     """Test bag_as_source method."""
     metadata = VersionMetadata()
     self.assertRaises(bagit.BagError, bag_as_source, 'tests/testdata/files/hello_out_there.txt', metadata)
     self.assertRaises(BaggerError, bag_as_source, 'tests/testdata/bags/invalid_bag', metadata)
     # Check metadata update
     metadata = VersionMetadata()
     bag_as_source('tests/testdata/bags/uaa_v1', metadata)
     self.assertEqual(metadata.created, '2020-01-01T00:00:00Z')
     self.assertEqual(metadata.address, 'mailto:[email protected]')
     self.assertEqual(metadata.name, 'Yog-Sothoth')
     self.assertEqual(metadata.message, 'First version')
     self.assertEqual(metadata.id, 'info:bb123cd4567')
     # Check metedata still None when not in bag
     metadata = VersionMetadata()
     bag_as_source('tests/testdata/bags/bag_no_metadata', metadata)
     self.assertEqual(metadata.created, None)
     self.assertEqual(metadata.address, None)
     self.assertEqual(metadata.name, None)
     self.assertEqual(metadata.message, None)
     self.assertEqual(metadata.id, None)
Esempio n. 6
0
 def test10_create(self):
     """Test create method."""
     tempdir = tempfile.mkdtemp(prefix='test_create')
     oo = Object()
     self.assertRaises(ObjectException, oo.create, srcdir='fixtures/1.0/content/spec-ex-full/v1')
     oo.id = 'uri:kliderkin'
     objdir = os.path.join(tempdir, '1')
     oo.create(srcdir='fixtures/1.0/content/spec-ex-full/v1',
               metadata=VersionMetadata(),
               objdir=objdir)
     self.assertEqual(set(os.listdir(objdir)),
                      set(['0=ocfl_object_1.0',
                           'inventory.json', 'inventory.json.sha512',
                           'v1']))
Esempio n. 7
0
 def test06_build_inventory(self):
     """Test build_inventory."""
     oo = Object(digest_algorithm="md5")
     src_fs = fs.open_fs('fixtures/1.0/content/spec-ex-full')
     inventory = None
     for (dummy_vdir, inventory,
          dummy_manifest_to_srcfile) in oo.build_inventory(
              src_fs, metadata=VersionMetadata()):
         pass
     self.assertEqual(inventory['type'],
                      'https://ocfl.io/1.0/spec/#inventory')
     self.assertEqual(inventory['head'], 'v3')
     self.assertEqual(
         inventory['manifest'], {
             '184f84e28cbe75e050e9c25ea7f2e939': ['v1/content/foo/bar.xml'],
             '2673a7b11a70bc7ff960ad8127b4adeb': ['v2/content/foo/bar.xml'],
             'c289c8ccd4bab6e385f5afdd89b5bda2': ['v1/content/image.tiff'],
             'd41d8cd98f00b204e9800998ecf8427e': ['v1/content/empty.txt']
         })
     self.assertEqual(len(inventory['versions']), 3)
Esempio n. 8
0
 def test03_from_inventory(self):
     """Test from_inventory method."""
     m = VersionMetadata()
     m.from_inventory(
         inventory={
             "id": "III",
             "head": "v1",
             "versions": {
                 "v1": {
                     "created": "2020-01-02T03:04:05",
                     "message": "text",
                     "user": {
                         "address": "mailto:[email protected]",
                         "name": "Alice"
                     }
                 }
             }
         })
     self.assertEqual(m.id, "III")
     self.assertEqual(m.created, "2020-01-02T03:04:05")
     self.assertEqual(m.message, "text")
     self.assertEqual(m.address, "mailto:[email protected]")
     self.assertEqual(m.name, "Alice")
     # Error cases
     m = VersionMetadata()
     self.assertRaises(VersionMetadataException,
                       m.from_inventory,
                       inventory={"no versions": 1})
     self.assertRaises(VersionMetadataException,
                       m.from_inventory,
                       inventory={
                           "versions": {},
                           "no head": 1
                       })
     self.assertRaises(VersionMetadataException,
                       m.from_inventory,
                       inventory={
                           "versions": {
                               "no v1": {}
                           },
                           "head": "v1"
                       })
Esempio n. 9
0
 def test05_add_version(self):
     """Test add_version method."""
     self.maxDiff = None
     oo = Object(digest_algorithm="md5")
     inventory = {'manifest': {}, 'versions': {}}
     with open('fixtures/1.0/content/spec-ex-full/v1_inventory.json') as fh:
         v_inventory = json.load(fh)
     metadata = VersionMetadata(inventory=v_inventory, version='v1')
     src_fs = fs.open_fs('fixtures/1.0/content/spec-ex-full')
     oo.add_version(inventory,
                    src_fs,
                    src_dir='v1',
                    vdir='v1',
                    metadata=metadata)
     self.assertEqual(inventory['head'], 'v1')
     self.assertEqual(
         inventory['manifest'], {
             '184f84e28cbe75e050e9c25ea7f2e939': ['v1/content/foo/bar.xml'],
             'c289c8ccd4bab6e385f5afdd89b5bda2': ['v1/content/image.tiff'],
             'd41d8cd98f00b204e9800998ecf8427e': ['v1/content/empty.txt']
         })
     self.assertEqual(
         inventory['versions'], {
             "v1": {
                 'created': '2018-01-01T01:01:01Z',
                 'message': 'Initial import',
                 'state': {
                     '184f84e28cbe75e050e9c25ea7f2e939': ['foo/bar.xml'],
                     'c289c8ccd4bab6e385f5afdd89b5bda2': ['image.tiff'],
                     'd41d8cd98f00b204e9800998ecf8427e': ['empty.txt']
                 },
                 'user': {
                     'address': '*****@*****.**',
                     'name': 'Alice'
                 }
             }
         })
     self.assertNotIn('fixity', inventory)
     # Now add second version to check forward delta
     with open('fixtures/1.0/content/spec-ex-full/v2_inventory.json') as fh:
         v_inventory = json.load(fh)
     metadata = VersionMetadata(inventory=v_inventory, version='v2')
     src_fs = fs.open_fs('fixtures/1.0/content/spec-ex-full/v2')
     oo.add_version(inventory,
                    src_fs,
                    src_dir='',
                    vdir='v2',
                    metadata=metadata)
     self.assertEqual(inventory['head'], 'v2')
     self.assertEqual(
         inventory['manifest'], {
             '184f84e28cbe75e050e9c25ea7f2e939': ['v1/content/foo/bar.xml'],
             '2673a7b11a70bc7ff960ad8127b4adeb': ['v2/content/foo/bar.xml'],
             'c289c8ccd4bab6e385f5afdd89b5bda2': ['v1/content/image.tiff'],
             'd41d8cd98f00b204e9800998ecf8427e': ['v1/content/empty.txt']
         })
     self.assertEqual(
         inventory['versions']['v2'], {
             'created': '2018-02-02T02:02:02Z',
             'message': 'Fix bar.xml, remove image.tiff, add empty2.txt',
             'state': {
                 '2673a7b11a70bc7ff960ad8127b4adeb': ['foo/bar.xml'],
                 'd41d8cd98f00b204e9800998ecf8427e':
                 ['empty.txt', 'empty2.txt']
             },
             'user': {
                 'address': '*****@*****.**',
                 'name': 'Bob'
             }
         })
     # Now with fixity
     oo = Object(digest_algorithm="md5", fixity=['sha1'])
     inventory = {'manifest': {}, 'versions': {}, 'fixity': {'sha1': {}}}
     md1 = VersionMetadata()
     with open('fixtures/1.0/content/spec-ex-full/v1_inventory.json') as fh:
         v_inventory = json.load(fh)
     md1 = VersionMetadata(inventory=v_inventory, version='v1')
     src_fs = fs.open_fs('fixtures/1.0/content/spec-ex-full/v1')
     manifest_to_srcfile = oo.add_version(inventory,
                                          src_fs,
                                          src_dir='',
                                          vdir='v1',
                                          metadata=md1)
     self.assertEqual(
         manifest_to_srcfile, {
             'v1/content/image.tiff': 'image.tiff',
             'v1/content/empty.txt': 'empty.txt',
             'v1/content/foo/bar.xml': 'foo/bar.xml'
         })
     self.assertEqual(len(inventory['fixity']['sha1']), 3)
     # Test dedupe=False and forward_delta=False settings
     oo = Object(dedupe=False, forward_delta=False, fixity=['md5'])
     inventory = {'manifest': {}, 'versions': {}, 'fixity': {'md5': {}}}
     md1 = VersionMetadata(inventory={
         "id": "http://example.org/dedupe_content",
         "versions": {
             "v1": {
                 "created": "2020-07-15T17:40:00",
                 "message": "Initial import",
                 "user": {
                     "address": "mailto:[email protected]",
                     "name": "Alice"
                 }
             }
         }
     },
                           version='v1')
     src_fs = fs.open_fs('extra_fixtures/content/dedupe_content')
     manifest_to_srcfile = oo.add_version(inventory,
                                          src_fs,
                                          src_dir='v1',
                                          vdir='v1',
                                          metadata=md1)
     # Because of dedupe=False we will have multiple copies of empty files
     self.assertEqual(
         manifest_to_srcfile, {
             'v1/content/empty1.txt': 'v1/empty1.txt',
             'v1/content/empty2.txt': 'v1/empty2.txt',
             'v1/content/empty3.txt': 'v1/empty3.txt'
         })
     self.assertEqual(
         inventory['fixity']['md5'], {
             "d41d8cd98f00b204e9800998ecf8427e": [
                 "v1/content/empty1.txt", "v1/content/empty2.txt",
                 "v1/content/empty3.txt"
             ]
         })
     # Add a second version which will test for forward_delta=False
     md2 = VersionMetadata(inventory={
         "id": "http://example.org/dedupe_content",
         "versions": {
             "v2": {
                 "created": "2020-07-15T17:54:00",
                 "message": "Update",
                 "user": {
                     "address": "mailto:[email protected]",
                     "name": "Bob"
                 }
             }
         }
     },
                           version='v2')
     manifest_to_srcfile = oo.add_version(inventory,
                                          src_fs,
                                          src_dir='v2',
                                          vdir='v2',
                                          metadata=md2)
     # Because of forward_delta=False we will have an additional copy of the empty file
     self.assertEqual(manifest_to_srcfile,
                      {'v2/content/empty4.txt': 'v2/empty4.txt'})
     self.assertEqual(
         inventory['fixity']['md5'], {
             "d41d8cd98f00b204e9800998ecf8427e": [
                 "v1/content/empty1.txt", "v1/content/empty2.txt",
                 "v1/content/empty3.txt", "v2/content/empty4.txt"
             ]
         })