|
| 1 | +import pytest |
| 2 | +from mock import Mock, ANY |
| 3 | +from rethinkdb.net import make_connection, DefaultConnection, DEFAULT_PORT |
| 4 | + |
| 5 | + |
| 6 | +@pytest.mark.unit |
| 7 | +class TestMakeConnection(object): |
| 8 | + def setup_method(self): |
| 9 | + self.reconnect = Mock() |
| 10 | + self.conn_type = Mock() |
| 11 | + self.conn_type.return_value.reconnect.return_value = self.reconnect |
| 12 | + |
| 13 | + self.host = "myhost" |
| 14 | + self.port = "1234" |
| 15 | + self.db = "mydb" |
| 16 | + self.auth_key = None |
| 17 | + self.user = "gabor" |
| 18 | + self.password = "strongpass" |
| 19 | + self.timeout = 20 |
| 20 | + |
| 21 | + |
| 22 | + def test_make_connection(self): |
| 23 | + ssl = dict() |
| 24 | + _handshake_version = 10 |
| 25 | + |
| 26 | + conn = make_connection( |
| 27 | + self.conn_type, |
| 28 | + host=self.host, |
| 29 | + port=self.port, |
| 30 | + db=self.db, |
| 31 | + auth_key=self.auth_key, |
| 32 | + user=self.user, |
| 33 | + password=self.password, |
| 34 | + timeout=self.timeout, |
| 35 | + ) |
| 36 | + |
| 37 | + assert conn == self.reconnect |
| 38 | + self.conn_type.assert_called_once_with( |
| 39 | + self.host, |
| 40 | + self.port, |
| 41 | + self.db, |
| 42 | + self.auth_key, |
| 43 | + self.user, |
| 44 | + self.password, |
| 45 | + self.timeout, |
| 46 | + ssl, |
| 47 | + _handshake_version |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | + def test_make_connection_db_url(self): |
| 52 | + url = "rethinkdb://gabor:strongpass@myhost:1234/mydb?auth_key=mykey&timeout=30" |
| 53 | + ssl = dict() |
| 54 | + _handshake_version = 10 |
| 55 | + |
| 56 | + conn = make_connection(self.conn_type, url=url) |
| 57 | + |
| 58 | + assert conn == self.reconnect |
| 59 | + self.conn_type.assert_called_once_with( |
| 60 | + self.host, |
| 61 | + self.port, |
| 62 | + self.db, |
| 63 | + "mykey", |
| 64 | + self.user, |
| 65 | + self.password, |
| 66 | + 30, |
| 67 | + ssl, |
| 68 | + _handshake_version |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | + def test_make_connection_no_host(self): |
| 73 | + conn = make_connection( |
| 74 | + self.conn_type, |
| 75 | + port=self.port, |
| 76 | + db=self.db, |
| 77 | + auth_key=self.auth_key, |
| 78 | + user=self.user, |
| 79 | + password=self.password, |
| 80 | + timeout=self.timeout, |
| 81 | + ) |
| 82 | + |
| 83 | + assert conn == self.reconnect |
| 84 | + self.conn_type.assert_called_once_with( |
| 85 | + "localhost", |
| 86 | + self.port, |
| 87 | + self.db, |
| 88 | + self.auth_key, |
| 89 | + self.user, |
| 90 | + self.password, |
| 91 | + self.timeout, |
| 92 | + ANY, |
| 93 | + ANY |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | + def test_make_connection_no_port(self): |
| 98 | + conn = make_connection( |
| 99 | + self.conn_type, |
| 100 | + host=self.host, |
| 101 | + db=self.db, |
| 102 | + auth_key=self.auth_key, |
| 103 | + user=self.user, |
| 104 | + password=self.password, |
| 105 | + timeout=self.timeout, |
| 106 | + ) |
| 107 | + |
| 108 | + assert conn == self.reconnect |
| 109 | + self.conn_type.assert_called_once_with( |
| 110 | + self.host, |
| 111 | + DEFAULT_PORT, |
| 112 | + self.db, |
| 113 | + self.auth_key, |
| 114 | + self.user, |
| 115 | + self.password, |
| 116 | + self.timeout, |
| 117 | + ANY, |
| 118 | + ANY |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | + def test_make_connection_no_user(self): |
| 123 | + conn = make_connection( |
| 124 | + self.conn_type, |
| 125 | + host=self.host, |
| 126 | + port=self.port, |
| 127 | + db=self.db, |
| 128 | + auth_key=self.auth_key, |
| 129 | + password=self.password, |
| 130 | + timeout=self.timeout, |
| 131 | + ) |
| 132 | + |
| 133 | + assert conn == self.reconnect |
| 134 | + self.conn_type.assert_called_once_with( |
| 135 | + self.host, |
| 136 | + self.port, |
| 137 | + self.db, |
| 138 | + self.auth_key, |
| 139 | + "admin", |
| 140 | + self.password, |
| 141 | + self.timeout, |
| 142 | + ANY, |
| 143 | + ANY |
| 144 | + ) |
| 145 | + |
| 146 | + |
| 147 | + def test_make_connection_with_ssl(self): |
| 148 | + ssl = dict() |
| 149 | + |
| 150 | + conn = make_connection( |
| 151 | + self.conn_type, |
| 152 | + host=self.host, |
| 153 | + port=self.port, |
| 154 | + db=self.db, |
| 155 | + auth_key=self.auth_key, |
| 156 | + user=self.user, |
| 157 | + password=self.password, |
| 158 | + timeout=self.timeout, |
| 159 | + ssl=ssl, |
| 160 | + ) |
| 161 | + |
| 162 | + assert conn == self.reconnect |
| 163 | + self.conn_type.assert_called_once_with( |
| 164 | + self.host, |
| 165 | + self.port, |
| 166 | + self.db, |
| 167 | + self.auth_key, |
| 168 | + self.user, |
| 169 | + self.password, |
| 170 | + self.timeout, |
| 171 | + ssl, |
| 172 | + ANY |
| 173 | + ) |
| 174 | + |
| 175 | + |
| 176 | + def test_make_connection_different_handshake_version(self): |
| 177 | + conn = make_connection( |
| 178 | + self.conn_type, |
| 179 | + host=self.host, |
| 180 | + port=self.port, |
| 181 | + db=self.db, |
| 182 | + auth_key=self.auth_key, |
| 183 | + user=self.user, |
| 184 | + password=self.password, |
| 185 | + timeout=self.timeout, |
| 186 | + _handshake_version=20, |
| 187 | + ) |
| 188 | + |
| 189 | + assert conn == self.reconnect |
| 190 | + self.conn_type.assert_called_once_with( |
| 191 | + self.host, |
| 192 | + self.port, |
| 193 | + self.db, |
| 194 | + self.auth_key, |
| 195 | + self.user, |
| 196 | + self.password, |
| 197 | + self.timeout, |
| 198 | + ANY, |
| 199 | + 20 |
| 200 | + ) |
0 commit comments