Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ebi-yade committed Oct 9, 2024
1 parent 452aa25 commit 0c7d6f7
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ go 1.23.2
require (
github.com/alecthomas/kong v1.2.1 // indirect
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/pingcap/errors v0.11.5-0.20240311024730-e056997136bb // indirect
github.com/pingcap/failpoint v0.0.0-20220801062533-2eaa32854a6c // indirect
github.com/pingcap/log v1.1.0 // indirect
github.com/pingcap/tidb/pkg/parser v0.0.0-20240820100743-1a0c3ac3292f // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/samber/lo v1.47.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3 // indirect
golang.org/x/text v0.16.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZx
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 h1:iwZdTE0PVqJCos1vaoKsclOGD3ADKpshg3SRtYBbwso=
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548/go.mod h1:e6NPNENfs9mPDVNRekM7lKScauxd5kXTr1Mfyig6TDM=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
Expand All @@ -21,6 +24,7 @@ github.com/pingcap/tidb/pkg/parser v0.0.0-20240820100743-1a0c3ac3292f h1:l9EkYIh
github.com/pingcap/tidb/pkg/parser v0.0.0-20240820100743-1a0c3ac3292f/go.mod h1:c/4la2yfv1vBYvtIG8WCDyDinLMDIUC5+zLRHiafY+Y=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
Expand All @@ -31,6 +35,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
Expand Down Expand Up @@ -72,3 +78,5 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
136 changes: 136 additions & 0 deletions mysql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package breaql_test

import (
"testing"

"github.com/ebi-yade/breaql"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
)

func TestRunMySQL(t *testing.T) {
tests := []struct {
name string
sql string
want breaql.BreakingChanges
expectsErr bool
}{
{
name: "DropDatabase",
sql: "DROP DATABASE test_db;",
want: breaql.BreakingChanges{
Databases: breaql.DatabaseChanges{"test_db": {"DROP DATABASE test_db;"}},
},
expectsErr: false,
},
{
name: "DropTable",
sql: "DROP TABLE test_table;",
want: breaql.BreakingChanges{
Tables: breaql.TableChanges{"test_table": {"DROP TABLE test_table;"}},
},
expectsErr: false,
},
{
name: "TruncateTable",
sql: "TRUNCATE TABLE test_table;",
want: breaql.BreakingChanges{
Tables: breaql.TableChanges{"test_table": {"TRUNCATE TABLE test_table;"}},
},
expectsErr: false,
},
{
name: "RenameTable",
sql: "RENAME TABLE test_table_old TO test_table_new;",
want: breaql.BreakingChanges{
Tables: breaql.TableChanges{"test_table_old": {"RENAME TABLE test_table_old TO test_table_new;"}},
},
expectsErr: false,
},
{
name: "AlterTableDropColumn",
sql: "ALTER TABLE test_table DROP COLUMN column_name;",
want: breaql.BreakingChanges{
Tables: breaql.TableChanges{"test_table": {"ALTER TABLE test_table DROP COLUMN column_name;"}},
},
expectsErr: false,
},
{
name: "AlterTableDropIndex",
sql: "ALTER TABLE test_table DROP INDEX index_name;",
want: breaql.BreakingChanges{
Tables: breaql.TableChanges{"test_table": {"ALTER TABLE test_table DROP INDEX index_name;"}},
},
expectsErr: false,
},
{
name: "AlterTableDropForeignKey",
sql: "ALTER TABLE test_table DROP FOREIGN KEY fk_name;",
want: breaql.BreakingChanges{
Tables: breaql.TableChanges{"test_table": {"ALTER TABLE test_table DROP FOREIGN KEY fk_name;"}},
},
expectsErr: false,
},
{
name: "AlterTableModifyColumn",
sql: "ALTER TABLE test_table MODIFY column_name VARCHAR(255);",
want: breaql.BreakingChanges{
Tables: breaql.TableChanges{"test_table": {"ALTER TABLE test_table MODIFY column_name VARCHAR(255);"}},
},
expectsErr: false,
},
{
name: "CreateTable",
sql: "CREATE TABLE test_table (id INT PRIMARY KEY);",
want: breaql.BreakingChanges{},
expectsErr: false,
},
{
name: "MultipleStatementsWithBreakingChanges",
sql: `CREATE TABLE test_table (id INT PRIMARY KEY);
ALTER TABLE test_table DROP COLUMN id;
DROP DATABASE test_db;`,
want: breaql.BreakingChanges{
Tables: breaql.TableChanges{"test_table": {"ALTER TABLE test_table DROP COLUMN id;"}},
Databases: breaql.DatabaseChanges{"test_db": {"DROP DATABASE test_db;"}},
},
expectsErr: false,
},
{
name: "MultipleStatementsWithNonBreakingAndBreakingChanges",
sql: `CREATE TABLE test_table (id INT PRIMARY KEY);
ALTER TABLE test_table ADD COLUMN new_column INT;
ALTER TABLE test_table DROP COLUMN id;
ALTER TABLE test_table DROP COLUMN new_column;
ALTER TABLE test_table ADD INDEX idx_new_column (new_column);`,
want: breaql.BreakingChanges{
Tables: breaql.TableChanges{"test_table": {"ALTER TABLE test_table DROP COLUMN id;", "ALTER TABLE test_table DROP COLUMN new_column;"}},
},
expectsErr: false,
},
{
name: "InvalidSQL",
sql: "INVALID SQL STATEMENT;",
expectsErr: true,
},
}

opts := []cmp.Option{
cmpopts.EquateEmpty(),
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := breaql.RunMySQL(tt.sql)
if tt.expectsErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
if diff := cmp.Diff(tt.want, got, opts...); diff != "" {
t.Errorf("RunMySQL() mismatch (-want +got):\n%s", diff)
}
}
})
}
}

0 comments on commit 0c7d6f7

Please sign in to comment.