Skip to content

Commit

Permalink
Spec for statement ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Dec 1, 2021
1 parent 7cedbc8 commit d88e899
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
58 changes: 58 additions & 0 deletions spec/action_migrate_dsl_statement_ordering_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package spec

import (
"testing"
)

func TestActionMigrateDslStatementOrdering(t *testing.T) {
c := mockCli(mockConfig(`
migration "create_animals" {
version = "v1"
up {
create_table "animals" {
column "id" "bigint" {}
}
create_index "animals" "idx_id" {
columns = ["id"]
}
sql = "ALTER INDEX idx_id RENAME TO idx_new"
drop_index "idx_new" {}
drop_table "animals" {}
}
down {}
}
migration_set "animals" {
migrations = [
migration.create_animals
]
}
`))
defer c.Teardown()
if c.AssertSuccessfulRun(t, []string{"migrate", "up", "animals"}) {
c.AssertSchemaMigrationTable(t, "public", "v1")
c.AssertSQLContains(t, `
CREATE TABLE "animals"(
"id" bigint
)
`)
c.AssertSQLContains(t, `
CREATE INDEX "idx_id" ON "animals"
`)
c.AssertSQLContains(t, `
ALTER INDEX idx_id RENAME TO idx_new
`)
c.AssertSQLContains(t, `
DROP INDEX "idx_new"
`)
c.AssertSQLContains(t, `
DROP TABLE "animals"
`)
}
}
31 changes: 28 additions & 3 deletions spec/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ type cliMock struct {

func (m *cliMock) setup(args []string) {
m.connection = &mockDBConnection{
recorder: []string{},
recorder: []string{},
assertedSQLIndex: 0,
}
m.errorWriter = bytes.Buffer{}
m.helpWriter = bytes.Buffer{}
Expand Down Expand Up @@ -158,13 +159,37 @@ func (m *cliMock) AssertSchemaMigrationTable(t *testing.T, schema string, expect
return false
}

// AssertSQLContains compares expected query with all recoreded queries.
// Assertions must happen in order the queries are executed, otherwise assertion fails.
func (m *cliMock) AssertSQLContains(t *testing.T, expected string) bool {
sql := strings.TrimSpace(strings.Join(m.connection.recorder, "\n"))
expected = strings.TrimSpace(expected)
return assert.Contains(t, sql, expected, fmt.Sprintf("SQL mismatch:\n%s\nwith:\n%s", expected, sql))
found := -1

// find matching query and remember last asserted query index
for i, sql := range m.connection.recorder {
if strings.Index(sql, expected) != -1 {
found = i
if i > m.connection.assertedSQLIndex {
m.connection.assertedSQLIndex = i
}
break
}
}

sql := ""
if found != -1 {
sql = m.connection.recorder[found]
// make sure assertion happen in the correct order
if found < m.connection.assertedSQLIndex {
return assert.True(t, false, "Queries asserted in the wrong order")
}
}

return assert.True(t, found != -1, fmt.Sprintf("SQL mismatch:\n%s\nwith:\n%s", expected, sql))
}

func (m *cliMock) ResetSQLRecorder() {
m.connection.assertedSQLIndex = 0
m.connection.recorder = []string{}
}

Expand Down
3 changes: 2 additions & 1 deletion spec/testdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
)

type mockDBConnection struct {
recorder []string
recorder []string
assertedSQLIndex int
}

func (m *mockDBConnection) Get(f func(db krabdb.DB) error) error {
Expand Down

0 comments on commit d88e899

Please sign in to comment.