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

     1  package startf
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"io/ioutil"
     7  	"testing"
     8  
     9  	"github.com/google/go-cmp/cmp"
    10  	"github.com/qri-io/dataset"
    11  	"github.com/qri-io/dataset/tabular"
    12  	"github.com/qri-io/qfs"
    13  )
    14  
    15  func TestDatasetBodyGet(t *testing.T) {
    16  	bodyData := `cat,meow,5
    17  dog,bark,6
    18  eel,zap,7
    19  `
    20  	ds := &dataset.Dataset{
    21  		Name: "my_ds",
    22  		Structure: &dataset.Structure{
    23  			Format: "csv",
    24  			Schema: tabular.BaseTabularSchema,
    25  		},
    26  	}
    27  	ds.SetBodyFile(qfs.NewMemfileBytes("body.csv", []byte(bodyData)))
    28  	runExecScript(t, ds, "testdata/dataset_body_get.star", "testdata/dataset_body_get.expect.txt")
    29  }
    30  
    31  func TestDatasetBodySet(t *testing.T) {
    32  	ds := &dataset.Dataset{
    33  		Name: "my_ds",
    34  		Structure: &dataset.Structure{
    35  			Format: "csv",
    36  			Schema: tabular.BaseTabularSchema,
    37  		},
    38  	}
    39  	runExecScript(t, ds, "testdata/dataset_body_set.star", "testdata/dataset_body_set.expect.txt")
    40  }
    41  
    42  func runExecScript(t *testing.T, ds *dataset.Dataset, scriptFilename, expectFilename string) {
    43  	ctx := context.Background()
    44  
    45  	ds.Transform = &dataset.Transform{}
    46  	ds.Transform.Syntax = "starlark"
    47  	ds.Transform.SetScriptFile(scriptFile(t, scriptFilename))
    48  
    49  	// Run the script and capture its print output
    50  	buf := &bytes.Buffer{}
    51  	err := ExecScript(ctx, ds, SetErrWriter(buf))
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	actual := string(buf.Bytes())
    56  
    57  	// Compare the actual output to the expected text
    58  	expectBytes, err := ioutil.ReadFile(expectFilename)
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	expect := string(expectBytes)
    63  	if diff := cmp.Diff(expect, actual); diff != "" {
    64  		t.Errorf("mismatch. (-want +got):\n%s", diff)
    65  	}
    66  }