-
Notifications
You must be signed in to change notification settings - Fork 478
/
test_util.py
executable file
·99 lines (77 loc) · 2.95 KB
/
test_util.py
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
# Copyright 2013-2014 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.
"""Tests methods in util.py
"""
import sys
from bson import timestamp
from pymongo import errors
sys.path[0:0] = [""] # noqa
from mongo_connector.util import bson_ts_to_long, long_to_bson_ts, retry_until_ok
from tests import unittest
def err_func(first_error):
"""Helper function for retry_until_ok test
"""
err_func.counter += 1
if err_func.counter == 3:
return True
elif err_func.counter == 2:
raise TypeError
else:
raise first_error
err_func.counter = 0
class TestUtil(unittest.TestCase):
""" Tests the utils
"""
def setUp(self):
err_func.counter = 0
def test_bson_ts_to_long(self):
"""Test bson_ts_to_long and long_to_bson_ts
"""
tstamp = timestamp.Timestamp(0x12345678, 0x90ABCDEF)
self.assertEqual(0x1234567890ABCDEF, bson_ts_to_long(tstamp))
self.assertEqual(long_to_bson_ts(0x1234567890ABCDEF), tstamp)
def test_retry_until_ok(self):
"""Test retry_until_ok
"""
self.assertTrue(retry_until_ok(err_func, errors.ConnectionFailure()))
self.assertEqual(err_func.counter, 3)
def test_retry_until_ok_operation_failure(self):
"""Test retry_until_ok retries on PyMongo OperationFailure.
"""
self.assertTrue(retry_until_ok(err_func, errors.OperationFailure("")))
self.assertEqual(err_func.counter, 3)
def test_retry_until_ok_authorization(self):
"""Test retry_until_ok does not mask authorization failures.
"""
with self.assertRaises(errors.OperationFailure):
retry_until_ok(err_func, errors.OperationFailure("", 13, None))
self.assertEqual(err_func.counter, 1)
def test_retry_until_ok_authorization_mongodb_24(self):
"""Test retry_until_ok does not mask authorization failures in
MongoDB 2.4.
"""
with self.assertRaises(errors.OperationFailure):
retry_until_ok(
err_func,
errors.OperationFailure("", details={"errmsg": "unauthorized"}),
)
self.assertEqual(err_func.counter, 1)
def test_retry_until_ok_runtime_error(self):
"""Test retry_until_ok does not mask RuntimeErrors.
"""
with self.assertRaises(RuntimeError):
retry_until_ok(err_func, RuntimeError)
self.assertEqual(err_func.counter, 1)
if __name__ == "__main__":
unittest.main()