github.com/Redstoneguy129/cli@v0.0.0-20230211220159-15dca4e91917/internal/migration/repair/repair_test.go (about) 1 package repair 2 3 import ( 4 "context" 5 "github.com/Redstoneguy129/cli/internal/utils" 6 "testing" 7 8 "github.com/jackc/pgerrcode" 9 "github.com/stretchr/testify/assert" 10 "github.com/Redstoneguy129/cli/internal/testing/pgtest" 11 ) 12 13 var ( 14 user = "admin" 15 pass = "password" 16 database = "postgres" 17 host = utils.Config.Hostname 18 ) 19 20 func TestRepairCommand(t *testing.T) { 21 t.Run("applies new version", func(t *testing.T) { 22 // Setup mock postgres 23 conn := pgtest.NewConn() 24 defer conn.Close(t) 25 conn.Query(CREATE_VERSION_SCHEMA). 26 Reply("CREATE SCHEMA"). 27 Query(CREATE_VERSION_TABLE). 28 Reply("CREATE TABLE"). 29 Query(INSERT_MIGRATION_VERSION, "0"). 30 Reply("INSERT 0 1") 31 // Run test 32 err := Run(context.Background(), user, pass, database, host, "0", Applied, conn.Intercept) 33 // Check error 34 assert.NoError(t, err) 35 }) 36 37 t.Run("reverts old version", func(t *testing.T) { 38 // Setup mock postgres 39 conn := pgtest.NewConn() 40 defer conn.Close(t) 41 conn.Query(CREATE_VERSION_SCHEMA). 42 Reply("CREATE SCHEMA"). 43 Query(CREATE_VERSION_TABLE). 44 Reply("CREATE TABLE"). 45 Query(DELETE_MIGRATION_VERSION, "0"). 46 Reply("DELETE 1") 47 // Run test 48 err := Run(context.Background(), user, pass, database, host, "0", Reverted, conn.Intercept) 49 // Check error 50 assert.NoError(t, err) 51 }) 52 53 t.Run("throws error on connect failure", func(t *testing.T) { 54 // Run test 55 err := Run(context.Background(), user, pass, database, "0", "0", Applied) 56 // Check error 57 assert.ErrorContains(t, err, "dial error (dial tcp 0.0.0.0:6543: connect: connection refused)") 58 }) 59 60 t.Run("throws error on insert failure", func(t *testing.T) { 61 // Setup mock postgres 62 conn := pgtest.NewConn() 63 defer conn.Close(t) 64 conn.Query(CREATE_VERSION_SCHEMA). 65 Reply("CREATE SCHEMA"). 66 Query(CREATE_VERSION_TABLE). 67 Reply("CREATE TABLE"). 68 Query(INSERT_MIGRATION_VERSION, "0"). 69 ReplyError(pgerrcode.DuplicateObject, `relation "supabase_migrations.schema_migrations" does not exist`) 70 // Run test 71 err := Run(context.Background(), user, pass, database, host, "0", Applied, conn.Intercept) 72 // Check error 73 assert.ErrorContains(t, err, `ERROR: relation "supabase_migrations.schema_migrations" does not exist (SQLSTATE 42710)`) 74 }) 75 }