Files
gocqlx/cmd/schemagen/schemagen_test.go

214 lines
4.8 KiB
Go
Raw Normal View History

2021-11-13 13:55:44 +02:00
package main
import (
"flag"
2021-11-13 13:55:44 +02:00
"fmt"
"os"
"strings"
2021-11-13 13:55:44 +02:00
"testing"
"github.com/gocql/gocql"
"github.com/google/go-cmp/cmp"
"github.com/scylladb/gocqlx/v3/gocqlxtest"
)
2021-11-13 13:55:44 +02:00
var flagUpdate = flag.Bool("update", false, "update golden file")
2021-11-13 13:55:44 +02:00
func TestSchemagen(t *testing.T) {
flag.Parse()
2021-11-13 13:55:44 +02:00
createTestSchema(t)
// add ignored types and table
*flagIgnoreNames = strings.Join([]string{
"composers",
"composers_by_name",
"label",
}, ",")
*flagIgnoreIndexes = true
2024-06-26 13:32:47 -04:00
b := runSchemagen(t, "schemagentest")
2021-11-13 13:55:44 +02:00
2024-06-26 13:32:47 -04:00
const goldenFile = "testdata/models.go"
if *flagUpdate {
if err := os.WriteFile(goldenFile, b, os.ModePerm); err != nil {
t.Fatal(err)
}
2021-11-13 13:55:44 +02:00
}
golden, err := os.ReadFile(goldenFile)
2021-11-13 13:55:44 +02:00
if err != nil {
t.Fatal(err)
2021-11-13 13:55:44 +02:00
}
if diff := cmp.Diff(string(golden), string(b)); diff != "" {
t.Fatal(diff)
2021-11-13 13:55:44 +02:00
}
}
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)
})
}
2021-11-13 13:55:44 +02:00
func createTestSchema(t *testing.T) {
t.Helper()
2021-11-13 13:55:44 +02:00
session := gocqlxtest.CreateSession(t)
defer session.Close()
err := session.ExecStmt(`CREATE KEYSPACE IF NOT EXISTS schemagen WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}`)
2021-11-13 13:55:44 +02:00
if err != nil {
t.Fatal("create keyspace:", err)
}
err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS schemagen.songs (
2021-11-13 13:55:44 +02:00
id uuid PRIMARY KEY,
title text,
album text,
artist text,
2024-06-26 13:32:47 -04:00
duration duration,
2021-11-13 13:55:44 +02:00
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 (
2021-11-13 13:55:44 +02:00
id uuid,
title text,
album frozen<album>,
2021-11-13 13:55:44 +02:00
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)
}
2021-11-13 13:55:44 +02:00
}
func runSchemagen(t *testing.T, pkgname string) []byte {
t.Helper()
2021-11-13 13:55:44 +02:00
dir, err := os.MkdirTemp("", "gocqlx")
2021-11-13 13:55:44 +02:00
if err != nil {
t.Fatal(err)
}
keyspace := "schemagen"
2024-06-26 13:32:47 -04:00
cl := "127.0.1.1"
2021-11-13 13:55:44 +02:00
2024-06-26 13:32:47 -04:00
flagCluster = &cl
flagKeyspace = &keyspace
flagPkgname = &pkgname
flagOutput = &dir
2021-11-13 13:55:44 +02:00
if err := schemagen(); err != nil {
t.Fatalf("schemagen() error %s", err)
2021-11-13 13:55:44 +02:00
}
f := fmt.Sprintf("%s/%s.go", dir, pkgname)
b, err := os.ReadFile(f)
2021-11-13 13:55:44 +02:00
if err != nil {
t.Fatalf("%s: %s", f, err)
2021-11-13 13:55:44 +02:00
}
return b
2021-11-13 13:55:44 +02:00
}