forked from raystack/compass
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: create unit test for converter
- Loading branch information
Muhammad Luthfi Fahlevi
committed
Aug 12, 2024
1 parent
a926792
commit 57897ea
Showing
4 changed files
with
343 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package queryexpr_test | ||
|
||
import ( | ||
"testing" | ||
|
||
queryexpr "github.com/goto/compass/pkg/query_expr" | ||
) | ||
|
||
func TestESExpr_String(t *testing.T) { | ||
tests := []struct { | ||
expr queryexpr.SQLExpr | ||
want string | ||
}{ | ||
{ | ||
expr: queryexpr.SQLExpr("test"), | ||
want: "test", | ||
}, | ||
{ | ||
expr: queryexpr.SQLExpr("bool_identifier == !(findLast([1, 2, 3, 4], # > 2) == 4)"), | ||
want: "bool_identifier == !(findLast([1, 2, 3, 4], # > 2) == 4)", | ||
}, | ||
} | ||
for i, tt := range tests { | ||
t.Run("test-case-"+string(rune(i)), func(t *testing.T) { | ||
if got := tt.expr.String(); got != tt.want { | ||
t.Errorf("String() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestESExpr_ToQuery(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
expr queryexpr.ESExpr | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "less than condition with single quote", | ||
expr: queryexpr.ESExpr(`updated_at < '2024-04-05 23:59:59'`), | ||
want: `{"query":{"range":{"updated_at":{"lt":"2024-04-05 23:59:59"}}}}`, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "greater than condition with double quote", | ||
expr: queryexpr.ESExpr(`updated_at > "2024-04-05 23:59:59"`), | ||
want: `{"query":{"range":{"updated_at":{"gt":"2024-04-05 23:59:59"}}}}`, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "in condition", | ||
expr: queryexpr.ESExpr(`service in ["test1","test2","test3"]`), | ||
want: `{"query":{"terms":{"service":["test1","test2","test3"]}}}`, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "equals or not in condition", | ||
expr: queryexpr.ESExpr(`name == "John" || service not in ["test1","test2","test3"]`), | ||
want: `{"query":{"bool":{"should":[{"term":{"name":"John"}},{"bool":{"must_not":[{"terms":{"service":["test1","test2","test3"]}}]}}]}}}`, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "complex query expression that can directly produce a value", | ||
expr: queryexpr.ESExpr(`bool_identifier == !(findLast([1, 2, 3, 4], # > 2) == 4)`), | ||
want: `{"query":{"term":{"bool_identifier":false}}}`, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "complex query expression that can NOT directly produce a value", | ||
expr: queryexpr.ESExpr(`service in filter(assets, .Service startsWith "T")`), | ||
want: "", | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := tt.expr.ToQuery() | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("ToQuery() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("ToQuery() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestESExpr_Validate(t *testing.T) { | ||
t.Run("should return nil as default validation", func(t *testing.T) { | ||
expr := queryexpr.ESExpr("query_es == 'test'") | ||
if err := (&expr).Validate(); err != nil { | ||
t.Errorf("Validate() error = %v, wantErr %v", err, nil) | ||
} | ||
return | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package queryexpr | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestGetIdentifiers(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
expr string | ||
want []string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "got 0 identifier test", | ||
expr: `findLast([1, 2, 3, 4], # > 2)`, | ||
want: nil, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "got 1 identifiers test", | ||
expr: `updated_at < '2024-04-05 23:59:59'`, | ||
want: []string{"updated_at"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "got 3 identifiers test", | ||
expr: `(identifier1 == !(findLast([1, 2, 3, 4], # > 2) == 4)) && identifier2 != 'John' || identifier3 == "hallo"`, | ||
want: []string{"identifier1", "identifier2", "identifier3"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "got error", | ||
expr: `findLast([1, 2, 3, 4], # > 2`, | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := GetIdentifiers(tt.expr) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("GetIdentifiers() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("GetIdentifiers() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGetQueryExprResult(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
expr string | ||
want any | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "return a value from func", | ||
expr: `findLast([1, 2, 3, 4], # > 2)`, | ||
want: 4, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "return a value func equation", | ||
expr: `false == !true`, | ||
want: true, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "got error due to can NOT directly produce a value", | ||
expr: `updated_at < '2024-04-05 23:59:59'`, | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := GetQueryExprResult(tt.expr) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("GetQueryExprResult() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("GetQueryExprResult() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGetTreeNodeFromQueryExpr(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
expr string | ||
want any | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "success using func from the library", | ||
expr: `findLast([1], # >= 1)`, | ||
want: "findLast([1], # >= 1)", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "success using equation", | ||
expr: `false == !true`, | ||
want: "false == !true", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "got error using wrong syntax", | ||
expr: `findLast(`, | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := GetTreeNodeFromQueryExpr(tt.expr) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("GetTreeNodeFromQueryExpr() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !tt.wantErr { | ||
if got.String() != tt.want { | ||
t.Errorf("GetTreeNodeFromQueryExpr() got = %v, want %v", got, tt.want) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.