2020-03-14 18:47:38 +03:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-02-27 22:31:10 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-03-14 18:47:38 +03:00
|
|
|
|
2022-08-20 11:52:11 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entities/common"
|
2020-03-14 18:47:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParse(t *testing.T) {
|
|
|
|
var tests = []struct {
|
|
|
|
input string
|
2020-03-22 15:53:34 +03:00
|
|
|
output *Query
|
2020-03-14 18:47:38 +03:00
|
|
|
}{
|
2020-08-20 13:00:34 +03:00
|
|
|
// KV
|
2020-03-14 18:47:38 +03:00
|
|
|
{"status:", nil},
|
|
|
|
{":value", nil},
|
|
|
|
|
2020-03-22 15:53:34 +03:00
|
|
|
{"status:open", &Query{
|
2022-08-20 11:52:11 +03:00
|
|
|
Filters: Filters{Status: []common.Status{common.OpenStatus}},
|
2020-03-14 18:47:38 +03:00
|
|
|
}},
|
2020-03-22 15:53:34 +03:00
|
|
|
{"status:closed", &Query{
|
2022-08-20 11:52:11 +03:00
|
|
|
Filters: Filters{Status: []common.Status{common.ClosedStatus}},
|
2020-03-14 18:47:38 +03:00
|
|
|
}},
|
|
|
|
{"status:unknown", nil},
|
|
|
|
|
2020-03-22 15:53:34 +03:00
|
|
|
{"author:rene", &Query{
|
|
|
|
Filters: Filters{Author: []string{"rene"}},
|
2020-03-14 18:47:38 +03:00
|
|
|
}},
|
2020-03-22 15:53:34 +03:00
|
|
|
{`author:"René Descartes"`, &Query{
|
|
|
|
Filters: Filters{Author: []string{"René Descartes"}},
|
2020-03-14 18:47:38 +03:00
|
|
|
}},
|
|
|
|
|
2020-03-22 15:53:34 +03:00
|
|
|
{"actor:bernhard", &Query{
|
|
|
|
Filters: Filters{Actor: []string{"bernhard"}},
|
2020-03-14 18:47:38 +03:00
|
|
|
}},
|
2020-03-22 15:53:34 +03:00
|
|
|
{"participant:leonhard", &Query{
|
|
|
|
Filters: Filters{Participant: []string{"leonhard"}},
|
2020-03-14 18:47:38 +03:00
|
|
|
}},
|
|
|
|
|
2020-03-22 15:53:34 +03:00
|
|
|
{"label:hello", &Query{
|
|
|
|
Filters: Filters{Label: []string{"hello"}},
|
2020-03-14 18:47:38 +03:00
|
|
|
}},
|
2020-03-22 15:53:34 +03:00
|
|
|
{`label:"Good first issue"`, &Query{
|
|
|
|
Filters: Filters{Label: []string{"Good first issue"}},
|
2020-03-14 18:47:38 +03:00
|
|
|
}},
|
|
|
|
|
2020-03-22 15:53:34 +03:00
|
|
|
{"title:titleOne", &Query{
|
|
|
|
Filters: Filters{Title: []string{"titleOne"}},
|
2020-03-14 18:47:38 +03:00
|
|
|
}},
|
2020-03-22 15:53:34 +03:00
|
|
|
{`title:"Bug titleTwo"`, &Query{
|
|
|
|
Filters: Filters{Title: []string{"Bug titleTwo"}},
|
2020-03-14 18:47:38 +03:00
|
|
|
}},
|
|
|
|
|
2020-03-22 15:53:34 +03:00
|
|
|
{"no:label", &Query{
|
|
|
|
Filters: Filters{NoLabel: true},
|
2020-03-14 18:47:38 +03:00
|
|
|
}},
|
|
|
|
|
2020-03-22 15:53:34 +03:00
|
|
|
{"sort:edit", &Query{
|
|
|
|
OrderBy: OrderByEdit,
|
2020-03-14 18:47:38 +03:00
|
|
|
}},
|
|
|
|
{"sort:unknown", nil},
|
|
|
|
|
2022-03-22 23:28:10 +03:00
|
|
|
{"label:\"foo:bar\"", &Query{
|
|
|
|
Filters: Filters{Label: []string{"foo:bar"}},
|
|
|
|
}},
|
|
|
|
|
2021-02-27 22:31:10 +03:00
|
|
|
// KVV
|
|
|
|
{`metadata:key:"https://www.example.com/"`, &Query{
|
|
|
|
Filters: Filters{Metadata: []StringPair{{"key", "https://www.example.com/"}}},
|
|
|
|
}},
|
|
|
|
|
2020-08-20 13:00:34 +03:00
|
|
|
// Search
|
|
|
|
{"search", &Query{
|
|
|
|
Search: []string{"search"},
|
|
|
|
}},
|
|
|
|
{"search \"more terms\"", &Query{
|
|
|
|
Search: []string{"search", "more terms"},
|
|
|
|
}},
|
|
|
|
|
|
|
|
// Complex
|
|
|
|
{`status:open author:"René Descartes" search participant:leonhard label:hello label:"Good first issue" sort:edit-desc "more terms"`,
|
2020-03-22 15:53:34 +03:00
|
|
|
&Query{
|
2020-08-20 13:00:34 +03:00
|
|
|
Search: []string{"search", "more terms"},
|
2020-03-22 15:53:34 +03:00
|
|
|
Filters: Filters{
|
2022-08-20 11:52:11 +03:00
|
|
|
Status: []common.Status{common.OpenStatus},
|
2020-03-14 18:47:38 +03:00
|
|
|
Author: []string{"René Descartes"},
|
|
|
|
Participant: []string{"leonhard"},
|
|
|
|
Label: []string{"hello", "Good first issue"},
|
|
|
|
},
|
2020-03-22 15:53:34 +03:00
|
|
|
OrderBy: OrderByEdit,
|
|
|
|
OrderDirection: OrderDescending,
|
2020-03-14 18:47:38 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.input, func(t *testing.T) {
|
|
|
|
query, err := Parse(tc.input)
|
|
|
|
if tc.output == nil {
|
2021-02-27 22:31:10 +03:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, query)
|
2020-03-14 18:47:38 +03:00
|
|
|
} else {
|
2021-02-27 22:31:10 +03:00
|
|
|
require.NoError(t, err)
|
2020-03-14 18:47:38 +03:00
|
|
|
if tc.output.OrderBy != 0 {
|
2021-02-27 22:31:10 +03:00
|
|
|
require.Equal(t, tc.output.OrderBy, query.OrderBy)
|
2020-03-14 18:47:38 +03:00
|
|
|
}
|
|
|
|
if tc.output.OrderDirection != 0 {
|
2021-02-27 22:31:10 +03:00
|
|
|
require.Equal(t, tc.output.OrderDirection, query.OrderDirection)
|
2020-03-14 18:47:38 +03:00
|
|
|
}
|
2021-02-27 22:31:10 +03:00
|
|
|
require.Equal(t, tc.output.Filters, query.Filters)
|
2020-03-14 18:47:38 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|