Testing

If you’re using ent.Client in your unit-tests, you can use the generated enttest package for creating a client and auto-running the schema migration as follows:

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

In order to pass functional options to Open, use 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. }