github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/job/util_test.go (about)

     1  //go:build unit || !integration
     2  
     3  package job
     4  
     5  import (
     6  	"fmt"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/filecoin-project/bacalhau/pkg/logger"
    11  	"github.com/stretchr/testify/require"
    12  	"github.com/stretchr/testify/suite"
    13  )
    14  
    15  // In order for 'go test' to run this suite, we need to create
    16  // a normal test function and pass our suite to suite.Run
    17  func TestJobUtilSuite(t *testing.T) {
    18  	suite.Run(t, new(JobUtilSuite))
    19  }
    20  
    21  // Define the suite, and absorb the built-in basic suite
    22  // functionality from testify - including a T() method which
    23  // returns the current testing context
    24  type JobUtilSuite struct {
    25  	suite.Suite
    26  }
    27  
    28  // Before each test
    29  func (s *JobUtilSuite) SetupTest() {
    30  	logger.ConfigureTestLogging(s.T())
    31  }
    32  
    33  func (s *JobUtilSuite) TestRun_URLs() {
    34  	tests := []struct {
    35  		numberOfJobs int
    36  	}{
    37  		{numberOfJobs: 1},
    38  	}
    39  
    40  	for range tests {
    41  		testURLs := []struct {
    42  			submittedURL string
    43  			convertedURL string // if we parse it, this is what it should look like
    44  			valid        bool
    45  			errorMsg     string
    46  		}{
    47  			{submittedURL: "http://example.com",
    48  				valid:    false,
    49  				errorMsg: "TYPE: Invalid (no file)"},
    50  			{submittedURL: "http://example.com/file.txt",
    51  				valid:    true,
    52  				errorMsg: "TYPE: Valid"},
    53  			{submittedURL: "ttps://example.com",
    54  				valid:    false,
    55  				errorMsg: "TYPE: Bad scheme"},
    56  			{submittedURL: "example.com",
    57  				valid:    false,
    58  				errorMsg: "TYPE: Mising scheme"},
    59  			{submittedURL: "http://example.com:8080/file.txt",
    60  				valid:    true,
    61  				errorMsg: "TYPE: With Ports"},
    62  			{submittedURL: `https://data.cityofnewyork.us/api/views/t29m-gskq/rows.csv?accessType=DOWNLOAD`,
    63  				valid:    true,
    64  				errorMsg: "TYPE: With query string"},
    65  			{submittedURL: `https://data.cityofnewyork.us/api/views/t29m-gskq/rows.csv?accessType=DOWNLOAD&foo=bar`,
    66  				valid:    true,
    67  				errorMsg: "TYPE: With query string with ampersand"},
    68  			{submittedURL: `"https://data.cityofnewyork.us/api/views/t29m-gskq/rows.csv?accessType=DOWNLOAD&foo=bar"`,
    69  				valid:    true,
    70  				errorMsg: "TYPE: With Double quotes"},
    71  			{submittedURL: `'https://data.cityofnewyork.us/api/views/t29m-gskq/rows.csv?accessType=DOWNLOAD&foo=bar'`,
    72  				valid:    true,
    73  				errorMsg: "TYPE: With single quotes"},
    74  		}
    75  
    76  		for _, testURL := range testURLs {
    77  			func() {
    78  				// Test all URLs against the validator
    79  				spec, err := buildJobInputs(nil, []string{testURL.submittedURL})
    80  				originalURLTrimmed := strings.Trim(testURL.submittedURL, `"' `)
    81  				convertedTrimmed := strings.Trim(testURL.convertedURL, `"' `)
    82  				if testURL.valid {
    83  					require.NoError(s.T(), err, fmt.Sprintf("%s: Should not have errored - %s", testURL.errorMsg, testURL.submittedURL))
    84  					require.Equal(s.T(), 1, len(spec), testURL.errorMsg)
    85  					if testURL.convertedURL != "" {
    86  						require.Equal(s.T(), convertedTrimmed, spec[0].URL, testURL.errorMsg)
    87  					} else {
    88  						require.Equal(s.T(), originalURLTrimmed, spec[0].URL, testURL.errorMsg)
    89  					}
    90  				} else {
    91  					require.Error(s.T(), err, fmt.Sprintf("%s: Should have errored - %s", testURL.errorMsg, testURL.submittedURL))
    92  				}
    93  			}()
    94  		}
    95  	}
    96  }