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