Skip to content

Commit

Permalink
Expand TestHandleJobResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
kavir1698 committed May 6, 2024
1 parent ca7b5b5 commit 3837d5d
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions datasetUtils/createRetrieveJob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,50 @@ func TestSendJobRequest(t *testing.T) {
}
}

// Checks if the function returns a job ID and no error when it's called with a valid response.
// Checks for a successful response, a response with a non-200 status code, and a response with invalid JSON.
func TestHandleJobResponse(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte(`{"id": "12345"}`))
}))
defer server.Close()

client := server.Client()
resp, _ := client.Get(server.URL)
user := map[string]string{
"mail": "[email protected]",
"username": "testuser",
}

// Test successful response
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte(`{"id": "12345"}`))
}))
client := server.Client()
resp, _ := client.Get(server.URL)
jobId, err := handleJobResponse(resp, user)

if err != nil {
t.Errorf("handleJobResponse() returned an error: %v", err)
}

if jobId != "12345" {
t.Errorf("handleJobResponse() returned job ID %v, want 12345", jobId)
}
server.Close()

// Test non-200 status code
server = httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte(`{"id": "12345"}`))
}))
client = server.Client()
resp, _ = client.Get(server.URL)
jobId, err = handleJobResponse(resp, user)
if err == nil {
t.Errorf("handleJobResponse() did not return an error for non-200 status code")
}
server.Close()

// Test invalid JSON in response
server = httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte(`invalid JSON`))
}))
client = server.Client()
resp, _ = client.Get(server.URL)
jobId, err = handleJobResponse(resp, user)
if err == nil {
t.Errorf("handleJobResponse() did not return an error for invalid JSON")
}
server.Close()
}

0 comments on commit 3837d5d

Please sign in to comment.