-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_test.go
93 lines (73 loc) · 1.76 KB
/
module_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
package broker_test
import (
"fmt"
"github.com/guneyin/gobist-broker"
"github.com/guneyin/gobist-broker/entity"
"os"
"reflect"
"testing"
)
func TestBrokers(t *testing.T) {
brokers := broker.GetBrokers()
assertNotNil(t, brokers)
fmt.Println("Supported Brokers:")
i := 0
for _, v := range brokers {
i++
fmt.Printf(" %d- %s\n", i, v.Info().TitleLong)
}
fmt.Println()
fmt.Printf("%s\n", brokers.ToJSON())
}
func TestBroker(t *testing.T) {
b := broker.GetBroker(entity.Garanti)
assertNotNil(t, b)
fmt.Println("Broker Info:")
fmt.Printf("%s\n", b.Info().ToJSON())
}
func TestImporter(t *testing.T) {
b := broker.GetBroker(entity.Garanti)
assertNotNil(t, b)
ts, err := importFile(b, "single")
assertError(t, err)
assertNotNil(t, ts)
ts, err = importFile(b, "duplicated")
assertError(t, err)
assertNotNil(t, ts)
ts, err = importFile(b, "full")
assertError(t, err)
assertNotNil(t, ts)
fmt.Println("Imported Transactions:")
for _, item := range ts.Items {
fmt.Printf(" %-10s %-35s %-5d %-10.2f %-15s\n", item.Symbol, item.Date, item.Quantity, item.Price, item.Type.String())
}
}
func importFile(b broker.Broker, t string) (*entity.Transactions, error) {
fPath := fmt.Sprintf("testdata/%s/%s.csv", b.Info().Name, t)
fileContent, err := os.ReadFile(fPath)
if err != nil {
return nil, err
}
return b.Parse(fileContent)
}
func assertError(t *testing.T, err error) {
if err != nil {
t.Errorf("Error occurred [%v]", err)
}
}
func assertNotNil(t *testing.T, v interface{}) {
if isNil(v) {
t.Errorf("[%v] was expected to be non-nil", v)
}
}
func isNil(v interface{}) bool {
if v == nil {
return true
}
rv := reflect.ValueOf(v)
kind := rv.Kind()
if kind >= reflect.Chan && kind <= reflect.Slice && rv.IsNil() {
return true
}
return false
}