github.com/jxskiss/gopkg@v0.17.3/sqlutil/batch_test.go (about)

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