Skip to content

Commit 78315cb

Browse files
committed
Fix docs
1 parent 43cd15f commit 78315cb

File tree

3 files changed

+39
-58
lines changed

3 files changed

+39
-58
lines changed

README.md

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -148,40 +148,56 @@ This works across the entire test suite.
148148

149149
Note that if unique parameters are passed to the `beforeTemplateIsBaked` (`null` in the above example), separate databases will still be created.
150150

151-
### "Nested" `beforeTemplateIsBaked` calls
151+
### Manual template creation
152152

153-
In some cases, if you do extensive setup in your `beforeTemplateIsBaked` hook, you might want to obtain a separate, additional database within it if your application uses several databases for different purposes. This is possible by using the passed `beforeTemplateIsBaked` to your hook callback:
153+
In some cases, if you do extensive setup in your `beforeTemplateIsBaked` hook, you might want to obtain a separate, additional database within it if your application uses several databases for different purposes. This is possible by using the `manuallyBuildAdditionalTemplate()` function passed to your hook callback:
154154

155155
```ts
156-
type DatabaseParams = {
157-
type: "foo" | "bar"
158-
}
156+
import test from "ava"
159157

160-
const getTestServer = getTestPostgresDatabaseFactory<DatabaseParams>({
158+
const getTestDatabase = getTestPostgresDatabaseFactory<DatabaseParams>({
161159
beforeTemplateIsBaked: async ({
162160
params,
163161
connection: { pool },
164-
beforeTemplateIsBaked,
162+
manuallyBuildAdditionalTemplate,
165163
}) => {
166-
if (params.type === "foo") {
167-
await pool.query(`CREATE TABLE "foo" ("id" SERIAL PRIMARY KEY)`)
168-
// Important: return early to avoid infinite loop
169-
return
170-
}
171-
172164
await pool.query(`CREATE TABLE "bar" ("id" SERIAL PRIMARY KEY)`)
173-
// This created database will be torn down at the end of the top-level `beforeTemplateIsBaked` call
174-
const fooDatabase = await beforeTemplateIsBaked({
175-
params: { type: "foo" },
176-
})
177165

178-
// This works now
179-
await fooDatabase.pool.query(`INSERT INTO "foo" DEFAULT VALUES`)
166+
const fooTemplateBuilder = await manuallyBuildAdditionalTemplate()
167+
await fooTemplateBuilder.connection.pool.query(
168+
`CREATE TABLE "foo" ("id" SERIAL PRIMARY KEY)`
169+
)
170+
const { templateName: fooTemplateName } = await fooTemplateBuilder.finish()
171+
172+
return { fooTemplateName }
180173
},
181174
})
175+
176+
test("foo", async (t) => {
177+
const barDatabase = await getTestDatabase({ type: "bar" })
178+
179+
// the "bar" database has the "bar" table...
180+
await t.notThrowsAsync(async () => {
181+
await barDatabase.pool.query(`SELECT * FROM "bar"`)
182+
})
183+
184+
// ...but not the "foo" table...
185+
await t.throwsAsync(async () => {
186+
await barDatabase.pool.query(`SELECT * FROM "foo"`)
187+
})
188+
189+
// ...and we can obtain a separate database with the "foo" table
190+
const fooDatabase = await getTestDatabase.fromTemplate(
191+
t,
192+
barDatabase.beforeTemplateIsBakedResult.fooTemplateName
193+
)
194+
await t.notThrowsAsync(async () => {
195+
await fooDatabase.pool.query(`SELECT * FROM "foo"`)
196+
})
197+
})
182198
```
183199

184-
Be very careful when using this to avoid infinite loops.
200+
Although it's not shown in the above example, because of `ava-postgres`'s automatic de-duping by parameter combinations, any returned template name is "linked" to the parameters passed to the `getTestDatabase()` function.
185201

186202
### Bind mounts & `exec`ing in the container
187203

src/tests/cleanup/does-database-exist.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/tests/hooks.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ test("beforeTemplateIsBaked (result isn't serializable)", async (t) => {
147147
)
148148
})
149149

150-
test("beforeTemplateIsBaked, get nested database", async (t) => {
150+
test("beforeTemplateIsBaked with manual template build", async (t) => {
151151
const getTestDatabase = getTestPostgresDatabaseFactory({
152152
postgresVersion: process.env.POSTGRES_VERSION,
153-
workerDedupeKey: "beforeTemplateIsBakedHookNestedDatabase",
153+
workerDedupeKey: "beforeTemplateIsBakedHookManualTemplateBuild",
154154
beforeTemplateIsBaked: async ({
155155
connection: { pool },
156156
manuallyBuildAdditionalTemplate,
@@ -178,7 +178,7 @@ test("beforeTemplateIsBaked, get nested database", async (t) => {
178178

179179
await t.notThrowsAsync(async () => {
180180
await fooDatabase.pool.query('SELECT * FROM "foo"')
181-
})
181+
}, "foo table should exist on database manually created from template")
182182

183183
await t.throwsAsync(async () => {
184184
await fooDatabase.pool.query('SELECT * FROM "bar"')

0 commit comments

Comments
 (0)