Skip to content

Commit 8c0982a

Browse files
committed
reverse: tests
1 parent 406525f commit 8c0982a

2 files changed

Lines changed: 68 additions & 9 deletions

File tree

.github/workflows/go.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ name: Go
22

33
on:
44
push:
5-
branches: [main]
6-
pull_request:
75
branches:
86
- *
97

8+
109
jobs:
1110
lint:
1211
runs-on: ubuntu-latest

replacer_test.go

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package csvheaders_test
33
import (
44
"bytes"
55
"encoding/csv"
6+
"errors"
67
"slices"
78
"testing"
89

@@ -14,6 +15,7 @@ func TestReplacer(t *testing.T) {
1415

1516
t.Run("Read", testReplacerRead)
1617
t.Run("ReadAll", testReplacerReadAll)
18+
t.Run("Reverse", testReplacerReverse)
1719
}
1820

1921
func testReplacerRead(t *testing.T) {
@@ -55,21 +57,20 @@ field 1,Charles,Monica
5557
func testReplacerReadAll(t *testing.T) {
5658
t.Parallel()
5759

58-
var rawRows = []byte(`recording artist,title,isrc
59-
THE BEATLES,LET IT BE,US1239875
60-
recording artist,my band,FR789230987
60+
var rawRows = []byte(`recording artist,title
61+
THE BEATLES,LET IT BE
62+
recording artist,my band
6163
`)
6264

6365
want := [][]string{
64-
{"ARTIST", "TITLE", "ISRC"},
65-
{"THE BEATLES", "LET IT BE", "US1239875"},
66-
{"recording artist", "my band", "FR789230987"},
66+
{"ARTIST", "TITLE"},
67+
{"THE BEATLES", "LET IT BE"},
68+
{"recording artist", "my band"},
6769
}
6870

6971
headerReplacements := map[string]string{
7072
"recording artist": "ARTIST",
7173
"title": "TITLE",
72-
"isrc": "ISRC",
7374
}
7475

7576
r := csvheaders.NewReplacer(csv.NewReader(bytes.NewBuffer(rawRows)), headerReplacements)
@@ -79,6 +80,65 @@ recording artist,my band,FR789230987
7980
t.Fatal(err)
8081
}
8182

83+
compareResults(t, want, got)
84+
}
85+
86+
func testReplacerReverse(t *testing.T) {
87+
t.Parallel()
88+
89+
var rawRows = []byte(`field 1,field 2,field 10
90+
value 1,field 2,value 10
91+
`)
92+
93+
testCases := map[string]struct {
94+
headerMapping map[string]string
95+
expectedError error
96+
want [][]string
97+
}{
98+
"duplicated header": {
99+
headerMapping: map[string]string{
100+
"field 1": "FIELD",
101+
"field 2": "FIELD",
102+
},
103+
expectedError: csvheaders.ErrDuplicateHeader,
104+
want: nil,
105+
},
106+
"nil headers": {
107+
headerMapping: nil,
108+
expectedError: nil,
109+
want: [][]string{{"field 1", "field 2", "field 10"}, {"value 1", "field 2", "value 10"}},
110+
},
111+
"no header conflict": {
112+
headerMapping: map[string]string{"Monday": "field 10"},
113+
expectedError: nil,
114+
want: [][]string{{"field 1", "field 2", "Monday"}, {"value 1", "field 2", "value 10"}},
115+
},
116+
}
117+
118+
for name, testCase := range testCases {
119+
t.Run(name, func(t *testing.T) {
120+
t.Parallel()
121+
122+
r, err := csvheaders.NewReverseReplacer(csv.NewReader(bytes.NewBuffer(rawRows)), testCase.headerMapping)
123+
if testCase.expectedError != nil {
124+
if !errors.Is(err, testCase.expectedError) {
125+
t.Fatalf("expected %s, got %s", testCase.expectedError, err)
126+
}
127+
128+
return
129+
}
130+
131+
got, err := r.ReadAll()
132+
if err != nil {
133+
t.Fatalf("unexpected error while reading: %s", err)
134+
}
135+
136+
compareResults(t, testCase.want, got)
137+
})
138+
}
139+
}
140+
141+
func compareResults(t *testing.T, want, got [][]string) {
82142
if len(got) != len(want) {
83143
t.Fatalf("got %d records, want %d", len(got), len(want))
84144
}

0 commit comments

Comments
 (0)