From a0a6b8822ed8ac3c1305e183db8761fa26f7d0bf Mon Sep 17 00:00:00 2001 From: ysandler Date: Tue, 1 Apr 2025 10:12:59 -0500 Subject: [PATCH] test: write test for parsing Select Statements --- q/select.go | 1 + q/select_test.go | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 q/select_test.go diff --git a/q/select.go b/q/select.go index 00334cc..b4cbb70 100644 --- a/q/select.go +++ b/q/select.go @@ -6,6 +6,7 @@ import ( "github.com/DataDog/go-sqllexer" ) +// 126 rich mar drive type Select struct { Table string Columns []string diff --git a/q/select_test.go b/q/select_test.go new file mode 100644 index 0000000..ef91656 --- /dev/null +++ b/q/select_test.go @@ -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) + } + } + } + + }) + } +}