2011-07-01 19:00:10 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2011-07-06 18:20:25 +00:00
|
|
|
package search_test
|
2011-07-01 19:00:10 +00:00
|
|
|
|
|
|
|
import (
|
2012-03-29 23:22:05 +00:00
|
|
|
. "camlistore.org/pkg/search"
|
2011-07-06 18:20:25 +00:00
|
|
|
|
2011-07-01 19:38:00 +00:00
|
|
|
"bytes"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"encoding/json"
|
2012-11-04 12:59:19 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2011-07-01 19:00:10 +00:00
|
|
|
"testing"
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"camlistore.org/pkg/blobref"
|
2012-11-04 14:26:13 +00:00
|
|
|
"camlistore.org/pkg/index"
|
|
|
|
"camlistore.org/pkg/index/indextest"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"camlistore.org/pkg/test"
|
2011-07-01 19:00:10 +00:00
|
|
|
)
|
|
|
|
|
2012-11-04 14:26:13 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2012-11-04 12:59:19 +00:00
|
|
|
type handlerTest struct {
|
|
|
|
// setup is responsible for populating the index before the
|
|
|
|
// handler is invoked.
|
|
|
|
//
|
|
|
|
// A FakeIndex is constructed and provided to setup and is
|
|
|
|
// generally then returned as the Index to use, but an
|
|
|
|
// alternate Index may be returned instead, in which case the
|
|
|
|
// FakeIndex is not used.
|
|
|
|
setup func(fi *test.FakeIndex) Index
|
2011-07-01 19:38:00 +00:00
|
|
|
|
2012-11-04 12:59:19 +00:00
|
|
|
query string // the HTTP path + optional query suffix after "camli/search/"
|
2011-07-01 19:38:00 +00:00
|
|
|
|
2012-11-04 12:59:19 +00:00
|
|
|
want map[string]interface{}
|
2011-07-01 19:38:00 +00:00
|
|
|
}
|
|
|
|
|
2011-07-01 21:33:15 +00:00
|
|
|
var owner = blobref.MustParse("abcown-123")
|
|
|
|
|
2012-11-04 14:26:13 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2012-11-04 12:59:19 +00:00
|
|
|
var handlerTests = []handlerTest{
|
2011-07-01 19:38:00 +00:00
|
|
|
{
|
2012-11-04 12:59:19 +00:00
|
|
|
setup: func(fi *test.FakeIndex) Index { return fi },
|
|
|
|
query: "describe?blobref=eabc-555",
|
|
|
|
want: map[string]interface{}{},
|
2011-07-01 19:38:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2012-11-04 12:59:19 +00:00
|
|
|
setup: func(fi *test.FakeIndex) Index {
|
2011-07-01 21:33:15 +00:00
|
|
|
fi.AddMeta(blobref.MustParse("abc-555"), "image/jpeg", 999)
|
2012-11-04 12:59:19 +00:00
|
|
|
return fi
|
2011-07-01 19:38:00 +00:00
|
|
|
},
|
2012-11-04 12:59:19 +00:00
|
|
|
query: "describe?blobref=abc-555",
|
|
|
|
want: map[string]interface{}{
|
2011-07-01 19:38:00 +00:00
|
|
|
"abc-555": map[string]interface{}{
|
2011-07-01 21:33:15 +00:00
|
|
|
"blobRef": "abc-555",
|
2011-07-01 19:38:00 +00:00
|
|
|
"mimeType": "image/jpeg",
|
2011-07-01 21:33:15 +00:00
|
|
|
"size": 999,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2012-11-04 14:26:13 +00:00
|
|
|
setup: func(fi *test.FakeIndex) Index {
|
2011-07-01 21:33:15 +00:00
|
|
|
pn := blobref.MustParse("perma-123")
|
|
|
|
fi.AddMeta(pn, "application/json; camliType=permanode", 123)
|
|
|
|
fi.AddClaim(owner, pn, "set-attribute", "camliContent", "foo-232")
|
|
|
|
fi.AddMeta(blobref.MustParse("foo-232"), "foo/bar", 878)
|
|
|
|
|
|
|
|
// Test deleting all attributes
|
|
|
|
fi.AddClaim(owner, pn, "add-attribute", "wont-be-present", "x")
|
|
|
|
fi.AddClaim(owner, pn, "add-attribute", "wont-be-present", "y")
|
|
|
|
fi.AddClaim(owner, pn, "del-attribute", "wont-be-present", "")
|
|
|
|
|
|
|
|
// Test deleting a specific attribute.
|
|
|
|
fi.AddClaim(owner, pn, "add-attribute", "only-delete-b", "a")
|
|
|
|
fi.AddClaim(owner, pn, "add-attribute", "only-delete-b", "b")
|
|
|
|
fi.AddClaim(owner, pn, "add-attribute", "only-delete-b", "c")
|
|
|
|
fi.AddClaim(owner, pn, "del-attribute", "only-delete-b", "b")
|
2012-11-04 12:59:19 +00:00
|
|
|
return fi
|
2011-07-01 21:33:15 +00:00
|
|
|
},
|
2012-11-04 12:59:19 +00:00
|
|
|
query: "describe?blobref=perma-123",
|
|
|
|
want: map[string]interface{}{
|
2011-07-01 21:33:15 +00:00
|
|
|
"foo-232": map[string]interface{}{
|
|
|
|
"blobRef": "foo-232",
|
|
|
|
"mimeType": "foo/bar",
|
|
|
|
"size": 878,
|
|
|
|
},
|
|
|
|
"perma-123": map[string]interface{}{
|
|
|
|
"blobRef": "perma-123",
|
|
|
|
"mimeType": "application/json; camliType=permanode",
|
|
|
|
"camliType": "permanode",
|
|
|
|
"size": 123,
|
|
|
|
"permanode": map[string]interface{}{
|
|
|
|
"attr": map[string]interface{}{
|
|
|
|
"camliContent": []string{"foo-232"},
|
|
|
|
"only-delete-b": []string{"a", "c"},
|
|
|
|
},
|
|
|
|
},
|
2011-07-01 19:38:00 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2012-11-04 14:26:13 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}`),
|
|
|
|
},
|
2012-11-05 14:09:34 +00:00
|
|
|
|
2012-11-05 14:48:13 +00:00
|
|
|
// Test recent permanodes with thumbnails
|
|
|
|
{
|
|
|
|
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?thumbnails=100",
|
|
|
|
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,
|
2012-11-05 16:23:42 +00:00
|
|
|
"thumbnailHeight": 100,
|
2012-11-05 15:32:56 +00:00
|
|
|
"thumbnailSrc": "/node.png",
|
2012-11-05 16:23:42 +00:00
|
|
|
"thumbnailWidth": 100
|
2012-11-05 14:48:13 +00:00
|
|
|
}
|
|
|
|
}`),
|
|
|
|
},
|
|
|
|
|
2012-11-05 14:09:34 +00:00
|
|
|
// edgeto handler: put a permanode (member) in two parent
|
|
|
|
// permanodes, then delete the second and verify that edges
|
|
|
|
// back from member only reveal the first parent.
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
|
|
|
|
parent1 := id.NewPlannedPermanode("pn1") // sha1-7ca7743e38854598680d94ef85348f2c48a44513
|
|
|
|
parent2 := id.NewPlannedPermanode("pn2")
|
|
|
|
member := id.NewPlannedPermanode("member") // always sha1-9ca84f904a9bc59e6599a53f0a3927636a6dbcae
|
|
|
|
id.AddAttribute(parent1, "camliMember", member.String())
|
|
|
|
id.AddAttribute(parent2, "camliMember", member.String())
|
|
|
|
id.DelAttribute(parent2, "camliMember")
|
|
|
|
return indexAndOwner{idx, id.SignerBlobRef}
|
|
|
|
},
|
|
|
|
query: "edgesto?blobref=sha1-9ca84f904a9bc59e6599a53f0a3927636a6dbcae",
|
|
|
|
want: parseJSON(`{"sha1-9ca84f904a9bc59e6599a53f0a3927636a6dbcae": {
|
|
|
|
"edgesTo": [
|
|
|
|
{"from": "sha1-7ca7743e38854598680d94ef85348f2c48a44513",
|
|
|
|
"fromType": "permanode"}
|
|
|
|
]
|
|
|
|
}}`),
|
|
|
|
},
|
2011-07-01 19:38:00 +00:00
|
|
|
}
|
|
|
|
|
2012-11-04 12:59:19 +00:00
|
|
|
func TestHandler(t *testing.T) {
|
|
|
|
for testn, tt := range handlerTests {
|
|
|
|
fakeIndex := test.NewFakeIndex()
|
|
|
|
idx := tt.setup(fakeIndex)
|
2012-11-04 14:26:13 +00:00
|
|
|
|
|
|
|
indexOwner := owner
|
|
|
|
if io, ok := idx.(indexOwnerer); ok {
|
|
|
|
indexOwner = io.IndexOwner()
|
|
|
|
}
|
|
|
|
h := NewHandler(idx, indexOwner)
|
2012-11-04 12:59:19 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/camli/search/"+tt.query, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%d: bad query: %v", testn, err)
|
|
|
|
}
|
|
|
|
req.Header.Set("X-PrefixHandler-PathSuffix", req.URL.Path[1:])
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
rr.Body = new(bytes.Buffer)
|
|
|
|
|
|
|
|
h.ServeHTTP(rr, req)
|
|
|
|
|
|
|
|
got := rr.Body.Bytes()
|
|
|
|
want, _ := json.MarshalIndent(tt.want, "", " ")
|
|
|
|
want = append(want, '\n')
|
2011-07-01 19:38:00 +00:00
|
|
|
if !bytes.Equal(got, want) {
|
2012-11-04 12:59:19 +00:00
|
|
|
t.Errorf("test %d:\nwant: %s\n got: %s", testn, want, got)
|
2011-07-01 19:38:00 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-01 19:00:10 +00:00
|
|
|
}
|