Skip to content

Commit f9d5c80

Browse files
committed
generated file: tests/test_services.py
1 parent 7750f10 commit f9d5c80

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

tests/test_services.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import pytest
2+
from fastapi import HTTPException
3+
from unittest.mock import patch, MagicMock
4+
from core.services.openai_service import OpenAIService
5+
from core.models.models import RequestBody, ResponseModel
6+
7+
@pytest.fixture
8+
def openai_service():
9+
return OpenAIService("your_openai_api_key")
10+
11+
def test_openai_service_init(openai_service):
12+
assert openai_service.openai is not None
13+
14+
@patch("openai.OpenAI.completions.create")
15+
async def test_generate_text_success(mock_create, openai_service):
16+
mock_create.return_value = MagicMock(choices=[MagicMock(text="Generated text")])
17+
text = "This is a test text."
18+
model = "gpt-3.5-turbo"
19+
response = await openai_service.generate_text(text, model)
20+
assert response.text == "Generated text"
21+
mock_create.assert_called_once_with(model=model, prompt=text, temperature=0.7, max_tokens=2048)
22+
23+
@patch("openai.OpenAI.completions.create")
24+
async def test_generate_text_error(mock_create, openai_service):
25+
mock_create.side_effect = Exception("Error generating text")
26+
text = "This is a test text."
27+
model = "gpt-3.5-turbo"
28+
with pytest.raises(HTTPException) as excinfo:
29+
await openai_service.generate_text(text, model)
30+
assert excinfo.value.detail == "Error generating text: Error generating text"
31+
32+
@patch("openai.OpenAI.translations.create")
33+
async def test_translate_text_success(mock_create, openai_service):
34+
mock_create.return_value = MagicMock(choices=[MagicMock(text="Translated text")])
35+
text = "This is a test text."
36+
model = "gpt-3.5-turbo"
37+
target_language = "fr"
38+
response = await openai_service.translate_text(text, model, target_language)
39+
assert response.text == "Translated text"
40+
mock_create.assert_called_once_with(model=model, prompt=text, target_language=target_language)
41+
42+
@patch("openai.OpenAI.translations.create")
43+
async def test_translate_text_error(mock_create, openai_service):
44+
mock_create.side_effect = Exception("Error translating text")
45+
text = "This is a test text."
46+
model = "gpt-3.5-turbo"
47+
target_language = "fr"
48+
with pytest.raises(HTTPException) as excinfo:
49+
await openai_service.translate_text(text, model, target_language)
50+
assert excinfo.value.detail == "Error translating text: Error translating text"
51+
52+
def test_request_body_valid_data():
53+
text = "This is a test text."
54+
model = "gpt-3.5-turbo"
55+
request_body = RequestBody(text=text, model=model)
56+
assert request_body.text == text
57+
assert request_body.model == model
58+
59+
def test_request_body_empty_text():
60+
with pytest.raises(ValueError) as excinfo:
61+
RequestBody(text="", model="gpt-3.5-turbo")
62+
assert str(excinfo.value) == "Text cannot be empty."
63+
64+
def test_request_body_invalid_model():
65+
with pytest.raises(ValueError) as excinfo:
66+
RequestBody(text="This is a test text.", model="invalid_model")
67+
assert str(excinfo.value) == "Invalid model name."
68+
69+
def test_response_model_valid_data():
70+
text = "This is a test response."
71+
response_model = ResponseModel(text=text)
72+
assert response_model.text == text
73+
74+
def test_response_model_empty_text():
75+
text = ""
76+
response_model = ResponseModel(text=text)
77+
assert response_model.text == text

0 commit comments

Comments
 (0)