Skip to content

Commit

Permalink
Make the tests pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
kavir1698 committed Mar 22, 2024
1 parent 629d072 commit 38b5d63
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 66 deletions.
90 changes: 26 additions & 64 deletions datasetIngestor/checkMetadata_test.go
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 {
Expand All @@ -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
}
4 changes: 4 additions & 0 deletions datasetIngestor/export_test.go
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
2 changes: 1 addition & 1 deletion datasetIngestor/testdata/metadata.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"creationLocation": "/PSI/SLS/CSAXS",
"creationLocation": "/PSI/SLS/CSAXS/SWISSFEL",
"datasetName": "CMakeCache",
"description": "",
"owner": "Ana Diaz",
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (

require (
github.com/creack/pty v1.1.17 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
golang.org/x/sys v0.2.0 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63n
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI=
github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
Expand Down

0 comments on commit 38b5d63

Please sign in to comment.