tests: add test for request_haiku

Refs: OPS-70
This commit is contained in:
Christoph J. Scherr 2025-03-21 16:27:34 +01:00
parent aa236775ba
commit 897bf80e38
No known key found for this signature in database
GPG key ID: 9EB784BB202BB7BB
4 changed files with 36 additions and 9 deletions

25
poetry.lock generated
View file

@ -1,4 +1,4 @@
# This file is automatically @generated by Poetry 2.1.1 and should not be changed by hand.
# This file is automatically @generated by Poetry 2.0.1 and should not be changed by hand.
[[package]]
name = "alabaster"
@ -25,7 +25,7 @@ files = [
]
[package.extras]
dev = ["backports.zoneinfo ; python_version < \"3.9\"", "freezegun (>=1.0,<2.0)", "jinja2 (>=3.0)", "pytest (>=6.0)", "pytest-cov", "pytz", "setuptools", "tzdata ; sys_platform == \"win32\""]
dev = ["backports.zoneinfo", "freezegun (>=1.0,<2.0)", "jinja2 (>=3.0)", "pytest (>=6.0)", "pytest-cov", "pytz", "setuptools", "tzdata"]
[[package]]
name = "blinker"
@ -255,7 +255,7 @@ files = [
]
[package.extras]
toml = ["tomli ; python_full_version <= \"3.11.0a6\""]
toml = ["tomli"]
[[package]]
name = "docutils"
@ -514,6 +514,21 @@ tomli = {version = ">=1", markers = "python_version < \"3.11\""}
[package.extras]
dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"]
[[package]]
name = "pytest-httpserver"
version = "1.1.2"
description = "pytest-httpserver is a httpserver for pytest"
optional = false
python-versions = ">=3.9"
groups = ["main"]
files = [
{file = "pytest_httpserver-1.1.2-py3-none-any.whl", hash = "sha256:93009d79574fc982301e8494fdea0884f21bb0caf3bcc719151dfbd1e3a943ea"},
{file = "pytest_httpserver-1.1.2.tar.gz", hash = "sha256:38d0b726580d05c47cbd0ced1ecb36a51668ef1596cdc6d70a9cfa2b3cc00ebd"},
]
[package.dependencies]
Werkzeug = ">=2.0.0"
[[package]]
name = "requests"
version = "2.32.3"
@ -752,7 +767,7 @@ files = [
]
[package.extras]
brotli = ["brotli (>=1.0.9) ; platform_python_implementation == \"CPython\"", "brotlicffi (>=0.8.0) ; platform_python_implementation != \"CPython\""]
brotli = ["brotli (>=1.0.9)", "brotlicffi (>=0.8.0)"]
h2 = ["h2 (>=4,<5)"]
socks = ["pysocks (>=1.5.6,!=1.5.7,<2.0)"]
zstd = ["zstandard (>=0.18.0)"]
@ -778,4 +793,4 @@ watchdog = ["watchdog (>=2.3)"]
[metadata]
lock-version = "2.1"
python-versions = ">=3.10"
content-hash = "ce9cac7092447dc5d6b3920853bb10fcc166018bc4f48c50925ef09db74e7891"
content-hash = "76f74a5c2443d5c8e9db9433f0bdcc94a1d12c469f8b49dd325f3cc5a1b06f4e"

View file

@ -12,6 +12,7 @@ dependencies = [
"tinydb (>=3.1.0,<4.0.0)",
"requests (>=2.32.3,<3.0.0)",
"coverage (>=7.6.12,<8.0.0)",
"pytest-httpserver (>=1.1.2,<2.0.0)",
]

View file

@ -18,7 +18,7 @@ class Haiku:
return json.dumps(self.lines)
@staticmethod
def request_haiku(seed: str) -> Haiku:
def request_haiku(seed: str, url=AI_BASE_URL + AI_GEN_ENDPOINT) -> Haiku:
"""This function prompts the ai to generate
the hauku based on the user input"""
@ -31,7 +31,7 @@ class Haiku:
while True:
try:
r = requests.post(url=AI_BASE_URL + AI_GEN_ENDPOINT,
r = requests.post(url=url,
json=ai_gen_request)
ai_response = str(r.json()["response"])

View file

@ -5,6 +5,8 @@ from __future__ import annotations
import os
import json
import logging
from pytest_httpserver import HTTPServer
# do not remove this import. This is needed for
# pytest fixtures to work
@ -44,6 +46,15 @@ def test_get_haiku_json():
print(data)
def test_request_haiku():
haiku = Haiku.request_haiku("apple banana papaya")
def test_request_haiku(httpserver: HTTPServer):
httpserver.expect_request(
"/testhaiku").respond_with_json({"response":
"The apparition of these\n"
"faces in a crowd; Petal\n"
"on a wet, black bough."
})
haiku = Haiku.request_haiku(
"apple banana papaya", url=httpserver.url_for("/testhaiku"))
print(haiku)