def test_error_raised_if_attempting_to_generate_signed_url_for_local_dataset( self): """Test that an error is raised if trying to generate a signed URL for a local dataset.""" with tempfile.TemporaryDirectory() as temporary_directory: dataset = Dataset(path=temporary_directory, tags={"hello": "world"}) with self.assertRaises(exceptions.CloudLocationNotSpecified): dataset.generate_signed_url()
def test_generating_signed_url_from_dataset_and_recreating_dataset_from_it( self): """Test that a signed URL can be generated for a dataset that can be used to recreate/get it, its metadata, and all its files. """ with tempfile.TemporaryDirectory() as temporary_directory: dataset_local_path = os.path.join(temporary_directory, "my-dataset-to-sign") with Datafile(path=os.path.join(dataset_local_path, "my-file.dat"), mode="w") as (datafile, f): f.write("hello") datafile.tags = {"my": "metadata"} dataset = Dataset(path=dataset_local_path, tags={"hello": "world"}) dataset.upload( storage.path.generate_gs_path(TEST_BUCKET_NAME, "my-dataset-to-sign")) with patch("google.cloud.storage.blob.Blob.generate_signed_url", new=mock_generate_signed_url): signed_url = dataset.generate_signed_url() downloaded_dataset = Dataset(path=signed_url) self.assertEqual(downloaded_dataset.tags, {"hello": "world"}) with downloaded_dataset.files.one() as (downloaded_datafile, f): self.assertEqual(f.read(), "hello") self.assertEqual(downloaded_datafile.name, "my-file.dat") self.assertEqual(downloaded_datafile.extension, "dat")