不要怂,就是干,撸起袖子干!

update.test.js 12.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
'use strict';

const chai = require('chai'),
  sinon = require('sinon'),
  Sequelize = require('../../../index'),
  expect = chai.expect,
  Support = require('../support'),
  DataTypes = require('../../../lib/data-types'),
  config = require('../../config/config'),
  current = Support.sequelize;

describe(Support.getTestDialectTeaser('Instance'), () => {
  before(function() {
    this.clock = sinon.useFakeTimers();
  });
  after(function() {
    this.clock.restore();
  });

  describe('update', () => {
    beforeEach(async function() {
      this.User = this.sequelize.define('User', {
        username: { type: DataTypes.STRING },
        uuidv1: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV1 },
        uuidv4: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4 },
        touchedAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
        aNumber: { type: DataTypes.INTEGER },
        bNumber: { type: DataTypes.INTEGER },
        aDate: { type: DataTypes.DATE },

        validateTest: {
          type: DataTypes.INTEGER,
          allowNull: true,
          validate: { isInt: true }
        },
        validateCustom: {
          type: DataTypes.STRING,
          allowNull: true,
          validate: { len: { msg: 'Length failed.', args: [1, 20] } }
        },
        validateSideEffect: {
          type: DataTypes.VIRTUAL,
          allowNull: true,
          validate: { isInt: true },
          set(val) {
            this.setDataValue('validateSideEffect', val);
            this.setDataValue('validateSideAffected', val * 2);
          }
        },
        validateSideAffected: {
          type: DataTypes.INTEGER,
          allowNull: true,
          validate: { isInt: true }
        },

        dateAllowNullTrue: {
          type: DataTypes.DATE,
          allowNull: true
        }
      });
      await this.User.sync({ force: true });
    });

    if (current.dialect.supports.transactions) {
      it('supports transactions', async function() {
        const sequelize = await Support.prepareTransactionTest(this.sequelize);
        const User = sequelize.define('User', { username: Support.Sequelize.STRING });

        await User.sync({ force: true });
        const user = await User.create({ username: 'foo' });
        const t = await sequelize.transaction();
        await user.update({ username: 'bar' }, { transaction: t });
        const users1 = await User.findAll();
        const users2 = await User.findAll({ transaction: t });
        expect(users1[0].username).to.equal('foo');
        expect(users2[0].username).to.equal('bar');
        await t.rollback();
      });
    }

    it('should update fields that are not specified on create', async function() {
      const User = this.sequelize.define(`User${  config.rand()}`, {
        name: DataTypes.STRING,
        bio: DataTypes.TEXT,
        email: DataTypes.STRING
      });

      await User.sync({ force: true });

      const user1 = await User.create({
        name: 'snafu',
        email: 'email'
      }, {
        fields: ['name', 'email']
      });

      const user0 = await user1.update({ bio: 'swag' });
      const user = await user0.reload();
      expect(user.get('name')).to.equal('snafu');
      expect(user.get('email')).to.equal('email');
      expect(user.get('bio')).to.equal('swag');
    });

    it('should succeed in updating when values are unchanged (without timestamps)', async function() {
      const User = this.sequelize.define(`User${  config.rand()}`, {
        name: DataTypes.STRING,
        bio: DataTypes.TEXT,
        email: DataTypes.STRING
      }, {
        timestamps: false
      });

      await User.sync({ force: true });

      const user1 = await User.create({
        name: 'snafu',
        email: 'email'
      }, {
        fields: ['name', 'email']
      });

      const user0 = await user1.update({
        name: 'snafu',
        email: 'email'
      });

      const user = await user0.reload();
      expect(user.get('name')).to.equal('snafu');
      expect(user.get('email')).to.equal('email');
    });

    it('should update timestamps with milliseconds', async function() {
      const User = this.sequelize.define(`User${  config.rand()}`, {
        name: DataTypes.STRING,
        bio: DataTypes.TEXT,
        email: DataTypes.STRING,
        createdAt: { type: DataTypes.DATE(6), allowNull: false },
        updatedAt: { type: DataTypes.DATE(6), allowNull: false }
      }, {
        timestamps: true
      });

      this.clock.tick(2100); //move the clock forward 2100 ms.

      await User.sync({ force: true });

      const user0 = await User.create({
        name: 'snafu',
        email: 'email'
      });

      const user = await user0.reload();
      expect(user.get('name')).to.equal('snafu');
      expect(user.get('email')).to.equal('email');
      const testDate = new Date();
      testDate.setTime(2100);
      expect(user.get('createdAt')).to.equalTime(testDate);
    });

    it('should only save passed attributes', async function() {
      const user = this.User.build();
      await user.save();
      user.set('validateTest', 5);
      expect(user.changed('validateTest')).to.be.ok;

      await user.update({
        validateCustom: '1'
      });

      expect(user.changed('validateTest')).to.be.ok;
      expect(user.validateTest).to.be.equal(5);
      await user.reload();
      expect(user.validateTest).to.not.be.equal(5);
    });

    it('should save attributes affected by setters', async function() {
      const user = this.User.build();
      await user.update({ validateSideEffect: 5 });
      expect(user.validateSideEffect).to.be.equal(5);
      await user.reload();
      expect(user.validateSideAffected).to.be.equal(10);
      expect(user.validateSideEffect).not.to.be.ok;
    });

    describe('hooks', () => {
      it('should update attributes added in hooks when default fields are used', async function() {
        const User = this.sequelize.define(`User${  config.rand()}`, {
          name: DataTypes.STRING,
          bio: DataTypes.TEXT,
          email: DataTypes.STRING
        });

        User.beforeUpdate(instance => {
          instance.set('email', 'B');
        });

        await User.sync({ force: true });

        const user0 = await User.create({
          name: 'A',
          bio: 'A',
          email: 'A'
        });

        await user0.update({
          name: 'B',
          bio: 'B'
        });

        const user = await User.findOne({});
        expect(user.get('name')).to.equal('B');
        expect(user.get('bio')).to.equal('B');
        expect(user.get('email')).to.equal('B');
      });

      it('should update attributes changed in hooks when default fields are used', async function() {
        const User = this.sequelize.define(`User${  config.rand()}`, {
          name: DataTypes.STRING,
          bio: DataTypes.TEXT,
          email: DataTypes.STRING
        });

        User.beforeUpdate(instance => {
          instance.set('email', 'C');
        });

        await User.sync({ force: true });

        const user0 = await User.create({
          name: 'A',
          bio: 'A',
          email: 'A'
        });

        await user0.update({
          name: 'B',
          bio: 'B',
          email: 'B'
        });

        const user = await User.findOne({});
        expect(user.get('name')).to.equal('B');
        expect(user.get('bio')).to.equal('B');
        expect(user.get('email')).to.equal('C');
      });

      it('should validate attributes added in hooks when default fields are used', async function() {
        const User = this.sequelize.define(`User${  config.rand()}`, {
          name: DataTypes.STRING,
          bio: DataTypes.TEXT,
          email: {
            type: DataTypes.STRING,
            validate: {
              isEmail: true
            }
          }
        });

        User.beforeUpdate(instance => {
          instance.set('email', 'B');
        });

        await User.sync({ force: true });

        const user0 = await User.create({
          name: 'A',
          bio: 'A',
          email: 'valid.email@gmail.com'
        });

        await expect(user0.update({
          name: 'B'
        })).to.be.rejectedWith(Sequelize.ValidationError);

        const user = await User.findOne({});
        expect(user.get('email')).to.equal('valid.email@gmail.com');
      });

      it('should validate attributes changed in hooks when default fields are used', async function() {
        const User = this.sequelize.define(`User${  config.rand()}`, {
          name: DataTypes.STRING,
          bio: DataTypes.TEXT,
          email: {
            type: DataTypes.STRING,
            validate: {
              isEmail: true
            }
          }
        });

        User.beforeUpdate(instance => {
          instance.set('email', 'B');
        });

        await User.sync({ force: true });

        const user0 = await User.create({
          name: 'A',
          bio: 'A',
          email: 'valid.email@gmail.com'
        });

        await expect(user0.update({
          name: 'B',
          email: 'still.valid.email@gmail.com'
        })).to.be.rejectedWith(Sequelize.ValidationError);

        const user = await User.findOne({});
        expect(user.get('email')).to.equal('valid.email@gmail.com');
      });
    });

    it('should not set attributes that are not specified by fields', async function() {
      const User = this.sequelize.define(`User${  config.rand()}`, {
        name: DataTypes.STRING,
        bio: DataTypes.TEXT,
        email: DataTypes.STRING
      });

      await User.sync({ force: true });

      const user0 = await User.create({
        name: 'snafu',
        email: 'email'
      });

      const user = await user0.update({
        bio: 'heyo',
        email: 'heho'
      }, {
        fields: ['bio']
      });

      expect(user.get('name')).to.equal('snafu');
      expect(user.get('email')).to.equal('email');
      expect(user.get('bio')).to.equal('heyo');
    });

    it('updates attributes in the database', async function() {
      const user = await this.User.create({ username: 'user' });
      expect(user.username).to.equal('user');
      const user0 = await user.update({ username: 'person' });
      expect(user0.username).to.equal('person');
    });

    it('ignores unknown attributes', async function() {
      const user = await this.User.create({ username: 'user' });
      const user0 = await user.update({ username: 'person', foo: 'bar' });
      expect(user0.username).to.equal('person');
      expect(user0.foo).not.to.exist;
    });

    it('ignores undefined attributes', async function() {
      await this.User.sync({ force: true });
      const user = await this.User.create({ username: 'user' });
      const user0 = await user.update({ username: undefined });
      expect(user0.username).to.equal('user');
    });

    it('doesn\'t update primary keys or timestamps', async function() {
      const User = this.sequelize.define(`User${  config.rand()}`, {
        name: DataTypes.STRING,
        bio: DataTypes.TEXT,
        identifier: { type: DataTypes.STRING, primaryKey: true }
      });

      await User.sync({ force: true });

      const user = await User.create({
        name: 'snafu',
        identifier: 'identifier'
      });

      const oldCreatedAt = user.createdAt,
        oldUpdatedAt = user.updatedAt,
        oldIdentifier = user.identifier;

      this.clock.tick(1000);

      const user0 = await user.update({
        name: 'foobar',
        createdAt: new Date(2000, 1, 1),
        identifier: 'another identifier'
      });

      expect(new Date(user0.createdAt)).to.equalDate(new Date(oldCreatedAt));
      expect(new Date(user0.updatedAt)).to.not.equalTime(new Date(oldUpdatedAt));
      expect(user0.identifier).to.equal(oldIdentifier);
    });

    it('stores and restores null values', async function() {
      const Download = this.sequelize.define('download', {
        startedAt: DataTypes.DATE,
        canceledAt: DataTypes.DATE,
        finishedAt: DataTypes.DATE
      });

      await Download.sync();

      const download = await Download.create({
        startedAt: new Date()
      });

      expect(download.startedAt instanceof Date).to.be.true;
      expect(download.canceledAt).to.not.be.ok;
      expect(download.finishedAt).to.not.be.ok;

      const download0 = await download.update({
        canceledAt: new Date()
      });

      expect(download0.startedAt instanceof Date).to.be.true;
      expect(download0.canceledAt instanceof Date).to.be.true;
      expect(download0.finishedAt).to.not.be.ok;

      const downloads = await Download.findAll({
        where: { finishedAt: null }
      });

      downloads.forEach(download => {
        expect(download.startedAt instanceof Date).to.be.true;
        expect(download.canceledAt instanceof Date).to.be.true;
        expect(download.finishedAt).to.not.be.ok;
      });
    });

    it('should support logging', async function() {
      const spy = sinon.spy();

      const user = await this.User.create({});
      await user.update({ username: 'yolo' }, { logging: spy });
      expect(spy.called).to.be.ok;
    });
  });
});