Skip to content

Commit a16f77d

Browse files
Add async tests for #2184
1 parent 7e791ac commit a16f77d

File tree

1 file changed

+47
-8
lines changed

1 file changed

+47
-8
lines changed

test/integ/aio/test_put_get_async.py

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
except ImportError:
2323
from test.randomize import random_string
2424

25-
from test.generate_test_files import generate_k_lines_of_n_files
26-
2725
try:
28-
from ..parameters import CONNECTION_PARAMETERS_ADMIN
26+
from src.snowflake.connector.compat import IS_WINDOWS
2927
except ImportError:
30-
CONNECTION_PARAMETERS_ADMIN = {}
28+
import platform
29+
30+
IS_WINDOWS = platform.system() == "Windows"
31+
32+
from test.generate_test_files import generate_k_lines_of_n_files
3133

3234
THIS_DIR = path.dirname(path.realpath(__file__))
3335

@@ -154,7 +156,9 @@ async def test_get_empty_file(tmp_path, aio_connection):
154156
assert not empty_file.exists()
155157

156158

157-
async def test_get_file_permission(tmp_path, aio_connection, caplog):
159+
@pytest.mark.parametrize("auto_compress", ["TRUE", "FALSE"])
160+
@pytest.mark.skipif(IS_WINDOWS, reason="not supported on Windows")
161+
async def test_get_file_permission(tmp_path, aio_connection, caplog, auto_compress):
158162
test_file = tmp_path / "data.csv"
159163
test_file.write_text("1,2,3\n")
160164
stage_name = random_string(5, "test_get_empty_file_")
@@ -163,19 +167,54 @@ async def test_get_file_permission(tmp_path, aio_connection, caplog):
163167
await cur.execute(f"create temporary stage {stage_name}")
164168
filename_in_put = str(test_file).replace("\\", "/")
165169
await cur.execute(
166-
f"PUT 'file://{filename_in_put}' @{stage_name}",
170+
f"PUT 'file://{filename_in_put}' @{stage_name} AUTO_COMPRESS={auto_compress}",
171+
)
172+
test_file.unlink()
173+
174+
with caplog.at_level(logging.ERROR):
175+
await cur.execute(f"GET @{stage_name}/data.csv file://{tmp_path}")
176+
assert "FileNotFoundError" not in caplog.text
177+
assert len(list(tmp_path.iterdir())) == 1
178+
downloaded_file = next(tmp_path.iterdir())
179+
180+
# get the default mask, usually it is 0o022
181+
default_mask = os.umask(0)
182+
os.umask(default_mask)
183+
# files by default are given the permission 600 (Octal)
184+
# umask is for denial, we need to negate
185+
assert oct(os.stat(downloaded_file).st_mode)[-3:] == oct(0o600 & ~default_mask)[-3:]
186+
187+
188+
@pytest.mark.parametrize("auto_compress", ["TRUE", "FALSE"])
189+
@pytest.mark.skipif(IS_WINDOWS, reason="not supported on Windows")
190+
async def test_get_unsafe_file_permission_when_flag_set(
191+
tmp_path, aio_connection, caplog, auto_compress
192+
):
193+
test_file = tmp_path / "data.csv"
194+
test_file.write_text("1,2,3\n")
195+
stage_name = random_string(5, "test_get_empty_file_")
196+
await aio_connection.connect()
197+
aio_connection.unsafe_file_write = True
198+
cur = aio_connection.cursor()
199+
await cur.execute(f"create temporary stage {stage_name}")
200+
filename_in_put = str(test_file).replace("\\", "/")
201+
await cur.execute(
202+
f"PUT 'file://{filename_in_put}' @{stage_name} AUTO_COMPRESS={auto_compress}",
167203
)
204+
test_file.unlink()
168205

169206
with caplog.at_level(logging.ERROR):
170207
await cur.execute(f"GET @{stage_name}/data.csv file://{tmp_path}")
171208
assert "FileNotFoundError" not in caplog.text
209+
assert len(list(tmp_path.iterdir())) == 1
210+
downloaded_file = next(tmp_path.iterdir())
172211

173212
# get the default mask, usually it is 0o022
174213
default_mask = os.umask(0)
175214
os.umask(default_mask)
176-
# files by default are given the permission 644 (Octal)
215+
# when unsafe_file_write is set, permission is 644 (Octal)
177216
# umask is for denial, we need to negate
178-
assert oct(os.stat(test_file).st_mode)[-3:] == oct(0o666 & ~default_mask)[-3:]
217+
assert oct(os.stat(downloaded_file).st_mode)[-3:] == oct(0o666 & ~default_mask)[-3:]
179218

180219

181220
async def test_get_multiple_files_with_same_name(tmp_path, aio_connection, caplog):

0 commit comments

Comments
 (0)