File size: 2,580 Bytes
ed0b977
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69


import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from flask import Flask, session, request
from utils.blueprints.assistant import assistant_bp
import mongomock

# Patch mongo with mock
from utils.blueprints import assistant
assistant.mongo.db = mongomock.MongoClient().db

app = Flask(__name__, template_folder='templates')
app.secret_key = 'test'
app.register_blueprint(assistant_bp)

def test_search_and_entry():
    with app.test_request_context():
        # Setup session
        session['user_type'] = 'assistant'
        session['role'] = 'data_entry'
        session['user_name'] = 'Test Assistant'
        
        # 1. Create a patient
        assistant.mongo.db.patient.insert_one({
            "id": 101, "sql_id": 101, "name": "John Doe", "phone": "1234567890", "email": "john@example.com"
        })
        
        # 2. Test Search by Phone
        with app.test_client() as client:
            with client.session_transaction() as sess:
                sess['user_type'] = 'assistant'
                sess['role'] = 'data_entry'
            
            resp = client.get('/assistant/dashboard/data_entry?q=1234567890')
            if b"John Doe" in resp.data:
                print("SUCCESS: Search by phone found patient.")
            else:
                print("FAILURE: Search by phone did not find patient.")
                
        # 3. Test Data Entry with String ID (Legacy Fix)
        with app.test_client() as client:
            with client.session_transaction() as sess:
                sess['user_type'] = 'assistant'
                sess['role'] = 'data_entry'
                sess['user_name'] = 'Test Assistant'
            
            # Simulate posting to a "string" ID url that matches the int ID in DB
            # The route is /add_record/<record_type>/<patient_id>
            # We pass "101" as string, backend should convert to int 101 and save.
            data = {
                "diagnosis_date": "2023-01-01",
                "condition": "Flu",
                "notes": "Test Note"
            }
            resp = client.post('/assistant/add_record/summary/101', data=data, follow_redirects=True)
            
            # Check DB
            record = assistant.mongo.db.medicalhistory.find_one({"patient_id": 101})
            if record and record['condition'] == 'Flu':
                print("SUCCESS: Data entry saved with ID resolution.")
            else:
                print("FAILURE: Data entry NOT saved.")

if __name__ == "__main__":
    test_search_and_entry()