|
| 1 | +import uuid |
| 2 | +from collections.abc import Iterator |
| 3 | +from contextlib import contextmanager |
| 4 | +from pathlib import Path |
| 5 | + |
1 | 6 | import botocore.session
|
2 | 7 | import pytest
|
3 | 8 |
|
|
8 | 13 | from . import duckdb_queries # noqa: E402
|
9 | 14 |
|
10 | 15 |
|
| 16 | +@contextmanager |
| 17 | +def tempdir(base_path: str) -> Iterator[str]: |
| 18 | + path = Path(base_path) / str(uuid.uuid4()) |
| 19 | + path.mkdir() |
| 20 | + try: |
| 21 | + yield path.name |
| 22 | + finally: |
| 23 | + path.rmdir() |
| 24 | + |
| 25 | + |
11 | 26 | @pytest.fixture
|
12 |
| -def connection(local, restart, tmp_path): |
13 |
| - def _(): |
14 |
| - con = duckdb.connect() |
15 |
| - |
16 |
| - if not local: # Setup s3 credentials |
17 |
| - session = botocore.session.Session() |
18 |
| - creds = session.get_credentials() |
19 |
| - con.install_extension("httpfs") |
20 |
| - con.load_extension("httpfs") |
21 |
| - con.sql( |
22 |
| - f""" |
23 |
| - SET temp_directory='{tmp_path}'; |
24 |
| -
|
25 |
| - SET s3_region='us-east-2'; |
26 |
| - SET s3_access_key_id='{creds.access_key}'; |
27 |
| - SET s3_secret_access_key='{creds.secret_key}'; |
28 |
| - SET s3_session_token='{creds.token}'; |
29 |
| - """ |
30 |
| - ) |
31 |
| - return con |
| 27 | +def connection(local, restart): |
| 28 | + @contextmanager |
| 29 | + def _(): |
| 30 | + if local: |
| 31 | + from tempfile import TemporaryDirectory |
| 32 | + |
| 33 | + tempdir_ctx = TemporaryDirectory() |
| 34 | + else: |
| 35 | + from distributed import get_worker |
| 36 | + |
| 37 | + tempdir_ctx = tempdir(get_worker().local_directory) |
| 38 | + |
| 39 | + with tempdir_ctx as dir, duckdb.connect() as con: |
| 40 | + con.sql(f"SET temp_directory='{dir}';") |
| 41 | + |
| 42 | + if not local: |
| 43 | + # Setup s3 credentials |
| 44 | + session = botocore.session.Session() |
| 45 | + creds = session.get_credentials() |
| 46 | + con.install_extension("httpfs") |
| 47 | + con.load_extension("httpfs") |
| 48 | + con.sql( |
| 49 | + f""" |
| 50 | + SET s3_region='us-east-2'; |
| 51 | + SET s3_access_key_id='{creds.access_key}'; |
| 52 | + SET s3_secret_access_key='{creds.secret_key}'; |
| 53 | + SET s3_session_token='{creds.token}'; |
| 54 | + """ |
| 55 | + ) |
| 56 | + yield con |
32 | 57 |
|
33 | 58 | return _
|
34 | 59 |
|
35 | 60 |
|
36 | 61 | def test_query_1(run, connection, dataset_path, scale):
|
37 | 62 | def _():
|
38 |
| - duckdb_queries.query_1(connection(), dataset_path, scale) |
| 63 | + with connection() as con: |
| 64 | + duckdb_queries.query_1(con, dataset_path, scale) |
39 | 65 |
|
40 | 66 | run(_)
|
41 | 67 |
|
42 | 68 |
|
43 | 69 | def test_query_2(run, connection, dataset_path, scale):
|
44 | 70 | def _():
|
45 |
| - duckdb_queries.query_2(connection(), dataset_path, scale) |
| 71 | + with connection() as con: |
| 72 | + duckdb_queries.query_2(con, dataset_path, scale) |
46 | 73 |
|
47 | 74 | run(_)
|
48 | 75 |
|
49 | 76 |
|
50 | 77 | def test_query_3(run, connection, dataset_path, scale):
|
51 | 78 | def _():
|
52 |
| - duckdb_queries.query_3(connection(), dataset_path, scale) |
| 79 | + with connection() as con: |
| 80 | + duckdb_queries.query_3(con, dataset_path, scale) |
53 | 81 |
|
54 | 82 | run(_)
|
55 | 83 |
|
56 | 84 |
|
57 | 85 | def test_query_4(run, connection, dataset_path, scale):
|
58 | 86 | def _():
|
59 |
| - duckdb_queries.query_4(connection(), dataset_path, scale) |
| 87 | + with connection() as con: |
| 88 | + duckdb_queries.query_4(con, dataset_path, scale) |
60 | 89 |
|
61 | 90 | run(_)
|
62 | 91 |
|
63 | 92 |
|
64 | 93 | def test_query_5(run, connection, dataset_path, scale):
|
65 | 94 | def _():
|
66 |
| - duckdb_queries.query_5(connection(), dataset_path, scale) |
| 95 | + with connection() as con: |
| 96 | + duckdb_queries.query_5(con, dataset_path, scale) |
67 | 97 |
|
68 | 98 | run(_)
|
69 | 99 |
|
70 | 100 |
|
71 | 101 | def test_query_6(run, connection, dataset_path, scale):
|
72 | 102 | def _():
|
73 |
| - duckdb_queries.query_6(connection(), dataset_path, scale) |
| 103 | + with connection() as con: |
| 104 | + duckdb_queries.query_6(con, dataset_path, scale) |
74 | 105 |
|
75 | 106 | run(_)
|
76 | 107 |
|
77 | 108 |
|
78 | 109 | def test_query_7(run, connection, dataset_path, scale):
|
79 | 110 | def _():
|
80 |
| - duckdb_queries.query_7(connection(), dataset_path, scale) |
| 111 | + with connection() as con: |
| 112 | + duckdb_queries.query_7(con, dataset_path, scale) |
81 | 113 |
|
82 | 114 | run(_)
|
83 | 115 |
|
84 | 116 |
|
85 | 117 | def test_query_8(run, connection, dataset_path, scale):
|
86 | 118 | def _():
|
87 |
| - duckdb_queries.query_8(connection(), dataset_path, scale) |
| 119 | + with connection() as con: |
| 120 | + duckdb_queries.query_8(con, dataset_path, scale) |
88 | 121 |
|
89 | 122 | run(_)
|
90 | 123 |
|
91 | 124 |
|
92 | 125 | def test_query_9(run, connection, dataset_path, scale):
|
93 | 126 | def _():
|
94 |
| - duckdb_queries.query_9(connection(), dataset_path, scale) |
| 127 | + with connection() as con: |
| 128 | + duckdb_queries.query_9(con, dataset_path, scale) |
95 | 129 |
|
96 | 130 | run(_)
|
97 | 131 |
|
98 | 132 |
|
99 | 133 | def test_query_10(run, connection, dataset_path, scale):
|
100 | 134 | def _():
|
101 |
| - duckdb_queries.query_10(connection(), dataset_path, scale) |
| 135 | + with connection() as con: |
| 136 | + duckdb_queries.query_10(con, dataset_path, scale) |
102 | 137 |
|
103 | 138 | run(_)
|
104 | 139 |
|
105 | 140 |
|
106 | 141 | def test_query_11(run, connection, dataset_path, scale):
|
107 | 142 | def _():
|
108 |
| - duckdb_queries.query_11(connection(), dataset_path, scale) |
| 143 | + with connection() as con: |
| 144 | + duckdb_queries.query_11(con, dataset_path, scale) |
109 | 145 |
|
110 | 146 | run(_)
|
111 | 147 |
|
112 | 148 |
|
113 | 149 | def test_query_12(run, connection, dataset_path, scale):
|
114 | 150 | def _():
|
115 |
| - duckdb_queries.query_12(connection(), dataset_path, scale) |
| 151 | + with connection() as con: |
| 152 | + duckdb_queries.query_12(con, dataset_path, scale) |
116 | 153 |
|
117 | 154 | run(_)
|
118 | 155 |
|
119 | 156 |
|
120 | 157 | def test_query_13(run, connection, dataset_path, scale):
|
121 | 158 | def _():
|
122 |
| - duckdb_queries.query_13(connection(), dataset_path, scale) |
| 159 | + with connection() as con: |
| 160 | + duckdb_queries.query_13(con, dataset_path, scale) |
123 | 161 |
|
124 | 162 | run(_)
|
125 | 163 |
|
126 | 164 |
|
127 | 165 | def test_query_14(run, connection, dataset_path, scale):
|
128 | 166 | def _():
|
129 |
| - duckdb_queries.query_14(connection(), dataset_path, scale) |
| 167 | + with connection() as con: |
| 168 | + duckdb_queries.query_14(con, dataset_path, scale) |
130 | 169 |
|
131 | 170 | run(_)
|
132 | 171 |
|
133 | 172 |
|
134 | 173 | def test_query_15(run, connection, dataset_path, scale):
|
135 | 174 | def _():
|
136 |
| - duckdb_queries.query_15(connection(), dataset_path, scale) |
| 175 | + with connection() as con: |
| 176 | + duckdb_queries.query_15(con, dataset_path, scale) |
137 | 177 |
|
138 | 178 | run(_)
|
139 | 179 |
|
140 | 180 |
|
141 | 181 | def test_query_16(run, connection, dataset_path, scale):
|
142 | 182 | def _():
|
143 |
| - duckdb_queries.query_16(connection(), dataset_path, scale) |
| 183 | + with connection() as con: |
| 184 | + duckdb_queries.query_16(con, dataset_path, scale) |
144 | 185 |
|
145 | 186 | run(_)
|
146 | 187 |
|
147 | 188 |
|
148 | 189 | def test_query_17(run, connection, dataset_path, scale):
|
149 | 190 | def _():
|
150 |
| - duckdb_queries.query_17(connection(), dataset_path, scale) |
| 191 | + with connection() as con: |
| 192 | + duckdb_queries.query_17(con, dataset_path, scale) |
151 | 193 |
|
152 | 194 | run(_)
|
153 | 195 |
|
154 | 196 |
|
155 | 197 | def test_query_18(run, connection, dataset_path, scale):
|
156 | 198 | def _():
|
157 |
| - duckdb_queries.query_18(connection(), dataset_path, scale) |
| 199 | + with connection() as con: |
| 200 | + duckdb_queries.query_18(con, dataset_path, scale) |
158 | 201 |
|
159 | 202 | run(_)
|
160 | 203 |
|
161 | 204 |
|
162 | 205 | def test_query_19(run, connection, dataset_path, scale):
|
163 | 206 | def _():
|
164 |
| - duckdb_queries.query_19(connection(), dataset_path, scale) |
| 207 | + with connection() as con: |
| 208 | + duckdb_queries.query_19(con, dataset_path, scale) |
165 | 209 |
|
166 | 210 | run(_)
|
167 | 211 |
|
168 | 212 |
|
169 | 213 | def test_query_20(run, connection, dataset_path, scale):
|
170 | 214 | def _():
|
171 |
| - duckdb_queries.query_20(connection(), dataset_path, scale) |
| 215 | + with connection() as con: |
| 216 | + duckdb_queries.query_20(con, dataset_path, scale) |
172 | 217 |
|
173 | 218 | run(_)
|
174 | 219 |
|
175 | 220 |
|
176 | 221 | def test_query_21(run, connection, dataset_path, scale):
|
177 | 222 | def _():
|
178 |
| - duckdb_queries.query_21(connection(), dataset_path, scale) |
| 223 | + with connection() as con: |
| 224 | + duckdb_queries.query_21(con, dataset_path, scale) |
179 | 225 |
|
180 | 226 | run(_)
|
181 | 227 |
|
182 | 228 |
|
183 | 229 | def test_query_22(run, connection, dataset_path, scale):
|
184 | 230 | def _():
|
185 |
| - duckdb_queries.query_22(connection(), dataset_path, scale) |
| 231 | + with connection() as con: |
| 232 | + duckdb_queries.query_22(con, dataset_path, scale) |
186 | 233 |
|
187 | 234 | run(_)
|
0 commit comments