Debian Codegraph @

tests/postgres_test.go

raw
1 package tests_test
2
3 import (
4 "testing"
5 "time"
6
7 "github.com/google/uuid"
8 "github.com/lib/pq"
9 "gorm.io/gorm"
10 "gorm.io/gorm/clause"
11 . "gorm.io/gorm/utils/tests"
12 )
13
14 func TestPostgresReturningIDWhichHasStringType(t *testing.T) {
15 if DB.Dialector.Name() != "postgres" {
16 t.Skip()
17 }
18
19 type Yasuo struct {
20 ID string `gorm:"default:gen_random_uuid()"`
21 Name string
22 CreatedAt time.Time `gorm:"type:TIMESTAMP WITHOUT TIME ZONE"`
23 UpdatedAt time.Time `gorm:"type:TIMESTAMP WITHOUT TIME ZONE;default:current_timestamp"`
24 }
25
26 if err := DB.Exec("CREATE EXTENSION IF NOT EXISTS pgcrypto;").Error; err != nil {
27 t.Errorf("Failed to create extension pgcrypto, got error %v", err)
28 }
29
30 DB.Migrator().DropTable(&Yasuo{})
31
32 if err := DB.AutoMigrate(&Yasuo{}); err != nil {
33 t.Fatalf("Failed to migrate for uuid default value, got error: %v", err)
34 }
35
36 yasuo := Yasuo{Name: "jinzhu"}
37 if err := DB.Create(&yasuo).Error; err != nil {
38 t.Fatalf("should be able to create data, but got %v", err)
39 }
40
41 if yasuo.ID == "" {
42 t.Fatal("should be able to has ID, but got zero value")
43 }
44
45 var result Yasuo
46 if err := DB.First(&result, "id = ?", yasuo.ID).Error; err != nil || yasuo.Name != "jinzhu" {
47 t.Errorf("No error should happen, but got %v", err)
48 }
49
50 if err := DB.Where("id = $1", yasuo.ID).First(&Yasuo{}).Error; err != nil || yasuo.Name != "jinzhu" {
51 t.Errorf("No error should happen, but got %v", err)
52 }
53
54 yasuo.Name = "jinzhu1"
55 if err := DB.Save(&yasuo).Error; err != nil {
56 t.Errorf("Failed to update date, got error %v", err)
57 }
58
59 if err := DB.First(&result, "id = ?", yasuo.ID).Error; err != nil || yasuo.Name != "jinzhu1" {
60 t.Errorf("No error should happen, but got %v", err)
61 }
62 }
63
64 func TestPostgres(t *testing.T) {
65 if DB.Dialector.Name() != "postgres" {
66 t.Skip()
67 }
68
69 type Harumph struct {
70 gorm.Model
71 Name string `gorm:"check:name_checker,name <> ''"`
72 Test uuid.UUID `gorm:"type:uuid;not null;default:gen_random_uuid()"`
73 CreatedAt time.Time `gorm:"type:TIMESTAMP WITHOUT TIME ZONE"`
74 UpdatedAt time.Time `gorm:"type:TIMESTAMP WITHOUT TIME ZONE;default:current_timestamp"`
75 Things pq.StringArray `gorm:"type:text[]"`
76 }
77
78 if err := DB.Exec("CREATE EXTENSION IF NOT EXISTS pgcrypto;").Error; err != nil {
79 t.Errorf("Failed to create extension pgcrypto, got error %v", err)
80 }
81
82 DB.Migrator().DropTable(&Harumph{})
83
84 if err := DB.AutoMigrate(&Harumph{}); err != nil {
85 t.Fatalf("Failed to migrate for uuid default value, got error: %v", err)
86 }
87
88 harumph := Harumph{}
89 if err := DB.Create(&harumph).Error; err == nil {
90 t.Fatalf("should failed to create data, name can't be blank")
91 }
92
93 harumph = Harumph{Name: "jinzhu"}
94 if err := DB.Create(&harumph).Error; err != nil {
95 t.Fatalf("should be able to create data, but got %v", err)
96 }
97
98 var result Harumph
99 if err := DB.First(&result, "id = ?", harumph.ID).Error; err != nil || harumph.Name != "jinzhu" {
100 t.Errorf("No error should happen, but got %v", err)
101 }
102
103 if err := DB.Where("id = $1", harumph.ID).First(&Harumph{}).Error; err != nil || harumph.Name != "jinzhu" {
104 t.Errorf("No error should happen, but got %v", err)
105 }
106
107 harumph.Name = "jinzhu1"
108 if err := DB.Save(&harumph).Error; err != nil {
109 t.Errorf("Failed to update date, got error %v", err)
110 }
111
112 if err := DB.First(&result, "id = ?", harumph.ID).Error; err != nil || harumph.Name != "jinzhu1" {
113 t.Errorf("No error should happen, but got %v", err)
114 }
115
116 DB.Migrator().DropTable("log_usage")
117
118 if err := DB.Exec(`
119 CREATE TABLE public.log_usage (
120 log_id bigint NOT NULL
121 );
122
123 ALTER TABLE public.log_usage ALTER COLUMN log_id ADD GENERATED BY DEFAULT AS IDENTITY (
124 SEQUENCE NAME public.log_usage_log_id_seq
125 START WITH 1
126 INCREMENT BY 1
127 NO MINVALUE
128 NO MAXVALUE
129 CACHE 1
130 );
131 `).Error; err != nil {
132 t.Fatalf("failed to create table, got error %v", err)
133 }
134
135 columns, err := DB.Migrator().ColumnTypes("log_usage")
136 if err != nil {
137 t.Fatalf("failed to get columns, got error %v", err)
138 }
139
140 hasLogID := false
141 for _, column := range columns {
142 if column.Name() == "log_id" {
143 hasLogID = true
144 autoIncrement, ok := column.AutoIncrement()
145 if !ok || !autoIncrement {
146 t.Fatalf("column log_id should be auto incrementment")
147 }
148 }
149 }
150
151 if !hasLogID {
152 t.Fatalf("failed to found column log_id")
153 }
154 }
155
156 type Post struct {
157 ID uuid.UUID `gorm:"primary_key;type:uuid;default:uuid_generate_v4();"`
158 Title string
159 Categories []*Category `gorm:"Many2Many:post_categories"`
160 }
161
162 type Category struct {
163 ID uuid.UUID `gorm:"primary_key;type:uuid;default:uuid_generate_v4();"`
164 Title string
165 Posts []*Post `gorm:"Many2Many:post_categories"`
166 }
167
168 func TestMany2ManyWithDefaultValueUUID(t *testing.T) {
169 if DB.Dialector.Name() != "postgres" {
170 t.Skip()
171 }
172
173 if err := DB.Exec(`create extension if not exists "uuid-ossp"`).Error; err != nil {
174 t.Fatalf("Failed to create 'uuid-ossp' extension, but got error %v", err)
175 }
176
177 DB.Migrator().DropTable(&Post{}, &Category{}, "post_categories")
178 DB.AutoMigrate(&Post{}, &Category{})
179
180 post := Post{
181 Title: "Hello World",
182 Categories: []*Category{
183 {Title: "Coding"},
184 {Title: "Golang"},
185 },
186 }
187
188 if err := DB.Create(&post).Error; err != nil {
189 t.Errorf("Failed, got error: %v", err)
190 }
191 }
192
193 func TestPostgresOnConstraint(t *testing.T) {
194 if DB.Dialector.Name() != "postgres" {
195 t.Skip()
196 }
197
198 type Thing struct {
199 gorm.Model
200 SomeID string
201 OtherID string
202 Data string
203 }
204
205 DB.Migrator().DropTable(&Thing{})
206 DB.Migrator().CreateTable(&Thing{})
207 if err := DB.Exec("ALTER TABLE things ADD CONSTRAINT some_id_other_id_unique UNIQUE (some_id, other_id)").Error; err != nil {
208 t.Error(err)
209 }
210
211 thing := Thing{
212 SomeID: "1234",
213 OtherID: "1234",
214 Data: "something",
215 }
216
217 DB.Create(&thing)
218
219 thing2 := Thing{
220 SomeID: "1234",
221 OtherID: "1234",
222 Data: "something else",
223 }
224
225 result := DB.Clauses(clause.OnConflict{
226 OnConstraint: "some_id_other_id_unique",
227 UpdateAll: true,
228 }).Create(&thing2)
229 if result.Error != nil {
230 t.Errorf("creating second thing: %v", result.Error)
231 }
232
233 var things []Thing
234 if err := DB.Find(&things).Error; err != nil {
235 t.Errorf("Failed, got error: %v", err)
236 }
237
238 if len(things) > 1 {
239 t.Errorf("expected 1 thing got more")
240 }
241 }
242
243 type CompanyNew struct {
244 ID int
245 Name int
246 }
247
248 func TestAlterColumnDataType(t *testing.T) {
249 DB.AutoMigrate(Company{})
250
251 if err := DB.Table("companies").Migrator().AlterColumn(CompanyNew{}, "name"); err != nil {
252 t.Fatalf("failed to alter column from string to int, got error %v", err)
253 }
254
255 DB.AutoMigrate(Company{})
256 }