|
| 1 | +package lib |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/ghodss/yaml" |
| 12 | + "github.com/qri-io/dataset" |
| 13 | + "github.com/qri-io/qri/config" |
| 14 | + "github.com/qri-io/qri/p2p" |
| 15 | + testrepo "github.com/qri-io/qri/repo/test" |
| 16 | + regmock "github.com/qri-io/registry/regserver/mock" |
| 17 | +) |
| 18 | + |
| 19 | +func TestExport(t *testing.T) { |
| 20 | + |
| 21 | + rc, _ := regmock.NewMockServer() |
| 22 | + mr, err := testrepo.NewTestRepo(rc) |
| 23 | + if err != nil { |
| 24 | + t.Fatalf("error allocating test repo: %s", err.Error()) |
| 25 | + } |
| 26 | + |
| 27 | + node, err := p2p.NewQriNode(mr, config.DefaultP2PForTesting()) |
| 28 | + if err != nil { |
| 29 | + t.Fatal(err.Error()) |
| 30 | + } |
| 31 | + req := NewExportRequests(node, nil) |
| 32 | + |
| 33 | + var fileWritten string |
| 34 | + |
| 35 | + tmpDir, err := ioutil.TempDir(os.TempDir(), "export") |
| 36 | + if err != nil { |
| 37 | + t.Fatal(err) |
| 38 | + } |
| 39 | + |
| 40 | + cases := []struct { |
| 41 | + description string |
| 42 | + params ExportParams |
| 43 | + output string // error or output filename |
| 44 | + }{ |
| 45 | + {"empty ref error", ExportParams{}, "repo: empty dataset reference"}, |
| 46 | + |
| 47 | + {"export default", ExportParams{Ref: "peer/movies"}, |
| 48 | + "peer-movies_-_0001-01-01-00-00-00.json"}, |
| 49 | + |
| 50 | + {"export yaml", ExportParams{Ref: "peer/movies", Format: "yaml"}, |
| 51 | + "peer-movies_-_0001-01-01-00-00-00.yaml"}, |
| 52 | + |
| 53 | + {"set output name", ExportParams{Ref: "peer/movies", Output: "ds.json"}, "ds.json"}, |
| 54 | + |
| 55 | + {"already exists", ExportParams{Ref: "peer/movies", Output: "ds.json"}, |
| 56 | + "already exists: \"ds.json\""}, |
| 57 | + |
| 58 | + {"set output name, yaml", ExportParams{Ref: "peer/movies", Output: "ds.yaml"}, "ds.yaml"}, |
| 59 | + |
| 60 | + {"output to directory", ExportParams{Ref: "peer/cities", Output: "./"}, |
| 61 | + "peer-cities_-_0001-01-01-00-00-00.json"}, |
| 62 | + |
| 63 | + {"export xlsx", ExportParams{Ref: "peer/movies", Format: "xlsx"}, |
| 64 | + "peer-movies_-_0001-01-01-00-00-00.xlsx"}, |
| 65 | + } |
| 66 | + |
| 67 | + for _, c := range cases { |
| 68 | + c.params.TargetDir = tmpDir |
| 69 | + err := req.Export(&c.params, &fileWritten) |
| 70 | + |
| 71 | + if err != nil { |
| 72 | + if c.output != err.Error() { |
| 73 | + t.Errorf("case \"%s\" error mismatch: expected: \"%s\", got: \"%s\"", |
| 74 | + c.description, c.output, err) |
| 75 | + } |
| 76 | + continue |
| 77 | + } |
| 78 | + |
| 79 | + if c.output != fileWritten { |
| 80 | + t.Errorf("case \"%s\" incorrect output: expected: \"%s\", got: \"%s\"", |
| 81 | + c.description, c.output, fileWritten) |
| 82 | + } |
| 83 | + |
| 84 | + var ds dataset.Dataset |
| 85 | + err = readDataset(filepath.Join(c.params.TargetDir, fileWritten), &ds) |
| 86 | + if err != nil { |
| 87 | + if err.Error() == "SKIP" { |
| 88 | + continue |
| 89 | + } |
| 90 | + t.Fatal(err) |
| 91 | + } |
| 92 | + |
| 93 | + ref := fmt.Sprintf("%s/%s", ds.Peername, ds.Name) |
| 94 | + if c.params.Ref != ref { |
| 95 | + t.Errorf("case \"%s\" mismatched ds name: expected: \"%s\", got: \"%s\"", |
| 96 | + c.description, c.params.Ref, ref) |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func readDataset(path string, ds *dataset.Dataset) error { |
| 102 | + file, err := os.Open(path) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + buffer, err := ioutil.ReadAll(file) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + ext := filepath.Ext(path) |
| 112 | + switch ext { |
| 113 | + case ".json": |
| 114 | + err = json.Unmarshal(buffer, ds) |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + case ".yaml": |
| 119 | + err = yaml.Unmarshal(buffer, ds) |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + case ".xlsx": |
| 124 | + return fmt.Errorf("SKIP") |
| 125 | + default: |
| 126 | + return fmt.Errorf("unknown format: %s", ext) |
| 127 | + } |
| 128 | + |
| 129 | + return nil |
| 130 | +} |
0 commit comments