Skip to content

Commit

Permalink
fix(Save): add tests to cmd that test you can save a transform and viz (
Browse files Browse the repository at this point in the history
#649)

Tests ensure you can savec a Viz or Transform to a dataset without having to add other dataset components as well.

Also, fixes to `prepareViz()` to ensure that we have a `Viz.Script` before creating the datasets
  • Loading branch information
ramfox authored Jan 8, 2019
1 parent e8d09bc commit 42410a5
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 16 deletions.
38 changes: 24 additions & 14 deletions base/viz.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package base

import (
"bytes"
"os"
"strings"

Expand All @@ -18,21 +19,30 @@ func prepareViz(r repo.Repo, ds *dataset.Dataset) (err error) {
return nil
}

if ds.Viz != nil && ds.Viz.ScriptPath != "" {
if strings.HasPrefix(ds.Viz.ScriptPath, "/ipfs") || strings.HasPrefix(ds.Viz.ScriptPath, "/map") || strings.HasPrefix(ds.Viz.ScriptPath, "/cafs") {
var f cafs.File
f, err = r.Store().Get(datastore.NewKey(ds.Viz.ScriptPath))
if err != nil {
return
}
ds.Viz.Script = f
} else {
var f *os.File
f, err = os.Open(ds.Viz.ScriptPath)
if err != nil {
return
if ds.Viz != nil {
if ds.Viz.Script != nil {
return
}
if ds.Viz.ScriptBytes != nil {
ds.Viz.Script = bytes.NewReader(ds.Viz.ScriptBytes)
return
}
if ds.Viz.ScriptPath != "" {
if strings.HasPrefix(ds.Viz.ScriptPath, "/ipfs") || strings.HasPrefix(ds.Viz.ScriptPath, "/map") || strings.HasPrefix(ds.Viz.ScriptPath, "/cafs") {
var f cafs.File
f, err = r.Store().Get(datastore.NewKey(ds.Viz.ScriptPath))
if err != nil {
return
}
ds.Viz.Script = f
} else {
var f *os.File
f, err = os.Open(ds.Viz.ScriptPath)
if err != nil {
return
}
ds.Viz.Script = f
}
ds.Viz.Script = f
}
}
return nil
Expand Down
14 changes: 12 additions & 2 deletions cmd/save_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,22 @@ func TestSaveRun(t *testing.T) {
err string
msg string
}{
// no data
{"me/bad_dataset", "", "", "", "", false, "", "no changes to save", ""},
// bad dataset file
{"me/cities", "bad/filpath.json", "", "", "", false, "", "open bad/filpath.json: no such file or directory", ""},
// bad body file
{"me/cities", "", "bad/bodypath.csv", "", "", false, "", "body file: open bad/bodypath.csv: no such file or directory", ""},
// good inputs
{"me/movies", "testdata/movies/dataset.json", "testdata/movies/body_ten.csv", "", "", true, "dataset saved: peer/movies@QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt/map/QmQK6mkfu9hvDXrFP7uKkx737DHzASZffyAKnuDaTx8Vqy\nthis dataset has 1 validation errors\n", "", ""},
{"me/movies", "", "testdata/movies/body_twenty.csv", "Added 10 more rows", "Adding to the number of rows in dataset", true, "dataset saved: peer/movies@QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt/map/QmaKAexWMNGWSHet7yW2rCRx6qvESd7mPUqM1v2eRHXz3r\nthis dataset has 1 validation errors\n", "", ""},
{"me/movies", "", "testdata/movies/body_twenty.csv", "trying to add again", "hopefully this errors", false, "", "error saving: no changes detected", ""},
// add rows, save
{"me/movies", "testdata/movies/dataset.json", "testdata/movies/body_twenty.csv", "Added 10 more rows", "Adding to the number of rows in dataset", true, "dataset saved: peer/movies@QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt/map/QmPwBVhCaKH5sBc8amByRcyGHeT4JMWWnpcfawxUx36T7d\nthis dataset has 1 validation errors\n", "", ""},
// no changes detected
{"me/movies", "testdata/movies/dataset.json", "testdata/movies/body_twenty.csv", "trying to add again", "hopefully this errors", false, "", "error saving: no changes detected", ""},
// add viz
{"me/movies", "testdata/movies/dataset_with_viz.json", "", "", "", false, "dataset saved: peer/movies@QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt/map/QmVrSNgJqJNof7qeZVsdZp9FpnmKPRVg4VY7AmY5iFvgPy\nthis dataset has 1 validation errors\n", "", ""},
// add transform
{"me/movies", "testdata/movies/dataset_with_tf.json", "", "", "", false, "dataset saved: peer/movies@QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt/map/QmRSTsTSGirzxzWpKgbku66WSVFV3qNn3EZE3zSkZeh59u\nthis dataset has 1 validation errors\n", "", ""},
}

for i, c := range cases {
Expand Down
33 changes: 33 additions & 0 deletions cmd/testdata/movies/dataset_with_tf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"qri": "ds:0",
"meta": {
"qri": "md:0",
"title": "example movie data"
},
"structure": {
"qri": "st:0",
"format": "csv",
"formatConfig": {
"headerRow": true
},
"schema": {
"type": "array",
"items": {
"type": "array",
"items": [
{
"title": "title",
"type": "string"
},
{
"title": "duration",
"type": "integer"
}
]
}
}
},
"transform": {
"scriptPath": "tf.star"
}
}
33 changes: 33 additions & 0 deletions cmd/testdata/movies/dataset_with_viz.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"qri": "ds:0",
"meta": {
"qri": "md:0",
"title": "example movie data"
},
"structure": {
"qri": "st:0",
"format": "csv",
"formatConfig": {
"headerRow": true
},
"schema": {
"type": "array",
"items": {
"type": "array",
"items": [
{
"title": "title",
"type": "string"
},
{
"title": "duration",
"type": "integer"
}
]
}
}
},
"viz": {
"scriptPath": "viz.html"
}
}
3 changes: 3 additions & 0 deletions cmd/testdata/movies/tf.star
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# transform that doesn't change anything.
def transform(ds, ctx):
print("hello Qri! \n")
19 changes: 19 additions & 0 deletions cmd/testdata/movies/viz.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>My Viz Template</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { margin: 0; padding: 0; font-family: helvetica, sans-serif; }
</style>
</head>
<body>
<div style="width: 100%; margin: 50px auto">
<h1>{{ .Meta.Title }}</h1>
<p>{{ .Meta.Description }}</p>
<small>Data Format: <b>{{ .Structure.Format }}</b></small>
</div>
</body>
</html>

0 comments on commit 42410a5

Please sign in to comment.