-
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.
- Loading branch information
Showing
5 changed files
with
34 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,15 @@ | ||
package datasetIngestor | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"io" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
"os" | ||
"time" | ||
"reflect" | ||
) | ||
|
||
// TestGetHost is a test function for the getHost function. | ||
func TestGetHost(t *testing.T) { | ||
// Call the function under test. | ||
host := getHost() | ||
host := GetHost() | ||
|
||
// fail the test and report an error if the returned hostname is an empty string. | ||
if len(host) == 0 { | ||
|
@@ -27,80 +23,46 @@ func TestGetHost(t *testing.T) { | |
} | ||
|
||
func TestCheckMetadata(t *testing.T) { | ||
// Define mock parameters for the function | ||
var TEST_API_SERVER string = "https://dacat-qa.psi.ch/api/v3" // "https://example.com/api" | ||
var APIServer = TEST_API_SERVER | ||
var metadatafile1 = "testdata/metadata.json" | ||
// var metadatafile2 = "testdata/metadata-short.json" | ||
|
||
// Mock HTTP client | ||
mockClient := &http.Client{ | ||
Transport: RoundTripFunc(func(req *http.Request) *http.Response { | ||
// Prepare a mock response for the HTTP client | ||
return &http.Response{ | ||
StatusCode: 200, | ||
Body: io.NopCloser(strings.NewReader(`{"valid":true}`)), | ||
Header: make(http.Header), | ||
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 | ||
mockUser := map[string]string{ | ||
"displayName": "testuser", | ||
user := map[string]string{ | ||
"displayName": "csaxsswissfel", | ||
"mail": "[email protected]", | ||
} | ||
|
||
// Mock access groups | ||
mockAccessGroups := []string{"group1", "group2"} | ||
|
||
// Mock metadata file content | ||
mockMetadata := map[string]interface{}{ | ||
"type": "raw", | ||
// Add other required fields as needed for testing | ||
} | ||
|
||
// Convert metadata to JSON | ||
mockMetadataJSON, err := json.Marshal(mockMetadata) | ||
if err != nil { | ||
t.Fatalf("Error marshaling mock metadata: %v", err) | ||
} | ||
|
||
// Create a temporary file for mock metadata | ||
tmpfile, err := ioutil.TempFile("", "mockmetadata.json") | ||
if err != nil { | ||
t.Fatalf("Error creating temporary file: %v", err) | ||
} | ||
defer tmpfile.Close() | ||
defer func() { | ||
// Clean up temporary file | ||
if err := tmpfile.Close(); err != nil { | ||
t.Fatalf("Error closing temporary file: %v", err) | ||
} | ||
if err := os.Remove(tmpfile.Name()); err != nil { | ||
t.Fatalf("Error removing temporary file: %v", err) | ||
} | ||
}() | ||
|
||
// Write mock metadata JSON to the temporary file | ||
if _, err := tmpfile.Write(mockMetadataJSON); err != nil { | ||
t.Fatalf("Error writing mock metadata to temporary file: %v", err) | ||
} | ||
accessGroups := []string{"group1", "p17301"} | ||
|
||
// Call the function with mock parameters | ||
metaDataMap, sourceFolder, beamlineAccount := CheckMetadata(mockClient, "http://example.com/api", tmpfile.Name(), mockUser, mockAccessGroups) | ||
metaDataMap, sourceFolder, beamlineAccount := CheckMetadata(client, APIServer, metadatafile1, user, accessGroups) | ||
|
||
// Add assertions here based on the expected behavior of the function | ||
// For example: | ||
if len(metaDataMap) == 0 { | ||
t.Error("Expected non-empty metadata map") | ||
} | ||
if sourceFolder == "" { | ||
t.Error("Expected non-empty source folder") | ||
} | ||
if !beamlineAccount { | ||
t.Error("Expected beamline account to be true") | ||
if reflect.TypeOf(beamlineAccount).Kind() != reflect.Bool { | ||
t.Error("Expected beamlineAccount to be boolean") | ||
} | ||
} | ||
|
||
// RoundTripFunc type is a custom implementation of http.RoundTripper | ||
type RoundTripFunc func(req *http.Request) *http.Response | ||
|
||
// RoundTrip executes a single HTTP transaction, returning a Response for the provided Request. | ||
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { | ||
return f(req), nil | ||
} |
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
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