mirror of https://github.com/perkeep/perkeep.git
Add another search handler test, for recent permanodes.
Change-Id: Iaf40cd94aba7b96c16fa1b04c2bfcebdfeea870e
This commit is contained in:
parent
305b4557b9
commit
e783ad1717
|
@ -18,6 +18,7 @@ package indextest
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
|
@ -25,7 +26,6 @@ import (
|
|||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
"log"
|
||||
|
||||
"camlistore.org/pkg/blobref"
|
||||
"camlistore.org/pkg/index"
|
||||
|
@ -88,6 +88,7 @@ func (id *IndexDeps) uploadAndSignMap(m schema.Map) *blobref.BlobRef {
|
|||
UnsignedJSON: unsigned,
|
||||
Fetcher: id.PublicKeyFetcher,
|
||||
EntityFetcher: id.EntityFetcher,
|
||||
SignatureTime: id.now,
|
||||
}
|
||||
signed, err := sr.Sign()
|
||||
if err != nil {
|
||||
|
@ -108,6 +109,13 @@ func (id *IndexDeps) NewPermanode() *blobref.BlobRef {
|
|||
return id.uploadAndSignMap(unsigned)
|
||||
}
|
||||
|
||||
// NewPermanode creates (& signs) a new planned permanode and adds it
|
||||
// to the index, returning its blobref.
|
||||
func (id *IndexDeps) NewPlannedPermanode(key string) *blobref.BlobRef {
|
||||
unsigned := schema.NewPlannedPermanode(key)
|
||||
return id.uploadAndSignMap(unsigned)
|
||||
}
|
||||
|
||||
func (id *IndexDeps) advanceTime() string {
|
||||
id.now = id.now.Add(1 * time.Second)
|
||||
return schema.RFC3339FromTime(id.now)
|
||||
|
|
|
@ -64,8 +64,12 @@ func (ix *Index) ReceiveBlob(blobRef *blobref.BlobRef, source io.Reader) (retsb
|
|||
return
|
||||
}
|
||||
|
||||
mimeType := sniffer.MimeType()
|
||||
log.Printf("indexer: received %s; type=%v; truncated=%v", blobRef, mimeType, sniffer.IsTruncated())
|
||||
// TODO(bradfitz): log levels? These are generally noisy
|
||||
// (especially in tests, like search/handler_test), but I
|
||||
// could see it being useful in production. For now, disabled:
|
||||
//
|
||||
// mimeType := sniffer.MimeType()
|
||||
// log.Printf("indexer: received %s; type=%v; truncated=%v", blobRef, mimeType, sniffer.IsTruncated())
|
||||
|
||||
return blobref.SizedBlobRef{blobRef, written}, nil
|
||||
}
|
||||
|
@ -157,7 +161,6 @@ func (ix *Index) populateClaim(br *blobref.BlobRef, ss *schema.Superset, sniffer
|
|||
return errors.New("index: populateClaim verification failure")
|
||||
}
|
||||
verifiedKeyId := vr.SignerKeyId
|
||||
log.Printf("index: verified claim %s from %s", br, verifiedKeyId)
|
||||
|
||||
bm.Set("signerkeyid:"+vr.CamliSigner.String(), verifiedKeyId)
|
||||
|
||||
|
|
|
@ -26,9 +26,26 @@ import (
|
|||
"testing"
|
||||
|
||||
"camlistore.org/pkg/blobref"
|
||||
"camlistore.org/pkg/index"
|
||||
"camlistore.org/pkg/index/indextest"
|
||||
"camlistore.org/pkg/test"
|
||||
)
|
||||
|
||||
// An indexOwnerer is something that knows who owns the index.
|
||||
// It is implemented by indexAndOwner for use by TestHandler.
|
||||
type indexOwnerer interface {
|
||||
IndexOwner() *blobref.BlobRef
|
||||
}
|
||||
|
||||
type indexAndOwner struct {
|
||||
Index
|
||||
owner *blobref.BlobRef
|
||||
}
|
||||
|
||||
func (io indexAndOwner) IndexOwner() *blobref.BlobRef {
|
||||
return io.owner
|
||||
}
|
||||
|
||||
type handlerTest struct {
|
||||
// setup is responsible for populating the index before the
|
||||
// handler is invoked.
|
||||
|
@ -46,6 +63,15 @@ type handlerTest struct {
|
|||
|
||||
var owner = blobref.MustParse("abcown-123")
|
||||
|
||||
func parseJSON(s string) map[string]interface{} {
|
||||
m := make(map[string]interface{})
|
||||
err := json.Unmarshal([]byte(s), &m)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
var handlerTests = []handlerTest{
|
||||
{
|
||||
setup: func(fi *test.FakeIndex) Index { return fi },
|
||||
|
@ -69,7 +95,7 @@ var handlerTests = []handlerTest{
|
|||
},
|
||||
|
||||
{
|
||||
setup: func(fi *test.FakeIndex) Index {
|
||||
setup: func(fi *test.FakeIndex) Index {
|
||||
pn := blobref.MustParse("perma-123")
|
||||
fi.AddMeta(pn, "application/json; camliType=permanode", 123)
|
||||
fi.AddClaim(owner, pn, "set-attribute", "camliContent", "foo-232")
|
||||
|
@ -108,13 +134,49 @@ var handlerTests = []handlerTest{
|
|||
},
|
||||
},
|
||||
},
|
||||
|
||||
// Test recent permanodes
|
||||
{
|
||||
setup: func(*test.FakeIndex) Index {
|
||||
// Ignore the fakeindex and use the real (but in-memory) implementation,
|
||||
// using IndexDeps to populate it.
|
||||
idx := index.NewMemoryIndex()
|
||||
id := indextest.NewIndexDeps(idx)
|
||||
|
||||
pn := id.NewPlannedPermanode("pn1")
|
||||
id.SetAttribute(pn, "title", "Some title")
|
||||
return indexAndOwner{idx, id.SignerBlobRef}
|
||||
},
|
||||
query: "recent",
|
||||
want: parseJSON(`{
|
||||
"recent": [
|
||||
{"blobref": "sha1-7ca7743e38854598680d94ef85348f2c48a44513",
|
||||
"modtime": "2011-11-28T01:32:37Z",
|
||||
"owner": "sha1-ad87ca5c78bd0ce1195c46f7c98e6025abbaf007"}
|
||||
],
|
||||
"sha1-7ca7743e38854598680d94ef85348f2c48a44513": {
|
||||
"blobRef": "sha1-7ca7743e38854598680d94ef85348f2c48a44513",
|
||||
"camliType": "permanode",
|
||||
"mimeType": "application/json; camliType=permanode",
|
||||
"permanode": {
|
||||
"attr": { "title": [ "Some title" ] }
|
||||
},
|
||||
"size": 534
|
||||
}
|
||||
}`),
|
||||
},
|
||||
}
|
||||
|
||||
func TestHandler(t *testing.T) {
|
||||
for testn, tt := range handlerTests {
|
||||
fakeIndex := test.NewFakeIndex()
|
||||
idx := tt.setup(fakeIndex)
|
||||
h := NewHandler(idx, owner)
|
||||
|
||||
indexOwner := owner
|
||||
if io, ok := idx.(indexOwnerer); ok {
|
||||
indexOwner = io.IndexOwner()
|
||||
}
|
||||
h := NewHandler(idx, indexOwner)
|
||||
|
||||
req, err := http.NewRequest("GET", "/camli/search/"+tt.query, nil)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue