def test__set_blob_metadata(self, mock_client, mock_creds):
     storage_conn = GoogleStorageConnector(GCS_BUCKET_NAME, SETTINGS)
     blob = storage_conn.bucket.blob("blob_name")
     metadata = {"key1": "value1", "key2": "value2"}
     storage_conn._set_blob_metadata(blob=blob,
                                     fmt="csv.gz",
                                     metadata=metadata)
     self.assertEqual(blob.content_encoding, "gzip")
     self.assertEqual(blob.cache_control, "no-cache")
     self.assertEqual(blob.metadata, metadata)
 def test_get_blob_metadata_valid(self, mock_client, mock_creds):
     storage_conn = GoogleStorageConnector(GCS_BUCKET_NAME, SETTINGS)
     valid_formats = [("markdown", EXPECTED_BLOB_METADATA_MARKDOWN),
                      ("object", EXPECTED_BLOB_METADATA_OBJECT)]
     for metadata_format in valid_formats:
         with self.subTest(
                 msg=f"Testing blob metadata format {metadata_format}",
                 _input=metadata_format):
             blob_metadata = storage_conn.get_blob_metadata(
                 "blob_name", metadata_format[0])
             self.assertEqual(blob_metadata, metadata_format[1])
 def test_write_valid(self, mock_client, mock_creds):
     test_data = "test;test;test\n1;2;3"
     metadata = {
         "id": "test",
         "title": "test",
         "description": "test",
         "bucket": GCS_BUCKET_NAME,
         "author": "test"
     }
     storage_conn = GoogleStorageConnector(GCS_BUCKET_NAME, SETTINGS)
     public_url = storage_conn.write(data=test_data,
                                     destination_blob_name="pakke/ressurs",
                                     fmt="csv",
                                     metadata=metadata)
     self.assertEqual(public_url, BLOB_PUBLIC_URL)
    def test__parse_gcp_credentials_valid(self):
        credential_formats = [
            f"{Path(self.credentials_dir).joinpath('service-account.json')}",
            GOOGLE_SERVICE_ACCOUNT,
            json.dumps(GOOGLE_SERVICE_ACCOUNT)
        ]

        for credential in credential_formats:
            with self.subTest(msg="Test parse gcp credentials",
                              _input=credential):
                creds = GoogleStorageConnector.parse_gcp_credentials(
                    credential)
                self.assertEqual(creds, GOOGLE_SERVICE_ACCOUNT)
 def test_read_valid(self, mock_client, mock_creds):
     storage_conn = GoogleStorageConnector(GCS_BUCKET_NAME, SETTINGS)
     blob = storage_conn.read("blob_name")
     self.assertEqual(blob, BLOB_DATA)
 def test_instantiation_invalid_bucket_not_found(self, mock_client,
                                                 mock_creds):
     with self.assertRaises(dataverk_exceptions.StorageBucketDoesNotExist):
         storage_conn = GoogleStorageConnector("non-existent-bucket",
                                               SETTINGS)
 def test_instantiation_valid_empty_settings(self, mock_client, mock_creds):
     storage_conn = GoogleStorageConnector(GCS_BUCKET_NAME, {})
     self.assertIsInstance(storage_conn, GoogleStorageConnector)
 def test_instantiation_valid(self, mock_client, mock_creds):
     storage_conn = GoogleStorageConnector(GCS_BUCKET_NAME, SETTINGS)
     self.assertIsInstance(storage_conn, GoogleStorageConnector)
 def test_is_json_file_false(self):
     is_json = GoogleStorageConnector.is_json_file(
         json.dumps(GOOGLE_SERVICE_ACCOUNT))
     self.assertFalse(is_json)
 def test_is_json_file_true(self):
     is_json = GoogleStorageConnector.is_json_file(
         f"{Path(self.credentials_dir).joinpath('service-account.json')}")
     self.assertTrue(is_json)
 def test_get_blob_metadata_invalid_format(self, mock_client, mock_creds):
     storage_conn = GoogleStorageConnector(GCS_BUCKET_NAME, SETTINGS)
     with self.assertRaises(NotImplementedError):
         blob_metadata = storage_conn.get_blob_metadata(
             "blob_name", "not-implemented-format")