-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
97 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"""Fake data for seed cli and testing.""" | ||
|
||
import json | ||
|
||
from server import db | ||
from server.models.document import Document | ||
|
||
FLASK_SEED_CORPUS = "server/nlp/corpus_flask_seed.json" | ||
|
||
|
||
def generate_test_documents(): | ||
"""Generate test documents.""" | ||
with open(FLASK_SEED_CORPUS) as f: | ||
corpus = json.load(f) | ||
|
||
documents = [] | ||
for doc in corpus: | ||
document = Document( | ||
question=doc["question"], | ||
label=doc["question"], | ||
source=doc["source"], | ||
content=doc["content"], | ||
) | ||
db.session.add(document) | ||
db.session.commit() | ||
documents.append(document) | ||
|
||
test_documents = [ | ||
{ | ||
"question": doc.question, | ||
"source": doc.source, | ||
"content": doc.content, | ||
"sql_id": doc.id, | ||
} | ||
for doc in documents | ||
] | ||
return test_documents |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
"""Utils for testing.""" | ||
|
||
import datetime | ||
import logging | ||
|
||
from werkzeug.test import TestResponse | ||
|
||
from server import ProperlyTypedSQLAlchemy | ||
from server.fake_data import generate_test_documents | ||
from server.models.email import Email | ||
from server.models.thread import Thread | ||
|
||
|
||
def assert_status(response: TestResponse, status: int): | ||
"""Asserts a response's status code, logging the response if it fails.""" | ||
|
@@ -15,3 +21,31 @@ def assert_status(response: TestResponse, status: int): | |
f"Response body: {response.data.decode()}" | ||
) | ||
raise | ||
|
||
def seed_database(db: ProperlyTypedSQLAlchemy): | ||
"""Seeds the database with some fake data.""" | ||
|
||
# add some documents to the database | ||
generate_test_documents() | ||
|
||
# create fake thread with 5 emails | ||
thread = Thread() | ||
db.session.add(thread) | ||
db.session.commit() | ||
|
||
emails = [] | ||
for i in range(5): | ||
# every other email is a reply sent from pigeon | ||
is_reply = i % 2 == 1 | ||
test_email = Email( | ||
date=datetime.datetime.now(datetime.timezone.utc), | ||
sender="[email protected]", | ||
subject="Test Subject", | ||
body="Test Body", | ||
message_id=f"test-message-id-{i}", | ||
is_reply=is_reply, | ||
thread_id=thread.id | ||
) | ||
emails.append(test_email) | ||
db.session.add(test_email) | ||
db.session.commit() |