github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/sqlx/builder/builder_z_stmt_test.go (about) 1 package builder_test 2 3 import ( 4 "testing" 5 6 "github.com/onsi/gomega" 7 8 . "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder" 9 . "github.com/machinefi/w3bstream/pkg/depends/testutil/buildertestutil" 10 ) 11 12 func TestStmtDelete(t *testing.T) { 13 table := T("T") 14 15 t.Run("delete", func(t *testing.T) { 16 gomega.NewWithT(t).Expect( 17 Delete().From(table, 18 Where(Col("F_a").Eq(1)), 19 Comment("Comment"), 20 ), 21 ).To(BeExpr(` 22 DELETE FROM T 23 WHERE f_a = ? 24 /* Comment */ 25 `, 1)) 26 }) 27 } 28 29 func TestStmtInsert(t *testing.T) { 30 table := T("T", Col("f_a"), Col("f_b")) 31 32 t.Run("Simple", func(t *testing.T) { 33 gomega.NewWithT(t).Expect( 34 Insert(). 35 Into(table, Comment("Comment")). 36 Values(Cols("f_a", "f_b"), 1, 2), 37 ).To(BeExpr(` 38 INSERT INTO T (f_a,f_b) VALUES (?,?) 39 /* Comment */ 40 `, 1, 2)) 41 }) 42 43 t.Run("WithModifier", func(t *testing.T) { 44 gomega.NewWithT(t).Expect( 45 Insert("IGNORE"). 46 Into(table). 47 Values(Cols("f_a", "f_b"), 1, 2), 48 ).To(BeExpr("INSERT IGNORE INTO T (f_a,f_b) VALUES (?,?)", 49 1, 2)) 50 }) 51 52 t.Run("MultiInsert", func(t *testing.T) { 53 gomega.NewWithT(t).Expect( 54 Insert(). 55 Into(table). 56 Values(Cols("f_a", "f_b"), 1, 2, 1, 2, 1, 2), 57 ).To(BeExpr("INSERT INTO T (f_a,f_b) VALUES (?,?),(?,?),(?,?)", 1, 2, 1, 2, 1, 2)) 58 }) 59 60 t.Run("FromSelect", func(t *testing.T) { 61 gomega.NewWithT(t).Expect( 62 Insert(). 63 Into(table). 64 Values(Cols("f_a", "f_b"), Select(Cols("f_a", "f_b")).From(table, Where(table.Col("f_a").Eq(1)))), 65 ).To(BeExpr(` 66 INSERT INTO T (f_a,f_b) SELECT f_a,f_b FROM T 67 WHERE f_a = ? 68 `, 1)) 69 }) 70 } 71 72 func TestSelect(t *testing.T) { 73 table := T("T") 74 75 t.Run("Simple", func(t *testing.T) { 76 gomega.NewWithT(t).Expect( 77 Select(nil). 78 From( 79 table, 80 Where( 81 Col("F_a").Eq(1), 82 ), 83 Comment("comment"), 84 ), 85 ).To(BeExpr(` 86 SELECT * FROM T 87 WHERE f_a = ? 88 /* comment */ 89 `, 1)) 90 }) 91 t.Run("WithModifier", func(t *testing.T) { 92 gomega.NewWithT(t).Expect( 93 Select(nil, "DISTINCT"). 94 From( 95 table, 96 Where( 97 Col("F_a").Eq(1), 98 ), 99 ), 100 ).To(BeExpr(` 101 SELECT DISTINCT * FROM T 102 WHERE f_a = ?`, 1)) 103 }) 104 t.Run("WithColumn", func(t *testing.T) { 105 gomega.NewWithT(t).Expect( 106 Select(Col("F_a")). 107 From(table, 108 Where( 109 Col("F_a").Eq(1), 110 ), 111 ), 112 ).To(BeExpr(` 113 SELECT f_a FROM T 114 WHERE f_a = ?`, 1)) 115 }) 116 t.Run("ForUpdate", func(t *testing.T) { 117 gomega.NewWithT(t).Expect( 118 Select(nil).From( 119 table, 120 Where(Col("F_a").Eq(1)), 121 ForUpdate(), 122 ), 123 ).To(BeExpr( 124 ` 125 SELECT * FROM T 126 WHERE f_a = ? 127 FOR UPDATE 128 `, 129 1, 130 )) 131 }) 132 } 133 134 func TestStmtUpdate(t *testing.T) { 135 table := T("T") 136 137 t.Run("Update", func(t *testing.T) { 138 gomega.NewWithT(t).Expect( 139 Update(table). 140 Set( 141 Col("F_a").ValueBy(1), 142 Col("F_b").ValueBy(2), 143 ). 144 Where( 145 Col("F_a").Eq(1), 146 Comment("Comment"), 147 ), 148 ).To(BeExpr(` 149 UPDATE T SET f_a = ?, f_b = ? 150 WHERE f_a = ? 151 /* Comment */`, 1, 2, 1)) 152 }) 153 }