cypress: Unable to connect multiple DB

Current behavior:

I have tests to execute SQL queries to two DB under same server Select * from customers - execute in DB Customers Select * from roles - execute in DB identity.

I have used Cypresses-Sql-Server which is given here https://www.npmjs.com/package/cypress-sql-server

created two DB configuration, but it always ended with executing query in database identity. Fails all the test cases which are in database Customers.

Is there a way to connect and work with Multiple DB.

Test code to reproduce

plugins\index.js

    database = require('./database');
    databaseIdentity = require('./identityDatabase')
const sqlServer = require('cypress-sql-server');
const sqlIdentityServer = require('cypress-sql-server')

module.exports = (on, config) => {
var dbconfig = database(on, config);
    sqlTask = sqlServer.loadDBPlugin(dbconfig);
    on('task', sqlTask);
  

var dbconfig1 = databaseIdentity(on, config);
    tasks = sqlServer.loadDBPlugin(dbconfig1);
    on('task', tasks);
 
   
};

support - index.js:

import sqlServer from 'cypress-sql-server';
Cypress.Commands.add(sqlServer, sqlServer.loadDBCommands())

Plugins\database.js

var dbconfig = {
            server: "ddddddddddd",
            port:yourport,
            userName: "dfdsfdsf",
            password: "xxxxxxxxxx",
            options: {
                database: Customers,
                encrypt: true,
                rowCollectionOnRequestCompletion : true
            }
       };
       return dbconfig;

Plugins\identityDatabase.js (deleted original values)

var dbconfig = {
            server: "dddddddddd",
            port: yourPort,
            userName: "dddddddddddd",
            password: "xxxxxxxxx",
            options: {
                database: Orders,
                encrypt: true,
                rowCollectionOnRequestCompletion : true
            }
       };
       return dbconfig;

TestCase:
cy.task('sql.server:execute':'select * from Customers').then(output=>
{

})

Failing with error Customers not found.

Versions

Cypress - 4.1.0

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (1 by maintainers)

Most upvoted comments

@djpasch Thanks you so much!!

I had the same problem like @isabellemr had with index.js. But the problem was what I used the last version of Tedius with the last version not work but if you use the same version 8.3.0 for tedious and 2.5.1 for coffeescript this works. Then before what you write “npm install” you should write this:

package.json:

“dependencies”: { “coffeescript”: “^2.5.1”, “tedious”: “^8.3.0” },

And later when you write “npm install”. This will make it install with the same version that @djpasch proposes.

The result in my case was here! This works perfectly well!!!

image

Finally. If you use this comand “npm install tedious” don’t works because for a weird reason with the last version don’t work for me.

This was my result with the last version. This not work 😦

image

Hope this is some help to everyone who is frustrated with this.

Regards!

I have a solution for you. cypress-sql-server is using Tedious. So you could use Tedious directly. The following code is based on cypress-sql-server

Install Tedious

package.json:

  "dependencies": {
    "coffeescript": "^2.5.1",
    "tedious": "^8.3.0"
  },

plugins/index.js

module.exports = (on, config) => {

    on('task', {
      sqlServerDB1: sql => {
        return execSQL(sql, config.env.db1)
      },
    }),

    on('task', {
      sqlServerDB2: sql => {
        return execSQL(sql, config.env.db2)
      },
    }),
  
  return config
}

const tedious = require('tedious')
function execSQL(sql, config) {
  const connection = new tedious.Connection(config);
  return new Promise((res, rej) => {
    connection.on('connect', err => {
      if (err) {
        rej(err);
      }

      const request = new tedious.Request(sql, function (err, rowCount, rows) {
        return err ? rej(err) : res(rows);
      });

      connection.execSql(request);
    });
  })
}

support/command.js

Cypress.Commands.add('sqlServerDB1', (query) => {
    if (!query) {
        throw new Error('Query must be set');
    }

    cy.task('sqlServerDB1', query).then(response => {
        let result = [];

        const flatten = r => Array.isArray(r) && r.length === 1 ? flatten(r[0]) : r;

        if (response) {
            for (let i in response) {
                result[i] = [];
                for (let c in response[i]) {
                    result[i][c] = response[i][c].value;
                }
            }
            result = flatten(result);
        } else {
            result = response;
        }

        return result;
    });
});

Cypress.Commands.add('sqlServerDB2', (query) => {
    if (!query) {
        throw new Error('Query must be set');
    }

    cy.task('sqlServerDB2', query).then(response => {
        let result = [];

        const flatten = r => Array.isArray(r) && r.length === 1 ? flatten(r[0]) : r;

        if (response) {
            for (let i in response) {
                result[i] = [];
                for (let c in response[i]) {
                    result[i][c] = response[i][c].value;
                }
            }
            result = flatten(result);
        } else {
            result = response;
        }

        return result;
    });
});

cypress.json (config)

  "env": {
    "db1": {
      "server": "server_db1_ip_addr",
      "authentication": {
        "type": "default",
        "options": {
          "userName": "",
          "password": ""
        }
      },
      "options": {
        "port": 50001,
        "database": "db1_name",
        "encrypt": true,
        "rowCollectionOnRequestCompletion": true,
        "trustServerCertificate": true,
        "validateBulkLoadParameters": true
      }
    },
    "db2": {
      "server": "server_db2_ip_addr",
      "authentication": {
        "type": "default",
        "options": {
          "userName": "",
          "password": ""
        }
      },
      "options": {
        "port": 50001,
        "database": "db2_name",
        "encrypt": true,
        "rowCollectionOnRequestCompletion": true,
        "trustServerCertificate": true,
        "validateBulkLoadParameters": true
      }
    }
  }

Application code could then use:

    cy.sqlServerDB1("select db_name()").then(result => cy.log(result))
    cy.sqlServerDB2("select db_name()").then(result => cy.log(result))

Any suggestions for improvement are welcome.

Hi @djpasch,

Please ignore my message above.

I got it resolved as the command line run was picking a wrong config.

Thanks

Good to hear it’s working for you!