Example #1
0
func Test_EntityQuery(t *testing.T) {
	assert := gaetest.NewAssert(t)
	router := wcg.NewRouter()
	router.GET("/api/",
		EntityQuery(testEntity.Query().Limit(2)),
	)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "5", "ID": "5"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "6", "ID": "6"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "7", "ID": "7"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "8", "ID": "8"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "9", "ID": "9"},
	})

	var got entities.Pagination
	req := ts.GET("/api/")
	res := req.RouteTo(router)
	assert.HTTPStatus(200, res)
	assert.JSONResponse(&got, res)
	assert.EqInt(2, got.Length())
	assert.EqInt(2, got.Next.N)
	assert.EqInt(1, got.Next.P)
	assert.Nil(got.Previous)
	// TODO: got does not has []TEntity, but has []map[string]interface{}.
	// Need  to provide a way to make got have []TEntity or []*TEntity
}
Example #2
0
func Test_Delete_CacheInvalidation(t *testing.T) {
	assert := gaetest.NewAssert(t)
	driver := memcache.NewDriver(ts.Context, wcg.NewLogger(nil))

	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "5", "ID": "5"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "6", "ID": "6"},
	})

	// create caches
	testEntity.Query().Cache("123").MustExecute(ts.GET("/").Request)
	testEntity.Get().Key("1").Cache(true).MustOne(ts.GET("/").Request)

	assert.OK(driver.Exists("ent.TestEntity.1"), "An entyty cache should exist")
	assert.OK(driver.Exists("query.TestEntity.123"), "A query cache should exist")

	testEntity.Delete().Key("1").Cache("123").MustCommit(ts.GET("/").Request)

	assert.Not(driver.Exists("ent.TestEntity.1"), "Entity cache should be invalidated")
	assert.Not(driver.Exists("query.TestEntity.123"), "Query cache should be invalidated")
}
Example #3
0
func Test_Query(t *testing.T) {
	assert := gaetest.NewAssert(t)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "5", "ID": "5"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "6", "ID": "6"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "7", "ID": "7"},
	})

	// no pagination parameter specified -> returns all entities
	page := testEntity.Query().MustExecute(ts.GET("/").Request)
	got := page.Data.([]TEntity)
	assert.EqInt(7, len(got), "len(Pagination.Data)")
	assert.Nil(page.Next, "Pagination.Next")
	assert.Nil(page.Previous, "Pagination.Previous")
	assert.EqStr("1", got[0].ID, "Pagination.Data[0].ID")
	assert.EqStr("2", got[1].ID, "Pagination.Data[1].ID")
	assert.EqStr("3", got[2].ID, "Pagination.Data[2].ID")
	assert.EqStr("4", got[3].ID, "Pagination.Data[3].ID")
	assert.EqStr("5", got[4].ID, "Pagination.Data[4].ID")
	assert.EqStr("6", got[5].ID, "Pagination.Data[5].ID")
	assert.EqStr("7", got[6].ID, "Pagination.Data[6].ID")
}
Example #4
0
func Test_GetMulti_Cache(t *testing.T) {
	assert := gaetest.NewAssert(t)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
	})

	driver := memcache.NewDriver(ts.Context, wcg.NewLogger(nil))

	_, list, err := testEntity.GetMulti().Keys("1", "3").Cache(true).List(ts.GET("/").Request)
	got := list.([]*TEntity)
	assert.Nil(err)
	assert.EqInt(2, len(got))
	assert.EqStr("1", got[0].ID)
	assert.Nil(got[1])

	assert.OK(driver.Exists("ent.TestEntity.1"))
	assert.OK(driver.Exists("ent.TestEntity.3"))

	var t1, t3 TEntity
	driver.Get("ent.TestEntity.1", &t1)
	driver.Get("ent.TestEntity.3", &t3)
	assert.EqStr("1", t1.ID)
	assert.EqStr("", t3.ID)

	got = make([]*TEntity, 2)
	driver.GetMulti([]string{"ent.TestEntity.1", "ent.TestEntity.3"}, got)
	assert.EqStr("1", got[0].ID)
	assert.Nil(got[1])
}
Example #5
0
func Test_EntityStreaming(t *testing.T) {
	assert := gaetest.NewAssert(t)
	router := wcg.NewRouter()
	router.GET("/api/",
		EntityStreaming(testEntity.Query(), false),
	)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"},
	})
	req := ts.GET("/api/")
	res := req.RouteTo(router)
	assert.HTTPStatus(200, res)
	assert.HTTPBodyString(
		`{"id":"1","value":0,"updated_at":"0001-01-01T00:00:00Z"}
{"id":"2","value":0,"updated_at":"0001-01-01T00:00:00Z"}
{"id":"3","value":0,"updated_at":"0001-01-01T00:00:00Z"}
{"id":"4","value":0,"updated_at":"0001-01-01T00:00:00Z"}
`,
		res,
	)
}
Example #6
0
func Test_Put_Update(t *testing.T) {
	assert := gaetest.NewAssert(t)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "5", "ID": "5"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "6", "ID": "6"},
	})

	var ent, err = testEntity.CreateEntityFromForm(url.Values{
		"desc":          []string{"hogehoge"},
		"digit":         []string{"2"},
		"content_bytes": []string{"abcdef"},
	})

	_, _ent, err := testEntity.Put().Key("1").Update(ts.PUTForm("/1", nil).Request, ent)
	got := _ent.(*TEntity)
	assert.Nil(err)
	assert.EqStr("1", got.ID)
	assert.EqInt(2, got.Digit)
	assert.EqStr("hogehoge", got.Desc)
	assert.EqStr("abcdef", string(got.ContentBytes))
	assert.OK(got.BeforeSaveProcessed, "BeforeSave triggerd")
	assert.OK(got.AfterSaveProcessed, "AfterSave triggerd")
}
Example #7
0
func Test_EntityPut(t *testing.T) {
	assert := gaetest.NewAssert(t)
	router := wcg.NewRouter()
	router.PUT("/api/:key.json",
		ParseForm(nil),
		EntityPut(testEntity.Put(), "key"),
	)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
	})

	var got TEntity
	req := ts.PUTForm("/api/1.json", url.Values{
		"value": []string{"1"},
	})
	res := req.RouteTo(router)
	assert.HTTPStatus(200, res)
	assert.JSONResponse(&got, res)
	assert.EqStr("1", got.ID)
	assert.EqInt(1, got.Value)

	_, one := testEntity.Get().Key(got.ID).MustOne(ts.GET("/").Request)
	assert.EqStr(got.ID, one.(*TEntity).ID)
	assert.EqInt(1, one.(*TEntity).Value)
}
Example #8
0
func Test_Query_WithCache(t *testing.T) {
	assert := gaetest.NewAssert(t)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "5", "ID": "5"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "6", "ID": "6"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "7", "ID": "7"},
	})
	page, err := testEntity.Query().Cache("123").Execute(ts.GET("/").Request)
	got := page.Data.([]TEntity)
	assert.Nil(err)
	assert.EqInt(7, len(got), "len(Pagination.Data)")

	driver := memcache.NewDriver(ts.Context, wcg.NewLogger(nil))
	assert.OK(driver.Exists("query.TestEntity.123"))

	// cache still exists even after removing data.
	gaetest.CleanupDatastore(ts.Context)
	page, err = testEntity.Query().Cache("123").Execute(ts.GET("/").Request)
	got = page.Data.([]TEntity)
	assert.Nil(err)
	assert.EqInt(7, len(got))
	assert.EqStr("1", got[0].ID)
}
Example #9
0
func Test_Delete(t *testing.T) {
	assert := gaetest.NewAssert(t)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "5", "ID": "5"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "6", "ID": "6"},
	})

	key := testEntity.Delete().Key("1").MustCommit(ts.DELETE("/").Request)
	assert.EqStr("1", key.StringID())
}
Example #10
0
func Test_DeleteMulti(t *testing.T) {
	assert := gaetest.NewAssert(t)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "5", "ID": "5"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "6", "ID": "6"},
	})

	keys, err := testEntity.DeleteMulti().Keys("1", "2").Commit(ts.GET("/").Request)
	assert.Nil(err)
	assert.EqInt(2, len(keys), "2 keys should be deleted")
}
Example #11
0
func Test_Query_WithPagination(t *testing.T) {
	assert := gaetest.NewAssert(t)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "5", "ID": "5"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "6", "ID": "6"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "7", "ID": "7"},
	})
	// pagination with n = 3
	page := testEntity.Query().MustExecute(ts.GET("/?p=0&n=3").Request)
	got := page.Data.([]TEntity)
	assert.EqInt(3, len(got), "len(Pagination.Data)")
	assert.Nil(page.Previous, "Pagination.Previous")
	assert.NotNil(page.Next, "Pagination.Next")
	assert.EqInt(1, page.Next.P, "Pagination.Next.P")
	assert.EqInt(3, page.Next.N, "Pagination.Next.N")
	assert.EqStr("1", got[0].ID, "Pagination.Data[0].ID")
	assert.EqStr("2", got[1].ID, "Pagination.Data[1].ID")
	assert.EqStr("3", got[2].ID, "Pagination.Data[2].ID")

	page = testEntity.Query().MustExecute(ts.GET("/?p=1&n=3").Request)
	got = page.Data.([]TEntity)
	assert.EqInt(3, len(got), "len(Pagination.Data)")
	assert.NotNil(page.Previous, "Pagination.Previous")
	assert.EqInt(0, page.Previous.P, "Pagination.Next.P")
	assert.EqInt(3, page.Previous.N, "Pagination.Next.N")
	assert.NotNil(page.Next, "Pagination.Next")
	assert.EqInt(2, page.Next.P, "Pagination.Next.P")
	assert.EqInt(3, page.Next.N, "Pagination.Next.N")
	assert.EqStr("4", got[0].ID, "Pagination.Data[0].ID (got = 1)")
	assert.EqStr("5", got[1].ID, "Pagination.Data[1].ID (got = 1)")
	assert.EqStr("6", got[2].ID, "Pagination.Data[2].ID (got = 1)")

	page = testEntity.Query().MustExecute(ts.GET("/?p=2&n=3").Request)
	got = page.Data.([]TEntity)
	assert.EqInt(1, len(got), "len(Pagination.Data)")
	assert.NotNil(page.Previous, "Pagination.Previous")
	assert.EqInt(1, page.Previous.P, "Pagination.Next.P")
	assert.EqInt(3, page.Previous.N, "Pagination.Next.N")
	assert.Nil(page.Next, "Pagination.Next")
	assert.EqStr("7", got[0].ID, "Pagination.Data[0].ID (got = 2)")
}
Example #12
0
func Test_DeleteMulti_NonExistingKey(t *testing.T) {
	assert := gaetest.NewAssert(t)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "5", "ID": "5"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "6", "ID": "6"},
	})

	keys, err := testEntity.DeleteMulti().Keys("not-exist").Commit(ts.GET("/").Request)
	assert.Nil(err)
	assert.EqInt(1, len(keys), "Should return num keys even if they do not exist")
	assert.EqStr("not-exist", keys[0].StringID())
}
Example #13
0
func Test_DeleteQuery(t *testing.T) {
	assert := gaetest.NewAssert(t)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "5", "ID": "5"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "6", "ID": "6"},
	})

	deleted := testEntity.DeleteQuery().Filter("ID =", "3").MustCommit(ts.GET("/").Request)
	assert.EqInt(1, deleted)

	exists := testEntity.Query().MustCount(ts.GET("/").Request)
	assert.EqInt(5, exists)
}
Example #14
0
func Test_Query_WithProject(t *testing.T) {
	assert := gaetest.NewAssert(t)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1", "Digit": 11},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2", "Digit": 12},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "5", "ID": "5"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "6", "ID": "6"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "7", "ID": "7"},
	})
	page := testEntity.Query().Project("Digit").MustExecute(ts.GET("/").Request)
	got := page.Data.([]TEntity)
	assert.EqInt(2, len(got), "len(Pagination.Data)")
	assert.EqStr("", got[0].ID)
	assert.EqInt(11, got[0].Digit)
}
Example #15
0
func Test_EntityDelete(t *testing.T) {
	assert := gaetest.NewAssert(t)
	router := wcg.NewRouter()
	router.GET("/api/:key.json",
		EntityDelete(testEntity.Delete(), "key"),
	)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
	})

	req := ts.GET("/api/1.json")
	res := req.RouteTo(router)
	assert.HTTPStatus(200, res)

	_, obj := testEntity.Get().Key("1").MustOne(ts.GET("/").Request)
	assert.Nil(obj)
}
Example #16
0
func Test_EntityPut_NotFound(t *testing.T) {
	assert := gaetest.NewAssert(t)
	router := wcg.NewRouter()
	router.PUT("/api/:key.json",
		ParseForm(nil),
		EntityPut(testEntity.Put(), "key"),
	)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
	})

	req := ts.PUTForm("/api/2.json", url.Values{
		"value": []string{"1"},
	})
	res := req.RouteTo(router)
	assert.HTTPStatus(404, res)
}
Example #17
0
func Test_GetMulti_NonExistingKey(t *testing.T) {
	assert := gaetest.NewAssert(t)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "5", "ID": "5"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "6", "ID": "6"},
	})

	_, list, err := testEntity.GetMulti().Keys("1", "8").List(ts.GET("/").Request)
	got := list.([]*TEntity)
	assert.Nil(err)
	assert.EqInt(2, len(got))
	assert.EqStr("1", got[0].ID)
	assert.Nil(got[1])
}
Example #18
0
func Test_Query_Cursor(t *testing.T) {
	assert := gaetest.NewAssert(t)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "5", "ID": "5"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "6", "ID": "6"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "7", "ID": "7"},
	})
	iter := testEntity.Query().Order("ID").MustRun(ts.GET("/").Request)
	_, d := iter.MustNext()
	assert.EqStr("1", d.(*TEntity).ID)
	iter2 := testEntity.Query().Order("ID").StartCursor(iter.MustCursorString()).MustRun(ts.GET("/").Request)
	_, d = iter2.MustNext()
	assert.EqStr("2", d.(*TEntity).ID)
}
Example #19
0
func Test_Get(t *testing.T) {
	assert := gaetest.NewAssert(t)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "5", "ID": "5"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "6", "ID": "6"},
	})

	_, one, err := testEntity.Get().Key("1").One(ts.GET("/").Request)
	got := one.(*TEntity)
	assert.Nil(err)
	assert.EqStr("1", got.ID, "TEntity.ID")

	_, one, err = testEntity.Get().Key("non-exist").One(ts.GET("/").Request)
	assert.Nil(err)
	assert.Nil(one)
}
Example #20
0
func Test_EntityTail(t *testing.T) {
	assert := gaetest.NewAssert(t)
	router := wcg.NewRouter()
	router.GET("/api/",
		EntityTail(testEntity.Query()),
	)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"},
	})

	var got TEntity
	req := ts.GET("/api/")
	res := req.RouteTo(router)
	assert.HTTPStatus(200, res)
	assert.JSONResponse(&got, res)
	assert.EqStr("4", got.ID)
}
Example #21
0
func Test_PutMulti(t *testing.T) {
	assert := gaetest.NewAssert(t)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "5", "ID": "5"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "6", "ID": "6"},
	})

	var list1 = []TEntity{
		TEntity{ID: "1", Digit: 10},
		TEntity{ID: "2", Digit: 20},
	}
	var list2 = []*TEntity{
		&TEntity{ID: "3", Digit: 30},
		&TEntity{ID: "4", Digit: 40},
	}
	_, err := testEntity.PutMulti().Update(ts.PUTForm("/", nil).Request, list1)
	assert.Nil(err)

	_, list, _ := testEntity.GetMulti().Keys("1", "2").List(ts.GET("/").Request)
	got := list.([]*TEntity)
	assert.EqStr("1", got[0].ID, "TEntity[0].ID")
	assert.EqInt(10, got[0].Digit, "TEntity[0].Digit")
	assert.EqStr("2", got[1].ID, "TEntity[1].ID")
	assert.EqInt(20, got[1].Digit, "TEntity[1].Digit")

	_, err = testEntity.PutMulti().Update(ts.PUTForm("/", nil).Request, list2)
	assert.Nil(err)

	_, list, _ = testEntity.GetMulti().Keys("3", "4").List(ts.GET("/").Request)
	got = list.([]*TEntity)
	assert.EqStr("3", got[0].ID, "TEntity[2].ID")
	assert.EqInt(30, got[0].Digit, "TEntity[2].Digit")
	assert.EqStr("4", got[1].ID, "TEntity[3].ID")
	assert.EqInt(40, got[1].Digit, "TEntity[3].Digit")
}
Example #22
0
func Test_Put_CacheInvalidation(t *testing.T) {
	assert := gaetest.NewAssert(t)
	driver := memcache.NewDriver(ts.Context, wcg.NewLogger(nil))

	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
	})
	// create caches
	testEntity.Query().Cache("123").MustExecute(ts.GET("/").Request)
	testEntity.Get().Key("1").Cache(true).MustOne(ts.GET("/").Request)

	assert.OK(driver.Exists("ent.TestEntity.1"), "An entyty cache should exist")
	assert.OK(driver.Exists("query.TestEntity.123"), "A query cache should exist")

	item := &TEntity{ID: "1", Digit: 30}

	testEntity.Put().Cache("123").MustUpdate(ts.PUTForm("/", nil).Request, item)

	assert.Not(driver.Exists("ent.TestEntity.1"), "Entity cache should be invalidated")
	assert.Not(driver.Exists("query.TestEntity.123"), "Query cache should be invalidated")
}
Example #23
0
func Test_EntityGet(t *testing.T) {
	assert := gaetest.NewAssert(t)
	router := wcg.NewRouter()
	router.GET("/api/:key.json",
		EntityGet(testEntity.Get(), "key"),
	)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
	})

	var got TEntity
	req := ts.GET("/api/1.json")
	res := req.RouteTo(router)
	assert.HTTPStatus(200, res)
	assert.JSONResponse(&got, res)
	assert.EqStr("1", got.ID)

	req = ts.GET("/2.json")
	res = req.RouteTo(router)
	assert.HTTPStatus(404, res)
}
Example #24
0
func Test_EntityPut_BadRequest(t *testing.T) {
	assert := gaetest.NewAssert(t)
	router := wcg.NewRouter()
	router.PUT("/api/:key.json",
		ParseForm(nil),
		EntityPut(testEntity.Put(), "key"),
	)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
	})

	var got map[string]string
	req := ts.PUTForm("/api/1.json", url.Values{
		"value": []string{"a"},
	})
	res := req.RouteTo(router)
	assert.HTTPStatus(400, res)
	assert.JSONResponse(&got, res)
	assert.EqStr("bad_request", got["error"])
	assert.EqStr(ErrInvalidFormParameters.Error(), got["message"])
}
Example #25
0
func Test_Query_WithLimit(t *testing.T) {
	assert := gaetest.NewAssert(t)
	gaetest.CleanupDatastore(ts.Context)
	gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{
		map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "5", "ID": "5"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "6", "ID": "6"},
		map[string]interface{}{"_kind": testEntity.String(), "_key": "7", "ID": "7"},
	})

	// limitation
	page := testEntity.Query().Limit(4).MustExecute(ts.GET("/").Request)
	got := page.Data.([]TEntity)
	assert.EqInt(4, len(got), "len(Pagination.Data)")
	assert.EqStr("1", got[0].ID, "Pagination.Data[0].ID (Default max limitation)")
	assert.Nil(page.Previous, "Pagination.Previous")
	assert.NotNil(page.Next, "Pagination.Next")
	assert.EqInt(1, page.Next.P, "Pagination.Next.P")
	assert.EqInt(4, page.Next.N, "Pagination.Next.N")
}