Skip to content

Commit

Permalink
test: Add test for deletion of records in a relationship (#329)
Browse files Browse the repository at this point in the history
resolves #221

These are non-transactional tests.

Should have some transactional tests too (@todo #328).
  • Loading branch information
shahzadlone authored Apr 18, 2022
1 parent 41dbd5a commit deb2ffd
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 7 deletions.
20 changes: 20 additions & 0 deletions db/tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Testing Guide

- We want to keep the mutation and query tests separate, here is what the folder
structure looks like currently:
```
db/tests
├── mutation/
└── query/
```

- Every immediate directory under `db/tests/mutation` and `db/tests/query` should ONLY contain
a single schema. For example:
`db/tests/query/simple` and `db/tests/query/complex` have different schemas.

- We can group different types of tests using the same schema into further sub-folders.
For example:
- `db/tests/mutation/simple/create`: contains tests that
use the `simple` schema to test only the create mutation.
- `db/tests/mutation/simple/mix`: contains test that use
the `simple` schema to test combination of mutations.
169 changes: 169 additions & 0 deletions db/tests/mutation/relation/delete/single_id_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// 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 relation_delete

import (
"testing"

testUtils "github.com/sourcenetwork/defradb/db/tests"
relationTests "github.com/sourcenetwork/defradb/db/tests/mutation/relation"
)

func TestRelationalDeletionOfADocumentUsingSingleKey_Success(t *testing.T) {
tests := []testUtils.QueryTestCase{

{
Description: "Relational delete mutation where one element exists.",
Query: `mutation {
delete_author(id: "bae-2f80f359-535d-508e-ba58-088a309ce3c3") {
_key
}
}`,
Docs: map[int][]string{
// Books
0: {
// bae-80eded16-ee4b-5c9d-b33f-6a7b83958af2
(`{
"name": "100 Go Mistakes to Avoid.",
"rating": 4.8,
"publisher_id": "bae-176ebdf0-77e7-5b2f-91ae-f620e37a29e3"
}`)},
// Authors
1: {
// bae-2f80f359-535d-508e-ba58-088a309ce3c3
(`{
"name": "Teiva Harsanyi",
"age": 48,
"verified": true,
"wrote_id": "bae-80eded16-ee4b-5c9d-b33f-6a7b83958af2"
}`)},
// Publishers
2: {
// bae-176ebdf0-77e7-5b2f-91ae-f620e37a29e3
(`{
"name": "Manning Early Access Program (MEAP)",
"address": "Online"
}`)},
},
Results: []map[string]interface{}{
{
"_key": "bae-2f80f359-535d-508e-ba58-088a309ce3c3",
},
},
ExpectedError: "",
},

{
Description: "Relational delete mutation with an aliased _key name.",
Query: `mutation {
delete_author(id: "bae-2f80f359-535d-508e-ba58-088a309ce3c3") {
AliasOfKey: _key
}
}`,
Docs: map[int][]string{
// Books
0: {
// bae-80eded16-ee4b-5c9d-b33f-6a7b83958af2
(`{
"name": "100 Go Mistakes to Avoid.",
"rating": 4.8,
"publisher_id": "bae-176ebdf0-77e7-5b2f-91ae-f620e37a29e3"
}`)},
// Authors
1: {
// bae-2f80f359-535d-508e-ba58-088a309ce3c3
(`{
"name": "Teiva Harsanyi",
"age": 48,
"verified": true,
"wrote_id": "bae-80eded16-ee4b-5c9d-b33f-6a7b83958af2"
}`)},
// Publishers
2: {
// bae-176ebdf0-77e7-5b2f-91ae-f620e37a29e3
(`{
"name": "Manning Early Access Program (MEAP)",
"address": "Online"
}`)},
},
Results: []map[string]interface{}{
{
"AliasOfKey": "bae-2f80f359-535d-508e-ba58-088a309ce3c3",
},
},
ExpectedError: "",
},

{
Description: "Relational Delete of an updated document and an aliased _key name.",
Query: `mutation {
delete_author(id: "bae-2f80f359-535d-508e-ba58-088a309ce3c3") {
Key: _key
}
}`,

Docs: map[int][]string{
// Books
0: {
// bae-80eded16-ee4b-5c9d-b33f-6a7b83958af2
(`{
"name": "100 Go Mistakes to Avoid.",
"rating": 4.8,
"publisher_id": "bae-176ebdf0-77e7-5b2f-91ae-f620e37a29e3"
}`),
},

// Authors
1: {
// bae-2f80f359-535d-508e-ba58-088a309ce3c3
(`{
"name": "Teiva Harsanyi",
"age": 48,
"verified": true,
"wrote_id": "bae-80eded16-ee4b-5c9d-b33f-6a7b83958af2"
}`),
},

// Publishers
2: {
// bae-176ebdf0-77e7-5b2f-91ae-f620e37a29e3
(`{
"name": "Manning Early Access Program (MEAP)",
"address": "Online"
}`),

// bae-5c599633-d6d2-56ae-b3f0-1b65b4cee9fe
(`{
"name": "Manning Publications",
"address": "Website"
}`),
},
},
Updates: map[int][]string{
0: {
(`{
"name": "Rust in Action.",
"publisher_id": "bae-5c599633-d6d2-56ae-b3f0-1b65b4cee9fe"
}`)},
},
Results: []map[string]interface{}{
{
"Key": "bae-2f80f359-535d-508e-ba58-088a309ce3c3",
},
},
ExpectedError: "",
},
}

for _, test := range tests {
relationTests.ExecuteTestCase(t, test)
}
}
48 changes: 48 additions & 0 deletions db/tests/mutation/relation/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 relation

import (
"testing"

testUtils "github.com/sourcenetwork/defradb/db/tests"
)

var bookAuthorPublisherGQLSchema = (`
type book {
name: String
rating: Float
author: author
publisher: publisher
}
type author {
name: String
age: Int
verified: Boolean
wrote: book @primary
}
type publisher {
name: String
address: String
published: book
}
`)

func ExecuteTestCase(t *testing.T, test testUtils.QueryTestCase) {
testUtils.ExecuteQueryTestCase(
t,
bookAuthorPublisherGQLSchema,
[]string{"book", "author", "publisher"},
test,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package complex
package mix

import (
"testing"
Expand All @@ -17,7 +17,7 @@ import (
simpleTests "github.com/sourcenetwork/defradb/db/tests/mutation/simple"
)

func TestMutationComplexWithTxnDeletesUserGivenSameTransaction(t *testing.T) {
func TestMutationWithTxnDeletesUserGivenSameTransaction(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Create followed by delete in same transaction",
TransactionalQueries: []testUtils.TransactionQuery{
Expand Down Expand Up @@ -55,7 +55,7 @@ func TestMutationComplexWithTxnDeletesUserGivenSameTransaction(t *testing.T) {
simpleTests.ExecuteTestCase(t, test)
}

func TestMutationComplexWithTxnDoesNotDeletesUserGivenDifferentTransactions(t *testing.T) {
func TestMutationWithTxnDoesNotDeletesUserGivenDifferentTransactions(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Create followed by delete on 2nd transaction",
TransactionalQueries: []testUtils.TransactionQuery{
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestMutationComplexWithTxnDoesNotDeletesUserGivenDifferentTransactions(t *t
simpleTests.ExecuteTestCase(t, test)
}

func TestMutationComplexWithTxnDoesUpdateUserGivenSameTransactions(t *testing.T) {
func TestMutationWithTxnDoesUpdateUserGivenSameTransactions(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Update followed by read in same transaction",
Docs: map[int][]string{
Expand Down Expand Up @@ -166,7 +166,7 @@ func TestMutationComplexWithTxnDoesUpdateUserGivenSameTransactions(t *testing.T)
simpleTests.ExecuteTestCase(t, test)
}

func TestMutationComplexWithTxnDoesNotUpdateUserGivenDifferentTransactions(t *testing.T) {
func TestMutationWithTxnDoesNotUpdateUserGivenDifferentTransactions(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Update followed by read in different transaction",
Docs: map[int][]string{
Expand Down Expand Up @@ -219,7 +219,7 @@ func TestMutationComplexWithTxnDoesNotUpdateUserGivenDifferentTransactions(t *te
simpleTests.ExecuteTestCase(t, test)
}

func TestMutationComplexWithTxnDoesNotAllowUpdateInSecondTransactionUser(t *testing.T) {
func TestMutationWithTxnDoesNotAllowUpdateInSecondTransactionUser(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Update by two different transactions",
Docs: map[int][]string{
Expand Down
2 changes: 1 addition & 1 deletion db/tests/mutation/simple/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package simple_test
package simple

import (
"testing"
Expand Down

0 comments on commit deb2ffd

Please sign in to comment.