Skip to content

Commit

Permalink
Move db setup code to plugins file (#40098)
Browse files Browse the repository at this point in the history
* Move db setup code to plugins file

* require
  • Loading branch information
laoneo authored Mar 13, 2023
1 parent 90435d6 commit 52715d6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 47 deletions.
49 changes: 2 additions & 47 deletions cypress.config.dist.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { defineConfig } = require('cypress');
const mysql = require('mysql');
const postgres = require('postgres');
const setupPlugins =require('./tests/cypress/plugins/index');

module.exports = defineConfig({
fixturesFolder: 'tests/cypress/fixtures',
Expand All @@ -10,51 +9,7 @@ module.exports = defineConfig({
viewportWidth: 1200,
e2e: {
setupNodeEvents(on, config) {
function queryTestDB(query, config) {
if (config.env.db_type === 'pgsql' || config.env.db_type === 'PostgreSQL (PDO)') {
const connection = postgres({
host: config.env.db_host,
database: config.env.db_name,
username: config.env.db_user,
password: config.env.db_password,
idle_timeout: 5,
max_lifetime: 60
});

// Postgres delivers the data direct as result of the insert query
if (query.indexOf('INSERT') === 0) {
query += ' returning *'
}

// Postgres needs double quotes
query = query.replaceAll('\`', '"');

return connection.unsafe(query).then((result) => {
if (query.indexOf('INSERT') !== 0 || result.length === 0) {
return result;
}

// Normalize the object
return {insertId: result[0].id};
});
}

return new Promise((resolve, reject) => {
const connection = mysql.createConnection({
host: config.env.db_host,
user: config.env.db_user,
password: config.env.db_password,
database: config.env.db_name
});
connection.connect();

connection.query(query, (error, results) => !error || !error.errno ? resolve(results) : reject(error));
});
}

on('task', {
queryDB: (query) => queryTestDB(query.replace('#__', config.env.db_prefix), config)
});
setupPlugins(on, config);
},
baseUrl: 'http://localhost/',
specPattern: [
Expand Down
Empty file removed tests/cypress/plugins/.gitkeep
Empty file.
52 changes: 52 additions & 0 deletions tests/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const mysql = require('mysql');
const postgres = require('postgres');

function queryTestDB(query, config) {
if (config.env.db_type === 'pgsql' || config.env.db_type === 'PostgreSQL (PDO)') {
const connection = postgres({
host: config.env.db_host,
database: config.env.db_name,
username: config.env.db_user,
password: config.env.db_password,
idle_timeout: 5,
max_lifetime: 60
});

// Postgres delivers the data direct as result of the insert query
if (query.indexOf('INSERT') === 0) {
query += ' returning *'
}

// Postgres needs double quotes
query = query.replaceAll('\`', '"');

return connection.unsafe(query).then((result) => {
if (query.indexOf('INSERT') !== 0 || result.length === 0) {
return result;
}

// Normalize the object
return { insertId: result[0].id };
});
}

return new Promise((resolve, reject) => {
const connection = mysql.createConnection({
host: config.env.db_host,
user: config.env.db_user,
password: config.env.db_password,
database: config.env.db_name
});
connection.connect();

connection.query(query, (error, results) => !error || !error.errno ? resolve(results) : reject(error));
});
};

function setupPlugins(on, config) {
on('task', {
queryDB: (query) => queryTestDB(query.replace('#__', config.env.db_prefix), config)
});
}

module.exports = setupPlugins;

0 comments on commit 52715d6

Please sign in to comment.