From 897bf80e38be5d94a7b88613dc923f3ea8099bbe Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Fri, 21 Mar 2025 16:27:34 +0100 Subject: [PATCH] tests: add test for request_haiku Refs: OPS-70 --- poetry.lock | 25 ++++++++++++++++++++----- pyproject.toml | 1 + senju/haiku.py | 4 ++-- tests/{test_tests.py => test_haiku.py} | 15 +++++++++++++-- 4 files changed, 36 insertions(+), 9 deletions(-) rename tests/{test_tests.py => test_haiku.py} (70%) diff --git a/poetry.lock b/poetry.lock index 0bd0101..33ca411 100644 --- a/poetry.lock +++ b/poetry.lock @@ -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" diff --git a/pyproject.toml b/pyproject.toml index ec97022..688e449 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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)", ] diff --git a/senju/haiku.py b/senju/haiku.py index f117a5f..b86951c 100644 --- a/senju/haiku.py +++ b/senju/haiku.py @@ -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"]) diff --git a/tests/test_tests.py b/tests/test_haiku.py similarity index 70% rename from tests/test_tests.py rename to tests/test_haiku.py index eac199e..d959278 100644 --- a/tests/test_tests.py +++ b/tests/test_haiku.py @@ -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)