github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/base/archive/zip_test.go (about) 1 package archive 2 3 import ( 4 "archive/zip" 5 "bytes" 6 "context" 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 "sort" 11 "testing" 12 13 "github.com/google/go-cmp/cmp" 14 "github.com/qri-io/qri/base" 15 "github.com/qri-io/qri/base/dsfs" 16 "github.com/qri-io/qri/dsref" 17 ) 18 19 var blankInitID = "" 20 21 func TestWriteZip(t *testing.T) { 22 ctx := context.Background() 23 fs, names, err := testFS() 24 if err != nil { 25 t.Errorf("error creating store: %s", err.Error()) 26 return 27 } 28 29 ds, err := dsfs.LoadDataset(ctx, fs, names["movies"]) 30 if err != nil { 31 t.Errorf("error fetching movies dataset from store: %s", err.Error()) 32 return 33 } 34 35 buf := &bytes.Buffer{} 36 err = WriteZip(ctx, fs, ds, "yaml", blankInitID, dsref.MustParse("peer/ref@/ipfs/Qmb"), buf) 37 if err != nil { 38 t.Errorf("error writing zip archive: %s", err.Error()) 39 return 40 } 41 42 zr, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len())) 43 if err != nil { 44 t.Errorf("error creating zip reader: %s", err.Error()) 45 return 46 } 47 48 // TODO (dlong): Actually test the contents of the zip. 49 for _, f := range zr.File { 50 rc, err := f.Open() 51 if err != nil { 52 t.Errorf("error opening file %s in package", f.Name) 53 break 54 } 55 56 if err := rc.Close(); err != nil { 57 t.Errorf("error closing file %s in package", f.Name) 58 break 59 } 60 } 61 } 62 63 func TestWriteZipFullDataset(t *testing.T) { 64 ctx := context.Background() 65 fs, names, err := testFSWithVizAndTransform() 66 if err != nil { 67 t.Errorf("error creating filesystem: %s", err.Error()) 68 return 69 } 70 71 ds, err := dsfs.LoadDataset(ctx, fs, names["movies"]) 72 if err != nil { 73 t.Errorf("error fetching movies dataset from store: %s", err.Error()) 74 return 75 } 76 77 err = base.OpenDataset(ctx, fs, ds) 78 if err != nil { 79 t.Fatal(err) 80 } 81 82 _, err = fs.Get(ctx, names["transform_script"]) 83 if err != nil { 84 t.Errorf("error fetching movies dataset from store: %s", err.Error()) 85 return 86 } 87 88 buf := &bytes.Buffer{} 89 err = WriteZip(ctx, fs, ds, "json", blankInitID, dsref.MustParse("peer/ref@/ipfs/Qmb"), buf) 90 if err != nil { 91 t.Errorf("error writing zip archive: %s", err.Error()) 92 return 93 } 94 95 path, err := ioutil.TempDir("", "TestArchiveFullDataset") 96 if err != nil { 97 t.Fatal(err) 98 } 99 tmppath := filepath.Join(path, "exported.zip") 100 // If this test explodes because you've intentionally changed the formatting 101 // of dataset saves, comment out the defer, uncomment the log, and examine the 102 // persisted zip archive at the logged path, overrite the existing test zip 103 // archive if everything looks ok 104 defer os.RemoveAll(tmppath) 105 // t.Log(tmppath) 106 err = ioutil.WriteFile(tmppath, buf.Bytes(), os.ModePerm) 107 if err != nil { 108 t.Errorf("error writing temp zip file: %s", err.Error()) 109 return 110 } 111 112 expectFile := zipTestdataFile("exported.zip") 113 expectBytes, err := ioutil.ReadFile(expectFile) 114 if err != nil { 115 t.Errorf("error reading expected bytes: %s", err.Error()) 116 return 117 } 118 if diff := cmp.Diff(expectBytes, buf.Bytes()); diff != "" { 119 t.Errorf("byte mismatch (-want +got):\n%s", diff) 120 } 121 } 122 123 // TODO(dustmop): Rewrite zip importing 124 //func TestUnzipDatasetBytes(t *testing.T) { 125 // path := zipTestdataFile("exported.zip") 126 // zipBytes, err := ioutil.ReadFile(path) 127 // if err != nil { 128 // t.Fatal(err) 129 // } 130 // 131 // dsp := &dataset.Dataset{} 132 // if err := UnzipDatasetBytes(zipBytes, dsp); err != nil { 133 // t.Error(err) 134 // } 135 //} 136 //func TestUnzipDataset(t *testing.T) { 137 // if err := UnzipDataset(bytes.NewReader([]byte{}), 0, &dataset.Dataset{}); err == nil { 138 // t.Error("expected passing bad reader to error") 139 // } 140 // 141 // path := zipTestdataFile("exported.zip") 142 // zipBytes, err := ioutil.ReadFile(path) 143 // if err != nil { 144 // t.Fatal(err) 145 // } 146 // 147 // dsp := &dataset.Dataset{} 148 // if err := UnzipDataset(bytes.NewReader(zipBytes), int64(len(zipBytes)), dsp); err != nil { 149 // t.Error(err) 150 // } 151 //} 152 153 func TestUnzipGetContents(t *testing.T) { 154 if _, err := UnzipGetContents([]byte{}); err == nil { 155 t.Error("expected passing bad reader to error") 156 } 157 158 path := zipTestdataFile("exported.zip") 159 zipBytes, err := ioutil.ReadFile(path) 160 if err != nil { 161 t.Fatal(err) 162 } 163 164 res, err := UnzipGetContents(zipBytes) 165 if err != nil { 166 t.Error(err) 167 } 168 169 keys := getKeys(res) 170 expectKeys := []string{"body.csv", "qri-ref.txt", "structure.json", "transform.json"} 171 if diff := cmp.Diff(expectKeys, keys); diff != "" { 172 t.Errorf("result mismatch (-want +got):\n%s", diff) 173 } 174 } 175 176 func getKeys(m map[string]string) []string { 177 keys := make([]string, 0, len(m)) 178 for k := range m { 179 keys = append(keys, k) 180 } 181 sort.Strings(keys) 182 return keys 183 }