From f34bbf799dc75b0382376d901cdefcd3dc9e7e64 Mon Sep 17 00:00:00 2001 From: Krzysztof Tomecki <152964795+chris-4chain@users.noreply.github.com> Date: Thu, 14 Nov 2024 12:31:55 +0100 Subject: [PATCH] chore(SPV-1169): ErrorAssert --- engine/transaction/record/record_outline_test.go | 6 ++---- .../record/testabilities/assert_record_outline.go | 13 ++++++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/engine/transaction/record/record_outline_test.go b/engine/transaction/record/record_outline_test.go index 87efdb43..02c842bd 100644 --- a/engine/transaction/record/record_outline_test.go +++ b/engine/transaction/record/record_outline_test.go @@ -277,8 +277,7 @@ func TestRecordOutlineOpReturnErrorCases(t *testing.T) { err := service.RecordTransactionOutline(context.Background(), test.outline) // then: - then.WithErrorIs(err, test.expectErr) - then.NothingChanged() + then.WithErrorIs(err, test.expectErr).NothingChanged() }) } } @@ -312,8 +311,7 @@ func TestOnBroadcastErr(t *testing.T) { err := service.RecordTransactionOutline(context.Background(), outline) // then: - then.WithErrorIs(err, txerrors.ErrTxBroadcast) - then.NothingChanged() + then.WithErrorIs(err, txerrors.ErrTxBroadcast).NothingChanged() } func ptr[T any](value T) *T { diff --git a/engine/transaction/record/testabilities/assert_record_outline.go b/engine/transaction/record/testabilities/assert_record_outline.go index 67ac575a..8775d6e7 100644 --- a/engine/transaction/record/testabilities/assert_record_outline.go +++ b/engine/transaction/record/testabilities/assert_record_outline.go @@ -7,13 +7,16 @@ import ( "github.com/stretchr/testify/require" ) +type ErrorAssert interface { + NothingChanged() +} + type RecordOutlineAssert interface { WithNoError(err error) SuccessfullyCreatedRecordOutlineAssertion - WithErrorIs(err, expectedError error) + WithErrorIs(err, expectedError error) ErrorAssert StoredOutputs([]database.Output) RecordOutlineAssert StoredData([]database.Data) RecordOutlineAssert - NothingChanged() RecordOutlineAssert } type SuccessfullyCreatedRecordOutlineAssertion interface { @@ -40,9 +43,10 @@ func (a *assert) WithNoError(err error) SuccessfullyCreatedRecordOutlineAssertio return a } -func (a *assert) WithErrorIs(err, expectedError error) { +func (a *assert) WithErrorIs(err, expectedError error) ErrorAssert { require.Error(a.t, err, "Record transaction outline has no error") require.ErrorIs(a.t, err, expectedError, "Record transaction outline has wrong error") + return a } func (a *assert) Broadcasted(txID string) SuccessfullyCreatedRecordOutlineAssertion { @@ -69,8 +73,7 @@ func (a *assert) StoredData(data []database.Data) RecordOutlineAssert { return a } -func (a *assert) NothingChanged() RecordOutlineAssert { +func (a *assert) NothingChanged() { require.ElementsMatch(a.t, a.given.initialOutputs, a.given.repository.GetAllOutputs(), "Outputs are changed") require.ElementsMatch(a.t, a.given.initialData, a.given.repository.GetAllData(), "Data are changed") - return a }