func expectedMigration(sql string) { // Migration transaction sqlmock.ExpectBegin() sqlmock.ExpectExec(regexp.QuoteMeta(sql)). WillReturnResult(sqlmock.NewResult(0, 0)) sqlmock.ExpectCommit() }
func expectMigrationsTablePresenceQuery() { expectedSQL := fmt.Sprintf( "SELECT 1 FROM %s LIMIT 1", config.MigrationsTableName, ) sqlmock.ExpectExec(regexp.QuoteMeta(expectedSQL)). WillReturnResult(sqlmock.NewResult(0, 0)) }
func expectedMigrationLogDelete(id string) { expectedSQL := fmt.Sprintf( "DELETE FROM %s WHERE migration_id=?", config.MigrationsTableName, ) sqlmock.ExpectExec(regexp.QuoteMeta(expectedSQL)). WithArgs(id). WillReturnResult(sqlmock.NewResult(0, 1)) }
func expectedMigrationLogInsert(id string) { expectedSQL := fmt.Sprintf( "INSERT INTO %s (migration_id) VALUES (?)", config.MigrationsTableName, ) sqlmock.ExpectExec(regexp.QuoteMeta(expectedSQL)). WithArgs(id). WillReturnResult(sqlmock.NewResult(0, 1)) }
tableNames := map[string]string{ "the default": "migrations", "a custom": "custom_name", } for desc, tableName := range tableNames { config.MigrationsTableName = tableName Context(fmt.Sprintf("with %s table name", desc), func() { Describe(".CreateMigrationsTable", func() { It("creates the migration table in the database", func() { expectedSQL := fmt.Sprintf( "CREATE TABLE %s (id INT NOT NULL AUTO_INCREMENT, migration_id VARCHAR(255) NOT NULL UNIQUE, PRIMARY KEY(id))", config.MigrationsTableName, ) sqlmock.ExpectExec(regexp.QuoteMeta(expectedSQL)). WillReturnResult(sqlmock.NewResult(0, 0)) err := CreateMigrationsTable() Expect(err).NotTo(HaveOccurred()) }) }) Describe(".DropMigrationsTable", func() { It("drops the migration table in the database", func() { expectedSQL := fmt.Sprintf( "DROP TABLE %s", config.MigrationsTableName, ) sqlmock.ExpectExec(regexp.QuoteMeta(expectedSQL)).