-
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 tests for
createDatasetEntry
(#47)
* Add tests for `createDatasetEntry` * Simplify mocking metaDataMap
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 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,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) | ||
} | ||
} |