Skip to content

Commit b8a79fc

Browse files
committed
Migrate more tests
1 parent 4a2fbf8 commit b8a79fc

File tree

1 file changed

+42
-25
lines changed

1 file changed

+42
-25
lines changed

tests/client/test_connection.py

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22
from unittest import TestCase
3+
from unittest.mock import patch, MagicMock
34

45
from urllib3 import Timeout
56

@@ -10,6 +11,47 @@
1011
from .settings import crate_host
1112

1213

14+
def test_lowest_server_version():
15+
"""
16+
Verify the lowest server version is correctly set.
17+
"""
18+
infos = [
19+
(None, None, "1.0.3"),
20+
(None, None, "5.5.2"),
21+
(None, None, "6.0.0"),
22+
(None, None, "not a version"),
23+
]
24+
25+
client = Client(servers="localhost:4200 localhost:4201 localhost:4202 localhost:4207")
26+
client.server_infos = lambda server: infos.pop()
27+
connection = connect(client=client)
28+
assert (1, 0, 3) == connection.lowest_server_version.version
29+
30+
31+
def test_invalid_server_version():
32+
"""
33+
Verify that when no correct version is set, the default (0, 0, 0) is returned.
34+
"""
35+
client = Client(servers="localhost:4200")
36+
client.server_infos = lambda server: (None, None, "No version")
37+
connection = connect(client=client)
38+
assert (0, 0, 0) == connection.lowest_server_version.version
39+
40+
41+
def test_context_manager():
42+
"""
43+
Verify the context manager implementation of `Connection`.
44+
"""
45+
with patch('crate.client.http.Client.close', return_value=MagicMock()) as close_func:
46+
with connect("localhost:4200") as conn:
47+
assert conn._closed == False
48+
49+
assert conn._closed == True
50+
# Checks that the close method of the client
51+
# is called when the connection is closed.
52+
close_func.assert_called_once()
53+
54+
1355
class ConnectionTest(TestCase):
1456
def test_connection_mock(self):
1557
"""
@@ -36,31 +78,6 @@ def server_infos(self, server):
3678
("localhost:4200", "my server", "0.42.0"),
3779
)
3880

39-
def test_lowest_server_version(self):
40-
infos = [
41-
(None, None, "0.42.3"),
42-
(None, None, "0.41.8"),
43-
(None, None, "not a version"),
44-
]
45-
46-
client = Client(servers="localhost:4200 localhost:4201 localhost:4202")
47-
client.server_infos = lambda server: infos.pop()
48-
connection = connect(client=client)
49-
self.assertEqual((0, 41, 8), connection.lowest_server_version.version)
50-
connection.close()
51-
52-
def test_invalid_server_version(self):
53-
client = Client(servers="localhost:4200")
54-
client.server_infos = lambda server: (None, None, "No version")
55-
connection = connect(client=client)
56-
self.assertEqual((0, 0, 0), connection.lowest_server_version.version)
57-
connection.close()
58-
59-
def test_context_manager(self):
60-
with connect("localhost:4200") as conn:
61-
pass
62-
self.assertEqual(conn._closed, True)
63-
6481
def test_with_timezone(self):
6582
"""
6683
The cursor can return timezone-aware `datetime` objects when requested.

0 commit comments

Comments
 (0)