Ejemplo n.º 1
0
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")
		}
	}
}
Ejemplo n.º 2
0
func TestMartialPropertyListError(t *testing.T) {

	type testEntity struct {
		IntVal int
	}

	pl := datastore.PropertyList{
		datastore.Property{"Prop", &testEntity{3}, false, false},
	}
	if _, err := nds.MarshalPropertyList(pl); err == nil {
		t.Fatal("expected error")
	}
}
Ejemplo n.º 3
0
func TestMarshalUnmarshalPropertyList(t *testing.T) {
	c, closeFunc := NewContext(t)
	defer closeFunc()

	timeVal := time.Now()
	timeProp := datastore.Property{Name: "Time",
		Value: timeVal, NoIndex: false, Multiple: false}

	byteStringVal := datastore.ByteString{0x23}
	byteStringProp := datastore.Property{Name: "ByteString",
		Value: byteStringVal, NoIndex: false, Multiple: false}

	keyVal := datastore.NewKey(c, "Entity", "stringID", 0, nil)
	keyProp := datastore.Property{Name: "Key",
		Value: keyVal, NoIndex: false, Multiple: false}

	blobKeyVal := appengine.BlobKey("blobkey")
	blobKeyProp := datastore.Property{Name: "BlobKey",
		Value: blobKeyVal, NoIndex: false, Multiple: false}

	geoPointVal := appengine.GeoPoint{1, 2}
	geoPointProp := datastore.Property{Name: "GeoPoint",
		Value: geoPointVal, NoIndex: false, Multiple: false}

	pl := datastore.PropertyList{
		timeProp,
		byteStringProp,
		keyProp,
		blobKeyProp,
		geoPointProp,
	}
	data, err := nds.MarshalPropertyList(pl)
	if err != nil {
		t.Fatal(err)
	}

	testEntity := &struct {
		Time       time.Time
		ByteString datastore.ByteString
		Key        *datastore.Key
		BlobKey    appengine.BlobKey
		GeoPoint   appengine.GeoPoint
	}{}

	pl = datastore.PropertyList{}
	if err := nds.UnmarshalPropertyList(data, &pl); err != nil {
		t.Fatal(err)
	}
	if err := nds.SetValue(reflect.ValueOf(testEntity), pl); err != nil {
		t.Fatal(err)
	}

	if !testEntity.Time.Equal(timeVal) {
		t.Fatal("timeVal not equal")
	}

	if string(testEntity.ByteString) != string(byteStringVal) {
		t.Fatal("byteStringVal not equal")
	}

	if !testEntity.Key.Equal(keyVal) {
		t.Fatal("keyVal not equal")
	}

	if testEntity.BlobKey != blobKeyVal {
		t.Fatal("blobKeyVal not equal")
	}

	if !reflect.DeepEqual(testEntity.GeoPoint, geoPointVal) {
		t.Fatal("geoPointVal not equal")
	}
}