github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/dsref/spec/load.go (about) 1 package spec 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/google/go-cmp/cmp" 8 "github.com/google/go-cmp/cmp/cmpopts" 9 "github.com/qri-io/dataset" 10 "github.com/qri-io/qfs" 11 "github.com/qri-io/qri/dsref" 12 ) 13 14 // PutDatasetFunc adds a dataset to a system that stores datasets 15 // PutDatasetFunc is required to run the LoaderSpec test. When called the 16 // Loader should retain the dataset for later loading by the spec test, and 17 // return a full reference to the saved version 18 type PutDatasetFunc func(ds *dataset.Dataset) (ref *dsref.Ref, err error) 19 20 // AssertLoaderSpec confirms the expected behaviour of a dsref.Loader 21 // Interface implementation. In addition to this test passing, implementations 22 // MUST be nil-callable. Please add a nil-callable test for each implementation 23 // 24 // TODO(b5) - this test isn't working network sources. At the moment it confirms 25 // only basic requirements of a local loader function 26 func AssertLoaderSpec(t *testing.T, r dsref.Loader, putFunc PutDatasetFunc) { 27 ctx := context.Background() 28 29 ds, err := GenerateExampleDataset(ctx) 30 if err != nil { 31 t.Fatal(err) 32 } 33 34 savedRef, err := putFunc(ds) 35 if err != nil { 36 t.Fatalf("putting dataset: %s", err) 37 } 38 39 // update our expected dataset with values from the added ref 40 ds.Peername = savedRef.Username 41 ds.Name = savedRef.Name 42 ds.Path = savedRef.Path 43 // TODO(b5): loading should require initID to be set 44 // ds.ID = savedRef.InitID 45 46 t.Run("empty_ref", func(t *testing.T) { 47 if _, err = r.LoadDataset(ctx, ""); err == nil { 48 t.Errorf("expected loading an empty string to fail, did not get an error") 49 } 50 }) 51 52 t.Run("full_reference_provided", func(t *testing.T) { 53 got, err := r.LoadDataset(ctx, savedRef.String()) 54 if err != nil { 55 t.Fatal(err) 56 } 57 58 if got.BodyFile() == nil { 59 t.Errorf("expected body file to be open & ready to read") 60 } 61 62 if savedRef.Username != got.Peername { 63 t.Errorf("load Dataset didn't set dataset.Peername field to given reference. want: %q got: %q", savedRef.Username, got.Peername) 64 } 65 if savedRef.Name != got.Name { 66 t.Errorf("load Dataset didn't set dataset.Name field to given reference. want: %q got: %q", savedRef.Name, got.Name) 67 } 68 69 if diff := cmp.Diff(ds, got, cmpopts.IgnoreUnexported(dataset.Dataset{}, dataset.Meta{})); diff != "" { 70 t.Errorf("result mismatch (-want +got):\n%s", diff) 71 } 72 }) 73 74 t.Run("alias_provided", func(t *testing.T) { 75 got, err := r.LoadDataset(ctx, savedRef.Alias()) 76 if err != nil { 77 t.Fatal(err) 78 } 79 80 if got.BodyFile() == nil { 81 t.Errorf("expected body file to be open & ready to read") 82 } 83 84 if savedRef.Username != got.Peername { 85 t.Errorf("load Dataset didn't set dataset.Peername field to given reference. want: %q got: %q", savedRef.Username, got.Peername) 86 } 87 if savedRef.Name != got.Name { 88 t.Errorf("load Dataset didn't set dataset.Name field to given reference. want: %q got: %q", savedRef.Name, got.Name) 89 } 90 91 if diff := cmp.Diff(ds, got, cmpopts.IgnoreUnexported(dataset.Dataset{}, dataset.Meta{})); diff != "" { 92 t.Errorf("result mismatch (-want +got):\n%s", diff) 93 } 94 }) 95 } 96 97 // GenerateExampleDataset creates an example dataset document 98 func GenerateExampleDataset(ctx context.Context) (*dataset.Dataset, error) { 99 ds := &dataset.Dataset{ 100 Name: "example_loader_spec_test", 101 Commit: &dataset.Commit{ 102 Title: "initial commit", 103 }, 104 Meta: &dataset.Meta{ 105 Title: "dataset loader spec test", 106 Description: "a dataset to check that", 107 }, 108 Structure: &dataset.Structure{ 109 Format: "json", 110 Schema: map[string]interface{}{ 111 "type": "array", 112 "items": map[string]interface{}{ 113 "type": "array", 114 "items": []interface{}{ 115 map[string]interface{}{"title": "a", "type": "string"}, 116 map[string]interface{}{"title": "b", "type": "number"}, 117 map[string]interface{}{"title": "c", "type": "boolean"}, 118 }, 119 }, 120 }, 121 }, 122 } 123 124 ds.SetBodyFile(qfs.NewMemfileBytes("body.json", []byte(`[ 125 ["a",1,false], 126 ["b",2,true], 127 ["c",3,true] 128 ]`))) 129 130 return ds, nil 131 }