|
| 1 | +""" |
| 2 | +test_checkpoint.py |
| 3 | +
|
| 4 | +Tests that the App class methods work as expected. |
| 5 | +
|
| 6 | +Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti. |
| 7 | + |
| 8 | +Last updated: 19/08/2024 |
| 9 | +""" |
| 10 | + |
| 11 | +from exceptions.exceptions import ( |
| 12 | + AuthenticationException, |
| 13 | + NotFoundException, |
| 14 | + UnknownException, |
| 15 | +) |
| 16 | + |
| 17 | +from cs3resource import Resource |
| 18 | + |
| 19 | +import cs3.rpc.v1beta1.code_pb2 as cs3code |
| 20 | +from fixtures import ( # noqa: F401 (they are used, the framework is not detecting it) |
| 21 | + mock_config, |
| 22 | + mock_logger, |
| 23 | + mock_authentication, |
| 24 | + mock_gateway, |
| 25 | + checkpoint_instance, |
| 26 | + mock_status_code_handler, |
| 27 | +) |
| 28 | + |
| 29 | +from unittest.mock import Mock, patch |
| 30 | + |
| 31 | +import pytest |
| 32 | + |
| 33 | +# Test cases for the Checkpoint class |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize( |
| 37 | + "status_code, status_message, expected_exception, versions", |
| 38 | + [ |
| 39 | + (cs3code.CODE_OK, None, None, [Mock(), Mock()]), |
| 40 | + (cs3code.CODE_UNAUTHENTICATED, "error", AuthenticationException, None), |
| 41 | + (cs3code.CODE_NOT_FOUND, "error", NotFoundException, None), |
| 42 | + (-2, "error", UnknownException, None), |
| 43 | + ], |
| 44 | +) |
| 45 | +def test_list_file_versions( |
| 46 | + checkpoint_instance, status_code, status_message, expected_exception, versions # noqa: F811 (not a redefinition) |
| 47 | +): |
| 48 | + resource = Resource.from_file_ref_and_endpoint(endpoint="", file="testfile") |
| 49 | + page_token = "page_token" |
| 50 | + page_size = 10 |
| 51 | + |
| 52 | + mock_response = Mock() |
| 53 | + mock_response.status.code = status_code |
| 54 | + mock_response.status.message = status_message |
| 55 | + mock_response.versions = versions |
| 56 | + |
| 57 | + with patch.object(checkpoint_instance._gateway, "ListFileVersions", return_value=mock_response): |
| 58 | + if expected_exception: |
| 59 | + with pytest.raises(expected_exception): |
| 60 | + checkpoint_instance.list_file_versions(resource, page_token, page_size) |
| 61 | + else: |
| 62 | + result = checkpoint_instance.list_file_versions(resource, page_token, page_size) |
| 63 | + assert result == versions |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize( |
| 67 | + "status_code, status_message, expected_exception", |
| 68 | + [ |
| 69 | + (cs3code.CODE_OK, None, None), |
| 70 | + (cs3code.CODE_UNAUTHENTICATED, "error", AuthenticationException), |
| 71 | + (cs3code.CODE_NOT_FOUND, "error", NotFoundException), |
| 72 | + (-2, "error", UnknownException), |
| 73 | + ], |
| 74 | +) |
| 75 | +def test_restore_file_version( |
| 76 | + checkpoint_instance, status_code, status_message, expected_exception # noqa: F811 (not a redefinition) |
| 77 | +): |
| 78 | + resource = Resource.from_file_ref_and_endpoint(endpoint="", file="testfile") |
| 79 | + version_key = "version_key" |
| 80 | + lock_id = "lock_id" |
| 81 | + |
| 82 | + mock_response = Mock() |
| 83 | + mock_response.status.code = status_code |
| 84 | + mock_response.status.message = status_message |
| 85 | + |
| 86 | + with patch.object(checkpoint_instance._gateway, "RestoreFileVersion", return_value=mock_response): |
| 87 | + if expected_exception: |
| 88 | + with pytest.raises(expected_exception): |
| 89 | + checkpoint_instance.restore_file_version(resource, version_key, lock_id) |
| 90 | + else: |
| 91 | + result = checkpoint_instance.restore_file_version(resource, version_key, lock_id) |
| 92 | + assert result is None |
0 commit comments