BlinkUX / sequelize-mock

A simple mock interface specifically for testing code relying on Sequelize models
https://sequelize-mock.readthedocs.io
MIT License
139 stars 73 forks source link

Include property (for associations) seems to be not supported #68

Open Kilian-Perisset opened 5 years ago

Kilian-Perisset commented 5 years ago

Hi,

Mock-Sequelize doesn't seem to support the "include" property from Sequelize. Here is an example of use :

const db = {};

// Creating a fake sector table
db.sector = dbMock.define(
  'sector',
  {
    name: 'Test sector',
    event_id: 1,
    created_at: '2018-11-18 00:00:01',
    updated_at: '2018-11-18 00:00:01',
    deleted_at: null,
  });

// Creating a fake event table
db.event = dbMock.define('event', {
  name: 'Test event,
  created_at: '2018-11-18 00:00:01',
  updated_at: '2018-11-18 00:00:01',
  deleted_at: null,
});

// Creating a relationship between sector and event
db.sector.belongsTo(db.event, { foreignKey: 'event_id' });

Above, I created two fake tables and now I want to get the event relative to a sector based on his id. So I have three choices :

1- Get the event with the function sector.getEvent() after getting my sector through a query. 2- Get the event through a query (like : event.findOne(... where: {id : sector.event_id}...)) 3- Get the event through the property include like this :

const sectorsQuery = {
      where: {
        id: params.id
      },
      include: [
        {
          model: db.event,
          required: true,
        }
      ]
    };
    const sector = await db.sector.findOne(sectorsQuery);

But the last one doesn't work with SequelizeMock and I don't know why... Why does the include property works well when I use Sequelize but not SequelizeMock ?

Do you have any functional examples of use ? for 1, N and N, M queries ? Or this feature is not (yet) supported by SequelizeMock ?

Thank you.

jpwiddy commented 4 years ago

Any work on this?

salauddinn commented 4 years ago

any update on this? @LoveAndCoding

salauddinn commented 4 years ago

Hi,

Mock-Sequelize doesn't seem to support the "include" property from Sequelize. Here is an example of use :

const db = {};

// Creating a fake sector table
db.sector = dbMock.define(
  'sector',
  {
    name: 'Test sector',
    event_id: 1,
    created_at: '2018-11-18 00:00:01',
    updated_at: '2018-11-18 00:00:01',
    deleted_at: null,
  });

// Creating a fake event table
db.event = dbMock.define('event', {
  name: 'Test event,
  created_at: '2018-11-18 00:00:01',
  updated_at: '2018-11-18 00:00:01',
  deleted_at: null,
});

// Creating a relationship between sector and event
db.sector.belongsTo(db.event, { foreignKey: 'event_id' });

Above, I created two fake tables and now I want to get the event relative to a sector based on his id. So I have three choices :

1- Get the event with the function sector.getEvent() after getting my sector through a query. 2- Get the event through a query (like : event.findOne(... where: {id : sector.event_id}...)) 3- Get the event through the property include like this :

const sectorsQuery = {
      where: {
        id: params.id
      },
      include: [
        {
          model: db.event,
          required: true,
        }
      ]
    };
    const sector = await db.sector.findOne(sectorsQuery);

But the last one doesn't work with SequelizeMock and I don't know why... Why does the include property works well when I use Sequelize but not SequelizeMock ?

Do you have any functional examples of use ? for 1, N and N, M queries ? Or this feature is not (yet) supported by SequelizeMock ?

Thank you.

@Shagequi were you able find any alternative to this?