00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 """Test pairing support.
00016
00017 These tests are skipped by nose by default (since they depend on having a
00018 paired setup. To run the tests just run this file manually.
00019
00020 Left and right nodes will be $DB_IP:$DB_PORT and $DB_IP2:$DB_PORT2 or
00021 localhost:27017 and localhost:27018 by default.
00022 """
00023
00024 import unittest
00025 import logging
00026 import os
00027 import sys
00028 import warnings
00029 sys.path[0:0] = [""]
00030
00031 from pymongo.errors import ConnectionFailure
00032 from pymongo.connection import Connection
00033
00034 skip_tests = True
00035
00036
00037 class TestPaired(unittest.TestCase):
00038
00039 def setUp(self):
00040 left_host = os.environ.get("DB_IP", "localhost")
00041 left_port = int(os.environ.get("DB_PORT", 27017))
00042 self.left = (left_host, left_port)
00043 right_host = os.environ.get("DB_IP2", "localhost")
00044 right_port = int(os.environ.get("DB_PORT2", 27018))
00045 self.right = (right_host, right_port)
00046 self.bad = ("somedomainthatdoesntexist.org", 12345)
00047
00048 def tearDown(self):
00049 pass
00050
00051 def skip(self):
00052 if skip_tests:
00053 from nose.plugins.skip import SkipTest
00054 raise SkipTest()
00055
00056 def test_types(self):
00057 self.skip()
00058 self.assertRaises(TypeError, Connection.paired, 5)
00059 self.assertRaises(TypeError, Connection.paired, "localhost")
00060 self.assertRaises(TypeError, Connection.paired, None)
00061 self.assertRaises(TypeError, Connection.paired, 5, self.right)
00062 self.assertRaises(TypeError, Connection.paired,
00063 "localhost", self.right)
00064 self.assertRaises(TypeError, Connection.paired, None, self.right)
00065 self.assertRaises(TypeError, Connection.paired, self.left, 5)
00066 self.assertRaises(TypeError, Connection.paired, self.left, "localhost")
00067 self.assertRaises(TypeError, Connection.paired, self.left, "localhost")
00068
00069 def test_connect(self):
00070 self.skip()
00071 self.assertRaises(ConnectionFailure, Connection.paired,
00072 self.bad, self.bad)
00073
00074 connection = Connection.paired(self.left, self.right)
00075 self.assert_(connection)
00076
00077 host = connection.host
00078 port = connection.port
00079
00080 connection = Connection.paired(self.right, self.left)
00081 self.assert_(connection)
00082 self.assertEqual(host, connection.host)
00083 self.assertEqual(port, connection.port)
00084
00085 slave = self.left == (host, port) and self.right or self.left
00086 self.assertRaises(ConnectionFailure, Connection.paired,
00087 slave, self.bad)
00088 self.assertRaises(ConnectionFailure, Connection.paired,
00089 self.bad, slave)
00090
00091 def test_repr(self):
00092 self.skip()
00093 connection = Connection.paired(self.left, self.right)
00094
00095 self.assertEqual(repr(connection),
00096 "Connection(['%s:%s', '%s:%s'])" %
00097 (self.left[0],
00098 self.left[1],
00099 self.right[0],
00100 self.right[1]))
00101
00102 def test_basic(self):
00103 self.skip()
00104 connection = Connection.paired(self.left, self.right)
00105
00106 db = connection.pymongo_test
00107
00108 db.drop_collection("test")
00109 a = {"x": 1}
00110 db.test.save(a)
00111 self.assertEqual(a, db.test.find_one())
00112
00113 def test_end_request(self):
00114 self.skip()
00115 connection = Connection.paired(self.left, self.right)
00116 db = connection.pymongo_test
00117
00118 for _ in range(100):
00119 db.test.remove({})
00120 db.test.insert({})
00121 self.assert_(db.test.find_one())
00122 connection.end_request()
00123
00124 def test_deprecation_warnings_paired_connections(self):
00125 warnings.simplefilter("error")
00126 try:
00127 self.assertRaises(DeprecationWarning, Connection.paired,
00128 self.left, self.right, timeout=3)
00129 self.assertRaises(DeprecationWarning, Connection.paired,
00130 self.left, self.right, auto_start_request=True)
00131 self.assertRaises(DeprecationWarning, Connection.paired,
00132 self.left, self.right, pool_size=20)
00133 finally:
00134 warnings.resetwarnings()
00135 warnings.simplefilter('ignore')
00136
00137 def test_paired_connections_pass_individual_connargs(self):
00138 c = Connection.paired(self.left, self.right, network_timeout=5)
00139 self.assertEqual(5, c._Connection__network_timeout)
00140
00141
00142 if __name__ == "__main__":
00143 skip_tests = False
00144 unittest.main()