+ def test_read_nonnormalized_manifest_with_collection_reader(self):
+ # client should be able to use CollectionReader on a manifest without normalizing it
+ client = self.api_client_mock(500)
+ nonnormal = ". acbd18db4cc2f85cedef654fccc4a4d8+3+Aabadbadbee@abeebdee 0:3:foo.txt 1:0:bar.txt 0:3:foo.txt\n"
+ reader = arvados.CollectionReader(
+ nonnormal,
+ api_client=client, num_retries=0)
+ # Ensure stripped_manifest() doesn't mangle our manifest in
+ # any way other than stripping hints.
+ self.assertEqual(
+ re.sub('\+[^\d\s\+]+', '', nonnormal),
+ reader.stripped_manifest())
+ # Ensure stripped_manifest() didn't mutate our reader.
+ self.assertEqual(nonnormal, reader.manifest_text())
+ # Ensure the files appear in the order given in the manifest.
+ self.assertEqual(
+ [[6, '.', 'foo.txt'],
+ [0, '.', 'bar.txt']],
+ [[f.size(), f.stream_name(), f.name()]
+ for f in reader.all_streams()[0].all_files()])
+
+ def test_read_empty_collection(self):
+ client = self.api_client_mock(200)
+ self.mock_get_collection(client, 200, 'empty')
+ reader = arvados.CollectionReader('d41d8cd98f00b204e9800998ecf8427e+0',
+ api_client=client)
+ self.assertEqual('', reader.manifest_text())
+
+ def test_api_response(self):
+ client = self.api_client_mock()
+ reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
+ self.assertEqual(self.DEFAULT_COLLECTION, reader.api_response())
+
+ def test_api_response_with_collection_from_keep(self):
+ client = self.api_client_mock()
+ self.mock_get_collection(client, 404, 'foo')
+ with tutil.mock_get_responses(self.DEFAULT_MANIFEST, 200):
+ reader = arvados.CollectionReader(self.DEFAULT_DATA_HASH,
+ api_client=client)
+ api_response = reader.api_response()
+ self.assertIsNone(api_response)
+
+ def check_open_file(self, coll_file, stream_name, file_name, file_size):
+ self.assertFalse(coll_file.closed, "returned file is not open")
+ self.assertEqual(stream_name, coll_file.stream_name())
+ self.assertEqual(file_name, coll_file.name())
+ self.assertEqual(file_size, coll_file.size())
+
+ def test_open_collection_file_one_argument(self):
+ client = self.api_client_mock(200)
+ reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
+ cfile = reader.open('./foo')
+ self.check_open_file(cfile, '.', 'foo', 3)
+
+ def test_open_collection_file_two_arguments(self):
+ client = self.api_client_mock(200)
+ reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
+ cfile = reader.open('.', 'foo')
+ self.check_open_file(cfile, '.', 'foo', 3)
+
+ def test_open_deep_file(self):
+ coll_name = 'collection_with_files_in_subdir'
+ client = self.api_client_mock(200)
+ self.mock_get_collection(client, 200, coll_name)
+ reader = arvados.CollectionReader(
+ self.API_COLLECTIONS[coll_name]['uuid'], api_client=client)
+ cfile = reader.open('./subdir2/subdir3/file2_in_subdir3.txt')
+ self.check_open_file(cfile, './subdir2/subdir3', 'file2_in_subdir3.txt',
+ 32)
+
+ def test_open_nonexistent_stream(self):
+ client = self.api_client_mock(200)
+ reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
+ self.assertRaises(ValueError, reader.open, './nonexistent', 'foo')
+
+ def test_open_nonexistent_file(self):
+ client = self.api_client_mock(200)
+ reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
+ self.assertRaises(ValueError, reader.open, '.', 'nonexistent')
+