-
Notifications
You must be signed in to change notification settings - Fork 0
/
quote_test.go
120 lines (114 loc) · 2.66 KB
/
quote_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
package types
import (
"reflect"
"testing"
"time"
"github.com/distributeddesigns/currency"
)
func TestQuote_ToCSV(t *testing.T) {
userID := "jappleseed"
stock := "AAPL"
tenD, _ := currency.NewFromFloat(10.0)
unixTime := time.Unix(123456, 789*1e6)
cryptokey := "abc123="
var id uint64 = 1
type fields struct {
Price currency.Currency
Stock string
UserID string
Timestamp time.Time
Cryptokey string
ID uint64
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Happy path",
fields: fields{tenD, stock, userID, unixTime, cryptokey, id},
want: "10.00,AAPL,jappleseed,123456789,abc123=,1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
q := &Quote{
UserID: tt.fields.UserID,
Stock: tt.fields.Stock,
Price: tt.fields.Price,
Timestamp: tt.fields.Timestamp,
Cryptokey: tt.fields.Cryptokey,
ID: tt.fields.ID,
}
if got := q.ToCSV(); got != tt.want {
t.Errorf("Quote.ToCSV() = %v, want %v", got, tt.want)
}
})
}
}
func TestParseQuote(t *testing.T) {
tenD, _ := currency.NewFromFloat(10.0)
stock := "AAPL"
userID := "jappleseed"
unixTime := time.Unix(123456, 789*1e6)
cryptokey := "abc123="
var id uint64 = 1
type args struct {
csv string
}
tests := []struct {
name string
args args
want Quote
wantErr bool
}{
{
name: "Happy path",
args: args{"10.00,AAPL,jappleseed,123456789,abc123=,1"},
want: Quote{tenD, stock, userID, unixTime, cryptokey, id},
},
{
name: "Strips linebreaks",
args: args{"10.00,AAPL,jappleseed,123456789,abc123=\n,1"},
want: Quote{tenD, stock, userID, unixTime, cryptokey, id},
},
{
name: "Too few args",
args: args{"10.00,AAPL,jappleseed,123456789,abc123="},
wantErr: true,
},
{
name: "Too many args",
args: args{"10.00,AAPL,jappleseed,123456789,abc123=,1,hello!"},
wantErr: true,
},
{
name: "Price stored as string",
args: args{"$10.00,AAPL,jappleseed,123456789,abc123=,1"},
wantErr: true,
},
{
name: "ID is negative",
args: args{"10.00,AAPL,jappleseed,123456789,abc123=,-1"},
wantErr: true,
},
{
name: "Time stored as formatted date",
args: args{"10.00,AAPL,jappleseed,1970-01-02 10:17:36.789 +0000 UTC,abc123=,1"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseQuote(tt.args.csv)
if (err != nil) != tt.wantErr {
t.Errorf("ParseQuote() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParseQuote() = %v, want %v", got, tt.want)
}
})
}
}