From 067ff0498777f8d3cbba664315a11c07743b38d0 Mon Sep 17 00:00:00 2001 From: Shahzad Lone Date: Fri, 7 Oct 2022 14:03:22 -0400 Subject: [PATCH] PR: Test - Clean up panic assertions and skipping from change detection. --- .../query/one_to_many/with_cid_dockey_test.go | 12 +---------- .../with_order_limit_test.go | 8 +------- .../with_sum_order_limit_test.go | 20 +++---------------- tests/integration/utils.go | 13 ++++++++++++ 4 files changed, 18 insertions(+), 35 deletions(-) diff --git a/tests/integration/query/one_to_many/with_cid_dockey_test.go b/tests/integration/query/one_to_many/with_cid_dockey_test.go index e877699571..9f3f462c37 100644 --- a/tests/integration/query/one_to_many/with_cid_dockey_test.go +++ b/tests/integration/query/one_to_many/with_cid_dockey_test.go @@ -13,8 +13,6 @@ package one_to_many import ( "testing" - "github.com/stretchr/testify/assert" - testUtils "github.com/sourcenetwork/defradb/tests/integration" ) @@ -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) }) } func TestQueryOneToManyWithCidAndDocKey(t *testing.T) { diff --git a/tests/integration/query/one_to_many_to_one/with_order_limit_test.go b/tests/integration/query/one_to_many_to_one/with_order_limit_test.go index b2d714f194..ddfef528ff 100644 --- a/tests/integration/query/one_to_many_to_one/with_order_limit_test.go +++ b/tests/integration/query/one_to_many_to_one/with_order_limit_test.go @@ -13,8 +13,6 @@ package one_to_many_to_one import ( "testing" - "github.com/stretchr/testify/assert" - testUtils "github.com/sourcenetwork/defradb/tests/integration" ) @@ -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) }) } diff --git a/tests/integration/query/one_to_many_to_one/with_sum_order_limit_test.go b/tests/integration/query/one_to_many_to_one/with_sum_order_limit_test.go index 42346d42f7..7f64a46a10 100644 --- a/tests/integration/query/one_to_many_to_one/with_sum_order_limit_test.go +++ b/tests/integration/query/one_to_many_to_one/with_sum_order_limit_test.go @@ -13,8 +13,6 @@ package one_to_many_to_one import ( "testing" - "github.com/stretchr/testify/assert" - testUtils "github.com/sourcenetwork/defradb/tests/integration" ) @@ -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. @@ -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. @@ -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) }) } diff --git a/tests/integration/utils.go b/tests/integration/utils.go index 590d57eea0..51b47ef107 100644 --- a/tests/integration/utils.go +++ b/tests/integration/utils.go @@ -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)