-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcsv_test.go
More file actions
72 lines (54 loc) · 1.26 KB
/
csv_test.go
File metadata and controls
72 lines (54 loc) · 1.26 KB
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
package origins
import (
"bytes"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCSVReader(t *testing.T) {
// Comments and spaces to test parser, mixed case in header, underscores and spaces.
var csvString = `
entity_domain,entity,ATTRIBUTE DOMAIN,attribute,value domain,value,operation,valid time
test,bob,test,knows,test,alice
test,bob,test,knows,test,joe,,2015-03-06
test,alice,test,likes,test,bob
# comment
test,alice,test,likes,test,bob,retract
`
buf := bytes.NewBufferString(csvString)
r := NewCSVReader(buf)
facts, err := ReadAll(r)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, len(facts), 4)
bk := facts[1]
assert.Equal(t, Assertion, bk.Operation)
assert.Equal(t, bk.Entity.Domain, "test")
assert.Equal(t, bk.Entity.Name, "bob")
assert.Equal(t, time.Date(2015, 3, 6, 0, 0, 0, 0, time.UTC), bk.Time)
assert.Equal(t, Retraction, facts[3].Operation)
}
// Benchmark parsing a single record.
func BenchmarkCSVParse(b *testing.B) {
header, _ := parseHeader(csvHeader)
reader := CSVReader{
header: header,
}
record := []string{
"people",
"assert",
"5",
"2014-02-01",
"people",
"bob",
"",
"knows",
"people",
"jane",
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
reader.parse(record)
}
}