Swift-Query / tests /verify_workflow.py
Darshan Kumar R
Deploy to Hugging Face Spaces (Clean)
ed0b977
Raw History Blame Contribute Delete
3.64 kB
import sys
import os
import json
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')))
from main import app, db
from utils.db.db import Doctor, ChatSession, ChatMessage, Patient
def verify_workflow():
with app.test_client() as client:
# Preamble: Create context
with app.app_context():
# Ensure Test Patient
if not Patient.query.get(999):
p = Patient(id=999, name="Test Patient", email="test@patient.com", password="pwd")
db.session.add(p)
# Ensure Test Doctor
doc = Doctor.query.filter_by(email="testdoc@example.com").first()
if not doc:
doc = Doctor(name="Test Doc", specialization="General", email="testdoc@example.com", phone="123", password="password", bio="Test Bio")
db.session.add(doc)
db.session.commit()
# 1. Login
print("\n--- Test 1: Login ---")
client.post('/login/doctor', data={'name': 'Test Doc', 'password': 'password'}, follow_redirects=True)
print("PASS: Login attempt made.")
# 2. Start Session (Patient Search)
print("\n--- Test 2: Start Session with Patient ID ---")
# Simulating frontend call: sends "patient search on" then calls ID
resp = client.post('/api/chat', json={'message': 'patient search on'}) # session_id=None
# Now send ID (simulating modal submit)
# Note: In real flow, frontend clears session_id then sends ID.
# Main.py logic: if no session_id provided, it creates one.
# If we send patient ID logic keywords, backend links it.
# Actually frontend sends `patientId` *as message body*? Yes: fetchAIResponse(patientId)
resp = client.post('/api/chat', json={'message': '999'})
data = resp.get_json()
session_id = data.get('session_id')
print(f"Response Session ID: {session_id}")
if session_id:
print("PASS: Session created.")
else:
print("FAIL: No session ID returned.")
return
# 3. Chat in Session
print("\n--- Test 3: Chat in Session ---")
resp = client.post('/api/chat', json={'message': 'Hello Doctor', 'session_id': session_id})
data = resp.get_json()
# Verify message count in DB
with app.app_context():
count = ChatMessage.query.filter_by(session_id=session_id).count()
print(f"Messages in DB for Session {session_id}: {count}")
if count >= 2: # At least User + AI response from step 2 and step 3... wait, step 2 was search result.
print("PASS: Messages saved.")
else:
print("FAIL: Messages not saved correctly.")
# 4. Get History
print("\n--- Test 4: Get History ---")
resp = client.get('/api/history')
hist = resp.get_json()['history']
print(f"History Sessions: {len(hist)}")
found = any(s['session_id'] == session_id for s in hist)
if found:
print("PASS: Session found in history.")
else:
print("FAIL: Session NOT found in history.")
# 5. Restore Session
print("\n--- Test 5: Restore Session ---")
resp = client.get(f'/api/history/{session_id}')
msgs = resp.get_json()['messages']
print(f"Restored Messages: {len(msgs)}")
if len(msgs) > 0:
print("PASS: Messages restored.")
else:
print("FAIL: Restoration failed.")
if __name__ == "__main__":
verify_workflow()