Skip to content

Commit 267ca71

Browse files
WaVEVtimgraham
authored andcommitted
add dbshell tests
1 parent fdcd488 commit 267ca71

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

tests/dbshell/test_mongodb.py

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

0 commit comments

Comments
 (0)