|
| 1 | +package output |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +type fakeCSVRow struct { |
| 12 | + fields []string |
| 13 | + err error |
| 14 | +} |
| 15 | + |
| 16 | +func (r fakeCSVRow) MarshalCSV() ([]string, error) { |
| 17 | + return r.fields, r.err |
| 18 | +} |
| 19 | + |
| 20 | +type fakeCSVRecordSet struct { |
| 21 | + headers []string |
| 22 | + records []CSVMarshaler |
| 23 | +} |
| 24 | + |
| 25 | +func (f fakeCSVRecordSet) CSVHeaders() []string { |
| 26 | + return f.headers |
| 27 | +} |
| 28 | + |
| 29 | +func (f fakeCSVRecordSet) CSVRecords() []CSVMarshaler { |
| 30 | + return f.records |
| 31 | +} |
| 32 | + |
| 33 | +func recordSet(headers []string, rows [][]string) fakeCSVRecordSet { |
| 34 | + records := make([]CSVMarshaler, len(rows)) |
| 35 | + for i, row := range rows { |
| 36 | + records[i] = fakeCSVRow{fields: row} |
| 37 | + } |
| 38 | + |
| 39 | + return fakeCSVRecordSet{headers: headers, records: records} |
| 40 | +} |
| 41 | + |
| 42 | +func TestMarshalCSV(t *testing.T) { |
| 43 | + t.Parallel() |
| 44 | + |
| 45 | + headers := []string{"user_type", "user_id", "relation", "object_type", "object_id", "condition_context"} |
| 46 | + |
| 47 | + tests := []struct { |
| 48 | + name string |
| 49 | + records [][]string |
| 50 | + expected string |
| 51 | + }{ |
| 52 | + { |
| 53 | + name: "no records writes only headers", |
| 54 | + records: nil, |
| 55 | + expected: "user_type,user_id,relation,object_type,object_id,condition_context\n", |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "single record", |
| 59 | + records: [][]string{ |
| 60 | + {"user", "john", "writer", "document", "abc.doc", ""}, |
| 61 | + }, |
| 62 | + expected: "user_type,user_id,relation,object_type,object_id,condition_context\n" + |
| 63 | + "user,john,writer,document,abc.doc,\n", |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "multiple records", |
| 67 | + records: [][]string{ |
| 68 | + {"user", "anne", "reader", "document", "x", ""}, |
| 69 | + {"group", "eng", "owner", "repo", "y", ""}, |
| 70 | + }, |
| 71 | + expected: "user_type,user_id,relation,object_type,object_id,condition_context\n" + |
| 72 | + "user,anne,reader,document,x,\n" + |
| 73 | + "group,eng,owner,repo,y,\n", |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "values with commas, quotes and newlines are escaped", |
| 77 | + records: [][]string{ |
| 78 | + {"user", "a,b", "say \"hi\"", "doc", "line\nbreak", `{"ip_addr":"10.0.0.1"}`}, |
| 79 | + }, |
| 80 | + expected: "user_type,user_id,relation,object_type,object_id,condition_context\n" + |
| 81 | + "user,\"a,b\",\"say \"\"hi\"\"\",doc,\"line\nbreak\",\"{\"\"ip_addr\"\":\"\"10.0.0.1\"\"}\"\n", |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + for _, test := range tests { |
| 86 | + t.Run(test.name, func(t *testing.T) { |
| 87 | + t.Parallel() |
| 88 | + |
| 89 | + got, err := marshalCSV(recordSet(headers, test.records)) |
| 90 | + require.NoError(t, err) |
| 91 | + assert.Equal(t, test.expected, string(got)) |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestMarshalCSVNotARecordSet(t *testing.T) { |
| 97 | + t.Parallel() |
| 98 | + |
| 99 | + _, err := marshalCSV([]string{"a", "b"}) |
| 100 | + assert.ErrorIs(t, err, errNotCSVMarshaler) |
| 101 | +} |
| 102 | + |
| 103 | +func TestMarshalCSVRecordError(t *testing.T) { |
| 104 | + t.Parallel() |
| 105 | + |
| 106 | + sentinel := errors.New("boom") |
| 107 | + set := fakeCSVRecordSet{ |
| 108 | + headers: []string{"col"}, |
| 109 | + records: []CSVMarshaler{fakeCSVRow{err: sentinel}}, |
| 110 | + } |
| 111 | + |
| 112 | + _, err := marshalCSV(set) |
| 113 | + assert.ErrorIs(t, err, sentinel) |
| 114 | +} |
0 commit comments