Download tests/test_tool_integrations.py from DiabetesCareChatbot/dmChatbotBackend: direct link, hf CLI and curl.
- Browser
- Download file 6.59 kB
-
https://huggingface.co/spaces/DiabetesCareChatbot/dmChatbotBackend/resolve/main/tests/test_tool_integrations.py
- Command line
-
hf download hf://spaces/DiabetesCareChatbot/dmChatbotBackend/tests/test_tool_integrations.py
-
curl -L -o test_tool_integrations.py https://huggingface.co/spaces/DiabetesCareChatbot/dmChatbotBackend/resolve/main/tests/test_tool_integrations.py
6.59 kB
| import os | |
| import sqlite3 | |
| import tempfile | |
| import pytest | |
| from src.tools.fhir_memory import ( | |
| save_patient, | |
| create_session, | |
| save_observation, | |
| get_observations_by_patient, | |
| get_patient_summary_fhir, | |
| save_chat_as_fhir, | |
| get_chat_history_by_session, | |
| get_sessions_by_patient, | |
| get_medications_by_patient, | |
| save_medication, | |
| ingest_fhir_bundle | |
| ) | |
| import src.tools.dietary_tools as dietary_tools | |
| from src.tools.web_tools import search_web | |
| def test_fhir_save_patient(clean_db): | |
| patient_id = "test-p1" | |
| name = "Alice Smith" | |
| res = save_patient.invoke({"patient_id": patient_id, "name": name}) | |
| assert "Successfully saved" in res | |
| # Check mock DB state | |
| db_patient = clean_db.db["patients"][patient_id] | |
| assert db_patient["id"] == patient_id | |
| assert db_patient["resource"]["name"][0]["text"] == name | |
| def test_fhir_session_creation(clean_db): | |
| patient_id = "test-p2" | |
| # Ensure patient exists | |
| save_patient.invoke({"patient_id": patient_id, "name": "Bob"}) | |
| session_id = create_session.invoke({"patient_id": patient_id, "title": "Diet Consult"}) | |
| assert session_id is not None | |
| sessions = get_sessions_by_patient.invoke({"patient_id": patient_id}) | |
| assert len(sessions) == 1 | |
| assert sessions[0]["id"] == session_id | |
| assert sessions[0]["title"] == "Diet Consult" | |
| def test_fhir_observation_saving(clean_db): | |
| patient_id = "test-p3" | |
| # Save observation | |
| res = save_observation.invoke({ | |
| "patient_id": patient_id, | |
| "value": 95.5, | |
| "unit": "mg/dL", | |
| "display": "Glucose", | |
| "loinc_code": "2339-0" | |
| }) | |
| assert "Successfully saved" in res | |
| # Retrieve and check | |
| obs = get_observations_by_patient.invoke({"patient_id": patient_id}) | |
| assert isinstance(obs, list) | |
| assert len(obs) == 1 | |
| assert obs[0]["valueQuantity"]["value"] == 95.5 | |
| assert obs[0]["subject"]["reference"] == f"Patient/{patient_id}" | |
| def test_fhir_chat_persistence(clean_db): | |
| patient_id = "test-p4" | |
| session_id = "session-1" | |
| messages = [ | |
| {"role": "user", "content": "How are you?"}, | |
| {"role": "assistant", "content": "I am a chatbot."} | |
| ] | |
| res = save_chat_as_fhir.invoke({ | |
| "patient_id": patient_id, | |
| "messages": messages, | |
| "session_id": session_id | |
| }) | |
| assert "Successfully saved" in res | |
| history = get_chat_history_by_session.invoke({"session_id": session_id}) | |
| assert len(history) == 1 | |
| assert history[0]["resourceType"] == "Communication" | |
| assert "user: How are you?" in history[0]["payload"][0]["contentString"] | |
| def test_fhir_medications(clean_db): | |
| patient_id = "test-p5" | |
| res = save_medication.invoke({ | |
| "patient_id": patient_id, | |
| "medication_name": "Metformin", | |
| "dosage": "500mg", | |
| "frequency": "twice daily" | |
| }) | |
| assert "Successfully saved" in res | |
| meds = get_medications_by_patient.invoke({"patient_id": patient_id}) | |
| assert len(meds) == 1 | |
| assert meds[0]["resourceType"] == "MedicationRequest" | |
| assert meds[0]["medication"]["coding"][0]["display"] == "Metformin" | |
| def test_ingest_fhir_bundle(clean_db): | |
| bundle = { | |
| "resourceType": "Bundle", | |
| "entry": [ | |
| { | |
| "resource": { | |
| "resourceType": "Patient", | |
| "id": "bundle-p1", | |
| "name": [{"text": "Bundle Patient"}] | |
| } | |
| }, | |
| { | |
| "resource": { | |
| "resourceType": "Observation", | |
| "id": "bundle-o1", | |
| "subject": {"reference": "Patient/bundle-p1"}, | |
| "valueQuantity": {"value": 120}, | |
| "code": {"coding": [{"display": "Systolic BP"}]} | |
| } | |
| } | |
| ] | |
| } | |
| res = ingest_fhir_bundle.invoke({"bundle": bundle}) | |
| assert "Successfully ingested 2 resources" in res | |
| patient = clean_db.db["patients"].get("bundle-p1") | |
| assert patient["resource"]["name"][0]["text"] == "Bundle Patient" | |
| def test_dietary_search_guidelines(): | |
| res = dietary_tools.search_guidelines.invoke({"query": "diabetes"}) | |
| assert "guidelines" in res | |
| assert "Mock Vector DB Guideline" in res | |
| def test_dietary_nutritional_database(): | |
| # Test case when database does not exist | |
| old_path = dietary_tools.DB_PATH | |
| dietary_tools.DB_PATH = "nonexistent_db_file.db" | |
| res_none = dietary_tools.get_nutritional_data.invoke({"food_name": "Apple"}) | |
| assert "not found" in res_none | |
| # Test case when database exists | |
| # Create a temporary SQLite database | |
| temp_db_fd, temp_db_path = tempfile.mkstemp() | |
| try: | |
| conn = sqlite3.connect(temp_db_path) | |
| cursor = conn.cursor() | |
| cursor.execute(""" | |
| CREATE TABLE nutritional_data ( | |
| food_name TEXT, | |
| calories INTEGER, | |
| protein REAL, | |
| carbs REAL, | |
| fat REAL, | |
| fiber REAL, | |
| vitamins TEXT | |
| ) | |
| """) | |
| cursor.execute(""" | |
| INSERT INTO nutritional_data VALUES ( | |
| 'Apple', 52, 0.3, 14.0, 0.2, 2.4, 'Vitamin C' | |
| ) | |
| """) | |
| conn.commit() | |
| conn.close() | |
| # Update DB_PATH to point to the temporary database | |
| dietary_tools.DB_PATH = temp_db_path | |
| # Test finding food item | |
| res_found = dietary_tools.get_nutritional_data.invoke({"food_name": "Apple"}) | |
| assert "Nutritional data found" in res_found | |
| assert "Apple: 52 kcal" in res_found | |
| assert "Protein: 0.3g" in res_found | |
| # Test finding non-existent food item | |
| res_missing = dietary_tools.get_nutritional_data.invoke({"food_name": "Banana"}) | |
| assert "No nutritional data found for 'Banana'" in res_missing | |
| finally: | |
| os.close(temp_db_fd) | |
| os.remove(temp_db_path) | |
| # Restore DB_PATH | |
| dietary_tools.DB_PATH = old_path | |
| def test_web_search(): | |
| res = search_web("diabetes symptoms") | |
| assert "Diabetes symptoms" in res | |
| assert "Common symptoms of diabetes." in res | |
| assert "https://example.com/diabetes" in res | |
| def test_fhir_tools_reject_missing_required_identity(clean_db): | |
| with pytest.raises(Exception): | |
| get_observations_by_patient.invoke({}) | |
| def test_fhir_bundle_rejects_missing_entries(clean_db): | |
| result = ingest_fhir_bundle.invoke({"bundle": {"resourceType": "Bundle"}}) | |
| assert result == "Successfully ingested 0 resources from FHIR Bundle." | |