在使用强一致性测试go-endpoints时,将endpoints.NewContext替换为aetest.NewContext。

huangapple go评论87阅读模式
英文:

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。

我无法将 otherCtx 传递给我的 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 上下文转换为 endpoints 上下文?

英文:

I have a search method:

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

as you can see it takes an endpoints.Context e.g:

ctx := endpoints.NewContext(req1)

however with aetest, i'm using different context:

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

Particularly this context has extra options for strong consistency - since i'm setting up data so I can test a read only api.

I can't pass the otherCxt through to my Search method because it's not an 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)
}

what's the recommend approach for testing go-endpoints with aetest? Is it possible to just transform the aetest context into the endpoints context?

答案1

得分: 2

这是一个示例代码片段,用于在Go语言中使用aetest和endpoints包进行搜索操作。它创建了一个实例(inst),构造了一个GET请求(r),并使用该请求创建了一个上下文(c)。然后,使用上下文(c)调用了名为Search的函数,其中的参数可以根据具体需求进行填充。

英文:

How about this:

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

sa.Search(c, ...)

答案2

得分: 0

根据Alex的说法,我有以下解决方案:

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", searchResults)
}

请注意,这是一个Go语言的测试函数,其中包含一些API调用和日志记录。

英文:

as per what alex said I have the following solution:

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)
}

huangapple
  • 本文由 发表于 2015年3月7日 11:58:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/28911212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定