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

     1  package p2p
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/qri-io/dataset"
     9  	"github.com/qri-io/qfs"
    10  	"github.com/qri-io/qri/base"
    11  	"github.com/qri-io/qri/base/dsfs"
    12  	testcfg "github.com/qri-io/qri/config/test"
    13  	"github.com/qri-io/qri/dsref"
    14  	"github.com/qri-io/qri/event"
    15  	p2ptest "github.com/qri-io/qri/p2p/test"
    16  	"github.com/qri-io/qri/repo"
    17  	reporef "github.com/qri-io/qri/repo/ref"
    18  )
    19  
    20  type testRunner struct {
    21  	Ctx context.Context
    22  }
    23  
    24  func newTestRunner(t *testing.T) (tr *testRunner, cleanup func()) {
    25  	tr = &testRunner{
    26  		Ctx: context.Background(),
    27  	}
    28  
    29  	cleanup = func() {}
    30  	return tr, cleanup
    31  }
    32  
    33  func (tr *testRunner) IPFSBackedQriNode(t *testing.T, username string) *QriNode {
    34  	ipfs, _, err := p2ptest.MakeIPFSNode(tr.Ctx)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	r, err := p2ptest.MakeRepoFromIPFSNode(tr.Ctx, ipfs, username, event.NilBus)
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	localResolver := dsref.SequentialResolver(r.Dscache(), r)
    43  	node, err := NewQriNode(r, testcfg.DefaultP2PForTesting(), event.NilBus, localResolver)
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	return node
    48  }
    49  
    50  func writeWorldBankPopulation(ctx context.Context, t *testing.T, r repo.Repo) reporef.DatasetRef {
    51  	prevTs := dsfs.Timestamp
    52  	dsfs.Timestamp = func() time.Time { return time.Time{} }
    53  	defer func() { dsfs.Timestamp = prevTs }()
    54  
    55  	ds := &dataset.Dataset{
    56  		Name: "world_bank_population",
    57  		Commit: &dataset.Commit{
    58  			Title: "initial commit",
    59  		},
    60  		Meta: &dataset.Meta{
    61  			Title: "World Bank Population",
    62  		},
    63  		Structure: &dataset.Structure{
    64  			Format: "json",
    65  			Schema: dataset.BaseSchemaArray,
    66  		},
    67  		Viz: &dataset.Viz{
    68  			Format: "html",
    69  		},
    70  		Transform: &dataset.Transform{
    71  			Syntax: "amaze",
    72  		},
    73  	}
    74  	ds.SetBodyFile(qfs.NewMemfileBytes("body.json", []byte("[100]")))
    75  
    76  	res, err := base.CreateDataset(ctx, r, r.Filesystem().DefaultWriteFS(), r.Profiles().Owner(ctx), ds, nil, base.SaveSwitches{Pin: true, ShouldRender: true})
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  
    81  	pro := r.Profiles().Owner(ctx)
    82  
    83  	return reporef.DatasetRef{
    84  		Peername:  pro.Peername,
    85  		ProfileID: pro.ID,
    86  		Name:      res.Name,
    87  		Path:      res.Path,
    88  	}
    89  }