forked from streamlit/llm-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_test.py
More file actions
58 lines (47 loc) · 1.98 KB
/
Copy pathapp_test.py
File metadata and controls
58 lines (47 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import datetime
import sys
from types import SimpleNamespace
from unittest.mock import patch
from streamlit.testing.v1 import AppTest
class _FakeGenerativeModel:
def __init__(self, *args, **kwargs):
pass
def generate_content(self, prompt):
return MockResponse("stub")
def _fake_configure(api_key=None):
pass
if "google.generativeai" not in sys.modules:
stub = SimpleNamespace(
GenerativeModel=_FakeGenerativeModel, configure=_fake_configure
)
google_mod = sys.modules.setdefault("google", SimpleNamespace())
google_mod.generativeai = stub
sys.modules["google.generativeai"] = stub
class MockResponse:
def __init__(self, text):
self.text = text
@patch("google.generativeai.GenerativeModel.generate_content")
def test_Chatbot(genai_create):
at = AppTest.from_file("Chatbot.py").run()
assert not at.exception
at.chat_input[0].set_value("Do you know any jokes?").run()
assert at.info[0].value == "Please add your Google API key to continue."
JOKE = "Why did the chicken cross the road? To get to the other side."
genai_create.return_value = MockResponse(JOKE)
at.text_input(key="chatbot_api_key").set_value("sk-...")
at.chat_input[0].set_value("Do you know any jokes?").run()
print(at)
assert at.chat_message[1].markdown[0].value == "Do you know any jokes?"
assert at.chat_message[2].markdown[0].value == JOKE
assert at.chat_message[2].avatar == "assistant"
assert not at.exception
@patch("google.generativeai.GenerativeModel.generate_content")
def test_Quickstart(genai_create):
at = AppTest.from_file("pages/3_Langchain_Quickstart.py").run()
assert at.info[0].value == "Please add your Google API key to continue."
RESPONSE = "1. The best way to learn how to code is by practicing..."
genai_create.return_value = MockResponse(RESPONSE)
at.sidebar.text_input[0].set_value("sk-...")
at.button[0].set_value(True).run()
print(at)
assert at.info[0].value == RESPONSE