Skip to content

Commit

Permalink
Add event tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Dec 13, 2022
1 parent 18bab4a commit bb1e82b
Show file tree
Hide file tree
Showing 6 changed files with 480 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tests/integration/events/simple/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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 simple

import (
"testing"

testUtils "github.com/sourcenetwork/defradb/tests/integration/events"
)

var schema = `
type users {
Name: String
}
`

func executeTestCase(t *testing.T, test testUtils.TestCase) {
testUtils.ExecuteQueryTestCase(t, schema, test)
}
66 changes: 66 additions & 0 deletions tests/integration/events/simple/with_create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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 simple

import (
"context"
"testing"

"github.com/sourcenetwork/immutable"
"github.com/stretchr/testify/assert"

"github.com/sourcenetwork/defradb/client"
testUtils "github.com/sourcenetwork/defradb/tests/integration/events"
)

func TestEventsSimpleWithCreate(t *testing.T) {
doc1, err := client.NewDocFromJSON(
[]byte(
`{
"Name": "John"
}`,
),
)
assert.Nil(t, err)
doc2, err := client.NewDocFromJSON(
[]byte(
`{
"Name": "Shahzad"
}`,
),
)
assert.Nil(t, err)

test := testUtils.TestCase{
CollectionCalls: map[string][]func(client.Collection){
"users": []func(c client.Collection){
func(c client.Collection) {
err = c.Save(context.Background(), doc1)
assert.Nil(t, err)
},
func(c client.Collection) {
err = c.Save(context.Background(), doc2)
assert.Nil(t, err)
},
},
},
ExpectedUpdates: []testUtils.ExpectedUpdate{
{
DocKey: immutable.Some("bae-43deba43-f2bc-59f4-9056-fef661b22832"),
},
{
DocKey: immutable.Some("bae-359dfe55-77a4-59de-a687-fc5049dc61fe"),
},
},
}

executeTestCase(t, test)
}
67 changes: 67 additions & 0 deletions tests/integration/events/simple/with_create_txn_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 simple

import (
"context"
"testing"

"github.com/sourcenetwork/immutable"
"github.com/stretchr/testify/assert"

"github.com/sourcenetwork/defradb/client"
testUtils "github.com/sourcenetwork/defradb/tests/integration/events"
)

func TestEventsSimpleWithCreateWithTxnDiscarded(t *testing.T) {
test := testUtils.TestCase{
DatabaseCalls: []func(context.Context, client.DB){
func(ctx context.Context, d client.DB) {
r := d.ExecQuery(
ctx,
`mutation {
create_users(data: "{\"Name\": \"John\"}") {
_key
}
}`,
)
for _, err := range r.GQL.Errors {
assert.Nil(t, err)
}
},
func(ctx context.Context, d client.DB) {
txn, err := d.NewTxn(ctx, false)
assert.Nil(t, err)
r := d.ExecTransactionalQuery(
ctx,
`mutation {
create_users(data: "{\"Name\": \"Shahzad\"}") {
_key
}
}`,
txn,
)
for _, err := range r.GQL.Errors {
assert.Nil(t, err)
}
txn.Discard(ctx)
},
},
ExpectedUpdates: []testUtils.ExpectedUpdate{
{
DocKey: immutable.Some("bae-43deba43-f2bc-59f4-9056-fef661b22832"),
},
// No event should be recieved for Shahzad, as the transaction was discarded.
},
}

executeTestCase(t, test)
}
59 changes: 59 additions & 0 deletions tests/integration/events/simple/with_delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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 simple

import (
"context"
"testing"

"github.com/sourcenetwork/immutable"
"github.com/stretchr/testify/assert"

"github.com/sourcenetwork/defradb/client"
testUtils "github.com/sourcenetwork/defradb/tests/integration/events"
)

// This test documents undesirable behaviour which should be corrected in
// https://github.com/sourcenetwork/defradb/issues/867
func TestEventsSimpleWithDelete(t *testing.T) {
doc1, err := client.NewDocFromJSON(
[]byte(
`{
"Name": "John"
}`,
),
)
assert.Nil(t, err)

test := testUtils.TestCase{
CollectionCalls: map[string][]func(client.Collection){
"users": []func(c client.Collection){
func(c client.Collection) {
err = c.Save(context.Background(), doc1)
assert.Nil(t, err)
},
func(c client.Collection) {
wasDeleted, err := c.Delete(context.Background(), doc1.Key())
assert.Nil(t, err)
assert.True(t, wasDeleted)
},
},
},
ExpectedUpdates: []testUtils.ExpectedUpdate{
{
DocKey: immutable.Some("bae-43deba43-f2bc-59f4-9056-fef661b22832"),
},
// No update to reflect the delete
},
}

executeTestCase(t, test)
}
77 changes: 77 additions & 0 deletions tests/integration/events/simple/with_update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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 simple

import (
"context"
"testing"

"github.com/sourcenetwork/immutable"
"github.com/stretchr/testify/assert"

"github.com/sourcenetwork/defradb/client"
testUtils "github.com/sourcenetwork/defradb/tests/integration/events"
)

func TestEventsSimpleWithUpdate(t *testing.T) {
doc1, err := client.NewDocFromJSON(
[]byte(
`{
"Name": "John"
}`,
),
)
assert.Nil(t, err)
doc2, err := client.NewDocFromJSON(
[]byte(
`{
"Name": "Shahzad"
}`,
),
)
assert.Nil(t, err)

test := testUtils.TestCase{
CollectionCalls: map[string][]func(client.Collection){
"users": []func(c client.Collection){
func(c client.Collection) {
err = c.Save(context.Background(), doc1)
assert.Nil(t, err)
},
func(c client.Collection) {
err = c.Save(context.Background(), doc2)
assert.Nil(t, err)
},
func(c client.Collection) {
// Update John
doc1.Set("Name", "Johnnnnn")
err = c.Save(context.Background(), doc2)
assert.Nil(t, err)
},
},
},
ExpectedUpdates: []testUtils.ExpectedUpdate{
{
DocKey: immutable.Some("bae-43deba43-f2bc-59f4-9056-fef661b22832"),
Cid: immutable.Some("bafybeihm4gewwhzsyqs7tuazdsbusp6pm3oinhnj2ae6funcwshwpxuxri"),
},
{
DocKey: immutable.Some("bae-359dfe55-77a4-59de-a687-fc5049dc61fe"),
},
{
DocKey: immutable.Some("bae-359dfe55-77a4-59de-a687-fc5049dc61fe"),
Cid: immutable.Some("bafybeie5j7x6nvy3n3m4qi3ydudvho5box37vvdmpy7vc46ownkxxjyxky"),
},
},
}

executeTestCase(t, test)
}
Loading

0 comments on commit bb1e82b

Please sign in to comment.