github.com/octohelm/storage@v0.0.0-20240516030302-1ac2cc1ea347/pkg/sqlbuilder/stmt_insert_test.go (about)

     1  package sqlbuilder_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/octohelm/storage/internal/testutil"
     7  	. "github.com/octohelm/storage/pkg/sqlbuilder"
     8  )
     9  
    10  func TestStmtInsert(t *testing.T) {
    11  	table := T("T", Col("f_a"), Col("f_b"))
    12  
    13  	t.Run("insert with modifier", func(t *testing.T) {
    14  		testutil.ShouldBeExpr(t,
    15  			Insert("IGNORE").
    16  				Into(table).
    17  				Values(Cols("f_a", "f_b"), 1, 2),
    18  
    19  			"INSERT IGNORE INTO T (f_a,f_b) VALUES (?,?)", 1, 2)
    20  	})
    21  
    22  	t.Run("insert simple", func(t *testing.T) {
    23  		testutil.ShouldBeExpr(t,
    24  			Insert().
    25  				Into(table, Comment("Comment")).
    26  				Values(Cols("f_a", "f_b"), 1, 2),
    27  			`
    28  INSERT INTO T (f_a,f_b) VALUES (?,?)
    29  /* Comment */
    30  `, 1, 2)
    31  	})
    32  
    33  	t.Run("multiple insert", func(t *testing.T) {
    34  		testutil.ShouldBeExpr(t,
    35  			Insert().
    36  				Into(table).
    37  				Values(Cols("f_a", "f_b"), 1, 2, 1, 2, 1, 2),
    38  			"INSERT INTO T (f_a,f_b) VALUES (?,?),(?,?),(?,?)", 1, 2, 1, 2, 1, 2)
    39  	})
    40  
    41  	t.Run("insert from select", func(t *testing.T) {
    42  		testutil.ShouldBeExpr(t,
    43  			Insert().
    44  				Into(table).
    45  				Values(Cols("f_a", "f_b"),
    46  					Select(Cols("f_a", "f_b")).
    47  						From(table, Where(TypedColOf[int](table, "f_a").V(Eq(1))))),
    48  			`
    49  INSERT INTO T (f_a,f_b) SELECT f_a,f_b FROM T
    50  WHERE f_a = ?
    51  `, 1)
    52  	})
    53  }