| 
									
										
										
										
											2024-09-30 02:56:59 +02:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | # pylint: disable=missing-module-docstring | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from unittest.mock import MagicMock, Mock | 
					
						
							| 
									
										
										
											
												[fix] unit tests: call searx.search.initialize in test's setUp
Depending on the order the unit tests are executed, the searx.search module is
initalized or not, issue reported in [1]::
    Traceback (most recent call last):
      File "searxng/tests/unit/test_results.py", line 72, in test_result_merge_by_title
        self.container.extend('stract', [fake_result(engine='stract', title='short title')])
      File "searxng/searx/results.py", line 243, in extend
        histogram_observe(standard_result_count, 'engine', engine_name, 'result', 'count')
      File "searxng/searx/metrics/__init__.py", line 49, in histogram_observe
        histogram_storage.get(*args).observe(duration)
        ^^^^^^^^^^^^^^^^^^^^^
      AttributeError: 'NoneType' object has no attribute 'get'
To ensure that the searx.search module is initialized, the
- searx.engines.load_engines is replace by
- searx.search.initialize
[1] https://github.com/searxng/searxng/pull/3932#discussion_r1822406569
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
											
										 
											2024-10-30 13:16:31 +01:00
										 |  |  | from searx.engines import mariadb_server | 
					
						
							| 
									
										
										
										
											2024-09-30 02:56:59 +02:00
										 |  |  | from tests import SearxTestCase | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MariadbServerTests(SearxTestCase):  # pylint: disable=missing-class-docstring | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_init_no_query_str_raises(self): | 
					
						
							|  |  |  |         self.assertRaises(ValueError, lambda: mariadb_server.init({})) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_init_non_select_raises(self): | 
					
						
							|  |  |  |         self.assertRaises(ValueError, lambda: mariadb_server.init({'query_str': 'foobar'})) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_search_returns_results(self): | 
					
						
							|  |  |  |         test_string = 'FOOBAR' | 
					
						
							|  |  |  |         cursor_mock = MagicMock() | 
					
						
							|  |  |  |         with cursor_mock as setup:  # pylint: disable=not-context-manager | 
					
						
							|  |  |  |             setup.__iter__ = Mock(return_value=iter([{test_string, 1}])) | 
					
						
							|  |  |  |             setup.description = [[test_string]] | 
					
						
							|  |  |  |         conn_mock = Mock() | 
					
						
							|  |  |  |         conn_mock.cursor.return_value = cursor_mock | 
					
						
							|  |  |  |         mariadb_server._connection = conn_mock  # pylint: disable=protected-access | 
					
						
							|  |  |  |         results = mariadb_server.search(test_string, {'pageno': 1}) | 
					
						
							|  |  |  |         self.assertEqual(1, len(results)) | 
					
						
							|  |  |  |         self.assertIn(test_string, results[0]) | 
					
						
							|  |  |  |         self.assertEqual(mariadb_server.result_template, results[0]['template']) |