Skip to content

Commit

Permalink
fix(transform): Pass prev dataset to ExecScript
Browse files Browse the repository at this point in the history
Pass prev dataset as well as next dataset, in order to support new get_body functionality added by qri-io/startf#41.
  • Loading branch information
dustmop committed May 7, 2019
1 parent cff24f9 commit 6ff666c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 7 deletions.
2 changes: 1 addition & 1 deletion actions/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func SaveDataset(node *p2p.QriNode, changes *dataset.Dataset, secrets map[string
mutateCheck := mutatedComponentsFunc(changes)

changes.Transform.Secrets = secrets
if err = ExecTransform(node, changes, scriptOut, mutateCheck); err != nil {
if err = ExecTransform(node, changes, prev, scriptOut, mutateCheck); err != nil {
return
}
// changes.Transform.SetScriptFile(mutable.Transform.ScriptFile())
Expand Down
4 changes: 2 additions & 2 deletions actions/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ please adjust either the transform script or remove the supplied '%s'`, path[0],
}

// ExecTransform executes a designated transformation
func ExecTransform(node *p2p.QriNode, ds *dataset.Dataset, scriptOut io.Writer, mutateCheck func(...string) error) error {
func ExecTransform(node *p2p.QriNode, ds, prev *dataset.Dataset, scriptOut io.Writer, mutateCheck func(...string) error) error {
if ds.Transform == nil {
return fmt.Errorf("no transform provided")
}
Expand All @@ -64,7 +64,7 @@ func ExecTransform(node *p2p.QriNode, ds *dataset.Dataset, scriptOut io.Writer,
setSecrets,
}

if err := startf.ExecScript(ds, configs...); err != nil {
if err := startf.ExecScript(ds, prev, configs...); err != nil {
return err
}

Expand Down
8 changes: 5 additions & 3 deletions actions/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ func TestExecTransform(t *testing.T) {
t.Fatal(err.Error())
}

ds := &dataset.Dataset{
prev := &dataset.Dataset{
Structure: &dataset.Structure{
Format: "json",
Schema: dataset.BaseSchemaArray,
},
}
next := &dataset.Dataset{
Transform: &dataset.Transform{
Syntax: "starlark",
Config: map[string]interface{}{"foo": "config"},
Expand All @@ -45,9 +47,9 @@ def transform(ds,ctx):
ctx.get_secret("bar")
ds.set_body([1,2,3])
`)
ds.Transform.SetScriptFile(qfs.NewMemfileBytes("transform.star", data))
next.Transform.SetScriptFile(qfs.NewMemfileBytes("transform.star", data))

if err := ExecTransform(node, ds, nil, nil); err != nil {
if err := ExecTransform(node, next, prev, nil, nil); err != nil {
t.Error(err.Error())
}
}
38 changes: 38 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,44 @@ func TestSaveTransformWithoutChanges(t *testing.T) {
}
}

// Test that calling `get_body` will retrieve the body of the previous version.
func TestTransformUsingGetBodyAndSetBody(t *testing.T) {
if err := confirmQriNotRunning(); err != nil {
t.Skip(err.Error())
}

// To keep hashes consistent, artificially specify the timestamp by overriding
// the dsfs.Timestamp func
prev := dsfs.Timestamp
defer func() { dsfs.Timestamp = prev }()
dsfs.Timestamp = func() time.Time { return time.Date(2001, 01, 01, 01, 01, 01, 01, time.UTC) }

r := NewTestRepoRoot(t, "qri_test_save_transform_get_body")
defer r.Delete()

cmdR := r.CreateCommandRunner()
_, err := executeCommand(cmdR, "qri save --body=testdata/movies/body_two.json me/test_ds")
if err != nil {
t.Fatalf(err.Error())
}

cmdR = r.CreateCommandRunner()
_, err = executeCommand(cmdR, "qri save --file=testdata/movies/tf_add_one.star me/test_ds")
if err != nil {
t.Fatalf(err.Error())
}

// Read body from the dataset that was created with the transform
dsPath := r.GetPathForDataset(0)
actualBody := r.ReadBodyFromIPFS(dsPath + "/body.json")

// This body is body_two.json, with the numbers in the second column increased by 1.
expectBody := `[["Avatar",179],["Pirates of the Caribbean: At World's End",170]]`
if actualBody != expectBody {
t.Errorf("error, dataset actual:\n%s\nexpect:\n%s\n", actualBody, expectBody)
}
}

// TODO: Perhaps this utility should move to a lower package, and be used as a way to validate the
// bodies of dataset in more of our test case. That would require extracting some parts out, like
// pathFactory, which would probably necessitate the pathFactory taking the testRepoRoot as a
Expand Down
4 changes: 4 additions & 0 deletions cmd/testdata/movies/body_two.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
["Avatar", 178],
["Pirates of the Caribbean: At World's End", 169]
]
6 changes: 6 additions & 0 deletions cmd/testdata/movies/tf_add_one.star
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def transform(ds, ctx):
body = ds.get_body()
for row in body:
row[1] = row[1] + 1
ds.set_body(body)

1 change: 0 additions & 1 deletion lib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type ConfigMethods struct {
inst *Instance
}


// NewConfigMethods creates a configuration handle from an instance
func NewConfigMethods(inst *Instance) *ConfigMethods {
return &ConfigMethods{inst: inst}
Expand Down

0 comments on commit 6ff666c

Please sign in to comment.