rochejul / sequelize-mocking

Sequelize extension to deal with data-mocking for testing
MIT License
63 stars 26 forks source link

Association and navigation method #6

Closed chriscamicas closed 4 years ago

chriscamicas commented 7 years ago

Does your mocking module work with association ? Let's say I have a parent-child model:

var Node = sequelize.define('Node', {
    id: { type: DataTypes.BIGINT, primaryKey: true, autoIncrement: true },
    text: DataTypes.STRING,
  }, {
    classMethods: {
      associate: function (models) {
        Node.hasMany(models.Node, { foreignKey: 'parent', as: 'Children', onDelete: 'CASCADE' });

In my classic model, I have a Node.getChildren() navigation method, when using sequelize-mocking, the same method is undefined.

thanks

rochejul commented 7 years ago

Hi

I don't know, I have not use yet association.

Could you extend on of the example (mocha, jasmine, tape) with a model with an association. I try to check it when I have enough time

Many thanks

flungo commented 7 years ago

Also having this problem. For example, if you add the following to the models.js:

UserModel.belongsTo(UserModel, { foreignKey: 'managerId', as: 'Manager', constraints: false });

Objects should then have a managerId field and the methods getManager and setManager should be added but they are not. To test this, I then added the following to the service-with-sequelize-mocking-mochaSpec:

describe('return an object with a', function () {
            describe('getManager method', function () {
                it('to exist', function () {
                    return UserService
                        .find(1)
                        .then(function (user) {
                            chai.expect(user.getManager).to.exist;
                        });
                });
                it('to return the target user', function () {
                    return UserService
                        .find(1)
                        .then(function (user) {
                            manager = user.getManager();

                            chai.expect(manager).deep.equals({
                                'id': 2,
                                'firstName': 'Joe',
                                'lastName': 'Bloggs',
                                'age': 56,
                                'description': null,
                                'managerId': null
                            });
                        });
                });
            });
        });

Having updated the fake-users-database.json to:

[
  {
    "model": "user",
    "data": {
      "id": 1,
      "firstName": "John",
      "lastName": "Doe",
      "age": 25,
      "description": null,
      "managerId": 2
    }
  },
  {
    "model": "user",
    "data": {
      "id": 2,
      "firstName": "Joe",
      "lastName": "Bloggs",
      "age": 56,
      "description": null,
      "managerId": null
    }
  }
]

If I have time I will make a full example for you.

rochejul commented 7 years ago

Hi @flungo

Many thanks for your feedback

If you could provide me an example, this is fine.

Otherwise, when I have some times, I try to look on it

Thanks again

rochejul commented 4 years ago

close due to inactivity