Skip to content

Commit

Permalink
feat: add Delete method for a ReceiptStore model and a test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
kweeuhree committed Jan 9, 2025
1 parent 2497168 commit 801ae5e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/models/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,14 @@ func (s *ReceiptStore) Get(id string) (Receipt, error) {

return receipt, nil
}

func (s *ReceiptStore) Delete(id string) error {
_, exists := s.receipts[id]
if !exists {
return fmt.Errorf("no receipt found for that ID")
}

delete(s.receipts, id)

return nil
}
33 changes: 33 additions & 0 deletions internal/models/receipt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,36 @@ func TestGetInvalidID(t *testing.T) {
t.Errorf("Expected an error message '%s', but got '%s'", expectedError, err.Error())
}
}

func TestDelete(t *testing.T) {
d := setupTestDependencies()
tests := []struct {
name string
id string
}{
{"Valid id", "123-qwe-456-rty-7890"},
{"Invalid id", "123"},
{"Empty id", ""},
}

for _, entry := range tests {
t.Run(entry.name, func(t *testing.T) {
// Insert a receipt
d.receiptStore.Insert(*SimpleReceipt)

// Attempt to delete a receipt using entry id
d.receiptStore.Delete(entry.id)

// Attempt to retrieve a receipt using entry id
inserted, _ := d.receiptStore.Get(entry.id)
if inserted.ID == SimpleReceipt.ID {
t.Errorf("Expected receipt with id %s to be deleted, but it was not.", SimpleReceipt.ID)
}

t.Cleanup(func() {
d.receiptStore = NewStore()
})
})
}

}

0 comments on commit 801ae5e

Please sign in to comment.