github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/lib/log_test.go (about) 1 package lib 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/google/go-cmp/cmp" 9 "github.com/google/go-cmp/cmp/cmpopts" 10 "github.com/qri-io/qri/base/params" 11 testcfg "github.com/qri-io/qri/config/test" 12 "github.com/qri-io/qri/dsref" 13 "github.com/qri-io/qri/event" 14 "github.com/qri-io/qri/p2p" 15 reporef "github.com/qri-io/qri/repo/ref" 16 testrepo "github.com/qri-io/qri/repo/test" 17 ) 18 19 func TestHistoryRequestsLog(t *testing.T) { 20 ctx, done := context.WithCancel(context.Background()) 21 defer done() 22 23 mr, refs, err := testrepo.NewTestRepoWithHistory() 24 if err != nil { 25 t.Fatalf("error allocating test repo: %s", err.Error()) 26 return 27 } 28 29 node, err := p2p.NewQriNode(mr, testcfg.DefaultP2PForTesting(), event.NilBus, nil) 30 if err != nil { 31 t.Fatal(err.Error()) 32 } 33 34 firstRef := refs[0].String() 35 36 items := make([]dsref.VersionInfo, len(refs)) 37 for i, r := range refs { 38 ds := r.Dataset 39 items[i] = reporef.ConvertToVersionInfo(&r) 40 items[i].MetaTitle = "" 41 items[i].BodyRows = 0 42 items[i].NumErrors = 0 43 items[i].BodyFormat = "" 44 if ds != nil && ds.Commit != nil { 45 items[i].CommitTitle = ds.Commit.Title 46 items[i].CommitMessage = ds.Commit.Message 47 } 48 } 49 50 cases := []struct { 51 description string 52 p *ActivityParams 53 refs []dsref.VersionInfo 54 err string 55 }{ 56 {"log list - empty", 57 &ActivityParams{}, nil, `activity: ref required`}, 58 {"log list - bad path", 59 &ActivityParams{Ref: "/badpath"}, nil, `"/badpath" is not a valid dataset reference: unexpected character at position 0: '/'`}, 60 {"log list - default", 61 &ActivityParams{Ref: firstRef}, items, ""}, 62 {"log list - offset 0 limit 3", 63 &ActivityParams{Ref: firstRef, List: params.List{Offset: 0, Limit: 3}}, items[:3], ""}, 64 {"log list - offset 3 limit 3", 65 &ActivityParams{Ref: firstRef, List: params.List{Offset: 3, Limit: 3}}, items[3:], ""}, 66 {"log list - offset 6 limit 3", 67 &ActivityParams{Ref: firstRef, List: params.List{Offset: 6, Limit: 3}}, nil, "repo: no history"}, 68 } 69 70 inst := NewInstanceFromConfigAndNode(ctx, testcfg.DefaultConfigForTesting(), node) 71 m := inst.Dataset() 72 for _, c := range cases { 73 got, err := m.Activity(ctx, c.p) 74 75 if !(err == nil && c.err == "" || err != nil && err.Error() == c.err) { 76 t.Errorf("case '%s' error mismatch: expected: %s, got: %s", c.description, c.err, err) 77 continue 78 } 79 80 if len(c.refs) != len(got) { 81 t.Errorf("case '%s' expected log length error. expected: %d, got: %d", c.description, len(c.refs), len(got)) 82 continue 83 } 84 85 t.Logf("-- %s", c.description) 86 for i, v := range c.refs { 87 t.Logf("expect: %d got: %d", v.BodySize, got[i].BodySize) 88 } 89 90 if diff := cmp.Diff(c.refs, got, cmpopts.IgnoreFields(dsref.VersionInfo{}, "CommitTime", "ProfileID"), cmpopts.IgnoreFields(dsref.Ref{}, "Path")); diff != "" { 91 t.Errorf("case '%s' result mismatch (-want +got):\n%s", c.description, diff) 92 } 93 } 94 } 95 96 func TestHistoryRequestsLogEntries(t *testing.T) { 97 ctx, done := context.WithCancel(context.Background()) 98 defer done() 99 100 mr, refs, err := testrepo.NewTestRepoWithHistory() 101 if err != nil { 102 t.Fatalf("error allocating test repo: %s", err.Error()) 103 return 104 } 105 106 node, err := p2p.NewQriNode(mr, testcfg.DefaultP2PForTesting(), event.NilBus, nil) 107 if err != nil { 108 t.Fatal(err.Error()) 109 } 110 111 firstRef := refs[0].String() 112 inst := NewInstanceFromConfigAndNode(ctx, testcfg.DefaultConfigForTesting(), node) 113 114 if _, err = inst.Log().Log(ctx, &RefListParams{}); err == nil { 115 t.Errorf("expected empty reference param to error") 116 } 117 118 res, err := inst.Log().Log(ctx, &RefListParams{Ref: firstRef, Limit: 30}) 119 if err != nil { 120 t.Fatal(err) 121 } 122 123 result := make([]string, len(res)) 124 for i := range res { 125 // set response times to zero for consistent results 126 res[i].Timestamp = time.Time{} 127 result[i] = res[i].String() 128 } 129 130 expect := []string{ 131 `12:00AM peer init branch main`, 132 `12:00AM peer save commit initial commit`, 133 `12:00AM peer save commit initial commit`, 134 `12:00AM peer save commit initial commit`, 135 `12:00AM peer save commit initial commit`, 136 `12:00AM peer save commit initial commit`, 137 } 138 139 if diff := cmp.Diff(expect, result); diff != "" { 140 t.Errorf("result mismatch (-want +got):\n%s", diff) 141 } 142 }