-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_parsing_test.go
64 lines (58 loc) · 2.37 KB
/
csv_parsing_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
package dataframe
import (
"os"
"io/ioutil"
"testing"
u "github.com/rom1mouret/ml-essentials/utils"
)
func Test1FromCSVFile(t *testing.T) {
f, err := ioutil.TempFile("", "test.*.csv")
if err != nil {
panic(err)
}
name := f.Name()
defer os.Remove(name) // clean up
// write a CSV with header
f.WriteString("col1,col2,col3,col4\n")
f.WriteString("0,0,1,0\n") // 0 0 1 0
f.WriteString("2.0,1,str,3\n") // 2.0 1 str 3
f.WriteString("3.0,1,str,3\n") // 3.0 1 str 3
f.WriteString("-,0,-,-\n") // - 0 - -
f.Close()
// test 1: with '-' as missing value
data, err := FromCSVFile(name, CSVReadingSpec{MissingValues: []string{"-"}, MaxCPU: 2})
if u.AssertTrue("error", err == nil, t) {
if data.CheckConsistency(t) {
u.AssertIntEquals("NumRows", data.NumAllocatedRows(), 4, t)
u.AssertStringSliceEquals("cols", data.FloatHeader().NameList(), []string{"col1"}, false, t)
u.AssertStringSliceEquals("cols", data.BoolHeader().NameList(), []string{"col2"}, false, t)
u.AssertStringSliceEquals("cols", data.ObjectHeader().NameList(), []string{"col3"}, false, t)
u.AssertStringSliceEquals("cols", data.StringHeader().NameList(), []string{"col3"}, false, t)
u.AssertStringSliceEquals("cols", data.IntHeader().NameList(), []string{"col4"}, false, t)
}
}
// test 2: without missing value
data, err = FromCSVFile(name, CSVReadingSpec{MaxCPU: 2})
if u.AssertTrue("error", err == nil, t) {
if data.CheckConsistency(t) {
expected := []string{"col1", "col3", "col4"}
u.AssertIntEquals("NumRows", data.NumAllocatedRows(), 4, t)
u.AssertStringSliceEquals("cols", data.ObjectHeader().NameList(), expected, false, t)
u.AssertStringSliceEquals("cols", data.StringHeader().NameList(), expected, false, t)
}
}
// test 3: without header
opt := CSVReadingSpec{
Header: []string{"c1", "c2", "c3", "c4"},
MissingValues: []string{"-"},
MaxCPU: 2}
data, err = FromCSVFile(name, opt)
if u.AssertTrue("error", err == nil, t) {
expected := []string{"c1", "c2", "c3", "c4"}
if data.CheckConsistency(t) {
u.AssertIntEquals("NumRows", data.NumAllocatedRows(), 5, t)
u.AssertStringSliceEquals("cols", data.ObjectHeader().NameList(), expected, false, t)
u.AssertStringSliceEquals("cols", data.StringHeader().NameList(), expected, false, t)
}
}
}