-
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 test for checkMetadata function (#4)
* Add test - not passing * Add comments and testdata dir * Make the tests pass. * Add more tests * Mark a line for future improvement
- Loading branch information
Showing
7 changed files
with
175 additions
and
11 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,118 @@ | ||
package datasetIngestor | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
"time" | ||
"reflect" | ||
) | ||
|
||
func TestGetHost(t *testing.T) { | ||
// Call the function under test. | ||
host := GetHost() | ||
|
||
// fail the test and report an error if the returned hostname is an empty string. | ||
if len(host) == 0 { | ||
t.Errorf("getHost() returned an empty string") | ||
} | ||
|
||
// fail the test and report an error if the returned hostname is "unknown". | ||
if host == "unknown" { | ||
t.Errorf("getHost() was unable to get the hostname") | ||
} | ||
} | ||
|
||
func TestCheckMetadata(t *testing.T) { | ||
// Define mock parameters for the function | ||
var TEST_API_SERVER string = "https://dacat-qa.psi.ch/api/v3" // TODO: Test Improvement. Change this to a mock server. At the moment, tests will fail if we change this to a mock server. | ||
var APIServer = TEST_API_SERVER | ||
var metadatafile1 = "testdata/metadata.json" | ||
var metadatafile2 = "testdata/metadata-short.json" | ||
|
||
// Mock HTTP client | ||
client := &http.Client{ | ||
Timeout: 5 * time.Second, // Set a timeout for requests | ||
Transport: &http.Transport{ | ||
// Customize the transport settings if needed (e.g., proxy, TLS config) | ||
// For a dummy client, default settings are usually sufficient | ||
}, | ||
CheckRedirect: func(req *http.Request, via []*http.Request) error { | ||
// Customize how redirects are handled if needed | ||
// For a dummy client, default behavior is usually sufficient | ||
return http.ErrUseLastResponse // Use the last response for redirects | ||
}, | ||
} | ||
|
||
// Mock user map | ||
user := map[string]string{ | ||
"displayName": "csaxsswissfel", | ||
"mail": "testuser@example.com", | ||
} | ||
|
||
// Mock access groups | ||
accessGroups := []string{"p17880", "p17301"} | ||
|
||
// Call the function with mock parameters | ||
metaDataMap, sourceFolder, beamlineAccount := CheckMetadata(client, APIServer, metadatafile1, user, accessGroups) | ||
|
||
// Add assertions here based on the expected behavior of the function | ||
if len(metaDataMap) == 0 { | ||
t.Error("Expected non-empty metadata map") | ||
} | ||
if sourceFolder == "" { | ||
t.Error("Expected non-empty source folder") | ||
} | ||
if sourceFolder != "/usr/share/gnome" { | ||
t.Error("sourceFolder should be '/usr/share/gnome'") | ||
} | ||
if reflect.TypeOf(beamlineAccount).Kind() != reflect.Bool { | ||
t.Error("Expected beamlineAccount to be boolean") | ||
} | ||
if beamlineAccount != false { | ||
t.Error("Expected beamlineAccount to be false") | ||
} | ||
if _, ok := metaDataMap["ownerEmail"]; !ok { | ||
t.Error("metaDataMap missing required key 'ownerEmail'") | ||
} | ||
if _, ok := metaDataMap["principalInvestigator"]; !ok { | ||
t.Error("metaDataMap missing required key 'principalInvestigator'") | ||
} | ||
if _, ok := metaDataMap["scientificMetadata"]; !ok { | ||
t.Error("metaDataMap missing required key 'scientificMetadata'") | ||
} | ||
scientificMetadata, ok := metaDataMap["scientificMetadata"].([]interface{}) | ||
if ok { | ||
firstEntry := scientificMetadata[0].(map[string]interface{}) | ||
sample, ok := firstEntry["sample"].(map[string]interface{}) | ||
if ok { | ||
if _, ok := sample["name"]; !ok { | ||
t.Error("Sample is missing 'name' field") | ||
} | ||
if _, ok := sample["description"]; !ok { | ||
t.Error("Sample is missing 'description' field") | ||
} | ||
} | ||
} else { | ||
t.Error("scientificMetadata is not a list") | ||
} | ||
|
||
// test with the second metadata file | ||
metaDataMap2, sourceFolder2, beamlineAccount2 := CheckMetadata(client, APIServer, metadatafile2, user, accessGroups) | ||
|
||
// Add assertions here based on the expected behavior of the function | ||
if len(metaDataMap2) == 0 { | ||
t.Error("Expected non-empty metadata map") | ||
} | ||
if sourceFolder2 == "" { | ||
t.Error("Expected non-empty source folder") | ||
} | ||
if sourceFolder2 != "/tmp/gnome" { | ||
t.Error("sourceFolder should be '/tmp/gnome'") | ||
} | ||
if reflect.TypeOf(beamlineAccount2).Kind() != reflect.Bool { | ||
t.Error("Expected beamlineAccount to be boolean") | ||
} | ||
if beamlineAccount2 != false { | ||
t.Error("Expected beamlineAccount to be false") | ||
} | ||
} |
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,4 @@ | ||
// This file exports internal functions for testing purposes. Since the name of the file ends with "_test.go", it will not be included in the final build of the application. | ||
package datasetIngestor | ||
|
||
var GetHost = getHost |
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,8 @@ | ||
{ | ||
"principalInvestigator":"egon.meier@psi.ch", | ||
"creationLocation":"/PSI/SLS/CSAXS/SWISSFEL", | ||
"sourceFolder": "/tmp/gnome", | ||
"owner": "Andreas Menzel", | ||
"type": "raw", | ||
"ownerGroup": "p17880" | ||
} |
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,20 @@ | ||
{ | ||
"creationLocation": "/PSI/SLS/CSAXS/SWISSFEL", | ||
"datasetName": "CMakeCache", | ||
"description": "", | ||
"owner": "Ana Diaz", | ||
"ownerEmail": "ana.diaz@psi.ch", | ||
"ownerGroup": "p17301", | ||
"principalInvestigator": "ana.diaz@psi.ch", | ||
"scientificMetadata": [ | ||
{ | ||
"sample": { | ||
"description": "", | ||
"name": "", | ||
"principalInvestigator": "" | ||
} | ||
} | ||
], | ||
"sourceFolder": "/usr/share/gnome", | ||
"type": "raw" | ||
} |
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