Skip to content

Commit

Permalink
Add DeleteEntry and DeleteADVEntry to Batcher
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinbarbour committed Nov 21, 2024
1 parent 1b5f7ab commit eb5140b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
23 changes: 23 additions & 0 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"errors"
"fmt"
"slices"
"strconv"
"strings"
"unicode/utf8"
Expand Down Expand Up @@ -528,12 +529,34 @@ 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
})
}

// AddADVEntry appends an ADV EntryDetail to the Batch
func (batch *Batch) AddADVEntry(entry *ADVEntryDetail) {
batch.category = entry.Category
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
})
}

// GetADVEntries returns a slice of entry details for the batch
func (batch *Batch) GetADVEntries() []*ADVEntryDetail {
return batch.ADVEntries
Expand Down
52 changes: 52 additions & 0 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1697,3 +1697,55 @@ func TestBatch_calculateEntryHash(t *testing.T) {
hash := b.calculateEntryHash()
require.Equal(t, 22140797, hash)
}

func TestBatch_DeleteEntry(t *testing.T) {
b := &Batch{}
b.SetHeader(mockBatchHeader())

ed1 := mockEntryDetail()
ed1.TraceNumber = "1"
b.AddEntry(ed1)

ed2 := mockEntryDetail()
ed2.TraceNumber = "2"
b.AddEntry(ed2)

ed3 := mockEntryDetail()
ed3.TraceNumber = "3"
b.AddEntry(ed3)

require.Equal(t, 3, len(b.Entries))

b.DeleteEntry(ed2)
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))
}

func TestBatch_DeleteADVEntry(t *testing.T) {
b := &Batch{}
b.SetHeader(mockBatchADVHeader())

ed1 := NewADVEntryDetail()
ed1.ID = "1"
b.AddADVEntry(ed1)

ed2 := NewADVEntryDetail()
ed2.ID = "2"
b.AddADVEntry(ed2)

ed3 := NewADVEntryDetail()
ed3.ID = "3"
b.AddADVEntry(ed3)

require.Equal(t, 3, len(b.ADVEntries))

b.DeleteADVEntry(ed2)
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))
}
2 changes: 2 additions & 0 deletions batcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ type Batcher interface {
SetADVControl(*ADVBatchControl)
GetEntries() []*EntryDetail
AddEntry(*EntryDetail)
DeleteEntry(*EntryDetail)
GetADVEntries() []*ADVEntryDetail
AddADVEntry(*ADVEntryDetail)
DeleteADVEntry(*ADVEntryDetail)
Create() error
Validate() error
SetID(string)
Expand Down

0 comments on commit eb5140b

Please sign in to comment.