Skip to content

Commit

Permalink
update getDatasetDetails tests
Browse files Browse the repository at this point in the history
  • Loading branch information
consolethinks committed Nov 4, 2024
1 parent b6e79ce commit f2e1a33
Showing 1 changed file with 55 additions and 4 deletions.
59 changes: 55 additions & 4 deletions datasetUtils/getDatasetDetails_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package datasetUtils
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)

Expand All @@ -25,12 +26,18 @@ func TestGetDatasetDetails_EmptyList(t *testing.T) {
client := &http.Client{}

// Call the function to be tested
datasets, _, _ := GetDatasetDetails(client, APIServer, accessToken, datasetList, ownerGroup)
datasets, notFoundIds, err := GetDatasetDetails(client, APIServer, accessToken, datasetList, ownerGroup)
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
}

// Check the result
if len(datasets) != 0 {
t.Errorf("Expected 0 datasets, got %d", len(datasets))
}
if len(notFoundIds) != 0 {
t.Errorf("")
}
}

func TestGetDatasetDetails_Non200StatusCode(t *testing.T) {
Expand All @@ -52,12 +59,18 @@ func TestGetDatasetDetails_Non200StatusCode(t *testing.T) {
client := &http.Client{}

// Call the function to be tested
datasets, _, _ := GetDatasetDetails(client, APIServer, accessToken, datasetList, ownerGroup)
datasets, notFoundIds, err := GetDatasetDetails(client, APIServer, accessToken, datasetList, ownerGroup)
if err == nil {
t.Errorf("Expected an error to be returned, got nil")
}

// Check the result
if len(datasets) != 0 {
t.Errorf("Expected 0 datasets, got %d", len(datasets))
}
if len(notFoundIds) != 0 {
t.Errorf("Expected 0 not found IDs, got %d", len(notFoundIds))
}
}

func TestGetDatasetDetails_DatasetNotFound(t *testing.T) {
Expand All @@ -79,12 +92,18 @@ func TestGetDatasetDetails_DatasetNotFound(t *testing.T) {
client := &http.Client{}

// Call the function to be tested
datasets, _, _ := GetDatasetDetails(client, APIServer, accessToken, datasetList, ownerGroup)
datasets, notFoundIds, err := GetDatasetDetails(client, APIServer, accessToken, datasetList, ownerGroup)
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
}

// Check the result
if len(datasets) != 0 {
t.Errorf("Expected 0 datasets, got %d", len(datasets))
}
if len(notFoundIds) != 1 {
t.Errorf("Expected 1 ID that was not found, got %d", len(notFoundIds))
}
}

func TestGetDatasetDetails_DatasetFound(t *testing.T) {
Expand All @@ -106,7 +125,10 @@ func TestGetDatasetDetails_DatasetFound(t *testing.T) {
client := &http.Client{}

// Call the function to be tested
datasets, _, _ := GetDatasetDetails(client, APIServer, accessToken, datasetList, ownerGroup)
datasets, notFoundIds, err := GetDatasetDetails(client, APIServer, accessToken, datasetList, ownerGroup)
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
}

// Check the result
if len(datasets) != 1 {
Expand All @@ -117,4 +139,33 @@ func TestGetDatasetDetails_DatasetFound(t *testing.T) {
t.Errorf("Dataset details do not match expected values")
}
}
if len(notFoundIds) != 0 {
t.Errorf("Expected no IDs that couldn't be found, got %d", len(notFoundIds))
}
}

func TestGetDatasetDetails_FilterString(t *testing.T) {
accessToken := "testToken"
datasetList := []string{"123"}
ownerGroup := "group1"
expectedFilter := `{"where":{"pid":{"inq":["` + strings.Join(datasetList, `","`) + `"]},"ownerGroup":"` + ownerGroup + `"},"fields":{"pid":true,"sourceFolder":true,"size":true,"ownerGroup":true}}`

// Create a mock server
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
filter := req.URL.Query().Get("filter")
if filter != expectedFilter {
t.Errorf("Filter does not match expected value - got: \"%s\", expected: \"%s\"", filter, expectedFilter)
}
// Send some response
rw.Write([]byte(`[{"Pid":"123","SourceFolder":"/path/to/dataset","Size":1024,"OwnerGroup":"group1","NumberOfFiles":10}]`))
}))
// Close the server when test finishes
defer server.Close()
APIServer := server.URL

// Create a new HTTP client
client := &http.Client{}

// Call the function to be tested
GetDatasetDetails(client, APIServer, accessToken, datasetList, ownerGroup)
}

0 comments on commit f2e1a33

Please sign in to comment.