Skip to content

Commit

Permalink
fixup! Currincies: Remove Format from FindDifferences
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Aug 12, 2024
1 parent e3422cf commit 5c1e2a4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 80 deletions.
115 changes: 36 additions & 79 deletions currency/pair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,85 +565,42 @@ func TestCopyPairFormat(t *testing.T) {
}

func TestFindPairDifferences(t *testing.T) {
pairList, err := NewPairsFromStrings([]string{defaultPairWDelimiter, "ETH-USD", "LTC-USD"})
if err != nil {
t.Fatal(err)
}

dash, err := NewPairsFromStrings([]string{"DASH-USD"})
if err != nil {
t.Fatal(err)
}

// Test new pair update
diff, err := pairList.FindDifferences(dash, PairFormat{Delimiter: DashDelimiter, Uppercase: true})
if err != nil {
t.Fatal(err)
}
if len(diff.New) != 1 && len(diff.Remove) != 3 {
t.Error("TestFindPairDifferences: Unexpected values")
}

diff, err = pairList.FindDifferences(Pairs{}, EMPTYFORMAT)
if err != nil {
t.Fatal(err)
}
if len(diff.New) != 0 && len(diff.Remove) != 3 {
t.Error("TestFindPairDifferences: Unexpected values")
}

diff, err = Pairs{}.FindDifferences(pairList, EMPTYFORMAT)
if err != nil {
t.Fatal(err)
}
if len(diff.New) != 3 && len(diff.Remove) != 0 {
t.Error("TestFindPairDifferences: Unexpected values")
}

// Test that the supplied pair lists are the same, so no newPairs or removedPairs
diff, err = pairList.FindDifferences(pairList, PairFormat{Delimiter: DashDelimiter, Uppercase: true})
if err != nil {
t.Fatal(err)
}
if len(diff.New) != 0 && len(diff.Remove) != 0 {
t.Error("TestFindPairDifferences: Unexpected values")
}

_, err = pairList.FindDifferences(Pairs{EMPTYPAIR}, EMPTYFORMAT)
if !errors.Is(err, ErrCurrencyPairEmpty) {
t.Fatalf("received: '%v' but expected: '%v'", err, ErrCurrencyPairEmpty)
}

_, err = Pairs{EMPTYPAIR}.FindDifferences(pairList, EMPTYFORMAT)
if !errors.Is(err, ErrCurrencyPairEmpty) {
t.Fatalf("received: '%v' but expected: '%v'", err, ErrCurrencyPairEmpty)
}

// Test duplication
duplication, err := NewPairsFromStrings([]string{defaultPairWDelimiter, "ETH-USD", "LTC-USD", "ETH-USD"})
if err != nil {
t.Fatal(err)
}

_, err = pairList.FindDifferences(duplication, EMPTYFORMAT)
if !errors.Is(err, ErrDuplicatePairs) {
t.Fatalf("received: '%v' but expected: '%v'", err, ErrDuplicatePairs)
}

// This will allow for the removal of the duplicated item to be returned if
// contained in the original list.
diff, err = duplication.FindDifferences(pairList, EMPTYFORMAT)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}

if len(diff.Remove) != 1 {
t.Fatal("expected removal value in pair difference struct")
}

if !diff.Remove[0].Equal(pairList[1]) {
t.Fatal("unexpected value returned", diff.Remove[0], pairList[1])
}
p := Pairs{NewPair(BTC, USD), NewPair(ETH, USD), NewPair(LTC, USD)}

diff, err := p.FindDifferences(Pairs{NewPair(DASH, USD)})
require.NoError(t, err)
assert.Len(t, diff.New, 1)
assert.Len(t, diff.Remove, 3)

diff, err = p.FindDifferences(Pairs{})
require.NoError(t, err)
assert.Empty(t, diff.New)
assert.Len(t, diff.Remove, 3)

diff, err = Pairs{}.FindDifferences(p)
require.NoError(t, err)
assert.Len(t, diff.New, 3)
assert.Empty(t, diff.Remove)

diff, err = p.FindDifferences(p)
require.NoError(t, err)
assert.Empty(t, diff.New)
assert.Empty(t, diff.Remove)

_, err = p.FindDifferences(Pairs{EMPTYPAIR})
assert.ErrorIs(t, err, ErrCurrencyPairEmpty)

_, err = Pairs{EMPTYPAIR}.FindDifferences(p)
assert.ErrorIs(t, err, ErrCurrencyPairEmpty)

p2 := append(p, p[0])
_, err = p.FindDifferences(p2)
assert.ErrorIs(t, err, ErrDuplicatePairs)

diff, err = p2.FindDifferences(p)
require.NoError(t, err, "Duplicates must be allowed in the invocant list")
require.Len(t, diff.Remove, 1)
require.True(t, p[0].Equal(diff.Remove[0]))
}

func TestPairsToStringArray(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion currency/pairs.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (p Pairs) GetMatch(pair Pair) (Pair, error) {
}

// FindDifferences returns pairs which are new or have been removed
func (p Pairs) FindDifferences(incoming Pairs, pairFmt PairFormat) (PairDifference, error) {
func (p Pairs) FindDifferences(incoming Pairs) (PairDifference, error) {
newPairs := make(Pairs, 0, len(incoming))
check := make(map[string]bool)
for x := range incoming {
Expand Down

0 comments on commit 5c1e2a4

Please sign in to comment.