Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHORE] Update comparison match logic for opposite amounts #76

Merged
merged 5 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ coverage.html
examples/server/server
examples/client/client
examples/fetcher/fetcher

.idea/
39 changes: 28 additions & 11 deletions parser/match_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ func operationMatch(
// equalAmounts returns an error if a slice of operations do not have
// equal amounts.
func equalAmounts(ops []*types.Operation) error {
if len(ops) <= 1 {
return fmt.Errorf("cannot check equality of %d operations", len(ops))
if len(ops) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would've expected this test to trigger a test change...can we write a simple one for testing equal amounts with 1 op? @qiwu7

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually there already are test cases for equalAmounts with only 1 op, that's why the test was failing previously.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool. I can help review that when it's ready :)

return errors.New("cannot check equality of 0 operations")
}

val, err := types.AmountValue(ops[0].Amount)
Expand Down Expand Up @@ -437,15 +437,32 @@ func comparisonMatch(
if err := matchIndexValid(matches, amountMatch[1]); err != nil {
return fmt.Errorf("%w: opposite amounts comparison error", err)
}
for _, op := range matches[amountMatch[0]].Operations {
for _, otherOp := range matches[amountMatch[1]].Operations {
if err := oppositeAmounts(
op,
otherOp,
); err != nil {
return fmt.Errorf("%w: amounts not opposites", err)
}
}

match0Ops := matches[amountMatch[0]].Operations
match1Ops := matches[amountMatch[1]].Operations
if err := equalAmounts(match0Ops); err != nil {
return fmt.Errorf(
"%w: opposite amounts comparison error for match index %d",
err,
amountMatch[0],
)
}
if err := equalAmounts(match1Ops); err != nil {
return fmt.Errorf(
"%w: opposite amounts comparison error for match index %d",
err,
amountMatch[1],
)
}

// only need to check opposites amount for the very first operation from each
// matched operations group since we made sure all amounts within the same
// matched operation group are the same
if err := oppositeAmounts(
match0Ops[0],
match1Ops[0],
); err != nil {
return fmt.Errorf("%w: amounts not opposites", err)
}
}

qiwu7 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
2 changes: 1 addition & 1 deletion reconciler/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestNewReconciler(t *testing.T) {
},
}
r.seenAccounts = map[string]struct{}{
types.Hash(accountCurrency): struct{}{},
types.Hash(accountCurrency): {},
}

return r
Expand Down