Skip to content

Commit

Permalink
Add tests for createDatasetEntry (#47)
Browse files Browse the repository at this point in the history
* Add tests for `createDatasetEntry`

* Simplify mocking metaDataMap
  • Loading branch information
kavir1698 authored May 16, 2024
1 parent 8750a2c commit 3908c0b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
20 changes: 20 additions & 0 deletions datasetIngestor/createDatasetEntry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ import (
"net/http"
)

/*
CreateDatasetEntry is a function that creates a new dataset entry in a specified API server.
Parameters:
- client: An *http.Client object used to send the HTTP request.
- APIServer: A string representing the URL of the API server.
- metaDataMap: A map[string]interface{} containing the metadata for the new dataset.
- accessToken: A string representing the access token for the API server.
The function first serializes the metaDataMap to JSON. It then checks the "type" field in the metaDataMap to determine the type of the dataset ("raw", "derived", or "base"). Depending on the type, it constructs the appropriate URL for the API server and sends a POST request to create the new dataset.
If the server responds with a status code of 200, the function decodes the response body to extract the "pid" field, which it returns as a string. If the server responds with any other status code, the function logs an error message and terminates the program.
If the "type" field is not present in the metaDataMap, or if it contains an unrecognized value, the function logs an error message and terminates the program.
Returns:
- A string representing the "pid" of the newly created dataset. If the function encounters an error, it will not return a value.
Note: This function will terminate the program if it encounters an error, such as a failure to serialize the metaDataMap, a failure to send the HTTP request, a non-200 response from the server, or an unrecognized dataset type.
*/
func CreateDatasetEntry(client *http.Client, APIServer string, metaDataMap map[string]interface{}, accessToken string) (datasetId string) {
// assemble json structure
bm, err := json.Marshal(metaDataMap)
Expand Down
51 changes: 51 additions & 0 deletions datasetIngestor/createDatasetEntry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package datasetIngestor

import (
"net/http"
"net/http/httptest"
"testing"
)

func TestCreateDatasetEntry(t *testing.T) {
var capturedPath string

// Create a mock server
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte(`{"pid": "1234"}`))
capturedPath = req.URL.Path
}))
// Close the server when test finishes
defer server.Close()

// Create a map for the metaData
metaDataMap := map[string]interface{}{
"type": "raw",
}

// Create a client
client := server.Client()

// Call the function
datasetId := CreateDatasetEntry(client, server.URL, metaDataMap, "testToken")

// Check the returned datasetId
if datasetId != "1234" {
t.Errorf("Expected datasetId to be '1234', but got '%s'", datasetId)
}

// Check the URL based on the type field
expectedPath := ""
switch metaDataMap["type"].(string) {
case "raw":
expectedPath = "/RawDatasets"
case "derived":
expectedPath = "/DerivedDatasets"
case "base":
expectedPath = "/Datasets"
}

// Check the URL
if capturedPath != expectedPath {
t.Errorf("Expected path to be '%s', but got '%s'", expectedPath, capturedPath)
}
}

0 comments on commit 3908c0b

Please sign in to comment.