Skip to content

Commit

Permalink
✨ feat(tests): add server run and random prompt tests
Browse files Browse the repository at this point in the history
Added test cases for the server run and random prompt functionalities in the
novelai-python project. The server run test checks the health endpoint and
generating images with a valid token. The random prompt test checks the
generation of random prompts with and without NSFW enabled, as well as
character features for different genders.

[optional footer(s)]
  • Loading branch information
sudoskys committed Jan 30, 2024
1 parent fbf55ad commit cfa0347
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
70 changes: 70 additions & 0 deletions tests/test_random_prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-
# @Time : 2024/1/27 上午10:41
# @Author : sudoskys
# @File : test_random_prompt.py
# @Software: PyCharm

from src.novelai_python.utils.random_prompt import RandomPromptGenerator


def test_generate_returns_string():
generator = RandomPromptGenerator(nsfw_enabled=True)
result = generator.generate()
assert isinstance(result, str)


def test_generate_returns_non_empty_string():
generator = RandomPromptGenerator(nsfw_enabled=True)
result = generator.generate()
assert len(result) > 0


def test_generate_returns_different_results():
generator = RandomPromptGenerator(nsfw_enabled=True)
result1 = generator.generate()
result2 = generator.generate()
assert result1 != result2


def test_generate_with_nsfw_disabled():
generator = RandomPromptGenerator(nsfw_enabled=False)
result = generator.generate()
assert 'nsfw' not in result


def test_generate_with_nsfw_enabled():
generator = RandomPromptGenerator(nsfw_enabled=True)
result = generator.generate()
assert 'nsfw' in result


def test_get_weighted_choice_returns_string():
generator = RandomPromptGenerator(nsfw_enabled=True)
result = generator.get_weighted_choice([['tag1', 1], ['tag2', 2]], [])
assert isinstance(result, str)


def test_get_weighted_choice_returns_valid_tag():
generator = RandomPromptGenerator(nsfw_enabled=True)
result = generator.get_weighted_choice([['tag1', 1], ['tag2', 2]], [])
assert result in ['tag1', 'tag2']


def test_character_features_returns_list():
generator = RandomPromptGenerator(nsfw_enabled=True)
result = generator.character_features('m', 'front', True, 1)
assert isinstance(result, list)


def test_character_features_returns_non_empty_list():
generator = RandomPromptGenerator(nsfw_enabled=True)
result = generator.character_features('m', 'front', True, 1)
assert len(result) > 0


def test_character_features_with_different_genders():
generator = RandomPromptGenerator(nsfw_enabled=True)
result_m = generator.character_features('m', 'front', True, 1)
result_f = generator.character_features('f', 'front', True, 1)
result_o = generator.character_features('o', 'front', True, 1)
assert result_m != result_f != result_o
28 changes: 28 additions & 0 deletions tests/test_server_run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
# @Time : 2024/1/30 下午11:52
# @Author : sudoskys
# @File : test_server_run.py
# @Software: PyCharm
from fastapi.testclient import TestClient

from src.novelai_python.sdk.ai.generate_image import GenerateImageInfer
from src.novelai_python.server import app, get_session

client = TestClient(app)


def test_health_check():
response = client.get("/health")
assert response.status_code == 200
assert response.json() == {"status": "ok"}


def test_generate_image_with_valid_token():
valid_token = "valid_token"
get_session(valid_token) # to simulate a valid session
response = client.post(
"/ai/generate_image",
headers={"Authorization": valid_token},
json=GenerateImageInfer(input="1girl").model_dump()
)
assert response.status_code == 500

0 comments on commit cfa0347

Please sign in to comment.