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

     1  package lib
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"testing"
     8  
     9  	"github.com/qri-io/qri/base/params"
    10  	"github.com/qri-io/qri/config"
    11  	testcfg "github.com/qri-io/qri/config/test"
    12  	"github.com/qri-io/qri/event"
    13  	"github.com/qri-io/qri/p2p"
    14  	"github.com/qri-io/qri/registry/regclient"
    15  	testrepo "github.com/qri-io/qri/repo/test"
    16  )
    17  
    18  func TestSearch(t *testing.T) {
    19  	ctx, done := context.WithCancel(context.Background())
    20  	defer done()
    21  
    22  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    23  		w.Write(mockResponse)
    24  	}))
    25  	rc := regclient.NewClient(&regclient.Config{Location: server.URL})
    26  	mr, err := testrepo.NewTestRepo()
    27  	if err != nil {
    28  		t.Fatalf("error allocating test repo: %s", err.Error())
    29  	}
    30  	node, err := p2p.NewQriNode(mr, testcfg.DefaultP2PForTesting(), event.NilBus, nil)
    31  	if err != nil {
    32  		t.Fatal(err.Error())
    33  	}
    34  
    35  	inst := NewInstanceFromConfigAndNode(ctx, config.DefaultConfig(), node)
    36  	inst.registry = rc
    37  
    38  	p := &SearchParams{Query: "nuun", List: params.List{Offset: 0, Limit: 100}}
    39  	got, err := inst.Search().Search(ctx, p)
    40  	if err != nil {
    41  		t.Error(err)
    42  	}
    43  
    44  	if 1 != len(got) {
    45  		t.Errorf("expected: %d results, got: %d", 1, len(got))
    46  	}
    47  }
    48  
    49  var mockResponse = []byte(`{"data":[
    50    {
    51      "type": "dataset",
    52      "id": "/ipfs/QmZEnjt3Y5RxXsoZyufJfFzcogicBEwfaimJSyDuC7nySA",
    53      "url": "https://qri.cloud/nuun/nuun",
    54      "value": {
    55        "commit": {
    56          "qri": "cm:0",
    57          "timestamp": "2019-08-31T12:07:56.212858Z",
    58          "title": "change to 10"
    59        },
    60        "meta": {
    61          "keywords": [
    62            "joke"
    63          ],
    64          "qri": "md:0",
    65          "title": "this is a d"
    66        },
    67        "name": "nuun",
    68        "path": "/ipfs/QmZEnjt3Y5RxXsoZyufJfFzcogicBEwfaimJSyDuC7nySA",
    69        "peername": "nuun",
    70        "qri": "ds:0",
    71        "structure": {
    72          "entries": 3,
    73          "format": "csv",
    74          "length": 36,
    75          "qri": "st:0"
    76        }
    77      }
    78    }
    79  ],"meta":{"code":200}}`)