Files
gocqlx/cmd/schemagen/schemagen_test.go
2025-06-11 11:56:33 -04:00

227 lines
5.2 KiB
Go

package main
import (
"flag"
"fmt"
"os"
"strings"
"testing"
"github.com/gocql/gocql"
"github.com/google/go-cmp/cmp"
"github.com/scylladb/gocqlx/v3/gocqlxtest"
)
var flagUpdate = flag.Bool("update", false, "update golden file")
func TestSchemagen(t *testing.T) {
flag.Parse()
createTestSchema(t)
// add ignored types and table
*flagIgnoreNames = strings.Join([]string{
"composers",
"composers_by_name",
"label",
}, ",")
// NOTE Only this generated models is used in real tests.
t.Run("IgnoreIndexes", func(t *testing.T) {
*flagIgnoreIndexes = true
b := runSchemagen(t, "schemagentest")
assertDiff(t, b, "testdata/models.go")
})
t.Run("NoIgnoreIndexes", func(t *testing.T) {
*flagIgnoreIndexes = false
b := runSchemagen(t, "schemagentest")
assertDiff(t, b, "testdata/no_ignore_indexes/models.go")
})
}
func Test_usedInTables(t *testing.T) {
tests := map[string]struct {
columnValidator string
typeName string
}{
"matches given a frozen collection": {
columnValidator: "frozen<album>",
typeName: "album",
},
"matches given a set": {
columnValidator: "set<artist>",
typeName: "artist",
},
"matches given a list": {
columnValidator: "list<song>",
typeName: "song",
},
"matches given a tuple: first of two elements": {
columnValidator: "tuple<first, second>",
typeName: "first",
},
"matches given a tuple: second of two elements": {
columnValidator: "tuple<first, second>",
typeName: "second",
},
"matches given a tuple: first of three elements": {
columnValidator: "tuple<first, second, third>",
typeName: "first",
},
"matches given a tuple: second of three elements": {
columnValidator: "tuple<first, second, third>",
typeName: "second",
},
"matches given a tuple: third of three elements": {
columnValidator: "tuple<first, second, third>",
typeName: "third",
},
"matches given a frozen set": {
columnValidator: "set<frozen<album>>",
typeName: "album",
},
"matches snake_case names given a nested map": {
columnValidator: "map<album, tuple<first, map<map_key, map-value>, third>>",
typeName: "map_key",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
tables := map[string]*gocql.TableMetadata{
"table": {Columns: map[string]*gocql.ColumnMetadata{
"column": {Type: tt.columnValidator},
}},
}
if !usedInTables(tt.typeName, tables) {
t.Fatal()
}
})
}
t.Run("doesn't panic with empty type name", func(t *testing.T) {
tables := map[string]*gocql.TableMetadata{
"table": {Columns: map[string]*gocql.ColumnMetadata{
"column": {Type: "map<text, album>"},
}},
}
usedInTables("", tables)
})
}
func assertDiff(t *testing.T, actual []byte, goldenFile string) {
t.Helper()
if *flagUpdate {
if err := os.WriteFile(goldenFile, actual, os.ModePerm); err != nil {
t.Fatal(err)
}
}
golden, err := os.ReadFile(goldenFile)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(string(golden), string(actual)); diff != "" {
t.Fatal(diff)
}
}
func createTestSchema(t *testing.T) {
t.Helper()
session := gocqlxtest.CreateSession(t)
defer session.Close()
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 schemagen.songs (
id uuid PRIMARY KEY,
title text,
album text,
artist text,
duration duration,
tags set<text>,
data blob)`)
if err != nil {
t.Fatal("create table:", err)
}
err = session.ExecStmt(`CREATE TYPE IF NOT EXISTS schemagen.album (
name text,
songwriters set<text>,)`)
if err != nil {
t.Fatal("create type:", err)
}
err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS schemagen.playlists (
id uuid,
title text,
album frozen<album>,
artist text,
song_id uuid,
PRIMARY KEY (id, title, album, artist))`)
if err != nil {
t.Fatal("create table:", err)
}
err = session.ExecStmt(`CREATE INDEX IF NOT EXISTS songs_title ON schemagen.songs (title)`)
if err != nil {
t.Fatal("create index:", err)
}
err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS schemagen.composers (
id uuid PRIMARY KEY,
name text)`)
if err != nil {
t.Fatal("create table:", err)
}
err = session.ExecStmt(`CREATE MATERIALIZED VIEW IF NOT EXISTS schemagen.composers_by_name AS
SELECT id, name
FROM composers
WHERE id IS NOT NULL AND name IS NOT NULL
PRIMARY KEY (id, name)`)
if err != nil {
t.Fatal("create view:", err)
}
err = session.ExecStmt(`CREATE TYPE IF NOT EXISTS schemagen.label (
name text,
artists set<text>)`)
if err != nil {
t.Fatal("create type:", err)
}
}
func runSchemagen(t *testing.T, pkgname string) []byte {
t.Helper()
dir, err := os.MkdirTemp("", "gocqlx")
if err != nil {
t.Fatal(err)
}
keyspace := "schemagen"
cl := "127.0.1.1"
flagCluster = &cl
flagKeyspace = &keyspace
flagPkgname = &pkgname
flagOutput = &dir
if err := schemagen(); err != nil {
t.Fatalf("schemagen() error %s", err)
}
f := fmt.Sprintf("%s/%s.go", dir, pkgname)
b, err := os.ReadFile(f)
if err != nil {
t.Fatalf("%s: %s", f, err)
}
return b
}