11package cache
22
33import (
4+ "errors"
45 "fmt"
56 "net"
67 "os"
@@ -133,7 +134,9 @@ func setupMySQL(t *testing.T) *gorm.DB {
133134 }
134135
135136 // Clean tables before each test
136- mysqlDB .Exec ("DROP TABLE IF EXISTS user_roles, users, user_sessions" )
137+ if err := mysqlDB .Exec ("DROP TABLE IF EXISTS user_roles, users, user_sessions" ).Error ; err != nil {
138+ t .Fatalf ("failed to drop MySQL tables: %v" , err )
139+ }
137140
138141 // Auto migrate
139142 err := mysqlDB .AutoMigrate (& UserRole {}, & User {}, & UserSession {})
@@ -213,7 +216,9 @@ func setupPostgreSQL(t *testing.T) *gorm.DB {
213216 }
214217
215218 // Clean tables before each test
216- pgDB .Exec ("DROP TABLE IF EXISTS user_roles, users, user_sessions CASCADE" )
219+ if err := pgDB .Exec ("DROP TABLE IF EXISTS user_roles, users, user_sessions CASCADE" ).Error ; err != nil {
220+ t .Fatalf ("failed to drop PostgreSQL tables: %v" , err )
221+ }
217222
218223 // Auto migrate
219224 err := pgDB .AutoMigrate (& UserRole {}, & User {}, & UserSession {})
@@ -314,7 +319,9 @@ func TestCompositePrimaryKey_MySQL(t *testing.T) {
314319 if err != nil {
315320 t .Fatalf ("Failed to create cache: %v" , err )
316321 }
317- db .Use (cache )
322+ if err := db .Use (cache ); err != nil {
323+ t .Fatalf ("failed to register cache plugin: %v" , err )
324+ }
318325
319326 // 创建测试数据
320327 userRoles := []UserRole {
@@ -379,7 +386,9 @@ func TestCompositePrimaryKey_PostgreSQL(t *testing.T) {
379386 if err != nil {
380387 t .Fatalf ("Failed to create cache: %v" , err )
381388 }
382- db .Use (cache )
389+ if err := db .Use (cache ); err != nil {
390+ t .Fatalf ("failed to register cache plugin: %v" , err )
391+ }
383392
384393 // 创建测试数据
385394 userRoles := []UserRole {
@@ -418,7 +427,9 @@ func TestUniqueKey_MySQL(t *testing.T) {
418427 if err != nil {
419428 t .Fatalf ("Failed to create cache: %v" , err )
420429 }
421- db .Use (cache )
430+ if err := db .Use (cache ); err != nil {
431+ t .Fatalf ("failed to register cache plugin: %v" , err )
432+ }
422433
423434 // 创建测试数据
424435 users := []User {
@@ -473,7 +484,9 @@ func TestCompositeUniqueKey_MySQL(t *testing.T) {
473484 if err != nil {
474485 t .Fatalf ("Failed to create cache: %v" , err )
475486 }
476- db .Use (cache )
487+ if err := db .Use (cache ); err != nil {
488+ t .Fatalf ("failed to register cache plugin: %v" , err )
489+ }
477490
478491 // 创建测试数据
479492 sessions := []UserSession {
@@ -519,7 +532,9 @@ func TestUniqueKey_PostgreSQL(t *testing.T) {
519532 if err != nil {
520533 t .Fatalf ("Failed to create cache: %v" , err )
521534 }
522- db .Use (cache )
535+ if err := db .Use (cache ); err != nil {
536+ t .Fatalf ("failed to register cache plugin: %v" , err )
537+ }
523538
524539 // 创建测试数据
525540 users := []User {
@@ -557,7 +572,9 @@ func TestCacheInvalidation_MySQL(t *testing.T) {
557572 if err != nil {
558573 t .Fatalf ("Failed to create cache: %v" , err )
559574 }
560- db .Use (cache )
575+ if err := db .Use (cache ); err != nil {
576+ t .Fatalf ("failed to register cache plugin: %v" , err )
577+ }
561578
562579 // 创建测试数据
563580 userRole := UserRole {UserID : 1 , RoleID : 1 , Name : "Admin" }
@@ -648,7 +665,9 @@ func TestCacheConsistency_Comprehensive_MySQL(t *testing.T) {
648665 if err != nil {
649666 t .Fatalf ("Failed to create cache: %v" , err )
650667 }
651- db .Use (cache )
668+ if err := db .Use (cache ); err != nil {
669+ t .Fatalf ("failed to register cache plugin: %v" , err )
670+ }
652671
653672 t .Run ("CreateAndQuery" , func (t * testing.T ) {
654673 // 创建数据后立即查询
@@ -674,7 +693,9 @@ func TestCacheConsistency_Comprehensive_MySQL(t *testing.T) {
674693 }
675694
676695 // 查询缓存
677- db .Where ("user_id = ? AND role_id = ?" , 100 , 200 ).First (& UserRole {})
696+ if err := db .Where ("user_id = ? AND role_id = ?" , 100 , 200 ).First (& UserRole {}).Error ; err != nil {
697+ t .Fatalf ("failed to warm cache: %v" , err )
698+ }
678699
679700 // 更新
680701 if err := db .Model (& UserRole {}).Where ("user_id = ? AND role_id = ?" , 100 , 200 ).Update ("name" , "Updated" ).Error ; err != nil {
@@ -720,7 +741,9 @@ func TestCacheConsistency_Comprehensive_MySQL(t *testing.T) {
720741 }
721742
722743 // 通过unique键查询缓存
723- db .Where ("email = ?" , "unique@test.com" ).First (& User {})
744+ if err := db .Where ("email = ?" , "unique@test.com" ).First (& User {}).Error ; err != nil {
745+ t .Fatalf ("failed to warm cache: %v" , err )
746+ }
724747
725748 // 更新
726749 if err := db .Model (& User {}).Where ("id = ?" , user .ID ).Update ("name" , "Updated" ).Error ; err != nil {
@@ -755,7 +778,9 @@ func TestCacheConsistency_Comprehensive_MySQL(t *testing.T) {
755778 }
756779
757780 // 查询缓存
758- db .Where ("user_id = ? AND role_id = ?" , 200 , 300 ).First (& UserRole {})
781+ if err := db .Where ("user_id = ? AND role_id = ?" , 200 , 300 ).First (& UserRole {}).Error ; err != nil {
782+ t .Fatalf ("failed to warm cache: %v" , err )
783+ }
759784
760785 // 删除
761786 if err := db .Where ("user_id = ? AND role_id = ?" , 200 , 300 ).Delete (& UserRole {}).Error ; err != nil {
@@ -765,7 +790,7 @@ func TestCacheConsistency_Comprehensive_MySQL(t *testing.T) {
765790 if err := waitForCondition (15 * time .Millisecond , 2 * time .Second , func () bool {
766791 var r UserRole
767792 err := db .Where ("user_id = ? AND role_id = ?" , 200 , 300 ).First (& r ).Error
768- return err != nil && err == gorm .ErrRecordNotFound
793+ return errors . Is ( err , gorm .ErrRecordNotFound )
769794 }); err != nil {
770795 t .Fatalf ("cache did not reflect delete: %v" , err )
771796 }
@@ -774,7 +799,7 @@ func TestCacheConsistency_Comprehensive_MySQL(t *testing.T) {
774799 var result UserRole
775800 if err := db .Where ("user_id = ? AND role_id = ?" , 200 , 300 ).First (& result ).Error ; err == nil {
776801 t .Error ("Expected error for deleted record" )
777- } else if err != gorm .ErrRecordNotFound {
802+ } else if ! errors . Is ( err , gorm .ErrRecordNotFound ) {
778803 t .Errorf ("Expected ErrRecordNotFound, got %v" , err )
779804 }
780805 })
@@ -812,7 +837,9 @@ func TestCacheConsistency_Advanced_MySQL(t *testing.T) {
812837 if err != nil {
813838 t .Fatalf ("Failed to create cache: %v" , err )
814839 }
815- db .Use (cache )
840+ if err := db .Use (cache ); err != nil {
841+ t .Fatalf ("failed to register cache plugin: %v" , err )
842+ }
816843
817844 t .Run ("BatchUpdate" , func (t * testing.T ) {
818845 // 批量更新
@@ -918,15 +945,17 @@ func TestCacheConsistency_Advanced_MySQL(t *testing.T) {
918945 t .Fatalf ("Failed to create: %v" , err )
919946 }
920947
921- db .Where ("user_id = ? AND role_id = ?" , 2000 , 3000 ).First (& UserRole {})
948+ if err := db .Where ("user_id = ? AND role_id = ?" , 2000 , 3000 ).First (& UserRole {}).Error ; err != nil {
949+ t .Fatalf ("failed to warm cache: %v" , err )
950+ }
922951
923952 if err := db .Where ("user_id = ? AND role_id = ?" , 2000 , 3000 ).Delete (& UserRole {}).Error ; err != nil {
924953 t .Fatalf ("Failed to delete: %v" , err )
925954 }
926955
927956 if err := waitForCondition (15 * time .Millisecond , 2 * time .Second , func () bool {
928957 var r UserRole
929- return db .Where ("user_id = ? AND role_id = ?" , 2000 , 3000 ).First (& r ).Error == gorm .ErrRecordNotFound
958+ return errors . Is ( db .Where ("user_id = ? AND role_id = ?" , 2000 , 3000 ).First (& r ).Error , gorm .ErrRecordNotFound )
930959 }); err != nil {
931960 t .Fatalf ("cache did not reflect delete: %v" , err )
932961 }
@@ -967,8 +996,12 @@ func TestCacheConsistency_Advanced_MySQL(t *testing.T) {
967996 t .Fatalf ("Failed to create: %v" , err )
968997 }
969998
970- db .Where ("email = ?" , "cross@test.com" ).First (& User {})
971- db .Where ("username = ?" , "crossuser" ).First (& User {})
999+ if err := db .Where ("email = ?" , "cross@test.com" ).First (& User {}).Error ; err != nil {
1000+ t .Fatalf ("failed to warm cache by email: %v" , err )
1001+ }
1002+ if err := db .Where ("username = ?" , "crossuser" ).First (& User {}).Error ; err != nil {
1003+ t .Fatalf ("failed to warm cache by username: %v" , err )
1004+ }
9721005
9731006 if err := db .Model (& User {}).Where ("id = ?" , user .ID ).Update ("name" , "Updated" ).Error ; err != nil {
9741007 t .Fatalf ("Failed to update: %v" , err )
0 commit comments