@@ -404,7 +404,7 @@ mod tests {
404404 use super :: * ;
405405 use crate :: operations:: DeltaOps ;
406406 use crate :: table:: config:: TableProperty ;
407- use crate :: writer:: test_utils:: { get_delta_schema, get_record_batch } ;
407+ use crate :: writer:: test_utils:: get_delta_schema;
408408 use tempfile:: TempDir ;
409409
410410 #[ tokio:: test]
@@ -517,138 +517,141 @@ mod tests {
517517 }
518518
519519 #[ cfg( feature = "datafusion" ) ]
520- #[ tokio:: test]
521- async fn test_create_table_save_mode ( ) {
522- let tmp_dir = tempfile:: tempdir ( ) . unwrap ( ) ;
523-
524- let schema = get_delta_schema ( ) ;
525- let table = CreateBuilder :: new ( )
526- . with_location ( tmp_dir. path ( ) . to_str ( ) . unwrap ( ) )
527- . with_columns ( schema. fields ( ) . cloned ( ) )
528- . await
529- . unwrap ( ) ;
530- assert_eq ! ( table. version( ) , Some ( 0 ) ) ;
531- let first_id = table. snapshot ( ) . unwrap ( ) . metadata ( ) . id ( ) . to_string ( ) ;
532-
533- let log_store = table. log_store ;
534-
535- // Check an error is raised when a table exists at location
536- let table = CreateBuilder :: new ( )
537- . with_log_store ( log_store. clone ( ) )
538- . with_columns ( schema. fields ( ) . cloned ( ) )
539- . with_save_mode ( SaveMode :: ErrorIfExists )
540- . await ;
541- assert ! ( table. is_err( ) ) ;
542-
543- // Check current table is returned when ignore option is chosen.
544- let table = CreateBuilder :: new ( )
545- . with_log_store ( log_store. clone ( ) )
546- . with_columns ( schema. fields ( ) . cloned ( ) )
547- . with_save_mode ( SaveMode :: Ignore )
548- . await
549- . unwrap ( ) ;
550- assert_eq ! ( table. snapshot( ) . unwrap( ) . metadata( ) . id( ) , first_id) ;
551-
552- // Check table is overwritten
553- let table = CreateBuilder :: new ( )
554- . with_log_store ( log_store)
555- . with_columns ( schema. fields ( ) . cloned ( ) )
556- . with_save_mode ( SaveMode :: Overwrite )
557- . await
558- . unwrap ( ) ;
559- assert_ne ! ( table. snapshot( ) . unwrap( ) . metadata( ) . id( ) , first_id)
560- }
561-
562- #[ cfg( feature = "datafusion" ) ]
563- #[ tokio:: test]
564- async fn test_create_or_replace_existing_table ( ) {
565- let batch = get_record_batch ( None , false ) ;
566- let schema = get_delta_schema ( ) ;
567- let table = DeltaOps :: new_in_memory ( )
568- . write ( vec ! [ batch. clone( ) ] )
569- . with_save_mode ( SaveMode :: ErrorIfExists )
570- . await
571- . unwrap ( ) ;
572- let state = table. snapshot ( ) . unwrap ( ) ;
573- assert_eq ! ( state. version( ) , 0 ) ;
574- assert_eq ! ( state. log_data( ) . num_files( ) , 1 ) ;
575-
576- let mut table = DeltaOps ( table)
577- . create ( )
578- . with_columns ( schema. fields ( ) . cloned ( ) )
579- . with_save_mode ( SaveMode :: Overwrite )
580- . await
581- . unwrap ( ) ;
582- table. load ( ) . await . unwrap ( ) ;
583- let state = table. snapshot ( ) . unwrap ( ) ;
584- assert_eq ! ( state. version( ) , 1 ) ;
585- // Checks if files got removed after overwrite
586- assert_eq ! ( state. log_data( ) . num_files( ) , 0 ) ;
587- }
588-
589- #[ tokio:: test]
590- #[ cfg( feature = "datafusion" ) ]
591- async fn test_create_or_replace_existing_table_partitioned ( ) {
592- let batch = get_record_batch ( None , false ) ;
593- let schema = get_delta_schema ( ) ;
594- let table = DeltaOps :: new_in_memory ( )
595- . write ( vec ! [ batch. clone( ) ] )
596- . with_save_mode ( SaveMode :: ErrorIfExists )
597- . await
598- . unwrap ( ) ;
599- let state = table. snapshot ( ) . unwrap ( ) ;
600- assert_eq ! ( state. version( ) , 0 ) ;
601- assert_eq ! ( state. log_data( ) . num_files( ) , 1 ) ;
602-
603- let mut table = DeltaOps ( table)
604- . create ( )
605- . with_columns ( schema. fields ( ) . cloned ( ) )
606- . with_save_mode ( SaveMode :: Overwrite )
607- . with_partition_columns ( vec ! [ "id" ] )
608- . await
609- . unwrap ( ) ;
610- table. load ( ) . await . unwrap ( ) ;
611- let state = table. snapshot ( ) . unwrap ( ) ;
612- assert_eq ! ( state. version( ) , 1 ) ;
613- // Checks if files got removed after overwrite
614- assert_eq ! ( state. log_data( ) . num_files( ) , 0 ) ;
615- }
616-
617- #[ tokio:: test]
618- async fn test_create_table_metadata_raise_if_key_not_exists ( ) {
619- let schema = get_delta_schema ( ) ;
620- let config: HashMap < String , Option < String > > =
621- vec ! [ ( "key" . to_string( ) , Some ( "value" . to_string( ) ) ) ]
622- . into_iter ( )
623- . collect ( ) ;
520+ mod datafusion_tests {
521+ use super :: * ;
522+
523+ use crate :: writer:: test_utils:: get_record_batch;
524+ #[ tokio:: test]
525+ async fn test_create_table_save_mode ( ) {
526+ let tmp_dir = tempfile:: tempdir ( ) . unwrap ( ) ;
527+
528+ let schema = get_delta_schema ( ) ;
529+ let table = CreateBuilder :: new ( )
530+ . with_location ( tmp_dir. path ( ) . to_str ( ) . unwrap ( ) )
531+ . with_columns ( schema. fields ( ) . cloned ( ) )
532+ . await
533+ . unwrap ( ) ;
534+ assert_eq ! ( table. version( ) , Some ( 0 ) ) ;
535+ let first_id = table. snapshot ( ) . unwrap ( ) . metadata ( ) . id ( ) . to_string ( ) ;
536+
537+ let log_store = table. log_store ;
538+
539+ // Check an error is raised when a table exists at location
540+ let table = CreateBuilder :: new ( )
541+ . with_log_store ( log_store. clone ( ) )
542+ . with_columns ( schema. fields ( ) . cloned ( ) )
543+ . with_save_mode ( SaveMode :: ErrorIfExists )
544+ . await ;
545+ assert ! ( table. is_err( ) ) ;
546+
547+ // Check current table is returned when ignore option is chosen.
548+ let table = CreateBuilder :: new ( )
549+ . with_log_store ( log_store. clone ( ) )
550+ . with_columns ( schema. fields ( ) . cloned ( ) )
551+ . with_save_mode ( SaveMode :: Ignore )
552+ . await
553+ . unwrap ( ) ;
554+ assert_eq ! ( table. snapshot( ) . unwrap( ) . metadata( ) . id( ) , first_id) ;
555+
556+ // Check table is overwritten
557+ let table = CreateBuilder :: new ( )
558+ . with_log_store ( log_store)
559+ . with_columns ( schema. fields ( ) . cloned ( ) )
560+ . with_save_mode ( SaveMode :: Overwrite )
561+ . await
562+ . unwrap ( ) ;
563+ assert_ne ! ( table. snapshot( ) . unwrap( ) . metadata( ) . id( ) , first_id)
564+ }
624565
625- // Fail to create table with unknown Delta key
626- let table = CreateBuilder :: new ( )
627- . with_location ( "memory:///" )
628- . with_columns ( schema. fields ( ) . cloned ( ) )
629- . with_configuration ( config. clone ( ) )
630- . await ;
631- assert ! ( table. is_err( ) ) ;
566+ #[ tokio:: test]
567+ async fn test_create_or_replace_existing_table ( ) {
568+ let batch = get_record_batch ( None , false ) ;
569+ let schema = get_delta_schema ( ) ;
570+ let table = DeltaOps :: new_in_memory ( )
571+ . write ( vec ! [ batch. clone( ) ] )
572+ . with_save_mode ( SaveMode :: ErrorIfExists )
573+ . await
574+ . unwrap ( ) ;
575+ let state = table. snapshot ( ) . unwrap ( ) ;
576+ assert_eq ! ( state. version( ) , 0 ) ;
577+ assert_eq ! ( state. log_data( ) . num_files( ) , 1 ) ;
578+
579+ let mut table = DeltaOps ( table)
580+ . create ( )
581+ . with_columns ( schema. fields ( ) . cloned ( ) )
582+ . with_save_mode ( SaveMode :: Overwrite )
583+ . await
584+ . unwrap ( ) ;
585+ table. load ( ) . await . unwrap ( ) ;
586+ let state = table. snapshot ( ) . unwrap ( ) ;
587+ assert_eq ! ( state. version( ) , 1 ) ;
588+ // Checks if files got removed after overwrite
589+ assert_eq ! ( state. log_data( ) . num_files( ) , 0 ) ;
590+ }
632591
633- // Succeed in creating table with unknown Delta key since we set raise_if_key_not_exists to false
634- let table = CreateBuilder :: new ( )
635- . with_location ( "memory:///" )
636- . with_columns ( schema. fields ( ) . cloned ( ) )
637- . with_raise_if_key_not_exists ( false )
638- . with_configuration ( config)
639- . await ;
640- assert ! ( table. is_ok( ) ) ;
592+ #[ tokio:: test]
593+ async fn test_create_or_replace_existing_table_partitioned ( ) {
594+ let batch = get_record_batch ( None , false ) ;
595+ let schema = get_delta_schema ( ) ;
596+ let table = DeltaOps :: new_in_memory ( )
597+ . write ( vec ! [ batch. clone( ) ] )
598+ . with_save_mode ( SaveMode :: ErrorIfExists )
599+ . await
600+ . unwrap ( ) ;
601+ let state = table. snapshot ( ) . unwrap ( ) ;
602+ assert_eq ! ( state. version( ) , 0 ) ;
603+ assert_eq ! ( state. log_data( ) . num_files( ) , 1 ) ;
604+
605+ let mut table = DeltaOps ( table)
606+ . create ( )
607+ . with_columns ( schema. fields ( ) . cloned ( ) )
608+ . with_save_mode ( SaveMode :: Overwrite )
609+ . with_partition_columns ( vec ! [ "id" ] )
610+ . await
611+ . unwrap ( ) ;
612+ table. load ( ) . await . unwrap ( ) ;
613+ let state = table. snapshot ( ) . unwrap ( ) ;
614+ assert_eq ! ( state. version( ) , 1 ) ;
615+ // Checks if files got removed after overwrite
616+ assert_eq ! ( state. log_data( ) . num_files( ) , 0 ) ;
617+ }
641618
642- // Ensure the non-Delta key was set correctly
643- let value = table
644- . unwrap ( )
645- . snapshot ( )
646- . unwrap ( )
647- . metadata ( )
648- . configuration ( )
649- . get ( "key" )
650- . unwrap ( )
651- . clone ( ) ;
652- assert_eq ! ( String :: from( "value" ) , value) ;
619+ #[ tokio:: test]
620+ async fn test_create_table_metadata_raise_if_key_not_exists ( ) {
621+ let schema = get_delta_schema ( ) ;
622+ let config: HashMap < String , Option < String > > =
623+ vec ! [ ( "key" . to_string( ) , Some ( "value" . to_string( ) ) ) ]
624+ . into_iter ( )
625+ . collect ( ) ;
626+
627+ // Fail to create table with unknown Delta key
628+ let table = CreateBuilder :: new ( )
629+ . with_location ( "memory:///" )
630+ . with_columns ( schema. fields ( ) . cloned ( ) )
631+ . with_configuration ( config. clone ( ) )
632+ . await ;
633+ assert ! ( table. is_err( ) ) ;
634+
635+ // Succeed in creating table with unknown Delta key since we set raise_if_key_not_exists to false
636+ let table = CreateBuilder :: new ( )
637+ . with_location ( "memory:///" )
638+ . with_columns ( schema. fields ( ) . cloned ( ) )
639+ . with_raise_if_key_not_exists ( false )
640+ . with_configuration ( config)
641+ . await ;
642+ assert ! ( table. is_ok( ) ) ;
643+
644+ // Ensure the non-Delta key was set correctly
645+ let value = table
646+ . unwrap ( )
647+ . snapshot ( )
648+ . unwrap ( )
649+ . metadata ( )
650+ . configuration ( )
651+ . get ( "key" )
652+ . unwrap ( )
653+ . clone ( ) ;
654+ assert_eq ! ( String :: from( "value" ) , value) ;
655+ }
653656 }
654657}
0 commit comments