Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More tests!! #3

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ def handle_image(self, from_: str, url: str) -> None:
self.send_message(from_, FAILED_PIC_SAVE)
else:
self.send_message(
from_, VIEW_SUBMISSIONS.format(self.generate_view_url(user.hash, prompt.id))
from_,
VIEW_SUBMISSIONS.format(self.generate_view_url(user.hash, prompt.id)),
)

def handle_admin_message(self, text: str) -> None:
Expand Down
2 changes: 1 addition & 1 deletion SmsClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from Client import Client
from config import Settings, settings
from constants import PROD_ENV, IGNORE_MESSAGE
from constants import IGNORE_MESSAGE, PROD_ENV
from database import Database

logger = logging.getLogger(__name__)
Expand Down
9 changes: 3 additions & 6 deletions TestClient.py → TextTestClient.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from typing import override

from sqlalchemy.orm import Session

from Client import Client
from config import Settings, settings
from database import Database


class TestClient(Client):
class TextTestClient(Client):
def __init__(self, settings: Settings, db: Database | None = None):
super().__init__(settings, db)

Expand All @@ -22,12 +20,11 @@ def receive_message(self, from_: str, text: str):

@override
def handle_beta_message(self, from_: str, text: str):
prompt_text = " ".join(text.split(" ")[1:])
return prompt_text
return text


if __name__ == "__main__":
client = TextClient()
client = TextTestClient()

while True:
text = input()
Expand Down
18 changes: 9 additions & 9 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
class Settings(BaseSettings):
model_config = SettingsConfigDict(env_file=".env")

environment: str
twilio_account_sid: str
twilio_auth_token: str
twilio_phone_number: str
db_name: str
hash_secret: str
admin_pass: str
beta_code: str
beta_allowlist: str
environment: str = "DEV"
twilio_account_sid: str = ""
twilio_auth_token: str = ""
twilio_phone_number: str = ""
db_name: str = "snapshot"
hash_secret: str = "test"
admin_pass: str = "definitely_not_admin"
beta_code: str = "test"
beta_allowlist: str = ""


settings = Settings()
78 changes: 69 additions & 9 deletions test_suite.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,89 @@
import re
from unittest.mock import MagicMock

from config import settings
from models import Registration
from TestClient import TestClient
from TextTestClient import TextTestClient

settings.twilio_account_sid = "please_dont_work"
settings.twilio_auth_token = "secret"
settings.twilio_phone_number = "8675309"


USER = "Jenny"
NUM = "8675309"


def test_handle_message_no_reg():
# Arrange
db = MagicMock()

db.get_reg = MagicMock(return_value=None)
# db.create_reg = MagicMock(
# return_value=Registration(username="Jenny", phone="8675309", state=0)
# )
db.create_reg = MagicMock(
return_value=Registration(username=USER, phone=NUM, state=0)
)
db.get_user_by_phone = MagicMock(return_value=None)

tc = TextTestClient(settings, db)
# Act
tc.handle_message(NUM, "HI")

# Assert
db.get_reg.assert_called_once_with(NUM)
db.create_reg.assert_called_once_with(NUM)


def test_handle_message_reg_state_0():
# Arrange
db = MagicMock()

db.get_reg = MagicMock(return_value=Registration(username=USER, phone=NUM, state=0))
db.create_reg = MagicMock(return_value=None)
db.get_user_by_phone = MagicMock(return_value=None)

tc = TextTestClient(settings, db)
# Act
tc.handle_message(NUM, "HI")

# Assert
db.get_reg.assert_called_once_with(NUM)
db.create_reg.assert_not_called()


def test_handle_message_reg_state_1():
# Arrange
db = MagicMock()

db.get_reg = MagicMock(return_value=Registration(username=USER, phone=NUM, state=1))
db.create_reg = MagicMock(return_value=None)
db.get_user_by_phone = MagicMock(return_value=None)
db.update_reg = MagicMock(return_value=None)

tc = TextTestClient(settings, db)
# Act
tc.handle_message(NUM, USER)

# Assert
db.get_reg.assert_called_once_with(NUM)
# db.update_reg.assert_called_once()


def test_handle_message_reg_state_2():
# Arrange
db = MagicMock()

db.get_reg = MagicMock(return_value=Registration(username=USER, phone=NUM, state=2))
db.create_reg = MagicMock(return_value=None)
db.get_user_by_phone = MagicMock(return_value=None)
db.update_reg = MagicMock(return_value=None)
db.create_user = MagicMock(return_value=None)
db.get_current_prompt = MagicMock(return_value=None)

tc = TestClient(settings, db)
tc = TextTestClient(settings, db)
# Act
tc.receive_message("8675309", "HI")
tc.handle_message(NUM, "yes")

# Assert
db.get_reg.assert_called_once_with("8675309")
db.create_reg.assert_called_once_with("8675309")
db.get_reg.assert_called_once_with(NUM)
db.create_user.assert_called_once_with(NUM, USER)
db.update_reg.assert_called_once_with(NUM, 3, USER)
db.get_current_prompt.assert_called_once()
Loading