-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: resetting and seeding database for individual tests
- Loading branch information
1 parent
2dbac0c
commit 73ffc6a
Showing
4 changed files
with
158 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,45 @@ | ||
const util = require("node:util"); | ||
const cp = require("node:child_process"); | ||
const { defineConfig } = require("cypress"); | ||
const { Pool } = require("pg"); | ||
|
||
const exec = util.promisify(cp.exec); | ||
const { env } = process; | ||
const pool = new Pool({ | ||
connectionString: `postgres://${env.DB_USER}:${env.DB_PASSWORD}@${env.DB_HOST}:${env.DB_PORT}/${env.DB_NAME}`, | ||
}); | ||
|
||
async function resetDatabase() { | ||
const client = await pool.connect(); | ||
try { | ||
const res = await client.query( | ||
"SELECT tablename FROM pg_tables WHERE schemaname = 'public';", | ||
); | ||
for (const row of res.rows) { | ||
await client.query(`TRUNCATE TABLE ${row.tablename} RESTART IDENTITY CASCADE;`); | ||
} | ||
} finally { | ||
client.release(); | ||
} | ||
return null; | ||
} | ||
|
||
async function seedDatabase() { | ||
await exec("cd .. && go run cmd/seed/main.go"); | ||
return null; | ||
} | ||
|
||
module.exports = defineConfig({ | ||
e2e: { | ||
defaultCommandTimeout: 15000, | ||
requestTimeout: 20000, | ||
responseTimeout: 20000, | ||
pageLoadTimeout: 60000, | ||
setupNodeEvents(on, config) { | ||
// implement node event listeners here | ||
}, | ||
}, | ||
e2e: { | ||
defaultCommandTimeout: 15000, | ||
requestTimeout: 20000, | ||
responseTimeout: 20000, | ||
pageLoadTimeout: 60000, | ||
setupNodeEvents(on, config) { | ||
on("task", { | ||
resetDatabase, | ||
seedDatabase, | ||
}); | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"dependencies": { | ||
"cypress": "^13.15.0" | ||
"cypress": "^13.15.0", | ||
"pg": "^8.13.3" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.