Skip to content

Commit

Permalink
refactor(i): Subscription test results (#2874)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves #2873

## Description

This PR is small refactor of the subscription tests to make it easier to
test ordering of results and eventually multiple selections within the
results.

## Tasks

- [x] I made sure the code is well commented, particularly
hard-to-understand areas.
- [x] I made sure the repository-held documentation is changed
accordingly.
- [x] I made sure the pull request title adheres to the conventional
commit style (the subset used in the project can be found in
[tools/configs/chglog/config.yml](tools/configs/chglog/config.yml)).
- [x] I made sure to discuss its limitations such as threats to
validity, vulnerability to mistake and misuse, robustness to
invalidation of assumptions, resource requirements, ...

## How has this been tested?

`make test`

Specify the platform(s) on which this was tested:
- MacOS
  • Loading branch information
nasdf authored Jul 26, 2024
1 parent e422058 commit 82659f8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 62 deletions.
64 changes: 39 additions & 25 deletions tests/integration/subscription/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@ func TestSubscriptionWithCreateMutations(t *testing.T) {
age
}
}`,
Results: []map[string]any{
Results: [][]map[string]any{
{
"_docID": "bae-b3ce089b-f543-5984-be9f-ad7d08969f4e",
"age": int64(27),
"name": "John",
{
"_docID": "bae-b3ce089b-f543-5984-be9f-ad7d08969f4e",
"age": int64(27),
"name": "John",
},
},
{
"_docID": "bae-bc20b854-10b3-5408-b28c-f273ddda9434",
"age": int64(31),
"name": "Addo",
{
"_docID": "bae-bc20b854-10b3-5408-b28c-f273ddda9434",
"age": int64(31),
"name": "Addo",
},
},
},
},
Expand Down Expand Up @@ -82,10 +86,12 @@ func TestSubscriptionWithFilterAndOneCreateMutation(t *testing.T) {
age
}
}`,
Results: []map[string]any{
Results: [][]map[string]any{
{
"age": int64(27),
"name": "John",
{
"age": int64(27),
"name": "John",
},
},
},
},
Expand Down Expand Up @@ -119,7 +125,7 @@ func TestSubscriptionWithFilterAndOneCreateMutationOutsideFilter(t *testing.T) {
age
}
}`,
Results: []map[string]any{},
Results: [][]map[string]any{},
},
testUtils.Request{
Request: `mutation {
Expand Down Expand Up @@ -150,10 +156,12 @@ func TestSubscriptionWithFilterAndCreateMutations(t *testing.T) {
age
}
}`,
Results: []map[string]any{
Results: [][]map[string]any{
{
"age": int64(27),
"name": "John",
{
"age": int64(27),
"name": "John",
},
},
},
},
Expand Down Expand Up @@ -217,11 +225,13 @@ func TestSubscriptionWithUpdateMutations(t *testing.T) {
points
}
}`,
Results: []map[string]any{
Results: [][]map[string]any{
{
"age": int64(27),
"name": "John",
"points": float64(45),
{
"age": int64(27),
"name": "John",
"points": float64(45),
},
},
},
},
Expand Down Expand Up @@ -273,16 +283,20 @@ func TestSubscriptionWithUpdateAllMutations(t *testing.T) {
points
}
}`,
Results: []map[string]any{
Results: [][]map[string]any{
{
"age": int64(31),
"name": "Addo",
"points": float64(55),
{
"age": int64(31),
"name": "Addo",
"points": float64(55),
},
},
{
"age": int64(27),
"name": "John",
"points": float64(55),
{
"age": int64(27),
"name": "John",
"points": float64(55),
},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ type SubscriptionRequest struct {
Request string

// The expected (data) results yielded through the subscription across its lifetime.
Results []map[string]any
Results [][]map[string]any

// Any error expected from the action. Optional.
//
Expand Down
56 changes: 20 additions & 36 deletions tests/integration/utils2.go
Original file line number Diff line number Diff line change
Expand Up @@ -1752,50 +1752,34 @@ func executeSubscriptionRequest(
}

go func() {
data := []map[string]any{}
errs := []error{}

var results []*client.GQLResult
allActionsAreDone := false
expectedDataRecieved := len(action.Results) == 0
for {
for !allActionsAreDone || len(results) < len(action.Results) {
select {
case s := <-result.Subscription:
sData, _ := s.Data.([]map[string]any)
errs = append(errs, s.Errors...)
data = append(data, sData...)

if len(data) >= len(action.Results) {
expectedDataRecieved = true
}
results = append(results, &s)

case <-s.allActionsDone:
allActionsAreDone = true
}
}

if expectedDataRecieved && allActionsAreDone {
finalResult := &client.GQLResult{
Data: data,
Errors: errs,
}

subscriptionAssert <- func() {
// This assert should be executed from the main test routine
// so that failures will be properly handled.
expectedErrorRaised := assertRequestResults(
s,
finalResult,
action.Results,
action.ExpectedError,
nil,
// anyof is not yet supported by subscription requests
0,
map[docFieldKey][]any{},
)

assertExpectedErrorRaised(s.t, s.testCase.Description, action.ExpectedError, expectedErrorRaised)
}

return
subscriptionAssert <- func() {
for i, r := range action.Results {
// This assert should be executed from the main test routine
// so that failures will be properly handled.
expectedErrorRaised := assertRequestResults(
s,
results[i],
r,
action.ExpectedError,
nil,
// anyof is not yet supported by subscription requests
0,
map[docFieldKey][]any{},
)

assertExpectedErrorRaised(s.t, s.testCase.Description, action.ExpectedError, expectedErrorRaised)
}
}
}()
Expand Down

0 comments on commit 82659f8

Please sign in to comment.