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

     1  package cmd
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/qri-io/qri/errors"
     8  )
     9  
    10  func TestFullFieldToAbbr(t *testing.T) {
    11  	cases := []struct {
    12  		field, exp string
    13  	}{
    14  		{"commit", "cm"},
    15  		{"structure", "st"},
    16  		{"body", "bd"},
    17  		{"meta", "md"},
    18  		{"viz", "vz"},
    19  		{"transform", "tf"},
    20  		{"rendered", "rd"},
    21  		{"foo", "foo"},
    22  	}
    23  
    24  	for i, c := range cases {
    25  		got := fullFieldToAbbr(c.field)
    26  		if got != c.exp {
    27  			t.Errorf("case %d, for field '%s', expected '%s'. got '%s'", i, c.field, c.exp, got)
    28  		}
    29  	}
    30  }
    31  
    32  func TestAbbrFieldToFull(t *testing.T) {
    33  	cases := []struct {
    34  		field, exp string
    35  	}{
    36  		{"cm", "commit"},
    37  		{"st", "structure"},
    38  		{"bd", "body"},
    39  		{"md", "meta"},
    40  		{"vz", "viz"},
    41  		{"tf", "transform"},
    42  		{"rd", "rendered"},
    43  		{"foo", "foo"},
    44  	}
    45  
    46  	for i, c := range cases {
    47  		got := abbrFieldToFull(c.field)
    48  		if got != c.exp {
    49  			t.Errorf("case %d, for field '%s', expected '%s'. got '%s'", i, c.field, c.exp, got)
    50  		}
    51  	}
    52  }
    53  
    54  func TestDAGComplete(t *testing.T) {
    55  	run := NewTestRunner(t, "test_peer_dag_complete", "qri_test_dag_complete")
    56  	defer run.Delete()
    57  
    58  	ctx, cancel := context.WithCancel(context.Background())
    59  	defer cancel()
    60  
    61  	f, err := NewTestFactory(ctx)
    62  	if err != nil {
    63  		t.Errorf("error creating new test factory: %s", err)
    64  		return
    65  	}
    66  
    67  	cases := []struct {
    68  		args, expRefs []string
    69  		label, err    string
    70  	}{
    71  		{[]string{}, []string{}, "", ""},
    72  		{[]string{"dataset/ref"}, []string{"dataset/ref"}, "", ""},
    73  		{[]string{"bad_label dataset/ref"}, []string{"bad_label dataset/ref"}, "", ""},
    74  		{[]string{"meta"}, []string{}, "md", ""},
    75  		{[]string{"structure", "dataset/ref"}, []string{"dataset/ref"}, "st", ""},
    76  		{[]string{"vz", "dataset/ref"}, []string{"dataset/ref"}, "vz", ""},
    77  	}
    78  	for i, c := range cases {
    79  		opt := &DAGOptions{
    80  			IOStreams: run.Streams,
    81  		}
    82  
    83  		opt.Complete(f, c.args, true)
    84  		if c.err != run.ErrStream.String() {
    85  			t.Errorf("case %d, error mismatch. Expected: '%s', Got: '%s'", i, c.err, run.ErrStream.String())
    86  			run.IOReset()
    87  			continue
    88  		}
    89  
    90  		if opt.inst == nil {
    91  			t.Errorf("case %d, opt.inst not set.", i)
    92  		}
    93  
    94  		if opt.Label != c.label {
    95  			t.Errorf("case %d, label mismatch. Expected: '%s', Got: '%s'", i, c.label, opt.Label)
    96  		}
    97  
    98  		if len(opt.Refs) != len(c.expRefs) {
    99  			t.Errorf("case %d, expected Refs mismatch. Expected: %s, Got: %s", i, c.expRefs, opt.Refs)
   100  			run.IOReset()
   101  			continue
   102  		}
   103  
   104  		for i, ref := range c.expRefs {
   105  			if opt.Refs[i] != ref {
   106  				t.Errorf("case %d, expected Refs mismatch. Expected: %s, Got: %s", i, c.expRefs, opt.Refs)
   107  				break
   108  			}
   109  		}
   110  		run.IOReset()
   111  	}
   112  }
   113  
   114  func TestDAGInfo(t *testing.T) {
   115  	run := NewTestRunner(t, "test_peer_dag_info", "qri_test_dag_info")
   116  	defer run.Delete()
   117  
   118  	ctx, cancel := context.WithCancel(context.Background())
   119  	defer cancel()
   120  
   121  	f, err := NewTestFactory(ctx)
   122  	if err != nil {
   123  		t.Errorf("error creating new test factory: %s", err)
   124  		return
   125  	}
   126  
   127  	cases := []struct {
   128  		description string
   129  		opt         *DAGOptions
   130  		stdout      string
   131  		err         string
   132  		errMsg      string
   133  	}{
   134  		{"dag with no options",
   135  			&DAGOptions{},
   136  			"",
   137  			"dataset reference required",
   138  			"",
   139  		},
   140  		// TODO (ramfox): blocked. Need a test subpackage in the dag package
   141  		// that mocks the NodeGetter for testing purposes
   142  		// {"dag info at a reference",
   143  		// 	&DAGOptions{Refs: []string{"me/movies"}},
   144  		// 	"0 elements. 0 insxrts. 0 deletes. 1 update.\n\n~ title: \"example city data\"\n",
   145  		// 	"", "",
   146  		// },
   147  		//     {"diff json output",
   148  		//       &DiffOptions{Left: "me/movies", Right: "me/cities", Selector: "meta", Format: "json"},
   149  		//       `[{"type":"update","path":"/title","value":"example city data","originalValue":"example movie data"}]
   150  		// `,
   151  		//       "", "",
   152  		//     },
   153  	}
   154  	for i, c := range cases {
   155  		inst, err := f.Instance()
   156  		if err != nil {
   157  			t.Errorf("case %d, error creating inst: %s", i, err)
   158  			continue
   159  		}
   160  
   161  		opt := c.opt
   162  		opt.IOStreams = run.Streams
   163  		opt.inst = inst
   164  
   165  		err = opt.Info()
   166  		if (err == nil && c.err != "") || (err != nil && c.err != err.Error()) {
   167  			t.Errorf("case %d, mismatched error. Expected: '%s', Got: '%v'", i, c.err, err)
   168  			run.IOReset()
   169  			continue
   170  		}
   171  
   172  		if libErr, ok := err.(errors.Error); ok {
   173  			if libErr.Message() != c.errMsg {
   174  				t.Errorf("case %d, mismatched user-friendly message. Expected: '%s', Got: '%s'", i, c.errMsg, libErr.Message())
   175  				run.IOReset()
   176  				continue
   177  			}
   178  		} else if c.errMsg != "" {
   179  			t.Errorf("case %d, mismatched user-friendly message. Expected: '%s', Got: ''", i, c.errMsg)
   180  			run.IOReset()
   181  			continue
   182  		}
   183  
   184  		if c.stdout != run.OutStream.String() {
   185  			t.Errorf("case %d, output mismatch. Expected: '%s', Got: '%s'", i, c.stdout, run.OutStream.String())
   186  			run.IOReset()
   187  			continue
   188  		}
   189  		run.IOReset()
   190  	}
   191  }