Skip to content

Commit 8577c6d

Browse files
committed
add tests
1 parent afa5e4c commit 8577c6d

6 files changed

Lines changed: 308 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
##########################################################################
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 explain plan functionality in Explain PostgreSQL module."""
11+
import json
12+
import unittest
13+
from unittest.mock import patch, MagicMock
14+
from pgadmin.utils.route import BaseTestGenerator
15+
from pgadmin.tools.expl_pgsql import MODULE_NAME
16+
17+
18+
class TestExplainFunctionality(BaseTestGenerator):
19+
"""Test the explain plan functionality."""
20+
21+
scenarios = [
22+
('Test explain plan endpoint', dict(
23+
url='/expl_pgsql/explain',
24+
method='POST',
25+
data={'plan': '{"Plan": {}}', 'query': 'SELECT * FROM test;'},
26+
expected_success=True
27+
)),
28+
('Test explain plan with invalid data', dict(
29+
url='/expl_pgsql/explain',
30+
method='POST',
31+
data='invalid_json',
32+
expected_success=False
33+
))
34+
]
35+
36+
def runTest(self):
37+
"""Run test case."""
38+
if self.method == 'POST':
39+
with patch('pgadmin.tools.expl_pgsql.get_preference_value') as mock_pref:
40+
mock_pref.return_value = 'https://explain.tensor.ru'
41+
42+
with patch('pgadmin.tools.expl_pgsql.is_valid_url') as mock_valid:
43+
mock_valid.return_value = True
44+
45+
with patch('pgadmin.tools.expl_pgsql.send_post_request') as mock_send:
46+
mock_send.return_value = (False, '/explain/12345')
47+
48+
if self.expected_success:
49+
response = self.tester.post(
50+
self.url,
51+
data=json.dumps(self.data),
52+
content_type='application/json'
53+
)
54+
self.assertEqual(response.status_code, 200)
55+
response_data = json.loads(response.data.decode('utf-8'))
56+
self.assertTrue(response_data['success'])
57+
else:
58+
response = self.tester.post(
59+
self.url,
60+
data=self.data,
61+
content_type='application/json'
62+
)
63+
self.assertEqual(response.status_code, 200)
64+
response_data = json.loads(response.data.decode('utf-8'))
65+
self.assertFalse(response_data['success'])
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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'])
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 preferences functionality in Explain PostgreSQL module."""
11+
import unittest
12+
from unittest.mock import patch, MagicMock
13+
from pgadmin.utils.route import BaseTestGenerator
14+
from pgadmin.tools.expl_pgsql import MODULE_NAME
15+
16+
17+
class TestPreferencesFunctionality(BaseTestGenerator):
18+
"""Test the preferences functionality."""
19+
20+
scenarios = [
21+
('Test getting preference value successfully', dict(
22+
pref_return_value='https://explain.tensor.ru',
23+
expected_result='https://explain.tensor.ru',
24+
exception=None
25+
)),
26+
('Test getting empty preference value', dict(
27+
pref_return_value='',
28+
expected_result=None,
29+
exception=None
30+
)),
31+
('Test getting preference value with exception', dict(
32+
pref_return_value=None,
33+
expected_result=None,
34+
exception=Exception("Preference module not found")
35+
))
36+
]
37+
38+
def runTest(self):
39+
"""Run test case"""
40+
mock_app = MagicMock()
41+
mock_app.logger = MagicMock()
42+
with patch('pgadmin.tools.expl_pgsql.Preferences') as mock_prefs, \
43+
patch('pgadmin.tools.expl_pgsql.current_app', mock_app):
44+
45+
if self.exception:
46+
mock_prefs.module.side_effect = self.exception
47+
else:
48+
mock_module = MagicMock()
49+
mock_pref = MagicMock()
50+
mock_pref.get.return_value = self.pref_return_value
51+
mock_module.preference.return_value = mock_pref
52+
mock_prefs.module.return_value = mock_module
53+
54+
from pgadmin.tools.expl_pgsql import get_preference_value
55+
result = get_preference_value('explain_postgresql_api')
56+
self.assertEqual(result, self.expected_result)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 status endpoint 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 TestStatusEndpoint(BaseTestGenerator):
18+
"""Test the status endpoint."""
19+
20+
scenarios = [
21+
('Test status endpoint enabled', dict(
22+
preference_value=True,
23+
expected_enabled=True
24+
)),
25+
('Test status endpoint disabled', dict(
26+
preference_value=False,
27+
expected_enabled=False
28+
))
29+
]
30+
31+
def runTest(self):
32+
"""Run test case."""
33+
with patch('pgadmin.tools.expl_pgsql.get_preference_value') as mock_pref:
34+
mock_pref.return_value = self.preference_value
35+
36+
response = self.tester.get('/expl_pgsql/status')
37+
self.assertEqual(response.status_code, 200)
38+
response_data = json.loads(response.data.decode('utf-8'))
39+
self.assertTrue(response_data['success'])
40+
self.assertEqual(response_data['data']['enabled'], self.expected_enabled)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 URL validation functionality in Explain PostgreSQL module."""
11+
import unittest
12+
from pgadmin.utils.route import BaseTestGenerator
13+
from pgadmin.tools.expl_pgsql import is_valid_url
14+
15+
16+
class TestURLValidation(BaseTestGenerator):
17+
"""Test the URL validation functionality."""
18+
19+
scenarios = [
20+
('Valid HTTPS URL', dict(
21+
url='https://explain.tensor.ru',
22+
expected=True
23+
)),
24+
('Valid HTTP URL', dict(
25+
url='http://explain.tensor.ru',
26+
expected=True
27+
)),
28+
('Invalid FTP URL', dict(
29+
url='ftp://explain.tensor.ru',
30+
expected=False
31+
)),
32+
('Invalid protocol', dict(
33+
url='javascript:alert(1)',
34+
expected=False
35+
)),
36+
('Empty URL', dict(
37+
url='',
38+
expected=False
39+
)),
40+
('None URL', dict(
41+
url=None,
42+
expected=False
43+
))
44+
]
45+
46+
def runTest(self):
47+
"""Run test case."""
48+
result = is_valid_url(self.url)
49+
self.assertEqual(result, self.expected)

0 commit comments

Comments
 (0)