-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema_models_test.go
More file actions
80 lines (74 loc) · 1.99 KB
/
schema_models_test.go
File metadata and controls
80 lines (74 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package sqlproc
import (
"os"
"strings"
"testing"
)
func TestSchemaModelGenerator_WritesStructs(t *testing.T) {
t.Parallel()
dir := t.TempDir()
tables := []*Table{
{
Schema: "public",
Name: "users",
Columns: []TableColumn{
{Name: "id", DBType: "int4", Nullable: false},
{Name: "email", DBType: "text", Nullable: true},
{Name: "created_at", DBType: "timestamptz", Nullable: false},
},
},
}
gen := &SchemaModelGenerator{
Options: SchemaModelOptions{
OutputDir: dir,
PackageName: "models",
StructTag: "db,json",
},
}
files, err := gen.Generate(tables)
if err != nil {
t.Fatalf("generate: %v", err)
}
if len(files) != 1 {
t.Fatalf("expected 1 file, got %d", len(files))
}
content, err := os.ReadFile(files[0])
if err != nil {
t.Fatalf("read file: %v", err)
}
src := string(content)
if !strings.Contains(src, "type Users struct") {
t.Fatalf("expected Users struct in output: %s", src)
}
if !strings.Contains(src, "`db:\"id\" json:\"id\"`") {
t.Fatalf("expected struct tags in output: %s", src)
}
if !strings.Contains(src, "*string") {
t.Fatalf("expected nullable column to generate pointer type: %s", src)
}
if !strings.Contains(src, "`db:\"created_at\" json:\"createdAt\"`") {
t.Fatalf("expected camelCase json tag: %s", src)
}
}
func TestBuildStructTag(t *testing.T) {
tag := buildStructTag([]string{"db", "json"}, "foo_bar")
want := "`db:\"foo_bar\" json:\"fooBar\"`"
if tag != want {
t.Fatalf("unexpected tag: got %s want %s", tag, want)
}
}
func TestGoTypeForColumn(t *testing.T) {
cases := []struct {
col TableColumn
want string
}{
{TableColumn{Name: "id", DBType: "int4", Nullable: false}, "int32"},
{TableColumn{Name: "payload", DBType: "jsonb", Nullable: true}, "[]byte"},
{TableColumn{Name: "published_at", DBType: "timestamp", Nullable: true}, "*time.Time"},
}
for _, c := range cases {
if got := goTypeForColumn(c.col); got != c.want {
t.Fatalf("goTypeForColumn(%v) = %s, want %s", c.col, got, c.want)
}
}
}