Files
sqimple/select_test.go
2026-07-07 20:30:02 +01:00

66 lines
1.8 KiB
Go

package sqimple
import (
"testing"
"github.com/alecthomas/assert/v2"
)
func TestSelect(t *testing.T) {
type TestCase struct {
In *SelectQuery
Args []any
String string
}
testCases := []TestCase{
{
In: Select("customer"),
String: "select * from customer",
},
{
In: Select("customer as c"),
String: "select * from customer as c",
},
{
In: Select("customer as c").Columns("c.id", "c.name"),
String: "select c.id, c.name from customer as c",
},
{
In: Select("customer as c").InnerJoin("invoices as i on i.customer_id = c.id").Columns("c.name", "i.date"),
String: "select c.name, i.date from customer as c inner join invoices as i on i.customer_id = c.id",
},
{
In: Select("customer").Where("id = ?", 1234),
Args: []any{1234},
String: "select * from customer where id = ?",
},
{
In: Select("customer").Where("id = ?", 1234).Where("and amount > ?", 1000),
Args: []any{1234, 1000},
String: "select * from customer where id = ? and amount > ?",
},
{
In: Select("customer").Where("id = ?", 1234).Columns("name", "email"),
Args: []any{1234},
String: "select name, email from customer where id = ?",
},
{
In: Select("customer").Where("age > ?", 18).OrderBy("age desc", "name asc"),
Args: []any{18},
String: "select * from customer where age > ? order by age desc, name asc",
},
{
In: Select("customer").Distinct(true).Columns("age"),
String: "select distinct age from customer",
},
}
for _, testCase := range testCases {
t.Run(testCase.String, func(t *testing.T) {
assert.Equal(t, testCase.Args, testCase.In.Args(), "collected arguments incorrectly")
assert.Equal(t, testCase.String, testCase.In.String(), "stringified incorrectly")
})
}
}