Skip to content

Commit e2491b8

Browse files
committed
seed database and get threads
1 parent 082dffc commit e2491b8

File tree

6 files changed

+97
-35
lines changed

6 files changed

+97
-35
lines changed

server/cli.py

+3-34
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Flask CLI commands."""
22

33
import datetime
4-
import json
54

65
from flask import Blueprint
76

@@ -11,6 +10,7 @@
1110
increment_response_count,
1211
thread_emails_to_openai_messages,
1312
)
13+
from server.fake_data import generate_test_documents
1414
from server.models.document import Document
1515
from server.models.email import Email
1616
from server.models.response import Response
@@ -20,37 +20,6 @@
2020

2121
seed = Blueprint("seed", __name__)
2222

23-
FLASK_SEED_CORPUS = "server/nlp/corpus_flask_seed.json"
24-
25-
26-
def _generate_test_documents():
27-
"""Generate test documents."""
28-
with open(FLASK_SEED_CORPUS) as f:
29-
corpus = json.load(f)
30-
31-
documents = []
32-
for doc in corpus:
33-
document = Document(
34-
question=doc["question"],
35-
label=doc["question"],
36-
source=doc["source"],
37-
content=doc["content"],
38-
)
39-
db.session.add(document)
40-
db.session.commit()
41-
documents.append(document)
42-
43-
test_documents = [
44-
{
45-
"question": doc.question,
46-
"source": doc.source,
47-
"content": doc.content,
48-
"sql_id": doc.id,
49-
}
50-
for doc in documents
51-
]
52-
return test_documents
53-
5423

5524
def _embed_existing_documents(documents: list[Document]):
5625
"""Embed existing documents."""
@@ -69,7 +38,7 @@ def _embed_existing_documents(documents: list[Document]):
6938
@seed.cli.command()
7039
def corpus():
7140
"""Add test documents to the corpus."""
72-
test_documents = _generate_test_documents()
41+
test_documents = generate_test_documents()
7342
embed_corpus(test_documents)
7443

7544

@@ -82,7 +51,7 @@ def email():
8251
# responses, so the only way this command succeeds is if the corpus is
8352
# already populated
8453
print("No documents in the database. Generating test documents...")
85-
test_documents = _generate_test_documents()
54+
test_documents = generate_test_documents()
8655
embed_corpus(test_documents)
8756
else:
8857
print("Embedding existing documents...")

server/fake_data.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Fake data for seed cli and testing."""
2+
3+
import json
4+
5+
from server import db
6+
from server.models.document import Document
7+
8+
FLASK_SEED_CORPUS = "server/nlp/corpus_flask_seed.json"
9+
10+
11+
def generate_test_documents():
12+
"""Generate test documents."""
13+
with open(FLASK_SEED_CORPUS) as f:
14+
corpus = json.load(f)
15+
16+
documents = []
17+
for doc in corpus:
18+
document = Document(
19+
question=doc["question"],
20+
label=doc["question"],
21+
source=doc["source"],
22+
content=doc["content"],
23+
)
24+
db.session.add(document)
25+
db.session.commit()
26+
documents.append(document)
27+
28+
test_documents = [
29+
{
30+
"question": doc.question,
31+
"source": doc.source,
32+
"content": doc.content,
33+
"sql_id": doc.id,
34+
}
35+
for doc in documents
36+
]
37+
return test_documents

server/nlp/responses.py

-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ def generate_context(
131131
docs = {}
132132

133133
results = query_all(3, questions)
134-
print("results", results)
135134
message = "Here is some context to help you answer this email: \n"
136135
for result in results:
137136
confidence = 0

server_tests/conftest.py

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from server import create_app, db
2121
from server.config import LOCAL, VECTOR_DIMENSION
22+
from server_tests.utils import seed_database
2223

2324

2425
@pytest.fixture(scope="session")
@@ -75,6 +76,9 @@ def app(db_url: str, redis_host: str):
7576
}
7677
)
7778

79+
with app.app_context():
80+
seed_database(db)
81+
7882
yield app
7983

8084

server_tests/test_email.py

+19
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,22 @@ def test_get_threads(app: APIFlask, client: FlaskClient):
88
"""Test fetching threads."""
99
response = client.get("/api/emails/get_threads")
1010
assert_status(response, 200)
11+
12+
threads = response.json
13+
assert threads is not None
14+
assert len(threads) == 1
15+
16+
thread = threads[0]
17+
assert thread["id"] == 1
18+
assert not thread["resolved"] # resolved is false
19+
assert len(thread["emailList"]) == 5
20+
21+
for email in thread["emailList"]:
22+
# checking that .map() is intact
23+
assert email["id"] is not None
24+
assert email["body"] is not None
25+
assert email["subject"] is not None
26+
assert email["sender"] is not None
27+
assert email["message_id"] is not None
28+
assert email["is_reply"] is not None
29+
assert email["thread_id"] is not None

server_tests/utils.py

+34
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
"""Utils for testing."""
22

3+
import datetime
34
import logging
45

56
from werkzeug.test import TestResponse
67

8+
from server import ProperlyTypedSQLAlchemy
9+
from server.fake_data import generate_test_documents
10+
from server.models.email import Email
11+
from server.models.thread import Thread
12+
713

814
def assert_status(response: TestResponse, status: int):
915
"""Asserts a response's status code, logging the response if it fails."""
@@ -15,3 +21,31 @@ def assert_status(response: TestResponse, status: int):
1521
f"Response body: {response.data.decode()}"
1622
)
1723
raise
24+
25+
def seed_database(db: ProperlyTypedSQLAlchemy):
26+
"""Seeds the database with some fake data."""
27+
28+
# add some documents to the database
29+
generate_test_documents()
30+
31+
# create fake thread with 5 emails
32+
thread = Thread()
33+
db.session.add(thread)
34+
db.session.commit()
35+
36+
emails = []
37+
for i in range(5):
38+
# every other email is a reply sent from pigeon
39+
is_reply = i % 2 == 1
40+
test_email = Email(
41+
date=datetime.datetime.now(datetime.timezone.utc),
42+
43+
subject="Test Subject",
44+
body="Test Body",
45+
message_id=f"test-message-id-{i}",
46+
is_reply=is_reply,
47+
thread_id=thread.id
48+
)
49+
emails.append(test_email)
50+
db.session.add(test_email)
51+
db.session.commit()

0 commit comments

Comments
 (0)