107 lines
2.6 KiB
Go
107 lines
2.6 KiB
Go
package sqimple
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/alecthomas/assert/v2"
|
|
)
|
|
|
|
func TestInsertTable(t *testing.T) {
|
|
type TestCase struct {
|
|
In string
|
|
IsValid bool
|
|
Table string
|
|
As string
|
|
}
|
|
|
|
testCases := []TestCase{
|
|
{
|
|
In: "into customer",
|
|
IsValid: true,
|
|
Table: "customer",
|
|
},
|
|
{
|
|
In: "into customer as c",
|
|
IsValid: true,
|
|
Table: "customer",
|
|
As: "c",
|
|
},
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
t.Run(testCase.In, func(t *testing.T) {
|
|
it := InsertTable(testCase.In)
|
|
table, as, ok := it.Parse()
|
|
|
|
assert.Equal(t, testCase.IsValid, ok, "incorrect test case")
|
|
assert.Equal(t, testCase.Table, table, "parsed table incorrectly")
|
|
assert.Equal(t, testCase.As, as, "parsed as incorrectly")
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSelectTable(t *testing.T) {
|
|
type TestCase struct {
|
|
In string
|
|
IsValid bool
|
|
SelectType string
|
|
Table string
|
|
As string
|
|
On *OnClause
|
|
}
|
|
|
|
testCases := []TestCase{
|
|
{
|
|
In: "from customer",
|
|
IsValid: true,
|
|
SelectType: "from",
|
|
Table: "customer",
|
|
},
|
|
{
|
|
In: "right join invoice on invoice.customer_id = customer.id",
|
|
IsValid: true,
|
|
SelectType: "right join",
|
|
Table: "invoice",
|
|
On: On("invoice.customer_id", "=", "customer.id"),
|
|
},
|
|
{
|
|
In: "inner join invoice as i on i.customer_id = c.id",
|
|
IsValid: true,
|
|
SelectType: "inner join",
|
|
Table: "invoice",
|
|
As: "i",
|
|
On: On("i.customer_id", "=", "c.id"),
|
|
},
|
|
{
|
|
In: "left join invoice as i on i.customer_id = c.id and i.paid = false",
|
|
IsValid: true,
|
|
SelectType: "left join",
|
|
Table: "invoice",
|
|
As: "i",
|
|
On: On("i.customer_id", "=", "c.id").And("i.paid", "=", "false"),
|
|
},
|
|
// Currently failing - need to fix bug with 3+ on statements
|
|
{
|
|
In: "left join invoice as i on i.customer_id = c.id and i.paid = false and i.amount > 1000",
|
|
IsValid: true,
|
|
SelectType: "left join",
|
|
Table: "invoice",
|
|
As: "i",
|
|
On: On("i.customer_id", "=", "c.id").And("i.paid", "=", "false").And("i.amount", ">", "1000"),
|
|
},
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
t.Run(testCase.In, func(t *testing.T) {
|
|
st := SelectTable(testCase.In)
|
|
selectType, table, as, on, ok := st.Parse()
|
|
|
|
assert.Equal(t, testCase.IsValid, ok, "incorrect test case")
|
|
assert.Equal(t, testCase.SelectType, selectType, "parsed select type incorrectly")
|
|
assert.Equal(t, testCase.Table, table, "parsed table incorrectly")
|
|
assert.Equal(t, testCase.As, as, "parsed as incorrectly")
|
|
assert.Equal(t, testCase.On, on, "parsed on incorrectly")
|
|
})
|
|
}
|
|
}
|