Skip to content

Commit

Permalink
Add a basic test for GetDatasetDetailsPublished
Browse files Browse the repository at this point in the history
  • Loading branch information
kavir1698 committed May 13, 2024
1 parent 93dd6a3 commit abb143a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 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)
}
}

0 comments on commit abb143a

Please sign in to comment.