Skip to content

Commit

Permalink
Merge pull request #1284 from adamdecaf/remove-panic
Browse files Browse the repository at this point in the history
build: remove usage of panic
  • Loading branch information
adamdecaf committed Aug 21, 2023
2 parents 21d6334 + 1a4e5ae commit 95c51d5
Show file tree
Hide file tree
Showing 32 changed files with 376 additions and 459 deletions.
2 changes: 1 addition & 1 deletion addenda99_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func TestAddenda99__SetValidation(t *testing.T) {
}

func TestAddenda99__CustomReturnCode(t *testing.T) {
mockBatch := mockBatch()
mockBatch := mockBatch(t)
// Add a Addenda Return to the mock batch
if len(mockBatch.Entries) != 1 {
t.Fatal("Expected 1 batch entry")
Expand Down
6 changes: 3 additions & 3 deletions advFileControl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func testParseADVFileControl(t testing.TB) {
var line = "90000010000010000000100053200010000000000000001050000000000000000000000 "
r := NewReader(strings.NewReader(line))
r.line = line
batchADV := mockBatchADV()
batchADV := mockBatchADV(t)
r.File.AddBatch(batchADV)

err := r.parseFileControl()
Expand Down Expand Up @@ -119,7 +119,7 @@ func testADVFCString(t testing.TB) {
var line = "90000010000010000000100053200010000000000000001050000000000000000000000 "
r := NewReader(strings.NewReader(line))
r.line = line
batchADV := mockBatchADV()
batchADV := mockBatchADV(t)
r.File.AddBatch(batchADV)

err := r.parseFileControl()
Expand Down Expand Up @@ -244,7 +244,7 @@ func TestInvalidADVFCParse(t *testing.T) {
var line = "9000001000001000000010005320001000000000000000105"
r := NewReader(strings.NewReader(line))
r.line = line
batchADV := mockBatchADV()
batchADV := mockBatchADV(t)
r.File.AddBatch(batchADV)

err := r.parseFileControl()
Expand Down
15 changes: 7 additions & 8 deletions batchADV_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/moov-io/base"
"github.com/stretchr/testify/require"
)

// mockBatchADVHeader creates a ADV batch header
Expand All @@ -39,12 +40,10 @@ func mockBatchADVHeader() *BatchHeader {
}

// mockBatchADV creates a ADV batch
func mockBatchADV() *BatchADV {
func mockBatchADV(t testing.TB) *BatchADV {
mockBatch := NewBatchADV(mockBatchADVHeader())
mockBatch.AddADVEntry(mockADVEntryDetail())
if err := mockBatch.Create(); err != nil {
panic(err)
}
require.NoError(t, mockBatch.Create())
return mockBatch
}

Expand Down Expand Up @@ -86,7 +85,7 @@ func TestBatchADVAddendum99(t *testing.T) {

// testBatchADVSEC validates that the standard entry class code is ADV for batchADV
func testBatchADVSEC(t testing.TB) {
mockBatch := mockBatchADV()
mockBatch := mockBatchADV(t)
mockBatch.Header.StandardEntryClassCode = RCK
err := mockBatch.Validate()
if !base.Match(err, ErrBatchSECType) {
Expand All @@ -109,7 +108,7 @@ func BenchmarkBatchADVSEC(b *testing.B) {

// testBatchADVServiceClassCode validates ServiceClassCode
func testBatchADVServiceClassCode(t testing.TB) {
mockBatch := mockBatchADV()
mockBatch := mockBatchADV(t)
// Batch Header information is required to Create a batch.
mockBatch.GetHeader().ServiceClassCode = 220
err := mockBatch.Create()
Expand Down Expand Up @@ -146,7 +145,7 @@ func TestBatchADVAddendum99Category(t *testing.T) {

// TestBatchADVInvalidTransactionCode validates TransactionCode
func TestBatchADVInvalidTransactionCode(t *testing.T) {
mockBatch := mockBatchADV()
mockBatch := mockBatchADV(t)
// Batch Header information is required to Create a batch.
mockBatch.GetADVEntries()[0].TransactionCode = CheckingCredit
err := mockBatch.Create()
Expand All @@ -173,7 +172,7 @@ func TestADVMaximumEntries(t *testing.T) {

// TestBatchADVOriginatorStatusCode validates the originator status code
func TestBatchADVOriginatorStatusCode(t *testing.T) {
mockBatch := mockBatchADV()
mockBatch := mockBatchADV(t)
mockBatch.Header.OriginatorStatusCode = 1
err := mockBatch.Create()
if !base.Match(err, ErrOrigStatusCode) {
Expand Down
29 changes: 14 additions & 15 deletions batchARC_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/moov-io/base"
"github.com/stretchr/testify/require"
)

// mockBatchARCHeader creates a BatchARC BatchHeader
Expand Down Expand Up @@ -50,12 +51,10 @@ func mockARCEntryDetail() *EntryDetail {
}

// mockBatchARC creates a BatchARC
func mockBatchARC() *BatchARC {
func mockBatchARC(t testing.TB) *BatchARC {
mockBatch := NewBatchARC(mockBatchARCHeader())
mockBatch.AddEntry(mockARCEntryDetail())
if err := mockBatch.Create(); err != nil {
panic(err)
}
require.NoError(t, mockBatch.Create())
return mockBatch
}

Expand Down Expand Up @@ -116,7 +115,7 @@ func BenchmarkBatchARCHeader(b *testing.B) {

// testBatchARCCreate validates BatchARC create
func testBatchARCCreate(t testing.TB) {
mockBatch := mockBatchARC()
mockBatch := mockBatchARC(t)
if err := mockBatch.Create(); err != nil {
t.Errorf("%T: %s", err, err)
}
Expand All @@ -137,7 +136,7 @@ func BenchmarkBatchARCCreate(b *testing.B) {

// testBatchARCStandardEntryClassCode validates BatchARC create for an invalid StandardEntryClassCode
func testBatchARCStandardEntryClassCode(t testing.TB) {
mockBatch := mockBatchARC()
mockBatch := mockBatchARC(t)
mockBatch.Header.StandardEntryClassCode = WEB
err := mockBatch.Create()
if !base.Match(err, ErrBatchSECType) {
Expand All @@ -160,7 +159,7 @@ func BenchmarkBatchARCStandardEntryClassCode(b *testing.B) {

// testBatchARCServiceClassCodeEquality validates service class code equality
func testBatchARCServiceClassCodeEquality(t testing.TB) {
mockBatch := mockBatchARC()
mockBatch := mockBatchARC(t)
mockBatch.GetControl().ServiceClassCode = MixedDebitsAndCredits
err := mockBatch.Validate()
if !base.Match(err, NewErrBatchHeaderControlEquality(220, MixedDebitsAndCredits)) {
Expand All @@ -183,7 +182,7 @@ func BenchmarkBatchARCServiceClassCodeEquality(b *testing.B) {

// testBatchARCMixedCreditsAndDebits validates BatchARC create for an invalid MixedCreditsAndDebits
func testBatchARCMixedCreditsAndDebits(t testing.TB) {
mockBatch := mockBatchARC()
mockBatch := mockBatchARC(t)
mockBatch.Header.ServiceClassCode = MixedDebitsAndCredits
err := mockBatch.Validate()
if !base.Match(err, NewErrBatchHeaderControlEquality(MixedDebitsAndCredits, 225)) {
Expand All @@ -206,7 +205,7 @@ func BenchmarkBatchARCMixedCreditsAndDebits(b *testing.B) {

// testBatchARCCreditsOnly validates BatchARC create for an invalid CreditsOnly
func testBatchARCCreditsOnly(t testing.TB) {
mockBatch := mockBatchARC()
mockBatch := mockBatchARC(t)
mockBatch.Header.ServiceClassCode = CreditsOnly
err := mockBatch.Validate()
if !base.Match(err, NewErrBatchHeaderControlEquality(CreditsOnly, 225)) {
Expand All @@ -229,7 +228,7 @@ func BenchmarkBatchARCCreditsOnly(b *testing.B) {

// testBatchARCAutomatedAccountingAdvices validates BatchARC create for an invalid AutomatedAccountingAdvices
func testBatchARCAutomatedAccountingAdvices(t testing.TB) {
mockBatch := mockBatchARC()
mockBatch := mockBatchARC(t)
mockBatch.Header.ServiceClassCode = AutomatedAccountingAdvices
err := mockBatch.Validate()
if !base.Match(err, NewErrBatchHeaderControlEquality(AutomatedAccountingAdvices, 225)) {
Expand All @@ -252,7 +251,7 @@ func BenchmarkBatchARCAutomatedAccountingAdvices(b *testing.B) {

// testBatchARCAmount validates BatchARC create for an invalid Amount
func testBatchARCAmount(t testing.TB) {
mockBatch := mockBatchARC()
mockBatch := mockBatchARC(t)
mockBatch.Entries[0].Amount = 2600000
err := mockBatch.Create()
if !base.Match(err, NewErrBatchAmount(2600000, 2500000)) {
Expand All @@ -275,7 +274,7 @@ func BenchmarkBatchARCAmount(b *testing.B) {

// testBatchARCCheckSerialNumber validates BatchARC CheckSerialNumber / IdentificationNumber is a mandatory field
func testBatchARCCheckSerialNumber(t testing.TB) {
mockBatch := mockBatchARC()
mockBatch := mockBatchARC(t)
// modify CheckSerialNumber / IdentificationNumber to nothing
mockBatch.GetEntries()[0].SetCheckSerialNumber("")
err := mockBatch.Validate()
Expand Down Expand Up @@ -323,7 +322,7 @@ func BenchmarkBatchARCTransactionCode(b *testing.B) {

// testBatchARCAddendaCount validates BatchARC Addenda count
func testBatchARCAddendaCount(t testing.TB) {
mockBatch := mockBatchARC()
mockBatch := mockBatchARC(t)
mockBatch.Entries[0].AddendaRecordIndicator = 1
mockBatch.GetEntries()[0].AddAddenda05(mockAddenda05())
err := mockBatch.Create()
Expand All @@ -347,7 +346,7 @@ func BenchmarkBatchARCAddendaCount(b *testing.B) {

// testBatchARCInvalidBuild validates an invalid batch build
func testBatchARCInvalidBuild(t testing.TB) {
mockBatch := mockBatchARC()
mockBatch := mockBatchARC(t)
mockBatch.GetHeader().ServiceClassCode = 3
err := mockBatch.Create()
if !base.Match(err, ErrServiceClass) {
Expand Down Expand Up @@ -411,7 +410,7 @@ func TestBatchARCAddendum99Category(t *testing.T) {

// testBatchARCMixedCreditsAndDebits validates BatchARC create for valid MixedCreditsAndDebits
func testBatchARCMixedCreditsAndDebitsBatchControlMixedDebitsAndCredits(t testing.TB) {
mockBatch := mockBatchARC()
mockBatch := mockBatchARC(t)
mockBatch.Header.ServiceClassCode = MixedDebitsAndCredits
mockBatch.Batch.Control.ServiceClassCode = MixedDebitsAndCredits
err := mockBatch.Validate()
Expand Down
29 changes: 14 additions & 15 deletions batchBOC_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/moov-io/base"
"github.com/stretchr/testify/require"
)

// mockBatchBOCHeader creates a BatchBOC BatchHeader
Expand Down Expand Up @@ -50,12 +51,10 @@ func mockBOCEntryDetail() *EntryDetail {
}

// mockBatchBOC creates a BatchBOC
func mockBatchBOC() *BatchBOC {
func mockBatchBOC(t testing.TB) *BatchBOC {
mockBatch := NewBatchBOC(mockBatchBOCHeader())
mockBatch.AddEntry(mockBOCEntryDetail())
if err := mockBatch.Create(); err != nil {
panic(err)
}
require.NoError(t, mockBatch.Create())
return mockBatch
}

Expand Down Expand Up @@ -116,7 +115,7 @@ func BenchmarkBatchBOCHeader(b *testing.B) {

// testBatchBOCCreate validates BatchBOC create
func testBatchBOCCreate(t testing.TB) {
mockBatch := mockBatchBOC()
mockBatch := mockBatchBOC(t)
if err := mockBatch.Create(); err != nil {
t.Errorf("%T: %s", err, err)
}
Expand All @@ -137,7 +136,7 @@ func BenchmarkBatchBOCCreate(b *testing.B) {

// testBatchBOCStandardEntryClassCode validates BatchBOC create for an invalid StandardEntryClassCode
func testBatchBOCStandardEntryClassCode(t testing.TB) {
mockBatch := mockBatchBOC()
mockBatch := mockBatchBOC(t)
mockBatch.Header.StandardEntryClassCode = WEB
err := mockBatch.Create()
if !base.Match(err, ErrBatchSECType) {
Expand All @@ -160,7 +159,7 @@ func BenchmarkBatchBOCStandardEntryClassCode(b *testing.B) {

// testBatchBOCServiceClassCodeEquality validates service class code equality
func testBatchBOCServiceClassCodeEquality(t testing.TB) {
mockBatch := mockBatchBOC()
mockBatch := mockBatchBOC(t)
mockBatch.GetControl().ServiceClassCode = MixedDebitsAndCredits
err := mockBatch.Validate()
if !base.Match(err, NewErrBatchHeaderControlEquality(220, MixedDebitsAndCredits)) {
Expand All @@ -183,7 +182,7 @@ func BenchmarkBatchBOCServiceClassCodeEquality(b *testing.B) {

// testBatchBOCMixedCreditsAndDebits validates BatchBOC create for an invalid MixedCreditsAndDebits
func testBatchBOCMixedCreditsAndDebits(t testing.TB) {
mockBatch := mockBatchBOC()
mockBatch := mockBatchBOC(t)
mockBatch.Header.ServiceClassCode = MixedDebitsAndCredits
err := mockBatch.Validate()
if !base.Match(err, NewErrBatchHeaderControlEquality(MixedDebitsAndCredits, 225)) {
Expand All @@ -206,7 +205,7 @@ func BenchmarkBatchBOCMixedCreditsAndDebits(b *testing.B) {

// testBatchBOCCreditsOnly validates BatchBOC create for an invalid CreditsOnly
func testBatchBOCCreditsOnly(t testing.TB) {
mockBatch := mockBatchBOC()
mockBatch := mockBatchBOC(t)
mockBatch.Header.ServiceClassCode = CreditsOnly

err := mockBatch.Validate()
Expand All @@ -230,7 +229,7 @@ func BenchmarkBatchBOCCreditsOnly(b *testing.B) {

// testBatchBOCAutomatedAccountingAdvices validates BatchBOC create for an invalid AutomatedAccountingAdvices
func testBatchBOCAutomatedAccountingAdvices(t testing.TB) {
mockBatch := mockBatchBOC()
mockBatch := mockBatchBOC(t)
mockBatch.Header.ServiceClassCode = AutomatedAccountingAdvices
err := mockBatch.Validate()
if !base.Match(err, NewErrBatchHeaderControlEquality(AutomatedAccountingAdvices, 225)) {
Expand All @@ -253,7 +252,7 @@ func BenchmarkBatchBOCAutomatedAccountingAdvices(b *testing.B) {

// testBatchBOCAmount validates BatchBOC create for an invalid Amount
func testBatchBOCAmount(t testing.TB) {
mockBatch := mockBatchBOC()
mockBatch := mockBatchBOC(t)
mockBatch.Entries[0].Amount = 2500001
err := mockBatch.Create()
if !base.Match(err, NewErrBatchAmount(2500001, 2500000)) {
Expand All @@ -276,7 +275,7 @@ func BenchmarkBatchBOCAmount(b *testing.B) {

// testBatchBOCCheckSerialNumber validates BatchBOC CheckSerialNumber / IdentificationNumber is a mandatory field
func testBatchBOCCheckSerialNumber(t testing.TB) {
mockBatch := mockBatchBOC()
mockBatch := mockBatchBOC(t)
// modify CheckSerialNumber / IdentificationNumber to empty string
mockBatch.GetEntries()[0].SetCheckSerialNumber("")
err := mockBatch.Validate()
Expand Down Expand Up @@ -323,7 +322,7 @@ func BenchmarkBatchBOCTransactionCode(b *testing.B) {

// testBatchBOCAddenda05 validates BatchBOC Addenda count
func testBatchBOCAddenda05(t testing.TB) {
mockBatch := mockBatchBOC()
mockBatch := mockBatchBOC(t)
mockBatch.Entries[0].AddendaRecordIndicator = 1
mockBatch.GetEntries()[0].AddAddenda05(mockAddenda05())
err := mockBatch.Create()
Expand All @@ -347,7 +346,7 @@ func BenchmarkBatchBOCAddenda05(b *testing.B) {

// testBatchBOCInvalidBuild validates an invalid batch build
func testBatchBOCInvalidBuild(t testing.TB) {
mockBatch := mockBatchBOC()
mockBatch := mockBatchBOC(t)
mockBatch.GetHeader().ServiceClassCode = 3
err := mockBatch.Create()
if !base.Match(err, ErrServiceClass) {
Expand Down Expand Up @@ -398,7 +397,7 @@ func TestBatchBOCAddendum99(t *testing.T) {

// testBatchBOCCreditsOnly validates BatchBOC create for Valid SCC MixedDebitsAndCredits with transCode Debit
func testBatchBOCMixedDebitsAndCreditsWithCreditTransCode(t testing.TB) {
mockBatch := mockBatchBOC()
mockBatch := mockBatchBOC(t)
mockBatch.Header.ServiceClassCode = MixedDebitsAndCredits
mockBatch.Batch.Control.ServiceClassCode = MixedDebitsAndCredits
err := mockBatch.Validate()
Expand Down
Loading

0 comments on commit 95c51d5

Please sign in to comment.