67 lines
1.9 KiB
Go
67 lines
1.9 KiB
Go
package lint_test
|
|
|
|
import (
|
|
"slices"
|
|
"testing"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/schema"
|
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/schema/lint"
|
|
)
|
|
|
|
func TestFailureCases(t *testing.T) {
|
|
test_cases := []struct {
|
|
sqlFile string
|
|
expectedFailures []string
|
|
}{
|
|
{"test_schemas/failure-has-foreign-key-no-index.sql", []string{"require_indexes_for_foreign_keys"}},
|
|
{"test_schemas/failure-has-ints.sql", []string{"forbid_int_type"}},
|
|
{"test_schemas/failure-has-nulls.sql", []string{"require_not_null"}},
|
|
{"test_schemas/failure-no-strict.sql", []string{"require_strict"}},
|
|
{"test_schemas/failure-total.sql", []string{
|
|
"require_not_null",
|
|
"require_explicit_primary_key",
|
|
"forbid_int_type",
|
|
"require_strict",
|
|
"require_indexes_for_foreign_keys",
|
|
}},
|
|
}
|
|
|
|
for _, test_case := range test_cases {
|
|
t.Run(test_case.sqlFile, func(t *testing.T) {
|
|
schema, err := schema.SchemaFromSQLFile(test_case.sqlFile)
|
|
require.NoError(t, err)
|
|
|
|
for _, check := range lint.Checks {
|
|
results := check.Execute(schema)
|
|
|
|
is_failure := len(results) > 0
|
|
is_failure_expected := slices.Contains(test_case.expectedFailures, check.Name)
|
|
|
|
if is_failure != is_failure_expected {
|
|
if is_failure_expected {
|
|
t.Errorf("Expected check '%s' to fail, but it passed: %s", check.Name, test_case.sqlFile)
|
|
} else {
|
|
t.Errorf("Expected check '%s' to pass, but it failed: %s (%q.%q)", check.Name, test_case.sqlFile, results[0].TableName, results[0].ColumnName)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSuccessCase(t *testing.T) {
|
|
file := "test_schemas/success.sql"
|
|
schema, err := schema.SchemaFromSQLFile(file)
|
|
require.NoError(t, err)
|
|
|
|
for _, check := range lint.Checks {
|
|
results := check.Execute(schema)
|
|
for _, r := range results {
|
|
t.Errorf("Unexpected error in file %q: %#v", file, r)
|
|
}
|
|
}
|
|
}
|