44#
55import decimal
66import math
7+ from unittest import mock
78
89import pytest
910
3132)
3233from snowflake .snowpark .mock ._snowflake_data_type import ColumnEmulator , ColumnType
3334from snowflake .snowpark .types import DoubleType , IntegerType , StructType , StructField
34- import snowflake .snowpark .context as context
3535from tests .utils import Utils
3636
3737
@@ -644,10 +644,9 @@ def test_agg_on_empty_df(session):
644644 reason = "HAVING clause is not supported in local testing mode" ,
645645)
646646def test_agg_filter_snowpark_connect_compatible (session ):
647- original_value = context ._is_snowpark_connect_compatible_mode
648-
649- try :
650- context ._is_snowpark_connect_compatible_mode = True
647+ with mock .patch (
648+ "snowflake.snowpark.context._is_snowpark_connect_compatible_mode" , True
649+ ):
651650 df = session .create_dataframe (
652651 [(1 , 2 , 3 ), (3 , 2 , 1 ), (3 , 2 , 1 )], ["a" , "b" , "c" ]
653652 )
@@ -679,19 +678,16 @@ def test_agg_filter_snowpark_connect_compatible(session):
679678 df .filter (grouping ("a" ) == 0 ).collect ()
680679
681680 Utils .check_answer (df .filter (col ("a" ) > 1 ), [Row (3 , 2 , 1 ), Row (3 , 2 , 1 )])
682- finally :
683- context ._is_snowpark_connect_compatible_mode = original_value
684681
685682
686683@pytest .mark .skipif (
687684 "config.getoption('local_testing_mode', default=False)" ,
688685 reason = "ORDER BY append is not supported in local testing mode" ,
689686)
690687def test_agg_sort_snowpark_connect_compatible (session ):
691- original_value = context ._is_snowpark_connect_compatible_mode
692-
693- try :
694- context ._is_snowpark_connect_compatible_mode = True
688+ with mock .patch (
689+ "snowflake.snowpark.context._is_snowpark_connect_compatible_mode" , True
690+ ):
695691 df = session .create_dataframe (
696692 [(1 , 2 , 3 ), (3 , 2 , 1 ), (3 , 2 , 1 )], ["a" , "b" , "c" ]
697693 )
@@ -717,32 +713,27 @@ def test_agg_sort_snowpark_connect_compatible(session):
717713 # original behavior on dataframe without group by
718714 df4 = df .sort (col ("a" ))
719715 Utils .check_answer (df4 , [Row (1 , 2 , 3 ), Row (3 , 2 , 1 ), Row (3 , 2 , 1 )])
720- finally :
721- context ._is_snowpark_connect_compatible_mode = original_value
722716
723717
724718def test_agg_no_grouping_exprs_limit_snowpark_connect_compatible (session ):
725- original_value = context . _is_snowpark_connect_compatible_mode
726- try :
727- context . _is_snowpark_connect_compatible_mode = True
719+ with mock . patch (
720+ "snowflake.snowpark.context._is_snowpark_connect_compatible_mode" , True
721+ ):
728722 df = session .create_dataframe ([[1 , 2 ], [3 , 4 ], [1 , 4 ]], schema = ["A" , "B" ])
729723 result = df .agg (sum_ (col ("a" ))).limit (2 )
730724 Utils .check_answer (result , [Row (5 )])
731725 result = df .group_by ().agg (sum_ (col ("b" ))).limit (2 )
732726 Utils .check_answer (result , [Row (10 )])
733- finally :
734- context ._is_snowpark_connect_compatible_mode = original_value
735727
736728
737729@pytest .mark .skipif (
738730 "config.getoption('local_testing_mode', default=False)" ,
739731 reason = "HAVING and ORDER BY append are not supported in local testing mode" ,
740732)
741733def test_agg_filter_and_sort_with_grouping_snowpark_connect_compatible (session ):
742- original_value = context ._is_snowpark_connect_compatible_mode
743-
744- try :
745- context ._is_snowpark_connect_compatible_mode = True
734+ with mock .patch (
735+ "snowflake.snowpark.context._is_snowpark_connect_compatible_mode" , True
736+ ):
746737 df = session .create_dataframe (
747738 [
748739 ("dotNET" , 2012 , 10000 ),
@@ -852,19 +843,16 @@ def test_agg_filter_and_sort_with_grouping_snowpark_connect_compatible(session):
852843 # First row should have highest grouping value (1)
853844 results6 = df6 .collect ()
854845 assert results6 [0 ][2 ] == 1 # gc=1 for NULL course
855- finally :
856- context ._is_snowpark_connect_compatible_mode = original_value
857846
858847
859848@pytest .mark .skipif (
860849 "config.getoption('local_testing_mode', default=False)" ,
861850 reason = "HAVING, ORDER BY append, and limit append are not supported in local testing mode" ,
862851)
863852def test_filter_sort_limit_snowpark_connect_compatible (session , sql_simplifier_enabled ):
864- original_value = context ._is_snowpark_connect_compatible_mode
865-
866- try :
867- context ._is_snowpark_connect_compatible_mode = True
853+ with mock .patch (
854+ "snowflake.snowpark.context._is_snowpark_connect_compatible_mode" , True
855+ ):
868856 df = session .create_dataframe (
869857 [(1 , 2 , 3 ), (3 , 2 , 1 ), (3 , 2 , 1 )], ["a" , "b" , "c" ]
870858 )
@@ -939,9 +927,6 @@ def test_filter_sort_limit_snowpark_connect_compatible(session, sql_simplifier_e
939927 # Should have 4 SELECT statements
940928 assert query6 .upper ().count ("SELECT" ) == 4 if sql_simplifier_enabled else 5
941929
942- finally :
943- context ._is_snowpark_connect_compatible_mode = original_value
944-
945930
946931@pytest .mark .skipif (
947932 "config.getoption('local_testing_mode', default=False)" ,
0 commit comments