@@ -52,73 +52,72 @@ def test_context_manager():
5252 close_func .assert_called_once ()
5353
5454
55- class ConnectionTest (TestCase ):
56- def test_connection_mock (self ):
57- """
58- For testing purposes it is often useful to replace the client used for
59- communication with the CrateDB server with a stub or mock.
60-
61- This can be done by passing an object of the Client class when calling
62- the `connect` method.
63- """
64-
65- class MyConnectionClient :
66- active_servers = ["localhost:4200" ]
67-
68- def __init__ (self ):
69- pass
70-
71- def server_infos (self , server ):
72- return ("localhost:4200" , "my server" , "0.42.0" )
73-
74- connection = connect ([crate_host ], client = MyConnectionClient ())
75- self .assertIsInstance (connection , Connection )
76- self .assertEqual (
77- connection .client .server_infos ("foo" ),
78- ("localhost:4200" , "my server" , "0.42.0" ),
79- )
80-
81- def test_with_timezone (self ):
82- """
83- The cursor can return timezone-aware `datetime` objects when requested.
84-
85- When switching the time zone at runtime on the connection object, only
86- new cursor objects will inherit the new time zone.
87- """
88-
89- tz_mst = datetime .timezone (datetime .timedelta (hours = 7 ), name = "MST" )
90- connection = connect ("localhost:4200" , time_zone = tz_mst )
91- cursor = connection .cursor ()
92- self .assertEqual (cursor .time_zone .tzname (None ), "MST" )
93- self .assertEqual (
94- cursor .time_zone .utcoffset (None ), datetime .timedelta (seconds = 25200 )
95- )
96-
97- connection .time_zone = datetime .timezone .utc
98- cursor = connection .cursor ()
99- self .assertEqual (cursor .time_zone .tzname (None ), "UTC" )
100- self .assertEqual (
101- cursor .time_zone .utcoffset (None ), datetime .timedelta (0 )
102- )
103-
104- def test_timeout_float (self ):
105- """
106- Verify setting the timeout value as a scalar (float) works.
107- """
108- with connect ("localhost:4200" , timeout = 2.42 ) as conn :
109- self .assertEqual (conn .client ._pool_kw ["timeout" ], 2.42 )
110-
111- def test_timeout_string (self ):
112- """
113- Verify setting the timeout value as a scalar (string) works.
114- """
115- with connect ("localhost:4200" , timeout = "2.42" ) as conn :
116- self .assertEqual (conn .client ._pool_kw ["timeout" ], 2.42 )
117-
118- def test_timeout_object (self ):
119- """
120- Verify setting the timeout value as a Timeout object works.
121- """
122- timeout = Timeout (connect = 2.42 , read = 0.01 )
123- with connect ("localhost:4200" , timeout = timeout ) as conn :
124- self .assertEqual (conn .client ._pool_kw ["timeout" ], timeout )
55+ def test_connection_mock ():
56+ """
57+ Verify that a custom client can be passed.
58+
59+
60+ For testing purposes, it is often useful to replace the client used for
61+ communication with the CrateDB server with a stub or mock.
62+
63+ This can be done by passing an object of the Client class when calling
64+ the `connect` method.
65+ """
66+
67+ mock = MagicMock (spec = Client )
68+ mock .server_infos .return_value = "localhost:4200" , "my server" , "0.42.0"
69+ connection = connect (crate_host , client = mock )
70+
71+ assert isinstance (connection , Connection )
72+ assert connection .client .server_infos ("foo" ) == ("localhost:4200" , "my server" , "0.42.0" )
73+
74+
75+ def test_with_timezone ():
76+ """
77+ Verify the logic of passing timezone objects to the client.
78+
79+ The cursor can return timezone-aware `datetime` objects when requested.
80+
81+ When switching the time zone at runtime on the connection object, only
82+ new cursor objects will inherit the new time zone.
83+
84+ These tests are complementary to timezone `test_cursor`
85+ """
86+
87+ tz_mst = datetime .timezone (datetime .timedelta (hours = 7 ), name = "MST" )
88+ connection = connect ("localhost:4200" , time_zone = tz_mst )
89+ cursor = connection .cursor ()
90+
91+ assert cursor .time_zone .tzname (None ) == "MST"
92+ assert cursor .time_zone .utcoffset (None ) == datetime .timedelta (seconds = 25200 )
93+
94+ connection .time_zone = datetime .timezone .utc
95+ cursor = connection .cursor ()
96+ assert cursor .time_zone .tzname (None ) == "UTC"
97+ assert cursor .time_zone .utcoffset (None ) == datetime .timedelta (0 )
98+
99+
100+
101+ def test_timeout_float ():
102+ """
103+ Verify setting the timeout value as a scalar (float) works.
104+ """
105+ with connect ("localhost:4200" , timeout = 2.42 ) as conn :
106+ assert conn .client ._pool_kw ["timeout" ] == 2.42
107+
108+
109+ def test_timeout_string ():
110+ """
111+ Verify setting the timeout value as a scalar (string) works.
112+ """
113+ with connect ("localhost:4200" , timeout = "2.42" ) as conn :
114+ assert conn .client ._pool_kw ["timeout" ] == 2.42
115+
116+
117+ def test_timeout_object ():
118+ """
119+ Verify setting the timeout value as a Timeout object works.
120+ """
121+ timeout = Timeout (connect = 2.42 , read = 0.01 )
122+ with connect ("localhost:4200" , timeout = timeout ) as conn :
123+ assert conn .client ._pool_kw ["timeout" ] == timeout
0 commit comments