-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add a basic test for `GetDatasetDetailsPublished` * Add a test for `getJson` * Fix go.mod
- Loading branch information
Showing
4 changed files
with
83 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package datasetUtils | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
// This test checks that the function correctly parses the response from the server. | ||
func TestGetDatasetDetailsPublished(t *testing.T) { | ||
// Create a mock HTTP server | ||
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
// Respond with a JSON representation of a list of datasets | ||
rw.Write([]byte(`[{"pid": "1", "sourceFolder": "/folder1", "size": 100, "ownerGroup": "group1", "numberOfFiles": 10}]`)) | ||
})) | ||
defer server.Close() | ||
|
||
// Create a new HTTP client | ||
client := &http.Client{} | ||
|
||
// Call the function with the mock server's URL and a list of dataset IDs | ||
datasets, urls := GetDatasetDetailsPublished(client, server.URL, []string{"1"}) | ||
|
||
// Test that the function returns the expected results | ||
if len(datasets) != 1 || datasets[0].Pid != "1" { | ||
t.Errorf("Unexpected datasets: %v", datasets) | ||
} | ||
if len(urls) != 1 || urls[0] != "https://doi2.psi.ch/datasets/folder1" { | ||
t.Errorf("Unexpected URLs: %v", urls) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package datasetUtils | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// This test creates a mock server that responds with a JSON object when it receives a GET request. It then calls GetJson with the URL of the mock server and checks that the function correctly decodes the response into the target variable. | ||
func TestGetJson(t *testing.T) { | ||
// Create a mock server | ||
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
rw.Write([]byte(`{"fakeKey": "fakeValue"}`)) | ||
})) | ||
// Close the server when test finishes | ||
defer server.Close() | ||
|
||
// Create a client | ||
client := &http.Client{} | ||
|
||
// Create a target to hold our expected data | ||
var target map[string]string | ||
|
||
// Send a request to our mock server | ||
err := GetJson(client, server.URL, &target) | ||
|
||
// Assert there was no error | ||
assert.Nil(t, err) | ||
|
||
// Assert the target is correctly populated | ||
assert.Equal(t, "fakeValue", target["fakeKey"]) | ||
} |