github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/cmd/log_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"regexp"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	"github.com/qri-io/dataset/dstest"
     9  )
    10  
    11  func TestLogbookCommand(t *testing.T) {
    12  	r := NewTestRunner(t, "test_peer_logbook", "qri_test_logbook")
    13  	defer r.Delete()
    14  
    15  	r.MustExec(t, "qri save --body=testdata/movies/body_ten.csv me/test_movies")
    16  
    17  	r.MustExec(t, "qri save --body=testdata/movies/body_thirty.csv me/test_movies")
    18  
    19  	// Cannot provide dataset reference with the --raw flag
    20  	if err := r.ExecCommand("qri logbook me/test_movies --raw"); err == nil {
    21  		t.Error("expected using a ref and the raw flag to error")
    22  	}
    23  
    24  	// Logbook formatted as raw json
    25  	tplString := `[{"ops":[{"type":"init","model":"user","name":"test_peer_logbook","authorID":"{{ .profileID }}","timestamp":"timeStampHere"}],"logs":[{"ops":[{"type":"init","model":"dataset","name":"test_movies","authorID":"{{ .authorID }}","timestamp":"timeStampHere"}],"logs":[{"ops":[{"type":"init","model":"branch","name":"main","authorID":"{{ .authorID }}","timestamp":"timeStampHere"},{"type":"init","model":"commit","ref":"{{ .path1 }}","timestamp":"timeStampHere","size":224,"note":"created dataset from body_ten.csv"},{"type":"init","model":"commit","ref":"{{ .path2 }}","prev":"{{ .path1 }}","timestamp":"timeStampHere","size":720,"note":"body changed by 70%"}]}]}]}]`
    26  
    27  	expect := dstest.Template(t, tplString, map[string]string{
    28  		"profileID": "QmeL2mdVka1eahKENjehK6tBxkkpk5dNQ1qMcgWi7Hrb4B",
    29  		"authorID":  "74iwd7hnx5u47nfnu73auj77hycw5ivdjswdrfyafh4cr3ylmwnq",
    30  		"path1":     "/ipfs/QmVmAAVSVewv6HzojRBr2bqJgWwZ8w18vVPqQ6VuTuH7UZ",
    31  		"path2":     "/ipfs/QmdvkyoxvUXkKYpxDYbSh9nswzY4hyjtWG7FBbLgYxpgbi",
    32  	})
    33  
    34  	// Regex that replaces the timestamp with just static text
    35  	fixTs := regexp.MustCompile(`"(timestamp|commitTime)":\s?"[0-9TZ.:+-]*?"`)
    36  
    37  	// Verify the raw output of the logbook
    38  	actual := r.MustExec(t, "qri logbook --raw")
    39  	// TODO(dustmop): Make logbook's timestamp stringifier be hot-swappable to avoid this hack.
    40  	actual = string(fixTs.ReplaceAll([]byte(actual), []byte(`"timestamp":"timeStampHere"`)))
    41  	if diff := cmp.Diff(expect, actual); diff != "" {
    42  		t.Errorf("unexpected (-want +got):\n%s", diff)
    43  	}
    44  }