Spaces:
Sleeping
Sleeping
| import unittest | |
| from unittest.mock import MagicMock, patch | |
| import sys | |
| import os | |
| # Add project root to path | |
| sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) | |
| # Need to mock the imports inside oracle_module BEFORE importing it if they are top-level? | |
| # oracle_module imports them inside the function 'get_oracle_data', so we can mock sys.modules or patch inside the test. | |
| # However, oracle_module sets sys.path to daily_psalms_api. | |
| # Ideally we test logic without relying on actual daily_psalms_api files if we can mock them. | |
| class TestOracleParity(unittest.TestCase): | |
| def setUp(self): | |
| # We need to ensure extensions of sys.path don't break things | |
| self.original_sys_path = sys.path.copy() | |
| def tearDown(self): | |
| sys.path = self.original_sys_path | |
| # Removed invalid decorators | |
| def test_legacy_calls(self): | |
| """Verify that specific legacy modules are called for each book.""" | |
| # We need to mock the imports INSIDE get_oracle_data. | |
| # We will use a context manager to patch sys.modules during the call. | |
| mock_torah = MagicMock() | |
| mock_quran = MagicMock() | |
| mock_bible = MagicMock() | |
| mock_utils = MagicMock() | |
| mock_gematria = MagicMock() | |
| # Setup specific returns | |
| mock_utils.date_to_words.return_value = "One One Two Thousand" | |
| mock_gematria.calculate_gematria.return_value = 100 | |
| # Mock torah return | |
| mock_torah.process_json_files.return_value = [{"result_text": "Hebrew", "translated_text": "English"}] | |
| # Mock bible return | |
| # bible.process_json_files returns a list of results | |
| mock_bible.process_json_files.return_value = [{"round": 1, "result_text": "MatthewText"}] | |
| # Mock quran return | |
| # quran.get_first_els_result_quran returns a dict | |
| mock_quran.get_first_els_result_quran.return_value = {"result_text": "QuranText"} | |
| modules = { | |
| 'torah': mock_torah, | |
| 'quran': mock_quran, | |
| 'bible': mock_bible, | |
| 'utils': mock_utils, | |
| 'gematria': mock_gematria, | |
| } | |
| # We also need to mock sqlite3 to avoid DB errors and return valid matches | |
| with patch.dict(sys.modules, modules), \ | |
| patch('sqlite3.connect') as mock_sql: | |
| # Setup SQL Mock | |
| mock_conn = MagicMock() | |
| mock_cursor = MagicMock() | |
| mock_sql.return_value.__enter__.return_value = mock_conn | |
| mock_conn.cursor.return_value = mock_cursor | |
| # We need to handle multiple execute calls for different DBs. | |
| # 1. Torah/Psalm (gematria.db) | |
| # 2. Revelation (bible.db) | |
| # 3. Sura (abjad.db) | |
| # For simplicity, we just return a valid row for ALL queries. | |
| # Row format depends on query. | |
| # Oracle code handles 4 or 5 columns. | |
| # Let's return 5 columns: words, trans, book, ch, vs | |
| mock_cursor.fetchone.return_value = ("Original", "Translation", "Book", 1, 1) | |
| import oracle_module | |
| res = oracle_module.get_oracle_data("Test", "Topic", "2025-01-01") | |
| # VERIFY CALLS | |
| # 1. Torah (Psalm) Check | |
| # adjusted_sum calculation: 100 + (100/137...) = 101 approx. | |
| # Verify torah.process_json_files called | |
| self.assertTrue(mock_torah.process_json_files.called, "Torah module should be called for Psalms") | |
| # 2. Matthew (Revelation) Check | |
| self.assertTrue(mock_bible.process_json_files.called, "Bible module should be called for Matthew/Revelation") | |
| args, _ = mock_bible.process_json_files.call_args | |
| self.assertEqual(args[0], 40, "Should search Book 40 (Matthew)") | |
| # 3. Quran (Sura) Check | |
| self.assertTrue(mock_quran.get_first_els_result_quran.called, "Quran module should be called for Sura") | |
| # VERIFY OUTPUT STRUCTURE | |
| self.assertNotIn("els_revelation", res, "els_revelation should NOT be in output") | |
| self.assertIn("wisdom_nodes", res) | |
| self.assertEqual(len(res["wisdom_nodes"]), 3, "Should have 3 wisdom nodes (Psalm, Sura, Rev)") | |
| for node in res["wisdom_nodes"]: | |
| self.assertIn("english", node, "Node must have english translation") | |
| self.assertNotEqual(node["english"], "...", "Translation should not be empty ellipsis if mocked correctly") | |
| if __name__ == '__main__': | |
| unittest.main() | |