Coverage for tests/test_md.py: 100%
35 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-20 21:23 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-20 21:23 +0000
1import re
2import os
3import io
4from app.models import User
5from app import db
6from werkzeug.datastructures import FileStorage
7from unittest.mock import patch
8# from app import app
10def login_user(client):
11 test_user = User(username="testuser")
12 test_user.set_password("password")
13 db.session.add(test_user)
14 db.session.commit()
15 response = client.post(
16 "/index",
17 data={"username": "testuser", "password": "password"},
18 follow_redirects=True,
19 )
20 return client
22@patch("app.doc_indexer.retrieve_document.query_database")
23def test_md(mock_query_database, client, app):
24 # When the database returns a similarity score, override it to avoid 'no doc found'
25 # mock_query_database.return_value = [
26 # ("Mocked document content about DNA", 0.9),
27 # ]
28 # Add document to database for use in testing
29 app.config["WTF_CSRF_ENABLED"] = (
30 False # In testing envionment, no need for CSRF_TOKEN
31 )
32 with app.app_context():
33 with client:
34 client = login_user(client)
35 file_path = os.path.join(os.path.dirname(__file__), "test_data", "Dna.pdf")
36 with open(file_path, "rb") as f:
37 # Create a FileStorage object from the file content
38 pdf_file = FileStorage(
39 stream=io.BytesIO(f.read()),
40 filename="Dna.pdf",
41 content_type="application/pdf",
42 )
43 data = {
44 "pdf_file": (io.BytesIO(pdf_file.read()), "Dna.pdf"),
45 }
46 response = client.post(
47 "/admin", data=data, content_type="multipart/form-data"
48 )
50 response = client.post("/chat", json={"message": "Tell me about DNA", "conversationHistory": []})
51 content = response.get_data(as_text=True)
52 print(content)
53 md_format = False
54 patterns = [
55 r"^\s*#+\s", # Headers (allow leading spaces)
56 r"(\*\*|__)(.*?)\1", # Bold text
57 r"(\*|_)(.*?)\1", # Italic text
58 r"!?\[(.*?)\]\((.*?)\)", # Images and links
59 r"`(.*?)`", # Inline code
60 r"^-{3,}$", # Horizontal rules
61 r"^\s*[-*\+]\s", # Lists
62 r">", # Blockquotes
63 ]
64 for pattern in patterns:
65 if re.search(pattern, content, re.M):
66 md_format = True
67 break
68 assert md_format == True
69 # try:
70 # html = markdown.markdown(content)
71 # except Exception as e:
72 # pytest.fail(f"Markdown conversion failed: {e}")
73 # assert response.status_code == 200