Skip to content

Commit 17d8a5d

Browse files
WaVEVtimgraham
authored andcommitted
add dbshell tests to CI
1 parent 19f8774 commit 17d8a5d

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

.github/workflows/test-python.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ jobs:
6363
dates
6464
datetimes
6565
db_functions
66+
dbshell_
6667
delete
6768
delete_regress
6869
empty

tests/dbshell_/__init__.py

Whitespace-only changes.

tests/dbshell_/tests.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import signal
2+
from unittest import mock
3+
4+
from django.db import connection
5+
from django.test import SimpleTestCase
6+
7+
from django_mongodb.client import DatabaseClient
8+
9+
10+
class MongoDbshellTests(SimpleTestCase):
11+
def settings_to_cmd_args_env(self, settings_dict, parameters=None):
12+
if parameters is None:
13+
parameters = []
14+
return DatabaseClient.settings_to_cmd_args_env(settings_dict, parameters)
15+
16+
def test_fails_with_keyerror_on_incomplete_config(self):
17+
with self.assertRaises(KeyError):
18+
self.settings_to_cmd_args_env({})
19+
20+
def test_basic_params_specified_in_settings(self):
21+
for options_parameters in [(None, None, None), ("value1", "value2", True)]:
22+
with self.subTest(keys=options_parameters):
23+
authentication_database, authentication_mechanism, retry_writes = options_parameters
24+
if authentication_database is not None:
25+
expected_args = [
26+
"mongosh",
27+
"--host",
28+
"somehost",
29+
"--port",
30+
444,
31+
"--username",
32+
"someuser",
33+
"--password",
34+
"somepassword",
35+
"--retryWrites",
36+
"true",
37+
"somedbname",
38+
]
39+
else:
40+
expected_args = [
41+
"mongosh",
42+
"--host",
43+
"somehost",
44+
"--port",
45+
444,
46+
"--username",
47+
"someuser",
48+
"--password",
49+
"somepassword",
50+
"somedbname",
51+
]
52+
53+
self.assertEqual(
54+
self.settings_to_cmd_args_env(
55+
{
56+
"NAME": "somedbname",
57+
"USER": "someuser",
58+
"PASSWORD": "somepassword",
59+
"HOST": "somehost",
60+
"PORT": 444,
61+
"OPTIONS": {
62+
"authenticationDatabase": authentication_database,
63+
"authenticationMechanism": authentication_mechanism,
64+
"retryWrites": retry_writes,
65+
},
66+
}
67+
),
68+
(expected_args, None),
69+
)
70+
71+
def test_options_override_settings_proper_values(self):
72+
settings_port = 444
73+
options_port = 555
74+
self.assertNotEqual(settings_port, options_port, "test pre-req")
75+
expected_args = [
76+
"mongosh",
77+
"--host",
78+
"settinghost",
79+
"--port",
80+
444,
81+
"--username",
82+
"settinguser",
83+
"--password",
84+
"settingpassword",
85+
"settingdbname",
86+
]
87+
expected_env = None
88+
89+
self.assertEqual(
90+
self.settings_to_cmd_args_env(
91+
{
92+
"NAME": "settingdbname",
93+
"USER": "settinguser",
94+
"PASSWORD": "settingpassword",
95+
"HOST": "settinghost",
96+
"PORT": settings_port,
97+
"OPTIONS": {"port": options_port},
98+
}
99+
),
100+
(expected_args, expected_env),
101+
)
102+
103+
def test_sigint_handler(self):
104+
"""SIGINT is ignored in Python and passed to Mongodb to abort queries."""
105+
106+
def _mock_subprocess_run(*args, **kwargs): # noqa: ARG001
107+
handler = signal.getsignal(signal.SIGINT)
108+
self.assertEqual(handler, signal.SIG_IGN)
109+
110+
sigint_handler = signal.getsignal(signal.SIGINT)
111+
# The default handler isn't SIG_IGN.
112+
self.assertNotEqual(sigint_handler, signal.SIG_IGN)
113+
with mock.patch("subprocess.run", new=_mock_subprocess_run):
114+
connection.client.runshell([])
115+
# dbshell restores the original handler.
116+
self.assertEqual(sigint_handler, signal.getsignal(signal.SIGINT))

0 commit comments

Comments
 (0)