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: Skip change detection for tests that assert panic #883

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
12 changes: 1 addition & 11 deletions tests/integration/query/one_to_many/with_cid_dockey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ package one_to_many
import (
"testing"

"github.com/stretchr/testify/assert"

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

Expand Down Expand Up @@ -62,15 +60,7 @@ func TestQueryOneToManyWithUnknownCidAndDocKey(t *testing.T) {
},
}

if testUtils.IsDetectingDbChanges() {
// The `assert.Panics` call will falsely fail if this test is executed during
// a detect changes test run
t.Skip()
}

assert.Panics(t, func() {
executeTestCase(t, test)
})
testUtils.AssertPanicAndSkipChangeDetection(t, func() { executeTestCase(t, test) })
Copy link
Contributor

@AndrewSisley AndrewSisley Oct 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: I was thinking more of adding a bool prop to the QueryTestCase struct and handling the skip within testUtils.executeTestCase - but this works too. Cheers for sorting it out

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that too, but IMO this gives us slightly more control (can assert a function other than executeTestCase if one desires) also wanted to avoid crowding QueryTestCase with another bool.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can revisit this in the future and is an easy change if we do prefer that route.

}

func TestQueryOneToManyWithCidAndDocKey(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ package one_to_many_to_one
import (
"testing"

"github.com/stretchr/testify/assert"

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

Expand Down Expand Up @@ -134,9 +132,5 @@ func TestOneToManyToOneDeepOrderBySubTypeOfBothDescAndAsc(t *testing.T) {
Results: []map[string]any{},
}

assert.Panics(
t,
func() { executeTestCase(t, test) },
"expected a panic, but none found.",
)
testUtils.AssertPanicAndSkipChangeDetection(t, func() { executeTestCase(t, test) })
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ package one_to_many_to_one
import (
"testing"

"github.com/stretchr/testify/assert"

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

Expand Down Expand Up @@ -302,11 +300,7 @@ func TestOneToManyToOneWithSumOfDeepOrderBySubTypeAndDeepOrderBySubtypeAscDirect
},
}

assert.Panics(
t,
func() { executeTestCase(t, test) },
"expected a panic, but none found.",
)
testUtils.AssertPanicAndSkipChangeDetection(t, func() { executeTestCase(t, test) })
}

// TODO: Fix this panic in #833.
Expand Down Expand Up @@ -421,11 +415,7 @@ func TestOneToManyToOneWithSumOfDeepOrderBySubTypeOfBothDescAndAsc(t *testing.T)
Results: []map[string]any{},
}

assert.Panics(
t,
func() { executeTestCase(t, test) },
"expected a panic, but none found.",
)
testUtils.AssertPanicAndSkipChangeDetection(t, func() { executeTestCase(t, test) })
}

// TODO: Fix this panic in #833.
Expand Down Expand Up @@ -542,9 +532,5 @@ func TestOneToManyToOneWithSumOfDeepOrderBySubTypeAndDeepOrderBySubtypeOppositeD
Results: []map[string]any{},
}

assert.Panics(
t,
func() { executeTestCase(t, test) },
"expected a panic, but none found.",
)
testUtils.AssertPanicAndSkipChangeDetection(t, func() { executeTestCase(t, test) })
}
13 changes: 13 additions & 0 deletions tests/integration/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ func IsDetectingDbChanges() bool {
return detectDbChanges
}

// AssertPanicAndSkipChangeDetection asserts that the code of function actually panics,
// also ensures the change detection is skipped so no false fails happen.
//
// Usage: AssertPanicAndSkipChangeDetection(t, func() { executeTestCase(t, test) })
func AssertPanicAndSkipChangeDetection(t *testing.T, f assert.PanicTestFunc) bool {
if IsDetectingDbChanges() {
// The `assert.Panics` call will falsely fail if this test is executed during
// a detect changes test run
t.Skip()
}
return assert.Panics(t, f, "expected a panic, but none found.")
}

func NewBadgerMemoryDB(ctx context.Context, dbopts ...db.Option) (databaseInfo, error) {
opts := badgerds.Options{Options: badger.DefaultOptions("").WithInMemory(true)}
rootstore, err := badgerds.NewDatastore("", &opts)
Expand Down