blob: d32841a78b9f38383a8c77bee94a2bfa3f0273f1 [file] [log] [blame]
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gerrit
import (
"context"
"maps"
"slices"
"testing"
"golang.org/x/oscar/internal/secret"
"golang.org/x/oscar/internal/storage"
"golang.org/x/oscar/internal/testutil"
)
func TestLoadTxtar(t *testing.T) {
check := testutil.Checker(t)
lg := testutil.Slogger(t)
db := storage.MemDB()
sdb := secret.Empty()
c := New("gerrit-test", lg, db, sdb, nil)
tc := c.Testing()
check(tc.LoadTxtar("testdata/change.txt"))
ch := tc.change(1)
if ch == nil {
t.Fatal("could not find loaded change")
}
tests := []changeTests{
{
"ChangeLabels",
wa(c.ChangeLabels),
map[string]string{
"Hold": "put change on hold",
},
func(got, want any) bool {
g := got.(map[string]*LabelInfo)
w := want.(map[string]string)
m := make(map[string]string)
for k, l := range g {
m[k] = l.Description
}
return maps.Equal(w, m)
},
},
{
"ChangeOwner",
wa(c.ChangeOwner),
"gopher@golang.org",
func(got, want any) bool {
return got.(*AccountInfo).Email == want
},
},
{
"ChangeDescription",
wa(c.ChangeDescription),
"gerrit: test change",
nil,
},
{
"ChangeHashtags",
wa(c.ChangeHashtags),
[]string{"tag1", "tag2"},
func(got, want any) bool {
g := got.([]string)
w := want.([]string)
return slices.Equal(g, w)
},
},
{
"ChangeMessages",
wa(c.ChangeMessages),
[]string{
"message 1",
"message 2",
},
func(got, want any) bool {
g := got.([]*ChangeMessageInfo)
w := want.([]string)
for i, m := range g {
if m.Message != w[i] {
return false
}
}
return true
},
},
}
testChangeTests(t, ch, tests)
want := "comment 1"
if got := tc.comments[1][0].Message; got != want {
t.Errorf("change #1: got comment %q, want %q", got, want)
}
}
func TestTestingChanges(t *testing.T) {
check := testutil.Checker(t)
ctx := context.Background()
tc := newTestingClient()
tc.queryLimit = 1000 // grab everything in one batch
check(tc.LoadTxtar("testdata/uniquetimes.txt"))
cnt := 0
// There should be six changes matching the criteria, but skip the first one.
for _, err := range tc.changes(ctx, "test", "2020-03-01 10:10:10.00000000", "2020-08-01 10:10:10.00000000", 1) {
check(err)
cnt++
}
if cnt != 5 {
t.Errorf("want 5 changes; got %d", cnt)
}
}