package q import ( "fmt" "testing" ) type ParsingTest struct { input string expected Query } func TestParseSelectStatement_StateMachine(t *testing.T) { var testSqlStatements = []ParsingTest{ { input: "SELECT * FROM users WHERE age >= 30", expected: &Select{ Type: SELECT, Table: Table{Name: "users"}, IsWildcard: true, Conditionals: []Conditional{ { Key: "age", Operator: ">=", Value: "30", }, }, }, }, { input: "SELECT CustomerName, City FROM Customers", expected: &Select{ Type: SELECT, Table: Table{Name: "Customers"}, IsWildcard: false, Columns: []Column{ { Name: "CustomerName", }, { Name: "City", }, }, }, }, { input: "SELECT CustomerName AS Customer, City AS town FROM Customers AS People", expected: &Select{ Type: SELECT, Table: Table{Name: "Customers", Alias: "People"}, IsWildcard: false, Columns: []Column{ { Name: "CustomerName", Alias: "Customer", }, { Name: "City", Alias: "town", }, }, }, }, { input: "SELECT * FROM Orders ORDER BY StreetNumber, CountryCode;", expected: &Select{ Type: SELECT, Table: Table{Name: "Orders"}, IsWildcard: true, OrderBys: []OrderBy{ { Key: "StreetNumber", }, { Key: "CountryCode", }, }, }, }, { input: "SELECT * FROM ZipCodes ORDER BY Code ASC, StateName DESC", expected: &Select{ Type: SELECT, Table: Table{Name: "ZipCodes"}, IsWildcard: true, OrderBys: []OrderBy{ { Key: "Code", IsDescend: false, }, { Key: "StateName", IsDescend: true, }, }, }, }, } for _, sql := range testSqlStatements { testName := fmt.Sprintf("%s", sql.input) expected := sql.expected.(*Select) t.Run(testName, func(t *testing.T) { answer, err := Parse(sql.input) if err != nil { t.Error(err) return } answerAsSelect := answer.(*Select) if answerAsSelect.Type != expected.Type { t.Errorf("got %d for Select.Type, expected %d", answerAsSelect.Type, expected.Type) } if answerAsSelect.IsWildcard != expected.IsWildcard { t.Errorf("got %#v for Select.IsWildcard, expected %#v", answerAsSelect.IsWildcard, expected.IsWildcard) } if answerAsSelect.Table.Name != expected.Table.Name { t.Errorf("got %s for Select.Table.Name, expected %s", answerAsSelect.Table.Name, expected.Table.Name) } if answerAsSelect.Table.Alias != expected.Table.Alias { t.Errorf("got %s for Select.Table.Alias, expected %s", answerAsSelect.Table.Alias, expected.Table.Alias) } if len(answerAsSelect.Columns) != len(expected.Columns) { t.Errorf("got %d number of columns for Select.Columns, expected %d", len(answerAsSelect.Columns), len(expected.Columns)) } else { for i, expectedColumn := range expected.Columns { if expectedColumn.Name != answerAsSelect.Columns[i].Name { t.Errorf("got %s for Select.Column[%d].Name, expected %s", answerAsSelect.Columns[i].Name, i, expectedColumn.Name) } if expectedColumn.Alias != answerAsSelect.Columns[i].Alias { t.Errorf("got %s for Select.Column[%d].Alias, expected %s", answerAsSelect.Columns[i].Alias, i, expectedColumn.Alias) } if expectedColumn.AggregateFunction != answerAsSelect.Columns[i].AggregateFunction { t.Errorf("got %d for Select.Column[%d].AggregateFunction, expected %d", answerAsSelect.Columns[i].AggregateFunction, i, expectedColumn.AggregateFunction) } } } if len(answerAsSelect.Conditionals) != len(expected.Conditionals) { t.Errorf("got %d number of conditionals for Select.Conditionals, expected %d", len(answerAsSelect.Conditionals), len(expected.Conditionals)) } else { for i, expectedCondition := range expected.Conditionals { if expectedCondition.Key != answerAsSelect.Conditionals[i].Key { t.Errorf("got %s for Select.Conditionals[%d].Key, expected %s", answerAsSelect.Conditionals[i].Key, i, expectedCondition.Key) } if expectedCondition.Operator != answerAsSelect.Conditionals[i].Operator { t.Errorf("got %s for Select.Conditionals[%d].Operator, expected %s", answerAsSelect.Conditionals[i].Operator, i, expectedCondition.Operator) } if expectedCondition.Value != answerAsSelect.Conditionals[i].Value { t.Errorf("got %s for Select.Conditionals[%d].Value, expected %s", answerAsSelect.Conditionals[i].Value, i, expectedCondition.Value) } if expectedCondition.Extension != answerAsSelect.Conditionals[i].Extension { t.Errorf("got %s for Select.Conditionals[%d].Extension, expected %s", answerAsSelect.Conditionals[i].Extension, i, expectedCondition.Extension) } } } if len(answerAsSelect.OrderBys) != len(expected.OrderBys) { t.Errorf("got %d number of orderBys for Select.OrderBys, expected %d", len(answerAsSelect.OrderBys), len(expected.OrderBys)) } else { for i, expectedOrderBy := range expected.OrderBys { if expectedOrderBy.Key != answerAsSelect.OrderBys[i].Key { t.Errorf("got %s for Select.OrderBys[%d].Key, expected %s", answerAsSelect.OrderBys[i].Key, i, expectedOrderBy.Key) } if expectedOrderBy.IsDescend != answerAsSelect.OrderBys[i].IsDescend { t.Errorf("got %#v for Select.OrderBys[%d].IsDescend, expected %#v", answerAsSelect.OrderBys[i].IsDescend, i, expectedOrderBy.IsDescend) } } } }) } }