From f3dea07d3847e4250271bb9bab18a0ef499fb235 Mon Sep 17 00:00:00 2001 From: b5 Date: Tue, 12 Dec 2017 10:22:48 -0500 Subject: [PATCH] fix: restoring tests, cleaning up post-ds.Transform refactor ) --- api/handlers/profile.go | 5 +- cmd/list.go | 19 ++++++- cmd/run.go | 3 +- core/history_test.go | 2 +- core/peers_test.go | 2 +- core/profile.go | 17 +++++- core/profile_test.go | 42 +++++++------- core/queries.go | 2 +- core/queries_test.go | 123 ++++++++++++++++++---------------------- repo/graph.go | 4 +- 10 files changed, 116 insertions(+), 103 deletions(-) diff --git a/api/handlers/profile.go b/api/handlers/profile.go index a4b31a3f4..c06ada2ab 100644 --- a/api/handlers/profile.go +++ b/api/handlers/profile.go @@ -8,7 +8,6 @@ import ( "github.com/qri-io/qri/core" "github.com/qri-io/qri/logging" "github.com/qri-io/qri/repo" - "github.com/qri-io/qri/repo/profile" ) // ProfileHandlers wraps a requests struct to interface with http.HandlerFunc @@ -91,7 +90,7 @@ func (h *ProfileHandlers) setProfilePhotoHandler(w http.ResponseWriter, r *http. } } - res := &profile.Profile{} + res := &core.Profile{} if err := h.SetProfilePhoto(p, res); err != nil { h.log.Infof("error initializing dataset: %s", err.Error()) util.WriteErrResponse(w, http.StatusInternalServerError, err) @@ -128,7 +127,7 @@ func (h *ProfileHandlers) setPosterHandler(w http.ResponseWriter, r *http.Reques } } - res := &profile.Profile{} + res := &core.Profile{} if err := h.SetPosterPhoto(p, res); err != nil { h.log.Infof("error initializing dataset: %s", err.Error()) util.WriteErrResponse(w, http.StatusInternalServerError, err) diff --git a/cmd/list.go b/cmd/list.go index 26336102c..e63df9b4d 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -4,9 +4,15 @@ import ( "encoding/json" "fmt" "github.com/qri-io/dataset" + "github.com/qri-io/qri/core" + "github.com/qri-io/qri/repo" "github.com/spf13/cobra" ) +var ( + dsListLimit, dsListOffset int +) + var datasetListCmd = &cobra.Command{ Use: "list", Aliases: []string{"ls"}, @@ -14,11 +20,18 @@ var datasetListCmd = &cobra.Command{ Long: ``, Run: func(cmd *cobra.Command, args []string) { // TODO - add limit & offset params - refs, err := GetRepo(false).Namespace(100, 0) + r, err := DatasetRequests(false) ExitIfErr(err) - outformat := cmd.Flag("format").Value.String() + p := &core.ListParams{ + Limit: dsListLimit, + Offset: dsListOffset, + } + refs := []*repo.DatasetRef{} + err = r.List(p, &refs) + ExitIfErr(err) + outformat := cmd.Flag("format").Value.String() switch outformat { case "": for _, ref := range refs { @@ -38,4 +51,6 @@ var datasetListCmd = &cobra.Command{ func init() { RootCmd.AddCommand(datasetListCmd) datasetListCmd.Flags().StringP("format", "f", "", "set output format [json]") + datasetListCmd.Flags().IntVarP(&dsListLimit, "limit", "l", 25, "limit results, default 25") + datasetListCmd.Flags().IntVarP(&dsListOffset, "offset", "o", 0, "offset results, default 0") } diff --git a/cmd/run.go b/cmd/run.go index 79bc3beb8..65f2d559e 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -27,8 +27,9 @@ var runCmd = &cobra.Command{ ErrExit(fmt.Errorf("Please provide a query string to execute")) } - req, err := QueryRequests(false) + r, cli, err := RepoOrClient(false) ExitIfErr(err) + req := core.NewQueryRequests(r, cli) format, err := dataset.ParseDataFormatString(cmd.Flag("format").Value.String()) if err != nil { diff --git a/core/history_test.go b/core/history_test.go index ecfd82fa7..47cedfe13 100644 --- a/core/history_test.go +++ b/core/history_test.go @@ -30,7 +30,7 @@ func TestHistoryRequestsLog(t *testing.T) { {&LogParams{Path: path}, []*repo.DatasetRef{&repo.DatasetRef{Path: path}}, ""}, } - req := NewHistoryRequests(mr) + req := NewHistoryRequests(mr, nil) for i, c := range cases { got := []*repo.DatasetRef{} err := req.Log(c.p, &got) diff --git a/core/peers_test.go b/core/peers_test.go index a9a7f8d69..97786338e 100644 --- a/core/peers_test.go +++ b/core/peers_test.go @@ -28,7 +28,7 @@ func TestPeerRequestsList(t *testing.T) { } // TODO - need to upgrade this to include a mock node - req := NewPeerRequests(mr, &p2p.QriNode{}) + req := NewPeerRequests(&p2p.QriNode{Repo: mr}, nil) for i, c := range cases { got := []*profile.Profile{} err := req.List(c.p, &got) diff --git a/core/profile.go b/core/profile.go index fb05673bc..a46839841 100644 --- a/core/profile.go +++ b/core/profile.go @@ -135,7 +135,7 @@ type FileParams struct { Data io.Reader // reader of structured data. either Url or Data is required } -func (r *ProfileRequests) SetProfilePhoto(p *FileParams, res *profile.Profile) error { +func (r *ProfileRequests) SetProfilePhoto(p *FileParams, res *Profile) error { if r.cli != nil { return r.cli.Call("ProfileRequests.SetProfilePhoto", p, res) } @@ -176,11 +176,16 @@ func (r *ProfileRequests) SetProfilePhoto(p *FileParams, res *profile.Profile) e return fmt.Errorf("error saving profile: %s", err.Error()) } - *res = *pro + _p, err := marshalProfile(pro) + if err != nil { + return err + } + + *res = *_p return nil } -func (r *ProfileRequests) SetPosterPhoto(p *FileParams, res *profile.Profile) error { +func (r *ProfileRequests) SetPosterPhoto(p *FileParams, res *Profile) error { if r.cli != nil { return r.cli.Call("ProfileRequests.SetPosterPhoto", p, res) } @@ -219,5 +224,11 @@ func (r *ProfileRequests) SetPosterPhoto(p *FileParams, res *profile.Profile) er return fmt.Errorf("error saving profile: %s", err.Error()) } + _p, err := marshalProfile(pro) + if err != nil { + return err + } + + *res = *_p return nil } diff --git a/core/profile_test.go b/core/profile_test.go index 20bd5fd6d..a8d14fcb4 100644 --- a/core/profile_test.go +++ b/core/profile_test.go @@ -26,9 +26,9 @@ func TestProfileRequestsGet(t *testing.T) { return } - req := NewProfileRequests(mr) + req := NewProfileRequests(mr, nil) for i, c := range cases { - got := &profile.Profile{} + got := &Profile{} err := req.GetProfile(&c.in, got) if !(err == nil && c.err == "" || err != nil && err.Error() == c.err) { @@ -40,12 +40,12 @@ func TestProfileRequestsGet(t *testing.T) { func TestProfileRequestsSave(t *testing.T) { cases := []struct { - p *profile.Profile - res *profile.Profile + p *Profile + res *Profile err string }{ {nil, nil, "profile required for update"}, - {&profile.Profile{}, nil, ""}, + {&Profile{}, nil, ""}, // TODO - moar tests } @@ -55,9 +55,9 @@ func TestProfileRequestsSave(t *testing.T) { return } - req := NewProfileRequests(mr) + req := NewProfileRequests(mr, nil) for i, c := range cases { - got := &profile.Profile{} + got := &Profile{} err := req.SaveProfile(c.p, got) if !(err == nil && c.err == "" || err != nil && err.Error() == c.err) { @@ -73,9 +73,9 @@ func TestProfileRequestsSetProfilePhoto(t *testing.T) { respath datastore.Key err string }{ - {"", datastore.Key{}, "file is required"}, - {"testdata/ink_big_photo.jpg", datastore.Key{}, "file size too large. max size is 250kb"}, - {"testdata/q_bang.svg", datastore.Key{}, "invalid file format. only .jpg & .png images allowed"}, + {"", datastore.NewKey(""), "file is required"}, + {"testdata/ink_big_photo.jpg", datastore.NewKey(""), "file size too large. max size is 250kb"}, + {"testdata/q_bang.svg", datastore.NewKey(""), "invalid file format. only .jpg & .png images allowed"}, {"testdata/rico_400x400.jpg", datastore.NewKey("/map/QmRdexT18WuAKVX3vPusqmJTWLeNSeJgjmMbaF5QLGHna1"), ""}, } @@ -85,7 +85,7 @@ func TestProfileRequestsSetProfilePhoto(t *testing.T) { return } - req := NewProfileRequests(mr) + req := NewProfileRequests(mr, nil) for i, c := range cases { p := &FileParams{} if c.infile != "" { @@ -98,15 +98,15 @@ func TestProfileRequestsSetProfilePhoto(t *testing.T) { p.Data = r } - res := &profile.Profile{} + res := &Profile{} err := req.SetProfilePhoto(p, res) if !(err == nil && c.err == "" || err != nil && err.Error() == c.err) { t.Errorf("case %d error mismatch. expected: %s, got: %s", i, c.err, err.Error()) continue } - if !c.respath.Equal(res.Profile) { - t.Errorf("case %d profile hash mismatch. expected: %s, got: %s", i, c.respath.String(), res.Profile.String()) + if !c.respath.Equal(datastore.NewKey(res.Profile)) { + t.Errorf("case %d profile hash mismatch. expected: %s, got: %s", i, c.respath.String(), res.Profile) continue } } @@ -118,9 +118,9 @@ func TestProfileRequestsSetPosterPhoto(t *testing.T) { respath datastore.Key err string }{ - {"", datastore.Key{}, "file is required"}, - {"testdata/ink_big_photo.jpg", datastore.Key{}, "file size too large. max size is 250kb"}, - {"testdata/q_bang.svg", datastore.Key{}, "invalid file format. only .jpg & .png images allowed"}, + {"", datastore.NewKey(""), "file is required"}, + {"testdata/ink_big_photo.jpg", datastore.NewKey(""), "file size too large. max size is 250kb"}, + {"testdata/q_bang.svg", datastore.NewKey(""), "invalid file format. only .jpg & .png images allowed"}, {"testdata/rico_poster_1500x500.jpg", datastore.NewKey("/map/QmdJgfxj4rocm88PLeEididS7V2cc9nQosA46RpvAnWvDL"), ""}, } @@ -130,7 +130,7 @@ func TestProfileRequestsSetPosterPhoto(t *testing.T) { return } - req := NewProfileRequests(mr) + req := NewProfileRequests(mr, nil) for i, c := range cases { p := &FileParams{} if c.infile != "" { @@ -143,15 +143,15 @@ func TestProfileRequestsSetPosterPhoto(t *testing.T) { p.Data = r } - res := &profile.Profile{} + res := &Profile{} err := req.SetProfilePhoto(p, res) if !(err == nil && c.err == "" || err != nil && err.Error() == c.err) { t.Errorf("case %d error mismatch. expected: %s, got: %s", i, c.err, err.Error()) continue } - if !c.respath.Equal(res.Profile) { - t.Errorf("case %d profile hash mismatch. expected: %s, got: %s", i, c.respath.String(), res.Profile.String()) + if !c.respath.Equal(datastore.NewKey(res.Profile)) { + t.Errorf("case %d profile hash mismatch. expected: %s, got: %s", i, c.respath.String(), res.Profile) continue } } diff --git a/core/queries.go b/core/queries.go index 8309694c7..a1169195f 100644 --- a/core/queries.go +++ b/core/queries.go @@ -166,7 +166,7 @@ func (r *QueryRequests) Run(p *RunParams, res *repo.DatasetRef) error { if err != nil { return fmt.Errorf("formatting error: %s", err.Error()) } - qpath, err := dsfs.SaveTransform(store, abst, false) + qpath, err := dsfs.SaveAbstractTransform(store, abst, false) if err != nil { return fmt.Errorf("error calculating query hash: %s", err.Error()) } diff --git a/core/queries_test.go b/core/queries_test.go index aeea58a99..b8c3bea44 100644 --- a/core/queries_test.go +++ b/core/queries_test.go @@ -2,7 +2,6 @@ package core import ( "encoding/json" - "fmt" "testing" "github.com/qri-io/dataset" @@ -17,7 +16,7 @@ func TestList(t *testing.T) { t.Errorf("error allocating test repo: %s", err.Error()) return } - req := NewQueryRequests(mr) + req := NewQueryRequests(mr, nil) if req == nil { t.Errorf("error: expected non-nil result from NewQueryRequests()") return @@ -48,7 +47,7 @@ func TestGet(t *testing.T) { t.Errorf("error allocating test repo: %s", err.Error()) return } - req := NewQueryRequests(mr) + req := NewQueryRequests(mr, nil) if req == nil { t.Errorf("error: expected non-nil result from NewQueryRequests()") @@ -81,7 +80,7 @@ func TestRun(t *testing.T) { return } - req := NewQueryRequests(mr) + req := NewQueryRequests(mr, nil) if req == nil { t.Errorf("error: expected non-nil result from NewQueryRequests()") @@ -108,7 +107,6 @@ func TestRun(t *testing.T) { } if c.err == "" { - fmt.Println("path:", got.Path.String()) df, err := mr.Store().Get(got.Path) if err != nil { t.Errorf("case %d error getting dataset path: %s: %s", i, got.Path.String(), err.Error()) @@ -130,72 +128,61 @@ func TestRun(t *testing.T) { if !ds.Structure.IsEmpty() { t.Errorf("expected stored dataset.Structure to be a reference") } - if !ds.AbstractStructure.IsEmpty() { - t.Errorf("expected stored dataset.AbstractStructure to be a reference") + if !ds.Abstract.IsEmpty() { + t.Errorf("expected stored dataset.Abstract to be a reference") } } } } -// TODO - RESTORE BEFORE MERGING -// func TestDatasetQueries(t *testing.T) { -// mr, err := testrepo.NewTestRepo() -// if err != nil { -// t.Errorf("error allocating test repo: %s", err.Error()) -// return -// } - -// req := NewQueryRequests(mr) - -// path, err := mr.GetPath("movies") -// if err != nil { -// t.Errorf("errog getting path for 'movies' dataset: %s", err.Error()) -// return -// } - -// // ns, err := mr.Namespace(30, 0) -// // if err != nil { -// // t.Errorf("error getting repo namespace: %s", err.Error()) -// // return -// // } - -// // for _, n := range ns { -// // fmt.Println(n) -// // } - -// qres := &repo.DatasetRef{} -// if err = req.Run(&RunParams{ -// Dataset: &dataset.Dataset{ -// QueryString: "select * from movies", -// }}, qres); err != nil { -// t.Errorf("error running query: %s", err.Error()) -// return -// } - -// cases := []struct { -// p *DatasetQueriesParams -// res []*repo.DatasetRef -// err string -// }{ -// {&DatasetQueriesParams{}, []*repo.DatasetRef{}, "path is required"}, -// {&DatasetQueriesParams{Path: path.String()}, []*repo.DatasetRef{&repo.DatasetRef{}}, ""}, -// // TODO: add more tests -// } - -// for i, c := range cases { -// got := []*repo.DatasetRef{} -// err := req.DatasetQueries(c.p, &got) -// if !(err == nil && c.err == "" || err != nil && err.Error() == c.err) { -// t.Errorf("case %d error mismatch: expected: %s, got: %s", i, c.err, err) -// continue -// } - -// // fmt.Println(got) - -// if len(c.res) != len(got) { -// t.Errorf("case %d returned wrong number of responses. exepected: %d, got %d", i, len(c.res), len(got)) -// continue -// } -// } -// } +func TestDatasetQueries(t *testing.T) { + mr, err := testrepo.NewTestRepo() + if err != nil { + t.Errorf("error allocating test repo: %s", err.Error()) + return + } + + req := NewQueryRequests(mr, nil) + + path, err := mr.GetPath("movies") + if err != nil { + t.Errorf("errog getting path for 'movies' dataset: %s", err.Error()) + return + } + + qres := &repo.DatasetRef{} + if err = req.Run(&RunParams{ + Dataset: &dataset.Dataset{ + QueryString: "select * from movies", + }}, qres); err != nil { + t.Errorf("error running query: %s", err.Error()) + return + } + + cases := []struct { + p *DatasetQueriesParams + res []*repo.DatasetRef + err string + }{ + {&DatasetQueriesParams{}, []*repo.DatasetRef{}, "path is required"}, + {&DatasetQueriesParams{Path: path.String()}, []*repo.DatasetRef{&repo.DatasetRef{}}, ""}, + // TODO: ALWAYS MOAR TESTS. OM NOM NOM FEED THE TEST MONSTER. + } + + for i, c := range cases { + got := []*repo.DatasetRef{} + err := req.DatasetQueries(c.p, &got) + if !(err == nil && c.err == "" || err != nil && err.Error() == c.err) { + t.Errorf("case %d error mismatch: expected: %s, got: %s", i, c.err, err) + continue + } + + // fmt.Println(got) + + if len(c.res) != len(got) { + t.Errorf("case %d returned wrong number of responses. exepected: %d, got %d", i, len(c.res), len(got)) + continue + } + } +} diff --git a/repo/graph.go b/repo/graph.go index 79f9dbc32..33c60852f 100644 --- a/repo/graph.go +++ b/repo/graph.go @@ -145,10 +145,10 @@ func (nl NodeList) nodesFromDatasetRef(r Repo, ref *DatasetRef) *dsgraph.Node { root.AddLinks(dsgraph.Link{From: root, To: commit}) } - if ds.AbstractStructure != nil && ds.AbstractStructure.Path().String() != "" { + if ds.Abstract != nil && ds.Abstract.Path().String() != "" { root.AddLinks(dsgraph.Link{ From: root, - To: nl.node(dsgraph.NtAbstStructure, ds.AbstractStructure.Path().String()), + To: nl.node(dsgraph.NtAbstDataset, ds.Abstract.Path().String()), }) }