-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_test.go
More file actions
109 lines (79 loc) · 2.42 KB
/
Copy pathcsv_test.go
File metadata and controls
109 lines (79 loc) · 2.42 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
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
// Copyright 2026 The Nanoninja Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package render
import (
"bytes"
"context"
"testing"
"github.com/nanoninja/assert"
)
var (
_ Renderer = (*csvRenderer)(nil)
_ Renderer = CSV()
)
func TestCSVRenderer(t *testing.T) {
t.Run("RendersSimpleStringArrayData", func(t *testing.T) {
var w bytes.Buffer
data := [][]string{
{"name", "age"},
{"Alice", "25"},
{"Bob", "30"},
}
err := CSV().Render(context.Background(), &w, data, NoOptions)
assert.NoError(t, err)
assert.Equal(t, "name,age\nAlice,25\nBob,30\n", w.String())
})
t.Run("HandlesCustomDelimiter", func(t *testing.T) {
var w bytes.Buffer
data := [][]string{
{"name", "age"},
{"Alice", "25"},
}
err := CSV().Render(context.Background(), &w, data, Options{Separator: ';'})
assert.NoError(t, err)
assert.Equal(t, "name;age\nAlice;25\n", w.String())
})
t.Run("HandlesEmptyData", func(t *testing.T) {
var w bytes.Buffer
err := CSV().Render(context.Background(), &w, [][]string{}, NoOptions)
assert.NoError(t, err)
assert.Equal(t, "", w.String())
})
t.Run("ReturnsErrorForInvalidDataType", func(t *testing.T) {
var w bytes.Buffer
data := "invalid data"
err := CSV().Render(context.Background(), &w, data, NoOptions)
assert.ErrorIs(t, err, ErrInvalidData)
})
t.Run("SetsCorrectContentType", func(t *testing.T) {
assert.Equal(t, "text/csv; charset=utf-8", CSV().ContentType())
})
t.Run("RespectsCRLFFormatOption", func(t *testing.T) {
var w bytes.Buffer
data := [][]string{
{"name", "age"},
{"Alice", "25"},
}
err := CSV().Render(context.Background(), &w, data, Options{LineEnding: "\r\n"})
assert.NoError(t, err)
assert.Equal(t, "name,age\r\nAlice,25\r\n", w.String())
})
t.Run("RespectsContextCancellation", func(t *testing.T) {
var w bytes.Buffer
data := [][]string{{"test"}}
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := CSV().Render(ctx, &w, data, NoOptions)
assert.ErrorIs(t, err, context.Canceled)
})
t.Run("ReturnsErrorOnWriteFailure", func(t *testing.T) {
data := make([][]string, 100)
for i := range data {
data[i] = []string{"name", "age", "city", "country"}
}
err := CSV().Render(context.Background(), &errorWriterTest{}, data, NoOptions)
assert.Error(t, err)
assert.StringContains(t, err.Error(), "csv:")
})
}