|
17 | 17 | from cryptography.hazmat.primitives.asymmetric import rsa |
18 | 18 |
|
19 | 19 | import snowflake.connector |
| 20 | +from snowflake.connector import SnowflakeConnection, connect |
20 | 21 | from snowflake.connector.connection import DEFAULT_CONFIGURATION |
21 | 22 | from snowflake.connector.errors import ( |
22 | 23 | Error, |
@@ -866,3 +867,89 @@ def test_reraise_error_in_file_transfer_work_function_config( |
866 | 867 | expected_value = bool(reraise_enabled) |
867 | 868 | actual_value = conn._reraise_error_in_file_transfer_work_function |
868 | 869 | assert actual_value == expected_value |
| 870 | + |
| 871 | + |
| 872 | +@pytest.mark.skipolddriver |
| 873 | +def test_connect_metadata_preservation(): |
| 874 | + """Test that the sync connect function preserves metadata from SnowflakeConnection.__init__. |
| 875 | +
|
| 876 | + This test verifies that various inspection methods return consistent metadata, |
| 877 | + ensuring IDE support, type checking, and documentation generation work correctly. |
| 878 | + """ |
| 879 | + import inspect |
| 880 | + |
| 881 | + # Test 1: Check __name__ is correct |
| 882 | + assert ( |
| 883 | + connect.__name__ == "__init__" |
| 884 | + ), f"connect.__name__ should be 'connect', but got '{connect.__name__}'" |
| 885 | + |
| 886 | + # Test 2: Check __wrapped__ points to SnowflakeConnection.__init__ |
| 887 | + assert hasattr(connect, "__wrapped__"), "connect should have __wrapped__ attribute" |
| 888 | + assert ( |
| 889 | + connect.__wrapped__ is SnowflakeConnection.__init__ |
| 890 | + ), "connect.__wrapped__ should reference SnowflakeConnection.__init__" |
| 891 | + |
| 892 | + # Test 3: Check __module__ is preserved |
| 893 | + assert hasattr(connect, "__module__"), "connect should have __module__ attribute" |
| 894 | + assert connect.__module__ == SnowflakeConnection.__init__.__module__, ( |
| 895 | + f"connect.__module__ should match SnowflakeConnection.__init__.__module__, " |
| 896 | + f"but got '{connect.__module__}' vs '{SnowflakeConnection.__init__.__module__}'" |
| 897 | + ) |
| 898 | + |
| 899 | + # Test 4: Check __doc__ is preserved |
| 900 | + assert hasattr(connect, "__doc__"), "connect should have __doc__ attribute" |
| 901 | + assert ( |
| 902 | + connect.__doc__ == SnowflakeConnection.__init__.__doc__ |
| 903 | + ), "connect.__doc__ should match SnowflakeConnection.__init__.__doc__" |
| 904 | + |
| 905 | + # Test 5: Check __annotations__ are preserved (or at least available) |
| 906 | + assert hasattr( |
| 907 | + connect, "__annotations__" |
| 908 | + ), "connect should have __annotations__ attribute" |
| 909 | + src_annotations = getattr(SnowflakeConnection.__init__, "__annotations__", {}) |
| 910 | + connect_annotations = getattr(connect, "__annotations__", {}) |
| 911 | + assert connect_annotations == src_annotations, ( |
| 912 | + f"connect.__annotations__ should match SnowflakeConnection.__init__.__annotations__, " |
| 913 | + f"but got {connect_annotations} vs {src_annotations}" |
| 914 | + ) |
| 915 | + |
| 916 | + # Test 6: Check inspect.signature works correctly |
| 917 | + try: |
| 918 | + connect_sig = inspect.signature(connect) |
| 919 | + source_sig = inspect.signature(SnowflakeConnection.__init__) |
| 920 | + assert str(connect_sig) == str(source_sig), ( |
| 921 | + f"inspect.signature(connect) should match inspect.signature(SnowflakeConnection.__init__), " |
| 922 | + f"but got '{connect_sig}' vs '{source_sig}'" |
| 923 | + ) |
| 924 | + except Exception as e: |
| 925 | + pytest.fail(f"inspect.signature(connect) failed: {e}") |
| 926 | + |
| 927 | + # Test 7: Check inspect.getdoc works correctly |
| 928 | + connect_doc = inspect.getdoc(connect) |
| 929 | + source_doc = inspect.getdoc(SnowflakeConnection.__init__) |
| 930 | + assert ( |
| 931 | + connect_doc == source_doc |
| 932 | + ), "inspect.getdoc(connect) should match inspect.getdoc(SnowflakeConnection.__init__)" |
| 933 | + |
| 934 | + # Test 8: Check that connect is callable |
| 935 | + assert callable(connect), "connect should be callable" |
| 936 | + |
| 937 | + # Test 9: Check type() and __class__ values (important for user introspection) |
| 938 | + assert ( |
| 939 | + type(connect).__name__ == "function" |
| 940 | + ), f"type(connect).__name__ should be 'function', but got '{type(connect).__name__}'" |
| 941 | + assert ( |
| 942 | + connect.__class__.__name__ == "function" |
| 943 | + ), f"connect.__class__.__name__ should be 'function', but got '{connect.__class__.__name__}'" |
| 944 | + assert inspect.isfunction( |
| 945 | + connect |
| 946 | + ), "connect should be recognized as a function by inspect.isfunction()" |
| 947 | + |
| 948 | + # Test 10: Verify the function has proper introspection capabilities |
| 949 | + # IDEs and type checkers should be able to resolve parameters |
| 950 | + sig = inspect.signature(connect) |
| 951 | + params = list(sig.parameters.keys()) |
| 952 | + assert ( |
| 953 | + len(params) > 0 |
| 954 | + ), "connect should have parameters from SnowflakeConnection.__init__" |
| 955 | + # Should have parameters like account, user, password, etc. |
0 commit comments