测试

如果你想在单元测试中测试 ent.Client,你可以用已经生成好的 enttest 包来创建客户端和自动执行表结构迁移:

  1. package main
  2. import (
  3. "testing"
  4. "<project>/ent/enttest"
  5. _ "github.com/mattn/go-sqlite3"
  6. )
  7. func TestXXX(t *testing.T) {
  8. client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
  9. defer client.Close()
  10. // ...
  11. }

如果需要传递选项参数给 Open,请使用 enttest.Option

  1. func TestXXX(t *testing.T) {
  2. opts := []enttest.Option{
  3. enttest.WithOptions(ent.Log(t.Log)),
  4. enttest.WithMigrateOptions(migrate.WithGlobalUniqueID(true)),
  5. }
  6. client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1", opts...)
  7. defer client.Close()
  8. // ...
  9. }