chore: parallelize integration tests and use template dbs (#907)
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
const path = require("path")
|
||||
const { spawn } = require("child_process")
|
||||
|
||||
const { setPort } = require("./use-api")
|
||||
|
||||
module.exports = ({ cwd, verbose }) => {
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
const path = require("path")
|
||||
require("dotenv").config({ path: path.join(__dirname, "../.env") })
|
||||
|
||||
const { dropDatabase, createDatabase } = require("pg-god")
|
||||
const { dropDatabase } = require("pg-god")
|
||||
const { createConnection } = require("typeorm")
|
||||
const dbFactory = require("./use-template-db")
|
||||
|
||||
const workerId = parseInt(process.env.JEST_WORKER_ID || "1")
|
||||
const DB_USERNAME = process.env.DB_USERNAME || "postgres"
|
||||
const DB_PASSWORD = process.env.DB_PASSWORD || ""
|
||||
const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@localhost/medusa-integration`
|
||||
const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@localhost/medusa-integration-${workerId}`
|
||||
|
||||
const pgGodCredentials = {
|
||||
user: DB_USERNAME,
|
||||
@@ -61,7 +63,7 @@ const DbTestUtil = {
|
||||
|
||||
shutdown: async function () {
|
||||
await this.db_.close()
|
||||
const databaseName = "medusa-integration"
|
||||
const databaseName = `medusa-integration-${workerId}`
|
||||
return await dropDatabase({ databaseName }, pgGodCredentials)
|
||||
},
|
||||
}
|
||||
@@ -96,28 +98,9 @@ module.exports = {
|
||||
instance.setDb(dbConnection)
|
||||
return dbConnection
|
||||
} else {
|
||||
const migrationDir = path.resolve(
|
||||
path.join(
|
||||
cwd,
|
||||
`node_modules`,
|
||||
`@medusajs`,
|
||||
`medusa`,
|
||||
`dist`,
|
||||
`migrations`
|
||||
)
|
||||
)
|
||||
const databaseName = `medusa-integration-${workerId}`
|
||||
|
||||
const databaseName = "medusa-integration"
|
||||
await createDatabase({ databaseName }, pgGodCredentials)
|
||||
|
||||
const connection = await createConnection({
|
||||
type: "postgres",
|
||||
url: DB_URL,
|
||||
migrations: [`${migrationDir}/*.js`],
|
||||
})
|
||||
|
||||
await connection.runMigrations()
|
||||
await connection.close()
|
||||
await dbFactory.createFromTemplate(databaseName)
|
||||
|
||||
const dbConnection = await createConnection({
|
||||
type: "postgres",
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
const path = require("path")
|
||||
require("dotenv").config({ path: path.join(__dirname, "../.env") })
|
||||
|
||||
const { createDatabase, dropDatabase } = require("pg-god")
|
||||
const { createConnection, getConnection } = require("typeorm")
|
||||
|
||||
const DB_USERNAME = process.env.DB_USERNAME || "postgres"
|
||||
const DB_PASSWORD = process.env.DB_PASSWORD || ""
|
||||
const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@localhost`
|
||||
|
||||
const pgGodCredentials = {
|
||||
user: DB_USERNAME,
|
||||
password: DB_PASSWORD,
|
||||
}
|
||||
|
||||
class DatabaseFactory {
|
||||
constructor() {
|
||||
this.connection_ = null
|
||||
this.masterConnectionName = "name"
|
||||
this.templateDbName = "medusa-integration-template"
|
||||
}
|
||||
|
||||
async createTemplateDb_() {
|
||||
try {
|
||||
const connection = await this.getMasterConnection()
|
||||
|
||||
const migrationDir = path.resolve(
|
||||
path.join(
|
||||
process.cwd(),
|
||||
"integration-tests",
|
||||
"api",
|
||||
`node_modules`,
|
||||
`@medusajs`,
|
||||
`medusa`,
|
||||
`dist`,
|
||||
`migrations`
|
||||
)
|
||||
)
|
||||
|
||||
await dropDatabase(
|
||||
{
|
||||
databaseName: this.templateDbName,
|
||||
errorIfNonExist: false,
|
||||
},
|
||||
pgGodCredentials
|
||||
)
|
||||
await createDatabase(
|
||||
{ databaseName: this.templateDbName },
|
||||
pgGodCredentials
|
||||
)
|
||||
|
||||
const templateDbConnection = await createConnection({
|
||||
type: "postgres",
|
||||
name: "templateConnection",
|
||||
url: `${DB_URL}/${this.templateDbName}`,
|
||||
migrations: [`${migrationDir}/*.js`],
|
||||
})
|
||||
|
||||
await templateDbConnection.runMigrations()
|
||||
await templateDbConnection.close()
|
||||
|
||||
return connection
|
||||
} catch (err) {
|
||||
console.log("error in createTemplateDb_")
|
||||
console.log(err)
|
||||
}
|
||||
}
|
||||
|
||||
async getMasterConnection() {
|
||||
try {
|
||||
return await getConnection(this.masterConnectionName)
|
||||
} catch (err) {
|
||||
return await this.createMasterConnection()
|
||||
}
|
||||
}
|
||||
|
||||
async createMasterConnection() {
|
||||
const connection = await createConnection({
|
||||
type: "postgres",
|
||||
name: this.masterConnectionName,
|
||||
url: `${DB_URL}`,
|
||||
})
|
||||
|
||||
return connection
|
||||
}
|
||||
|
||||
async createFromTemplate(dbName) {
|
||||
const connection = await this.getMasterConnection()
|
||||
|
||||
await connection.query(`DROP DATABASE IF EXISTS "${dbName}";`)
|
||||
await connection.query(
|
||||
`CREATE DATABASE "${dbName}" TEMPLATE "${this.templateDbName}";`
|
||||
)
|
||||
}
|
||||
|
||||
async destroy() {
|
||||
let connection = await this.getMasterConnection()
|
||||
|
||||
await connection.query(`DROP DATABASE IF EXISTS "${this.templateDbName}";`)
|
||||
await connection.close()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new DatabaseFactory()
|
||||
Reference in New Issue
Block a user