test: write test for parsing Select Statements

This commit is contained in:
Yehoshua Sandler 2025-04-01 10:12:59 -05:00
parent f2804372a3
commit a0a6b8822e
2 changed files with 79 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/DataDog/go-sqllexer"
)
// 126 rich mar drive
type Select struct {
Table string
Columns []string

78
q/select_test.go Normal file
View File

@ -0,0 +1,78 @@
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)
}
}
}
})
}
}