Skip to content

Commit

Permalink
Replace DeleteEntry with DeleteEntries
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinbarbour committed Nov 21, 2024
1 parent eb5140b commit 88d0b27
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 40 deletions.
24 changes: 6 additions & 18 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
62 changes: 42 additions & 20 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions batcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 88d0b27

Please sign in to comment.