forked from mongodb/mongo-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_uri_spec.py
More file actions
177 lines (142 loc) · 6.43 KB
/
test_uri_spec.py
File metadata and controls
177 lines (142 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Copyright 2011-2015 MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Test that the pymongo.uri_parser module is compliant with the connection
string and uri options specifications."""
import json
import os
import sys
import warnings
sys.path[0:0] = [""]
from pymongo.common import INTERNAL_URI_OPTION_NAME_MAP, validate
from pymongo.compression_support import _HAVE_SNAPPY
from pymongo.uri_parser import parse_uri
from test import clear_warning_registry, unittest
CONN_STRING_TEST_PATH = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
os.path.join('connection_string', 'test'))
URI_OPTIONS_TEST_PATH = os.path.join(
os.path.dirname(os.path.realpath(__file__)), 'uri_options')
TEST_DESC_SKIP_LIST = [
"Valid options specific to single-threaded drivers are parsed correctly",
"Invalid serverSelectionTryOnce causes a warning"]
class TestAllScenarios(unittest.TestCase):
def setUp(self):
clear_warning_registry()
def get_error_message_template(expected, artefact):
return "%s %s for test '%s'" % (
"Expected" if expected else "Unexpected", artefact, "%s")
def run_scenario_in_dir(target_workdir):
def workdir_context_decorator(func):
def modified_test_scenario(*args, **kwargs):
original_workdir = os.getcwd()
os.chdir(target_workdir)
func(*args, **kwargs)
os.chdir(original_workdir)
return modified_test_scenario
return workdir_context_decorator
def create_test(test, test_workdir):
def run_scenario(self):
compressors = (test.get('options') or {}).get('compressors', [])
if 'snappy' in compressors and not _HAVE_SNAPPY:
self.skipTest('This test needs the snappy module.')
valid = True
warning = False
with warnings.catch_warnings(record=True) as ctx:
warnings.simplefilter('always')
try:
options = parse_uri(test['uri'], warn=True)
except Exception:
valid = False
else:
warning = len(ctx) > 0
expected_valid = test.get('valid', True)
self.assertEqual(
valid, expected_valid, get_error_message_template(
not expected_valid, "error") % test['description'])
if expected_valid:
expected_warning = test.get('warning', False)
self.assertEqual(
warning, expected_warning, get_error_message_template(
expected_warning, "warning") % test['description'])
# Compare hosts and port.
if test['hosts'] is not None:
self.assertEqual(
len(test['hosts']), len(options['nodelist']),
"Incorrect number of hosts parsed from URI")
for exp, actual in zip(test['hosts'],
options['nodelist']):
self.assertEqual(exp['host'], actual[0],
"Expected host %s but got %s"
% (exp['host'], actual[0]))
if exp['port'] is not None:
self.assertEqual(exp['port'], actual[1],
"Expected port %s but got %s"
% (exp['port'], actual))
# Compare auth options.
auth = test['auth']
if auth is not None:
auth['database'] = auth.pop('db') # db == database
# Special case for PyMongo's collection parsing.
if options.get('collection') is not None:
options['database'] += "." + options['collection']
for elm in auth:
if auth[elm] is not None:
self.assertEqual(auth[elm], options[elm],
"Expected %s but got %s"
% (auth[elm], options[elm]))
# Compare URI options.
err_msg = "For option %s expected %s but got %s"
if test['options']:
opts = options['options']
for opt in test['options']:
lopt = opt.lower()
optname = INTERNAL_URI_OPTION_NAME_MAP.get(lopt, lopt)
if opts.get(optname) is not None:
if opts[optname] == test['options'][opt]:
expected_value = test['options'][opt]
else:
expected_value = validate(
lopt, test['options'][opt])[1]
self.assertEqual(
opts[optname], expected_value,
err_msg % (opt, expected_value, opts[optname],))
else:
self.fail(
"Missing expected option %s" % (opt,))
return run_scenario_in_dir(test_workdir)(run_scenario)
def create_tests(test_path):
for dirpath, _, filenames in os.walk(test_path):
dirname = os.path.split(dirpath)
dirname = os.path.split(dirname[-2])[-1] + '_' + dirname[-1]
for filename in filenames:
if not filename.endswith('.json'):
# skip everything that is not a test specification
continue
with open(os.path.join(dirpath, filename)) as scenario_stream:
scenario_def = json.load(scenario_stream)
for testcase in scenario_def['tests']:
dsc = testcase['description']
if dsc in TEST_DESC_SKIP_LIST:
print("Skipping test '%s'" % dsc)
continue
testmethod = create_test(testcase, dirpath)
testname = 'test_%s_%s_%s' % (
dirname, os.path.splitext(filename)[0],
str(dsc).replace(' ', '_'))
testmethod.__name__ = testname
setattr(TestAllScenarios, testmethod.__name__, testmethod)
for test_path in [CONN_STRING_TEST_PATH, URI_OPTIONS_TEST_PATH]:
create_tests(test_path)
if __name__ == "__main__":
unittest.main()