-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* List non-current and live version(s) and Restore specific version from non-current to live * Added unit tests for list_versions and restore() * Bumped new version 2.0.14 -> 2.0.15
- Loading branch information
1 parent
d93b008
commit 1d176f9
Showing
3 changed files
with
191 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,148 @@ | ||
# Test for FileClient class | ||
|
||
import unittest | ||
from unittest.mock import Mock | ||
from unittest.mock import patch | ||
|
||
from dapla import FileClient | ||
|
||
PATH_WITH_PREFIX = "gs://bucket/path" | ||
PATH_WITHOUT_PREFIX = "bucket/path" | ||
|
||
|
||
def test_ensure_gcs_uri_prefix() -> None: | ||
assert FileClient._ensure_gcs_uri_prefix(PATH_WITH_PREFIX) == PATH_WITH_PREFIX | ||
assert FileClient._ensure_gcs_uri_prefix(PATH_WITHOUT_PREFIX) == PATH_WITH_PREFIX | ||
class TestFiles(unittest.TestCase): | ||
|
||
def test_ensure_gcs_uri_prefix(self) -> None: | ||
assert FileClient._ensure_gcs_uri_prefix(PATH_WITH_PREFIX) == PATH_WITH_PREFIX | ||
assert ( | ||
FileClient._ensure_gcs_uri_prefix(PATH_WITHOUT_PREFIX) == PATH_WITH_PREFIX | ||
) | ||
|
||
def test_remove_gcs_uri_prefix(self) -> None: | ||
assert ( | ||
FileClient._remove_gcs_uri_prefix(PATH_WITH_PREFIX) == PATH_WITHOUT_PREFIX | ||
) | ||
assert ( | ||
FileClient._remove_gcs_uri_prefix(PATH_WITHOUT_PREFIX) | ||
== PATH_WITHOUT_PREFIX | ||
) | ||
|
||
@patch("google.cloud.storage.Client") | ||
def test_get_versions_valid(self, mock_client: Mock) -> None: | ||
# Arrange | ||
bucket_name = "test-bucket" | ||
file_name = "test-file.txt" | ||
mock_bucket = Mock() | ||
mock_client.return_value.bucket.return_value = mock_bucket | ||
mock_blob1 = Mock( | ||
name="test-file.txt", | ||
generation=1, | ||
updated="2023-04-01T00:00:00Z", | ||
time_deleted=None, | ||
) | ||
mock_blob2 = Mock( | ||
name="test-file.txt", | ||
generation=2, | ||
updated="2023-04-02T00:00:00Z", | ||
time_deleted=None, | ||
) | ||
mock_bucket.list_blobs.return_value = [mock_blob1, mock_blob2] | ||
|
||
files = FileClient.get_versions(bucket_name, file_name) | ||
|
||
mock_client.return_value.bucket.assert_called_with(bucket_name) | ||
mock_bucket.list_blobs.assert_called_with(prefix=file_name, versions=True) | ||
|
||
assert len(files) == 2 | ||
|
||
assert files[0].name == mock_blob1.name | ||
assert files[0].generation == mock_blob1.generation | ||
assert files[0].updated == mock_blob1.updated | ||
assert files[0].time_deleted is None | ||
|
||
@patch("google.cloud.storage.Client") | ||
def test_get_versions_nonexistent_file(self, mock_client: Mock) -> None: | ||
bucket_name = "test-bucket" | ||
file_name = "nonexistent-file.txt" | ||
mock_bucket = Mock() | ||
mock_client.return_value.bucket.return_value = mock_bucket | ||
mock_bucket.list_blobs.return_value = [] | ||
|
||
files = FileClient.get_versions(bucket_name, file_name) | ||
|
||
mock_client.return_value.bucket.assert_called_with(bucket_name) | ||
mock_bucket.list_blobs.assert_called_with(prefix=file_name, versions=True) | ||
|
||
assert len(files) == 0 | ||
assert files == [] | ||
|
||
@patch("google.cloud.storage.Client") | ||
def test_get_versions_empty_bucket(self, mock_client: Mock) -> None: | ||
bucket_name = "test-bucket" | ||
file_name = "test-file.txt" | ||
mock_bucket = Mock() | ||
mock_client.return_value.bucket.return_value = mock_bucket | ||
mock_bucket.list_blobs.return_value = [] | ||
|
||
files = FileClient.get_versions(bucket_name, file_name) | ||
|
||
mock_client.return_value.bucket.assert_called_with(bucket_name) | ||
mock_bucket.list_blobs.assert_called_with(prefix=file_name, versions=True) | ||
|
||
assert len(files) == 0 | ||
assert files == [] | ||
|
||
@patch("google.cloud.storage.Client") | ||
def test_restore_version_success(self, mock_client: Mock) -> None: | ||
mock_bucket = Mock() | ||
mock_source_blob = Mock() | ||
mock_client.return_value.bucket.return_value = mock_bucket | ||
mock_bucket.blob.return_value = mock_source_blob | ||
|
||
blob = FileClient.restore_version( | ||
bucket_name="test-bucket", | ||
file_name="test-file.txt", | ||
destination_file="restored-file.txt", | ||
generation_id="1234567890", | ||
destination_generation_id="0", | ||
) | ||
|
||
mock_client.return_value.bucket.assert_called_with("test-bucket") | ||
mock_bucket.blob.assert_called_with("test-file.txt") | ||
mock_bucket.copy_blob.assert_called_with( | ||
mock_source_blob, | ||
mock_bucket, | ||
"restored-file.txt", | ||
source_generation="1234567890", | ||
if_generation_match="0", | ||
) | ||
assert blob == mock_bucket.copy_blob.return_value | ||
|
||
@patch("google.cloud.storage.Client") | ||
def test_restore_version_existing_live_version(self, mock_client: Mock) -> None: | ||
mock_bucket = Mock() | ||
mock_source_blob = Mock() | ||
mock_client.return_value.bucket.return_value = mock_bucket | ||
mock_bucket.blob.return_value = mock_source_blob | ||
|
||
blob = FileClient.restore_version( | ||
bucket_name="test-bucket", | ||
file_name="test-file.txt", | ||
destination_file="restored-file.txt", | ||
generation_id="1234567890", | ||
destination_generation_id="0987654321", | ||
) | ||
|
||
mock_client.return_value.bucket.assert_called_with("test-bucket") | ||
mock_bucket.blob.assert_called_with("test-file.txt") | ||
mock_bucket.copy_blob.assert_called_with( | ||
mock_source_blob, | ||
mock_bucket, | ||
"restored-file.txt", | ||
source_generation="1234567890", | ||
if_generation_match="0987654321", | ||
) | ||
assert blob == mock_bucket.copy_blob.return_value | ||
|
||
|
||
def test_remove_gcs_uri_prefix() -> None: | ||
assert FileClient._remove_gcs_uri_prefix(PATH_WITH_PREFIX) == PATH_WITHOUT_PREFIX | ||
assert FileClient._remove_gcs_uri_prefix(PATH_WITHOUT_PREFIX) == PATH_WITHOUT_PREFIX | ||
if __name__ == "__main__": | ||
unittest.main() |