AlaSQL / alasql

AlaSQL.js - JavaScript SQL database for browser and Node.js. Handles both traditional relational tables and nested JSON data (NoSQL). Export, store, and import data from localStorage, IndexedDB, or Excel.
http://alasql.org
MIT License
7.02k stars 654 forks source link

[Question] How do I inject alasql instance to sequelize? (For testing purpose) #1003

Open martabacc opened 6 years ago

martabacc commented 6 years ago

[Description] For testing purpose, we want to simulate model & some other db-related script stuff and running it on some test scenarios. Rather than running it on some db, we prefer to run it on some in-memory database and alasql looks good for this.

We want to make sure that our defined models and db-related script is runs perfectly with current database condition.

[Purpose] Inject alasql instance to sequelize config. But in sequelize source code, the constructor needs dialects, username and password.

class Sequelize {
     constructor(database, username, password, options) {
     //some code
    }

If it is possible, the code will be like this for the setupTest.js


   import alasql from 'alasql'
   import Sequelize from 'sequelize'
   import sinon from 'sinon'
   import models from '../../src/models'; //models definition with sequelize

   describe('Model Test', () => {
        beforeEach( () => {
             const alasqlInstance = new alasql.Database();
             const alasqlConnectionInfo = alasqlInstance.getConnInfo();
             const sequelizeWithAlasql = new Sequelize(...alasqlConnectionInfo);

             // some bunch scripts to import data from real db
             dbTestSetup.replicateFromActualDB(sequelizeWithAlasql); 

             this.sandbox = sinon.sandbox.create();
             this.sequelizeStub = this.sandbox.stub(Sequelize);
             this.sequelizeStub.returns(sequelizeWithAlasql);
        });
        afterEach ( () => {
             this.sandbox.restore();
        });

        it('should run the query and get the right result', async () => {
             const expectedResult = [ /*some expectation*/ ];
             const query = await models.Transaction.getTransaction();
             expect(query).toDeepEqual(expectedResult);
        });
   });

Is there any way to inject alasql to this piece of code? Thank you

whyayala commented 5 years ago

I also would like to use alasql for testing purposes with knex.js

martabacc commented 5 years ago

is knex.js also has the same functionality like sequelize @whyayala ?