Fix ut
This commit is contained in:
		
							parent
							
								
									dac14f0b78
								
							
						
					
					
						commit
						636f90fbcd
					
				| @ -2,7 +2,9 @@ package callbacks | |||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"os" | ||||||
| 	"reflect" | 	"reflect" | ||||||
|  | 	"strconv" | ||||||
| 	"strings" | 	"strings" | ||||||
| 
 | 
 | ||||||
| 	"gorm.io/gorm" | 	"gorm.io/gorm" | ||||||
| @ -36,12 +38,21 @@ func BeforeCreate(db *gorm.DB) { | |||||||
| // Create create hook
 | // Create create hook
 | ||||||
| func Create(config *Config) func(db *gorm.DB) { | func Create(config *Config) func(db *gorm.DB) { | ||||||
| 	supportReturning := utils.Contains(config.CreateClauses, "RETURNING") | 	supportReturning := utils.Contains(config.CreateClauses, "RETURNING") | ||||||
|  | 	rawSupportReturning := supportReturning | ||||||
| 
 | 
 | ||||||
| 	return func(db *gorm.DB) { | 	return func(db *gorm.DB) { | ||||||
| 		if db.Error != nil { | 		if db.Error != nil { | ||||||
| 			return | 			return | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
|  | 		mock := os.Getenv("GORM_E2E_TEST_MOCK_CREATE_RETURNING") | ||||||
|  | 		mockSupportReturning, err := strconv.ParseBool(mock) | ||||||
|  | 		if err == nil { | ||||||
|  | 			supportReturning = mockSupportReturning | ||||||
|  | 		} else { | ||||||
|  | 			supportReturning = rawSupportReturning | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
| 		if db.Statement.Schema != nil { | 		if db.Statement.Schema != nil { | ||||||
| 			if !db.Statement.Unscoped { | 			if !db.Statement.Unscoped { | ||||||
| 				for _, c := range db.Statement.Schema.CreateClauses { | 				for _, c := range db.Statement.Schema.CreateClauses { | ||||||
|  | |||||||
| @ -6,6 +6,7 @@ import ( | |||||||
| 	"database/sql/driver" | 	"database/sql/driver" | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"os" | ||||||
| 	"regexp" | 	"regexp" | ||||||
| 	"testing" | 	"testing" | ||||||
| 	"time" | 	"time" | ||||||
| @ -553,19 +554,39 @@ func (m ConnPoolLastInsertIDMock) ExecContext(ctx context.Context, query string, | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestCreateWithDisableLastInsertID(t *testing.T) { | func TestCreateWithDisableLastInsertID(t *testing.T) { | ||||||
| 	rawPool := DB.ConnPool | 	mockCreateSupportReturning := func() func() { | ||||||
| 	DB.ConnPool = ConnPoolLastInsertIDMock{rawPool} | 		revertCreateSupportReturning := func() { | ||||||
| 	defer func() { | 			os.Setenv("GORM_E2E_TEST_MOCK_CREATE_RETURNING", "") | ||||||
| 		DB.ConnPool = rawPool | 		} | ||||||
| 	}() | 
 | ||||||
|  | 		os.Setenv("GORM_E2E_TEST_MOCK_CREATE_RETURNING", "false") | ||||||
|  | 		return revertCreateSupportReturning | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	mockConnPoolExec := func() func() { | ||||||
|  | 		rawPool := DB.ConnPool | ||||||
|  | 		DB.ConnPool = ConnPoolLastInsertIDMock{rawPool} | ||||||
|  | 		rawStatementPool := DB.Statement.ConnPool | ||||||
|  | 		DB.Statement.ConnPool = ConnPoolLastInsertIDMock{rawStatementPool} | ||||||
|  | 		return func() { | ||||||
|  | 			DB.ConnPool = rawPool | ||||||
|  | 			DB.Statement.ConnPool = rawStatementPool | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	defer mockCreateSupportReturning()() | ||||||
|  | 	defer mockConnPoolExec()() | ||||||
| 
 | 
 | ||||||
| 	user := &User{Name: "TestCreateWithDisableLastInsertID"} | 	user := &User{Name: "TestCreateWithDisableLastInsertID"} | ||||||
| 	err := DB.Create(&user).Error | 	err := DB.Create(&user).Error | ||||||
| 	if err == nil { | 	if DB.RowsAffected > 0 && err == nil { | ||||||
| 		t.Fatalf("it should be error") | 		t.Fatalf("it should be error") | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	DB.DisableLastInsertID = true | 	DB.DisableLastInsertID = true | ||||||
|  | 	defer func() { | ||||||
|  | 		DB.DisableLastInsertID = false | ||||||
|  | 	}() | ||||||
| 	err = DB.Create(&user).Error | 	err = DB.Create(&user).Error | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatalf("it should be nil") | 		t.Fatalf("it should be nil") | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Krisdiano
						Krisdiano