-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(export): With no filename, write zip bytes to stdout or API
- Loading branch information
Showing
8 changed files
with
189 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/qri-io/dataset" | ||
"github.com/qri-io/qri/base/dsfs" | ||
"github.com/qri-io/qri/lib" | ||
"github.com/qri-io/qri/p2p" | ||
reporef "github.com/qri-io/qri/repo/ref" | ||
) | ||
|
||
type APITestRunner struct { | ||
Node *p2p.QriNode | ||
NodeTeardown func() | ||
Inst *lib.Instance | ||
DsfsTsFunc func() time.Time | ||
TmpDir string | ||
WorkDir string | ||
PrevXformVer string | ||
} | ||
|
||
func NewAPITestRunner(t *testing.T) *APITestRunner { | ||
run := APITestRunner{} | ||
run.Node, run.NodeTeardown = newTestNode(t) | ||
run.Inst = newTestInstanceWithProfileFromNode(run.Node) | ||
|
||
tmpDir, err := ioutil.TempDir("", "api_test") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
run.TmpDir = tmpDir | ||
|
||
counter := 0 | ||
run.DsfsTsFunc = dsfs.Timestamp | ||
dsfs.Timestamp = func() time.Time { | ||
counter++ | ||
return time.Date(2001, 01, 01, 01, counter, 01, 01, time.UTC) | ||
} | ||
|
||
run.PrevXformVer = APIVersion | ||
APIVersion = "test_version" | ||
|
||
return &run | ||
} | ||
|
||
func (r *APITestRunner) Delete() { | ||
os.RemoveAll(r.TmpDir) | ||
APIVersion = r.PrevXformVer | ||
r.NodeTeardown() | ||
} | ||
|
||
func (r *APITestRunner) MustMakeWorkDir(t *testing.T, name string) string { | ||
r.WorkDir = filepath.Join(r.TmpDir, name) | ||
if err := os.MkdirAll(r.WorkDir, os.ModePerm); err != nil { | ||
t.Fatal(err) | ||
} | ||
return r.WorkDir | ||
} | ||
|
||
func (r *APITestRunner) BuildDataset(dsName string) *dataset.Dataset { | ||
ds := dataset.Dataset{ | ||
Peername: "peer", | ||
Name: dsName, | ||
} | ||
return &ds | ||
} | ||
|
||
func (r *APITestRunner) SaveDataset(ds *dataset.Dataset, bodyFilename string) { | ||
dsm := lib.NewDatasetMethods(r.Inst) | ||
saveParams := lib.SaveParams{ | ||
Ref: fmt.Sprintf("peer/%s", ds.Name), | ||
Dataset: ds, | ||
BodyPath: bodyFilename, | ||
} | ||
res := reporef.DatasetRef{} | ||
if err := dsm.Save(&saveParams, &res); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func (r *APITestRunner) NewRenderHandlers() *RenderHandlers { | ||
return NewRenderHandlers(r.Inst) | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package api | ||
|
||
import ( | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/qri-io/dataset" | ||
) | ||
|
||
func TestGetZip(t *testing.T) { | ||
run := NewAPITestRunner(t) | ||
defer run.Delete() | ||
|
||
// Save a version of the dataset | ||
ds := run.BuildDataset("test_ds") | ||
ds.Meta = &dataset.Meta{Title: "some title"} | ||
ds.Readme = &dataset.Readme{ScriptBytes: []byte("# hi\n\nthis is a readme")} | ||
run.SaveDataset(ds, "testdata/cities/data.csv") | ||
|
||
// Get a zip file binary over the API | ||
dsHandler := NewDatasetHandlers(run.Inst, false) | ||
gotStatusCode, gotBodyString := APICall("/peer/test_ds?format=zip", dsHandler.GetHandler) | ||
if gotStatusCode != 200 { | ||
t.Fatalf("expected status code 200, got %d", gotStatusCode) | ||
} | ||
|
||
// Compare the API response to the expected zip file | ||
expectBytes, err := ioutil.ReadFile("testdata/cities/exported.zip") | ||
if err != nil { | ||
t.Fatalf("error reading expected bytes: %s", err) | ||
} | ||
if diff := cmp.Diff(string(expectBytes), gotBodyString); diff != "" { | ||
t.Errorf("byte mismatch (-want +got):\n%s", diff) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters