-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add tests for deletion of documents with a filter
- Loading branch information
1 parent
8ddb27f
commit 2ad0ec7
Showing
2 changed files
with
289 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,289 @@ | ||
// Copyright 2022 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package delete | ||
|
||
import ( | ||
"testing" | ||
|
||
testUtils "github.com/sourcenetwork/defradb/db/tests" | ||
simpleTests "github.com/sourcenetwork/defradb/db/tests/mutation/simple" | ||
) | ||
|
||
func TestDeletionOfDocumentsWithFilter_Success(t *testing.T) { | ||
tests := []testUtils.QueryTestCase{ | ||
|
||
{ | ||
Description: "Delete using filter - One matching document, that exists.", | ||
|
||
Query: `mutation { | ||
delete_user(filter: {name: {_eq: "Shahzad"}}) { | ||
_key | ||
} | ||
}`, | ||
|
||
Docs: map[int][]string{ | ||
0: { | ||
(`{ | ||
"name": "Shahzad", | ||
"age": 26, | ||
"points": 48.48, | ||
"verified": true | ||
}`), | ||
}, | ||
}, | ||
|
||
Results: []map[string]interface{}{ | ||
{ | ||
"_key": "bae-6a6482a8-24e1-5c73-a237-ca569e41507d", | ||
}, | ||
}, | ||
|
||
ExpectedError: "", | ||
}, | ||
|
||
{ | ||
Description: "Delete using filter - Multiple matching documents that exist.", | ||
Query: `mutation { | ||
delete_user(filter: {name: {_eq: "Shahzad"}}) { | ||
_key | ||
} | ||
}`, | ||
|
||
Docs: map[int][]string{ | ||
0: { | ||
(`{ | ||
"name": "Shahzad", | ||
"age": 26, | ||
"points": 48.48, | ||
"verified": true | ||
}`), | ||
(`{ | ||
"name": "Shahzad", | ||
"age": 25, | ||
"points": 48.48, | ||
"verified": true | ||
}`), | ||
(`{ | ||
"name": "Shahzad", | ||
"age": 6, | ||
"points": 48.48, | ||
"verified": true | ||
}`), | ||
(`{ | ||
"name": "Shahzad", | ||
"age": 1, | ||
"points": 48.48, | ||
"verified": true | ||
}`), | ||
(`{ | ||
"name": "John", | ||
"age": 26, | ||
"points": 48.48, | ||
"verified": true | ||
}`), | ||
}, | ||
}, | ||
|
||
Results: []map[string]interface{}{ | ||
{ | ||
"_key": "bae-4b5b1765-560c-5843-9abc-24d21d8aa598", | ||
}, | ||
{ | ||
"_key": "bae-5a8530c0-c521-5e83-8243-4ce267bc76fa", | ||
}, | ||
{ | ||
"_key": "bae-6a6482a8-24e1-5c73-a237-ca569e41507d", | ||
}, | ||
{ | ||
"_key": "bae-ca88bc10-1415-59b1-a72c-d19ed44d4e15", | ||
}, | ||
}, | ||
|
||
ExpectedError: "", | ||
}, | ||
|
||
{ | ||
Description: "Delete using filter - Multiple matching documents that exist with alias.", | ||
|
||
Query: `mutation { | ||
delete_user(filter: { | ||
_and: [ | ||
{age: {_lt: 26}}, | ||
{verified: {_eq: true}}, | ||
] | ||
}) { | ||
DeletedKeyByFilter: _key | ||
} | ||
}`, | ||
|
||
Docs: map[int][]string{ | ||
0: { | ||
(`{ | ||
"name": "Shahzad", | ||
"age": 26, | ||
"points": 48.48, | ||
"verified": true | ||
}`), | ||
(`{ | ||
"name": "Shahzad", | ||
"age": 25, | ||
"points": 48.48, | ||
"verified": true | ||
}`), | ||
(`{ | ||
"name": "Shahzad", | ||
"age": 6, | ||
"points": 48.48, | ||
"verified": true | ||
}`), | ||
(`{ | ||
"name": "Shahzad", | ||
"age": 1, | ||
"points": 48.48, | ||
"verified": true | ||
}`), | ||
(`{ | ||
"name": "John", | ||
"age": 26, | ||
"points": 48.48, | ||
"verified": true | ||
}`), | ||
}, | ||
}, | ||
|
||
Results: []map[string]interface{}{ | ||
{ | ||
"DeletedKeyByFilter": "bae-4b5b1765-560c-5843-9abc-24d21d8aa598", | ||
}, | ||
{ | ||
"DeletedKeyByFilter": "bae-5a8530c0-c521-5e83-8243-4ce267bc76fa", | ||
}, | ||
{ | ||
"DeletedKeyByFilter": "bae-ca88bc10-1415-59b1-a72c-d19ed44d4e15", | ||
}, | ||
}, | ||
|
||
ExpectedError: "", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
simpleTests.ExecuteTestCase(t, test) | ||
} | ||
} | ||
|
||
func TestDeletionOfDocumentsWithFilter_Failure(t *testing.T) { | ||
tests := []testUtils.QueryTestCase{ | ||
{ | ||
Description: "No delete with filter: because no document matches filter.", | ||
|
||
Query: `mutation { | ||
delete_user(filter: {name: {_eq: "Lone"}}) { | ||
_key | ||
} | ||
}`, | ||
|
||
Docs: map[int][]string{ | ||
0: { | ||
(`{ | ||
"name": "Shahzad", | ||
"age": 26, | ||
"points": 48.48, | ||
"verified": true | ||
}`), | ||
}, | ||
}, | ||
|
||
Results: nil, | ||
|
||
ExpectedError: "", | ||
}, | ||
|
||
{ | ||
Description: "No delete with filter: because the collection is empty.", | ||
|
||
Query: `mutation { | ||
delete_user(filter: {name: {_eq: "Shahzad"}}) { | ||
_key | ||
} | ||
}`, | ||
|
||
Docs: map[int][]string{}, | ||
|
||
Results: nil, | ||
|
||
ExpectedError: "", | ||
}, | ||
|
||
{ | ||
Description: "No delete with filter: because has no sub-selection.", | ||
|
||
Query: `mutation { | ||
delete_user(filter: {name: {_eq: "Shahzad"}}) | ||
}`, | ||
|
||
Docs: map[int][]string{ | ||
0: { | ||
(`{ | ||
"name": "Shahzad", | ||
"age": 26, | ||
"points": 48.48, | ||
"verified": true | ||
}`), | ||
(`{ | ||
"name": "John", | ||
"age": 26, | ||
"points": 48.48, | ||
"verified": true | ||
}`), | ||
}, | ||
}, | ||
|
||
Results: nil, | ||
|
||
ExpectedError: "[Field \"delete_user\" of type \"[user]\" must have a sub selection.]", | ||
}, | ||
|
||
{ | ||
Description: "No delete with filter: because has no _key in sub-selection.", | ||
|
||
Query: `mutation { | ||
delete_user(filter: {name: {_eq: "Shahzad"}}) { | ||
} | ||
}`, | ||
|
||
Docs: map[int][]string{ | ||
0: { | ||
(`{ | ||
"name": "Shahzad", | ||
"age": 26, | ||
"points": 48.48, | ||
"verified": true | ||
}`), | ||
(`{ | ||
"name": "John", | ||
"age": 26, | ||
"points": 48.48, | ||
"verified": true | ||
}`), | ||
}, | ||
}, | ||
|
||
Results: nil, | ||
|
||
ExpectedError: "Syntax Error GraphQL request (2:53) Unexpected empty IN {}\n\n1: mutation {\n2: \\u0009\\u0009\\u0009\\u0009\\u0009\\u0009delete_user(filter: {name: {_eq: \"Shahzad\"}}) {\n ^\n3: \\u0009\\u0009\\u0009\\u0009\\u0009\\u0009}\n", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
simpleTests.ExecuteTestCase(t, test) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters