15
15
from datetime import date , datetime , timezone
16
16
from typing import NamedTuple
17
17
from unittest import mock
18
+ from unittest .mock import MagicMock
18
19
19
20
import pytest
20
21
import pytz
@@ -792,6 +793,7 @@ async def test_invalid_bind_data_type(conn_cnx):
792
793
await cnx .cursor ().execute ("select 1 from dual where 1=%s" , ([1 , 2 , 3 ],))
793
794
794
795
796
+ @pytest .mark .skipolddriver
795
797
async def test_timeout_query (conn_cnx ):
796
798
async with conn_cnx () as cnx :
797
799
async with cnx .cursor () as c :
@@ -802,10 +804,32 @@ async def test_timeout_query(conn_cnx):
802
804
)
803
805
assert err .value .errno == 604 , (
804
806
"Invalid error code"
805
- and "SQL execution was cancelled by the client due to a timeout"
807
+ and "SQL execution was cancelled by the client due to a timeout. Error message received from the server: SQL execution canceled "
806
808
in err .value .msg
807
809
)
808
810
811
+ with pytest .raises (errors .ProgrammingError ) as err :
812
+ # we can not precisely control the timing to send cancel query request right after server
813
+ # executes the query but before returning the results back to client
814
+ # it depends on python scheduling and server processing speed, so we mock here
815
+ mock_timebomb = MagicMock ()
816
+ mock_timebomb .result .return_value = True
817
+
818
+ with mock .patch .object (c , "_timebomb" , mock_timebomb ):
819
+ await c .execute (
820
+ "select 123'" ,
821
+ timeout = 0.1 ,
822
+ )
823
+ assert (
824
+ mock_timebomb .result .return_value is True and err .value .errno == 1003
825
+ ), (
826
+ "Invalid error code"
827
+ and "SQL compilation error:\n syntax error line 1 at position 10 unexpected '''."
828
+ in err .value .msg
829
+ and "SQL execution was cancelled by the client due to a timeout"
830
+ not in err .value .msg
831
+ )
832
+
809
833
810
834
async def test_executemany (conn , db_parameters ):
811
835
"""Executes many statements. Client binding is supported by either dict, or list data types.
0 commit comments