github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/api/test_run_medium_test.go (about)

     1  //go:build medium
     2  // +build medium
     3  
     4  package api
     5  
     6  import (
     7  	"encoding/json"
     8  	"io"
     9  	"net/http"
    10  	"net/http/httptest"
    11  	"testing"
    12  
    13  	"github.com/gorilla/mux"
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/web-platform-tests/wpt.fyi/shared"
    16  	"github.com/web-platform-tests/wpt.fyi/shared/sharedtest"
    17  )
    18  
    19  func TestGetTestRunByID(t *testing.T) {
    20  	i, err := sharedtest.NewAEInstance(true)
    21  	assert.Nil(t, err)
    22  	defer i.Close()
    23  	r, err := i.NewRequest("GET", "/api/runs/123", nil)
    24  	r = mux.SetURLVars(r, map[string]string{"id": "123"})
    25  	assert.Nil(t, err)
    26  
    27  	ctx := r.Context()
    28  	resp := httptest.NewRecorder()
    29  	apiTestRunHandler(resp, r)
    30  	assert.Equal(t, http.StatusNotFound, resp.Code)
    31  
    32  	chrome := shared.TestRun{
    33  		ProductAtRevision: shared.ProductAtRevision{
    34  			Product: shared.Product{
    35  				BrowserName: "chrome",
    36  			},
    37  			Revision: "abcdef0123",
    38  		},
    39  	}
    40  
    41  	store := shared.NewAppEngineDatastore(ctx, false)
    42  	store.Put(store.NewIDKey("TestRun", 123), &chrome)
    43  	resp = httptest.NewRecorder()
    44  	apiTestRunHandler(resp, r)
    45  	assert.Equal(t, http.StatusOK, resp.Code)
    46  	var bodyTestRun shared.TestRun
    47  	bodyBytes, _ := io.ReadAll(resp.Body)
    48  	json.Unmarshal(bodyBytes, &bodyTestRun)
    49  	assert.Equal(t, int64(123), bodyTestRun.ID)
    50  }