Skip to content

Commit de1e6ba

Browse files
committed
Fix docs
1 parent 43cd15f commit de1e6ba

File tree

4 files changed

+75
-79
lines changed

4 files changed

+75
-79
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/public-types.ts

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,37 +59,52 @@ export interface GetTestPostgresDatabaseFactoryOptions<
5959
params: Params
6060
containerExec: (command: string[]) => Promise<ExecResult>
6161
/**
62+
* 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.
6263
*
63-
* 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.
64-
* Be very careful when using this to avoid infinite loops.
6564
* @example
6665
* ```ts
67-
* type DatabaseParams = {
68-
* type: "foo" | "bar"
69-
* }
70-
71-
* const getTestServer = getTestPostgresDatabaseFactory<DatabaseParams>({
66+
* import test from "ava"
67+
*
68+
* const getTestDatabase = getTestPostgresDatabaseFactory<DatabaseParams>({
7269
* beforeTemplateIsBaked: async ({
7370
* params,
7471
* connection: { pool },
75-
* beforeTemplateIsBaked,
72+
* manuallyBuildAdditionalTemplate,
7673
* }) => {
77-
* if (params.type === "foo") {
78-
* await pool.query(`CREATE TABLE "foo" ("id" SERIAL PRIMARY KEY)`)
79-
* // Important: return early to avoid infinite loop
80-
* return
81-
* }
82-
8374
* await pool.query(`CREATE TABLE "bar" ("id" SERIAL PRIMARY KEY)`)
84-
* // This created database will be torn down at the end of the top-level `beforeTemplateIsBaked` call
85-
* const fooDatabase = await beforeTemplateIsBaked({
86-
* params: { type: "foo" },
87-
* })
88-
89-
* // This works now
90-
* await fooDatabase.pool.query(`INSERT INTO "foo" DEFAULT VALUES`)
75+
*
76+
* const fooTemplateBuilder = await manuallyBuildAdditionalTemplate()
77+
* await fooTemplateBuilder.connection.pool.query(
78+
* `CREATE TABLE "foo" ("id" SERIAL PRIMARY KEY)`
79+
* )
80+
* const { templateName: fooTemplateName } = await fooTemplateBuilder.finish()
81+
*
82+
* return { fooTemplateName }
9183
* },
9284
* })
85+
*
86+
* test("foo", async (t) => {
87+
* const barDatabase = await getTestDatabase({ type: "bar" })
88+
*
89+
* // the "bar" database has the "bar" table...
90+
* await t.notThrowsAsync(async () => {
91+
* await barDatabase.pool.query(`SELECT * FROM "bar"`)
92+
* })
93+
*
94+
* // ...but not the "foo" table...
95+
* await t.throwsAsync(async () => {
96+
* await barDatabase.pool.query(`SELECT * FROM "foo"`)
97+
* })
98+
*
99+
* // ...and we can obtain a separate database with the "foo" table
100+
* const fooDatabase = await getTestDatabase.fromTemplate(
101+
* t,
102+
* barDatabase.beforeTemplateIsBakedResult.fooTemplateName
103+
* )
104+
* await t.notThrowsAsync(async () => {
105+
* await fooDatabase.pool.query(`SELECT * FROM "foo"`)
106+
* })
107+
* })
93108
* ```
94109
*/
95110
manuallyBuildAdditionalTemplate: () => Promise<{

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)