github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/base/archive/archive_test.go (about)

     1  package archive
     2  
     3  import (
     4  	"context"
     5  	"path/filepath"
     6  	"runtime"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/qri-io/dataset"
    11  	"github.com/qri-io/qfs"
    12  	testkeys "github.com/qri-io/qri/auth/key/test"
    13  	"github.com/qri-io/qri/base/dsfs"
    14  	"github.com/qri-io/qri/event"
    15  )
    16  
    17  func TestGenerateFilename(t *testing.T) {
    18  	// no commit
    19  	// no structure & no format
    20  	// no format & yes structure
    21  	// timestamp and format!
    22  	loc := time.FixedZone("UTC-8", -8*60*60)
    23  	timeStamp := time.Date(2009, time.November, 10, 23, 0, 0, 0, loc)
    24  	cases := []struct {
    25  		description string
    26  		ds          *dataset.Dataset
    27  		format      string
    28  		expected    string
    29  		err         string
    30  	}{
    31  		{
    32  			"no format & no structure",
    33  			&dataset.Dataset{}, "", "", "no format specified and no format present in the dataset Structure",
    34  		},
    35  		{
    36  			"no format & no Structure.Format",
    37  			&dataset.Dataset{
    38  				Structure: &dataset.Structure{
    39  					Format: "",
    40  				},
    41  			}, "", "", "no format specified and no format present in the dataset Structure",
    42  		},
    43  		{
    44  			"no format specified & Structure.Format exists",
    45  			&dataset.Dataset{
    46  				Commit: &dataset.Commit{
    47  					Timestamp: timeStamp,
    48  				},
    49  				Structure: &dataset.Structure{
    50  					Format: "json",
    51  				},
    52  				Peername: "cassie",
    53  				Name:     "fun_dataset",
    54  			}, "", "cassie-fun_dataset_-_2009-11-10-23-00-00.json", "",
    55  		},
    56  		{
    57  			"no format specified & Structure.Format exists",
    58  			&dataset.Dataset{
    59  				Commit: &dataset.Commit{
    60  					Timestamp: timeStamp,
    61  				},
    62  				Structure: &dataset.Structure{
    63  					Format: "json",
    64  				},
    65  				Peername: "brandon",
    66  				Name:     "awesome_dataset",
    67  			}, "", "brandon-awesome_dataset_-_2009-11-10-23-00-00.json", "",
    68  		},
    69  		{
    70  			"format: json",
    71  			&dataset.Dataset{
    72  				Commit: &dataset.Commit{
    73  					Timestamp: timeStamp,
    74  				},
    75  				Peername: "ricky",
    76  				Name:     "rad_dataset",
    77  			}, "json", "ricky-rad_dataset_-_2009-11-10-23-00-00.json", "",
    78  		},
    79  		{
    80  			"format: csv",
    81  			&dataset.Dataset{
    82  				Commit: &dataset.Commit{
    83  					Timestamp: timeStamp,
    84  				},
    85  				Peername: "justin",
    86  				Name:     "cool_dataset",
    87  			}, "csv", "justin-cool_dataset_-_2009-11-10-23-00-00.csv", "",
    88  		},
    89  		{
    90  			"no timestamp",
    91  			&dataset.Dataset{
    92  				Peername: "no",
    93  				Name:     "time",
    94  			}, "csv", "no-time_-_0001-01-01-00-00-00.csv", "",
    95  		},
    96  	}
    97  	for _, c := range cases {
    98  		got, err := GenerateFilename(c.ds, c.format)
    99  		if !(err == nil && c.err == "" || err != nil && err.Error() == c.err) {
   100  			t.Errorf("case '%s' error mismatched: expected: '%s', got: '%s'", c.description, c.err, err)
   101  		}
   102  		if got != c.expected {
   103  			t.Errorf("case '%s' filename mismatched: expected: '%s', got: '%s'", c.description, c.expected, got)
   104  		}
   105  	}
   106  }
   107  
   108  func testFS() (qfs.Filesystem, map[string]string, error) {
   109  	ctx := context.Background()
   110  	dataf := qfs.NewMemfileBytes("/body.csv", []byte("movie\nup\nthe incredibles"))
   111  	pk := testkeys.GetKeyData(0).PrivKey
   112  
   113  	// Map strings to ds.keys for convenience
   114  	ns := map[string]string{
   115  		"movies": "",
   116  	}
   117  
   118  	ds := &dataset.Dataset{
   119  		Structure: &dataset.Structure{
   120  			Format: "csv",
   121  			Schema: map[string]interface{}{
   122  				"type": "array",
   123  				"items": map[string]interface{}{
   124  					"type": "array",
   125  					"items": []interface{}{
   126  						map[string]interface{}{"title": "movie", "type": "string"},
   127  					},
   128  				},
   129  			},
   130  		},
   131  	}
   132  	ds.SetBodyFile(dataf)
   133  
   134  	fs := qfs.NewMemFS()
   135  	dskey, err := dsfs.WriteDataset(ctx, nil, fs, nil, ds, event.NilBus, pk, dsfs.SaveSwitches{})
   136  	if err != nil {
   137  		return fs, ns, err
   138  	}
   139  	ns["movies"] = dskey
   140  
   141  	return fs, ns, nil
   142  }
   143  
   144  func testFSWithVizAndTransform() (qfs.Filesystem, map[string]string, error) {
   145  	ctx := context.Background()
   146  	ds := &dataset.Dataset{
   147  		Structure: &dataset.Structure{
   148  			Format: "csv",
   149  			Schema: map[string]interface{}{
   150  				"type": "array",
   151  				"items": map[string]interface{}{
   152  					"type": "array",
   153  					"items": []interface{}{
   154  						map[string]interface{}{"title": "movie", "type": "string"},
   155  					},
   156  				},
   157  			},
   158  		},
   159  		Transform: &dataset.Transform{
   160  			ScriptPath: "/transform_script",
   161  			Text:       "def transform(ds):\nreturn ds\n",
   162  		},
   163  		Viz: &dataset.Viz{
   164  			ScriptPath: dsfs.PackageFileVizScript.Filename(),
   165  			Text:       "<html>template</html>\n",
   166  		},
   167  	}
   168  	// load scripts into file pointers, time for a NewDataset function?
   169  	// ds.Transform.OpenScriptFile(ctx, nil)
   170  	ds.Transform.SetScriptFile(qfs.NewMemfileBytes(ds.Transform.ScriptPath, []byte(ds.Transform.Text)))
   171  	ds.Viz.OpenScriptFile(ctx, nil)
   172  	ds.Viz.SetRenderedFile(qfs.NewMemfileBytes("index.html", []byte("<html>rendered</html<\n")))
   173  
   174  	// Map strings to ds.keys for convenience
   175  	ns := map[string]string{}
   176  	// Store the files
   177  	st := qfs.NewMemFS()
   178  	ds.SetBodyFile(qfs.NewMemfileBytes("/body.csv", []byte("movie\nup\nthe incredibles")))
   179  	privKey := testkeys.GetKeyData(10).PrivKey
   180  
   181  	dskey, err := dsfs.WriteDataset(ctx, st, st, nil, ds, event.NilBus, privKey, dsfs.SaveSwitches{Pin: true})
   182  	if err != nil {
   183  		return st, ns, err
   184  	}
   185  	ns["movies"] = dskey
   186  	ns["transform_script"] = ds.Transform.ScriptPath
   187  	ns["viz_template"] = ds.Viz.ScriptPath
   188  	ns["index.html"] = ds.Viz.RenderedPath
   189  	return st, ns, nil
   190  }
   191  
   192  func zipTestdataFile(path string) string {
   193  	_, currfile, _, _ := runtime.Caller(0)
   194  	testdataPath := filepath.Join(filepath.Dir(currfile), "../dsfs/testdata/zip")
   195  	return filepath.Join(testdataPath, path)
   196  }