lint: convert snake to camel case
This commit is contained in:
@@ -114,8 +114,8 @@ var Checks = []Check{
|
||||
}
|
||||
|
||||
// Check if target column is a primary key
|
||||
for _, target_col := range s.Tables[column.ForeignKeyTargetTable].Columns {
|
||||
if target_col.Name == column.ForeignKeyTargetColumn && target_col.IsPrimaryKey {
|
||||
for _, targetCol := range s.Tables[column.ForeignKeyTargetTable].Columns {
|
||||
if targetCol.Name == column.ForeignKeyTargetColumn && targetCol.IsPrimaryKey {
|
||||
continue fk_loop
|
||||
}
|
||||
}
|
||||
@@ -125,8 +125,8 @@ var Checks = []Check{
|
||||
if idx.TableName != column.ForeignKeyTargetTable {
|
||||
continue
|
||||
}
|
||||
for _, idx_col_name := range idx.Columns {
|
||||
if column.ForeignKeyTargetColumn == idx_col_name {
|
||||
for _, idxColName := range idx.Columns {
|
||||
if column.ForeignKeyTargetColumn == idxColName {
|
||||
continue fk_loop
|
||||
}
|
||||
break // Only look at 1st column
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
func TestFailureCases(t *testing.T) {
|
||||
test_cases := []struct {
|
||||
testCases := []struct {
|
||||
sqlFile string
|
||||
expectedFailures []string
|
||||
}{
|
||||
@@ -34,24 +34,24 @@ func TestFailureCases(t *testing.T) {
|
||||
}},
|
||||
}
|
||||
|
||||
for _, test_case := range test_cases {
|
||||
t.Run(test_case.sqlFile, func(t *testing.T) {
|
||||
schema, err := schema.SchemaFromSQLFile(test_case.sqlFile)
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.sqlFile, func(t *testing.T) {
|
||||
schema, err := schema.SchemaFromSQLFile(testCase.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)
|
||||
isFailure := len(results) > 0
|
||||
isFailureExpected := slices.Contains(testCase.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)
|
||||
if isFailure != isFailureExpected {
|
||||
if isFailureExpected {
|
||||
t.Errorf("Expected check '%s' to fail, but it passed: %s", check.Name, testCase.sqlFile)
|
||||
} else {
|
||||
t.Errorf("Expected check '%s' to pass, but it failed: %s (%q.%q)",
|
||||
check.Name,
|
||||
test_case.sqlFile,
|
||||
testCase.sqlFile,
|
||||
results[0].TableName, results[0].ColumnName,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -15,12 +15,12 @@ import (
|
||||
)
|
||||
|
||||
//go:embed views.sql
|
||||
var create_views string
|
||||
var createViews string
|
||||
|
||||
// InitDB creates an in-memory DB from a given schema string.
|
||||
func InitDB(sql_schema string) *sqlx.DB {
|
||||
func InitDB(sqlSchema string) *sqlx.DB {
|
||||
db := sqlx.MustOpen("sqlite3", ":memory:")
|
||||
db.MustExec(sql_schema)
|
||||
db.MustExec(sqlSchema)
|
||||
return db
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ func SchemaFromSQLFile(filepath string) (Schema, error) {
|
||||
// SchemaFromDB takes a DB connection, checks its schema metadata tables, and returns a Schema.
|
||||
func SchemaFromDB(db *sqlx.DB) Schema {
|
||||
ret := Schema{Tables: map[string]Table{}, Indexes: map[string]Index{}}
|
||||
db.MustExec(create_views)
|
||||
db.MustExec(createViews)
|
||||
|
||||
var tables []Table
|
||||
must.Do(db.Select(&tables, `select name, table_type, is_strict, is_without_rowid from tables`))
|
||||
|
||||
@@ -12,15 +12,15 @@ import (
|
||||
|
||||
func TestParseSchema(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
schema_sql, err := os.ReadFile("../../sample_data/test_schemas/food.sql")
|
||||
schemaSQL, err := os.ReadFile("../../sample_data/test_schemas/food.sql")
|
||||
require.NoError(t, err)
|
||||
|
||||
db := schema.InitDB(string(schema_sql))
|
||||
db := schema.InitDB(string(schemaSQL))
|
||||
schema := schema.SchemaFromDB(db)
|
||||
expected_tbls := []string{"food_types", "foods", "units", "ingredients", "recipes", "iterations", "db_version"}
|
||||
for _, tbl_name := range expected_tbls {
|
||||
_, is_ok := schema.Tables[tbl_name]
|
||||
assert.True(is_ok)
|
||||
expectedTbls := []string{"food_types", "foods", "units", "ingredients", "recipes", "iterations", "db_version"}
|
||||
for _, tblName := range expectedTbls {
|
||||
_, isOk := schema.Tables[tblName]
|
||||
assert.True(isOk)
|
||||
}
|
||||
|
||||
foods := schema.Tables["foods"]
|
||||
@@ -57,23 +57,23 @@ func TestParseSchema(t *testing.T) {
|
||||
// -------
|
||||
|
||||
// Unique index
|
||||
units_name_idx, isOk := schema.Indexes["sqlite_autoindex_units_1"]
|
||||
unitsNameIdx, isOk := schema.Indexes["sqlite_autoindex_units_1"]
|
||||
require.True(t, isOk)
|
||||
assert.True(units_name_idx.IsUnique)
|
||||
assert.Equal("units", units_name_idx.TableName)
|
||||
assert.Equal([]string{"name"}, units_name_idx.Columns)
|
||||
assert.True(unitsNameIdx.IsUnique)
|
||||
assert.Equal("units", unitsNameIdx.TableName)
|
||||
assert.Equal([]string{"name"}, unitsNameIdx.Columns)
|
||||
|
||||
// Non-unique, declared index
|
||||
foods_protein_idx, isOk := schema.Indexes["foods_protein"]
|
||||
foodsProteinIdx, isOk := schema.Indexes["foods_protein"]
|
||||
require.True(t, isOk)
|
||||
assert.False(foods_protein_idx.IsUnique)
|
||||
assert.Equal("foods", foods_protein_idx.TableName)
|
||||
assert.Equal([]string{"protein"}, foods_protein_idx.Columns)
|
||||
assert.False(foodsProteinIdx.IsUnique)
|
||||
assert.Equal("foods", foodsProteinIdx.TableName)
|
||||
assert.Equal([]string{"protein"}, foodsProteinIdx.Columns)
|
||||
|
||||
// Multi-column index
|
||||
ingr_seq_idx, isOk := schema.Indexes["sqlite_autoindex_ingredients_1"]
|
||||
ingrSeqIdx, isOk := schema.Indexes["sqlite_autoindex_ingredients_1"]
|
||||
require.True(t, isOk)
|
||||
assert.True(ingr_seq_idx.IsUnique)
|
||||
assert.Equal("ingredients", ingr_seq_idx.TableName)
|
||||
assert.Equal([]string{"in_recipe_id", "list_order"}, ingr_seq_idx.Columns)
|
||||
assert.True(ingrSeqIdx.IsUnique)
|
||||
assert.Equal("ingredients", ingrSeqIdx.TableName)
|
||||
assert.Equal([]string{"in_recipe_id", "list_order"}, ingrSeqIdx.Columns)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user