Skip to content

Commit

Permalink
Fix bulk v1 unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyergler committed Jan 30, 2024
1 parent a4670ac commit 7eb8d67
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 23 deletions.
74 changes: 51 additions & 23 deletions bulk/v1/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,31 +281,50 @@ func TestBatch_Results(t *testing.T) {
fields: fields{
session: &session.Mock{
URL: "https://test.salesforce.com",
HTTPClient: mockHTTPClient(
func(req *http.Request) *http.Response {
resp := `[
{
"success" : true,
"created" : true,
"id" : "001xx000003DHP0AAO",
"errors" : []
HTTPClient: multiMockHTTPClient(
map[string]mock{
"https://test.salesforce.com/services/async/42.0/job/1234/batch/abcd/result": {
fn: func(req *http.Request) *http.Response {
resp := `[
{
"success" : true,
"created" : true,
"id" : "001xx000003DHP0AAO",
"errors" : []
},
{
"success" : true,
"created" : true,
"id" : "001xx000003DHP1AAO",
"errors" : []
}
]`
return &http.Response{
StatusCode: http.StatusOK,
Status: "OK",
Body: ioutil.NopCloser(strings.NewReader(resp)),
Header: make(http.Header),
}
},
{
"success" : true,
"created" : true,
"id" : "001xx000003DHP1AAO",
"errors" : []
}
]`
return &http.Response{
StatusCode: http.StatusOK,
Status: "OK",
Body: ioutil.NopCloser(strings.NewReader(resp)),
Header: make(http.Header),
}
cond: []mockHTTPFilter{
wantMethod(http.MethodGet),
},
},
"https://test.salesforce.com/services/async/42.0/job/1234/batch/abcd/request": {
fn: func(req *http.Request) *http.Response {
resp := `[]`
return &http.Response{
StatusCode: http.StatusOK,
Status: "OK",
Body: ioutil.NopCloser(strings.NewReader(resp)),
Header: make(http.Header),
}
},
cond: []mockHTTPFilter{
wantMethod(http.MethodGet),
},
},
},
wantMethod(http.MethodGet),
wantURL("https://test.salesforce.com/services/async/42.0/job/1234/batch/abcd/result"),
),
},
},
Expand All @@ -321,12 +340,18 @@ func TestBatch_Results(t *testing.T) {
Created: true,
JobRecord: bulk.JobRecord{
ID: "001xx000003DHP0AAO",
UnprocessedRecord: bulk.UnprocessedRecord{
Fields: map[string]interface{}{},
},
},
},
{
Created: true,
JobRecord: bulk.JobRecord{
ID: "001xx000003DHP1AAO",
UnprocessedRecord: bulk.UnprocessedRecord{
Fields: map[string]interface{}{},
},
},
},
},
Expand Down Expand Up @@ -383,6 +408,9 @@ func TestBatch_Results(t *testing.T) {
Created: true,
JobRecord: bulk.JobRecord{
ID: "001xx000003DHP0AAO",
UnprocessedRecord: bulk.UnprocessedRecord{
Fields: map[string]interface{}{},
},
},
},
},
Expand Down
28 changes: 28 additions & 0 deletions bulk/v1/mock_http_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,31 @@ func mockHTTPClient(fn roundTripFunc, filters ...mockHTTPFilter) *http.Client {
),
}
}

type mock struct {
fn roundTripFunc
cond []mockHTTPFilter
}

func multiMockHTTPClient(mocks map[string]mock) *http.Client {
return &http.Client{
Transport: roundTripFunc(
func(req *http.Request) *http.Response {
if mock, ok := mocks[req.URL.String()]; ok {
for _, f := range mock.cond {
if resp := f(req); resp != nil {
return resp
}
}
return mock.fn(req)
}
return &http.Response{
StatusCode: 500,
Status: "Invalid URL",
Body: ioutil.NopCloser(strings.NewReader(req.URL.String())),
Header: make(http.Header),
}
},
),
}
}

0 comments on commit 7eb8d67

Please sign in to comment.