本文整理匯總了Golang中github.com/qedus/nds.GetMulti函數的典型用法代碼示例。如果您正苦於以下問題:Golang GetMulti函數的具體用法?Golang GetMulti怎麽用?Golang GetMulti使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了GetMulti函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestGetMultiArgs
func TestGetMultiArgs(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
IntVal int64
}
key := datastore.NewKey(c, "Entity", "", 1, nil)
keys := []*datastore.Key{key}
val := testEntity{}
if err := nds.GetMulti(c, keys, nil); err == nil {
t.Fatal("expected error for nil vals")
}
structVals := []testEntity{val}
if err := nds.GetMulti(c, nil, structVals); err == nil {
t.Fatal("expected error for nil keys")
}
if err := nds.GetMulti(c, keys, []testEntity{}); err == nil {
t.Fatal("expected error for unequal keys and vals")
}
if err := nds.GetMulti(c, keys, datastore.PropertyList{}); err == nil {
t.Fatal("expected error for propertyList")
}
rte := newReaderTestEntity()
if err := nds.GetMulti(c, keys, []io.Reader{rte}); err == nil {
t.Fatal("expected error for interface")
}
}
示例2: TestGetMultiInterface
func TestGetMultiInterface(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
IntVal int64
}
keys := []*datastore.Key{}
entities := []testEntity{}
for i := int64(1); i < 3; i++ {
keys = append(keys, datastore.NewKey(c, "Entity", "", i, nil))
entities = append(entities, testEntity{i})
}
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
// Get from datastore.
response := make([]interface{}, len(keys))
for i := 0; i < len(response); i++ {
response[i] = &testEntity{}
}
if err := nds.GetMulti(c, keys, response); err != nil {
t.Fatal(err)
}
for i := int64(0); i < 2; i++ {
if te, ok := response[i].(*testEntity); ok {
if te.IntVal != i+1 {
t.Fatal("incorrect IntVal")
}
} else {
t.Fatal("incorrect type")
}
}
// Get from cache.
response = make([]interface{}, len(keys))
for i := 0; i < len(response); i++ {
response[i] = &testEntity{}
}
if err := nds.GetMulti(c, keys, response); err != nil {
t.Fatal(err)
}
for i := int64(0); i < 2; i++ {
if te, ok := response[i].(*testEntity); ok {
if te.IntVal != i+1 {
t.Fatal("incorrect IntVal")
}
} else {
t.Fatal("incorrect type")
}
}
}
示例3: TestRunInTransaction
func TestRunInTransaction(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
Val int
}
key := datastore.NewKey(c, "Entity", "", 3, nil)
keys := []*datastore.Key{key}
entity := testEntity{42}
entities := []testEntity{entity}
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
err := nds.RunInTransaction(c, func(tc context.Context) error {
entities := make([]testEntity, 1, 1)
if err := nds.GetMulti(tc, keys, entities); err != nil {
t.Fatal(err)
}
entity := entities[0]
if entity.Val != 42 {
t.Fatalf("entity.Val != 42: %d", entity.Val)
}
entities[0].Val = 43
putKeys, err := nds.PutMulti(tc, keys, entities)
if err != nil {
t.Fatal(err)
} else if len(putKeys) != 1 {
t.Fatal("putKeys should be len 1")
} else if !putKeys[0].Equal(key) {
t.Fatal("keys not equal")
}
return nil
}, nil)
if err != nil {
t.Fatal(err)
}
entities = make([]testEntity, 1, 1)
if err := nds.GetMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
entity = entities[0]
if entity.Val != 43 {
t.Fatalf("entity.Val != 43: %d", entity.Val)
}
}
示例4: TestDeleteMulti
func TestDeleteMulti(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type TestEntity struct {
Value int
}
for _, count := range []int{499, 500, 501} {
keys := make([]*datastore.Key, count)
entities := make([]TestEntity, count)
for i := range keys {
keys[i] = datastore.NewKey(c, "TestEntity", strconv.Itoa(i), 0, nil)
entities[i] = TestEntity{i}
}
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
// Prime cache.
entities = make([]TestEntity, count)
if err := nds.GetMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
if err := nds.DeleteMulti(c, keys); err != nil {
t.Fatal(err)
}
err := nds.GetMulti(c, keys, make([]TestEntity, count))
if err == nil {
t.Fatal("expect error")
}
me, ok := err.(appengine.MultiError)
if !ok {
t.Fatal("should be MultiError")
}
for _, e := range me {
if e != datastore.ErrNoSuchEntity {
t.Fatal("expected ErrNoSuchEntity")
}
}
}
}
示例5: TestGetMultiNoSuchEntity
func TestGetMultiNoSuchEntity(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
Val int
}
// Test no such entity.
for _, count := range []int{999, 1000, 1001} {
keys := []*datastore.Key{}
entities := []*testEntity{}
for i := 0; i < count; i++ {
keys = append(keys,
datastore.NewKey(c, "Test", strconv.Itoa(i), 0, nil))
entities = append(entities, &testEntity{})
}
err := nds.GetMulti(c, keys, entities)
if me, ok := err.(appengine.MultiError); ok {
if len(me) != count {
t.Fatal("multi error length incorrect")
}
for _, e := range me {
if e != datastore.ErrNoSuchEntity {
t.Fatal("expecting datastore.ErrNoSuchEntity but got", e)
}
}
}
}
}
示例6: TestPutPropertyLoadSaver
func TestPutPropertyLoadSaver(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
IntVal int
}
te := &testEntity{2}
pl, err := datastore.SaveStruct(te)
if err != nil {
t.Fatal(err)
}
keys := []*datastore.Key{datastore.NewKey(c, "Test", "", 1, nil)}
pls := datastore.PropertyList(pl)
if _, err := nds.PutMulti(c, keys,
[]datastore.PropertyLoadSaver{&pls}); err != nil {
t.Fatal(err)
}
getPl := datastore.PropertyList{}
if err := nds.GetMulti(c,
keys, []datastore.PropertyLoadSaver{&getPl}); err != nil {
t.Fatal(err)
}
getTe := &testEntity{}
if err := datastore.LoadStruct(getTe, getPl); err != nil {
t.Fatal(err)
}
if te.IntVal != getTe.IntVal {
t.Fatal("expected same IntVal", getTe.IntVal)
}
}
示例7: TestGetMultiLockReturnEntitySetValueFail
func TestGetMultiLockReturnEntitySetValueFail(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
IntVal int64
}
keys := []*datastore.Key{}
entities := []testEntity{}
for i := int64(1); i < 3; i++ {
keys = append(keys, datastore.NewKey(c, "Entity", "", i, nil))
entities = append(entities, testEntity{i})
}
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
// Fail to unmarshal test.
memcacheGetChan := make(chan func(c context.Context, keys []string) (
map[string]*memcache.Item, error), 2)
memcacheGetChan <- memcache.GetMulti
memcacheGetChan <- func(c context.Context,
keys []string) (map[string]*memcache.Item, error) {
items, err := memcache.GetMulti(c, keys)
if err != nil {
return nil, err
}
pl := datastore.PropertyList{
datastore.Property{"One", 1, false, false},
}
value, err := nds.MarshalPropertyList(pl)
if err != nil {
return nil, err
}
items[keys[0]].Flags = nds.EntityItem
items[keys[0]].Value = value
items[keys[1]].Flags = nds.EntityItem
items[keys[1]].Value = value
return items, nil
}
nds.SetMemcacheGetMulti(func(c context.Context,
keys []string) (map[string]*memcache.Item, error) {
f := <-memcacheGetChan
return f(c, keys)
})
response := make([]testEntity, len(keys))
if err := nds.GetMulti(c, keys, response); err != nil {
t.Fatal(err)
}
defer nds.SetMemcacheGetMulti(memcache.GetMulti)
for i := 0; i < len(keys); i++ {
if entities[i].IntVal != response[i].IntVal {
t.Fatal("IntVal not equal")
}
}
}
示例8: TestPutMulti
func TestPutMulti(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type TestEntity struct {
Value int
}
for _, count := range []int{499, 500, 501} {
keys := make([]*datastore.Key, count)
entities := make([]TestEntity, count)
for i := range keys {
keys[i] = datastore.NewKey(c, "TestEntity", strconv.Itoa(i), 0, nil)
entities[i] = TestEntity{i}
}
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
entities = make([]TestEntity, count)
if err := nds.GetMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
}
}
示例9: TestGetMultiInterfaceError
func TestGetMultiInterfaceError(t *testing.T) {
c, closeFunc := NewContext(t, nil)
defer closeFunc()
type testEntity struct {
IntVal int64
}
keys := []*datastore.Key{}
entities := []testEntity{}
for i := int64(1); i < 3; i++ {
keys = append(keys, datastore.NewKey(c, "Entity", "", i, nil))
entities = append(entities, testEntity{i})
}
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
// Get from datastore.
// No errors expected.
response := []interface{}{&testEntity{}, &testEntity{}}
if err := nds.GetMulti(c, keys, response); err != nil {
t.Fatal(err)
}
for i := int64(0); i < 2; i++ {
if te, ok := response[i].(*testEntity); ok {
if te.IntVal != i+1 {
t.Fatal("incorrect IntVal")
}
} else {
t.Fatal("incorrect type")
}
}
// Get from cache.
// Errors expected.
response = []interface{}{&testEntity{}, testEntity{}}
if err := nds.GetMulti(c, keys, response); err == nil {
t.Fatal("expected invalid entity type error")
}
}
示例10: TestGetMultiNoPropertyList
func TestGetMultiNoPropertyList(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
keys := []*datastore.Key{datastore.NewKey(c, "Test", "", 1, nil)}
pl := datastore.PropertyList{datastore.Property{}}
if err := nds.GetMulti(c, keys, pl); err == nil {
t.Fatal("expecting no PropertyList error")
}
}
示例11: TestGetMultiNonStruct
func TestGetMultiNonStruct(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
keys := []*datastore.Key{datastore.NewKey(c, "Test", "", 1, nil)}
vals := []int{12}
if err := nds.GetMulti(c, keys, vals); err == nil {
t.Fatal("expecting unsupported vals type")
}
}
示例12: TestGetMultiStructPtrNil
func TestGetMultiStructPtrNil(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
IntVal int64
}
keys := []*datastore.Key{}
entities := []testEntity{}
for i := int64(1); i < 3; i++ {
keys = append(keys, datastore.NewKey(c, "Entity", "", i, nil))
entities = append(entities, testEntity{i})
}
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
// Get from datastore.
response := make([]*testEntity, len(keys))
if err := nds.GetMulti(c, keys, response); err != nil {
t.Fatal(err)
}
for i := int64(0); i < 2; i++ {
if response[i].IntVal != i+1 {
t.Fatal("incorrect IntVal")
}
}
// Get from cache.
response = make([]*testEntity, len(keys))
if err := nds.GetMulti(c, keys, response); err != nil {
t.Fatal(err)
}
for i := int64(0); i < 2; i++ {
if response[i].IntVal != i+1 {
t.Fatal("incorrect IntVal")
}
}
}
示例13: TestDelete
func TestDelete(t *testing.T) {
c, closeFunc := NewContext(t, nil)
defer closeFunc()
type testEntity struct {
Val int
}
key := datastore.NewKey(c, "Entity", "", 1, nil)
keys := []*datastore.Key{key}
entities := make([]testEntity, 1)
entities[0].Val = 43
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
entities = make([]testEntity, 1)
if err := nds.GetMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
entity := entities[0]
if entity.Val != 43 {
t.Fatal("incorrect entity.Val", entity.Val)
}
if err := nds.DeleteMulti(c, keys); err != nil {
t.Fatal(err)
}
keys = []*datastore.Key{key}
entities = make([]testEntity, 1)
err := nds.GetMulti(c, keys, entities)
if me, ok := err.(appengine.MultiError); ok {
if me[0] != datastore.ErrNoSuchEntity {
t.Fatal("entity should be deleted", entities)
}
} else {
t.Fatal("expected appengine.MultiError")
}
}
示例14: TestGetMultiLockReturnUnknown
func TestGetMultiLockReturnUnknown(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
IntVal int64
}
keys := []*datastore.Key{}
entities := []testEntity{}
for i := int64(1); i < 3; i++ {
keys = append(keys, datastore.NewKey(c, "Entity", "", i, nil))
entities = append(entities, testEntity{i})
}
if _, err := nds.PutMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
memcacheGetChan := make(chan func(c context.Context, keys []string) (
map[string]*memcache.Item, error), 2)
memcacheGetChan <- memcache.GetMulti
memcacheGetChan <- func(c context.Context,
keys []string) (map[string]*memcache.Item, error) {
items, err := memcache.GetMulti(c, keys)
if err != nil {
return nil, err
}
// Unknown lock values.
items[keys[0]].Flags = 23
items[keys[1]].Flags = 24
return items, nil
}
nds.SetMemcacheGetMulti(func(c context.Context,
keys []string) (map[string]*memcache.Item, error) {
f := <-memcacheGetChan
return f(c, keys)
})
response := make([]testEntity, len(keys))
if err := nds.GetMulti(c, keys, response); err != nil {
t.Fatal(err)
}
defer nds.SetMemcacheGetMulti(memcache.GetMulti)
for i := 0; i < len(keys); i++ {
if entities[i].IntVal != response[i].IntVal {
t.Fatal("IntVal not equal")
}
}
}
示例15: TestGetMultiNoKeys
func TestGetMultiNoKeys(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
IntVal int64
}
keys := []*datastore.Key{}
entities := []testEntity{}
if err := nds.GetMulti(c, keys, entities); err != nil {
t.Fatal(err)
}
}