NewTestAppWithConfig creates and initializes a test application instance from the provided config. If config.DataDir is not set it fallbacks to the default internal test data directory. config.DataDir is cloned for each new test application instance. It is the caller's responsibility to call app.
(config core.BaseAppConfig)
| 92 | // |
| 93 | // It is the caller's responsibility to call app.Cleanup() when the app is no longer needed. |
| 94 | func NewTestAppWithConfig(config core.BaseAppConfig) (*TestApp, error) { |
| 95 | if config.DataDir == "" { |
| 96 | // fallback to the default test data directory |
| 97 | _, currentFile, _, _ := runtime.Caller(0) |
| 98 | config.DataDir = filepath.Join(path.Dir(currentFile), "data") |
| 99 | } |
| 100 | |
| 101 | tempDir, err := TempDirClone(config.DataDir) |
| 102 | if err != nil { |
| 103 | return nil, err |
| 104 | } |
| 105 | |
| 106 | // replace with the clone |
| 107 | config.DataDir = tempDir |
| 108 | |
| 109 | app := core.NewBaseApp(config) |
| 110 | |
| 111 | // load data dir and db connections |
| 112 | if err := app.Bootstrap(); err != nil { |
| 113 | return nil, err |
| 114 | } |
| 115 | |
| 116 | // ensure that the Dao and DB configurations are properly loaded |
| 117 | if _, err := app.DB().NewQuery("Select 1").Execute(); err != nil { |
| 118 | return nil, err |
| 119 | } |
| 120 | if _, err := app.AuxDB().NewQuery("Select 1").Execute(); err != nil { |
| 121 | return nil, err |
| 122 | } |
| 123 | |
| 124 | // apply any missing migrations |
| 125 | if err := app.RunAllMigrations(); err != nil { |
| 126 | return nil, err |
| 127 | } |
| 128 | |
| 129 | // force disable request logs because the logs db call execute in a separate |
| 130 | // go routine and it is possible to panic due to earlier api test completion. |
| 131 | app.Settings().Logs.MaxDays = 0 |
| 132 | |
| 133 | t := &TestApp{ |
| 134 | BaseApp: app, |
| 135 | EventCalls: make(map[string]int), |
| 136 | TestMailer: &TestMailer{}, |
| 137 | } |
| 138 | |
| 139 | t.OnBootstrap().Bind(&hook.Handler[*core.BootstrapEvent]{ |
| 140 | Func: func(e *core.BootstrapEvent) error { |
| 141 | t.registerEventCall("OnBootstrap") |
| 142 | return e.Next() |
| 143 | }, |
| 144 | Priority: -99999, |
| 145 | }) |
| 146 | |
| 147 | t.OnServe().Bind(&hook.Handler[*core.ServeEvent]{ |
| 148 | Func: func(e *core.ServeEvent) error { |
| 149 | t.registerEventCall("OnServe") |
| 150 | e.InstallerFunc = nil // https://github.com/pocketbase/pocketbase/discussions/7202 |
| 151 | return e.Next() |
no test coverage detected
searching dependent graphs…