-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathschema_columns_gen_test.go
167 lines (146 loc) · 4.02 KB
/
schema_columns_gen_test.go
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package gen
import (
"os"
"testing"
"time"
"github.com/kataras/pg"
)
// BaseEntity is a struct that defines common fields for all entities in the database.
// It has an ID field of type uuid that is the primary key, and two timestamp fields
// for tracking the creation and update times of each row.
type BaseEntity struct {
ID string `pg:"type=uuid,primary"`
CreatedAt time.Time `pg:"type=timestamp,default=clock_timestamp()"`
UpdatedAt time.Time `pg:"type=timestamp,default=clock_timestamp()"`
}
// Company is a struct that represents a company entity in the database.
type Company struct {
BaseEntity
Name string `pg:"type=varchar(255)"`
}
// Customer is a struct that represents a customer entity in the database.
// It embeds the BaseEntity struct and adds a CognitoUserID field of type uuid
// that is required and unique. It also specifies a conflict resolution strategy
// for the CognitoUserID field in case of duplicate values.
type Customer struct {
BaseEntity
// CognitoUserID string `pg:"type=uuid,conflict=DO UPDATE SET cognito_user_id=EXCLUDED.cognito_user_id"`
CognitoUserID string `pg:"type=uuid,unique_index=customer_unique_idx"`
Email string `pg:"type=varchar(255),unique_index=customer_unique_idx"` // optional: unique to allow upsert by "email"-only column confliction instead of the unique_index.
Name string `pg:"type=varchar(255)"`
CompanyID string `pg:"type=uuid,ref=companies(id)"`
}
func TestGenerateColumnsFromSchema(t *testing.T) {
const (
rootDir = "./_testdata"
)
defer func() {
os.RemoveAll(rootDir)
time.Sleep(1 * time.Second)
}()
schema := pg.NewSchema()
schema.MustRegister("companies", Company{})
schema.MustRegister("customers", Customer{})
opts := ExportOptions{
RootDir: rootDir,
}
if err := GenerateColumnsFromSchema(schema, opts); err != nil {
t.Fatal(err)
}
companyContents, err := os.ReadFile(rootDir + "/company.go")
if err != nil {
t.Fatal(err)
}
rootContents, err := os.ReadFile(rootDir + "/testdata.go")
if err != nil {
t.Fatal(err)
}
customerContents, err := os.ReadFile(rootDir + "/customer.go")
if err != nil {
t.Fatal(err)
}
const (
expectedRootContents = `// Code generated by pg. DO NOT EDIT.
package testdata
// Column is a struct that represents a column in a table.
type Column struct {
Name string
}
// String returns the name of the column.
func (c Column) String() string {
return c.Name
}
`
expectedCompanyContents = `// Code generated by pg. DO NOT EDIT.
package testdata
// Company is a struct value that represents a record in the companies table.
var Company = struct {
PG_TableName string
ID Column
CreatedAt Column
UpdatedAt Column
Name Column
}{
PG_TableName: "companies",
ID: Column{
Name: "id",
},
CreatedAt: Column{
Name: "created_at",
},
UpdatedAt: Column{
Name: "updated_at",
},
Name: Column{
Name: "name",
},
}
`
expectedCustomerContents = `// Code generated by pg. DO NOT EDIT.
package testdata
// Customer is a struct value that represents a record in the customers table.
var Customer = struct {
PG_TableName string
ID Column
CreatedAt Column
UpdatedAt Column
CognitoUserID Column
Email Column
Name Column
CompanyID Column
}{
PG_TableName: "customers",
ID: Column{
Name: "id",
},
CreatedAt: Column{
Name: "created_at",
},
UpdatedAt: Column{
Name: "updated_at",
},
CognitoUserID: Column{
Name: "cognito_user_id",
},
Email: Column{
Name: "email",
},
Name: Column{
Name: "name",
},
CompanyID: Column{
Name: "company_id",
},
}
`
)
if string(companyContents) != expectedCompanyContents {
t.Fatalf("expected company contents to be %q but got %q", expectedCompanyContents, companyContents)
}
if string(rootContents) != expectedRootContents {
t.Fatalf("expected root contents to be %q but got %q", expectedRootContents, rootContents)
}
if string(customerContents) != expectedCustomerContents {
t.Fatalf("expected customer contents to be %q but got %q", expectedCustomerContents, customerContents)
}
}