/
test_connection.py
126 lines (99 loc) · 4.58 KB
/
test_connection.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
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
# Copyright 2009 10gen, 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 the connection module."""
import unittest
import os
import sys
sys.path[0:0] = [""]
from pymongo.errors import ConnectionFailure, InvalidName
from pymongo.database import Database
from pymongo.connection import Connection
def get_connection():
host = os.environ.get("DB_IP", "localhost")
port = int(os.environ.get("DB_PORT", 27017))
return Connection(host, port)
class TestConnection(unittest.TestCase):
def setUp(self):
self.host = os.environ.get("DB_IP", "localhost")
self.port = int(os.environ.get("DB_PORT", 27017))
def test_types(self):
self.assertRaises(TypeError, Connection, 1)
self.assertRaises(TypeError, Connection, 1.14)
self.assertRaises(TypeError, Connection, [])
self.assertRaises(TypeError, Connection, "localhost", "27017")
self.assertRaises(TypeError, Connection, "localhost", 1.14)
self.assertRaises(TypeError, Connection, "localhost", [])
def test_constants(self):
Connection.HOST = self.host
Connection.PORT = self.port
self.assert_(Connection())
Connection.HOST = "somedomainthatdoesntexist.org"
Connection.PORT = 123456789
self.assertRaises(ConnectionFailure, Connection)
Connection.HOST = self.host
Connection.PORT = self.port
self.assert_(Connection())
def test_connect(self):
self.assertRaises(ConnectionFailure, Connection, "somedomainthatdoesntexist.org")
self.assertRaises(ConnectionFailure, Connection, self.host, 123456789)
self.assert_(Connection(self.host, self.port))
def test_repr(self):
self.assertEqual(repr(Connection(self.host, self.port)),
"Connection('%s', %s)" % (self.host, self.port))
def test_getters(self):
self.assertEqual(Connection(self.host, self.port).host(), self.host)
self.assertEqual(Connection(self.host, self.port).port(), self.port)
def test_get_db(self):
connection = Connection(self.host, self.port)
def make_db(base, name):
return base[name]
self.assertRaises(InvalidName, make_db, connection, "")
self.assertRaises(InvalidName, make_db, connection, "te$t")
self.assertRaises(InvalidName, make_db, connection, "te.t")
self.assertRaises(InvalidName, make_db, connection, "te\\t")
self.assertRaises(InvalidName, make_db, connection, "te/t")
self.assertRaises(InvalidName, make_db, connection, "te st")
self.assert_(isinstance(connection.test, Database))
self.assertEqual(connection.test, connection["test"])
self.assertEqual(connection.test, Database(connection, "test"))
def test_database_names(self):
connection = Connection(self.host, self.port)
connection.pymongo_test.test.save({"dummy": u"object"})
connection.pymongo_test_mike.test.save({"dummy": u"object"})
dbs = connection.database_names()
self.assert_("pymongo_test" in dbs)
self.assert_("pymongo_test_mike" in dbs)
def test_drop_database(self):
connection = Connection(self.host, self.port)
self.assertRaises(TypeError, connection.drop_database, 5)
self.assertRaises(TypeError, connection.drop_database, None)
connection.pymongo_test.test.save({"dummy": u"object"})
dbs = connection.database_names()
self.assert_("pymongo_test" in dbs)
connection.drop_database("pymongo_test")
dbs = connection.database_names()
self.assert_("pymongo_test" not in dbs)
connection.pymongo_test.test.save({"dummy": u"object"})
dbs = connection.database_names()
self.assert_("pymongo_test" in dbs)
connection.drop_database(connection.pymongo_test)
dbs = connection.database_names()
self.assert_("pymongo_test" not in dbs)
def test_iteration(self):
connection = Connection(self.host, self.port)
def iterate():
[a for a in connection]
self.assertRaises(TypeError, iterate)
if __name__ == "__main__":
unittest.main()