Skip to content

Commit

Permalink
test: Add tests for deletion of documents with a filter
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzadlone committed Feb 15, 2022
1 parent 8ddb27f commit 2ad0ec7
Show file tree
Hide file tree
Showing 2 changed files with 289 additions and 5 deletions.
289 changes: 289 additions & 0 deletions db/tests/mutation/simple/delete/with_filter_test.go
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)
}
}
5 changes: 0 additions & 5 deletions query/graphql/planner/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
package planner

import (
"fmt"

"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/core"
"github.com/sourcenetwork/defradb/document/key"
Expand Down Expand Up @@ -61,17 +59,14 @@ func (n *deleteNode) Next() (bool, error) {
}
results, err = n.collection.DeleteWithKeys(n.p.ctx, keys)
} else { // @todo: handle filter vs ID based
fmt.Println("=-=-=-=-=-=-=-=-=-=-===-=-=-=-=-=-=-=-=-=-===-=-=-=-=-=-=-=-=-=-==-")
results, err = n.collection.DeleteWithFilter(n.p.ctx, n.filter)
}

fmt.Println("delete node error:", err)
if err != nil {
return false, err
}

// Consume the deletes into our valuesNode
fmt.Println(results)
for _, resKey := range results.DocKeys {
err := n.deleteIter.docs.AddDoc(map[string]interface{}{"_key": resKey})
if err != nil {
Expand Down

0 comments on commit 2ad0ec7

Please sign in to comment.