dongyong8098 2015-11-25 23:37
浏览 36
已采纳

go / appengine:更快的数据存储区测试

Recently I've been playing around with golang and google's app engine.

I've been experiencing really slow unit test executions when implementing repositories.

The thing I do is to call ctx,_ := aetest.NewContext(nil) in each test in order to obtain a clean database. This starts a new server in each test and thus leads in slow testing.

Lately I've been trying to work this around by starting it in TestMain.

var ctx aetest.Context

func TestMain(m *testing.M) {
    ctx,_ = aetest.NewContext(nil)
    code := m.Run()
    ctx.Close()
    os.Exit(code)
}

func TestMyRepository(t *testing.T){
    cleanDatastore()
    repo := &MyRepository{ctx}
    repo.DoSomething()
}

In the function cleanDatastore I've been executing a bash script that basically runs a SQLite command to clean the local database saved in /tmp.

#!/usr/bin/env bash

PATH=$1
cd $PATH

echo "Cleaning datastore..."

/usr/bin/sqlite3 datastore "delete from \"dev~testapp!!EntitiesByProperty\";"
/usr/bin/sqlite3 datastore "delete from \"dev~testapp!!Entities\";"

echo "Datastore is clean."

Is what I am trying to do making any sense or is there a simpler way to achieve better testing time.

  • 写回答

1条回答 默认 最新

  • doulianxing4015 2015-11-27 18:37
    关注

    A kindless, keys-only query (getting the keys of all entities, regardless of kind) should help:

       q := datastore.NewQuery("").KeysOnly()
    

    and then, you loop over all such keys and delete each of them, e.g:

       for t := q.Run(ctx); ; {
            key, err := t.Next(nil)
            if err == datastore.Done {
                break
            }
            if err != nil {
                serveError(c, w, err)
                return
            }
            datastore.Delete(ctx, key)
        }
    

    It may be slightly faster to get the keys into an array and use DeleteMulti, but, on a local datastore, I suspect that would not make a measurable difference.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题