cmd/schemagen: refactoring
- Replace log.Faltal with error wrapping in schemagen func - Simplify tests, use temp dir and ioutil functions, remove boilerplate code - In test use schemagen keyspace to avoid name conflict with examples - Change template
This commit is contained in:
committed by
Michal Jan Matczuk
parent
39bf42f122
commit
8477485a45
@@ -1,84 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/scylladb/gocqlx/v2/gocqlxtest"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/scylladb/gocqlx/v2/gocqlxtest"
|
||||
)
|
||||
|
||||
func TestCamelize(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
want string
|
||||
}{
|
||||
{"hello", "Hello"},
|
||||
{"_hello", "Hello"},
|
||||
{"__hello", "Hello"},
|
||||
{"hello_", "Hello"},
|
||||
{"hello_world", "HelloWorld"},
|
||||
{"hello__world", "HelloWorld"},
|
||||
{"_hello_world", "HelloWorld"},
|
||||
{"helloWorld", "HelloWorld"},
|
||||
{"HelloWorld", "HelloWorld"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.input, func(t *testing.T) {
|
||||
if got := camelize(tt.input); got != tt.want {
|
||||
t.Errorf("camelize() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
var flagUpdate = flag.Bool("update", false, "update golden file")
|
||||
|
||||
func Test_schemagen_defaultParams(t *testing.T) {
|
||||
cleanup(t, "models")
|
||||
defer cleanup(t, "models")
|
||||
func TestSchemagen(t *testing.T) {
|
||||
flag.Parse()
|
||||
createTestSchema(t)
|
||||
runSchemagen(t, "", "")
|
||||
assertResult(t, "models", "models")
|
||||
}
|
||||
b := runSchemagen(t, "foobar")
|
||||
|
||||
func Test_schemagen_customParams(t *testing.T) {
|
||||
cleanup(t, "asdf")
|
||||
defer cleanup(t, "asdf")
|
||||
createTestSchema(t)
|
||||
runSchemagen(t, "qwer", "asdf")
|
||||
assertResult(t, "qwer", "asdf")
|
||||
}
|
||||
|
||||
func cleanup(t *testing.T, output string) {
|
||||
err := os.RemoveAll(output)
|
||||
const goldenFile = "testdata/models.go.txt"
|
||||
if *flagUpdate {
|
||||
if err := ioutil.WriteFile(goldenFile, b, os.ModePerm); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
golden, err := ioutil.ReadFile(goldenFile)
|
||||
if err != nil {
|
||||
t.Fatalf("could not delete %s directory: %v\n", output, err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = os.Remove("./schemagen")
|
||||
if err != nil {
|
||||
t.Fatalf("could not delete binary: %v\n", err)
|
||||
}
|
||||
|
||||
cmd := exec.Command("go", "build")
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("could not build binary for schemagen: %v\nOutput:\n%v\n", err, string(out))
|
||||
if diff := cmp.Diff(string(golden), string(b)); diff != "" {
|
||||
t.Fatalf(diff)
|
||||
}
|
||||
}
|
||||
|
||||
func createTestSchema(t *testing.T) {
|
||||
t.Helper()
|
||||
|
||||
session := gocqlxtest.CreateSession(t)
|
||||
defer session.Close()
|
||||
|
||||
err := session.ExecStmt(`CREATE KEYSPACE IF NOT EXISTS examples WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}`)
|
||||
err := session.ExecStmt(`CREATE KEYSPACE IF NOT EXISTS schemagen WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}`)
|
||||
if err != nil {
|
||||
t.Fatal("create keyspace:", err)
|
||||
}
|
||||
|
||||
err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS examples.songs (
|
||||
err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS schemagen.songs (
|
||||
id uuid PRIMARY KEY,
|
||||
title text,
|
||||
album text,
|
||||
@@ -89,7 +56,7 @@ func createTestSchema(t *testing.T) {
|
||||
t.Fatal("create table:", err)
|
||||
}
|
||||
|
||||
err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS examples.playlists (
|
||||
err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS schemagen.playlists (
|
||||
id uuid,
|
||||
title text,
|
||||
album text,
|
||||
@@ -101,59 +68,27 @@ func createTestSchema(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func runSchemagen(t *testing.T, pkgname, output string) {
|
||||
dir, err := os.Getwd()
|
||||
func runSchemagen(t *testing.T, pkgname string) []byte {
|
||||
t.Helper()
|
||||
|
||||
dir, err := os.MkdirTemp("", "gocqlx")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
keyspace := "schemagen"
|
||||
|
||||
args := []string{"-keyspace=examples"}
|
||||
for _, arg := range os.Args {
|
||||
if strings.HasPrefix(arg, "-cluster") {
|
||||
args = append(args, arg)
|
||||
}
|
||||
flagKeyspace = &keyspace
|
||||
flagPkgname = &pkgname
|
||||
flagOutput = &dir
|
||||
|
||||
if err := schemagen(); err != nil {
|
||||
t.Fatalf("schemagen() error %s", err)
|
||||
}
|
||||
|
||||
if pkgname != "" {
|
||||
args = append(args, fmt.Sprintf("-pkgname=%s", pkgname))
|
||||
}
|
||||
|
||||
if output != "" {
|
||||
args = append(args, fmt.Sprintf("-output=%s", output))
|
||||
}
|
||||
|
||||
cmd := exec.Command(path.Join(dir, "schemagen"), args...)
|
||||
err = cmd.Run()
|
||||
f := fmt.Sprintf("%s/%s.go", dir, pkgname)
|
||||
b, err := os.ReadFile(f)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
t.Fatalf("%s: %s", f, err)
|
||||
}
|
||||
}
|
||||
|
||||
func assertResult(t *testing.T, pkgname, output string) {
|
||||
path := fmt.Sprintf("%s/%s.go", output, pkgname)
|
||||
res, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("can't read output file (%s): %s\n", path, err)
|
||||
}
|
||||
|
||||
want := resultWant(t, pkgname)
|
||||
|
||||
if string(res) != want {
|
||||
t.Fatalf("unexpected result: %s\nWanted:\n%s\n", string(res), want)
|
||||
}
|
||||
}
|
||||
|
||||
func resultWant(t *testing.T, pkgname string) string {
|
||||
f, err := os.Open("testdata/models.go.txt")
|
||||
if err != nil {
|
||||
t.Fatalf("can't open testdata/models.go.txt")
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
b, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
t.Fatalf("can't read testdata/models.go.txt")
|
||||
}
|
||||
|
||||
return strings.Replace(string(b), "{{pkgname}}", pkgname, 1)
|
||||
return b
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user