Skip to content

Commit

Permalink
feat(engine): add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
craftslab committed May 8, 2024
1 parent 6c5a911 commit 05ffd87
Show file tree
Hide file tree
Showing 3 changed files with 325 additions and 1 deletion.
2 changes: 1 addition & 1 deletion engine/engine_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func executeGlobalVariableStatement(
env *ast.Environment,
statement *ast.GlobalVariableStatement,
) error {
value, err := EvaluateExpression(env, nil, nil, nil)
value, err := EvaluateExpression(env, statement.Value, []string{}, nil)
if err != nil {
return err
}
Expand Down
306 changes: 306 additions & 0 deletions engine/engine_executor_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,307 @@
package engine

import (
"fmt"
"testing"

"github.com/ggql/ggql/ast"
"github.com/go-git/go-git/v5"
)

const path = "F:/tt/test.git"

func TestExecuteStatement(t *testing.T) {
env := ast.Environment{
Globals: map[string]ast.Value{},
GlobalsTypes: map[string]ast.DataType{},
Scopes: map[string]ast.DataType{},
}

statement := &ast.SelectStatement{
TableName: "commits",
FieldsNames: []string{"commit_id", "title", "message", "name", "email", "datetime", "repo"},
FieldsValues: []ast.Expression{},
AliasTable: make(map[string]string),
IsDistinct: false,
}

if err := TestNewRepo(path); err != nil {
t.Fatal("failed to create repo:", err)
}
repo, err := git.PlainOpen(path)
if err != nil {
t.Fatal("failed to open repo")
}

var object ast.GitQLObject
var table map[string]string
selection := []string{}

ret := ExecuteStatement(&env, statement, repo, &object, table, selection)
if ret == nil {
t.Log("execute statement succeeded")
} else {
t.Errorf("execute statement failed: %v", ret)
}

if err := TestDeleteRepo(path); err != nil {
t.Fatal("failed to delete repo:", err)
}
}

func TestExecuteSelectStatement(t *testing.T) {
env := ast.Environment{
Globals: map[string]ast.Value{},
GlobalsTypes: map[string]ast.DataType{},
Scopes: map[string]ast.DataType{},
}

statement := &ast.SelectStatement{
TableName: "commits",
FieldsNames: []string{"commit_id", "title", "message", "name", "email", "datetime", "repo"},
FieldsValues: []ast.Expression{},
AliasTable: make(map[string]string),
IsDistinct: false,
}

if err := TestNewRepo(path); err != nil {
t.Fatal("failed to create repo:", err)
}
repo, err := git.PlainOpen(path)
if err != nil {
t.Fatal("failed to open repo")
}

var object ast.GitQLObject
selections := []string{}

ret := executeSelectStatement(&env, statement, repo, &object, selections)
if ret == nil {
t.Log("execute statement succeeded")
} else {
t.Errorf("execute statement failed: %v", ret)
}

if err := TestDeleteRepo(path); err != nil {
t.Fatal("failed to delete repo:", err)
}
}

func TestExecuteWhereStatement(t *testing.T) {
env := ast.Environment{
Globals: map[string]ast.Value{},
GlobalsTypes: map[string]ast.DataType{},
Scopes: map[string]ast.DataType{},
}
statement := &ast.WhereStatement{
Condition: &ast.NumberExpression{Value: ast.IntegerValue{Value: 1}},
}

var object ast.GitQLObject
object.Titles = []string{"title1", "title2"}
object.Groups = []ast.Group{
{Rows: []ast.Row{
{Values: []ast.Value{
ast.IntegerValue{Value: 1}, ast.IntegerValue{Value: 2},
}},
{Values: []ast.Value{
ast.IntegerValue{Value: 3}, ast.IntegerValue{Value: 4},
}},
}},
}

ret := executeWhereStatement(&env, statement, &object)
if ret == nil {
t.Log("execute statement succeeded")
} else {
t.Errorf("execute statement failed: %v", ret)
}
}

func TestExecuteHavingStatement(t *testing.T) {
env := ast.Environment{
Globals: map[string]ast.Value{},
GlobalsTypes: map[string]ast.DataType{},
Scopes: map[string]ast.DataType{},
}
statement := &ast.HavingStatement{
Condition: &ast.NumberExpression{Value: ast.IntegerValue{Value: 1}},
}
var object ast.GitQLObject
object.Titles = []string{"title1", "title2"}
object.Groups = []ast.Group{
{Rows: []ast.Row{
{Values: []ast.Value{
ast.IntegerValue{Value: 1}, ast.IntegerValue{Value: 2},
}},
{Values: []ast.Value{
ast.IntegerValue{Value: 3}, ast.IntegerValue{Value: 4},
}},
}},
}
ret := executeHavingStatement(&env, statement, &object)
if ret == nil {
t.Log("execute statement succeeded")
} else {
t.Errorf("execute statement failed: %v", ret)
}
}

func TestExecuteLimitStatement(t *testing.T) {
statement := &ast.LimitStatement{
Count: 0,
}
var object ast.GitQLObject
object.Titles = []string{"title1", "title2"}
object.Groups = []ast.Group{
{Rows: []ast.Row{
{Values: []ast.Value{
ast.IntegerValue{Value: 1}, ast.IntegerValue{Value: 2},
}},
{Values: []ast.Value{
ast.IntegerValue{Value: 3}, ast.IntegerValue{Value: 4},
}},
}},
}
ret := executeLimitStatement(statement, &object)
if ret == nil {
t.Log("execute statement succeeded")
} else {
t.Errorf("execute statement failed: %v", ret)
}
}

func TestExecuteOffsetStatement(t *testing.T) {
statement := &ast.OffsetStatement{
Count: 0,
}
var object ast.GitQLObject
object.Titles = []string{"title1", "title2"}
object.Groups = []ast.Group{
{Rows: []ast.Row{
{Values: []ast.Value{
ast.IntegerValue{Value: 1}, ast.IntegerValue{Value: 2},
}},
{Values: []ast.Value{
ast.IntegerValue{Value: 3}, ast.IntegerValue{Value: 4},
}},
}},
}
ret := executeOffsetStatement(statement, &object)
if ret == nil {
t.Log("execute statement succeeded")
} else {
t.Errorf("execute statement failed: %v", ret)
}
}

func TestExecuteOrderByStatement(t *testing.T) {
env := ast.Environment{
Globals: map[string]ast.Value{},
GlobalsTypes: map[string]ast.DataType{},
Scopes: map[string]ast.DataType{},
}
statement := &ast.OrderByStatement{
Arguments: []ast.Expression{&ast.NumberExpression{Value: ast.IntegerValue{Value: 5}}},
SortingOrders: []ast.SortingOrder{ast.Ascending},
}
var object ast.GitQLObject
object.Titles = []string{"title1", "title2"}
object.Groups = []ast.Group{
{Rows: []ast.Row{
{Values: []ast.Value{
ast.IntegerValue{Value: 1}, ast.IntegerValue{Value: 2},
}},
{Values: []ast.Value{
ast.IntegerValue{Value: 3}, ast.IntegerValue{Value: 4},
}},
}},
}
ret := executeOrderByStatement(&env, statement, &object)
if ret == nil {
t.Log("execute statement succeeded")
} else {
t.Errorf("execute statement failed: %v", ret)
}
}

func TestExecuteGroupByStatement(t *testing.T) {
statement := &ast.GroupByStatement{
FieldName: "title1",
}
var object ast.GitQLObject
object.Titles = []string{"title1", "title2"}
object.Groups = []ast.Group{
{Rows: []ast.Row{
{Values: []ast.Value{
ast.IntegerValue{Value: 1}, ast.IntegerValue{Value: 2},
}},
{Values: []ast.Value{
ast.IntegerValue{Value: 3}, ast.IntegerValue{Value: 4},
}},
}},
}
ret := executeGroupByStatement(statement, &object)
if ret == nil {
t.Log("execute statement succeeded")
} else {
t.Errorf("execute statement failed: %v", ret)
}
}

func TestExecuteAggregationFunctionStatement(t *testing.T) {
env := ast.Environment{
Globals: map[string]ast.Value{},
GlobalsTypes: map[string]ast.DataType{},
Scopes: map[string]ast.DataType{},
}
statement := &ast.AggregationsStatement{
Aggregations: make(map[string]ast.AggregateValue),
}
var a ast.AggregateValue
a.Function.Name = "max"
a.Function.Arg = "title3"

statement.Aggregations["title"] = a
var object ast.GitQLObject
object.Titles = []string{"title1", "title2"}
object.Groups = []ast.Group{
{Rows: []ast.Row{
{Values: []ast.Value{
ast.IntegerValue{Value: 1}, ast.IntegerValue{Value: 2},
}},
{Values: []ast.Value{
ast.IntegerValue{Value: 3}, ast.IntegerValue{Value: 4},
}},
}},
}
table := make(map[string]string)
table["title"] = "title1"
fmt.Println("5")
ret := executeAggregationFunctionStatement(&env, statement, &object, table)
fmt.Println("3")
if ret == nil {
t.Log("execute statement succeeded")
} else {
t.Errorf("execute statement failed: %v", ret)
}
}

func TestExecuteGlobalVariableStatement(t *testing.T) {
env := ast.Environment{
Globals: map[string]ast.Value{},
GlobalsTypes: map[string]ast.DataType{},
Scopes: map[string]ast.DataType{},
}
statement := &ast.GlobalVariableStatement{
Name: "name",
Value: &ast.NumberExpression{Value: ast.IntegerValue{Value: 1}},
}

ret := executeGlobalVariableStatement(&env, statement)
if ret == nil {
t.Log("execute statement succeeded")
} else {
t.Errorf("execute statement failed: %v", ret)
}
}
18 changes: 18 additions & 0 deletions engine/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package engine

import (
"os"

"github.com/go-git/go-git/v5"
)

func TestNewRepo(path string) error {
// Clone the given repository to the given path
_, err := git.PlainInit(path, true)
return err
}

func TestDeleteRepo(path string) error {
err := os.RemoveAll(path)
return err
}

0 comments on commit 05ffd87

Please sign in to comment.