Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding unit tests for files API (#403)
- Loading branch information
Showing
3 changed files
with
91 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from http import HTTPStatus | ||
|
||
import pytest | ||
from fastapi.testclient import TestClient | ||
from sqlalchemy.orm import Session | ||
|
||
# fixtures for test, aren't used directly so we need to ignore the lint here | ||
from tests.common_fixtures import ( # noqa: F401 | ||
patch_file_forbidden, | ||
patch_file_not_found, | ||
) | ||
|
||
|
||
@pytest.mark.usefixtures("patch_file_forbidden") | ||
def test_files_forbidden(db: Session, client: TestClient) -> None: | ||
validate_files_status_code(client, HTTPStatus.FORBIDDEN.value) | ||
|
||
|
||
@pytest.mark.usefixtures("patch_file_not_found") | ||
def test_files_not_found(db: Session, client: TestClient) -> None: | ||
validate_files_status_code(client, HTTPStatus.NOT_FOUND.value) | ||
|
||
|
||
def validate_files_status_code(client: TestClient, status_code: int): | ||
resp = client.get('/api/files?schema=v3io&path=mybucket/files.txt') | ||
assert resp.status_code == status_code | ||
|
||
resp = client.get('/api/files?schema=v3io&path=mybucket/') | ||
assert resp.status_code == status_code | ||
|
||
resp = client.get('/api/filestat?schema=v3io&path=mybucket/files.txt') | ||
assert resp.status_code == status_code |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from http import HTTPStatus | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
import requests | ||
|
||
import v3io.dataplane | ||
|
||
|
||
@pytest.fixture | ||
def patch_file_forbidden(monkeypatch): | ||
class MockV3ioClient: | ||
def __init__(self, *args, **kwargs): | ||
pass | ||
|
||
def get_container_contents(self, *args, **kwargs): | ||
raise RuntimeError('Permission denied') | ||
|
||
mock_get = mock_failed_get_func(HTTPStatus.FORBIDDEN.value) | ||
|
||
monkeypatch.setattr(requests, "get", mock_get) | ||
monkeypatch.setattr(requests, "head", mock_get) | ||
monkeypatch.setattr(v3io.dataplane, "Client", MockV3ioClient) | ||
|
||
|
||
@pytest.fixture | ||
def patch_file_not_found(monkeypatch): | ||
class MockV3ioClient: | ||
def __init__(self, *args, **kwargs): | ||
pass | ||
|
||
def get_container_contents(self, *args, **kwargs): | ||
raise FileNotFoundError | ||
|
||
mock_get = mock_failed_get_func(HTTPStatus.NOT_FOUND.value) | ||
|
||
monkeypatch.setattr(requests, "get", mock_get) | ||
monkeypatch.setattr(requests, "head", mock_get) | ||
monkeypatch.setattr(v3io.dataplane, "Client", MockV3ioClient) | ||
|
||
|
||
def mock_failed_get_func(status_code: int): | ||
def mock_get(*args, **kwargs): | ||
mock_response = Mock() | ||
mock_response.status_code = status_code | ||
mock_response.raise_for_status = Mock( | ||
side_effect=requests.HTTPError('Error', response=mock_response) | ||
) | ||
return mock_response | ||
|
||
return mock_get |
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