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

     1  package model
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/evergreen-ci/evergreen/model/task"
     8  	"github.com/evergreen-ci/evergreen/util"
     9  )
    10  
    11  // APITest contains the data to be returned whenever a test is used in the
    12  // API.
    13  type APITest struct {
    14  	TaskId    APIString `json:"task_id"`
    15  	Status    APIString `json:"status"`
    16  	TestFile  APIString `json:"test_file"`
    17  	Logs      TestLogs  `json:"logs"`
    18  	ExitCode  int       `json:"exit_code"`
    19  	StartTime APITime   `json:"start_time"`
    20  	EndTime   APITime   `json:"end_time"`
    21  }
    22  
    23  // TestLogs is a struct for storing the information about logs that will
    24  // be written out as part of an APITest.
    25  type TestLogs struct {
    26  	URL     APIString `json:"url"`
    27  	LineNum int       `json:"line_num"`
    28  	URLRaw  APIString `json:"url_raw"`
    29  	LogId   APIString `json:"log_id"`
    30  }
    31  
    32  func (at *APITest) BuildFromService(st interface{}) error {
    33  	switch v := st.(type) {
    34  	case *task.TestResult:
    35  		at.Status = APIString(v.Status)
    36  		at.TestFile = APIString(v.TestFile)
    37  		at.ExitCode = v.ExitCode
    38  
    39  		startTime := util.FromPythonTime(v.StartTime)
    40  		endTime := util.FromPythonTime(v.EndTime)
    41  
    42  		at.StartTime = APITime(startTime)
    43  		at.EndTime = APITime(endTime)
    44  
    45  		at.Logs = TestLogs{
    46  			URL:     APIString(v.URL),
    47  			URLRaw:  APIString(v.URLRaw),
    48  			LogId:   APIString(v.LogId),
    49  			LineNum: v.LineNum,
    50  		}
    51  	case string:
    52  		at.TaskId = APIString(v)
    53  	default:
    54  		return fmt.Errorf("Incorrect type when creating APITest")
    55  	}
    56  	return nil
    57  }
    58  
    59  func (at *APITest) ToService() (interface{}, error) {
    60  	return &task.TestResult{
    61  		Status:    string(at.Status),
    62  		TestFile:  string(at.TestFile),
    63  		URL:       string(at.Logs.URL),
    64  		URLRaw:    string(at.Logs.URLRaw),
    65  		LogId:     string(at.Logs.LogId),
    66  		LineNum:   at.Logs.LineNum,
    67  		ExitCode:  at.ExitCode,
    68  		StartTime: util.ToPythonTime(time.Time(at.StartTime)),
    69  		EndTime:   util.ToPythonTime(time.Time(at.EndTime)),
    70  	}, nil
    71  }