Skip to content

Commit

Permalink
test(save): Command-line tests for basic save functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
dustmop committed Mar 19, 2020
1 parent a9ed295 commit c93394a
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 3 deletions.
4 changes: 1 addition & 3 deletions cmd/fsi_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,7 @@ func TestCheckoutWithStructure(t *testing.T) {
modifyFileUsingStringReplace("body.csv", "Avatar", "The Avengers")

// Status again, check that the body is changed.
run.MustExec(t, "qri status")

output = run.GetCommandOutput()
output = run.MustExec(t, "qri status")
expect := `for linked dataset [test_peer/ten_movies]
modified: body (source: body.csv)
Expand Down
100 changes: 100 additions & 0 deletions cmd/save_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package cmd

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -199,6 +202,92 @@ func TestSaveRun(t *testing.T) {
}
}

func TestSaveBasicCommands(t *testing.T) {
pwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
tmpPath, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer func() {
os.Chdir(pwd)
os.RemoveAll(tmpPath)
}()

// Copy some files into tmpPath, change to it
copyFile(t, "testdata/movies/ds_ten.yaml", filepath.Join(tmpPath, "dataset.yaml"))
copyFile(t, "testdata/movies/body_ten.csv", filepath.Join(tmpPath, "body_ten.csv"))
copyFile(t, "testdata/movies/structure_override.json", filepath.Join(tmpPath, "structure.json"))
os.Chdir(tmpPath)

goodCases := []struct {
description string
command string
expect string
}{
{
"dataset file infer name",
"qri save --file dataset.yaml",
"dataset saved: test_peer/ten_movies@/ipfs/QmU935SkFafE786u7jQPknB858PDrJRFUJboxbS4vKkAe7\nthis dataset has 1 validation errors\n",
},
{
"dataset file me ref",
"qri save --file dataset.yaml me/my_dataset",
"dataset saved: test_peer/my_dataset@/ipfs/QmSSTAcuehjFFiHNBFicTXJv2uhbKg4hjGgCL8W46D3jGG\nthis dataset has 1 validation errors\n",
},
{
"dataset file explicit ref",
"qri save --file dataset.yaml test_peer/my_dataset",
"dataset saved: test_peer/my_dataset@/ipfs/QmeW1M8W9yj69pxE3uJZbYVBCaEARYkrkje3NHY1s6tHtP\nthis dataset has 1 validation errors\n",
},
{
"body file infer name",
"qri save --body body_ten.csv",
"dataset saved: test_peer/body_tencsv@/ipfs/QmWaS8ndeAhAWWSUmwJ6HiXKMNWnsL1LYhzrWJyzCgR9pd\nthis dataset has 1 validation errors\n",
},
{
"body file me ref",
"qri save --body body_ten.csv me/my_dataset",
"dataset saved: test_peer/my_dataset@/ipfs/QmeyznW4FBaLY8rk5xpZyFHXixrq8qFJrFf8v1REZzmLLg\nthis dataset has 1 validation errors\n",
},
{
"body file explicit ref",
"qri save --body body_ten.csv test_peer/my_dataset",
"dataset saved: test_peer/my_dataset@/ipfs/QmP9yay2KwPnyDLLhGXZsLdKG31qMAR3CSBAmscz3znC9C\nthis dataset has 1 validation errors\n",
},
// TODO(dustmop): It's intended that a user can save a dataset with a structure but no
// body. At some point that functionality broke, because there was no test for it. Fix that
// in a follow-up change.
//{
// "structure file me ref",
// "qri save --file structure.json me/my_dataset",
// "TODO(dustmop): Should be possible to save a dataset with structure and no body",
//},
//{
// "structure file explicit ref",
// "qri save --file structure.json test_peer/my_dataset",
// "TODO(dustmop): Should be possible to save a dataset with structure and no body",
//},
}
for _, c := range goodCases {
// TODO(dustmop): Would be preferable to instead have a way to clear the refstore
run := NewTestRunner(t, "test_peer", "qri_test_save_basic")
defer run.Delete()

err := run.ExecCommand(c.command)
if err != nil {
t.Errorf("%s: error %s\n", c.description, err)
continue
}
actual := parseDatasetRefFromOutput(run.GetCommandOutput())
if diff := cmp.Diff(c.expect, actual); diff != "" {
t.Errorf("%s: result mismatch (-want +got):%s\n", c.description, diff)
}
}
}

func TestSaveInferName(t *testing.T) {
run := NewTestRunner(t, "test_peer", "qri_test_save_infer_name")
defer run.Delete()
Expand Down Expand Up @@ -410,3 +499,14 @@ func parseDatasetRefFromOutput(text string) string {
}
return text[pos:]
}

func copyFile(t *testing.T, source, destin string) {
data, err := ioutil.ReadFile(source)
if err != nil {
t.Fatal(err)
}
err = ioutil.WriteFile(destin, data, 0644)
if err != nil {
t.Fatal(err)
}
}

0 comments on commit c93394a

Please sign in to comment.