From 88d0b2799af87cc51b02735440dd19e25064aa0b Mon Sep 17 00:00:00 2001 From: Kevin Barbour Date: Thu, 21 Nov 2024 16:38:18 -0500 Subject: [PATCH] Replace DeleteEntry with DeleteEntries --- batch.go | 24 +++++--------------- batch_test.go | 62 ++++++++++++++++++++++++++++++++++----------------- batcher.go | 4 ++-- 3 files changed, 50 insertions(+), 40 deletions(-) diff --git a/batch.go b/batch.go index 4cd904aa2..5dfdfb7c3 100644 --- a/batch.go +++ b/batch.go @@ -529,15 +529,9 @@ func (batch *Batch) AddEntry(entry *EntryDetail) { batch.Entries = append(batch.Entries, entry) } -// DeleteEntry deletes an EntryDetail from the Batch -func (batch *Batch) DeleteEntry(entry *EntryDetail) { - if entry == nil { - return - } - - batch.Entries = slices.DeleteFunc(batch.Entries, func(e *EntryDetail) bool { - return e == entry - }) +// DeleteEntries deletes all Entries from the Batch where del() == true +func (batch *Batch) DeleteEntries(del func(e *EntryDetail) bool) { + batch.Entries = slices.DeleteFunc(batch.Entries, del) } // AddADVEntry appends an ADV EntryDetail to the Batch @@ -546,15 +540,9 @@ func (batch *Batch) AddADVEntry(entry *ADVEntryDetail) { batch.ADVEntries = append(batch.ADVEntries, entry) } -// DeleteADVEntry deletes an ADV EntryDetail from the Batch -func (batch *Batch) DeleteADVEntry(entry *ADVEntryDetail) { - if entry == nil { - return - } - - batch.ADVEntries = slices.DeleteFunc(batch.ADVEntries, func(e *ADVEntryDetail) bool { - return e == entry - }) +// DeleteADVEntries deletes all ADV Entries from the Batch where del() == true +func (batch *Batch) DeleteADVEntries(del func(e *ADVEntryDetail) bool) { + batch.ADVEntries = slices.DeleteFunc(batch.ADVEntries, del) } // GetADVEntries returns a slice of entry details for the batch diff --git a/batch_test.go b/batch_test.go index d5f4b2c2e..f350a7bc6 100644 --- a/batch_test.go +++ b/batch_test.go @@ -1698,54 +1698,76 @@ func TestBatch_calculateEntryHash(t *testing.T) { require.Equal(t, 22140797, hash) } -func TestBatch_DeleteEntry(t *testing.T) { +func TestBatch_DeleteEntries(t *testing.T) { b := &Batch{} b.SetHeader(mockBatchHeader()) ed1 := mockEntryDetail() ed1.TraceNumber = "1" - b.AddEntry(ed1) - + ed1.Amount = 20 ed2 := mockEntryDetail() ed2.TraceNumber = "2" - b.AddEntry(ed2) - + ed2.Amount = 60 ed3 := mockEntryDetail() ed3.TraceNumber = "3" + ed3.Amount = 100 + + b.AddEntry(ed1) + b.AddEntry(ed2) b.AddEntry(ed3) require.Equal(t, 3, len(b.Entries)) - b.DeleteEntry(ed2) + b.DeleteEntries(func(e *EntryDetail) bool { + return e.TraceNumber == "2" + }) + require.Equal(t, 2, len(b.Entries)) require.Equal(t, "1", b.Entries[0].TraceNumber) require.Equal(t, "3", b.Entries[1].TraceNumber) - b.DeleteEntry(ed2) - require.Equal(t, 2, len(b.Entries)) + + b.AddEntry(ed2) + + b.DeleteEntries(func(e *EntryDetail) bool { + return e.Amount >= 50 + }) + require.Equal(t, 1, len(b.Entries)) + require.Equal(t, "1", b.Entries[0].TraceNumber) } -func TestBatch_DeleteADVEntry(t *testing.T) { +func TestBatch_DeleteADVEntries(t *testing.T) { b := &Batch{} - b.SetHeader(mockBatchADVHeader()) + b.SetHeader(mockBatchHeader()) - ed1 := NewADVEntryDetail() + ed1 := mockADVEntryDetail() ed1.ID = "1" - b.AddADVEntry(ed1) - - ed2 := NewADVEntryDetail() + ed1.Amount = 20 + ed2 := mockADVEntryDetail() ed2.ID = "2" - b.AddADVEntry(ed2) - - ed3 := NewADVEntryDetail() + ed2.Amount = 60 + ed3 := mockADVEntryDetail() ed3.ID = "3" + ed3.Amount = 100 + + b.AddADVEntry(ed1) + b.AddADVEntry(ed2) b.AddADVEntry(ed3) require.Equal(t, 3, len(b.ADVEntries)) - b.DeleteADVEntry(ed2) + b.DeleteADVEntries(func(e *ADVEntryDetail) bool { + return e.ID == "2" + }) + require.Equal(t, 2, len(b.ADVEntries)) require.Equal(t, "1", b.ADVEntries[0].ID) require.Equal(t, "3", b.ADVEntries[1].ID) - b.DeleteADVEntry(ed2) - require.Equal(t, 2, len(b.ADVEntries)) + + b.AddADVEntry(ed2) + + b.DeleteADVEntries(func(e *ADVEntryDetail) bool { + return e.Amount >= 50 + }) + require.Equal(t, 1, len(b.ADVEntries)) + require.Equal(t, "1", b.ADVEntries[0].ID) } diff --git a/batcher.go b/batcher.go index dbc29649e..542ee31dc 100644 --- a/batcher.go +++ b/batcher.go @@ -38,10 +38,10 @@ type Batcher interface { SetADVControl(*ADVBatchControl) GetEntries() []*EntryDetail AddEntry(*EntryDetail) - DeleteEntry(*EntryDetail) + DeleteEntries(func(*EntryDetail) bool) GetADVEntries() []*ADVEntryDetail AddADVEntry(*ADVEntryDetail) - DeleteADVEntry(*ADVEntryDetail) + DeleteADVEntries(func(*ADVEntryDetail) bool) Create() error Validate() error SetID(string)