UNPKG

@stenneepro/mongoose-soft-delete

Version:
67 lines (66 loc) 2.91 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const mocha_1 = require("mocha"); const setupModel_1 = __importDefault(require("./utils/setupModel")); const dropModel_1 = __importDefault(require("./utils/dropModel")); const chai_1 = require("chai"); (0, mocha_1.describe)('aggregation', function () { let TestModel; before(async function () { TestModel = (0, setupModel_1.default)('TestAggregateDelete', { name: String }); }); beforeEach(async function () { await TestModel.create([ { name: 'Obi-Wan Kenobi', deleted: true }, { name: 'Darth Vader' }, { name: 'Luke Skywalker', deleted: true } ]); }); afterEach(async function () { await (0, dropModel_1.default)('TestAggregateDelete'); }); it('aggregate() -> returns 1 document', async function () { const documents = await TestModel.aggregate([ { $project: { name: 1 } } ]); (0, chai_1.expect)(documents).to.have.lengthOf(1); }); it('aggregate() -> with pipeline returns 1 document', async function () { const documents = await TestModel.aggregate().match({ name: 'Darth Vader' }).project({ name: 1 }); (0, chai_1.expect)(documents).to.have.lengthOf(1); }); it('aggregate() -> with deleted pipeline returns 0 document', async function () { const documents = await TestModel.aggregate().match({ name: 'Luke Skywalker' }).project({ name: 1 }); (0, chai_1.expect)(documents).to.have.lengthOf(0); }); it('aggregate() -> with delete returns 2 document', async function () { const documents = await TestModel.aggregate([ { $match: { deleted: true } }, { $project: { name: 1 } } ]); (0, chai_1.expect)(documents).to.have.lengthOf(2); }); it('aggregate() -> with delete returns 1 document', async function () { const documents = await TestModel.aggregate([ { $match: { name: 'Obi-Wan Kenobi' } }, { $match: { deleted: { $in: [true, false] } } }, { $project: { name: 1 } } ]); (0, chai_1.expect)(documents).to.have.lengthOf(1); }); it('aggregate() -> with onlyDeleted returns 2 document', async function () { const documents = await TestModel.aggregate([ { $project: { name: 1 } } ], { onlyDeleted: true }); (0, chai_1.expect)(documents).to.have.lengthOf(2); }); it('aggregate() -> with withDeleted returns 3 document', async function () { const documents = await TestModel.aggregate([ { $project: { name: 1 } } ], { withDeleted: true }); (0, chai_1.expect)(documents).to.have.lengthOf(3); }); });