gitee.com/eden-framework/sqlx@v0.0.3/builder/stmt_insert_test.go (about)

     1  package builder_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "gitee.com/eden-framework/sqlx/builder"
     7  	. "gitee.com/eden-framework/sqlx/builder/buidertestingutils"
     8  	"github.com/onsi/gomega"
     9  )
    10  
    11  func TestStmtInsert(t *testing.T) {
    12  	table := T("T", Col("f_a"), Col("f_b"))
    13  
    14  	t.Run("insert with modifier", func(t *testing.T) {
    15  		gomega.NewWithT(t).Expect(
    16  			Insert("IGNORE").
    17  				Into(table).
    18  				Values(Cols("f_a", "f_b"), 1, 2),
    19  		).To(BeExpr("INSERT IGNORE INTO T (f_a,f_b) VALUES (?,?)",
    20  			1, 2))
    21  	})
    22  
    23  	t.Run("insert simple", func(t *testing.T) {
    24  		gomega.NewWithT(t).Expect(
    25  			Insert().
    26  				Into(table, Comment("Comment")).
    27  				Values(Cols("f_a", "f_b"), 1, 2),
    28  		).To(BeExpr(`
    29  INSERT INTO T (f_a,f_b) VALUES (?,?)
    30  /* Comment */
    31  `, 1, 2))
    32  	})
    33  
    34  	t.Run("multiple insert", func(t *testing.T) {
    35  		gomega.NewWithT(t).Expect(
    36  			Insert().
    37  				Into(table).
    38  				Values(Cols("f_a", "f_b"), 1, 2, 1, 2, 1, 2),
    39  		).To(BeExpr("INSERT INTO T (f_a,f_b) VALUES (?,?),(?,?),(?,?)", 1, 2, 1, 2, 1, 2))
    40  	})
    41  
    42  	t.Run("insert from select", func(t *testing.T) {
    43  		gomega.NewWithT(t).Expect(
    44  			Insert().
    45  				Into(table).
    46  				Values(Cols("f_a", "f_b"), Select(Cols("f_a", "f_b")).From(table, Where(table.Col("f_a").Eq(1)))),
    47  		).To(BeExpr(`
    48  INSERT INTO T (f_a,f_b) SELECT f_a,f_b FROM T
    49  WHERE f_a = ?
    50  `, 1))
    51  	})
    52  }