@@ -342,20 +342,33 @@ def test_empty_dataframe_write_pandas(
342342 ), f"sucess: { success } , num_chunks: { num_chunks } , num_rows: { num_rows } "
343343
344344
345- @pytest .mark .parametrize ("quote_identifiers" , [True , False ])
346- def test_location_building_db_schema (conn_cnx , quote_identifiers : bool ):
347- """This tests that write_pandas constructs location correctly with database, schema and table name."""
345+ @pytest .mark .parametrize (
346+ "database,schema,quote_identifiers,expected_location" ,
347+ [
348+ ("database" , "schema" , True , '"database"."schema"."table"' ),
349+ ("database" , "schema" , False , "database.schema.table" ),
350+ (None , "schema" , True , '"schema"."table"' ),
351+ (None , "schema" , False , "schema.table" ),
352+ (None , None , True , '"table"' ),
353+ (None , None , False , "table" ),
354+ ],
355+ )
356+ def test_table_location_building (
357+ conn_cnx ,
358+ database : str | None ,
359+ schema : str | None ,
360+ quote_identifiers : bool ,
361+ expected_location : str ,
362+ ):
363+ """This tests that write_pandas constructs table location correctly with database, schema, and table name."""
348364 from snowflake .connector .cursor import SnowflakeCursor
349365
350366 with conn_cnx () as cnx :
351367
352368 def mocked_execute (* args , ** kwargs ):
353369 if len (args ) >= 1 and args [0 ].startswith ("COPY INTO" ):
354370 location = args [0 ].split (" " )[2 ]
355- if quote_identifiers :
356- assert location == '"database"."schema"."table"'
357- else :
358- assert location == "database.schema.table"
371+ assert location == expected_location
359372 cur = SnowflakeCursor (cnx )
360373 cur ._result = iter ([])
361374 return cur
@@ -368,29 +381,42 @@ def mocked_execute(*args, **kwargs):
368381 cnx ,
369382 sf_connector_version_df .get (),
370383 "table" ,
371- database = " database" ,
372- schema = " schema" ,
384+ database = database ,
385+ schema = schema ,
373386 quote_identifiers = quote_identifiers ,
374387 )
375388 assert m_execute .called and any (
376389 map (lambda e : "COPY INTO" in str (e [0 ]), m_execute .call_args_list )
377390 )
378391
379392
380- @pytest .mark .parametrize ("quote_identifiers" , [True , False ])
381- def test_location_building_schema (conn_cnx , quote_identifiers : bool ):
382- """This tests that write_pandas constructs location correctly with schema and table name."""
393+ @pytest .mark .parametrize (
394+ "database,schema,quote_identifiers,expected_db_schema" ,
395+ [
396+ ("database" , "schema" , True , '"database"."schema"' ),
397+ ("database" , "schema" , False , "database.schema" ),
398+ (None , "schema" , True , '"schema"' ),
399+ (None , "schema" , False , "schema" ),
400+ (None , None , True , "" ),
401+ (None , None , False , "" ),
402+ ],
403+ )
404+ def test_stage_location_building (
405+ conn_cnx ,
406+ database : str | None ,
407+ schema : str | None ,
408+ quote_identifiers : bool ,
409+ expected_db_schema : str ,
410+ ):
411+ """This tests that write_pandas constructs stage location correctly with database and schema."""
383412 from snowflake .connector .cursor import SnowflakeCursor
384413
385414 with conn_cnx () as cnx :
386415
387416 def mocked_execute (* args , ** kwargs ):
388- if len (args ) >= 1 and args [0 ].startswith ("COPY INTO" ):
389- location = args [0 ].split (" " )[2 ]
390- if quote_identifiers :
391- assert location == '"schema"."table"'
392- else :
393- assert location == "schema.table"
417+ if len (args ) >= 1 and args [0 ].startswith ("create temporary stage" ):
418+ db_schema = "." .join (args [0 ].split (" " )[- 1 ].split ("." )[:- 1 ])
419+ assert db_schema == expected_db_schema
394420 cur = SnowflakeCursor (cnx )
395421 cur ._result = iter ([])
396422 return cur
@@ -403,30 +429,53 @@ def mocked_execute(*args, **kwargs):
403429 cnx ,
404430 sf_connector_version_df .get (),
405431 "table" ,
406- schema = "schema" ,
432+ database = database ,
433+ schema = schema ,
407434 quote_identifiers = quote_identifiers ,
408435 )
409436 assert m_execute .called and any (
410- map (lambda e : "COPY INTO" in str (e [0 ]), m_execute .call_args_list )
437+ map (
438+ lambda e : "CREATE TEMP STAGE" in str (e [0 ]),
439+ m_execute .call_args_list ,
440+ )
411441 )
412442
413443
414- @pytest .mark .parametrize ("quote_identifiers" , [True , False ])
415- def test_location_building (conn_cnx , quote_identifiers : bool ):
416- """This tests that write_pandas constructs location correctly with schema and table name."""
444+ @pytest .mark .parametrize (
445+ "database,schema,quote_identifiers,expected_db_schema" ,
446+ [
447+ ("database" , "schema" , True , '"database"."schema"' ),
448+ ("database" , "schema" , False , "database.schema" ),
449+ (None , "schema" , True , '"schema"' ),
450+ (None , "schema" , False , "schema" ),
451+ (None , None , True , "" ),
452+ (None , None , False , "" ),
453+ ],
454+ )
455+ def test_file_format_location_building (
456+ conn_cnx ,
457+ database : str | None ,
458+ schema : str | None ,
459+ quote_identifiers : bool ,
460+ expected_db_schema : str ,
461+ ):
462+ """This tests that write_pandas constructs file format location correctly with database and schema."""
417463 from snowflake .connector .cursor import SnowflakeCursor
418464
419465 with conn_cnx () as cnx :
420466
421467 def mocked_execute (* args , ** kwargs ):
422- if len (args ) >= 1 and args [0 ].startswith ("COPY INTO" ):
423- location = args [0 ].split (" " )[2 ]
424- if quote_identifiers :
425- assert location == '"teble.table"'
426- else :
427- assert location == "teble.table"
468+ if len (args ) >= 1 and args [0 ].startswith ("CREATE FILE FORMAT" ):
469+ db_schema = "." .join (args [0 ].split (" " )[3 ].split ("." )[:- 1 ])
470+ assert db_schema == expected_db_schema
428471 cur = SnowflakeCursor (cnx )
429- cur ._result = iter ([])
472+ if args [0 ].startswith ("SELECT" ):
473+ cur ._rownumber = 0
474+ cur ._result = iter (
475+ [(col , "" ) for col in sf_connector_version_df .get ().columns ]
476+ )
477+ else :
478+ cur ._result = iter ([])
430479 return cur
431480
432481 with mock .patch (
@@ -436,11 +485,17 @@ def mocked_execute(*args, **kwargs):
436485 success , nchunks , nrows , _ = write_pandas (
437486 cnx ,
438487 sf_connector_version_df .get (),
439- "teble.table" ,
488+ "table" ,
489+ database = database ,
490+ schema = schema ,
440491 quote_identifiers = quote_identifiers ,
492+ auto_create_table = True ,
441493 )
442494 assert m_execute .called and any (
443- map (lambda e : "COPY INTO" in str (e [0 ]), m_execute .call_args_list )
495+ map (
496+ lambda e : "CREATE TEMP FILE FORMAT" in str (e [0 ]),
497+ m_execute .call_args_list ,
498+ )
444499 )
445500
446501
0 commit comments