Skip to content

Commit

Permalink
Test getJson (#67)
Browse files Browse the repository at this point in the history
* Add a basic test for `GetDatasetDetailsPublished`

* Add a test for `getJson`

* Fix go.mod
  • Loading branch information
kavir1698 authored May 22, 2024
1 parent 14eafe2 commit 50f564f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
20 changes: 17 additions & 3 deletions datasetUtils/getDatasetDetailsPublished.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package datasetUtils
import (
"encoding/json"
"github.com/fatih/color"
"io/ioutil"
"io"
"log"
"net/http"
"net/url"
Expand All @@ -12,8 +12,22 @@ import (

const PUBLISHServer string = "doi2.psi.ch"

// get dataset details and filter by ownergroup
/*
GetDatasetDetailsPublished retrieves details of published datasets from a given API server.
Parameters:
- client: An HTTP client used to send requests.
- APIServer: The URL of the API server from which to retrieve dataset details.
- datasetList: A list of dataset IDs for which to retrieve details.
The function sends HTTP GET requests to the API server, querying for details of datasets in chunks of 100 at a time.
For each dataset, it checks if the details were found and if so, it logs the details, adds the dataset to the output list,
and constructs a URL for the dataset. If the details were not found, it logs a message indicating that the dataset will not be copied.
The function returns two lists:
- A list of Dataset objects for which details were found.
- A list of URLs for the datasets.
*/
func GetDatasetDetailsPublished(client *http.Client, APIServer string, datasetList []string) ([]Dataset, []string) {
outputDatasetDetails := make([]Dataset, 0)
urls := make([]string, 0)
Expand Down Expand Up @@ -49,7 +63,7 @@ func GetDatasetDetailsPublished(client *http.Client, APIServer string, datasetLi
defer resp.Body.Close()

if resp.StatusCode == 200 {
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)

datasetDetails := make([]Dataset, 0)

Expand Down
31 changes: 31 additions & 0 deletions datasetUtils/getDatasetDetailsPublished_test.go
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)
}
}
2 changes: 1 addition & 1 deletion datasetUtils/getJson.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)



// GetJson sends a GET request to a specified URL and decodes the JSON response into a target variable.
func GetJson(client *http.Client, myurl string, target interface{}) error {
r, err := client.Get(myurl)
if err != nil {
Expand Down
34 changes: 34 additions & 0 deletions datasetUtils/getJson_test.go
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"])
}

0 comments on commit 50f564f

Please sign in to comment.