github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/utils/sqlutil/batch_test.go (about) 1 package sqlutil 2 3 import ( 4 "context" 5 "database/sql" 6 "strings" 7 "testing" 8 "time" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 type TestObject struct { 14 ID *int64 15 COLUMN_1 int64 16 Column2 *string 17 privateField int 18 Column_3_ABC string 19 Column_4 int32 `gorm:"column:bling4"` 20 Column_5 *int32 `db:"bling5"` 21 Column_6 time.Time `gorm:"-"` 22 Column_7 *time.Time `` 23 Column_8 []byte `db:"-"` 24 } 25 26 func TestMakeBatchInsertSQL(t *testing.T) { 27 var rows1 []TestObject 28 for i := 0; i < 50; i++ { 29 rows1 = append(rows1, TestObject{}) 30 } 31 got1, args1 := MakeBatchInsertSQL(rows1) 32 want1 := "INSERT INTO test_object (id,column_1,column2,column_3_abc,bling4,bling5,column_7) VALUES " 33 varCount := 50 * 7 34 35 assert.Contains(t, got1, want1) 36 assert.Len(t, args1, varCount) 37 assert.Equal(t, varCount, countPlaceholder(got1)) 38 39 var rows2 []*TestObject 40 for i := 0; i < 50; i++ { 41 rows2 = append(rows2, &TestObject{}) 42 } 43 got2, args2 := MakeBatchInsertSQL(rows2, WithContext(context.Background()), WithTable("dummy_table"), WithIgnore()) 44 want2 := "INSERT IGNORE INTO dummy_table (id,column_1,column2,column_3_abc,bling4,bling5,column_7) VALUES " 45 46 assert.Contains(t, got2, want2) 47 assert.Len(t, args2, varCount) 48 assert.Equal(t, varCount, countPlaceholder(got2)) 49 50 var rows3 []*TestObject 51 for i := 0; i < 50; i++ { 52 rows3 = append(rows3, &TestObject{}) 53 } 54 got3, args3 := MakeBatchInsertSQL(rows3, WithQuote("`")) 55 want3 := "INSERT INTO `test_object` (`id`,`column_1`,`column2`,`column_3_abc`,`bling4`,`bling5`,`column_7`) VALUES " 56 57 assert.Contains(t, got3, want3) 58 assert.Len(t, args3, varCount) 59 assert.Equal(t, varCount, countPlaceholder(got3)) 60 } 61 62 func TestMakeBatchInsertSQL_OmitCols(t *testing.T) { 63 var rows1 []*TestObject 64 for i := 0; i < 50; i++ { 65 rows1 = append(rows1, &TestObject{}) 66 } 67 got1, args1 := MakeBatchInsertSQL(rows1, OmitColumns("id", "column_1", "bling4")) 68 want1 := "INSERT INTO test_object (column2,column_3_abc,bling5,column_7) VALUES " 69 varCount := 50 * 4 70 71 assert.Contains(t, got1, want1) 72 assert.Len(t, args1, varCount) 73 assert.Equal(t, varCount, countPlaceholder(got1)) 74 } 75 76 func TestMakeBatchInsertSQL_Panic(t *testing.T) { 77 for _, test := range []any{ 78 nil, 79 TestObject{}, 80 &TestObject{}, 81 []int64{1, 2, 3}, 82 []TestObject{}, 83 []*TestObject{}, 84 } { 85 assert.Panics(t, func() { _, _ = MakeBatchInsertSQL(test) }) 86 _, err := BatchInsert(dummyExecutor{}, test) 87 t.Log(err) 88 assert.NotNil(t, err) 89 assert.True(t, strings.HasPrefix(err.Error(), "BatchInsert: ")) 90 } 91 } 92 93 func countPlaceholder(query string) int { 94 return len(strings.Split(query, "?")) - 1 95 } 96 97 type dummyExecutor struct{} 98 99 func (p dummyExecutor) Exec(query string, args ...any) (sql.Result, error) { 100 return dummyResult{}, nil 101 } 102 103 func (p dummyExecutor) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) { 104 return dummyResult{}, nil 105 } 106 107 type dummyResult struct{} 108 109 func (d dummyResult) LastInsertId() (int64, error) { return 0, nil } 110 111 func (d dummyResult) RowsAffected() (int64, error) { return 0, nil }