Skip to content

Commit

Permalink
integration test suite (#13)
Browse files Browse the repository at this point in the history
- adds a list of tests to execute in order
- fixing bugs that I find along the way
- the same test runs should work the same regardless of database type
- these call the compiled binary and make real database requests
- currently it requires a fresh database and won't do any cleanup
  - great for CI, annoying for developers 😢 
- currently writing the suite in Node.js... and it requires a globally installed package... 😭 
  - eventually the tests should be rewritten in idiomatic Go
  • Loading branch information
tlhunter authored Feb 18, 2023
1 parent b745bf7 commit ec7ad75
Show file tree
Hide file tree
Showing 8 changed files with 437 additions and 5 deletions.
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,18 @@ publish: build
git tag "v$(MIG_VERSION)"
git push origin main "v$(MIG_VERSION)"

# unit tests
test: build
go test -v ./...

# integration tests
#integration: build
# cd tests/postgres
# sh ../test.mjs
# cd ../mysql
# sh ../test.mjs
# cd ../..

clean:
rm mig || true
rm mig-linux-amd64 || true
Expand Down
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,15 @@ make

### Testing

The following commands let you easily spin up databases within Docker for testing:
There are some unit tests. Those can be run with the following command:

```sh
make test
```

However most of the tests are available as integration tests. Those require active databases.

The following commands spin up databases within Docker for local testing:

```sh
docker run --name some-postgres -p 5432:5432 \
Expand All @@ -172,3 +180,19 @@ docker run --name some-mysql -p 3306:3306 \
```

The `tests/<DBMS>` directories contain config files and migrations to experiment with functionality.

Run the following commands to run the different integration tests:

```sh
npm install -g zx

pushd tests/postgres
node ../test.mjs
popd

pushd tests/mysql
node ../test.mjs
popd
```

Unfortunately the integration tests currently require that Node.js also be installed. This will be fixed in the future.
3 changes: 0 additions & 3 deletions commands/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import (

func Dispatch(cfg config.MigConfig, subcommands []string) result.Response {
var res result.Response
if len(subcommands) == 0 {
res.SetError("usage: mig <command>", "command_usage")
}

switch subcommands[0] {
case "create":
Expand Down
3 changes: 3 additions & 0 deletions commands/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func CommandStatus(cfg config.MigConfig) result.Response {

if !existMigrations && !existLock {
res := result.NewError("The tables used for tracking migrations are missing.", "missing_tables")
res.ExitStatus = 9

res.AddErrorLn(color.WhiteString("This likely means that mig hasn't yet been initialized."))
res.AddErrorLn(color.WhiteString("This can be solved by running the following command:"))
Expand All @@ -96,6 +97,7 @@ func CommandStatus(cfg config.MigConfig) result.Response {

if !existMigrations {
res := *result.NewError("The 'migrations' table is missing but the 'migrations_lock' table is present!", "missing_migrations_table")
res.ExitStatus = 9
res.AddErrorLn("This might mean that data has been corrupted and that migration status is missing.")
res.AddErrorLn("Consider looking into the root cause of the problem.")
res.AddErrorLn("The quickest fix is to delete the lock table and initialize again:")
Expand All @@ -106,6 +108,7 @@ func CommandStatus(cfg config.MigConfig) result.Response {

if !existLock {
res := *result.NewError("The 'migrations' table is present but the 'migrations_lock' table is missing!", "missing_lock_table")
res.ExitStatus = 9
res.AddErrorLn("This might mean that data has been corrupted.")
res.AddErrorLn("Consider looking into the cause of the problem.")
res.AddErrorLn("The quickest fix is backup the migrations table data, initialize again, then restore the data:")
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ func main() {

var res result.Response

if err != nil && len(subcommands) == 1 && subcommands[0] == "version" {
if err != nil && len(subcommands) == 0 {
res.SetError("usage: mig <command>", "command_usage")
} else if err != nil && len(subcommands) == 1 && subcommands[0] == "version" {
res = commands.CommandVersion(cfg)
} else if err == nil {
res = commands.Dispatch(cfg, subcommands)
Expand Down
Loading

0 comments on commit ec7ad75

Please sign in to comment.