-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtests.go
127 lines (111 loc) · 3.81 KB
/
tests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package tests
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/code-payments/code-server/pkg/code/data/paymentrequest"
"github.com/code-payments/code-server/pkg/kin"
"github.com/code-payments/code-server/pkg/pointer"
)
func RunTests(t *testing.T, s paymentrequest.Store, teardown func()) {
for _, tf := range []func(t *testing.T, s paymentrequest.Store){
testRoundTrip,
testInvalidRecord,
} {
tf(t, s)
teardown()
}
}
func testRoundTrip(t *testing.T, s paymentrequest.Store) {
t.Run("testRoundTrip", func(t *testing.T) {
for i, fees := range [][]*paymentrequest.Fee{
nil,
{
{
DestinationTokenAccount: "destination2",
BasisPoints: 50,
},
{
DestinationTokenAccount: "destination3",
BasisPoints: 1234,
},
},
} {
ctx := context.Background()
intentId := fmt.Sprintf("test_intent%d", i)
actual, err := s.Get(ctx, intentId)
require.Error(t, err)
assert.Equal(t, paymentrequest.ErrPaymentRequestNotFound, err)
assert.Nil(t, actual)
expected := &paymentrequest.Record{
Intent: intentId,
DestinationTokenAccount: pointer.String("destination1"),
ExchangeCurrency: pointer.String("usd"),
NativeAmount: pointer.Float64(2.46),
ExchangeRate: pointer.Float64(1.23),
Quantity: pointer.Uint64(kin.ToQuarks(2)),
Fees: fees,
Domain: pointer.String("example.com"),
IsVerified: true,
CreatedAt: time.Now(),
}
cloned := expected.Clone()
err = s.Put(ctx, expected)
require.NoError(t, err)
assert.Equal(t, paymentrequest.ErrPaymentRequestAlreadyExists, s.Put(ctx, expected))
require.NoError(t, err)
actual, err = s.Get(ctx, intentId)
require.NoError(t, err)
assertEquivalentRecords(t, &cloned, actual)
assert.True(t, actual.Id > 0)
}
})
}
func testInvalidRecord(t *testing.T, s paymentrequest.Store) {
t.Run("testInvalidRecord", func(t *testing.T) {
ctx := context.Background()
expected := &paymentrequest.Record{
Intent: "test_intent",
DestinationTokenAccount: pointer.String("destination1"),
ExchangeCurrency: pointer.String("usd"),
NativeAmount: pointer.Float64(2.46),
ExchangeRate: pointer.Float64(1.23),
Quantity: pointer.Uint64(kin.ToQuarks(2)),
Fees: []*paymentrequest.Fee{
{
DestinationTokenAccount: "destination2",
BasisPoints: 1,
},
{
DestinationTokenAccount: "destination2",
BasisPoints: 2,
},
},
Domain: pointer.String("example.com"),
IsVerified: true,
CreatedAt: time.Now(),
}
assert.Equal(t, paymentrequest.ErrInvalidPaymentRequest, s.Put(ctx, expected))
_, err := s.Get(ctx, "test_intent")
assert.Equal(t, paymentrequest.ErrPaymentRequestNotFound, err)
})
}
func assertEquivalentRecords(t *testing.T, obj1, obj2 *paymentrequest.Record) {
assert.Equal(t, obj1.Intent, obj2.Intent)
assert.EqualValues(t, obj1.DestinationTokenAccount, obj2.DestinationTokenAccount)
assert.EqualValues(t, obj1.ExchangeCurrency, obj2.ExchangeCurrency)
assert.EqualValues(t, obj1.NativeAmount, obj2.NativeAmount)
assert.EqualValues(t, obj1.ExchangeRate, obj2.ExchangeRate)
assert.EqualValues(t, obj1.Quantity, obj2.Quantity)
assert.EqualValues(t, obj1.Domain, obj2.Domain)
assert.Equal(t, obj1.IsVerified, obj2.IsVerified)
assert.Equal(t, obj1.CreatedAt.Unix(), obj2.CreatedAt.Unix())
require.Equal(t, len(obj1.Fees), len(obj2.Fees))
for i := 0; i < len(obj1.Fees); i++ {
assert.Equal(t, obj1.Fees[i].DestinationTokenAccount, obj2.Fees[i].DestinationTokenAccount)
assert.Equal(t, obj1.Fees[i].BasisPoints, obj2.Fees[i].BasisPoints)
}
}