github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/apiv3/servicecontext/test.go (about)

     1  package servicecontext
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/evergreen-ci/evergreen/apiv3"
     8  	"github.com/evergreen-ci/evergreen/model/task"
     9  )
    10  
    11  // DBTestConnector is a struct that implements the Test related methods
    12  // from the ServiceContext through interactions with the backing database.
    13  type DBTestConnector struct{}
    14  
    15  func (tc *DBTestConnector) FindTestsByTaskId(taskId, testFilename, status string, limit,
    16  	sortDir int) ([]task.TestResult, error) {
    17  	pipeline := task.TestResultsByTaskIdPipeline(taskId, testFilename, status, limit, sortDir)
    18  	res := []task.TestResult{}
    19  
    20  	err := task.Aggregate(pipeline, &res)
    21  	if err != nil {
    22  		return []task.TestResult{}, err
    23  	}
    24  	if len(res) == 0 {
    25  		var message string
    26  		if status != "" {
    27  			message = fmt.Sprintf("tests for task with taskId '%s' and status '%s' not found", taskId, status)
    28  		} else {
    29  			message = fmt.Sprintf("tests for task with taskId '%s' not found", taskId)
    30  		}
    31  		return []task.TestResult{}, &apiv3.APIError{
    32  			StatusCode: http.StatusNotFound,
    33  			Message:    message,
    34  		}
    35  	}
    36  
    37  	if testFilename != "" {
    38  		found := false
    39  		for _, t := range res {
    40  			if t.TestFile == testFilename {
    41  				found = true
    42  				break
    43  			}
    44  		}
    45  		if !found {
    46  			return []task.TestResult{}, &apiv3.APIError{
    47  				StatusCode: http.StatusNotFound,
    48  				Message:    fmt.Sprintf("test with filename %s not found", testFilename),
    49  			}
    50  		}
    51  	}
    52  	return res, nil
    53  }
    54  
    55  // MockTaskConnector stores a cached set of tests that are queried against by the
    56  // implementations of the ServiceContext interface's Test related functions.
    57  type MockTestConnector struct {
    58  	CachedTests []task.TestResult
    59  	StoredError error
    60  }
    61  
    62  // FindTestsBytaskId
    63  func (mtc *MockTestConnector) FindTestsByTaskId(taskId, testFilename, status string, limit,
    64  	sortDir int) ([]task.TestResult, error) {
    65  	if mtc.StoredError != nil {
    66  		return []task.TestResult{}, mtc.StoredError
    67  	}
    68  
    69  	// loop until the filename is found
    70  	for ix, t := range mtc.CachedTests {
    71  		if t.TestFile == testFilename {
    72  			// We've found the test
    73  			var testsToReturn []task.TestResult
    74  			if sortDir < 0 {
    75  				if ix-limit > 0 {
    76  					testsToReturn = mtc.CachedTests[ix-(limit) : ix]
    77  				} else {
    78  					testsToReturn = mtc.CachedTests[:ix]
    79  				}
    80  			} else {
    81  				if ix+limit > len(mtc.CachedTests) {
    82  					testsToReturn = mtc.CachedTests[ix:]
    83  				} else {
    84  					testsToReturn = mtc.CachedTests[ix : ix+limit]
    85  				}
    86  			}
    87  			return testsToReturn, nil
    88  		}
    89  	}
    90  	return nil, nil
    91  }
    92  
    93  type testSorter []task.TestResult
    94  
    95  func (ts testSorter) Len() int {
    96  	return len(ts)
    97  }
    98  
    99  func (ts testSorter) Less(i, j int) bool {
   100  	return ts[i].TestFile < ts[j].TestFile
   101  }
   102  func (ts testSorter) Swap(i, j int) {
   103  	ts[i], ts[j] = ts[j], ts[i]
   104  }