0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

golang の datastore の unittest でハマった話

Last updated at Posted at 2020-08-02

概要

datastore.NewClient は実在の projectId を要求する

  • context は aetest.NewContext を使う
  • datastore client はテストでも使い方は同じ
  • projectId が非実在のものだとエラーになる
  • 実際の datastore には書き込まれていないのは確認済み

--> テストでも実在の ProjectId を指定する必要がある

少し調べた範囲ではこれに関する情報が見つけられなかったため、あくまで推論。情報があれば教えて下さい。

コード

func NewClient(ctx context.Context, projectId string) (*datastore.Client, error){
    return datastore.NewClient(ctx, ProjectId)
}
func TestDatastore(t *testing.T) {
  ctx, done, err := aetest.NewContext()
  if err != nil {
    t.Fatal(err)
  }
  defer done()
  
  // NG
  if c, err = NewClient(ctx, "dummy-project"); err != nil {
    t.Fatal(err)
  }

  // OK
  if c, err = NewClient(ctx, "real-project-12345"); err != nil {
    t.Fatal(err)
  }

  // ...
}

追記

luci/gae というのを使うと高速にできるようです

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?