83 lines
2.4 KiB
Go
83 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.warky.dev/wdevs/relspecgo/pkg/models"
|
|
)
|
|
|
|
func TestLowercaseFlagAndFileOutput(t *testing.T) {
|
|
prior := lowercaseIdentifiers
|
|
t.Cleanup(func() { lowercaseIdentifiers = prior })
|
|
for _, cmd := range []struct {
|
|
name string
|
|
flag bool
|
|
}{{"convert", convertCmd.Flags().Lookup("lowercase") != nil}, {"batch", batchCmd.Flags().Lookup("lowercase") != nil}, {"merge", mergeCmd.Flags().Lookup("lowercase") != nil}, {"templ", templCmd.Flags().Lookup("lowercase") != nil}, {"job", jobCmd.PersistentFlags().Lookup("lowercase") != nil}} {
|
|
if !cmd.flag {
|
|
t.Fatalf("missing %s --lowercase", cmd.name)
|
|
}
|
|
}
|
|
if err := convertCmd.Flags().Set("lowercase", "true"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := filepath.Join(t.TempDir(), "schema.sql")
|
|
db := models.InitDatabase("DB")
|
|
s := models.InitSchema("App")
|
|
table := models.InitTable("Users", "App")
|
|
col := models.InitColumn("Name", "Users", "App")
|
|
col.Type = "text"
|
|
col.Default = "Keep CASE"
|
|
table.Columns[col.Name] = col
|
|
s.Tables = append(s.Tables, table)
|
|
db.Schemas = append(db.Schemas, s)
|
|
if err := writeDatabase(db, "pgsql", path, "", "", false, "", "", false, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(data), `"app"."users"`) || !strings.Contains(string(data), `"name" text DEFAULT 'Keep CASE'`) {
|
|
t.Fatalf("%s", data)
|
|
}
|
|
if table.Name != "Users" {
|
|
t.Fatal("input mutated")
|
|
}
|
|
}
|
|
|
|
func TestJobLowercaseOption(t *testing.T) {
|
|
prior := lowercaseIdentifiers
|
|
lowercaseIdentifiers = false
|
|
t.Cleanup(func() { lowercaseIdentifiers = prior })
|
|
dir := t.TempDir()
|
|
writeTestJSON(t, filepath.Join(dir, "input.json"), []string{"MixedCase"})
|
|
writeFile(t, filepath.Join(dir, "relspec.yml"), `version: 1
|
|
jobs:
|
|
lower:
|
|
command: convert
|
|
inputs:
|
|
- format: json
|
|
path: input.json
|
|
output:
|
|
format: json
|
|
path: output.json
|
|
options:
|
|
lowercase_identifiers: true
|
|
`)
|
|
set := mustLoadSet(t, filepath.Join(dir, "relspec.yml"))
|
|
if err := executeJobPlan(set, "lower", false, false, &bytes.Buffer{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, err := os.ReadFile(filepath.Join(dir, "output.json"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(data), "mixedcase") || strings.Contains(string(data), "MixedCase") {
|
|
t.Fatalf("%s", data)
|
|
}
|
|
}
|