blob: 84cfb7f3f9e89532be2411b6c656ba0ba2934bb3 [file] [log] [blame]
package tryjobstore
import (
"encoding/json"
"fmt"
"strings"
"testing"
"time"
assert "github.com/stretchr/testify/require"
"go.skia.org/infra/go/deepequal"
"go.skia.org/infra/go/testutils/unittest"
"go.skia.org/infra/go/util"
)
func TestIssueDetails(t *testing.T) {
unittest.SmallTest(t)
issue := &Issue{
ID: 12345,
Subject: "Test Subject",
Owner: "jdoe@example.com",
Updated: time.Now(),
URL: "https://cr.example.com",
}
firstPS := &PatchsetDetail{ID: 34567}
seconPS := &PatchsetDetail{ID: 23456}
thirdPS := &PatchsetDetail{ID: 44567}
issue.UpdatePatchsets([]*PatchsetDetail{firstPS})
assert.Equal(t, firstPS, issue.PatchsetDetails[0])
// Add the first again and the others.
issue.UpdatePatchsets([]*PatchsetDetail{firstPS})
issue.UpdatePatchsets([]*PatchsetDetail{seconPS})
issue.UpdatePatchsets([]*PatchsetDetail{thirdPS})
issue.UpdatePatchsets([]*PatchsetDetail{firstPS})
issue.UpdatePatchsets([]*PatchsetDetail{seconPS})
issue.UpdatePatchsets([]*PatchsetDetail{thirdPS})
issue.UpdatePatchsets([]*PatchsetDetail{firstPS})
issue.UpdatePatchsets([]*PatchsetDetail{seconPS})
issue.UpdatePatchsets([]*PatchsetDetail{thirdPS})
// Make sure we are sorted.
deepequal.AssertDeepEqual(t, []*PatchsetDetail{seconPS, firstPS, thirdPS}, issue.PatchsetDetails)
assert.NoError(t, nil)
}
func TestSerialize(t *testing.T) {
unittest.SmallTest(t)
status := TryjobStatus(TRYJOB_INGESTED)
jsonStatus, err := json.Marshal(status)
assert.NoError(t, err)
assert.Equal(t, "\"ingested\"", string(jsonStatus))
}
func TestTimeJsonMs(t *testing.T) {
unittest.SmallTest(t)
now := TimeJsonMs(time.Now())
expMs := fmt.Sprintf("%d", time.Time(now).UnixNano()/int64(time.Millisecond))
jsonBytes, err := json.Marshal(now)
assert.NoError(t, err)
assert.Equal(t, expMs, string(jsonBytes))
tjs := []*Issue{
{Updated: time.Time(now)},
}
jsonBytes, err = json.Marshal(tjs)
assert.NoError(t, err)
expField := `"updated":` + expMs
assert.True(t, strings.Contains(string(jsonBytes), expField))
}
func TestTryjobJsonCodec(t *testing.T) {
unittest.SmallTest(t)
tryjob_1 := &Tryjob{
IssueID: 12345,
PatchsetID: 9,
Builder: "Test-Builder-1",
BuildBucketID: 45409309403,
Status: TRYJOB_RUNNING,
}
// Create a codec like we do for firing events and test the round trip.
codec := util.JSONCodec(&Tryjob{})
jsonBytes, err := codec.Encode(tryjob_1)
assert.NoError(t, err)
foundInterface, err := codec.Decode(jsonBytes)
assert.NoError(t, err)
assert.IsType(t, foundInterface, &Tryjob{})
assert.Equal(t, tryjob_1, foundInterface.(*Tryjob))
}