123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- 'use strict';
- const chai = require('chai');
- const expect = chai.expect;
- const sinon = require('sinon');
- const Support = require('../support');
- const { DataTypes } = require('@sequelize/core');
- describe(Support.getTestDialectTeaser('Paranoid'), () => {
- beforeEach(async function () {
- const S = this.sequelize;
- const DT = DataTypes;
- const A = (this.A = S.define('A', { name: DT.STRING }, { paranoid: true }));
- const B = (this.B = S.define('B', { name: DT.STRING }, { paranoid: true }));
- const C = (this.C = S.define('C', { name: DT.STRING }, { paranoid: true }));
- const D = (this.D = S.define('D', { name: DT.STRING }, { paranoid: true }));
- A.belongsTo(B);
- A.belongsToMany(D, { through: 'a_d' });
- A.hasMany(C);
- B.hasMany(A);
- B.hasMany(C);
- C.belongsTo(A);
- C.belongsTo(B);
- D.belongsToMany(A, { through: 'a_d' });
- await S.sync({ force: true });
- });
- before(function () {
- this.clock = sinon.useFakeTimers();
- });
- after(function () {
- this.clock.restore();
- });
- it('paranoid with timestamps: false should be ignored / not crash', async function () {
- const S = this.sequelize;
- const Test = S.define(
- 'Test',
- {
- name: DataTypes.STRING,
- },
- {
- timestamps: false,
- paranoid: true,
- },
- );
- await S.sync({ force: true });
- await Test.findByPk(1);
- });
- it('test if non required is marked as false', async function () {
- const A = this.A;
- const B = this.B;
- const options = {
- include: [
- {
- model: B,
- required: false,
- },
- ],
- };
- await A.findOne(options);
- expect(options.include[0].required).to.equal(false);
- });
- it('test if required is marked as true', async function () {
- const A = this.A;
- const B = this.B;
- const options = {
- include: [
- {
- model: B,
- required: true,
- },
- ],
- };
- await A.findOne(options);
- expect(options.include[0].required).to.equal(true);
- });
- it('should not load paranoid, destroyed instances, with a non-paranoid parent', async function () {
- const X = this.sequelize.define(
- 'x',
- {
- name: DataTypes.STRING,
- },
- {
- paranoid: false,
- },
- );
- const Y = this.sequelize.define(
- 'y',
- {
- name: DataTypes.STRING,
- },
- {
- timestamps: true,
- paranoid: true,
- },
- );
- X.hasMany(Y);
- await this.sequelize.sync({ force: true });
- const [x0, y] = await Promise.all([X.create(), Y.create()]);
- this.x = x0;
- this.y = y;
- await x0.addY(y);
- await this.y.destroy();
- // prevent CURRENT_TIMESTAMP to be same
- this.clock.tick(1000);
- const obj = await X.findAll({
- include: [Y],
- });
- const x = await obj[0];
- expect(x.ys).to.have.length(0);
- });
- });
|