Skip to content

Commit

Permalink
[chore] bumped go version
Browse files Browse the repository at this point in the history
- fixed flickering query merge test
- added comparison to Query and Filter struct
  • Loading branch information
Kharonus committed Feb 19, 2025
1 parent 0b47e89 commit 0f682f8
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 12 additions & 0 deletions components/common/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
15 changes: 14 additions & 1 deletion components/common/slice_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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")
}
}
41 changes: 41 additions & 0 deletions components/requests/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package requests

import (
"fmt"
"github.com/opf/openproject-cli/components/common"
"net/url"
"slices"
"strings"
)

Expand All @@ -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...)

Expand Down Expand Up @@ -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 ""
Expand Down
71 changes: 65 additions & 6 deletions components/requests/query_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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",
Expand All @@ -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&timestamps=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)
}
}
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 0f682f8

Please sign in to comment.