1
1
from tests .e2e .test_driver import PySQLPytestTestCase
2
2
3
- from databricks .sql .ae import AsyncExecutionStatus as AsyncExecutionStatus
4
-
3
+ from databricks .sql .ae import (
4
+ AsyncExecutionStatus ,
5
+ AsyncExecutionException ,
6
+ AsyncExecution ,
7
+ )
8
+ import pytest
5
9
import time
6
10
7
- LONG_RUNNING_QUERY = """
11
+ LONG_RUNNING_QUERY = """
8
12
SELECT SUM(A.id - B.id)
9
13
FROM range(1000000000) A CROSS JOIN range(100000000) B
10
14
GROUP BY (A.id - B.id)
11
15
"""
12
16
17
+
13
18
class TestExecuteAsync (PySQLPytestTestCase ):
19
+ @pytest .fixture
20
+ def long_running_ae (self , scope = "function" ) -> AsyncExecution :
21
+ """Start a long-running query so we can make assertions about it."""
22
+ with self .connection () as conn :
23
+ ae = conn .execute_async (LONG_RUNNING_QUERY )
24
+ yield ae
25
+
26
+ # cancellation is idempotent
27
+ ae .cancel ()
14
28
15
29
def test_basic_api (self ):
16
- """This is a WIP test of the basic API defined in PECO-1263
17
- """
30
+ """This is a WIP test of the basic API defined in PECO-1263"""
18
31
# This is taken directly from the design doc
19
32
20
33
with self .connection () as conn :
21
34
ae = conn .execute_async ("select :param `col`" , {"param" : 1 })
22
35
while ae .is_running :
23
- ae .get_result_or_status ()
36
+ ae .poll_for_status ()
24
37
time .sleep (1 )
25
-
26
- result = ae .get_result_or_status ().fetchone ()
27
38
28
- assert result . col == 1
39
+ result = ae . get_result (). fetchone ()
29
40
30
- def test_cancel_running_query (self ):
31
- """Start a long-running query and cancel it
32
- """
41
+ assert result .col == 1
33
42
34
- with self .connection () as conn :
35
- ae = conn .execute_async (LONG_RUNNING_QUERY )
36
- time .sleep (2 )
37
- ae .cancel ()
43
+ def test_cancel_running_query (self , long_running_ae : AsyncExecution ):
44
+ long_running_ae .cancel ()
45
+ assert long_running_ae .status == AsyncExecutionStatus .CANCELED
38
46
39
- status = ae .get_result_or_status ()
47
+ def test_cant_get_results_while_running (self , long_running_ae : AsyncExecution ):
48
+ with pytest .raises (AsyncExecutionException , match = "Query is still running" ):
49
+ long_running_ae .get_result ()
40
50
41
- assert ae .status == AsyncExecutionStatus .CANCELED
51
+ def test_cant_get_results_after_cancel (self , long_running_ae : AsyncExecution ):
52
+ long_running_ae .cancel ()
53
+ with pytest .raises (AsyncExecutionException , match = "Query was canceled" ):
54
+ long_running_ae .get_result ()
42
55
43
-
44
56
45
57
def test_staging_operation (self ):
46
58
"""We need to test what happens with a staging operation since this query won't have a result set
47
59
that user needs. It could be sufficient to notify users that they shouldn't use this API for staging/volumes
48
60
queries...
49
61
"""
50
- assert False
62
+ assert False
0 commit comments