@@ -561,6 +561,86 @@ mod tests {
561561 use rstest:: rstest;
562562 use vespertide_core:: IndexDef ;
563563
564+ #[ rstest]
565+ #[ case( BuiltQuery :: Raw ( "RAW" . into( ) ) , DatabaseBackend :: Postgres , "RAW" ) ]
566+ #[ case(
567+ BuiltQuery :: CreateTable ( Box :: new(
568+ Table :: create( ) . table( Alias :: new( "t" ) ) . to_owned( )
569+ ) ) ,
570+ DatabaseBackend :: Postgres ,
571+ "CREATE TABLE \" t\" ( )"
572+ ) ] // sea-query inserts two spaces when there are no columns
573+ #[ case(
574+ BuiltQuery :: DropTable ( Box :: new(
575+ Table :: drop( ) . table( Alias :: new( "t" ) ) . to_owned( )
576+ ) ) ,
577+ DatabaseBackend :: Sqlite ,
578+ "DROP TABLE \" t\" "
579+ ) ]
580+ #[ case(
581+ BuiltQuery :: AlterTable ( Box :: new(
582+ Table :: alter( )
583+ . table( Alias :: new( "t" ) )
584+ . add_column(
585+ SeaColumnDef :: new( Alias :: new( "c" ) )
586+ . integer( )
587+ . not_null( )
588+ . to_owned( ) ,
589+ )
590+ . to_owned( )
591+ ) ) ,
592+ DatabaseBackend :: Postgres ,
593+ "ALTER TABLE \" t\" ADD COLUMN \" c\" integer NOT NULL"
594+ ) ]
595+ #[ case(
596+ BuiltQuery :: CreateIndex ( Box :: new(
597+ Index :: create( ) . name( "idx" ) . table( Alias :: new( "t" ) ) . col( Alias :: new( "c" ) ) . to_owned( )
598+ ) ) ,
599+ DatabaseBackend :: Postgres ,
600+ "CREATE INDEX \" idx\" ON \" t\" (\" c\" )"
601+ ) ]
602+ #[ case(
603+ BuiltQuery :: DropIndex ( Box :: new( Index :: drop( ) . name( "idx" ) . table( Alias :: new( "t" ) ) . to_owned( ) ) ) ,
604+ DatabaseBackend :: Postgres ,
605+ "DROP INDEX \" idx\" "
606+ ) ]
607+ #[ case(
608+ BuiltQuery :: RenameTable ( Box :: new(
609+ Table :: rename( ) . table( Alias :: new( "a" ) , Alias :: new( "b" ) ) . to_owned( )
610+ ) ) ,
611+ DatabaseBackend :: Sqlite ,
612+ "ALTER TABLE \" a\" RENAME TO \" b\" "
613+ ) ]
614+ #[ case(
615+ BuiltQuery :: CreateForeignKey ( Box :: new(
616+ ForeignKey :: create( )
617+ . name( "fk" )
618+ . from_tbl( Alias :: new( "a" ) )
619+ . from_col( Alias :: new( "c" ) )
620+ . to_tbl( Alias :: new( "b" ) )
621+ . to_col( Alias :: new( "id" ) )
622+ . to_owned( )
623+ ) ) ,
624+ DatabaseBackend :: Postgres ,
625+ "ALTER TABLE \" a\" ADD CONSTRAINT \" fk\" FOREIGN KEY (\" c\" ) REFERENCES \" b\" (\" id\" )"
626+ ) ]
627+ #[ case(
628+ BuiltQuery :: DropForeignKey ( Box :: new(
629+ ForeignKey :: drop( ) . name( "fk" ) . table( Alias :: new( "a" ) ) . to_owned( )
630+ ) ) ,
631+ DatabaseBackend :: Postgres ,
632+ "ALTER TABLE \" a\" DROP CONSTRAINT \" fk\" "
633+ ) ]
634+ fn test_built_query_builds_sql (
635+ #[ case] q : BuiltQuery ,
636+ #[ case] backend : DatabaseBackend ,
637+ #[ case] expected : & str ,
638+ ) {
639+ assert_eq ! ( q. build( backend) , expected) ;
640+ assert_eq ! ( q. sql( ) , q. build( DatabaseBackend :: Postgres ) ) ;
641+ assert ! ( q. binds( ) . is_empty( ) ) ;
642+ }
643+
564644 fn col ( name : & str , ty : ColumnType ) -> ColumnDef {
565645 ColumnDef {
566646 name : name. to_string ( ) ,
0 commit comments