Skip to content

Commit

Permalink
fix: Reverse transaction (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Gelloz authored Mar 29, 2023
1 parent 223af06 commit 5b78af1
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 117 deletions.
15 changes: 6 additions & 9 deletions pkg/core/posting.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@ type Posting struct {

type Postings []Posting

func (ps Postings) Reverse() {
if len(ps) == 1 {
ps[0].Source, ps[0].Destination = ps[0].Destination, ps[0].Source
return
func (p Postings) Reverse() {
for i := range p {
p[i].Source, p[i].Destination = p[i].Destination, p[i].Source
}
for i := len(ps)/2 - 1; i >= 0; i-- {
opp := len(ps) - 1 - i
ps[i], ps[opp] = ps[opp], ps[i]
ps[i].Source, ps[i].Destination = ps[i].Destination, ps[i].Source
ps[opp].Source, ps[opp].Destination = ps[opp].Destination, ps[opp].Source

for i := 0; i < len(p)/2; i++ {
p[i], p[len(p)-i-1] = p[len(p)-i-1], p[i]
}
}

Expand Down
71 changes: 0 additions & 71 deletions pkg/core/posting_test.go

This file was deleted.

159 changes: 122 additions & 37 deletions pkg/core/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,136 @@ package core
import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
)

func TestReverseTransaction(t *testing.T) {
tx := &ExpandedTransaction{
Transaction: Transaction{
TransactionData: TransactionData{
Postings: Postings{
{
Source: "world",
Destination: "users:001",
Amount: NewMonetaryInt(100),
Asset: "COIN",
t.Run("1 posting", func(t *testing.T) {
tx := &ExpandedTransaction{
Transaction: Transaction{
TransactionData: TransactionData{
Postings: Postings{
{
Source: "world",
Destination: "users:001",
Amount: NewMonetaryInt(100),
Asset: "COIN",
},
},
{
Source: "users:001",
Destination: "payments:001",
Amount: NewMonetaryInt(100),
Asset: "COIN",
Reference: "foo",
},
},
}

expected := TransactionData{
Postings: Postings{
{
Source: "users:001",
Destination: "world",
Amount: NewMonetaryInt(100),
Asset: "COIN",
},
},
Reference: "revert_foo",
}
require.Equal(t, expected, tx.Reverse())
})

t.Run("2 postings", func(t *testing.T) {
tx := &ExpandedTransaction{
Transaction: Transaction{
TransactionData: TransactionData{
Postings: Postings{
{
Source: "world",
Destination: "users:001",
Amount: NewMonetaryInt(100),
Asset: "COIN",
},
{
Source: "users:001",
Destination: "payments:001",
Amount: NewMonetaryInt(100),
Asset: "COIN",
},
},
Reference: "foo",
},
Reference: "foo",
},
},
}

expected := TransactionData{
Postings: Postings{
{
Source: "payments:001",
Destination: "users:001",
Amount: NewMonetaryInt(100),
Asset: "COIN",
}

expected := TransactionData{
Postings: Postings{
{
Source: "payments:001",
Destination: "users:001",
Amount: NewMonetaryInt(100),
Asset: "COIN",
},
{
Source: "users:001",
Destination: "world",
Amount: NewMonetaryInt(100),
Asset: "COIN",
},
},
{
Source: "users:001",
Destination: "world",
Amount: NewMonetaryInt(100),
Asset: "COIN",
Reference: "revert_foo",
}
require.Equal(t, expected, tx.Reverse())
})

t.Run("3 postings", func(t *testing.T) {
tx := &ExpandedTransaction{
Transaction: Transaction{
TransactionData: TransactionData{
Postings: Postings{
{
Source: "world",
Destination: "users:001",
Amount: NewMonetaryInt(100),
Asset: "COIN",
},
{
Source: "users:001",
Destination: "payments:001",
Amount: NewMonetaryInt(100),
Asset: "COIN",
},
{
Source: "payments:001",
Destination: "alice",
Amount: NewMonetaryInt(100),
Asset: "COIN",
},
},
Reference: "foo",
},
},
},
Reference: "revert_foo",
}
}

if diff := cmp.Diff(expected, tx.Reverse()); diff != "" {
t.Errorf("Reverse() mismatch (-want +got):\n%s", diff)
}
expected := TransactionData{
Postings: Postings{
{
Source: "alice",
Destination: "payments:001",
Amount: NewMonetaryInt(100),
Asset: "COIN",
},
{
Source: "payments:001",
Destination: "users:001",
Amount: NewMonetaryInt(100),
Asset: "COIN",
},
{
Source: "users:001",
Destination: "world",
Amount: NewMonetaryInt(100),
Asset: "COIN",
},
},
Reference: "revert_foo",
}
require.Equal(t, expected, tx.Reverse())
})
}

0 comments on commit 5b78af1

Please sign in to comment.