From 0f682f806d1b7109766fc1f71825339936834e27 Mon Sep 17 00:00:00 2001 From: Eric Schubert Date: Wed, 19 Feb 2025 17:11:36 +0100 Subject: [PATCH] [chore] bumped go version - fixed flickering query merge test - added comparison to Query and Filter struct --- .github/workflows/ci.yml | 2 +- .github/workflows/release.yml | 2 +- components/common/slice.go | 12 ++++++ components/common/slice_test.go | 15 ++++++- components/requests/query.go | 41 ++++++++++++++++++ components/requests/query_test.go | 71 ++++++++++++++++++++++++++++--- go.mod | 7 +-- 7 files changed, 136 insertions(+), 14 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f3b7932..af4d2b6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v3 with: - go-version: '1.23.4' + go-version: '1.24.0' - name: Build run: go build -v ./... diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a6244ad..dc43065 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -14,7 +14,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v3 with: - go-version: '1.23.4' + go-version: '1.24.0' - name: Set version run: echo "version=${GITHUB_REF#refs/*/}" >> "$GITHUB_ENV" diff --git a/components/common/slice.go b/components/common/slice.go index 28f08fc..13e3523 100644 --- a/components/common/slice.go +++ b/components/common/slice.go @@ -31,3 +31,15 @@ func Filter[T any](slice []T, f func(T) bool) []T { []T{}, ) } + +func All[T any](slice []T, f func(T) bool) bool { + for _, val := range slice { + if f(val) { + continue + } else { + return false + } + } + + return true +} diff --git a/components/common/slice_test.go b/components/common/slice_test.go index 5b96bea..6c91362 100644 --- a/components/common/slice_test.go +++ b/components/common/slice_test.go @@ -1,8 +1,9 @@ package common_test import ( - "github.com/opf/openproject-cli/components/common" "testing" + + "github.com/opf/openproject-cli/components/common" ) func TestContains(t *testing.T) { @@ -47,3 +48,15 @@ func TestFilter(t *testing.T) { t.Errorf("Expected %v to contain %d and %d, but does not", list, list[2], list[3]) } } + +func TestAll(t *testing.T) { + list := []int{23, 245, 54, 132, 4325} + + if !common.All(list, func(value int) bool { return value > 1 }) { + t.Error("Expected result to be true, but got false") + } + + if common.All(list, func(value int) bool { return value%2 == 0 }) { + t.Error("Expected result to be false, but got true") + } +} diff --git a/components/requests/query.go b/components/requests/query.go index 5032b96..1657618 100644 --- a/components/requests/query.go +++ b/components/requests/query.go @@ -2,7 +2,9 @@ package requests import ( "fmt" + "github.com/opf/openproject-cli/components/common" "net/url" + "slices" "strings" ) @@ -26,6 +28,19 @@ func (filter Filter) String() string { ) } +func (filter Filter) Equals(other Filter) bool { + valuesEqual := true + + for _, value := range filter.Values { + if !slices.Contains(other.Values, value) { + valuesEqual = false + break + } + } + + return filter.Operator == other.Operator && filter.Name == other.Name && valuesEqual +} + func (query Query) Merge(another Query) Query { filters := append(query.filters, another.filters...) @@ -56,6 +71,32 @@ func (query Query) String() string { return queryStr } +func (query Query) Equals(other Query) bool { + filtersEqual := common.All( + query.filters, + func(filter Filter) bool { + filterExists := false + for _, f := range other.filters { + if filter.Equals(f) { + filterExists = true + break + } + } + + return filterExists + }) + + attributesEqual := true + for idx, value := range query.attributes { + if other.attributes[idx] != value { + attributesEqual = false + break + } + } + + return filtersEqual && attributesEqual +} + func filtersQueryAttribute(filters []Filter) string { if len(filters) == 0 { return "" diff --git a/components/requests/query_test.go b/components/requests/query_test.go index 100522a..be58969 100644 --- a/components/requests/query_test.go +++ b/components/requests/query_test.go @@ -1,12 +1,12 @@ package requests_test import ( - "github.com/opf/openproject-cli/components/resources/notifications" - "github.com/opf/openproject-cli/components/resources/work_packages" "strings" "testing" "github.com/opf/openproject-cli/components/requests" + "github.com/opf/openproject-cli/components/resources/notifications" + "github.com/opf/openproject-cli/components/resources/work_packages" ) func TestFilterQuery_String_WithFilters(t *testing.T) { @@ -100,6 +100,55 @@ func TestQuery_String(t *testing.T) { } } +func TestFilter_Equals(t *testing.T) { + filter1 := work_packages.StatusFilter("1,3") + filter2 := work_packages.StatusFilter("3,1") + filter3 := work_packages.StatusFilter("1") + + if !filter1.Equals(filter2) { + t.Errorf("Expected %+v to equal %+v", filter1, filter2) + } + + if filter1.Equals(filter3) { + t.Errorf("Expected %+v to not equal %+v", filter1, filter3) + } + + if filter2.Equals(filter3) { + t.Errorf("Expected %+v to not equal %+v", filter2, filter3) + } +} + +func TestQuery_Equals(t *testing.T) { + query1 := requests.NewQuery( + map[string]string{"pageSize": "20", "timestamps": "PT0S"}, + []requests.Filter{work_packages.TypeFilter("!1"), work_packages.StatusFilter("1,3")}, + ) + query2 := requests.NewQuery( + map[string]string{"timestamps": "PT0S", "pageSize": "20"}, + []requests.Filter{work_packages.StatusFilter("3,1"), work_packages.TypeFilter("!1")}, + ) + query3 := requests.NewQuery( + map[string]string{"pageSize": "20"}, + []requests.Filter{work_packages.StatusFilter("3,1"), work_packages.TypeFilter("!1")}, + ) + query4 := requests.NewQuery( + map[string]string{"timestamps": "PT0S", "pageSize": "20"}, + []requests.Filter{work_packages.StatusFilter("1"), work_packages.TypeFilter("!1")}, + ) + + if !query1.Equals(query2) { + t.Errorf("Expected %+v to equal %+v", query1, query2) + } + + if query1.Equals(query3) { + t.Errorf("Expected %+v to not equal %+v", query1, query2) + } + + if query1.Equals(query4) { + t.Errorf("Expected %+v to not equal %+v", query1, query2) + } +} + func TestQuery_Merge(t *testing.T) { attributes1 := map[string]string{ "pageSize": "20", @@ -117,13 +166,23 @@ func TestQuery_Merge(t *testing.T) { work_packages.TypeFilter("!1"), } + attributes3 := map[string]string{ + "pageSize": "25", + "timestamps": "PT0S", + "includeSubprojects": "true", + } + + filters3 := []requests.Filter{ + work_packages.TypeFilter("!1"), + work_packages.StatusFilter("1,3"), + } + query1 := requests.NewQuery(attributes1, filters1) query2 := requests.NewQuery(attributes2, filters2) + query3 := requests.NewQuery(attributes3, filters3) result := query1.Merge(query2) - expected := "filters=%5B%7B%22status%22%3A%7B%22operator%22%3A%22%3D%22%2C%22values%22%3A%5B%221%22%2C%223%22%5D%7D%7D%2C%7B%22type%22%3A%7B%22operator%22%3A%22%21%22%2C%22values%22%3A%5B%221%22%5D%7D%7D%5D&pageSize=25×tamps=PT0S&includeSubprojects=true" - - if result.String() != expected { - t.Errorf("Expected %s, but got %s", expected, result.String()) + if !result.Equals(query3) { + t.Errorf("Expected %+v, but got %+v", query3, result) } } diff --git a/go.mod b/go.mod index 29d1d58..ff4830f 100644 --- a/go.mod +++ b/go.mod @@ -1,17 +1,14 @@ module github.com/opf/openproject-cli -go 1.23.0 - -toolchain go1.23.4 +go 1.24.0 require ( github.com/briandowns/spinner v1.23.1 github.com/go-git/go-git/v5 v5.12.0 + github.com/sosodev/duration v1.3.1 github.com/spf13/cobra v1.8.1 ) -require github.com/sosodev/duration v1.3.1 - require ( dario.cat/mergo v1.0.1 // indirect github.com/Microsoft/go-winio v0.6.2 // indirect