From 2ac0696f4c73c4f9884314c69c47e8bcf6355eeb Mon Sep 17 00:00:00 2001 From: cruvie Date: Thu, 10 Apr 2025 22:52:53 +0800 Subject: [PATCH 1/2] testOmit --- go.mod | 30 +++++++++++++++--------------- main_test.go | 10 ++++++++-- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index f194634f..6234859e 100644 --- a/go.mod +++ b/go.mod @@ -1,13 +1,13 @@ module gorm.io/playground -go 1.22.0 +go 1.23.0 -toolchain go1.23.3 +toolchain go1.24.1 require ( gorm.io/driver/mysql v1.5.7 - gorm.io/driver/postgres v1.5.10 - gorm.io/driver/sqlite v1.5.6 + gorm.io/driver/postgres v1.5.11 + gorm.io/driver/sqlite v1.5.7 gorm.io/driver/sqlserver v1.5.4 gorm.io/gen v0.3.26 gorm.io/gorm v1.25.12 @@ -15,25 +15,25 @@ require ( require ( filippo.io/edwards25519 v1.1.0 // indirect - github.com/go-sql-driver/mysql v1.8.1 // indirect + github.com/go-sql-driver/mysql v1.9.2 // indirect github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect github.com/golang-sql/sqlexp v0.1.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect - github.com/jackc/pgx/v5 v5.7.1 // indirect + github.com/jackc/pgx/v5 v5.7.4 // indirect github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect - github.com/mattn/go-sqlite3 v1.14.24 // indirect - github.com/microsoft/go-mssqldb v1.7.2 // indirect - golang.org/x/crypto v0.29.0 // indirect - golang.org/x/mod v0.22.0 // indirect - golang.org/x/sync v0.9.0 // indirect - golang.org/x/sys v0.27.0 // indirect - golang.org/x/text v0.20.0 // indirect - golang.org/x/tools v0.27.0 // indirect - gorm.io/datatypes v1.2.4 // indirect + github.com/mattn/go-sqlite3 v1.14.27 // indirect + github.com/microsoft/go-mssqldb v1.8.0 // indirect + golang.org/x/crypto v0.37.0 // indirect + golang.org/x/mod v0.24.0 // indirect + golang.org/x/sync v0.13.0 // indirect + golang.org/x/sys v0.32.0 // indirect + golang.org/x/text v0.24.0 // indirect + golang.org/x/tools v0.32.0 // indirect + gorm.io/datatypes v1.2.5 // indirect gorm.io/hints v1.1.2 // indirect gorm.io/plugin/dbresolver v1.5.3 // indirect ) diff --git a/main_test.go b/main_test.go index 60a388f7..a093fd25 100644 --- a/main_test.go +++ b/main_test.go @@ -13,8 +13,14 @@ func TestGORM(t *testing.T) { DB.Create(&user) - var result User - if err := DB.First(&result, user.ID).Error; err != nil { + err := DB.Debug().Select("*").Omit("id").Omit("created_at").Omit("name"). + Where("name = ?", "jinzhu"). + Updates(user).Error + if err != nil { t.Errorf("Failed, got error: %v", err) } + /* + [0.883ms] [rows:1] UPDATE `users` SET `id`=1,`created_at`="2025-04-10 22:50:54.487",`updated_at`="2025-04-10 22:50:54.488",`deleted_at`=NULL,`age`=0,`birthday`=NULL,`company_id`=NULL,`manager_id`=NULL,`active`=false WHERE name = "jinzhu" AND `users`.`deleted_at` IS NULL AND `id` = 1 + --- PASS: TestGORM (0.00s) + */ } From ec90a0bb6dd9da01774523383183adc08573a15d Mon Sep 17 00:00:00 2001 From: cruvie Date: Thu, 10 Apr 2025 23:01:41 +0800 Subject: [PATCH 2/2] testOmit --- main_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/main_test.go b/main_test.go index a093fd25..d34211fb 100644 --- a/main_test.go +++ b/main_test.go @@ -1,7 +1,9 @@ package main import ( + "gorm.io/gorm" "testing" + "time" ) // GORM_REPO: https://github.com/go-gorm/gorm.git @@ -9,18 +11,62 @@ import ( // TEST_DRIVERS: sqlite, mysql, postgres, sqlserver func TestGORM(t *testing.T) { - user := User{Name: "jinzhu"} + preTime := time.Date(2025, 4, 10, 0, 0, 0, 0, time.UTC) + var preId uint = 10 + user := User{ + Model: gorm.Model{ + ID: preId, + CreatedAt: preTime, + }, + Name: "jinzhu", + } DB.Create(&user) + user2 := User{ + Model: gorm.Model{ + ID: 12, + CreatedAt: time.Date(2025, 4, 11, 0, 0, 0, 0, time.UTC), + }, + Name: "jinzhu", + } + err := DB.Debug().Select("*").Omit("id").Omit("created_at").Omit("name"). Where("name = ?", "jinzhu"). - Updates(user).Error + Updates(user2).Error if err != nil { t.Errorf("Failed, got error: %v", err) } + getUser := User{} + err = DB.First(&getUser, user2.ID).Error + if err != nil { + t.Errorf("Failed, got error: %v", err) + } + if getUser.ID != preId { + t.Errorf("id changed!!!") + } + if getUser.CreatedAt != preTime { + t.Errorf("created_at changed!!!") + } /* - [0.883ms] [rows:1] UPDATE `users` SET `id`=1,`created_at`="2025-04-10 22:50:54.487",`updated_at`="2025-04-10 22:50:54.488",`deleted_at`=NULL,`age`=0,`birthday`=NULL,`company_id`=NULL,`manager_id`=NULL,`active`=false WHERE name = "jinzhu" AND `users`.`deleted_at` IS NULL AND `id` = 1 - --- PASS: TestGORM (0.00s) + 2025/04/10 23:00:53 testing sqlite3... + === RUN TestGORM + + 2025/04/10 23:00:53 /Users/cruvie/cruvie-space/code-hub/open-source/playground/main_test.go:24 + [1.637ms] [rows:1] INSERT INTO `users` (`created_at`,`updated_at`,`deleted_at`,`name`,`age`,`birthday`,`company_id`,`manager_id`,`active`,`id`) VALUES ("2025-04-10 00:00:00","2025-04-10 23:00:53.669",NULL,"jinzhu",0,NULL,NULL,NULL,false,10) RETURNING `id` + + 2025/04/10 23:00:53 /Users/cruvie/cruvie-space/code-hub/open-source/playground/main_test.go:36 + [0.154ms] [rows:0] UPDATE `users` SET `id`=12,`created_at`="2025-04-11 00:00:00",`updated_at`="2025-04-10 23:00:53.671",`deleted_at`=NULL,`age`=0,`birthday`=NULL,`company_id`=NULL,`manager_id`=NULL,`active`=false WHERE name = "jinzhu" AND `users`.`deleted_at` IS NULL AND `id` = 12 + + 2025/04/10 23:00:53 /Users/cruvie/cruvie-space/code-hub/open-source/playground/main_test.go:41 record not found + [0.043ms] [rows:0] SELECT * FROM `users` WHERE `users`.`id` = 12 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1 + main_test.go:43: Failed, got error: record not found + main_test.go:46: id changed!!! + main_test.go:49: created_at changed!!! + --- FAIL: TestGORM (0.00s) + + FAIL + + 进程 已完成,退出代码为 1 */ }