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

     1  package cmd
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	"github.com/qri-io/dataset/dstest"
     9  )
    10  
    11  // Test that deleting an entire dataset works properly with the logbook.
    12  func TestLogAndDeletes(t *testing.T) {
    13  	run := NewTestRunner(t, "test_peer_log_and_deletes", "qri_test_log_and_deletes")
    14  	defer run.Delete()
    15  
    16  	// Save a dataset containing a body.json, no meta, nothing special.
    17  	err := run.ExecCommand("qri save --body=testdata/movies/body_two.json me/log_test")
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  
    22  	// Log should have exactly one version
    23  	err = run.ExecCommand("qri log me/log_test")
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	output := run.GetCommandOutput()
    28  	expect := dstest.Template(t, `1   Commit:  {{ .path }}
    29      Date:    Sun Dec 31 20:01:01 EST 2000
    30      Storage: local
    31      Size:    79 B
    32  
    33      created dataset from body_two.json
    34  
    35  `, map[string]string{
    36  		"path": "/ipfs/QmfU8fcG7DjpL94JvDvAvzo2zkWWXx2Lj8kiq3KhB7Kvat",
    37  	})
    38  
    39  	if diff := cmpTextLines(expect, output); diff != "" {
    40  		t.Errorf("qri log (-want +got):\n%s", diff)
    41  	}
    42  
    43  	// Save anoter dataset version
    44  	err = run.ExecCommand("qri save --body=testdata/movies/body_four.json me/log_test")
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  
    49  	// Log should have two versions
    50  	err = run.ExecCommand("qri log me/log_test")
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	output = run.GetCommandOutput()
    55  	expect = dstest.Template(t, `1   Commit:  {{ .path1 }}
    56      Date:    Sun Dec 31 20:02:01 EST 2000
    57      Storage: local
    58      Size:    137 B
    59  
    60      body added row 2 and added row 3
    61      body:
    62      	added row 2
    63      	added row 3
    64  
    65  2   Commit:  {{ .path2 }}
    66      Date:    Sun Dec 31 20:01:01 EST 2000
    67      Storage: local
    68      Size:    79 B
    69  
    70      created dataset from body_two.json
    71  
    72  `, map[string]string{
    73  		"path1": "/ipfs/QmZfKcJ9yAqwaYnVo9fqczLP11ScUtQY4tcQVQcMfp2o7Y",
    74  		"path2": "/ipfs/QmfU8fcG7DjpL94JvDvAvzo2zkWWXx2Lj8kiq3KhB7Kvat",
    75  	})
    76  
    77  	if diff := cmpTextLines(expect, output); diff != "" {
    78  		t.Errorf("qri log (-want +got):\n%s", diff)
    79  	}
    80  
    81  	// Save anoter dataset version
    82  	err = run.ExecCommand("qri remove --revisions=1 me/log_test")
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  
    87  	// Log should only have the first version
    88  	err = run.ExecCommand("qri log me/log_test")
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  	output = run.GetCommandOutput()
    93  	expect = dstest.Template(t, `1   Commit:  {{ .path }}
    94      Date:    Sun Dec 31 20:01:01 EST 2000
    95      Storage: local
    96      Size:    79 B
    97  
    98      created dataset from body_two.json
    99  
   100  `, map[string]string{
   101  		"path": "/ipfs/QmfU8fcG7DjpL94JvDvAvzo2zkWWXx2Lj8kiq3KhB7Kvat",
   102  	})
   103  
   104  	if diff := cmpTextLines(expect, output); diff != "" {
   105  		t.Errorf("qri log (-want +got):\n%s", diff)
   106  	}
   107  
   108  	// TODO(dlong): Get the logbook log, verify that it contains 4 entries (init, 2 saves, delete).
   109  	// Don't render the raw output, just inspect the number of rows, so that we don't couple
   110  	// tightly with the logbook's internal representation.
   111  
   112  	// Save anoter dataset version
   113  	err = run.ExecCommand("qri remove --all me/log_test")
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  
   118  	// Save anoter dataset version
   119  	err = run.ExecCommand("qri save --body=testdata/movies/body_ten.csv me/log_test")
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  
   124  	// Log should only have the new version
   125  	err = run.ExecCommand("qri log me/log_test")
   126  	if err != nil {
   127  		t.Fatal(err)
   128  	}
   129  	output = run.GetCommandOutput()
   130  	expect = dstest.Template(t, `1   Commit:  {{ .path }}
   131      Date:    Sun Dec 31 20:03:01 EST 2000
   132      Storage: local
   133      Size:    224 B
   134  
   135      created dataset from body_ten.csv
   136  
   137  `, map[string]string{
   138  		"path": "/ipfs/QmQBGw3fgPUd4PsuUQc2QRdcK3VuEXyzzQ1JrRKaVTZHgB",
   139  	})
   140  
   141  	if diff := cmpTextLines(expect, output); diff != "" {
   142  		t.Errorf("qri log (-want +got):\n%s", diff)
   143  	}
   144  
   145  	// TODO(dlong): Get the logbook, verify that it contains 2 books. The first should end
   146  	// with a deletion, the second should have only two entries (1 init, 1 save).
   147  }
   148  
   149  func cmpTextLines(left, right string) string {
   150  	lside := strings.Split(left, "\n")
   151  	rside := strings.Split(right, "\n")
   152  	return cmp.Diff(lside, rside)
   153  }