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
|
|
|
|
|
2014-04-06 21:48:48 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-01 19:00:10 +00:00
|
|
|
|
|
|
|
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 (
|
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"
|
2014-04-19 19:58:55 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2013-03-05 18:15:22 +00:00
|
|
|
"io/ioutil"
|
2012-11-04 12:59:19 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2013-03-05 18:15:22 +00:00
|
|
|
"path/filepath"
|
2014-04-19 19:58:55 +00:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2011-07-01 19:00:10 +00:00
|
|
|
"testing"
|
2013-03-05 18:15:22 +00:00
|
|
|
"time"
|
2011-07-01 19:00:10 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
"camlistore.org/pkg/blob"
|
2013-06-12 09:17:30 +00:00
|
|
|
"camlistore.org/pkg/httputil"
|
2012-11-04 14:26:13 +00:00
|
|
|
"camlistore.org/pkg/index"
|
|
|
|
"camlistore.org/pkg/index/indextest"
|
2013-03-05 18:15:22 +00:00
|
|
|
"camlistore.org/pkg/osutil"
|
2014-04-19 19:58:55 +00:00
|
|
|
. "camlistore.org/pkg/search"
|
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 {
|
2013-08-04 02:54:30 +00:00
|
|
|
IndexOwner() blob.Ref
|
2012-11-04 14:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type indexAndOwner struct {
|
2013-11-16 23:00:30 +00:00
|
|
|
index.Interface
|
2013-08-04 02:54:30 +00:00
|
|
|
owner blob.Ref
|
2012-11-04 14:26:13 +00:00
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
func (io indexAndOwner) IndexOwner() blob.Ref {
|
2012-11-04 14:26:13 +00:00
|
|
|
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.
|
2013-11-16 23:00:30 +00:00
|
|
|
setup func(fi *test.FakeIndex) index.Interface
|
2011-07-01 19:38:00 +00:00
|
|
|
|
2014-04-19 19:58:55 +00:00
|
|
|
name string // test name
|
|
|
|
query string // the HTTP path + optional query suffix after "camli/search/"
|
|
|
|
postBody string // if non-nil, a POST request
|
2011-07-01 19:38:00 +00:00
|
|
|
|
2012-11-04 12:59:19 +00:00
|
|
|
want map[string]interface{}
|
2014-04-19 19:58:55 +00:00
|
|
|
// wantDescribed is a list of blobref strings that should've been
|
|
|
|
// described in meta. If want is nil and this is non-zero length,
|
|
|
|
// want is ignored.
|
|
|
|
wantDescribed []string
|
2011-07-01 19:38:00 +00:00
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
var owner = blob.MustParse("abcown-123")
|
2011-07-01 21:33:15 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-11-15 19:35:57 +00:00
|
|
|
// addToClockOrigin returns the given Duration added
|
|
|
|
// to test.ClockOrigin, in UTC, and RFC3339Nano formatted.
|
|
|
|
func addToClockOrigin(d time.Duration) string {
|
|
|
|
return test.ClockOrigin.Add(d).UTC().Format(time.RFC3339Nano)
|
|
|
|
}
|
|
|
|
|
2013-12-22 09:19:52 +00:00
|
|
|
func handlerDescribeTestSetup(fi *test.FakeIndex) index.Interface {
|
|
|
|
pn := blob.MustParse("perma-123")
|
|
|
|
fi.AddMeta(pn, "permanode", 123)
|
2014-04-06 21:48:48 +00:00
|
|
|
fi.AddClaim(owner, pn, "set-attribute", "camliContent", "fakeref-232")
|
|
|
|
fi.AddMeta(blob.MustParse("fakeref-232"), "", 878)
|
2013-12-22 09:19:52 +00:00
|
|
|
|
|
|
|
// 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")
|
|
|
|
return fi
|
|
|
|
}
|
|
|
|
|
2014-03-11 03:11:19 +00:00
|
|
|
// extends handlerDescribeTestSetup but adds a camliContentImage to pn.
|
|
|
|
func handlerDescribeTestSetupWithImage(fi *test.FakeIndex) index.Interface {
|
|
|
|
handlerDescribeTestSetup(fi)
|
|
|
|
pn := blob.MustParse("perma-123")
|
2014-04-06 21:48:48 +00:00
|
|
|
imageRef := blob.MustParse("fakeref-789")
|
2014-03-11 03:11:19 +00:00
|
|
|
fi.AddMeta(imageRef, "", 789)
|
|
|
|
fi.AddClaim(owner, pn, "set-attribute", "camliContentImage", imageRef.String())
|
|
|
|
return fi
|
|
|
|
}
|
|
|
|
|
2014-04-06 21:48:48 +00:00
|
|
|
// extends handlerDescribeTestSetup but adds various embedded references to other nodes.
|
|
|
|
func handlerDescribeTestSetupWithEmbeddedRefs(fi *test.FakeIndex) index.Interface {
|
|
|
|
handlerDescribeTestSetup(fi)
|
|
|
|
pn := blob.MustParse("perma-123")
|
|
|
|
c1 := blob.MustParse("fakeref-01")
|
|
|
|
c2 := blob.MustParse("fakeref-02")
|
|
|
|
c3 := blob.MustParse("fakeref-03")
|
|
|
|
c4 := blob.MustParse("fakeref-04")
|
|
|
|
c5 := blob.MustParse("fakeref-05")
|
|
|
|
c6 := blob.MustParse("fakeref-06")
|
|
|
|
fi.AddMeta(c1, "", 1)
|
|
|
|
fi.AddMeta(c2, "", 2)
|
|
|
|
fi.AddMeta(c3, "", 3)
|
|
|
|
fi.AddMeta(c4, "", 4)
|
|
|
|
fi.AddMeta(c5, "", 5)
|
|
|
|
fi.AddMeta(c6, "", 6)
|
|
|
|
fi.AddClaim(owner, pn, "set-attribute", c1.String(), "foo")
|
|
|
|
fi.AddClaim(owner, pn, "set-attribute", "foo,"+c2.String()+"=bar", "foo")
|
|
|
|
fi.AddClaim(owner, pn, "set-attribute", "foo:"+c3.String()+"?bar,"+c4.String(), "foo")
|
|
|
|
fi.AddClaim(owner, pn, "set-attribute", "foo", c5.String())
|
|
|
|
fi.AddClaim(owner, pn, "add-attribute", "bar", "baz")
|
|
|
|
fi.AddClaim(owner, pn, "add-attribute", "bar", "monkey\n"+c6.String())
|
|
|
|
return fi
|
|
|
|
}
|
|
|
|
|
2012-11-04 12:59:19 +00:00
|
|
|
var handlerTests = []handlerTest{
|
2011-07-01 19:38:00 +00:00
|
|
|
{
|
2013-02-08 17:27:23 +00:00
|
|
|
name: "describe-missing",
|
2013-11-16 23:00:30 +00:00
|
|
|
setup: func(fi *test.FakeIndex) index.Interface { return fi },
|
2014-04-06 21:48:48 +00:00
|
|
|
query: "describe?blobref=eabfakeref-0555",
|
2013-02-08 17:27:23 +00:00
|
|
|
want: parseJSON(`{
|
|
|
|
"meta": {
|
|
|
|
}
|
|
|
|
}`),
|
2011-07-01 19:38:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2013-02-07 17:33:00 +00:00
|
|
|
name: "describe-jpeg-blob",
|
2013-11-16 23:00:30 +00:00
|
|
|
setup: func(fi *test.FakeIndex) index.Interface {
|
2014-04-06 21:48:48 +00:00
|
|
|
fi.AddMeta(blob.MustParse("abfakeref-0555"), "", 999)
|
2012-11-04 12:59:19 +00:00
|
|
|
return fi
|
2011-07-01 19:38:00 +00:00
|
|
|
},
|
2014-04-06 21:48:48 +00:00
|
|
|
query: "describe?blobref=abfakeref-0555",
|
2013-02-08 17:27:23 +00:00
|
|
|
want: parseJSON(`{
|
|
|
|
"meta": {
|
2014-04-06 21:48:48 +00:00
|
|
|
"abfakeref-0555": {
|
|
|
|
"blobRef": "abfakeref-0555",
|
2013-02-08 17:27:23 +00:00
|
|
|
"size": 999
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`),
|
2011-07-01 21:33:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2013-12-22 09:19:52 +00:00
|
|
|
name: "describe-permanode",
|
|
|
|
setup: handlerDescribeTestSetup,
|
2012-11-04 12:59:19 +00:00
|
|
|
query: "describe?blobref=perma-123",
|
2013-02-08 17:27:23 +00:00
|
|
|
want: parseJSON(`{
|
|
|
|
"meta": {
|
2014-04-06 21:48:48 +00:00
|
|
|
"fakeref-232": {
|
|
|
|
"blobRef": "fakeref-232",
|
2013-02-08 17:27:23 +00:00
|
|
|
"size": 878
|
2011-07-01 21:33:15 +00:00
|
|
|
},
|
2013-02-08 17:27:23 +00:00
|
|
|
"perma-123": {
|
|
|
|
"blobRef": "perma-123",
|
|
|
|
"camliType": "permanode",
|
|
|
|
"size": 123,
|
|
|
|
"permanode": {
|
|
|
|
"attr": {
|
2014-04-06 21:48:48 +00:00
|
|
|
"camliContent": [ "fakeref-232" ],
|
2013-02-08 17:27:23 +00:00
|
|
|
"only-delete-b": [ "a", "c" ]
|
2013-11-15 19:35:57 +00:00
|
|
|
},
|
|
|
|
"modtime": "` + addToClockOrigin(8*time.Second) + `"
|
2013-02-08 17:27:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`),
|
2011-07-01 19:38:00 +00:00
|
|
|
},
|
2012-11-04 14:26:13 +00:00
|
|
|
|
2014-03-11 03:11:19 +00:00
|
|
|
{
|
|
|
|
name: "describe-permanode-image",
|
|
|
|
setup: handlerDescribeTestSetupWithImage,
|
|
|
|
query: "describe?blobref=perma-123",
|
|
|
|
want: parseJSON(`{
|
|
|
|
"meta": {
|
2014-04-06 21:48:48 +00:00
|
|
|
"fakeref-232": {
|
|
|
|
"blobRef": "fakeref-232",
|
2014-03-11 03:11:19 +00:00
|
|
|
"size": 878
|
|
|
|
},
|
2014-04-06 21:48:48 +00:00
|
|
|
"fakeref-789": {
|
|
|
|
"blobRef": "fakeref-789",
|
2014-03-11 03:11:19 +00:00
|
|
|
"size": 789
|
|
|
|
},
|
|
|
|
"perma-123": {
|
|
|
|
"blobRef": "perma-123",
|
|
|
|
"camliType": "permanode",
|
|
|
|
"size": 123,
|
|
|
|
"permanode": {
|
|
|
|
"attr": {
|
2014-04-06 21:48:48 +00:00
|
|
|
"camliContent": [ "fakeref-232" ],
|
|
|
|
"camliContentImage": [ "fakeref-789" ],
|
2014-03-11 03:11:19 +00:00
|
|
|
"only-delete-b": [ "a", "c" ]
|
|
|
|
},
|
|
|
|
"modtime": "` + addToClockOrigin(9*time.Second) + `"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`),
|
|
|
|
},
|
|
|
|
|
2014-04-06 21:48:48 +00:00
|
|
|
{
|
|
|
|
name: "describe-permanode-embedded-references",
|
|
|
|
setup: handlerDescribeTestSetupWithEmbeddedRefs,
|
|
|
|
query: "describe?blobref=perma-123",
|
|
|
|
want: parseJSON(`{
|
|
|
|
"meta": {
|
|
|
|
"fakeref-01": {
|
|
|
|
"blobRef": "fakeref-01",
|
|
|
|
"size": 1
|
|
|
|
},
|
|
|
|
"fakeref-02": {
|
|
|
|
"blobRef": "fakeref-02",
|
|
|
|
"size": 2
|
|
|
|
},
|
|
|
|
"fakeref-03": {
|
|
|
|
"blobRef": "fakeref-03",
|
|
|
|
"size": 3
|
|
|
|
},
|
|
|
|
"fakeref-04": {
|
|
|
|
"blobRef": "fakeref-04",
|
|
|
|
"size": 4
|
|
|
|
},
|
|
|
|
"fakeref-05": {
|
|
|
|
"blobRef": "fakeref-05",
|
|
|
|
"size": 5
|
|
|
|
},
|
|
|
|
"fakeref-06": {
|
|
|
|
"blobRef": "fakeref-06",
|
|
|
|
"size": 6
|
|
|
|
},
|
|
|
|
"fakeref-232": {
|
|
|
|
"blobRef": "fakeref-232",
|
|
|
|
"size": 878
|
|
|
|
},
|
|
|
|
"perma-123": {
|
|
|
|
"blobRef": "perma-123",
|
|
|
|
"camliType": "permanode",
|
|
|
|
"size": 123,
|
|
|
|
"permanode": {
|
|
|
|
"attr": {
|
|
|
|
"bar": [
|
|
|
|
"baz",
|
|
|
|
"monkey\nfakeref-06"
|
|
|
|
],
|
|
|
|
"fakeref-01": [
|
|
|
|
"foo"
|
|
|
|
],
|
|
|
|
"camliContent": [
|
|
|
|
"fakeref-232"
|
|
|
|
],
|
|
|
|
"foo": [
|
|
|
|
"fakeref-05"
|
|
|
|
],
|
|
|
|
"foo,fakeref-02=bar": [
|
|
|
|
"foo"
|
|
|
|
],
|
|
|
|
"foo:fakeref-03?bar,fakeref-04": [
|
|
|
|
"foo"
|
|
|
|
],
|
|
|
|
"camliContent": [ "fakeref-232" ],
|
|
|
|
"only-delete-b": [ "a", "c" ]
|
|
|
|
},
|
|
|
|
"modtime": "` + addToClockOrigin(14*time.Second) + `"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`),
|
|
|
|
},
|
|
|
|
|
2013-12-22 09:19:52 +00:00
|
|
|
{
|
|
|
|
name: "describe-permanode-timetravel",
|
|
|
|
setup: handlerDescribeTestSetup,
|
|
|
|
query: "describe?blobref=perma-123&at=" + addToClockOrigin(3*time.Second),
|
|
|
|
want: parseJSON(`{
|
|
|
|
"meta": {
|
2014-04-06 21:48:48 +00:00
|
|
|
"fakeref-232": {
|
|
|
|
"blobRef": "fakeref-232",
|
2013-12-22 09:19:52 +00:00
|
|
|
"size": 878
|
|
|
|
},
|
|
|
|
"perma-123": {
|
|
|
|
"blobRef": "perma-123",
|
|
|
|
"camliType": "permanode",
|
|
|
|
"size": 123,
|
|
|
|
"permanode": {
|
|
|
|
"attr": {
|
2014-04-06 21:48:48 +00:00
|
|
|
"camliContent": [ "fakeref-232" ],
|
2013-12-22 09:19:52 +00:00
|
|
|
"wont-be-present": [ "x", "y" ]
|
|
|
|
},
|
|
|
|
"modtime": "` + addToClockOrigin(3*time.Second) + `"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`),
|
|
|
|
},
|
|
|
|
|
2013-07-11 00:03:56 +00:00
|
|
|
// test that describe follows camliPath:foo attributes
|
|
|
|
{
|
|
|
|
name: "describe-permanode-follows-camliPath",
|
2013-11-16 23:00:30 +00:00
|
|
|
setup: func(fi *test.FakeIndex) index.Interface {
|
2013-08-04 02:54:30 +00:00
|
|
|
pn := blob.MustParse("perma-123")
|
2013-11-19 00:36:08 +00:00
|
|
|
fi.AddMeta(pn, "permanode", 123)
|
2014-04-06 21:48:48 +00:00
|
|
|
fi.AddClaim(owner, pn, "set-attribute", "camliPath:foo", "fakeref-123")
|
2013-07-11 00:03:56 +00:00
|
|
|
|
2014-04-06 21:48:48 +00:00
|
|
|
fi.AddMeta(blob.MustParse("fakeref-123"), "", 123)
|
2013-07-11 00:03:56 +00:00
|
|
|
return fi
|
|
|
|
},
|
|
|
|
query: "describe?blobref=perma-123",
|
|
|
|
want: parseJSON(`{
|
|
|
|
"meta": {
|
2014-04-06 21:48:48 +00:00
|
|
|
"fakeref-123": {
|
|
|
|
"blobRef": "fakeref-123",
|
|
|
|
"size": 123
|
|
|
|
},
|
|
|
|
"perma-123": {
|
|
|
|
"blobRef": "perma-123",
|
|
|
|
"camliType": "permanode",
|
|
|
|
"size": 123,
|
|
|
|
"permanode": {
|
|
|
|
"attr": {
|
|
|
|
"camliPath:foo": [
|
|
|
|
"fakeref-123"
|
|
|
|
]
|
|
|
|
},
|
2013-11-15 19:35:57 +00:00
|
|
|
"modtime": "` + addToClockOrigin(1*time.Second) + `"
|
2014-04-06 21:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-11 00:03:56 +00:00
|
|
|
}
|
|
|
|
}`),
|
|
|
|
},
|
|
|
|
|
2012-11-04 14:26:13 +00:00
|
|
|
// Test recent permanodes
|
|
|
|
{
|
2013-02-07 17:33:00 +00:00
|
|
|
name: "recent-1",
|
2013-11-16 23:00:30 +00:00
|
|
|
setup: func(*test.FakeIndex) index.Interface {
|
2012-11-04 14:26:13 +00:00
|
|
|
// 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(`{
|
2014-04-06 21:48:48 +00:00
|
|
|
"recent": [
|
|
|
|
{"blobref": "sha1-7ca7743e38854598680d94ef85348f2c48a44513",
|
|
|
|
"modtime": "2011-11-28T01:32:37.000123456Z",
|
|
|
|
"owner": "sha1-ad87ca5c78bd0ce1195c46f7c98e6025abbaf007"}
|
|
|
|
],
|
|
|
|
"meta": {
|
|
|
|
"sha1-7ca7743e38854598680d94ef85348f2c48a44513": {
|
2012-11-04 14:26:13 +00:00
|
|
|
"blobRef": "sha1-7ca7743e38854598680d94ef85348f2c48a44513",
|
|
|
|
"camliType": "permanode",
|
2014-04-06 21:48:48 +00:00
|
|
|
"permanode": {
|
|
|
|
"attr": { "title": [ "Some title" ] },
|
2013-11-15 19:35:57 +00:00
|
|
|
"modtime": "` + addToClockOrigin(1*time.Second) + `"
|
2014-04-06 21:48:48 +00:00
|
|
|
},
|
|
|
|
"size": 534
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`),
|
2012-11-04 14:26:13 +00:00
|
|
|
},
|
2012-11-05 14:09:34 +00:00
|
|
|
|
2013-03-05 18:15:22 +00:00
|
|
|
// Test recent permanode of a file
|
|
|
|
{
|
|
|
|
name: "recent-file",
|
2013-11-16 23:00:30 +00:00
|
|
|
setup: func(*test.FakeIndex) index.Interface {
|
2013-03-05 18:15:22 +00:00
|
|
|
// Ignore the fakeindex and use the real (but in-memory) implementation,
|
|
|
|
// using IndexDeps to populate it.
|
|
|
|
idx := index.NewMemoryIndex()
|
|
|
|
id := indextest.NewIndexDeps(idx)
|
|
|
|
|
|
|
|
// Upload a basic image
|
|
|
|
camliRootPath, err := osutil.GoPackagePath("camlistore.org")
|
|
|
|
if err != nil {
|
|
|
|
panic("Package camlistore.org no found in $GOPATH or $GOPATH not defined")
|
|
|
|
}
|
2013-08-04 02:54:30 +00:00
|
|
|
uploadFile := func(file string, modTime time.Time) blob.Ref {
|
2013-03-05 18:15:22 +00:00
|
|
|
fileName := filepath.Join(camliRootPath, "pkg", "index", "indextest", "testdata", file)
|
|
|
|
contents, err := ioutil.ReadFile(fileName)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
br, _ := id.UploadFile(file, string(contents), modTime)
|
|
|
|
return br
|
|
|
|
}
|
|
|
|
dudeFileRef := uploadFile("dude.jpg", time.Time{})
|
|
|
|
|
|
|
|
pn := id.NewPlannedPermanode("pn1")
|
|
|
|
id.SetAttribute(pn, "camliContent", dudeFileRef.String())
|
|
|
|
return indexAndOwner{idx, id.SignerBlobRef}
|
|
|
|
},
|
|
|
|
query: "recent",
|
|
|
|
want: parseJSON(`{
|
2014-04-06 21:48:48 +00:00
|
|
|
"recent": [
|
|
|
|
{"blobref": "sha1-7ca7743e38854598680d94ef85348f2c48a44513",
|
|
|
|
"modtime": "2011-11-28T01:32:37.000123456Z",
|
|
|
|
"owner": "sha1-ad87ca5c78bd0ce1195c46f7c98e6025abbaf007"}
|
|
|
|
],
|
|
|
|
"meta": {
|
|
|
|
"sha1-7ca7743e38854598680d94ef85348f2c48a44513": {
|
2013-03-05 18:15:22 +00:00
|
|
|
"blobRef": "sha1-7ca7743e38854598680d94ef85348f2c48a44513",
|
|
|
|
"camliType": "permanode",
|
2014-04-06 21:48:48 +00:00
|
|
|
"permanode": {
|
|
|
|
"attr": {
|
|
|
|
"camliContent": [
|
|
|
|
"sha1-e3f0ee86622dda4d7e8a4a4af51117fb79dbdbbb"
|
|
|
|
]
|
|
|
|
},
|
2013-11-15 19:35:57 +00:00
|
|
|
"modtime": "` + addToClockOrigin(1*time.Second) + `"
|
2014-04-06 21:48:48 +00:00
|
|
|
},
|
|
|
|
"size": 534
|
|
|
|
},
|
|
|
|
"sha1-e3f0ee86622dda4d7e8a4a4af51117fb79dbdbbb": {
|
|
|
|
"blobRef": "sha1-e3f0ee86622dda4d7e8a4a4af51117fb79dbdbbb",
|
|
|
|
"camliType": "file",
|
|
|
|
"size": 184,
|
|
|
|
"file": {
|
|
|
|
"fileName": "dude.jpg",
|
|
|
|
"size": 1932,
|
|
|
|
"mimeType": "image/jpeg"
|
|
|
|
},
|
|
|
|
"image": {
|
|
|
|
"width": 50,
|
|
|
|
"height": 100
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`),
|
2013-03-05 18:15:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Test recent permanode of a file, in a collection
|
|
|
|
{
|
|
|
|
name: "recent-file-collec",
|
2013-11-16 23:00:30 +00:00
|
|
|
setup: func(*test.FakeIndex) index.Interface {
|
2013-03-05 18:15:22 +00:00
|
|
|
SetTestHookBug121(func() {
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
})
|
|
|
|
// Ignore the fakeindex and use the real (but in-memory) implementation,
|
|
|
|
// using IndexDeps to populate it.
|
|
|
|
idx := index.NewMemoryIndex()
|
|
|
|
id := indextest.NewIndexDeps(idx)
|
|
|
|
|
|
|
|
// Upload a basic image
|
|
|
|
camliRootPath, err := osutil.GoPackagePath("camlistore.org")
|
|
|
|
if err != nil {
|
|
|
|
panic("Package camlistore.org no found in $GOPATH or $GOPATH not defined")
|
|
|
|
}
|
2013-08-04 02:54:30 +00:00
|
|
|
uploadFile := func(file string, modTime time.Time) blob.Ref {
|
2013-03-05 18:15:22 +00:00
|
|
|
fileName := filepath.Join(camliRootPath, "pkg", "index", "indextest", "testdata", file)
|
|
|
|
contents, err := ioutil.ReadFile(fileName)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
br, _ := id.UploadFile(file, string(contents), modTime)
|
|
|
|
return br
|
|
|
|
}
|
|
|
|
dudeFileRef := uploadFile("dude.jpg", time.Time{})
|
|
|
|
pn := id.NewPlannedPermanode("pn1")
|
|
|
|
id.SetAttribute(pn, "camliContent", dudeFileRef.String())
|
|
|
|
collec := id.NewPlannedPermanode("pn2")
|
|
|
|
id.SetAttribute(collec, "camliMember", pn.String())
|
|
|
|
return indexAndOwner{idx, id.SignerBlobRef}
|
|
|
|
},
|
|
|
|
query: "recent",
|
|
|
|
want: parseJSON(`{
|
|
|
|
"recent": [
|
2014-04-06 21:48:48 +00:00
|
|
|
{
|
|
|
|
"blobref": "sha1-3c8b5d36bd4182c6fe802984832f197786662ccf",
|
|
|
|
"modtime": "2011-11-28T01:32:38.000123456Z",
|
|
|
|
"owner": "sha1-ad87ca5c78bd0ce1195c46f7c98e6025abbaf007"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"blobref": "sha1-7ca7743e38854598680d94ef85348f2c48a44513",
|
|
|
|
"modtime": "2011-11-28T01:32:37.000123456Z",
|
|
|
|
"owner": "sha1-ad87ca5c78bd0ce1195c46f7c98e6025abbaf007"
|
|
|
|
}
|
2013-03-05 18:15:22 +00:00
|
|
|
],
|
|
|
|
"meta": {
|
2014-04-06 21:48:48 +00:00
|
|
|
"sha1-3c8b5d36bd4182c6fe802984832f197786662ccf": {
|
|
|
|
"blobRef": "sha1-3c8b5d36bd4182c6fe802984832f197786662ccf",
|
|
|
|
"camliType": "permanode",
|
|
|
|
"size": 534,
|
|
|
|
"permanode": {
|
|
|
|
"attr": {
|
|
|
|
"camliMember": [
|
|
|
|
"sha1-7ca7743e38854598680d94ef85348f2c48a44513"
|
|
|
|
]
|
|
|
|
},
|
2013-11-15 19:35:57 +00:00
|
|
|
"modtime": "` + addToClockOrigin(2*time.Second) + `"
|
2014-04-06 21:48:48 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"sha1-7ca7743e38854598680d94ef85348f2c48a44513": {
|
|
|
|
"blobRef": "sha1-7ca7743e38854598680d94ef85348f2c48a44513",
|
|
|
|
"camliType": "permanode",
|
|
|
|
"size": 534,
|
|
|
|
"permanode": {
|
|
|
|
"attr": {
|
|
|
|
"camliContent": [
|
|
|
|
"sha1-e3f0ee86622dda4d7e8a4a4af51117fb79dbdbbb"
|
|
|
|
]
|
|
|
|
},
|
2013-11-15 19:35:57 +00:00
|
|
|
"modtime": "` + addToClockOrigin(1*time.Second) + `"
|
2014-04-06 21:48:48 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"sha1-e3f0ee86622dda4d7e8a4a4af51117fb79dbdbbb": {
|
|
|
|
"blobRef": "sha1-e3f0ee86622dda4d7e8a4a4af51117fb79dbdbbb",
|
|
|
|
"camliType": "file",
|
|
|
|
"size": 184,
|
|
|
|
"file": {
|
|
|
|
"fileName": "dude.jpg",
|
|
|
|
"size": 1932,
|
|
|
|
"mimeType": "image/jpeg"
|
|
|
|
},
|
|
|
|
"image": {
|
|
|
|
"width": 50,
|
|
|
|
"height": 100
|
|
|
|
}
|
|
|
|
}
|
2013-03-05 18:15:22 +00:00
|
|
|
}
|
|
|
|
}`),
|
|
|
|
},
|
|
|
|
|
2012-11-05 14:48:13 +00:00
|
|
|
// Test recent permanodes with thumbnails
|
|
|
|
{
|
2013-02-07 17:33:00 +00:00
|
|
|
name: "recent-thumbs",
|
2013-11-16 23:00:30 +00:00
|
|
|
setup: func(*test.FakeIndex) index.Interface {
|
2012-11-05 14:48:13 +00:00
|
|
|
// 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(`{
|
2014-04-06 21:48:48 +00:00
|
|
|
"recent": [
|
|
|
|
{"blobref": "sha1-7ca7743e38854598680d94ef85348f2c48a44513",
|
|
|
|
"modtime": "2011-11-28T01:32:37.000123456Z",
|
|
|
|
"owner": "sha1-ad87ca5c78bd0ce1195c46f7c98e6025abbaf007"}
|
|
|
|
],
|
|
|
|
"meta": {
|
|
|
|
"sha1-7ca7743e38854598680d94ef85348f2c48a44513": {
|
2012-11-05 14:48:13 +00:00
|
|
|
"blobRef": "sha1-7ca7743e38854598680d94ef85348f2c48a44513",
|
|
|
|
"camliType": "permanode",
|
2014-04-06 21:48:48 +00:00
|
|
|
"permanode": {
|
|
|
|
"attr": { "title": [ "Some title" ] },
|
2013-11-15 19:35:57 +00:00
|
|
|
"modtime": "` + addToClockOrigin(1*time.Second) + `"
|
2014-04-06 21:48:48 +00:00
|
|
|
},
|
|
|
|
"size": 534,
|
|
|
|
"thumbnailHeight": 100,
|
|
|
|
"thumbnailSrc": "node.png",
|
|
|
|
"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.
|
|
|
|
{
|
2013-02-07 17:33:00 +00:00
|
|
|
name: "edge-to",
|
2013-11-16 23:00:30 +00:00
|
|
|
setup: func(*test.FakeIndex) index.Interface {
|
2012-11-05 14:09:34 +00:00
|
|
|
// 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())
|
2013-09-23 12:30:05 +00:00
|
|
|
id.DelAttribute(parent2, "camliMember", "")
|
2012-11-05 14:09:34 +00:00
|
|
|
return indexAndOwner{idx, id.SignerBlobRef}
|
|
|
|
},
|
|
|
|
query: "edgesto?blobref=sha1-9ca84f904a9bc59e6599a53f0a3927636a6dbcae",
|
2013-04-26 15:31:05 +00:00
|
|
|
want: parseJSON(`{
|
|
|
|
"toRef": "sha1-9ca84f904a9bc59e6599a53f0a3927636a6dbcae",
|
|
|
|
"edgesTo": [
|
|
|
|
{"from": "sha1-7ca7743e38854598680d94ef85348f2c48a44513",
|
|
|
|
"fromType": "permanode"}
|
|
|
|
]
|
|
|
|
}`),
|
2012-11-05 14:09:34 +00:00
|
|
|
},
|
2011-07-01 19:38:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 19:58:55 +00:00
|
|
|
func marshalJSON(v interface{}) string {
|
|
|
|
b, err := json.MarshalIndent(v, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func jmap(v interface{}) map[string]interface{} {
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
if err := json.NewDecoder(strings.NewReader(marshalJSON(v))).Decode(&m); err != nil {
|
|
|
|
panic(err)
|
2013-11-09 22:24:35 +00:00
|
|
|
}
|
2014-04-19 19:58:55 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkNoDups(sliceName string, tests []handlerTest) {
|
2013-02-07 17:33:00 +00:00
|
|
|
seen := map[string]bool{}
|
2014-04-19 19:58:55 +00:00
|
|
|
for _, tt := range tests {
|
2013-02-07 17:33:00 +00:00
|
|
|
if seen[tt.name] {
|
2014-04-19 19:58:55 +00:00
|
|
|
panic(fmt.Sprintf("duplicate handlerTest named %q in var %s", tt.name, sliceName))
|
2013-02-07 17:33:00 +00:00
|
|
|
}
|
|
|
|
seen[tt.name] = true
|
2014-04-19 19:58:55 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-07 17:33:00 +00:00
|
|
|
|
2014-04-19 19:58:55 +00:00
|
|
|
func init() {
|
|
|
|
checkNoDups("handlerTests", handlerTests)
|
|
|
|
}
|
2012-11-04 14:26:13 +00:00
|
|
|
|
2014-04-19 19:58:55 +00:00
|
|
|
func (ht handlerTest) test(t *testing.T) {
|
|
|
|
SetTestHookBug121(func() {})
|
2012-11-04 12:59:19 +00:00
|
|
|
|
2014-04-19 19:58:55 +00:00
|
|
|
fakeIndex := test.NewFakeIndex()
|
|
|
|
idx := ht.setup(fakeIndex)
|
2012-11-04 12:59:19 +00:00
|
|
|
|
2014-04-19 19:58:55 +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
|
|
|
|
2014-04-19 19:58:55 +00:00
|
|
|
var body io.Reader
|
|
|
|
var method = "GET"
|
|
|
|
if ht.postBody != "" {
|
|
|
|
method = "POST"
|
|
|
|
body = strings.NewReader(ht.postBody)
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest(method, "/camli/search/"+ht.query, body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: bad query: %v", ht.name, err)
|
|
|
|
}
|
|
|
|
req.Header.Set(httputil.PathSuffixHeader, req.URL.Path[1:])
|
2012-11-04 12:59:19 +00:00
|
|
|
|
2014-04-19 19:58:55 +00:00
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
rr.Body = new(bytes.Buffer)
|
2013-02-07 17:33:00 +00:00
|
|
|
|
2014-04-19 19:58:55 +00:00
|
|
|
h.ServeHTTP(rr, req)
|
|
|
|
got := rr.Body.Bytes()
|
2013-02-07 17:33:00 +00:00
|
|
|
|
2014-04-19 19:58:55 +00:00
|
|
|
if len(ht.wantDescribed) > 0 {
|
|
|
|
dr := new(DescribeResponse)
|
|
|
|
if err := json.NewDecoder(bytes.NewReader(got)).Decode(dr); err != nil {
|
|
|
|
t.Fatalf("On test %s: Non-JSON response: %s", ht.name, got)
|
|
|
|
}
|
|
|
|
var gotDesc []string
|
|
|
|
for k := range dr.Meta {
|
|
|
|
gotDesc = append(gotDesc, k)
|
|
|
|
}
|
|
|
|
sort.Strings(ht.wantDescribed)
|
|
|
|
sort.Strings(gotDesc)
|
|
|
|
if !reflect.DeepEqual(gotDesc, ht.wantDescribed) {
|
|
|
|
t.Errorf("On test %s: described blobs:\n%v\nwant:\n%v\n",
|
|
|
|
ht.name, gotDesc, ht.wantDescribed)
|
2013-02-07 17:33:00 +00:00
|
|
|
}
|
2014-04-19 19:58:55 +00:00
|
|
|
if ht.want == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
want, _ := json.MarshalIndent(ht.want, "", " ")
|
|
|
|
trim := bytes.TrimSpace
|
|
|
|
|
|
|
|
if bytes.Equal(trim(got), trim(want)) {
|
|
|
|
return
|
|
|
|
}
|
2013-02-07 17:33:00 +00:00
|
|
|
|
2014-04-19 19:58:55 +00:00
|
|
|
// Try with re-encoded got, since the JSON ordering doesn't matter
|
|
|
|
// to the test,
|
|
|
|
gotj := parseJSON(string(got))
|
|
|
|
got2, _ := json.MarshalIndent(gotj, "", " ")
|
|
|
|
if bytes.Equal(got2, want) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
diff := test.Diff(want, got2)
|
|
|
|
|
|
|
|
t.Errorf("test %s:\nwant: %s\n got: %s\ndiff:\n%s", ht.name, want, got, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandler(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping in short mode")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer SetTestHookBug121(func() {})
|
|
|
|
for _, ht := range handlerTests {
|
|
|
|
ht.test(t)
|
2011-07-01 19:38:00 +00:00
|
|
|
}
|
2011-07-01 19:00:10 +00:00
|
|
|
}
|