|
| 1 | +########################################################################## |
| 2 | +# |
| 3 | +# pgAdmin 4 - PostgreSQL Tools |
| 4 | +# |
| 5 | +# Copyright (C) 2013 - 2026, The pgAdmin Development Team |
| 6 | +# This software is released under the PostgreSQL Licence |
| 7 | +# |
| 8 | +########################################################################## |
| 9 | + |
| 10 | +"""Test the SQL formatting functionality in Explain PostgreSQL module.""" |
| 11 | +import json |
| 12 | +import unittest |
| 13 | +from unittest.mock import patch |
| 14 | +from pgadmin.utils.route import BaseTestGenerator |
| 15 | + |
| 16 | + |
| 17 | +class TestFormatSQLFunctionality(BaseTestGenerator): |
| 18 | + """Test the SQL formatting functionality.""" |
| 19 | + |
| 20 | + scenarios = [ |
| 21 | + ('Test format SQL endpoint', dict( |
| 22 | + url='/expl_pgsql/formatSQL', |
| 23 | + method='POST', |
| 24 | + data={'query_src': 'SELECT * FROM test_table WHERE id = 1;'}, |
| 25 | + expected_success=True |
| 26 | + )), |
| 27 | + ('Test format SQL with invalid data', dict( |
| 28 | + url='/expl_pgsql/formatSQL', |
| 29 | + method='POST', |
| 30 | + data='invalid_json', |
| 31 | + expected_success=False |
| 32 | + )) |
| 33 | + ] |
| 34 | + |
| 35 | + def runTest(self): |
| 36 | + """Run test case.""" |
| 37 | + if self.expected_success: |
| 38 | + response = self.tester.post( |
| 39 | + self.url, |
| 40 | + data=json.dumps(self.data), |
| 41 | + content_type='application/json' |
| 42 | + ) |
| 43 | + self.assertEqual(response.status_code, 200) |
| 44 | + response_data = json.loads(response.data.decode('utf-8')) |
| 45 | + self.assertTrue(response_data['success']) |
| 46 | + else: |
| 47 | + response = self.tester.post( |
| 48 | + self.url, |
| 49 | + data=self.data, |
| 50 | + content_type='application/json' |
| 51 | + ) |
| 52 | + self.assertEqual(response.status_code, 200) |
| 53 | + response_data = json.loads(response.data.decode('utf-8')) |
| 54 | + self.assertFalse(response_data['success']) |
| 55 | + |
| 56 | + |
| 57 | + if self.method == 'POST': |
| 58 | + # Mock the preference values |
| 59 | + with patch('pgadmin.tools.expl_pgsql.get_preference_value') as mock_pref: |
| 60 | + mock_pref.return_value = 'https://explain.tensor.ru' |
| 61 | + |
| 62 | + # Mock the URL validation |
| 63 | + with patch('pgadmin.tools.expl_pgsql.is_valid_url') as mock_valid: |
| 64 | + mock_valid.return_value = True |
| 65 | + |
| 66 | + # Mock the HTTP request |
| 67 | + with patch('pgadmin.tools.expl_pgsql.send_post_request') as mock_send: |
| 68 | + mock_send.return_value = (False, json.dumps({ |
| 69 | + 'btf_query': '<span class=\'sql_keyword\'>SELECT</span>\\n <span class=\'sql_asterisk\'>*</span>\\n<span class=\'sql_keyword\'>FROM</span>\\n <span class=\'sql_relation\'>test_table</span>\\n<span class=\'sql_keyword\'>WHERE</span>\\n <span class=\'sql_column\'>id</span> = <span class=\'sql_const\'>1</span>;', |
| 70 | + 'btf_query_text': 'SELECT\\n\\t*\\nFROM\\n\\ttest_table\\nWHERE\\n\\tid = 1;' |
| 71 | + })) |
| 72 | + |
| 73 | + if self.expected_success: |
| 74 | + response = self.tester.post( |
| 75 | + self.url, |
| 76 | + data=json.dumps(self.data), |
| 77 | + content_type='application/json' |
| 78 | + ) |
| 79 | + self.assertEqual(response.status_code, 200) |
| 80 | + response_data = json.loads(response.data.decode('utf-8')) |
| 81 | + self.assertTrue(response_data['success']) |
| 82 | + else: |
| 83 | + response = self.tester.post( |
| 84 | + self.url, |
| 85 | + data=self.data, |
| 86 | + content_type='application/json' |
| 87 | + ) |
| 88 | + self.assertEqual(response.status_code, 200) |
| 89 | + response_data = json.loads(response.data.decode('utf-8')) |
| 90 | + self.assertFalse(response_data['success']) |
0 commit comments