gauravmeena0708/epfo-circulars
0
1import io2import unittest3import docx4 5import docx_export6 7 8class TestDocxExport(unittest.TestCase):9 10 def test_create_research_report_docx(self):11 query = "What is the procedure for joint declaration?"12 answer = "To update joint declaration, the member and employer must submit the request online via Unified Portal."13 references = [14 {15 "rank": 1,16 "title": "Standard Operating Procedure for Joint Declaration",17 "source": "Circular No. 12345",18 "date": "2023-10-15",19 "score": 0.92,20 "text": "The joint declaration application should be verified by the employer within 30 days.",21 "url": "https://epfindia.gov.in/circulars/12345.pdf",22 }23 ]24 25 doc_bytes = docx_export.create_research_report_docx(query, answer, references)26 self.assertIsInstance(doc_bytes, bytes)27 self.assertGreater(len(doc_bytes), 1000)28 29 # Verify it can be loaded by python-docx30 doc = docx.Document(io.BytesIO(doc_bytes))31 all_text = "\n".join(p.text for p in doc.paragraphs) + "\n" + "\n".join(32 c.text for t in doc.tables for r in t.rows for c in r.cells33 )34 self.assertIn("EPFO Knowledge Assistant", all_text)35 self.assertIn(query, all_text)36 self.assertIn("Standard Operating Procedure", all_text)37 38 def test_create_chat_transcript_docx(self):39 title = "Data Analysis: employee_data.csv"40 chat_history = [41 {"role": "user", "content": "What is the average salary in the dataset?"},42 {"role": "assistant", "content": "The average salary observed is ₹45,000 based on 1,200 records."},43 ]44 metadata = {"File Name": "employee_data.csv", "Total Rows": 1200}45 46 doc_bytes = docx_export.create_chat_transcript_docx(title, chat_history, metadata)47 self.assertIsInstance(doc_bytes, bytes)48 self.assertGreater(len(doc_bytes), 1000)49 50 doc = docx.Document(io.BytesIO(doc_bytes))51 full_text = "\n".join(p.text for p in doc.paragraphs)52 self.assertIn(title, full_text)53 self.assertIn("What is the average salary", full_text)54 self.assertIn("45,000", full_text)55 56 def test_create_text_document_docx(self):57 title = "Extracted OCR Text"58 body = "--- [Page 1 / 1] ---\nThis is scanned noting sheet text from regional office."59 doc_bytes = docx_export.create_text_document_docx(title, body)60 self.assertIsInstance(doc_bytes, bytes)61 self.assertGreater(len(doc_bytes), 500)62 63 doc = docx.Document(io.BytesIO(doc_bytes))64 full_text = "\n".join(p.text for p in doc.paragraphs)65 self.assertIn(title, full_text)66 self.assertIn("scanned noting sheet text", full_text)67 68 69if __name__ == "__main__":70 unittest.main()71 