Exemplo n.º 1
0
func TestPutDatastoreMultiError(t *testing.T) {
	c, closeFunc := NewContext(t)
	defer closeFunc()

	type testEntity struct {
		IntVal int
	}

	expectedErr := errors.New("expected error")

	nds.SetDatastorePutMulti(func(c context.Context,
		keys []*datastore.Key, vals interface{}) ([]*datastore.Key, error) {
		return nil, appengine.MultiError{expectedErr}
	})

	defer func() {
		nds.SetDatastorePutMulti(datastore.PutMulti)
	}()

	key := datastore.NewKey(c, "Test", "", 1, nil)
	val := &testEntity{42}

	if _, err := nds.Put(c, key, val); err == nil {
		t.Fatal("expected error")
	} else if err != expectedErr {
		t.Fatal("should be expectedErr")
	}
}
Exemplo n.º 2
0
func TestPutMultiError(t *testing.T) {
	c, closeFunc := NewContext(t)
	defer closeFunc()

	expectedErrs := appengine.MultiError{
		nil,
		errors.New("expected error"),
	}

	nds.SetDatastorePutMulti(func(c context.Context,
		keys []*datastore.Key, vals interface{}) ([]*datastore.Key, error) {
		return keys, expectedErrs
	})
	defer nds.SetDatastorePutMulti(datastore.PutMulti)

	keys := []*datastore.Key{
		datastore.NewKey(c, "Test", "", 1, nil),
		datastore.NewKey(c, "Test", "", 2, nil),
	}

	type TestEntity struct {
		Value int
	}
	entities := []TestEntity{
		{1},
		{2},
	}

	_, err := nds.PutMulti(c, keys, entities)
	me, ok := err.(appengine.MultiError)
	if !ok {
		t.Fatal("expected appengine.MultiError")
	}
	for i, e := range me {
		if e != expectedErrs[i] {
			t.Fatal("error incorrect")
		}
	}
}
Exemplo n.º 3
0
func TestPutGetDelete(t *testing.T) {
	c, closeFunc := NewContext(t)
	defer closeFunc()

	type testEntity struct {
		IntVal int
	}

	// Check we set memcahce, put datastore and delete memcache.
	seq := make(chan string, 3)
	nds.SetMemcacheSetMulti(func(c context.Context,
		items []*memcache.Item) error {
		seq <- "memcache.SetMulti"
		return memcache.SetMulti(c, items)
	})
	nds.SetDatastorePutMulti(func(c context.Context,
		keys []*datastore.Key, vals interface{}) ([]*datastore.Key, error) {
		seq <- "datastore.PutMulti"
		return datastore.PutMulti(c, keys, vals)
	})
	nds.SetMemcacheDeleteMulti(func(c context.Context,
		keys []string) error {
		seq <- "memcache.DeleteMulti"
		close(seq)
		return memcache.DeleteMulti(c, keys)
	})

	incompleteKey := datastore.NewIncompleteKey(c, "Entity", nil)
	key, err := nds.Put(c, incompleteKey, &testEntity{43})
	if err != nil {
		t.Fatal(err)
	}

	nds.SetMemcacheSetMulti(memcache.SetMulti)
	nds.SetDatastorePutMulti(datastore.PutMulti)
	nds.SetMemcacheDeleteMulti(memcache.DeleteMulti)

	if s := <-seq; s != "memcache.SetMulti" {
		t.Fatal("memcache.SetMulti not", s)
	}
	if s := <-seq; s != "datastore.PutMulti" {
		t.Fatal("datastore.PutMulti not", s)
	}
	if s := <-seq; s != "memcache.DeleteMulti" {
		t.Fatal("memcache.DeleteMulti not", s)
	}
	// Check chan is closed.
	<-seq

	if key.Incomplete() {
		t.Fatal("Key is incomplete")
	}

	te := &testEntity{}
	if err := nds.Get(c, key, te); err != nil {
		t.Fatal(err)
	}

	if te.IntVal != 43 {
		t.Fatal("te.Val != 43", te.IntVal)
	}

	// Get from cache.
	te = &testEntity{}
	if err := nds.Get(c, key, te); err != nil {
		t.Fatal(err)
	}

	if te.IntVal != 43 {
		t.Fatal("te.Val != 43", te.IntVal)
	}

	// Change value.
	if _, err := nds.Put(c, key, &testEntity{64}); err != nil {
		t.Fatal(err)
	}

	// Get from cache.
	te = &testEntity{}
	if err := nds.Get(c, key, te); err != nil {
		t.Fatal(err)
	}

	if te.IntVal != 64 {
		t.Fatal("te.Val != 64", te.IntVal)
	}

	if err := nds.Delete(c, key); err != nil {
		t.Fatal(err)
	}

	if err := nds.Get(c, key, &testEntity{}); err != datastore.ErrNoSuchEntity {
		t.Fatal("expected datastore.ErrNoSuchEntity")
	}
}