From 6405503a05852b5fb921e232b14281d34f453a04 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Mon, 16 Dec 2024 09:56:34 -0800 Subject: [PATCH] Cache test should run with or without environment variables. --- .../example-pytest-selfie/tests/cache_test.py | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/python/example-pytest-selfie/tests/cache_test.py b/python/example-pytest-selfie/tests/cache_test.py index 2d3200bd..0222e003 100644 --- a/python/example-pytest-selfie/tests/cache_test.py +++ b/python/example-pytest-selfie/tests/cache_test.py @@ -1,23 +1,24 @@ -import random import os -import requests - -from selfie_lib import cache_selfie, cache_selfie_binary, cache_selfie_json +import random +import requests from openai import OpenAI +from selfie_lib import cache_selfie, cache_selfie_binary, cache_selfie_json def test_cache_selfie(): cache_selfie(lambda: str(random.random())).to_be("0.06699295946441819") -def test_gen_ai(): - # Initialize OpenAI API client - openai = OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) +def openai(): + return OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) + +def test_gen_ai(): # Fetch the chat response with caching chat = cache_selfie_json( - lambda: openai.chat.completions.create( + lambda: openai() + .chat.completions.create( model="gpt-4o", messages=[ { @@ -25,7 +26,8 @@ def test_gen_ai(): "content": "Expressive but brief language describing a robot creating a self portrait.", } ], - ).to_dict() + ) + .to_dict() ).to_be("""{ "id": "chatcmpl-Af1Nf34netAfGW7ZIQArEHavfuYtg", "choices": [ @@ -62,9 +64,11 @@ def test_gen_ai(): }""") # raise ValueError(f"KEYS={chat.keys()} TYPE={type(chat)}") image_url = cache_selfie_json( - lambda: openai.images.generate( + lambda: openai() + .images.generate( model="dall-e-3", prompt=chat["choices"][0]["message"]["content"] - ).to_dict() + ) + .to_dict() ).to_be("""{ "created": 1734340142, "data": [