79 lines
2.6 KiB
Go
79 lines
2.6 KiB
Go
package q
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
type ParsingTest struct {
|
|
input string
|
|
expected Select
|
|
}
|
|
|
|
func TestParseSelectStatement(t *testing.T) {
|
|
var testSqlStatements = []ParsingTest{
|
|
{
|
|
input: "SELECT * FROM users WHERE age >= 30",
|
|
expected: Select{
|
|
Table: "users",
|
|
IsWildcard: true,
|
|
Conditionals: []Conditional{
|
|
{
|
|
Key: "age",
|
|
Operator: ">=",
|
|
Value: "30",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, sql := range testSqlStatements {
|
|
testName := fmt.Sprintf("%s", sql.input)
|
|
expected := sql.expected
|
|
|
|
t.Run(testName, func(t *testing.T) {
|
|
answer := ParseSelectStatement(sql.input)
|
|
|
|
if answer.Table != expected.Table {
|
|
t.Errorf("got %s for Select.Table, expected %s", answer.Table, expected.Table)
|
|
}
|
|
if answer.IsWildcard != expected.IsWildcard {
|
|
t.Errorf("got %#v for Select.IsWildcard, expected %#v", answer.IsWildcard, expected.IsWildcard)
|
|
}
|
|
if len(answer.Columns) != len(expected.Columns) {
|
|
t.Errorf("got %d number of columns for Select.Columns, expected %d", len(answer.Columns), len(expected.Columns))
|
|
} else {
|
|
for i, expectedColumn := range expected.Columns {
|
|
if expectedColumn != answer.Columns[i] {
|
|
t.Errorf("got %s for Select.Column[%d], expected %s", answer.Columns[i], i, expectedColumn)
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(answer.Conditionals) != len(expected.Conditionals) {
|
|
t.Errorf("got %d number of conditionals for Select.Conditionals, expected %d", len(answer.Conditionals), len(expected.Conditionals))
|
|
} else {
|
|
for i, expectedCondition := range expected.Conditionals {
|
|
if expectedCondition.Key != answer.Conditionals[i].Key {
|
|
t.Errorf("got %s for Select.Conditionals[%d].Key, expected %s", answer.Conditionals[i].Key, i, expectedCondition.Key)
|
|
}
|
|
if expectedCondition.Operator != answer.Conditionals[i].Operator {
|
|
t.Errorf("got %s for Select.Conditionals[%d].Operator, expected %s", answer.Conditionals[i].Operator, i, expectedCondition.Operator)
|
|
}
|
|
if expectedCondition.Value != answer.Conditionals[i].Value {
|
|
t.Errorf("got %s for Select.Conditionals[%d].Value, expected %s", answer.Conditionals[i].Value, i, expectedCondition.Value)
|
|
}
|
|
if expectedCondition.DataType != answer.Conditionals[i].DataType {
|
|
t.Errorf("got %s for Select.Conditionals[%d].DataType, expected %s", answer.Conditionals[i].DataType, i, expectedCondition.DataType)
|
|
}
|
|
if expectedCondition.Extension != answer.Conditionals[i].Extension {
|
|
t.Errorf("got %s for Select.Conditionals[%d].Extension, expected %s", answer.Conditionals[i].Extension, i, expectedCondition.Extension)
|
|
}
|
|
}
|
|
}
|
|
|
|
})
|
|
}
|
|
}
|