Spaces:
Runtime error
Runtime error
Download tests/test_pipeline.py from Modrik-Team/Modrik: direct link, hf CLI and curl.
- Browser
- Download file 15.3 kB
-
https://huggingface.co/spaces/Modrik-Team/Modrik/resolve/main/tests/test_pipeline.py
- Command line
-
hf download hf://spaces/Modrik-Team/Modrik/tests/test_pipeline.py
-
curl -L -o test_pipeline.py https://huggingface.co/spaces/Modrik-Team/Modrik/resolve/main/tests/test_pipeline.py
15.3 kB
| """ | |
| Pipeline tests โ covers every public function in the project. | |
| Modules tested: | |
| - core.file_service._chunk | |
| - core.file_service.extract_text (.txt, .pdf, unsupported) | |
| - core.ai_service.stream_response | |
| - core.vector_store.embed_and_store / retrieve | |
| - core.pii_guard.redact_pii | |
| """ | |
| import io | |
| import sys | |
| import os | |
| import unittest | |
| from unittest.mock import patch, MagicMock | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # 1. core.file_service._chunk | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| class TestChunk(unittest.TestCase): | |
| def setUp(self): | |
| from core.file_service import _chunk | |
| self.chunk = _chunk | |
| def test_short_text_single_chunk(self): | |
| text = "Hello world" | |
| result = self.chunk(text, size=500, overlap=50) | |
| self.assertEqual(result, "Hello world") | |
| def test_multiple_chunks_separator(self): | |
| text = "A" * 600 | |
| result = self.chunk(text, size=500, overlap=50) | |
| self.assertIn("---", result, "Chunks must be separated by ---") | |
| parts = result.split("\n\n---\n\n") | |
| self.assertEqual(len(parts), 2) | |
| def test_overlap_produces_shared_content(self): | |
| text = "X" * 100 | |
| result = self.chunk(text, size=60, overlap=20) | |
| parts = result.split("\n\n---\n\n") | |
| self.assertGreater(len(parts), 1) | |
| def test_empty_string(self): | |
| result = self.chunk("", size=500, overlap=50) | |
| self.assertEqual(result, "") | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # 2. core.file_service.extract_text โ .txt path | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| class TestExtractTxt(unittest.TestCase): | |
| def setUp(self): | |
| from core.file_service import extract_text | |
| self.extract_text = extract_text | |
| def _make_file(self, name: str, content: bytes): | |
| f = io.BytesIO(content) | |
| f.name = name | |
| return f | |
| def test_txt_extraction(self): | |
| content = b"Hello from a text file." | |
| f = self._make_file("sample.txt", content) | |
| result = self.extract_text(f) | |
| self.assertIn("Hello from a text file.", result) | |
| def test_txt_long_content_is_chunked(self): | |
| content = ("word " * 300).encode("utf-8") # ~1500 chars โ multiple chunks | |
| f = self._make_file("long.txt", content) | |
| result = self.extract_text(f) | |
| self.assertIn("---", result) | |
| def test_unsupported_type_raises(self): | |
| f = self._make_file("doc.docx", b"binary data") | |
| with self.assertRaises(ValueError): | |
| self.extract_text(f) | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # 3. core.file_service.extract_text โ .pdf path (mocked via fitz) | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| class TestExtractPdf(unittest.TestCase): | |
| def setUp(self): | |
| from core.file_service import extract_text | |
| self.extract_text = extract_text | |
| def test_pdf_extraction(self): | |
| fake_page = MagicMock() | |
| fake_page.get_text.return_value = "PDF page text here." | |
| fake_doc = MagicMock() | |
| fake_doc.__iter__ = MagicMock(return_value=iter([fake_page])) | |
| f = io.BytesIO(b"%PDF-1.4 fake") | |
| f.name = "document.pdf" | |
| with patch("core.file_service.fitz.open", return_value=fake_doc): | |
| result = self.extract_text(f) | |
| self.assertIn("PDF page text here.", result) | |
| def test_pdf_empty_pages(self): | |
| fake_page = MagicMock() | |
| fake_page.get_text.return_value = "" # fitz returns "" for blank pages | |
| fake_doc = MagicMock() | |
| fake_doc.__iter__ = MagicMock(return_value=iter([fake_page])) | |
| f = io.BytesIO(b"%PDF-1.4 fake") | |
| f.name = "empty.pdf" | |
| with patch("core.file_service.fitz.open", return_value=fake_doc): | |
| result = self.extract_text(f) | |
| self.assertEqual(result, "") | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # 4. core.ai_service.stream_response | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| class TestStreamResponse(unittest.TestCase): | |
| def _make_chunk(self, text): | |
| chunk = MagicMock() | |
| chunk.choices[0].delta.content = text | |
| return chunk | |
| def test_yields_content_tokens(self): | |
| from core.ai_service import stream_response | |
| fake_chunks = [self._make_chunk("Hello"), self._make_chunk(" world")] | |
| mock_stream = MagicMock() | |
| mock_stream.__iter__ = MagicMock(return_value=iter(fake_chunks)) | |
| mock_client = MagicMock() | |
| mock_client.chat.completions.create.return_value = mock_stream | |
| with patch("core.ai_service.OpenAI", return_value=mock_client): | |
| tokens = list(stream_response("hi")) | |
| self.assertEqual(tokens, ["Hello", " world"]) | |
| def test_none_content_skipped(self): | |
| """Chunks where delta.content is None must not be yielded.""" | |
| from core.ai_service import stream_response | |
| chunk_none = MagicMock() | |
| chunk_none.choices[0].delta.content = None | |
| chunk_real = self._make_chunk("data") | |
| mock_stream = MagicMock() | |
| mock_stream.__iter__ = MagicMock(return_value=iter([chunk_none, chunk_real])) | |
| mock_client = MagicMock() | |
| mock_client.chat.completions.create.return_value = mock_stream | |
| with patch("core.ai_service.OpenAI", return_value=mock_client): | |
| tokens = list(stream_response("hi")) | |
| self.assertEqual(tokens, ["data"]) | |
| def test_context_becomes_second_system_message(self): | |
| """Context must appear as a second system message, after SYSTEM_PROMPT.""" | |
| from core.ai_service import stream_response | |
| mock_stream = MagicMock() | |
| mock_stream.__iter__ = MagicMock(return_value=iter([self._make_chunk("ok")])) | |
| mock_client = MagicMock() | |
| mock_client.chat.completions.create.return_value = mock_stream | |
| with patch("core.ai_service.OpenAI", return_value=mock_client): | |
| list(stream_response("hello", context="some law context")) | |
| call_kwargs = mock_client.chat.completions.create.call_args[1] | |
| messages = call_kwargs["messages"] | |
| self.assertEqual(messages[0]["role"], "system") | |
| self.assertIn("some law context", messages[1]["content"]) | |
| self.assertEqual(messages[2]["role"], "user") | |
| self.assertEqual(messages[2]["content"], "hello") | |
| def test_no_context_sends_system_and_user(self): | |
| """Without context, exactly system prompt + user message are sent.""" | |
| from core.ai_service import stream_response | |
| mock_stream = MagicMock() | |
| mock_stream.__iter__ = MagicMock(return_value=iter([self._make_chunk("ok")])) | |
| mock_client = MagicMock() | |
| mock_client.chat.completions.create.return_value = mock_stream | |
| with patch("core.ai_service.OpenAI", return_value=mock_client): | |
| list(stream_response("hello")) | |
| call_kwargs = mock_client.chat.completions.create.call_args[1] | |
| messages = call_kwargs["messages"] | |
| self.assertEqual(len(messages), 2) | |
| self.assertEqual(messages[0]["role"], "system") | |
| self.assertEqual(messages[1]["role"], "user") | |
| self.assertEqual(messages[1]["content"], "hello") | |
| def test_stream_flag_is_true(self): | |
| """The OpenAI call must always use stream=True.""" | |
| from core.ai_service import stream_response | |
| mock_stream = MagicMock() | |
| mock_stream.__iter__ = MagicMock(return_value=iter([])) | |
| mock_client = MagicMock() | |
| mock_client.chat.completions.create.return_value = mock_stream | |
| with patch("core.ai_service.OpenAI", return_value=mock_client): | |
| list(stream_response("test")) | |
| call_kwargs = mock_client.chat.completions.create.call_args[1] | |
| self.assertTrue(call_kwargs.get("stream"), "stream must be True") | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # 5. core.vector_store.embed_and_store + retrieve | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| class TestVectorStore(unittest.TestCase): | |
| def _fake_embed(self, texts, is_query=False): | |
| """Returns deterministic unit vectors without loading the real model.""" | |
| import numpy as np | |
| rng = np.random.default_rng(seed=42) | |
| vecs = rng.random((len(texts), 1024)).astype("float32") | |
| norms = np.linalg.norm(vecs, axis=1, keepdims=True) | |
| return vecs / norms | |
| def test_embed_and_store_then_retrieve_returns_strings(self): | |
| import core.vector_store as vs | |
| chunks = ["cats are mammals", "dogs bark loudly", "fish live in water"] | |
| with patch("core.vector_store._embed", side_effect=self._fake_embed): | |
| vs.embed_and_store(chunks) | |
| results = vs.retrieve("mammals", k=2) | |
| self.assertIsInstance(results, list) | |
| self.assertEqual(len(results), 2) | |
| self.assertTrue(all(isinstance(r, str) for r in results)) | |
| self.assertTrue(all(r in chunks for r in results)) | |
| def test_retrieve_k_capped_to_index_size(self): | |
| import core.vector_store as vs | |
| chunks = ["only one chunk"] | |
| with patch("core.vector_store._embed", side_effect=self._fake_embed): | |
| vs.embed_and_store(chunks) | |
| results = vs.retrieve("query", k=10) | |
| self.assertEqual(len(results), 1) | |
| def test_retrieve_before_store_returns_empty(self): | |
| import core.vector_store as vs | |
| vs._index = None | |
| vs._chunks = [] | |
| results = vs.retrieve("anything") | |
| self.assertEqual(results, []) | |
| def test_embed_and_store_resets_previous_index(self): | |
| import core.vector_store as vs | |
| with patch("core.vector_store._embed", side_effect=self._fake_embed): | |
| vs.embed_and_store(["first batch"]) | |
| vs.embed_and_store(["second batch A", "second batch B"]) | |
| results = vs.retrieve("query", k=5) | |
| self.assertEqual(len(results), 2) | |
| self.assertTrue(all("second" in r for r in results)) | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # 6. core.pii_guard.redact_pii | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| class TestPiiGuard(unittest.TestCase): | |
| def setUp(self): | |
| from core.pii_guard import redact_pii | |
| self.redact_pii = redact_pii | |
| def test_saudi_id_redacted(self): | |
| text = "ุฑูู ุงููููุฉ: 1234567890" | |
| result, count = self.redact_pii(text) | |
| self.assertNotIn("1234567890", result) | |
| self.assertIn("[ุฑูู ูููุฉ]", result) | |
| self.assertEqual(count, 1) | |
| def test_iqama_id_redacted(self): | |
| text = "ุฑูู ุงูุฅูุงู ุฉ: 2987654321" | |
| result, count = self.redact_pii(text) | |
| self.assertNotIn("2987654321", result) | |
| self.assertIn("[ุฑูู ูููุฉ]", result) | |
| self.assertEqual(count, 1) | |
| def test_iban_redacted(self): | |
| text = "ุฑูู IBAN: SA1234567890123456789012" | |
| result, count = self.redact_pii(text) | |
| self.assertNotIn("SA1234567890123456789012", result) | |
| self.assertIn("[ุฑูู IBAN]", result) | |
| self.assertEqual(count, 1) | |
| def test_no_pii_returns_unchanged(self): | |
| text = "ูุฐุง ูุต ุนุงุฏู ุจุฏูู ุจูุงูุงุช ุญุณุงุณุฉ" | |
| result, count = self.redact_pii(text) | |
| self.assertEqual(result, text) | |
| self.assertEqual(count, 0) | |
| def test_multiple_pii_all_redacted(self): | |
| text = "ูููุฉ: 1234567890 ูIBAN: SA0987654321098765432109" | |
| result, count = self.redact_pii(text) | |
| self.assertEqual(count, 2) | |
| self.assertNotIn("1234567890", result) | |
| self.assertNotIn("SA0987654321098765432109", result) | |
| def test_id_inside_longer_number_not_matched(self): | |
| """An 11-digit number must not be treated as a Saudi ID.""" | |
| text = "ุงูุฑูู : 12345678901" | |
| result, count = self.redact_pii(text) | |
| self.assertEqual(count, 0) | |
| self.assertEqual(result, text) | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # 6. app.py โ show_sources widget keys must stay unique across messages | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| class TestShowSourcesUniqueKeys(unittest.TestCase): | |
| """Regression test for StreamlitDuplicateElementId when two chat | |
| turns share the same uploaded file or the same retrieved article.""" | |
| def test_overlapping_sources_across_messages_do_not_crash(self): | |
| from streamlit.testing.v1 import AppTest | |
| app_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "app.py") | |
| at = AppTest.from_file(app_path) | |
| same_chunk = ("ูุต ุงูู ุงุฏุฉ ุงูุฃููู ู ู ูุธุงู ุงูุนู ู", 0.91) | |
| same_file_text = "ูุต ู ูู ุงูุนูุฏ ุงูู ุฑููุน ููุงุฎุชุจุงุฑ" | |
| at.session_state["messages"] = [ | |
| {"role": "user", "content": "ุณุคุงู 1"}, | |
| {"role": "assistant", "content": "ุฌูุงุจ 1", "scored_chunks": [same_chunk], "file_text": same_file_text}, | |
| {"role": "user", "content": "ุณุคุงู 2"}, | |
| {"role": "assistant", "content": "ุฌูุงุจ 2", "scored_chunks": [same_chunk], "file_text": same_file_text}, | |
| ] | |
| at.run(timeout=60) | |
| self.assertEqual(at.exception, [], f"App crashed: {[e.value for e in at.exception]}") | |
| if __name__ == "__main__": | |
| unittest.main(verbosity=2) | |