github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/logbook/temp_builder.go (about) 1 package logbook 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/qri-io/dataset" 9 "github.com/qri-io/qfs" 10 "github.com/qri-io/qri/dsref" 11 "github.com/qri-io/qri/event" 12 "github.com/qri-io/qri/logbook/oplog" 13 "github.com/qri-io/qri/profile" 14 ) 15 16 // BookBuilder builds a logbook in a convenient way 17 type BookBuilder struct { 18 Book *Book 19 Owner *profile.Profile 20 Dsrefs map[string][]string 21 } 22 23 // NewLogbookTempBuilder constructs a logbook tmp BookBuilder 24 func NewLogbookTempBuilder(t *testing.T, owner *profile.Profile, fs qfs.Filesystem, fsPath string) BookBuilder { 25 book, err := NewJournal(*owner, event.NilBus, fs, fsPath) 26 if err != nil { 27 t.Fatal(err) 28 } 29 builder := BookBuilder{ 30 Book: book, 31 Owner: owner, 32 Dsrefs: make(map[string][]string), 33 } 34 return builder 35 } 36 37 // DatasetInit initializes a new dataset and return a reference to it 38 func (b *BookBuilder) DatasetInit(ctx context.Context, t *testing.T, dsname string) string { 39 author := b.Owner 40 initID, err := b.Book.WriteDatasetInit(ctx, author, dsname) 41 if err != nil { 42 t.Fatal(err) 43 } 44 b.Dsrefs[dsname] = make([]string, 0) 45 return initID 46 } 47 48 // DatasetRename changes the name of a dataset 49 func (b *BookBuilder) DatasetRename(ctx context.Context, t *testing.T, initID, newName string) dsref.Ref { 50 author := b.Owner 51 if err := b.Book.WriteDatasetRename(ctx, author, initID, newName); err != nil { 52 t.Fatal(err) 53 } 54 ref := dsref.Ref{} 55 return dsref.Ref{Username: author.Peername, Name: newName, Path: ref.Path} 56 } 57 58 // DatasetDelete deletes a dataset 59 func (b *BookBuilder) DatasetDelete(ctx context.Context, t *testing.T, initID string) { 60 author := b.Owner 61 if err := b.Book.WriteDatasetDeleteAll(ctx, author, initID); err != nil { 62 t.Fatal(err) 63 } 64 ref := dsref.Ref{} 65 delete(b.Dsrefs, ref.Name) 66 } 67 68 // AddForeign merges a foreign log into this book, signs using owner 69 func (b *BookBuilder) AddForeign(ctx context.Context, t *testing.T, log *oplog.Log) { 70 log.Sign(b.Owner.PrivKey) 71 if err := b.Book.MergeLog(ctx, b.Owner.PubKey, log); err != nil { 72 t.Fatal(err) 73 } 74 } 75 76 // Commit adds a commit to a dataset 77 func (b *BookBuilder) Commit(ctx context.Context, t *testing.T, initID, title, ipfsHash string) dsref.Ref { 78 author := b.Owner 79 ref := dsref.Ref{} 80 ds := &dataset.Dataset{ 81 ID: initID, 82 Peername: ref.Username, 83 Name: ref.Name, 84 Commit: &dataset.Commit{ 85 Timestamp: time.Unix(0, NewTimestamp()), 86 Title: title, 87 }, 88 Path: ipfsHash, 89 PreviousPath: ref.Path, 90 } 91 if err := b.Book.WriteVersionSave(ctx, author, ds, nil); err != nil { 92 t.Fatal(err) 93 } 94 b.Dsrefs[ref.Name] = append(b.Dsrefs[ref.Name], ipfsHash) 95 return dsref.Ref{Username: ref.Username, Name: ref.Name, Path: ipfsHash} 96 } 97 98 // Delete removes some number of commits from a dataset 99 func (b *BookBuilder) Delete(ctx context.Context, t *testing.T, initID string, num int) dsref.Ref { 100 author := b.Owner 101 ref := dsref.Ref{} 102 if err := b.Book.WriteVersionDelete(ctx, author, initID, num); err != nil { 103 t.Fatal(err) 104 } 105 prevRefs := b.Dsrefs[ref.Name] 106 nextRefs := prevRefs[:len(prevRefs)-num] 107 b.Dsrefs[ref.Name] = nextRefs 108 lastRef := nextRefs[len(nextRefs)-1] 109 return dsref.Ref{Username: ref.Username, Name: ref.Name, Path: lastRef} 110 } 111 112 // Logbook returns the built logbook 113 func (b *BookBuilder) Logbook() *Book { 114 return b.Book 115 }