Skip to content

Commit

Permalink
add WithTotal to /query
Browse files Browse the repository at this point in the history
  • Loading branch information
IngoRoessner committed Dec 11, 2023
1 parent d265b5d commit 9d1ba7f
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 33 deletions.
165 changes: 165 additions & 0 deletions lib/apiv3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,48 @@ func TestApiV3(t *testing.T) {
}
})

t.Run("client query search total", func(t *testing.T) {
actual, _, err := client.QueryWithTotal[[]TestAspect](c, ctoken, client.QueryMessage{
Resource: "aspects",
Find: &client.QueryFind{
QueryListCommons: client.QueryListCommons{
Limit: 3,
Offset: 1,
SortBy: "name",
SortDesc: true,
},
Search: "aspect",
},
})
if err != nil {
t.Error(err)
return
}
temp, err := client.JsonCast[[]TestAspect]([]map[string]interface{}{
getTestAspectResult("aspect4"),
getTestAspectResult("aspect3"),
getTestAspectResult("aspect2"),
})
if err != nil {
t.Error(err)
return
}
expected := client.WithTotal[[]TestAspect]{
Result: temp,
Total: 5,
}
if len(expected.Result) == 0 || expected.Result[0].Name == "" {
t.Error(expected)
return
}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("\n%#v\n%#v\n", expected, actual)
actualJson, _ := json.Marshal(actual)
expectedJson, _ := json.Marshal(expected)
t.Log("\n", string(actualJson), "\n", string(expectedJson))
}
})

t.Run("query search", testRequest(config, "POST", "/v3/query", model.QueryMessage{
Resource: "aspects",
Find: &model.QueryFind{
Expand Down Expand Up @@ -717,6 +759,47 @@ func TestApiV3(t *testing.T) {
}
})

t.Run("client query search filter with total", func(t *testing.T) {
actual, _, err := client.QueryWithTotal[[]TestAspect](c, ctoken, client.QueryMessage{
Resource: "aspects",
Find: &client.QueryFind{
Filter: &client.Selection{
Condition: client.ConditionConfig{
Feature: "features.name",
Operation: "==",
Value: "aspect5_name",
},
},
Search: "aspect",
},
})
if err != nil {
t.Error(err)
return
}
temp, err := client.JsonCast[[]TestAspect]([]map[string]interface{}{
getTestAspectResult("aspect5"),
})
if err != nil {
t.Error(err)
return
}
expected := client.WithTotal[[]TestAspect]{
Result: temp,
Total: 1,
}
if len(expected.Result) == 0 || expected.Result[0].Name == "" {
t.Error(expected)
return
}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("\n%#v\n%#v\n", expected, actual)
actualJson, _ := json.Marshal(actual)
expectedJson, _ := json.Marshal(expected)
t.Log("\n", string(actualJson), "\n", string(expectedJson))
}
})

t.Run("query search filter", testRequest(config, "POST", "/v3/query", model.QueryMessage{
Resource: "aspects",
Find: &model.QueryFind{
Expand Down Expand Up @@ -798,6 +881,46 @@ func TestApiV3(t *testing.T) {
}
})

t.Run("client query filter Total", func(t *testing.T) {
actual, _, err := client.QueryWithTotal[[]TestAspect](c, ctoken, client.QueryMessage{
Resource: "aspects",
Find: &client.QueryFind{
Filter: &client.Selection{
Condition: client.ConditionConfig{
Feature: "features.name",
Operation: "==",
Value: "aspect5_name",
},
},
},
})
if err != nil {
t.Error(err)
return
}
temp, err := client.JsonCast[[]TestAspect]([]map[string]interface{}{
getTestAspectResult("aspect5"),
})
if err != nil {
t.Error(err)
return
}
expected := client.WithTotal[[]TestAspect]{
Result: temp,
Total: 1,
}
if len(expected.Result) == 0 || expected.Result[0].Name == "" {
t.Error(expected)
return
}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("\n%#v\n%#v\n", expected, actual)
actualJson, _ := json.Marshal(actual)
expectedJson, _ := json.Marshal(expected)
t.Log("\n", string(actualJson), "\n", string(expectedJson))
}
})

t.Run("query filter", testRequest(config, "POST", "/v3/query", model.QueryMessage{
Resource: "aspects",
Find: &model.QueryFind{
Expand Down Expand Up @@ -911,6 +1034,48 @@ func TestApiV3(t *testing.T) {
}
})

t.Run("client query ids 5 generic total", func(t *testing.T) {
actual, _, err := client.QueryWithTotal[[]TestAspect](c, ctoken, client.QueryMessage{
Resource: "aspects",
ListIds: &client.QueryListIds{
QueryListCommons: client.QueryListCommons{
Limit: 3,
Offset: 1,
SortBy: "name",
SortDesc: true,
},
Ids: []string{"aspect1", "aspect2", "aspect3", "aspect4", "aspect5"},
},
})
if err != nil {
t.Error(err)
return
}
temp, err := client.JsonCast[[]TestAspect]([]map[string]interface{}{
getTestAspectResult("aspect4"),
getTestAspectResult("aspect3"),
getTestAspectResult("aspect2"),
})
if err != nil {
t.Error(err)
return
}
expected := client.WithTotal[[]TestAspect]{
Result: temp,
Total: 5,
}
if len(expected.Result) == 0 || expected.Result[0].Name == "" {
t.Error(expected)
return
}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("\n%#v\n%#v\n", expected, actual)
actualJson, _ := json.Marshal(actual)
expectedJson, _ := json.Marshal(expected)
t.Log("\n", string(actualJson), "\n", string(expectedJson))
}
})

t.Run("query ids 5", testRequest(config, "POST", "/v3/query", model.QueryMessage{
Resource: "aspects",
ListIds: &model.QueryListIds{
Expand Down
15 changes: 15 additions & 0 deletions lib/client/generics.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ func Query[Result any](client Client, token string, query model.QueryMessage) (r
return result, code, err
}

type WithTotal[Result any] struct {
Total int64
Result Result
}

func QueryWithTotal[Result any](client Client, token string, query model.QueryMessage) (result WithTotal[Result], code int, err error) {
if query.Find != nil {
query.Find.QueryListCommons.WithTotal = true
}
if query.ListIds != nil {
query.ListIds.QueryListCommons.WithTotal = true
}
return Query[WithTotal[Result]](client, token, query)
}

func List[Result any](client Client, token string, kind string, options model.ListOptions) (result Result, err error) {
temp, err := client.List(token, kind, options)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions lib/model/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type QueryListCommons struct {
// possible modifiers can be found and configured in the configuration.json under result_modifiers
// example value: url.Values{"service_group_selection": {"a8ee3b1c-4cda-4f0d-9f55-4ef4882ce0af"}}
AddIdModifier url.Values `json:"add_id_modifier,omitempty"`

WithTotal bool `json:"with_total"`
}

type ListAfter struct {
Expand Down
Loading

0 comments on commit 9d1ba7f

Please sign in to comment.