-
Notifications
You must be signed in to change notification settings - Fork 2
/
joins_test.go
41 lines (37 loc) · 1.21 KB
/
joins_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package sqb
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestJB_LeftJoin(t *testing.T) {
var tests = [...]struct {
name string
sqb SQB
wantErr bool
expectedRawSQL string
expectedArgs []interface{}
}{
{
name: "4 tables join",
expectedRawSQL: `users LEFT JOIN posts ON users.id=posts.user_id RIGHT JOIN cities ON users.city_id=cities.id LEFT JOIN regions ON cities.region_id=regions.id`,
sqb: JB(TableName("users")).
LeftJoin(TableName("posts"), Eq(Column("users.id"), Column("posts.user_id"))).
RightJoin(TableName("cities"), Eq(Column("users.city_id"), Column("cities.id"))).
LeftJoin(TableName("regions"), Eq(Column("cities.region_id"), Column("regions.id"))),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sqb := tt.sqb
tsw := &DefaultSQLWriter{}
if err := sqb.WriteSQLTo(tsw); (err != nil) != tt.wantErr {
t.Errorf("WriteSQLTo() error = %v, wantErr %v", err, tt.wantErr)
}
builded := tsw.String()
if builded != tt.expectedRawSQL {
t.Errorf("WriteSQLTo() raw SQL expected = %v, actual = %v", tt.expectedRawSQL, builded)
}
assert.Equal(t, tt.expectedArgs, tsw.Args)
})
}
}