|
| 1 | +const { factory } = require('factory-girl'); |
| 2 | +const { DataTypes, Sequelize } = require('sequelize'); |
| 3 | +const faker = require('faker/locale/pt_BR'); |
| 4 | +const sequelizeNoUpdate = require('../lib'); |
| 5 | + |
| 6 | +const sequelize = new Sequelize('sqlite::memory:', { |
| 7 | + logging: false, // Disables logging |
| 8 | +}); |
| 9 | + |
| 10 | +sequelizeNoUpdate(sequelize); |
| 11 | + |
| 12 | +const Model_1 = sequelize.define('Model_1', { |
| 13 | + attr1: DataTypes.STRING, |
| 14 | + attr2: DataTypes.STRING, |
| 15 | +}); |
| 16 | + |
| 17 | +const Model_2 = sequelize.define('Model_2', { |
| 18 | + attr1: { |
| 19 | + type: DataTypes.STRING, |
| 20 | + noUpdate: true, |
| 21 | + }, |
| 22 | + attr2: { |
| 23 | + type: DataTypes.STRING, |
| 24 | + noUpdate: false, |
| 25 | + }, |
| 26 | + attr3: DataTypes.STRING, |
| 27 | +}); |
| 28 | + |
| 29 | +const Model_3 = sequelize.define('Model_3', { |
| 30 | + attr1: { |
| 31 | + type: DataTypes.STRING, |
| 32 | + noUpdate: { |
| 33 | + readOnly: true, |
| 34 | + }, |
| 35 | + }, |
| 36 | + attr2: { |
| 37 | + type: DataTypes.STRING, |
| 38 | + noUpdate: false, |
| 39 | + }, |
| 40 | + attr3: DataTypes.STRING, |
| 41 | +}); |
| 42 | + |
| 43 | +factory.define('Model_1', Model_1, { |
| 44 | + attr1: faker.name.findName(), |
| 45 | + attr2: faker.name.findName(), |
| 46 | + attr3: faker.name.findName(), |
| 47 | +}); |
| 48 | +factory.define('Model_2', Model_2, { |
| 49 | + attr1: faker.name.findName(), |
| 50 | + attr2: faker.name.findName(), |
| 51 | + attr3: faker.name.findName(), |
| 52 | +}); |
| 53 | +factory.define('Model_3', Model_3, { |
| 54 | + attr1: faker.name.findName(), |
| 55 | + attr2: faker.name.findName(), |
| 56 | + attr3: faker.name.findName(), |
| 57 | +}); |
| 58 | + |
| 59 | +describe('if `noUpdate`', () => { |
| 60 | + beforeAll(() => { |
| 61 | + return sequelize.sync({ |
| 62 | + force: true, |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('was not set', () => { |
| 67 | + it('should allow attributes modifications', async () => { |
| 68 | + const model_1 = await factory.create('Model_1'); |
| 69 | + |
| 70 | + const response = model_1.update( |
| 71 | + { attr1: 'david' }, |
| 72 | + { |
| 73 | + where: { id: model_1.id }, |
| 74 | + } |
| 75 | + ); |
| 76 | + |
| 77 | + await expect(response).resolves.toMatchObject(response); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('was set', () => { |
| 82 | + it('should allow modifications on attributes without `noUpdate` set', async () => { |
| 83 | + const model_2 = await factory.create('Model_2'); |
| 84 | + |
| 85 | + const response = model_2.update( |
| 86 | + { attr3: 'david' }, |
| 87 | + { |
| 88 | + where: { id: model_2.id }, |
| 89 | + } |
| 90 | + ); |
| 91 | + |
| 92 | + await expect(response).resolves.toMatchObject(response); |
| 93 | + }); |
| 94 | + it('should allow modifications on attributes with `noUpdate=false` set', async () => { |
| 95 | + const model_2 = await factory.create('Model_2'); |
| 96 | + |
| 97 | + const response = model_2.update( |
| 98 | + { attr2: 'david' }, |
| 99 | + { |
| 100 | + where: { id: model_2.id }, |
| 101 | + } |
| 102 | + ); |
| 103 | + |
| 104 | + await expect(response).resolves.toMatchObject(response); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should not allow modifications on attributes with `noUpdate=true` set', async () => { |
| 109 | + const model_2 = await factory.create('Model_2'); |
| 110 | + |
| 111 | + const response = model_2.update( |
| 112 | + { attr1: 'david' }, |
| 113 | + { |
| 114 | + where: { id: model_2.id }, |
| 115 | + } |
| 116 | + ); |
| 117 | + |
| 118 | + await expect(response).rejects.toThrow( |
| 119 | + '`attr1` cannot be updated due `noUpdate` constraint' |
| 120 | + ); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('and readonly was set', () => { |
| 124 | + it('should not allow modifications on attributes with `readOnly=true` set', async () => { |
| 125 | + const model_3 = await factory.create('Model_3'); |
| 126 | + |
| 127 | + const response = model_3.update( |
| 128 | + { attr1: 'david' }, |
| 129 | + { |
| 130 | + where: { id: model_3.id }, |
| 131 | + } |
| 132 | + ); |
| 133 | + |
| 134 | + await expect(response).rejects.toThrow( |
| 135 | + 'attr1` cannot be updated due `noUpdate:readOnly` constraint' |
| 136 | + ); |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
0 commit comments