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) }
func Test_Query_NoData_WithCache(t *testing.T) { assert := gaetest.NewAssert(t) gaetest.CleanupDatastore(ts.Context) page := testEntity.Query().Cache("nodata").MustExecute(ts.GET("/").Request) gaetest.CleanupDatastore(ts.Context) page = testEntity.Query().Cache("nodata").MustExecute(ts.GET("/").Request) got := page.Data.([]TEntity) assert.EqInt(0, len(got)) }
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") }
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") }
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) }
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]) }
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") }
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 }
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, ) }
func Test_GetWith_Default(t *testing.T) { assert := gaetest.NewAssert(t) gaetest.CleanupDatastore(ts.Context) _, one, err := testEntity.Get().Key("1").UseDefaultIfNil(true).One(ts.GET("/").Request) got := one.(*TEntity) assert.Nil(err) assert.EqStr("1", got.ID, "TEntity.ID") assert.EqStr("This is defualt value", got.Desc, "TEntity.Desc") }
func Test_API_Task_Sync(t *testing.T) { assert := gaetest.NewAssert(t) gaetest.CleanupDatastore(ts.Context, "hplink") httptest.StartMockServer(func(mock *httptest.MockServer) { mock.Routes().GET("/", middleware.ServeFile("./fixtures/mocks/helloproject.html")) mock.Routes().GET("/c-ute/", middleware.ServeFile("./fixtures/mocks/c-ute/index.html")) mock.Routes().GET("/c-ute/*", middleware.StaticFile("", "fixtures/mocks/c-ute/")) crawler.MockExternalResource(map[string]string{ "http://www.helloproject.com/artist/": fmt.Sprintf("%s", mock.BaseURL()), "http://www.helloproject.com/artist/c-ute/": fmt.Sprintf("%sc-ute/", mock.BaseURL()), "http://www.helloproject.com/artist/c-ute/profile/maimi_yajima/": fmt.Sprintf("%sc-ute/maimi_yajima.html", mock.BaseURL()), "http://www.helloproject.com/artist/c-ute/profile/saki_nakajima/": fmt.Sprintf("%sc-ute/saki_nkajima.html", mock.BaseURL()), "http://www.helloproject.com/artist/c-ute/profile/airi_suzuki/": fmt.Sprintf("%sc-ute/airi_suzuki.html", mock.BaseURL()), "http://www.helloproject.com/artist/c-ute/profile/chisato_okai/": fmt.Sprintf("%sc-ute/chisato_okai.html", mock.BaseURL()), "http://www.helloproject.com/artist/c-ute/profile/mai_hagiwara/": fmt.Sprintf("%sc-ute/mai_hagiwara.html", mock.BaseURL()), }, func() { runner := testhelper.NewAsyncTaskTestRunner(t, instance.Routes(), ts) runner.OnMonitor(func(testreq *httptest.TestRequest, f func()) { testhelper.TemporaryAllow(testreq, "hplink.admin", f) }) runner.OnTrigger(func(testreq *httptest.TestRequest, f func()) { testhelper.TemporaryAllow(testreq, "hplink.admin", f) }) runner.Run("/api/hplink/tasks/profile/", url.Values{}) p := entities.Artist.Query().Order("Index").MustExecute(ts.GET("/").Request) artistList := p.Data.([]hplink.Artist) assert.EqInt(1, len(artistList)) assert.EqInt(0, artistList[0].Index) assert.EqStr("c-ute", artistList[0].Key) assert.EqStr("℃-ute", artistList[0].Name) p = entities.Member.Query().Order("Index").Filter("ArtistKey=", "c-ute").MustExecute(ts.GET("/").Request) memberList := p.Data.([]hplink.Member) assert.EqInt(5, len(memberList)) assert.EqInt(0, memberList[0].Index) assert.EqStr("c-ute", memberList[0].ArtistKey) assert.EqStr("c-ute.maimi_yajima", memberList[0].Key) assert.EqStr("矢島舞美", memberList[0].Name) entities.FillMemberPublicProfiles(ts.GET("/").Request, memberList) assert.NotNil(memberList[0].PublicProfile) assert.EqStr("c-ute.maimi_yajima", memberList[0].PublicProfile.Key) assert.EqStr("埼玉県", memberList[0].PublicProfile.Hometown) _, settings := entities.CrawlerSettings.Get().Key("http://ameblo.jp/c-ute-official").MustOne(ts.GET("/").Request) assert.NotNil(settings) assert.EqStr("c-ute", settings.(*hplink.CrawlerSettings).ArtistKey) assert.EqInt(int(hplink.CrawlerSettingsTypeAmeblo), int(settings.(*hplink.CrawlerSettings).Type)) }) }) }
func Test_EntityGet_NotFound(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.GET("/api/:key.json", EntityGet(testEntity.Get(), "key"), ) gaetest.CleanupDatastore(ts.Context) req := ts.GET("/1.json") res := req.RouteTo(router) assert.HTTPStatus(404, res) }
func Test_EntityAll_Empty(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.GET("/api/", EntityAll(testEntity.Query()), ) gaetest.CleanupDatastore(ts.Context) req := ts.GET("/api/") res := req.RouteTo(router) assert.HTTPStatus(200, res) assert.HTTPBodyString("[]", res) }
func Test_EntityQuery_Empty(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.GET("/api/", EntityQuery(testEntity.Query().Limit(2)), ) gaetest.CleanupDatastore(ts.Context) var got entities.Pagination req := ts.GET("/api/") res := req.RouteTo(router) assert.HTTPStatus(200, res) assert.JSONResponse(&got, res) assert.EqInt(0, got.Length()) }
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()) }
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") }
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()) }
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)") }
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) }
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) }
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) }
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) }
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]) }
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) }
func Test_EntityPost_BadRequest(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.POST("/api/", ParseForm(nil), EntityPost(testEntity.Put()), ) gaetest.CleanupDatastore(ts.Context) var err map[string]string req := ts.POSTForm("/api/", url.Values{ "value": []string{"a"}, }) res := req.RouteTo(router) assert.HTTPStatus(400, res) assert.JSONResponse(&err, res) assert.EqStr("bad_request", err["error"]) assert.EqStr(ErrInvalidFormParameters.Error(), err["message"]) }
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) }
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) }
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") }
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"]) }
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) }