Skip to content

Commit 0f682f8

Browse files
committed
[chore] bumped go version
- fixed flickering query merge test - added comparison to Query and Filter struct
1 parent 0b47e89 commit 0f682f8

File tree

7 files changed

+136
-14
lines changed

7 files changed

+136
-14
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
- name: Set up Go
2020
uses: actions/setup-go@v3
2121
with:
22-
go-version: '1.23.4'
22+
go-version: '1.24.0'
2323

2424
- name: Build
2525
run: go build -v ./...

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- name: Set up Go
1515
uses: actions/setup-go@v3
1616
with:
17-
go-version: '1.23.4'
17+
go-version: '1.24.0'
1818

1919
- name: Set version
2020
run: echo "version=${GITHUB_REF#refs/*/}" >> "$GITHUB_ENV"

components/common/slice.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,15 @@ func Filter[T any](slice []T, f func(T) bool) []T {
3131
[]T{},
3232
)
3333
}
34+
35+
func All[T any](slice []T, f func(T) bool) bool {
36+
for _, val := range slice {
37+
if f(val) {
38+
continue
39+
} else {
40+
return false
41+
}
42+
}
43+
44+
return true
45+
}

components/common/slice_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package common_test
22

33
import (
4-
"github.com/opf/openproject-cli/components/common"
54
"testing"
5+
6+
"github.com/opf/openproject-cli/components/common"
67
)
78

89
func TestContains(t *testing.T) {
@@ -47,3 +48,15 @@ func TestFilter(t *testing.T) {
4748
t.Errorf("Expected %v to contain %d and %d, but does not", list, list[2], list[3])
4849
}
4950
}
51+
52+
func TestAll(t *testing.T) {
53+
list := []int{23, 245, 54, 132, 4325}
54+
55+
if !common.All(list, func(value int) bool { return value > 1 }) {
56+
t.Error("Expected result to be true, but got false")
57+
}
58+
59+
if common.All(list, func(value int) bool { return value%2 == 0 }) {
60+
t.Error("Expected result to be false, but got true")
61+
}
62+
}

components/requests/query.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package requests
22

33
import (
44
"fmt"
5+
"github.com/opf/openproject-cli/components/common"
56
"net/url"
7+
"slices"
68
"strings"
79
)
810

@@ -26,6 +28,19 @@ func (filter Filter) String() string {
2628
)
2729
}
2830

31+
func (filter Filter) Equals(other Filter) bool {
32+
valuesEqual := true
33+
34+
for _, value := range filter.Values {
35+
if !slices.Contains(other.Values, value) {
36+
valuesEqual = false
37+
break
38+
}
39+
}
40+
41+
return filter.Operator == other.Operator && filter.Name == other.Name && valuesEqual
42+
}
43+
2944
func (query Query) Merge(another Query) Query {
3045
filters := append(query.filters, another.filters...)
3146

@@ -56,6 +71,32 @@ func (query Query) String() string {
5671
return queryStr
5772
}
5873

74+
func (query Query) Equals(other Query) bool {
75+
filtersEqual := common.All(
76+
query.filters,
77+
func(filter Filter) bool {
78+
filterExists := false
79+
for _, f := range other.filters {
80+
if filter.Equals(f) {
81+
filterExists = true
82+
break
83+
}
84+
}
85+
86+
return filterExists
87+
})
88+
89+
attributesEqual := true
90+
for idx, value := range query.attributes {
91+
if other.attributes[idx] != value {
92+
attributesEqual = false
93+
break
94+
}
95+
}
96+
97+
return filtersEqual && attributesEqual
98+
}
99+
59100
func filtersQueryAttribute(filters []Filter) string {
60101
if len(filters) == 0 {
61102
return ""

components/requests/query_test.go

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package requests_test
22

33
import (
4-
"github.com/opf/openproject-cli/components/resources/notifications"
5-
"github.com/opf/openproject-cli/components/resources/work_packages"
64
"strings"
75
"testing"
86

97
"github.com/opf/openproject-cli/components/requests"
8+
"github.com/opf/openproject-cli/components/resources/notifications"
9+
"github.com/opf/openproject-cli/components/resources/work_packages"
1010
)
1111

1212
func TestFilterQuery_String_WithFilters(t *testing.T) {
@@ -100,6 +100,55 @@ func TestQuery_String(t *testing.T) {
100100
}
101101
}
102102

103+
func TestFilter_Equals(t *testing.T) {
104+
filter1 := work_packages.StatusFilter("1,3")
105+
filter2 := work_packages.StatusFilter("3,1")
106+
filter3 := work_packages.StatusFilter("1")
107+
108+
if !filter1.Equals(filter2) {
109+
t.Errorf("Expected %+v to equal %+v", filter1, filter2)
110+
}
111+
112+
if filter1.Equals(filter3) {
113+
t.Errorf("Expected %+v to not equal %+v", filter1, filter3)
114+
}
115+
116+
if filter2.Equals(filter3) {
117+
t.Errorf("Expected %+v to not equal %+v", filter2, filter3)
118+
}
119+
}
120+
121+
func TestQuery_Equals(t *testing.T) {
122+
query1 := requests.NewQuery(
123+
map[string]string{"pageSize": "20", "timestamps": "PT0S"},
124+
[]requests.Filter{work_packages.TypeFilter("!1"), work_packages.StatusFilter("1,3")},
125+
)
126+
query2 := requests.NewQuery(
127+
map[string]string{"timestamps": "PT0S", "pageSize": "20"},
128+
[]requests.Filter{work_packages.StatusFilter("3,1"), work_packages.TypeFilter("!1")},
129+
)
130+
query3 := requests.NewQuery(
131+
map[string]string{"pageSize": "20"},
132+
[]requests.Filter{work_packages.StatusFilter("3,1"), work_packages.TypeFilter("!1")},
133+
)
134+
query4 := requests.NewQuery(
135+
map[string]string{"timestamps": "PT0S", "pageSize": "20"},
136+
[]requests.Filter{work_packages.StatusFilter("1"), work_packages.TypeFilter("!1")},
137+
)
138+
139+
if !query1.Equals(query2) {
140+
t.Errorf("Expected %+v to equal %+v", query1, query2)
141+
}
142+
143+
if query1.Equals(query3) {
144+
t.Errorf("Expected %+v to not equal %+v", query1, query2)
145+
}
146+
147+
if query1.Equals(query4) {
148+
t.Errorf("Expected %+v to not equal %+v", query1, query2)
149+
}
150+
}
151+
103152
func TestQuery_Merge(t *testing.T) {
104153
attributes1 := map[string]string{
105154
"pageSize": "20",
@@ -117,13 +166,23 @@ func TestQuery_Merge(t *testing.T) {
117166
work_packages.TypeFilter("!1"),
118167
}
119168

169+
attributes3 := map[string]string{
170+
"pageSize": "25",
171+
"timestamps": "PT0S",
172+
"includeSubprojects": "true",
173+
}
174+
175+
filters3 := []requests.Filter{
176+
work_packages.TypeFilter("!1"),
177+
work_packages.StatusFilter("1,3"),
178+
}
179+
120180
query1 := requests.NewQuery(attributes1, filters1)
121181
query2 := requests.NewQuery(attributes2, filters2)
182+
query3 := requests.NewQuery(attributes3, filters3)
122183

123184
result := query1.Merge(query2)
124-
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"
125-
126-
if result.String() != expected {
127-
t.Errorf("Expected %s, but got %s", expected, result.String())
185+
if !result.Equals(query3) {
186+
t.Errorf("Expected %+v, but got %+v", query3, result)
128187
}
129188
}

go.mod

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
module github.com/opf/openproject-cli
22

3-
go 1.23.0
4-
5-
toolchain go1.23.4
3+
go 1.24.0
64

75
require (
86
github.com/briandowns/spinner v1.23.1
97
github.com/go-git/go-git/v5 v5.12.0
8+
github.com/sosodev/duration v1.3.1
109
github.com/spf13/cobra v1.8.1
1110
)
1211

13-
require github.com/sosodev/duration v1.3.1
14-
1512
require (
1613
dario.cat/mergo v1.0.1 // indirect
1714
github.com/Microsoft/go-winio v0.6.2 // indirect

0 commit comments

Comments
 (0)