使用 aetest.NewContext 代替 endpoints.NewContext 来测试具有强一致性的 go-endpoints

use aetest.NewContext in place of endpoints.NewContext for testing go-endpoints with strong consistency

我有一个搜索方法:

func (sa *SearchApi) Search(c endpoints.Context, r *SearchQuery) (*SearchResults, error) { .. }

如您所见,它需要一个 endpoints.Context 例如:

ctx := endpoints.NewContext(req1)

但是对于 aetest,我使用的是不同的上下文:

otherCtx, err := aetest.NewContext(&aetest.Options{"", true})

特别是这个上下文有额外的强一致性选项 - 因为我正在设置数据所以我可以测试只读 api。

我无法将 otherCxt 传递给我的 Search 方法,因为它不是 endpoints.Context

otherCtx:

type Context interface {
    appengine.Context

    // Login causes the context to act as the given user.
    Login(*user.User)
    // Logout causes the context to act as a logged-out user.
    Logout()
    // Close kills the child api_server.py process,
    // releasing its resources.
    io.Closer
}

endpoints.Context:

type Context interface {
    appengine.Context

    // HTTPRequest returns the request associated with this context.
    HTTPRequest() *http.Request

    // Namespace returns a replacement context that operates within the given namespace.
    Namespace(name string) (Context, error)

    // CurrentOAuthClientID returns a clientID associated with the scope.
    CurrentOAuthClientID(scope string) (string, error)

    // CurrentOAuthUser returns a user of this request for the given scope.
    // It caches OAuth info at the first call for future invocations.
    //
    // Returns an error if data for this scope is not available.
    CurrentOAuthUser(scope string) (*user.User, error)
}

使用 aetest 测试 go-endpoints 的推荐方法是什么?是否可以将 aetest 上下文转换为端点上下文?

这个怎么样:

inst := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true})
r, _ := inst.NewRequest("GET", "/", nil)
c := endpoints.NewContext(r)

sa.Search(c, ...)

根据亚历克斯所说,我有以下解决方案:

func TestApi(t *testing.T) {
    inst, _ := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true})
    defer inst.Close()
    r, _ := inst.NewRequest("GET", "/", nil)
    c := endpoints.NewContext(r)
    makeCourses(c)
    api := SearchApi{}
    searchQuery := &SearchQuery{"my-search-term", nil}
    searchResults, _ := api.Search(c, searchQuery)
    log.Println("got results %v", searchResults)
}