Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add assert on DocIndex for child documents #2871

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions tests/integration/index/query_with_relation_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,19 +864,19 @@ func TestQueryWithIndexOnOneToMany_IfIndexedRelationIsNil_EqNilFilterShouldUseIn
},
testUtils.CreateDoc{
CollectionID: 1,
Doc: `{
"model": "Walkman",
DocMap: map[string]any{
"model": "Walkman",
"manufacturer": "Sony",
"owner": "bae-5622129c-b893-5768-a3f4-8f745db4cc04"
}`,
"owner": testUtils.NewDocIndex(0, 0),
},
},
testUtils.CreateDoc{
CollectionID: 1,
Doc: `{
"model": "iPhone",
DocMap: map[string]any{
"model": "iPhone",
"manufacturer": "Apple",
"owner": "bae-5622129c-b893-5768-a3f4-8f745db4cc04"
}`,
"owner": testUtils.NewDocIndex(0, 0),
},
},
testUtils.CreateDoc{
CollectionID: 1,
Expand Down Expand Up @@ -967,8 +967,8 @@ func TestQueryWithIndexOnManyToOne_MultipleViaOneToMany(t *testing.T) {
{
"devices": []map[string]any{
{
"owner_id": "bae-1ef746f8-821e-586f-99b2-4cb1fb9b782f",
"manufacturer_id": "bae-18c7d707-c44d-552f-b6d6-9e3d05bbf9c1",
"owner_id": testUtils.NewDocIndex(0, 0),
"manufacturer_id": testUtils.NewDocIndex(2, 0),
},
},
},
Expand Down
30 changes: 15 additions & 15 deletions tests/integration/query/one_to_many_to_many/joins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,20 +187,20 @@ func TestOneToManyToManyJoinsAreLinkedProperly(t *testing.T) {
}`,
Results: []map[string]any{
{
"_docID": "bae-4819f8a1-b519-5b46-ae39-4fdda8558e4f",
"_docID": testUtils.NewDocIndex(0, 2),
"book": []map[string]any{},
"name": "Not a Writer",
},
{
"name": "Cornelia Funke",
"_docID": "bae-72e8c691-9f20-55e7-9228-8af1cf54cace",
"_docID": testUtils.NewDocIndex(0, 1),
"book": []map[string]any{
{
"_docID": "bae-4dbc2bbc-0652-5412-8063-486499f1c341",
"_docID": testUtils.NewDocIndex(1, 0),
"name": "The Rooster Bar",
"publisher": []map[string]any{
{
"_docID": "bae-8a8cbab7-65db-5955-b618-b82f44761cee",
"_docID": testUtils.NewDocIndex(2, 0),
"name": "Only Publisher of The Rooster Bar",
},
},
Expand All @@ -209,53 +209,53 @@ func TestOneToManyToManyJoinsAreLinkedProperly(t *testing.T) {
},
{
"name": "John Grisham",
"_docID": "bae-e1ea288f-09fa-55fa-b0b5-0ac8941ea35b",
"_docID": testUtils.NewDocIndex(0, 0),
"book": []map[string]any{
{
"_docID": "bae-13164fd9-60fd-5c32-9cb5-8bff3ef8ea53",
"_docID": testUtils.NewDocIndex(1, 1),
"name": "Theif Lord",
"publisher": []map[string]any{
{
"_docID": "bae-0107f5cc-c25a-5295-8439-2b08a286af83",
"_docID": testUtils.NewDocIndex(2, 1),
"name": "Only Publisher of Theif Lord",
},
},
},
{
"_docID": "bae-1ccf3043-d760-543e-be1b-6691fa6aa7a8",
"_docID": testUtils.NewDocIndex(1, 2),
"name": "The Associate",
"publisher": []map[string]any{},
},
{
"_docID": "bae-5366ba09-54e8-5381-8169-a770aa9282ae",
"_docID": testUtils.NewDocIndex(1, 3),
"name": "Painted House",
"publisher": []map[string]any{
{
"_docID": "bae-35f1e55a-c51b-53d7-9b28-9beb904a1343",
"_docID": testUtils.NewDocIndex(2, 2),
"name": "Only Publisher of Painted House",
},
},
},
{
"_docID": "bae-96c9de0f-2903-5589-9604-b42882afde8c",
"_docID": testUtils.NewDocIndex(1, 4),
"name": "A Time for Mercy",
"publisher": []map[string]any{
{
"_docID": "bae-37451579-7e50-541d-8a3c-849b290ea416",
"_docID": testUtils.NewDocIndex(2, 3),
"name": "Only Publisher of A Time for Mercy",
},
},
},
{
"_docID": "bae-f52abfc3-9026-5713-9622-2d3458a386e0",
"_docID": testUtils.NewDocIndex(1, 5),
"name": "Sooley",
"publisher": []map[string]any{
{
"_docID": "bae-c46b7771-843e-51ac-92be-d145aa2cfc07",
"_docID": testUtils.NewDocIndex(2, 5),
"name": "Second of Two Publishers of Sooley",
},
{
"_docID": "bae-fc233f9c-f117-59de-be2b-60e4f6f0a898",
"_docID": testUtils.NewDocIndex(2, 4),
"name": "First of Two Publishers of Sooley",
},
},
Expand Down
64 changes: 49 additions & 15 deletions tests/integration/utils2.go
Original file line number Diff line number Diff line change
Expand Up @@ -1852,43 +1852,61 @@ func assertRequestResults(

log.InfoContext(s.ctx, "", corelog.Any("RequestResults", result.Data))

return assertRequestResultDocs(s, nodeID, expectedResults, resultantData, anyOfByField)
}

func assertRequestResultDocs(
s *state,
nodeID int,
expectedResults []map[string]any,
actualResults []map[string]any,
anyOfByField map[docFieldKey][]any,
) bool {
// compare results
require.Equal(s.t, len(expectedResults), len(resultantData),
require.Equal(s.t, len(expectedResults), len(actualResults),
s.testCase.Description+" \n(number of results don't match)")

for docIndex, result := range resultantData {
expectedResult := expectedResults[docIndex]
for actualDocIndex, actualDoc := range actualResults {
expectedDoc := expectedResults[actualDocIndex]

require.Equal(
s.t,
len(expectedResult),
len(result),
len(expectedDoc),
len(actualDoc),
fmt.Sprintf(
"%s \n(number of properties for item at index %v don't match)",
s.testCase.Description,
docIndex,
actualDocIndex,
),
)

for field, actualValue := range result {
expectedValue := expectedResult[field]

switch r := expectedValue.(type) {
for field, actualValue := range actualDoc {
switch expectedValue := expectedDoc[field].(type) {
case AnyOf:
assertResultsAnyOf(s.t, s.clientType, r, actualValue)
assertResultsAnyOf(s.t, s.clientType, expectedValue, actualValue)

dfk := docFieldKey{docIndex, field}
dfk := docFieldKey{actualDocIndex, field}
valueSet := anyOfByField[dfk]
valueSet = append(valueSet, actualValue)
anyOfByField[dfk] = valueSet
case DocIndex:
expectedDocID := s.docIDs[r.CollectionIndex][r.Index].String()
expectedDocID := s.docIDs[expectedValue.CollectionIndex][expectedValue.Index].String()
assertResultsEqual(
s.t,
s.clientType,
expectedDocID,
actualValue,
fmt.Sprintf("node: %v, doc: %v", nodeID, docIndex),
fmt.Sprintf("node: %v, doc: %v", nodeID, actualDocIndex),
)
case []map[string]any:
actualValueMap := convertToArrayOfMaps(s.t, actualValue)

assertRequestResultDocs(
s,
nodeID,
expectedValue,
actualValueMap,
anyOfByField,
)

default:
Expand All @@ -1897,7 +1915,7 @@ func assertRequestResults(
s.clientType,
expectedValue,
actualValue,
fmt.Sprintf("node: %v, doc: %v", nodeID, docIndex),
fmt.Sprintf("node: %v, doc: %v", nodeID, actualDocIndex),
)
}
}
Expand All @@ -1906,6 +1924,22 @@ func assertRequestResults(
return false
}

func convertToArrayOfMaps(t testing.TB, value any) []map[string]any {
valueArrayMap, ok := value.([]map[string]any)
if ok {
return valueArrayMap
}
valueArray, ok := value.([]any)
require.True(t, ok, "expected value to be an array of maps %v", value)

valueArrayMap = make([]map[string]any, len(valueArray))
for i, v := range valueArray {
valueArrayMap[i], ok = v.(map[string]any)
require.True(t, ok, "expected value to be an array of maps %v", value)
}
return valueArrayMap
}

func assertExpectedErrorRaised(t testing.TB, description string, expectedError string, wasRaised bool) {
if expectedError != "" && !wasRaised {
assert.Fail(t, "Expected an error however none was raised.", description)
Expand Down
Loading