@@ -4,13 +4,13 @@ import (
44 "testing"
55
66 sq "github.com/Masterminds/squirrel"
7- "github.com/goware/pgkit/v2/db"
87 "github.com/stretchr/testify/assert"
98 "github.com/stretchr/testify/require"
9+
10+ "github.com/goware/pgkit/v2/db"
1011)
1112
1213func TestCond (t * testing.T ) {
13-
1414 t .Run ("equal to" , func (t * testing.T ) {
1515 cond := db.Cond {"one" : 1 }
1616 s , args , err := cond .ToSql ()
@@ -19,6 +19,14 @@ func TestCond(t *testing.T) {
1919 assert .Equal (t , "one = ?" , s )
2020 })
2121
22+ t .Run ("equal to with multiple parameters" , func (t * testing.T ) {
23+ cond := db.And {db.Cond {"one" : 1 }, db.Cond {"two" : 2 }}
24+ s , args , err := cond .ToSql ()
25+ require .NoError (t , err )
26+ assert .Equal (t , []interface {}{1 , 2 }, args )
27+ assert .Equal (t , "(one = ? AND two = ?)" , s )
28+ })
29+
2230 t .Run ("equal to (inverted)" , func (t * testing.T ) {
2331 cond := db.Cond {1 : "one" }
2432 s , args , err := cond .ToSql ()
@@ -64,6 +72,16 @@ func TestCond(t *testing.T) {
6472 })
6573
6674 t .Run ("IN with slice" , func (t * testing.T ) {
75+ sl1 := []int {1 , 2 , 3 }
76+ cond := db.Cond {"list" : db .In (sl1 ... )}
77+ s , args , err := cond .ToSql ()
78+ require .NoError (t , err )
79+
80+ assert .Equal (t , []interface {}{1 , 2 , 3 }, args )
81+ assert .Equal (t , "list IN (?, ?, ?)" , s )
82+ })
83+
84+ t .Run ("IN with slice variadic" , func (t * testing.T ) {
6785 cond := db.Cond {"list" : db .In (1 , 2 , 3 )}
6886 s , args , err := cond .ToSql ()
6987 require .NoError (t , err )
@@ -72,6 +90,48 @@ func TestCond(t *testing.T) {
7290 assert .Equal (t , "list IN (?, ?, ?)" , s )
7391 })
7492
93+ t .Run ("multiple IN with slice" , func (t * testing.T ) {
94+ sl1 := []int {1 , 2 , 3 }
95+ sl2 := []int {4 , 5 , 6 }
96+ cond := db.Cond {"list" : db .In ([]interface {}{sl1 , sl2 }... )}
97+ s , args , err := cond .ToSql ()
98+ require .NoError (t , err )
99+
100+ assert .Equal (t , []interface {}{1 , 2 , 3 , 4 , 5 , 6 }, args )
101+ assert .Equal (t , "list IN ((?,?,?),(?,?,?))" , s )
102+ })
103+
104+ t .Run ("multiple IN with slice AND where ID" , func (t * testing.T ) {
105+ cond := db.And {db.Cond {"list" : db .In ([][]string {{"1" , "2" , "3" }, {"3" , "4" , "5" }}... )}, db.Cond {"id" : 1 }}
106+ s , args , err := cond .ToSql ()
107+ require .NoError (t , err )
108+
109+ assert .Equal (t , []interface {}{"1" , "2" , "3" , "3" , "4" , "5" , 1 }, args )
110+ assert .Equal (t , "(list IN ((?,?,?),(?,?,?)) AND id = ?)" , s )
111+ })
112+
113+ t .Run ("multiple IN with struct" , func (t * testing.T ) {
114+ randomStruct := []struct {
115+ Id uint64
116+ Name string
117+ }{
118+ {Id : 1 , Name : "Lukas" },
119+ {Id : 2 , Name : "David" },
120+ }
121+
122+ data := [][]interface {}{}
123+ for _ , s := range randomStruct {
124+ data = append (data , []interface {}{s .Id , s .Name })
125+ }
126+
127+ cond := db.Cond {"list" : db .In (data ... )}
128+ s , args , err := cond .ToSql ()
129+ require .NoError (t , err )
130+
131+ assert .Equal (t , []interface {}{uint64 (1 ), "Lukas" , uint64 (2 ), "David" }, args )
132+ assert .Equal (t , "list IN ((?,?),(?,?))" , s )
133+ })
134+
75135 t .Run ("NOT IN" , func (t * testing.T ) {
76136 cond := db.Cond {"list" : db .NotIn ("Czech Republic" , "Slovakia" )}
77137 s , args , err := cond .ToSql ()
0 commit comments