From 9f8a00a801ca7924d2343f7e97332ded724d3115 Mon Sep 17 00:00:00 2001 From: tauslim Date: Tue, 19 Mar 2024 14:29:41 +0800 Subject: [PATCH] test: add test cases for parseURL --- parser_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/parser_test.go b/parser_test.go index 3450f64..3278cea 100644 --- a/parser_test.go +++ b/parser_test.go @@ -1,6 +1,7 @@ package gorql import ( + "net/url" "strings" "testing" ) @@ -14,3 +15,65 @@ func FuzzParse(f *testing.F) { _, _ = p.Parse(strings.NewReader(a)) }) } + +type ParseURLTest struct { + Name string // Name of the test + URL string // Input URL + WantParseError bool // Test should raise an error when parsing the RQL query + Model interface{} // Input Model for query +} + +var parseURLTests = []ParseURLTest{ + { + Name: `Basic URL parse`, + URL: `http://localhost:8000?and(eq(foo,42),gt(price,10),not(disabled=false))`, + WantParseError: false, + Model: new(struct { + Foo string `rql:"filter"` + Price float64 `rql:"filter"` + Disabled bool `rql:"filter"` + }), + }, + { + Name: `Basic URL parse with space encoding`, + URL: `http://localhost:8000?eq(foo,john%20wick%20is%20back)`, + WantParseError: false, + Model: new(struct { + Foo string `rql:"filter"` + }), + }, + { + Name: `URL parse with no space encoding`, + URL: `http://localhost:8000?eq(foo,john wick is back)`, + WantParseError: false, + Model: new(struct { + Foo string `rql:"filter"` + }), + }, + { + Name: `URL parse with invalid RQL query (missing closing bracket)`, + URL: `http://localhost:8000?and(eq(foo,john wick is back)`, + WantParseError: true, + Model: new(struct { + Foo string `rql:"filter"` + }), + }, +} + +func TestParseURL(t *testing.T) { + for _, test := range parseURLTests { + test.Run(t) + } +} + +func (p ParseURLTest) Run(t *testing.T) { + parser, err := NewParser(&Config{Model: p.Model}) + if err != nil { + t.Fatalf("(%s) New parser error :%v\n", p.Name, err) + } + u, _ := url.Parse(p.URL) + _, err = parser.ParseURL(u.Query()) + if p.WantParseError != (err != nil) { + t.Fatalf("(%s) Expecting parse error :%v\nGot error : %v", p.Name, p.WantParseError, err) + } +}