Skip to content

Commit

Permalink
fix: Revert transactions (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Gelloz authored Mar 29, 2023
1 parent cc9177b commit e088da1
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 45 deletions.
13 changes: 5 additions & 8 deletions pkg/core/posting.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ 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
}
for i := len(ps)/2 - 1; i >= 0; i-- {
opp := len(ps) - 1 - i
ps[i], ps[opp] = ps[opp], ps[i]
for i := range ps {
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(ps)/2; i++ {
ps[i], ps[len(ps)-i-1] = ps[len(ps)-i-1], ps[i]
}
}

Expand Down
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 e088da1

Please sign in to comment.