| 
									
										
										
										
											2024-08-20 05:02:06 +02:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | # pylint: disable=missing-module-docstring | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												[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
										 |  |  | import logging | 
					
						
							| 
									
										
										
										
											2024-08-20 05:02:06 +02:00
										 |  |  | from datetime import datetime | 
					
						
							|  |  |  | from unittest.mock import Mock | 
					
						
							|  |  |  | from requests import HTTPError | 
					
						
							| 
									
										
										
										
											2024-09-01 00:04:37 +02:00
										 |  |  | from parameterized import parameterized | 
					
						
							| 
									
										
										
											
												[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
										 |  |  | import searx.search | 
					
						
							|  |  |  | import searx.engines | 
					
						
							| 
									
										
										
										
											2024-08-20 05:02:06 +02:00
										 |  |  | from tests import SearxTestCase | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TinEyeTests(SearxTestCase):  # pylint: disable=missing-class-docstring | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							| 
									
										
										
											
												[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
										 |  |  |         searx.search.initialize( | 
					
						
							|  |  |  |             [{'name': 'tineye', 'engine': 'tineye', 'shortcut': 'tin', 'timeout': 9.0, 'disabled': True}] | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.tineye = searx.engines.engines['tineye'] | 
					
						
							|  |  |  |         self.tineye.logger.setLevel(logging.CRITICAL) | 
					
						
							| 
									
										
										
										
											2024-08-20 05:02:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def tearDown(self): | 
					
						
							| 
									
										
										
											
												[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
										 |  |  |         searx.search.load_engines([]) | 
					
						
							| 
									
										
										
										
											2024-08-20 05:02:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_status_code_raises(self): | 
					
						
							|  |  |  |         response = Mock() | 
					
						
							|  |  |  |         response.status_code = 401 | 
					
						
							|  |  |  |         response.raise_for_status.side_effect = HTTPError() | 
					
						
							| 
									
										
										
											
												[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
										 |  |  |         self.assertRaises(HTTPError, lambda: self.tineye.response(response)) | 
					
						
							| 
									
										
										
										
											2024-08-20 05:02:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-01 00:04:37 +02:00
										 |  |  |     @parameterized.expand([(400), (422)]) | 
					
						
							|  |  |  |     def test_returns_empty_list(self, status_code): | 
					
						
							| 
									
										
										
										
											2024-08-20 05:02:06 +02:00
										 |  |  |         response = Mock() | 
					
						
							|  |  |  |         response.json.return_value = {} | 
					
						
							| 
									
										
										
										
											2024-09-01 00:04:37 +02:00
										 |  |  |         response.status_code = status_code | 
					
						
							| 
									
										
										
										
											2024-08-20 05:02:06 +02:00
										 |  |  |         response.raise_for_status.side_effect = HTTPError() | 
					
						
							| 
									
										
										
											
												[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
										 |  |  |         results = self.tineye.response(response) | 
					
						
							| 
									
										
										
										
											2024-08-20 05:02:06 +02:00
										 |  |  |         self.assertEqual(0, len(results)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_logs_format_for_422(self): | 
					
						
							|  |  |  |         response = Mock() | 
					
						
							|  |  |  |         response.json.return_value = {"suggestions": {"key": "Invalid image URL"}} | 
					
						
							|  |  |  |         response.status_code = 422 | 
					
						
							|  |  |  |         response.raise_for_status.side_effect = HTTPError() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												[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
										 |  |  |         with self.assertLogs(self.tineye.logger) as assert_logs_context: | 
					
						
							|  |  |  |             self.tineye.response(response) | 
					
						
							|  |  |  |             self.assertIn(self.tineye.FORMAT_NOT_SUPPORTED, ','.join(assert_logs_context.output)) | 
					
						
							| 
									
										
										
										
											2024-08-20 05:02:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_logs_signature_for_422(self): | 
					
						
							|  |  |  |         response = Mock() | 
					
						
							|  |  |  |         response.json.return_value = {"suggestions": {"key": "NO_SIGNATURE_ERROR"}} | 
					
						
							|  |  |  |         response.status_code = 422 | 
					
						
							|  |  |  |         response.raise_for_status.side_effect = HTTPError() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												[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
										 |  |  |         with self.assertLogs(self.tineye.logger) as assert_logs_context: | 
					
						
							|  |  |  |             self.tineye.response(response) | 
					
						
							|  |  |  |             self.assertIn(self.tineye.NO_SIGNATURE_ERROR, ','.join(assert_logs_context.output)) | 
					
						
							| 
									
										
										
										
											2024-08-20 05:02:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_logs_download_for_422(self): | 
					
						
							|  |  |  |         response = Mock() | 
					
						
							|  |  |  |         response.json.return_value = {"suggestions": {"key": "Download Error"}} | 
					
						
							|  |  |  |         response.status_code = 422 | 
					
						
							|  |  |  |         response.raise_for_status.side_effect = HTTPError() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												[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
										 |  |  |         with self.assertLogs(self.tineye.logger) as assert_logs_context: | 
					
						
							|  |  |  |             self.tineye.response(response) | 
					
						
							|  |  |  |             self.assertIn(self.tineye.DOWNLOAD_ERROR, ','.join(assert_logs_context.output)) | 
					
						
							| 
									
										
										
										
											2024-08-20 05:02:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_logs_description_for_400(self): | 
					
						
							|  |  |  |         description = 'There was a problem with that request. Error ID: ad5fc955-a934-43c1-8187-f9a61d301645' | 
					
						
							|  |  |  |         response = Mock() | 
					
						
							|  |  |  |         response.json.return_value = {"suggestions": {"description": [description], "title": "Oops! We're sorry!"}} | 
					
						
							|  |  |  |         response.status_code = 400 | 
					
						
							|  |  |  |         response.raise_for_status.side_effect = HTTPError() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												[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
										 |  |  |         with self.assertLogs(self.tineye.logger) as assert_logs_context: | 
					
						
							|  |  |  |             self.tineye.response(response) | 
					
						
							| 
									
										
										
										
											2024-08-20 05:02:06 +02:00
										 |  |  |             self.assertIn(description, ','.join(assert_logs_context.output)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_crawl_date_parses(self): | 
					
						
							|  |  |  |         date_str = '2020-05-25' | 
					
						
							|  |  |  |         date = datetime.strptime(date_str, '%Y-%m-%d') | 
					
						
							|  |  |  |         response = Mock() | 
					
						
							|  |  |  |         response.json.return_value = { | 
					
						
							|  |  |  |             'matches': [ | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     'backlinks': [ | 
					
						
							|  |  |  |                         { | 
					
						
							|  |  |  |                             'crawl_date': date_str, | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     ] | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         response.status_code = 200 | 
					
						
							| 
									
										
										
											
												[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
										 |  |  |         results = self.tineye.response(response) | 
					
						
							| 
									
										
										
										
											2024-08-20 05:02:06 +02:00
										 |  |  |         self.assertEqual(date, results[0]['publishedDate']) |