Skip to content

Commit

Permalink
fastapi_test added
Browse files Browse the repository at this point in the history
  • Loading branch information
Bulrush3 committed May 22, 2024
1 parent a31414e commit ad4aa13
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
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

0 comments on commit ad4aa13

Please sign in to comment.