github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/transform/startf/entry_reader_test.go (about)

     1  package startf
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/qri-io/dataset"
     8  	"github.com/qri-io/dataset/dsio"
     9  	"go.starlark.net/starlark"
    10  )
    11  
    12  // assert *EntryReader conforms to dsio.EntryReader interface
    13  var _ dsio.EntryReader = (*EntryReader)(nil)
    14  
    15  func TestEntryReaderSimpleList(t *testing.T) {
    16  	var elems *starlark.List
    17  	elems = starlark.NewList([]starlark.Value{})
    18  	elems.Append(starlark.MakeInt(1))
    19  	elems.Append(starlark.MakeInt(2))
    20  	elems.Append(starlark.MakeInt(3))
    21  	st := &dataset.Structure{
    22  		Schema: dataset.BaseSchemaArray,
    23  	}
    24  	r := NewEntryReader(st, elems)
    25  
    26  	expect := []struct {
    27  		index int
    28  		key   string
    29  		value string
    30  	}{
    31  		{0, "", "1"},
    32  		{1, "", "2"},
    33  		{2, "", "3"},
    34  	}
    35  
    36  	for i, e := range expect {
    37  		ent, err := r.ReadEntry()
    38  		if err != nil {
    39  			t.Fatal(err)
    40  		}
    41  
    42  		if e.index != ent.Index {
    43  			t.Errorf("case %d: index did not match, expect: %d, actual: %d", i, e.index, ent.Index)
    44  		}
    45  		if e.key != ent.Key {
    46  			t.Errorf("case %d: key did not match, expect: %s, actual: %s", i, e.key, ent.Key)
    47  		}
    48  		val := fmt.Sprintf("%v", ent.Value)
    49  		if e.value != val {
    50  			t.Errorf("case %d: value did not match, expect: %s, actual: %s", i, e.value, val)
    51  		}
    52  	}
    53  }
    54  
    55  func TestEntryReaderSimpleDict(t *testing.T) {
    56  	var elems *starlark.Dict
    57  	elems = &starlark.Dict{}
    58  	elems.SetKey(starlark.String("a"), starlark.MakeInt(1))
    59  	elems.SetKey(starlark.String("b"), starlark.MakeInt(2))
    60  	elems.SetKey(starlark.String("c"), starlark.MakeInt(3))
    61  	st := &dataset.Structure{
    62  		Schema: dataset.BaseSchemaObject,
    63  	}
    64  	r := NewEntryReader(st, elems)
    65  
    66  	expect := []struct {
    67  		index int
    68  		key   string
    69  		value string
    70  	}{
    71  		{0, "a", "1"},
    72  		{0, "b", "2"},
    73  		{0, "c", "3"},
    74  	}
    75  
    76  	for i, e := range expect {
    77  		ent, err := r.ReadEntry()
    78  		if err != nil {
    79  			t.Fatal(err)
    80  		}
    81  
    82  		if e.index != ent.Index {
    83  			t.Errorf("case %d: index did not match, expect: %d, actual: %d", i, e.index, ent.Index)
    84  		}
    85  		if e.key != ent.Key {
    86  			t.Errorf("case %d: key did not match, expect: %s, actual: %s", i, e.key, ent.Key)
    87  		}
    88  		val := fmt.Sprintf("%v", ent.Value)
    89  		if e.value != val {
    90  			t.Errorf("case %d: value did not match, expect: %s, actual: %s", i, e.value, val)
    91  		}
    92  	}
    93  }