Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fastapi_test added #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 37 additions & 0 deletions fastapi_test.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ requests
Pillow
pydantic
streamlit
translators
translators
pytest
pytest-mock
Loading