-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathprice_test.go
122 lines (107 loc) · 2.65 KB
/
price_test.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
package optimus
import (
"math/big"
"testing"
"github.com/sonm-io/core/proto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRelativePriceThresholdExceeds(t *testing.T) {
v, err := NewRelativePriceThreshold(1.0)
require.NoError(t, err)
require.NotNil(t, v)
assert.True(t, v.Exceeds(big.NewInt(1010), big.NewInt(1000)))
assert.False(t, v.Exceeds(big.NewInt(1009), big.NewInt(1000)))
}
func TestParseRelativePriceThreshold(t *testing.T) {
v, err := ParseRelativePriceThreshold("2.0%")
require.NoError(t, err)
require.NotNil(t, v)
assert.True(t, v.Exceeds(big.NewInt(1020), big.NewInt(1000)))
assert.False(t, v.Exceeds(big.NewInt(1019), big.NewInt(1000)))
}
func TestErrParseRelativePriceThreshold(t *testing.T) {
tests := []struct {
name string
threshold string
}{
{
name: "missing percent",
threshold: "1.5",
},
{
name: "trailing percent",
threshold: "1.5%1",
},
{
name: "not number",
threshold: "1.5c%",
},
{
name: "zero number",
threshold: "0.0%",
},
{
name: "negative number",
threshold: "-1.5%",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
v, err := ParseRelativePriceThreshold(test.threshold)
require.Error(t, err)
require.Nil(t, v)
})
}
}
func TestAbsolutePriceThresholdExceeds(t *testing.T) {
p := &sonm.Price{}
err := p.LoadFromString("0.02 USD/h")
require.NoError(t, err)
v, err := NewAbsolutePriceThreshold(p)
require.NoError(t, err)
require.NotNil(t, v)
assert.True(t, v.Exceeds(big.NewInt(105555555555556), big.NewInt(100000000000000)))
assert.False(t, v.Exceeds(big.NewInt(105555555555554), big.NewInt(100000000000000)))
}
func TestParseAbsolutePriceThreshold(t *testing.T) {
v, err := ParseAbsolutePriceThreshold("0.02 USD/h")
require.NoError(t, err)
require.NotNil(t, v)
assert.True(t, v.Exceeds(big.NewInt(105555555555556), big.NewInt(100000000000000)))
assert.False(t, v.Exceeds(big.NewInt(105555555555554), big.NewInt(100000000000000)))
}
func TestErrParseAbsolutePriceThreshold(t *testing.T) {
tests := []struct {
name string
threshold string
}{
{
name: "missing dimension",
threshold: "1.5",
},
{
name: "invalid dimension",
threshold: "1.5 USD/day",
},
{
name: "not number",
threshold: "1.5c USD/h",
},
{
name: "zero number",
threshold: "0.0 USD/h",
},
{
name: "negative number",
threshold: "-1.5 USD/h",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
v, err := ParseAbsolutePriceThreshold(test.threshold)
require.Error(t, err)
require.Nil(t, v)
})
}
}