4823: Add Collection.copy tests
[arvados.git] / sdk / python / tests / test_collections.py
index 5ed8716d8955ae36b05255dda81598ec35f1c2a1..892f146e9088a074e53d72bd8f7c31db117c6591 100644 (file)
@@ -15,6 +15,8 @@ import unittest
 import run_test_server
 import arvados_testutil as tutil
 from arvados.ranges import Range, LocatorAndRange
+from arvados import import_manifest, export_manifest
+from arvados.arvfile import SYNC_EXPLICIT
 
 class TestResumableWriter(arvados.ResumableCollectionWriter):
     KEEP_BLOCK_SIZE = 1024  # PUT to Keep every 1K.
@@ -760,15 +762,14 @@ class CollectionWriterTestCase(unittest.TestCase, CollectionTestMixin):
 
     def test_open_flush(self):
         client = self.api_client_mock()
-        writer = arvados.CollectionWriter(client)
-        with writer.open('flush_test') as out_file:
-            out_file.write('flush1')
-            data_loc1 = hashlib.md5('flush1').hexdigest() + '+6'
-            with self.mock_keep(data_loc1, 200) as keep_mock:
+        data_loc1 = hashlib.md5('flush1').hexdigest() + '+6'
+        data_loc2 = hashlib.md5('flush2').hexdigest() + '+6'
+        with self.mock_keep((data_loc1, 200), (data_loc2, 200)) as keep_mock:
+            writer = arvados.CollectionWriter(client)
+            with writer.open('flush_test') as out_file:
+                out_file.write('flush1')
                 out_file.flush()
-            out_file.write('flush2')
-            data_loc2 = hashlib.md5('flush2').hexdigest() + '+6'
-        with self.mock_keep(data_loc2, 200) as keep_mock:
+                out_file.write('flush2')
             self.assertEqual(". {} {} 0:12:flush_test\n".format(data_loc1,
                                                                 data_loc2),
                              writer.manifest_text())
@@ -787,15 +788,14 @@ class CollectionWriterTestCase(unittest.TestCase, CollectionTestMixin):
 
     def test_two_opens_two_streams(self):
         client = self.api_client_mock()
-        writer = arvados.CollectionWriter(client)
-        with writer.open('file') as out_file:
-            out_file.write('file')
-            data_loc1 = hashlib.md5('file').hexdigest() + '+4'
-        with self.mock_keep(data_loc1, 200) as keep_mock:
+        data_loc1 = hashlib.md5('file').hexdigest() + '+4'
+        data_loc2 = hashlib.md5('indir').hexdigest() + '+5'
+        with self.mock_keep((data_loc1, 200), (data_loc2, 200)) as keep_mock:
+            writer = arvados.CollectionWriter(client)
+            with writer.open('file') as out_file:
+                out_file.write('file')
             with writer.open('./dir', 'indir') as out_file:
                 out_file.write('indir')
-                data_loc2 = hashlib.md5('indir').hexdigest() + '+5'
-        with self.mock_keep(data_loc2, 200) as keep_mock:
             expected = ". {} 0:4:file\n./dir {} 0:5:indir\n".format(
                 data_loc1, data_loc2)
             self.assertEqual(expected, writer.manifest_text())
@@ -813,7 +813,62 @@ class NewCollectionTestCase(unittest.TestCase, CollectionTestMixin):
 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt
 """
-        print arvados.export_manifest(arvados.import_manifest(m1))
+        self.assertEqual(". 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:127:md5sum.txt\n", arvados.export_manifest(arvados.import_manifest(m1)))
+
+    def test_init_manifest(self):
+        m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
+. 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
+. 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt
+"""
+        self.assertEqual(". 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:127:md5sum.txt\n", arvados.export_manifest(arvados.Collection(m1)))
+
+
+    def test_remove(self):
+        with arvados.import_manifest('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n', sync=SYNC_EXPLICIT) as c:
+            self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n", export_manifest(c))
+            self.assertTrue("count1.txt" in c)
+            c.remove("count1.txt")
+            self.assertFalse("count1.txt" in c)
+            self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", export_manifest(c))
+
+    def test_remove_in_subdir(self):
+        with arvados.import_manifest('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n', sync=SYNC_EXPLICIT) as c:
+            c.remove("foo/count2.txt")
+            self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", export_manifest(c))
+
+    def test_remove_empty_subdir(self):
+        with arvados.import_manifest('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n', sync=SYNC_EXPLICIT) as c:
+            c.remove("foo/count2.txt")
+            c.remove("foo")
+            self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", export_manifest(c))
+
+    def test_remove_nonempty_subdir(self):
+        with arvados.import_manifest('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n', sync=SYNC_EXPLICIT) as c:
+            with self.assertRaises(IOError):
+                c.remove("foo")
+            c.remove("foo", rm_r=True)
+            self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", export_manifest(c))
+
+    def test_copy_to_dir1(self):
+        with arvados.import_manifest('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n', sync=SYNC_EXPLICIT) as c:
+            c.copy("count1.txt", "foo/count2.txt")
+            self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", export_manifest(c))
+
+    def test_copy_to_dir2(self):
+        with arvados.import_manifest('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n', sync=SYNC_EXPLICIT) as c:
+            c.copy("count1.txt", "foo")
+            self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", export_manifest(c))
+
+    def test_copy_to_dir2(self):
+        with arvados.import_manifest('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n', sync=SYNC_EXPLICIT) as c:
+            c.copy("count1.txt", "foo/")
+            self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", export_manifest(c))
+
+    def test_copy_file(self):
+        with arvados.import_manifest('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n', sync=SYNC_EXPLICIT) as c:
+            c.copy("count1.txt", "count2.txt")
+            self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n", export_manifest(c))
+
 
 if __name__ == '__main__':
     unittest.main()