|
| 1 | +#!/usr/bin/env python |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import os |
| 5 | +from tempfile import TemporaryDirectory |
| 6 | +from typing import TYPE_CHECKING, Callable, Generator |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +try: |
| 11 | + from snowflake.connector.options import pandas |
| 12 | + from snowflake.connector.pandas_tools import ( |
| 13 | + _iceberg_config_statement_helper, |
| 14 | + write_pandas, |
| 15 | + ) |
| 16 | +except ImportError: |
| 17 | + pandas = None |
| 18 | + write_pandas = None |
| 19 | + _iceberg_config_statement_helper = None |
| 20 | + |
| 21 | +if TYPE_CHECKING: |
| 22 | + from snowflake.connector import SnowflakeConnection, SnowflakeCursor |
| 23 | + |
| 24 | + |
| 25 | +def _normalize_windows_local_path(path): |
| 26 | + return path.replace("\\", "\\\\").replace("'", "\\'") |
| 27 | + |
| 28 | + |
| 29 | +def _validate_upload_content( |
| 30 | + expected_content, cursor, stage_name, local_dir, base_file_name, is_compressed |
| 31 | +): |
| 32 | + gz_suffix = ".gz" |
| 33 | + stage_path = f"@{stage_name}/{base_file_name}" |
| 34 | + local_path = os.path.join(local_dir, base_file_name) |
| 35 | + |
| 36 | + cursor.execute( |
| 37 | + f"GET {stage_path} 'file://{_normalize_windows_local_path(local_dir)}'", |
| 38 | + ) |
| 39 | + if is_compressed: |
| 40 | + stage_path += gz_suffix |
| 41 | + local_path += gz_suffix |
| 42 | + import gzip |
| 43 | + |
| 44 | + with gzip.open(local_path, "r") as f: |
| 45 | + read_content = f.read().decode("utf-8") |
| 46 | + assert read_content == expected_content, (read_content, expected_content) |
| 47 | + else: |
| 48 | + with open(local_path) as f: |
| 49 | + read_content = f.read() |
| 50 | + assert read_content == expected_content, (read_content, expected_content) |
| 51 | + |
| 52 | + |
| 53 | +def _test_runner( |
| 54 | + conn_cnx: Callable[..., Generator[SnowflakeConnection]], |
| 55 | + task: Callable[[SnowflakeCursor, str, str, str], None], |
| 56 | + is_compressed: bool, |
| 57 | + special_stage_name: str = None, |
| 58 | + special_base_file_name: str = None, |
| 59 | +): |
| 60 | + from snowflake.connector._utils import TempObjectType, random_name_for_temp_object |
| 61 | + |
| 62 | + with conn_cnx() as conn: |
| 63 | + cursor = conn.cursor() |
| 64 | + stage_name = special_stage_name or random_name_for_temp_object( |
| 65 | + TempObjectType.STAGE |
| 66 | + ) |
| 67 | + cursor.execute(f"CREATE OR REPLACE SCOPED TEMP STAGE {stage_name}") |
| 68 | + expected_content = "hello, world" |
| 69 | + with TemporaryDirectory() as temp_dir: |
| 70 | + base_file_name = special_base_file_name or "test.txt" |
| 71 | + src_file_name = os.path.join(temp_dir, base_file_name) |
| 72 | + with open(src_file_name, "w") as f: |
| 73 | + f.write(expected_content) |
| 74 | + # Run the file operation |
| 75 | + task(cursor, stage_name, temp_dir, base_file_name) |
| 76 | + # Clean up before validation. |
| 77 | + os.remove(src_file_name) |
| 78 | + # Validate result. |
| 79 | + _validate_upload_content( |
| 80 | + expected_content, |
| 81 | + cursor, |
| 82 | + stage_name, |
| 83 | + temp_dir, |
| 84 | + base_file_name, |
| 85 | + is_compressed=is_compressed, |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.skipolddriver |
| 90 | +@pytest.mark.parametrize("is_compressed", [False, True]) |
| 91 | +def test_upload( |
| 92 | + conn_cnx: Callable[..., Generator[SnowflakeConnection]], |
| 93 | + is_compressed: bool, |
| 94 | +): |
| 95 | + def upload_task(cursor, stage_name, temp_dir, base_file_name): |
| 96 | + cursor._upload( |
| 97 | + local_file_name=f"'file://{_normalize_windows_local_path(os.path.join(temp_dir, base_file_name))}'", |
| 98 | + stage_location=f"@{stage_name}", |
| 99 | + options={"auto_compress": is_compressed}, |
| 100 | + ) |
| 101 | + |
| 102 | + _test_runner(conn_cnx, upload_task, is_compressed=is_compressed) |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.skipolddriver |
| 106 | +@pytest.mark.parametrize("is_compressed", [False, True]) |
| 107 | +def test_upload_stream( |
| 108 | + conn_cnx: Callable[..., Generator[SnowflakeConnection]], |
| 109 | + is_compressed: bool, |
| 110 | +): |
| 111 | + def upload_stream_task(cursor, stage_name, temp_dir, base_file_name): |
| 112 | + with open(f"{os.path.join(temp_dir, base_file_name)}", "rb") as input_stream: |
| 113 | + cursor._upload_stream( |
| 114 | + input_stream=input_stream, |
| 115 | + stage_location=f"@{os.path.join(stage_name, base_file_name)}", |
| 116 | + options={"auto_compress": is_compressed}, |
| 117 | + ) |
| 118 | + |
| 119 | + _test_runner(conn_cnx, upload_stream_task, is_compressed=is_compressed) |
0 commit comments