go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/api/gitiles/log_test.go (about)

     1  // Copyright 2017 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package gitiles
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"net/http"
    21  	"testing"
    22  
    23  	"go.chromium.org/luci/common/proto/gitiles"
    24  
    25  	. "github.com/smartystreets/goconvey/convey"
    26  )
    27  
    28  func TestPagingLog(t *testing.T) {
    29  	t.Parallel()
    30  	ctx := context.Background()
    31  
    32  	Convey("PagingLog", t, func() {
    33  		var reqs []http.Request
    34  		srv, c := newMockClient(func(w http.ResponseWriter, r *http.Request) {
    35  			reqs = append(reqs, *r)
    36  
    37  			w.Header().Set("Content-Type", "application/json")
    38  			fmt.Fprintf(w, ")]}'\n")
    39  			if r.FormValue("s") == "" {
    40  				fmt.Fprintf(w, `{"log": [%s], "next": "next_cursor_value"}`, fakeCommit1Str)
    41  			} else {
    42  				fmt.Fprintf(w, `{"log": [%s]}`, fakeCommit2Str)
    43  			}
    44  		})
    45  		defer srv.Close()
    46  
    47  		Convey("Page till no cursor", func() {
    48  			req := &gitiles.LogRequest{
    49  				Project:            "repo",
    50  				ExcludeAncestorsOf: "master",
    51  				Committish:         "8de6836858c99e48f3c58164ab717bda728e95dd",
    52  			}
    53  			commits, err := PagingLog(ctx, c, req, 10)
    54  			So(err, ShouldBeNil)
    55  			So(reqs, ShouldHaveLength, 2)
    56  			So(reqs[0].FormValue("s"), ShouldEqual, "")
    57  			So(reqs[1].FormValue("s"), ShouldEqual, "next_cursor_value")
    58  			So(len(commits), ShouldEqual, 2)
    59  			So(commits[0].Author.Name, ShouldEqual, "Author 1")
    60  			So(commits[1].Id, ShouldEqual, "dc1dbf1aa56e4dd4cbfaab61c4d30a35adce5f40")
    61  		})
    62  
    63  		Convey("Page till limit", func() {
    64  			req := &gitiles.LogRequest{
    65  				Project:    "repo",
    66  				Committish: "master",
    67  			}
    68  			commits, err := PagingLog(ctx, c, req, 1)
    69  			So(err, ShouldBeNil)
    70  			So(reqs, ShouldHaveLength, 1)
    71  			So(reqs[0].FormValue("n"), ShouldEqual, "1")
    72  			So(len(commits), ShouldEqual, 1)
    73  			So(commits[0].Author.Name, ShouldEqual, "Author 1")
    74  		})
    75  	})
    76  }