This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
forked from jazibjohar/intercom-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_api_test.go
62 lines (57 loc) · 1.8 KB
/
job_api_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
61
62
package intercom
import (
"io/ioutil"
"testing"
)
func TestJobAPISaveUser(t *testing.T) {
http := TestJobHTTPClient{t: t, expectedURI: "/bulk/users", fixtureFilename: "fixtures/job.json"}
api := JobAPI{httpClient: &http}
user := User{UserID: "1234"}
job := JobRequest{Items: []*JobItem{NewUserJobItem(&user, JOB_POST)}, bulkType: "users"}
http.f = func(job *JobRequest) {
if job.Items[0].DataType != "user" {
t.Errorf("job item was of wrong data type, expected %s, was %s", "user", job.Items[0].DataType)
}
if job.Items[0].Data.(requestUser).UserID != "1234" {
t.Errorf("wrong user id sent")
}
}
savedJob, _ := api.save(&job)
if savedJob.ID != "job_5ca1ab1eca11ab1e" {
t.Errorf("Did not respond with correct job")
}
}
func TestJobAPISaveEvent(t *testing.T) {
http := TestJobHTTPClient{t: t, expectedURI: "/bulk/events", fixtureFilename: "fixtures/job.json"}
api := JobAPI{httpClient: &http}
event := Event{UserID: "1234"}
job := JobRequest{Items: []*JobItem{NewEventJobItem(&event)}, bulkType: "events"}
http.f = func(job *JobRequest) {
if job.Items[0].DataType != "event" {
t.Errorf("job item was of wrong data type, expected %s, was %s", "event", job.Items[0].DataType)
}
if job.Items[0].Data.(*Event).UserID != "1234" {
t.Errorf("wrong user id sent")
}
}
savedJob, _ := api.save(&job)
if savedJob.ID != "job_5ca1ab1eca11ab1e" {
t.Errorf("Did not respond with correct job")
}
}
type TestJobHTTPClient struct {
TestHTTPClient
t *testing.T
f func(job *JobRequest)
fixtureFilename string
expectedURI string
}
func (t *TestJobHTTPClient) Post(uri string, body interface{}) ([]byte, error) {
if t.expectedURI != uri {
t.t.Errorf("Wrong endpoint called")
}
if t.f != nil {
t.f(body.(*JobRequest))
}
return ioutil.ReadFile(t.fixtureFilename)
}