initial commit
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package simql
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/alecthomas/assert/v2"
|
||||
)
|
||||
|
||||
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")
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user