-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_test.go
60 lines (50 loc) · 1.38 KB
/
file_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
52
53
54
55
56
57
58
59
60
package utils
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/kylelemons/godebug/pretty"
"github.com/slack-go/slack"
)
const mockURL = "files-pri/T012345AB-F01234ABC/download/fake.csv"
func TestDownloadAndReadCSV(t *testing.T) {
testCases := []struct {
description string
csvDownloadResp []byte
wantRows [][]string
wantErr error
}{
{
description: "successful download returns valid rows",
csvDownloadResp: []byte(mockCSVDownloadResp),
wantRows: [][]string{
[]string{"email"},
[]string{"hoge@email.com"},
[]string{"foo@email.com"},
[]string{"bar@email.com"},
},
wantErr: nil,
},
{
description: "invalid/empty CSV file err",
wantErr: ErrInvalidCSV,
},
}
for _, tc := range testCases {
mux := http.NewServeMux()
mux.HandleFunc(fmt.Sprintf("/%v", mockURL), func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(tc.csvDownloadResp)
})
testServ := httptest.NewServer(mux)
defer testServ.Close()
client := slack.New("x012345", slack.OptionAPIURL(fmt.Sprintf("%v/", testServ.URL)))
rows, err := DownloadAndReadCSV(client, fmt.Sprintf("%v/%v", testServ.URL, mockURL))
if diff := pretty.Compare(rows, tc.wantRows); diff != "" {
t.Fatalf("+got -want %s\n", diff)
}
if diff := pretty.Compare(tc.wantErr, err); diff != "" {
t.Fatalf("+got -want %s\n", diff)
}
}
}