diff --git a/.gitignore b/.gitignore index 287a2f0..eec56e0 100644 --- a/.gitignore +++ b/.gitignore @@ -160,3 +160,4 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +__pycache__/image_classification_fastapi.cpython-311.pyc diff --git a/fastapi_test.py b/fastapi_test.py new file mode 100644 index 0000000..856f2ee --- /dev/null +++ b/fastapi_test.py @@ -0,0 +1,37 @@ +import pytest + +from fastapi.testclient import TestClient +from image_classification_fastapi import app, load_image + +client = TestClient(app) + +def test_root(): + response = client.get("/") + assert response.status_code == 200 + assert response.json() == {"message": "Image classification API"} + +@pytest.fixture +def mock_image(): + """Фикстура для загрузки изображения.""" + from PIL import Image + import io + + # Создание простого черного изображения для тестов + image = Image.new('RGB', (224, 224), color='black') + img_byte_arr = io.BytesIO() + image.save(img_byte_arr, format='PNG') + img_byte_arr.seek(0) + + return img_byte_arr + +class MockResponse: + def __init__(self, raw): + self.raw = raw + +def test_load_image(mocker, mock_image): + """Тест для функции загрузки изображения.""" + mock_response = MockResponse(mock_image) + mocker.patch('requests.get', return_value=mock_response) + url = "https://www.rgo.ru/sites/default/files/styles/head_image_article/public/node/61549/photo-2023-11-08-150058.jpeg" + image = load_image(url) + assert image is not None \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index a9ce732..5e34ad1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,6 @@ requests Pillow pydantic streamlit -translators \ No newline at end of file +translators +pytest +pytest-mock \ No newline at end of file