-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter_test.go
51 lines (46 loc) · 1.29 KB
/
writer_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
package csv
import (
"bytes"
"testing"
testifyrequire "github.com/stretchr/testify/require"
)
type testWriterStruct struct {
Email string `csv:"email"`
Age int `csv:"age"`
Owed float64 `csv:"owed"`
ShouldBill bool `cst:"should_bill"`
}
type testWriterOmitEmptyStruct struct {
Email string `csv:"email,omitempty"`
Age int `csv:"age,omitempty"`
Owed float64 `csv:"owed,omitempty"`
ShouldBill bool `cst:"should_bill,omitempty"`
}
func TestNewWriter(t *testing.T) {
t.Run("Basic writer", func(t *testing.T) {
require := testifyrequire.New(t)
buf := bytes.Buffer{}
writer := NewWriter[testWriterStruct](&buf)
err := writer.WriteRecord(testWriterStruct{
Email: "test@example.com",
Age: 32,
Owed: 6512.23,
ShouldBill: true,
})
require.NoError(err)
require.Equal("test@example.com,32,6512.23,TRUE\n", buf.String())
})
t.Run("Omit Empty", func(t *testing.T) {
require := testifyrequire.New(t)
buf := bytes.Buffer{}
writer := NewWriter[testWriterStruct](&buf)
err := writer.WriteRecord(testWriterStruct{
Email: "test@example.com",
Age: 32,
Owed: 6512.23,
ShouldBill: true,
})
require.NoError(err)
require.Equal("test@example.com,32,6512.23,TRUE\n", buf.String())
})
}