github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/stats/stats_test.go (about)

     1  package stats
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"io/ioutil"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  	"github.com/qri-io/dataset"
    12  	"github.com/qri-io/qfs"
    13  	"github.com/qri-io/qri/base"
    14  	"github.com/qri-io/qri/base/dsfs"
    15  	"github.com/qri-io/qri/dsref"
    16  	repotest "github.com/qri-io/qri/repo/test"
    17  )
    18  
    19  func TestStatsService(t *testing.T) {
    20  	ctx := context.Background()
    21  
    22  	workDir, err := ioutil.TempDir("", "qri_test_stats_service")
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  	defer os.RemoveAll(workDir)
    27  
    28  	mr, err := repotest.NewTestRepo()
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  
    33  	ref := dsref.MustParse("peer/cities")
    34  	if _, err := mr.ResolveRef(ctx, &ref); err != nil {
    35  		t.Fatal(err)
    36  	}
    37  
    38  	ds, err := dsfs.LoadDataset(ctx, mr.Filesystem(), ref.Path)
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	if err = base.OpenDataset(ctx, mr.Filesystem(), ds); err != nil {
    43  		t.Fatal(err)
    44  	}
    45  
    46  	cache, err := NewLocalCache(workDir, 1000<<8)
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	svc := New(cache)
    51  
    52  	expect := ds.Stats
    53  	sa, err := svc.Stats(ctx, ds)
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  	if diff := cmp.Diff(expect, sa); diff != "" {
    58  		t.Errorf("stat component result mismatch. (-want +got):%s\n", diff)
    59  	}
    60  
    61  	// TODO(b5): dataset.Dataset.BodyFile() should return a name that matches.
    62  	// currently we need to set the filename manually so detect.Structure has something
    63  	// to work with
    64  	ds.SetBodyFile(qfs.NewMemfileReader(ds.Structure.BodyFilename(), ds.BodyFile()))
    65  
    66  	// remove path. recalculated stats won't have a path set
    67  	expect.Path = ""
    68  	// drop stats & structure to force recalculation of both
    69  	ds.Stats = nil
    70  	ds.Structure = nil
    71  
    72  	sa, err = svc.Stats(ctx, ds)
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  
    77  	// TODO (b5) - there are currently discrepencies in the types created by
    78  	// dsstats.ToMap and the types inferred by a trip through JSON serialization
    79  	// so we need to marshal/unmarshal the result. The proper fix for this
    80  	// is stronger typing on the Stats field of the Stats component
    81  	data, err := json.Marshal(sa)
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  	sa = &dataset.Stats{}
    86  	if err := json.Unmarshal(data, sa); err != nil {
    87  		t.Fatal(err)
    88  	}
    89  
    90  	if diff := cmp.Diff(expect, sa); diff != "" {
    91  		t.Errorf("calculated stat result mismatch. (-want +got):%s\n", diff)
    92  	}
    93  
    94  	// drop stats again. at this point the body file is consumed,
    95  	// but we should hit the cache
    96  	ds.Stats = nil
    97  	ds.Structure = nil
    98  
    99  	sa, err = svc.Stats(ctx, ds)
   100  	if err != nil {
   101  		t.Fatal(err)
   102  	}
   103  	if diff := cmp.Diff(expect, sa); diff != "" {
   104  		t.Errorf("cached stat result mismatch. (-want +got):%s\n", diff)
   105  	}
   106  }