From c68a99fb5a40709c8ceab020f9d178687ae720e7 Mon Sep 17 00:00:00 2001 From: Sanel Deljkic Date: Fri, 9 Jun 2023 15:05:24 +0200 Subject: [PATCH] Refactoring and cleaning tests --- index_original.js | 1051 ------------------------------------ test/index.js | 1286 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 1027 insertions(+), 1310 deletions(-) delete mode 100644 index_original.js diff --git a/index_original.js b/index_original.js deleted file mode 100644 index 4761c10..0000000 --- a/index_original.js +++ /dev/null @@ -1,1051 +0,0 @@ -var chai = require('chai'), - should = chai.should(), - expect = chai.expect, - mongoose = require('mongoose'), - Schema = mongoose.Schema; - -var mongoose_delete = require('../'); - -var ObjectId = mongoose.Types.ObjectId; - -var mongooseMajorVersion = +mongoose.version[0]; // 4, 5, 6... - -chai.use(function (_chai, utils) { - utils.addChainableMethod(chai.Assertion.prototype, 'mongoose_count', function (val) { - if (mongooseMajorVersion >= 6) { - new chai.Assertion(this._obj.matchedCount).to.be.equal(val); - } else { - new chai.Assertion(this._obj.n).to.be.equal(val); - } - }); - - utils.addChainableMethod(chai.Assertion.prototype, 'mongoose_ok', function () { - if (mongooseMajorVersion >= 6) { - new chai.Assertion(this._obj.acknowledged).to.be.equal(true); - } else { - new chai.Assertion(this._obj.ok).to.be.equal(1); - } - }); - -}); - - - - - -describe("check the existence of override static methods: { overrideMethods: true }", function () { - var TestSchema = new Schema({name: String}, {collection: 'mongoose_delete_test'}); - TestSchema.plugin(mongoose_delete, {overrideMethods: true}); - var TestModel = mongoose.model('Test6', TestSchema); - - it("count() -> method should exist", function (done) { - expect(TestModel.count).to.exist; - done(); - }); - - it("countDeleted() -> method should exist", function (done) { - expect(TestModel.countDeleted).to.exist; - done(); - }); - - it("countWithDeleted() -> method should exist", function (done) { - expect(TestModel.countWithDeleted).to.exist; - done(); - }); - - it("countDocuments() -> method should exist", function (done) { - expect(TestModel.countDocuments).to.exist; - done(); - }); - - it("countDocumentsDeleted() -> method should exist", function (done) { - expect(TestModel.countDocumentsDeleted).to.exist; - done(); - }); - - it("countDocumentsWithDeleted() -> method should exist", function (done) { - expect(TestModel.countDocumentsWithDeleted).to.exist; - done(); - }); - - it("find() -> method should exist", function (done) { - expect(TestModel.find).to.exist; - done(); - }); - - it("findDeleted() -> method should exist", function (done) { - expect(TestModel.findDeleted).to.exist; - done(); - }); - - it("findWithDeleted() -> method should exist", function (done) { - expect(TestModel.findWithDeleted).to.exist; - done(); - }); - - it("findOne() -> method should exist", function (done) { - expect(TestModel.findOne).to.exist; - done(); - }); - - it("findOneDeleted() -> method should exist", function (done) { - expect(TestModel.findOneDeleted).to.exist; - done(); - }); - - it("findOneWithDeleted() -> method should exist", function (done) { - expect(TestModel.findOneWithDeleted).to.exist; - done(); - }); - - it("findOneAndUpdate() -> method should exist", function (done) { - expect(TestModel.findOneAndUpdate).to.exist; - done(); - }); - - it("findOneAndUpdateDeleted() -> method should exist", function (done) { - expect(TestModel.findOneAndUpdateDeleted).to.exist; - done(); - }); - - it("findOneAndUpdateWithDeleted() -> method should exist", function (done) { - expect(TestModel.findOneAndUpdateWithDeleted).to.exist; - done(); - }); - - it("update() -> method should exist", function (done) { - expect(TestModel.update).to.exist; - done(); - }); - - it("updateDeleted() -> method should exist", function (done) { - expect(TestModel.updateDeleted).to.exist; - done(); - }); - - it("updateWithDeleted() -> method should exist", function (done) { - expect(TestModel.updateWithDeleted).to.exist; - done(); - }); - - it("updateOne() -> method should exist", function (done) { - expect(TestModel.updateOne).to.exist; - done(); - }); - - it("updateOneDeleted() -> method should exist", function (done) { - expect(TestModel.updateOneDeleted).to.exist; - done(); - }); - - it("updateOneWithDeleted() -> method should exist", function (done) { - expect(TestModel.updateOneWithDeleted).to.exist; - done(); - }); - - it("updateMany() -> method should exist", function (done) { - expect(TestModel.updateMany).to.exist; - done(); - }); - - it("updateManyDeleted() -> method should exist", function (done) { - expect(TestModel.updateManyDeleted).to.exist; - done(); - }); - - it("updateManyWithDeleted() -> method should exist", function (done) { - expect(TestModel.updateManyWithDeleted).to.exist; - done(); - }); -}); - -describe("check the existence of override static methods: { overrideMethods: ['testError', 'count', 'countDocuments', 'find', 'findOne', 'findOneAndUpdate', 'update', 'updateOne', 'updateMany'] }", function () { - var TestSchema = new Schema({name: String}, {collection: 'mongoose_delete_test'}); - TestSchema.plugin(mongoose_delete, {overrideMethods: ['testError', 'count', 'countDocuments', 'find', 'findOne', 'findOneAndUpdate', 'update', 'updateOne', 'updateMany']}); - var TestModel = mongoose.model('Test7', TestSchema); - - it("testError() -> method should not exist", function (done) { - expect(TestModel.testError).to.not.exist; - done(); - }); - - it("count() -> method should exist", function (done) { - expect(TestModel.count).to.exist; - done(); - }); - - it("countDeleted() -> method should exist", function (done) { - expect(TestModel.countDeleted).to.exist; - done(); - }); - - it("countWithDeleted() -> method should exist", function (done) { - expect(TestModel.countWithDeleted).to.exist; - done(); - }); - - it("countDocuments() -> method should exist", function (done) { - expect(TestModel.countDocuments).to.exist; - done(); - }); - - it("countDocumentsDeleted() -> method should exist", function (done) { - expect(TestModel.countDocumentsDeleted).to.exist; - done(); - }); - - it("countDocumentsWithDeleted() -> method should exist", function (done) { - expect(TestModel.countDocumentsWithDeleted).to.exist; - done(); - }); - - it("find() -> method should exist", function (done) { - expect(TestModel.find).to.exist; - done(); - }); - - it("findDeleted() -> method should exist", function (done) { - expect(TestModel.findDeleted).to.exist; - done(); - }); - - it("findWithDeleted() -> method should exist", function (done) { - expect(TestModel.findWithDeleted).to.exist; - done(); - }); - - it("findOne() -> method should exist", function (done) { - expect(TestModel.findOne).to.exist; - done(); - }); - - it("findOneDeleted() -> method should exist", function (done) { - expect(TestModel.findOneDeleted).to.exist; - done(); - }); - - it("findOneWithDeleted() -> method should exist", function (done) { - expect(TestModel.findOneWithDeleted).to.exist; - done(); - }); - - it("findOneAndUpdate() -> method should exist", function (done) { - expect(TestModel.findOneAndUpdate).to.exist; - done(); - }); - - it("findOneAndUpdateDeleted() -> method should exist", function (done) { - expect(TestModel.findOneAndUpdateDeleted).to.exist; - done(); - }); - - it("findOneAndUpdateWithDeleted() -> method should exist", function (done) { - expect(TestModel.findOneAndUpdateWithDeleted).to.exist; - done(); - }); - - it("update() -> method should exist", function (done) { - expect(TestModel.update).to.exist; - done(); - }); - - it("updateDeleted() -> method should exist", function (done) { - expect(TestModel.updateDeleted).to.exist; - done(); - }); - - it("updateWithDeleted() -> method should exist", function (done) { - expect(TestModel.updateWithDeleted).to.exist; - done(); - }); - - it("updateOne() -> method should exist", function (done) { - expect(TestModel.updateOne).to.exist; - done(); - }); - - it("updateOneDeleted() -> method should exist", function (done) { - expect(TestModel.updateOneDeleted).to.exist; - done(); - }); - - it("updateOneWithDeleted() -> method should exist", function (done) { - expect(TestModel.updateOneWithDeleted).to.exist; - done(); - }); - - it("updateMany() -> method should exist", function (done) { - expect(TestModel.updateMany).to.exist; - done(); - }); - - it("updateManyDeleted() -> method should exist", function (done) { - expect(TestModel.updateManyDeleted).to.exist; - done(); - }); - - it("updateManyWithDeleted() -> method should exist", function (done) { - expect(TestModel.updateManyWithDeleted).to.exist; - done(); - }); -}); - -describe("check the existence of override static methods: { overrideMethods: ['count', 'countDocuments', 'find'] }", function () { - var TestSchema = new Schema({name: String}, {collection: 'mongoose_delete_test'}); - TestSchema.plugin(mongoose_delete, {overrideMethods: ['count', 'countDocuments', 'find']}); - var TestModel = mongoose.model('Test8', TestSchema); - - it("testError() -> method should not exist", function (done) { - expect(TestModel.testError).to.not.exist; - done(); - }); - - it("count() -> method should exist", function (done) { - expect(TestModel.count).to.exist; - done(); - }); - - it("countDeleted() -> method should exist", function (done) { - expect(TestModel.countDeleted).to.exist; - done(); - }); - - it("countWithDeleted() -> method should exist", function (done) { - expect(TestModel.countWithDeleted).to.exist; - done(); - }); - - it("countDocuments() -> method should exist", function (done) { - expect(TestModel.countDocuments).to.exist; - done(); - }); - - it("countDocumentsDeleted() -> method should exist", function (done) { - expect(TestModel.countDocumentsDeleted).to.exist; - done(); - }); - - it("countDocumentsWithDeleted() -> method should exist", function (done) { - expect(TestModel.countDocumentsWithDeleted).to.exist; - done(); - }); - - it("find() -> method should exist", function (done) { - expect(TestModel.find).to.exist; - done(); - }); - - it("findDeleted() -> method should exist", function (done) { - expect(TestModel.findDeleted).to.exist; - done(); - }); - - it("findWithDeleted() -> method should exist", function (done) { - expect(TestModel.findWithDeleted).to.exist; - done(); - }); - - it("findOne() -> method should exist", function (done) { - expect(TestModel.findOne).to.exist; - done(); - }); - - it("findOneDeleted() -> method should not exist", function (done) { - expect(TestModel.findOneDeleted).to.not.exist; - done(); - }); - - it("findOneWithDeleted() -> method should not exist", function (done) { - expect(TestModel.findOneWithDeleted).to.not.exist; - done(); - }); - - it("findOneAndUpdate() -> method should exist", function (done) { - expect(TestModel.findOneAndUpdate).to.exist; - done(); - }); - - it("findOneAndUpdateDeleted() -> method should not exist", function (done) { - expect(TestModel.findOneAndUpdateDeleted).to.not.exist; - done(); - }); - - it("findOneAndUpdateWithDeleted() -> method should not exist", function (done) { - expect(TestModel.findOneAndUpdateWithDeleted).to.not.exist; - done(); - }); - - it("update() -> method should exist", function (done) { - expect(TestModel.update).to.exist; - done(); - }); - - it("updateDeleted() -> method should not exist", function (done) { - expect(TestModel.updateDeleted).to.not.exist; - done(); - }); - - it("updateWithDeleted() -> method should not exist", function (done) { - expect(TestModel.updateWithDeleted).to.not.exist; - done(); - }); - - it("updateOne() -> method should exist", function (done) { - expect(TestModel.updateOne).to.exist; - done(); - }); - - it("updateOneDeleted() -> method should not exist", function (done) { - expect(TestModel.updateOneDeleted).to.not.exist; - done(); - }); - - it("updateOneWithDeleted() -> method should not exist", function (done) { - expect(TestModel.updateOneWithDeleted).to.not.exist; - done(); - }); - - it("updateMany() -> method should exist", function (done) { - expect(TestModel.updateMany).to.exist; - done(); - }); - - it("updateManyDeleted() -> method should not exist", function (done) { - expect(TestModel.updateManyDeleted).to.not.exist; - done(); - }); - - it("updateManyWithDeleted() -> method should not exist", function (done) { - expect(TestModel.updateManyWithDeleted).to.not.exist; - done(); - }); -}); - -describe("delete multiple documents", function () { - var TestSchema = new Schema({name: String, side: Number}, {collection: 'mongoose_delete_test'}); - TestSchema.plugin(mongoose_delete, {overrideMethods: 'all', deletedAt: true, deletedBy: true}); - var TestModel = mongoose.model('Test14', TestSchema); - - beforeEach(function (done) { - TestModel.create( - [ - {name: 'Obi-Wan Kenobi', side: 0}, - {name: 'Darth Vader', side: 1}, - {name: 'Luke Skywalker', side: 0} - ], done); - }); - - afterEach(function (done) { - mongoose.connection.db.dropCollection("mongoose_delete_test", done); - }); - - it("delete(cb) -> delete multiple documents", function (done) { - TestModel.delete(function (err, documents) { - should.not.exist(err); - - expect(documents).to.be.mongoose_ok(); - expect(documents).to.be.mongoose_count(3); - - done(); - }); - }); - - it("delete(query, cb) -> delete multiple documents with conditions", function (done) { - TestModel.delete({side: 0}, function (err, documents) { - should.not.exist(err); - - expect(documents).to.be.mongoose_ok(); - expect(documents).to.be.mongoose_count(2); - - done(); - }); - }); - - - it("delete(query, deletedBy, cb) -> delete multiple documents with conditions and user ID", function (done) { - var userId = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf"); - - TestModel.delete({side: 1}, userId, function (err, documents) { - should.not.exist(err); - - expect(documents).to.be.mongoose_ok(); - expect(documents).to.be.mongoose_count(1); - - done(); - }); - }); - - it("delete().exec() -> delete all documents", function (done) { - TestModel.delete().exec(function (err, documents) { - should.not.exist(err); - - expect(documents).to.be.mongoose_ok(); - expect(documents).to.be.mongoose_count(3); - - done(); - }); - }); - - it("delete(query).exec() -> delete multiple documents with conditions", function (done) { - TestModel.delete({side: 0}).exec(function (err, documents) { - should.not.exist(err); - - expect(documents).to.be.mongoose_ok(); - expect(documents).to.be.mongoose_count(2); - - done(); - }); - }); - - it("delete(query, deletedBy).exec() -> delete multiple documents with conditions and user ID", function (done) { - var userId = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf"); - - TestModel.delete({side: 1}, userId).exec(function (err, documents) { - should.not.exist(err); - - expect(documents).to.be.mongoose_count(1); - expect(documents).to.be.mongoose_ok(); - - done(); - }); - }); - - it("delete({}, deletedBy).exec() -> delete all documents passing user ID", function (done) { - var userId = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf"); - - TestModel.delete({}, userId).exec(function (err, documents) { - should.not.exist(err); - - expect(documents).to.be.mongoose_count(3); - expect(documents).to.be.mongoose_ok(); - - done(); - }); - }); -}); - -describe("delete multiple documents aaa", function () { - var TestSchema = new Schema({name: String, side: Number}, {collection: 'mongoose_delete_test'}); - TestSchema.plugin(mongoose_delete); - var TestModel = mongoose.model('Test13', TestSchema); - - beforeEach(function (done) { - TestModel.create( - [ - {name: 'Obi-Wan Kenobi', side: 0}, - {name: 'Darth Vader', side: 1}, - {name: 'Luke Skywalker', side: 0} - ], done); - }); - - afterEach(function (done) { - mongoose.connection.db.dropCollection("mongoose_delete_test", done); - }); - - it("delete(cb) -> delete multiple documents", function (done) { - TestModel.delete(function (err, documents) { - should.not.exist(err); - - expect(documents).to.be.mongoose_ok(); - expect(documents).to.be.mongoose_count(3); - - done(); - }); - }); -}); - -describe("restore multiple documents bbb", function () { - var TestSchema = new Schema({name: String, side: Number}, {collection: 'mongoose_restore_test'}); - TestSchema.plugin(mongoose_delete, {overrideMethods: 'all', deletedAt: true, deletedBy: true}); - var TestModel = mongoose.model('Test15', TestSchema); - - beforeEach(function (done) { - TestModel.create( - [ - {name: 'Obi-Wan Kenobi', side: 0}, - {name: 'Darth Vader', side: 1, deleted: true}, - {name: 'Luke Skywalker', side: 0, deleted: true} - ], done); - }); - - afterEach(function (done) { - mongoose.connection.db.dropCollection("mongoose_restore_test", done); - }); - - it("restore(cb) -> restore all documents", function (done) { - TestModel.restore(function (err, documents) { - should.not.exist(err); - - expect(documents).to.be.mongoose_ok(); - expect(documents).to.be.mongoose_count(3); - - done(); - }); - }); - - it("restore(query, cb) -> restore multiple documents with conditions", function (done) { - TestModel.restore({side: 0}, function (err, documents) { - should.not.exist(err); - - expect(documents).to.be.mongoose_ok(); - expect(documents).to.be.mongoose_count(2); - - done(); - }); - }); - - it("restore().exec() -> restore all documents", function (done) { - TestModel.restore().exec(function (err, documents) { - should.not.exist(err); - - expect(documents).to.be.mongoose_ok(); - expect(documents).to.be.mongoose_count(3); - - done(); - }); - }); - - it("restore(query).exec() -> restore multiple documents with conditions", function (done) { - TestModel.restore({side: 0}).exec(function (err, documents) { - should.not.exist(err); - - expect(documents).to.be.mongoose_ok(); - expect(documents).to.be.mongoose_count(2); - - done(); - }); - }); - -}); - -describe("restore multiple documents aaa", function () { - var TestSchema = new Schema({name: String, side: Number}, {collection: 'mongoose_restore_test'}); - TestSchema.plugin(mongoose_delete); - var TestModel = mongoose.model('Test16', TestSchema); - - beforeEach(function (done) { - TestModel.create( - [ - {name: 'Obi-Wan Kenobi', side: 0}, - {name: 'Darth Vader', side: 1, deleted: true}, - {name: 'Luke Skywalker', side: 0} - ], done); - }); - - afterEach(function (done) { - mongoose.connection.db.dropCollection("mongoose_restore_test", done); - }); - - it("restore(cb) -> restore all documents", function (done) { - TestModel.restore(function (err, documents) { - should.not.exist(err); - - expect(documents).to.be.mongoose_ok(); - expect(documents).to.be.mongoose_count(3); - - done(); - }); - }); -}); - -describe("model validation on delete (default): { validateBeforeDelete: true }", function () { - var TestSchema = new Schema({ - name: {type: String, required: true} - }, {collection: 'mongoose_restore_test'}); - TestSchema.plugin(mongoose_delete); - var TestModel = mongoose.model('Test17', TestSchema); - - beforeEach(function (done) { - TestModel.create( - [ - {name: 'Luke Skywalker'} - ], done); - }); - - afterEach(function (done) { - mongoose.connection.db.dropCollection("mongoose_restore_test", done); - }); - - it("delete() -> should raise ValidationError error", function (done) { - TestModel.findOne({name: 'Luke Skywalker'}, function (err, luke) { - should.not.exist(err); - luke.name = ""; - - luke.delete(function (err) { - err.should.exist; - err.name.should.exist; - err.name.should.equal('ValidationError'); - done(); - }); - }); - }); - - it("delete() -> should not raise ValidationError error", function (done) { - TestModel.findOne({name: 'Luke Skywalker'}, function (err, luke) { - should.not.exist(err); - luke.name = "Test Name"; - - luke.delete(function (err) { - should.not.exist(err); - done(); - }); - }); - }); -}); - -describe("model validation on delete: { validateBeforeDelete: false }", function () { - var TestSchema = new Schema({ - name: {type: String, required: true} - }, {collection: 'mongoose_restore_test'}); - TestSchema.plugin(mongoose_delete, {validateBeforeDelete: false}); - var TestModel = mongoose.model('Test18', TestSchema); - - beforeEach(function (done) { - TestModel.create( - [ - {name: 'Luke Skywalker'} - ], done); - }); - - afterEach(function (done) { - mongoose.connection.db.dropCollection("mongoose_restore_test", done); - }); - - it("delete() -> should not raise ValidationError error", function (done) { - TestModel.findOne({name: 'Luke Skywalker'}, function (err, luke) { - should.not.exist(err); - luke.name = ""; - - luke.delete(function (err) { - should.not.exist(err); - done(); - }); - }); - }); - - it("delete() -> should not raise ValidationError error", function (done) { - TestModel.findOne({name: 'Luke Skywalker'}, function (err, luke) { - should.not.exist(err); - luke.name = "Test Name"; - - luke.delete(function (err) { - should.not.exist(err); - done(); - }); - }); - }); -}); - -describe("mongoose_delete indexFields options", function () { - - it("all fields must have index: { indexFields: true }", function (done) { - var TestSchema = new Schema({name: String}, {collection: 'mongoose_delete_test_indexFields'}); - TestSchema.plugin(mongoose_delete, {indexFields: true, deletedAt: true, deletedBy: true}); - var Test0 = mongoose.model('Test0_indexFields', TestSchema); - - expect(Test0.schema.paths.deleted._index).to.be.true; - expect(Test0.schema.paths.deletedAt._index).to.be.true; - expect(Test0.schema.paths.deletedBy._index).to.be.true; - - done(); - }); - - it("all fields must have index: { indexFields: 'all' }", function (done) { - var TestSchema = new Schema({name: String}, {collection: 'mongoose_delete_test_indexFields'}); - TestSchema.plugin(mongoose_delete, {indexFields: 'all', deletedAt: true, deletedBy: true}); - var Test0 = mongoose.model('Test1_indexFields', TestSchema); - - expect(Test0.schema.paths.deleted._index).to.be.true; - expect(Test0.schema.paths.deletedAt._index).to.be.true; - expect(Test0.schema.paths.deletedBy._index).to.be.true; - done(); - }); - - it("only 'deleted' field must have index: { indexFields: ['deleted'] }", function (done) { - var TestSchema = new Schema({name: String}, {collection: 'mongoose_delete_test_indexFields'}); - TestSchema.plugin(mongoose_delete, {indexFields: ['deleted'], deletedAt: true, deletedBy: true}); - var Test0 = mongoose.model('Test2_indexFields', TestSchema); - - expect(Test0.schema.paths.deletedAt._index).to.be.false; - expect(Test0.schema.paths.deletedBy._index).to.be.false; - expect(Test0.schema.paths.deleted._index).to.be.true; - done(); - }); - - it("only 'deletedAt' and 'deletedBy' fields must have index: { indexFields: ['deletedAt', 'deletedBy'] }", function (done) { - var TestSchema = new Schema({name: String}, {collection: 'mongoose_delete_test_indexFields'}); - TestSchema.plugin(mongoose_delete, {indexFields: ['deletedAt', 'deletedBy'], deletedAt: true, deletedBy: true}); - var Test0 = mongoose.model('Test3_indexFields', TestSchema); - - expect(Test0.schema.paths.deleted._index).to.be.false; - expect(Test0.schema.paths.deletedAt._index).to.be.true; - expect(Test0.schema.paths.deletedBy._index).to.be.true; - done(); - }); -}); - -describe("check usage of $ne operator", function () { - var TestRawSchema = new Schema({name: String, deleted: Boolean}, {collection: 'mongoose_delete_test_ne'}); - var TestRawModel = mongoose.model('TestNeRaw', TestRawSchema); - - var TestSchema = new Schema({name: String}, {collection: 'mongoose_delete_test_ne'}); - TestSchema.plugin(mongoose_delete, {overrideMethods: 'all', use$neOperator: false}); - var TestModel = mongoose.model('Test55', TestSchema); - - before(function (done) { - TestRawModel.create( - [ - {name: 'One'}, - {name: 'Two', deleted: true}, - {name: 'Three', deleted: false} - ], done); - }); - - after(function (done) { - mongoose.connection.db.dropCollection("mongoose_delete_test_ne", done); - }); - - it("count() -> should return 1 documents", function (done) { - if (mongooseMajorVersion < 5) { - TestModel.count(function (err, count) { - should.not.exist(err); - - count.should.equal(1); - done(); - }); - } else { - done(); - } - }); - - it("countDeleted() -> should return 1 deleted documents", function (done) { - if (mongooseMajorVersion < 5) { - TestModel.countDeleted(function (err, count) { - should.not.exist(err); - - count.should.equal(1); - done(); - }); - } else { - done(); - } - }); - - it("find() -> should return 1 documents", function (done) { - TestModel.find(function (err, documents) { - should.not.exist(err); - - documents.length.should.equal(1); - done(); - }); - }); - - it("findDeleted() -> should return 1 documents", function (done) { - TestModel.findDeleted(function (err, documents) { - should.not.exist(err); - - documents.length.should.equal(1); - done(); - }); - }); -}); - -describe("aggregate methods: { overrideMethods: ['aggregate'] }", function () { - var TestSchema = new Schema({ name: String }, { collection: 'mongoose_delete_test_aggregate' }); - TestSchema.plugin(mongoose_delete, { overrideMethods: ['aggregate'] }); - - var TestModel = mongoose.model('Test5_Aggregate', TestSchema); - - beforeEach(function (done) { - TestModel.create( - [ - { name: 'Obi-Wan Kenobi', deleted: true }, - { name: 'Darth Vader' }, - { name: 'Luke Skywalker', deleted: true } - ], done); - }); - - afterEach(function (done) { - mongoose.connection.db.dropCollection("mongoose_delete_test_aggregate", done); - }); - - it("aggregate([{$project : {name : 1} }]) -> should return 1 document", function (done) { - TestModel.aggregate([ - { - $project : { name : 1 } - } - ], function (err, documents) { - should.not.exist(err); - documents.length.should.equal(1); - done(); - }); - }); - - it("aggregate([{$project : {name : 1} }]) -> should return 1 document (pipeline)", function (done) { - TestModel - .aggregate() - .project({ name : 1 }) - .exec(function (err, documents) { - should.not.exist(err); - documents.length.should.equal(1); - done(); - }); - }); - - it("aggregateDeleted([{$project : {name : 1} }]) -> should return deleted documents", function (done) { - TestModel.aggregateDeleted([ - { - $project : { name : 1 } - } - ], function (err, documents) { - should.not.exist(err); - documents.length.should.equal(2); - done(); - }); - }); - - it("aggregateDeleted([{$project : {name : 1} }]) -> should return deleted documents (pipeline)", function (done) { - TestModel - .aggregateDeleted() - .project({ name : 1 }) - .exec(function (err, documents) { - should.not.exist(err); - documents.length.should.equal(2); - done(); - }); - }); - - it("aggregateWithDeleted([{$project : {name : 1} }]) -> should return deleted documents", function (done) { - TestModel.aggregateWithDeleted([ - { - $project : { name : 1 } - } - ], function (err, documents) { - should.not.exist(err); - - documents.length.should.equal(3); - done(); - }); - }); - - it("aggregateWithDeleted([{$project : {name : 1} }]) -> should return deleted documents (pipeline)", function (done) { - TestModel - .aggregateWithDeleted() - .project({ name : 1 }) - .exec(function (err, documents) { - should.not.exist(err); - documents.length.should.equal(3); - done(); - }); - }); - -}); - -describe("mongoose_delete find method overridden with populate", function () { - var TestPopulateSchema1 = new Schema( - { name: String }, - { collection: 'TestPopulate1' } - ); - TestPopulateSchema1.plugin(mongoose_delete, { overrideMethods: 'all' }); - var TestPopulate1 = mongoose.model('TestPopulate1', TestPopulateSchema1); - - var TestPopulateSchema2 = new Schema( - { - name: String, - test: { type: ObjectId, ref: 'TestPopulate1' } - }, - { collection: 'TestPopulate2' } - ); - TestPopulateSchema2.plugin(mongoose_delete, { overrideMethods: 'all' }); - var TestPopulate2 = mongoose.model('TestPopulate2', TestPopulateSchema2); - - beforeEach(function (done) { - TestPopulate1.create( - [ - { name: 'Obi-Wan Kenobi', _id: ObjectId("53da93b16b4a6670076b16b1"), deleted: true }, - { name: 'Darth Vader', _id: ObjectId("53da93b16b4a6670076b16b2") }, - { name: 'Luke Skywalker', _id: ObjectId("53da93b16b4a6670076b16b3"), deleted: true } - ], - function() { - TestPopulate2.create( - [ - { name: 'Student 1', test: ObjectId("53da93b16b4a6670076b16b1") }, - { name: 'Student 2', test: ObjectId("53da93b16b4a6670076b16b2") }, - { name: 'Student 3', test: ObjectId("53da93b16b4a6670076b16b3"), deleted: true } - ], - done - ) - } - ); - }); - - afterEach(function (done) { - mongoose.connection.db.dropCollection("TestPopulate1", done); - }); - - afterEach(function (done) { - mongoose.connection.db.dropCollection("TestPopulate2", done); - }); - - it("populate() -> should not return deleted sub-document", function (done) { - TestPopulate2 - .findOne({ name: 'Student 1' }) - .populate({ path: 'test' }) - .exec(function (err, document) { - should.not.exist(err); - - expect(document.test).to.be.null; - done(); - }); - }); - - it("populate() -> should return the deleted sub-document using { withDeleted: true }", function (done) { - TestPopulate2 - .findOne({ name: 'Student 1' }) - .populate({ path: 'test', options: { withDeleted: true } }) - .exec(function (err, document) { - should.not.exist(err); - expect(document.test).not.to.be.null; - document.test.deleted.should.equal(true); - done(); - }); - }); - - it("populate() -> should not return deleted documents and sub-documents", function (done) { - TestPopulate2 - .find({ }) - .populate({ path: 'test' }) - .exec(function (err, documents) { - should.not.exist(err); - - var student1 = documents.findIndex(function(i) { return i.name === "Student 1" }); - var student2 = documents.findIndex(function(i) { return i.name === "Student 2" }); - - documents.length.should.equal(2) - expect(documents[student1].test).to.be.null; - expect(documents[student2].test).not.to.be.null; - - done(); - }); - }); - - it("populate() -> should return deleted documents and sub-documents", function (done) { - TestPopulate2 - .findWithDeleted() - .populate({ path: 'test', options: { withDeleted: true } }) - .exec(function (err, documents) { - should.not.exist(err); - - documents.length.should.equal(3); - - var student1 = documents.findIndex(function(i) { return i.name === "Student 1" }); - var student2 = documents.findIndex(function(i) { return i.name === "Student 2" }); - var student3 = documents.findIndex(function(i) { return i.name === "Student 3" }); - - expect(documents[student1].test).not.to.be.null; - expect(documents[student2].test).not.to.be.null; - expect(documents[student3].test).not.to.be.null; - done(); - }); - }); -}); diff --git a/test/index.js b/test/index.js index bde69f7..847caa0 100644 --- a/test/index.js +++ b/test/index.js @@ -696,7 +696,6 @@ describe("check overridden static methods: { overrideMethods: 'all' }", function } }); - it("countDocumentsDeleted() -> should return 2 deleted documents", async function () { try { const count = await TestModel.countDocumentsDeleted(); @@ -706,7 +705,6 @@ describe("check overridden static methods: { overrideMethods: 'all' }", function } }); - it("countDocumentsWithDeleted() -> should return 3 documents", async function () { try { const count = await TestModel.countDocumentsWithDeleted(); @@ -815,7 +813,6 @@ describe("check overridden static methods: { overrideMethods: 'all' }", function } }); - it("updateOne(conditions, update, options, callback) -> should not update first deleted document", async function () { try { const doc = await TestModel.updateOne({name: 'Luke Skywalker'}, {name: 'Luke Skywalker Test'}, {}); @@ -825,259 +822,1030 @@ describe("check overridden static methods: { overrideMethods: 'all' }", function should.not.exist(err); } }); - // - // it("updateOne(conditions, update, options, callback) -> should insert new document", function (done) { - // TestModel.updateOne({name: 'Luke Skywalker'}, {name: 'Luke Skywalker Test'}, {upsert: true}, function (err, doc) { - // should.not.exist(err); - // - // expect(doc).to.be.mongoose_ok(); - // - // if (mongooseMajorVersion >= 6) { - // expect(doc.upsertedId).not.to.be.null; - // expect(doc.upsertedId).not.to.be.undefined; - // doc.upsertedCount.should.equal(1); - // } else { - // expect(doc.upserted).not.to.be.null; - // expect(doc.upserted).not.to.be.undefined; - // expect(doc).to.be.mongoose_count(1); - // } - // - // done(); - // }); - // }); - // - // it("updateMany(conditions, update, options, callback) -> should not update deleted documents", function (done) { - // TestModel.updateMany({}, {name: 'Luke Skywalker Test'}, {multi: true}, function (err, doc) { - // should.not.exist(err); - // - // expect(doc).to.be.mongoose_ok(); - // expect(doc).to.be.mongoose_count(1); - // done(); - // }); - // }); - // - // it("update(conditions, update, options) -> should not update deleted documents", function (done) { - // if (mongooseMajorVersion < 5) { - // TestModel.update({}, {name: 'Luke Skywalker Test'}, {multi: true}).exec(function (err, doc) { - // should.not.exist(err); - // - // expect(doc).to.be.mongoose_ok(); - // expect(doc).to.be.mongoose_count(1); - // done(); - // }); - // } else { - // done(); - // } - // }); - // - // it("updateOne(conditions, update, options) -> should not update first deleted document", function (done) { - // TestModel.updateOne({name: 'Luke Skywalker'}, {name: 'Luke Skywalker Test'}, {}).exec(function (err, doc) { - // should.not.exist(err); - // - // expect(doc).to.be.mongoose_ok(); - // expect(doc).to.be.mongoose_count(0); - // done(); - // }); - // }); - // - // it("updateOne(conditions, update, options) -> should insert new document", function (done) { - // TestModel.updateOne({name: 'Luke Skywalker'}, {name: 'Luke Skywalker Test'}, {upsert: true}).exec(function (err, doc) { - // should.not.exist(err); - // - // expect(doc).to.be.mongoose_ok(); - // - // if (mongooseMajorVersion >= 6) { - // expect(doc.upsertedId).not.to.be.null; - // expect(doc.upsertedId).not.to.be.undefined; - // doc.upsertedCount.should.equal(1); - // } else { - // expect(doc.upserted).not.to.be.null; - // expect(doc.upserted).not.to.be.undefined; - // expect(doc).to.be.mongoose_count(1); - // } - // - // done(); - // }); - // }); - // - // it("updateMany(conditions, update, options) -> should not update deleted documents", function (done) { - // TestModel.updateMany({}, {name: 'Luke Skywalker Test'}, {multi: true}).exec(function (err, doc) { - // should.not.exist(err); - // - // expect(doc).to.be.mongoose_ok(); - // expect(doc).to.be.mongoose_count(1); - // - // done(); - // }); - // }); - // - // it("update(conditions, update, callback) -> should not update deleted documents", function (done) { - // if (mongooseMajorVersion < 5) { - // TestModel.update({}, {name: 'Luke Skywalker Test'}, function (err, doc) { - // should.not.exist(err); - // - // expect(doc).to.be.mongoose_ok(); - // expect(doc).to.be.mongoose_count(1); - // - // done(); - // }); - // } else { - // done(); - // } - // }); - // - // it("updateOne(conditions, update, callback) -> should not update first deleted document", function (done) { - // TestModel.updateOne({name: 'Luke Skywalker'}, {name: 'Luke Skywalker Test'}, function (err, doc) { - // should.not.exist(err); - // - // expect(doc).to.be.mongoose_ok(); - // expect(doc).to.be.mongoose_count(0); - // - // done(); - // }); - // }); - // - // it("updateMany(conditions, update, callback) -> should not update deleted documents", function (done) { - // TestModel.updateMany({}, {name: 'Luke Skywalker Test'}, function (err, doc) { - // should.not.exist(err); - // - // expect(doc).to.be.mongoose_ok(); - // expect(doc).to.be.mongoose_count(1); - // done(); - // }); - // }); - // - // it("update(conditions, update) -> should not update deleted documents", function (done) { - // if (mongooseMajorVersion < 5) { - // TestModel.update({}, {name: 'Luke Skywalker Test'}).exec(function (err, doc) { - // should.not.exist(err); - // - // expect(doc).to.be.mongoose_ok(); - // expect(doc).to.be.mongoose_count(1); - // done(); - // }); - // } else { - // done(); - // } - // }); - // - // it("updateOne(conditions, update) -> should not update first deleted document", function (done) { - // TestModel.updateOne({name: 'Luke Skywalker'}, {name: 'Luke Skywalker Test'}, {}, function (err, doc) { - // should.not.exist(err); - // - // expect(doc).to.be.mongoose_ok(); - // expect(doc).to.be.mongoose_count(0); - // - // done(); - // }); - // }); - // - // it("updateMany(conditions, update) -> should not update deleted documents", function (done) { - // TestModel.updateMany({}, {name: 'Luke Skywalker Test'}).exec(function (err, doc) { - // should.not.exist(err); - // - // expect(doc).to.be.mongoose_ok(); - // expect(doc).to.be.mongoose_count(1); - // - // done(); - // }); - // }); - // - // it("updateDeleted() -> should update deleted document", function (done) { - // if (mongooseMajorVersion < 5) { - // TestModel.updateDeleted({}, {name: 'Test 123'}, {multi: true}, function (err, doc) { - // should.not.exist(err); - // - // expect(doc).to.be.mongoose_ok(); - // expect(doc).to.be.mongoose_count(2); - // - // done(); - // }); - // } else { - // done(); - // } - // }); - // - // it("updateOneDeleted(conditions, update, options, callback) -> should update first deleted document", function (done) { - // TestModel.updateOneDeleted({name: 'Luke Skywalker'}, {name: 'Luke Skywalker Test'}, {}, function (err, doc) { - // should.not.exist(err); - // - // expect(doc).to.be.mongoose_ok(); - // expect(doc).to.be.mongoose_count(1); - // - // done(); - // }); - // }); - // - // it("updateOneDeleted(conditions, update, options, callback) -> should update first deleted document", function (done) { - // TestModel.updateOneDeleted({name: 'Luke Skywalker'}, {name: 'Luke Skywalker Test'}, {upsert: true}, function (err, doc) { - // should.not.exist(err); - // - // expect(doc.upserted).to.be.undefined; - // expect(doc).to.be.mongoose_ok(); - // expect(doc).to.be.mongoose_count(1); - // - // done(); - // }); - // }); - // - // it("updateManyDeleted() -> should update deleted document", function (done) { - // TestModel.updateManyDeleted({}, {name: 'Test 123'}, {multi: true}, function (err, doc) { - // should.not.exist(err); - // - // expect(doc).to.be.mongoose_ok(); - // expect(doc).to.be.mongoose_count(2); - // - // done(); - // }); - // }); - // - // it("updateWithDeleted() -> should update all document", function (done) { - // if (mongooseMajorVersion < 5) { - // TestModel.updateWithDeleted({}, {name: 'Test 654'}, {multi: true}, function (err, doc) { - // should.not.exist(err); - // - // expect(doc).to.be.mongoose_ok(); - // expect(doc).to.be.mongoose_count(3); - // - // done(); - // }); - // } else { - // done(); - // } - // }); - // - // it("updateOneWithDeleted(conditions, update, options, callback) -> should update first deleted document", function (done) { - // TestModel.updateOneWithDeleted({name: 'Luke Skywalker'}, {name: 'Luke Skywalker Test'}, {}, function (err, doc) { - // should.not.exist(err); - // - // expect(doc).to.be.mongoose_ok(); - // expect(doc).to.be.mongoose_count(1); - // - // done(); - // }); - // }); - // - // it("updateOneWithDeleted(conditions, update, options, callback) -> should update first deleted document", function (done) { - // TestModel.updateOneWithDeleted({name: 'Luke Skywalker'}, {name: 'Luke Skywalker Test'}, {upsert: true}, function (err, doc) { - // should.not.exist(err); - // - // expect(doc.upserted).to.be.undefined; - // - // expect(doc).to.be.mongoose_ok(); - // expect(doc).to.be.mongoose_count(1); - // - // done(); - // }); - // }); - // - // it("updateManyWithDeleted() -> should update all document", function (done) { - // TestModel.updateManyWithDeleted({}, {name: 'Test 654'}, {multi: true}, function (err, doc) { - // should.not.exist(err); - // - // expect(doc).to.be.mongoose_ok(); - // expect(doc).to.be.mongoose_count(3); - // done(); - // }); - // }); -}); \ No newline at end of file + + it("updateOne(conditions, update, options, callback) -> should insert new document", async function () { + try { + const doc = await TestModel.updateOne({name: 'Luke Skywalker'}, {name: 'Luke Skywalker'}, {upsert: true}); + + expect(doc).to.be.mongoose_ok(); + + if (mongooseMajorVersion >= 6) { + expect(doc.upsertedId).not.to.be.null; + expect(doc.upsertedId).not.to.be.undefined; + doc.upsertedCount.should.equal(1); + } else { + expect(doc.upserted).not.to.be.null; + expect(doc.upserted).not.to.be.undefined; + expect(doc).to.be.mongoose_count(1); + } + } catch (err) { + should.not.exist(err); + } + }); + + it("updateMany(conditions, update, options, callback) -> should not update deleted documents", async function () { + try { + const doc = await TestModel.updateMany({}, {name: 'Luke Skywalker Test'}, {multi: true}); + expect(doc).to.be.mongoose_ok(); + expect(doc).to.be.mongoose_count(1); + } catch (err) { + should.not.exist(err); + } + }); + + it("updateOne(conditions, update, options) -> should not update first deleted document", async function () { + try { + const doc = await TestModel.updateOne({name: 'Luke Skywalker'}, {name: 'Luke Skywalker Test'}, {}); + expect(doc).to.be.mongoose_ok(); + expect(doc).to.be.mongoose_count(0); + } catch (err) { + should.not.exist(err); + } + }); + + it("updateOne(conditions, update, options) -> should insert new document", async function () { + try { + const doc = await TestModel.updateOne({name: 'Luke Skywalker'}, {name: 'Luke Skywalker Test'}, {upsert: true}); + + expect(doc).to.be.mongoose_ok(); + + if (mongooseMajorVersion >= 6) { + expect(doc.upsertedId).not.to.be.null; + expect(doc.upsertedId).not.to.be.undefined; + doc.upsertedCount.should.equal(1); + } else { + expect(doc.upserted).not.to.be.null; + expect(doc.upserted).not.to.be.undefined; + expect(doc).to.be.mongoose_count(1); + } + } catch (err) { + should.not.exist(err); + } + }); + + it("updateMany(conditions, update, options) -> should not update deleted documents", async function () { + try { + const doc = await TestModel.updateMany({}, {name: 'Luke Skywalker Test'}, {multi: true}); + expect(doc).to.be.mongoose_ok(); + expect(doc).to.be.mongoose_count(1); + } catch (err) { + + } + }); + + it("updateOne(conditions, update, callback) -> should not update first deleted document", async function () { + try { + const doc = await TestModel.updateOne({name: 'Luke Skywalker'}, {name: 'Luke Skywalker Test'}); + expect(doc).to.be.mongoose_ok(); + expect(doc).to.be.mongoose_count(0); + } catch (err) { + should.not.exist(err); + } + }); + + it("updateMany(conditions, update, callback) -> should not update deleted documents", async function () { + try { + const doc = await TestModel.updateMany({}, {name: 'Luke Skywalker Test'}); + expect(doc).to.be.mongoose_ok(); + expect(doc).to.be.mongoose_count(1); + } catch (err) { + should.not.exist(err); + } + }); + + it("updateOne(conditions, update) -> should not update first deleted document", async function () { + try { + const doc = await TestModel.updateOne({name: 'Luke Skywalker'}, {name: 'Luke Skywalker Test'}, {}); + expect(doc).to.be.mongoose_ok(); + expect(doc).to.be.mongoose_count(0); + } catch (err) { + should.not.exist(err); + } + }); + + it("updateMany(conditions, update) -> should not update deleted documents", async function () { + try { + const doc = await TestModel.updateMany({}, {name: 'Luke Skywalker Test'}); + expect(doc).to.be.mongoose_ok(); + expect(doc).to.be.mongoose_count(1); + } catch (err) { + should.not.exist(err); + } + }); + + it("updateOneDeleted(conditions, update, options, callback) -> should update first deleted document", async function () { + try { + const doc = await TestModel.updateOneDeleted({name: 'Luke Skywalker'}, {name: 'Luke Skywalker Test'}, {}); + + expect(doc).to.be.mongoose_ok(); + expect(doc).to.be.mongoose_count(1); + } catch (err) { + should.not.exist(err); + } + }); + + it("updateOneDeleted(conditions, update, options, callback) -> should update first deleted document", async function () { + try { + const doc = await TestModel.updateOneDeleted({name: 'Luke Skywalker'}, {name: 'Luke Skywalker Test'}, {upsert: true}); + + expect(doc.upserted).to.be.undefined; + expect(doc).to.be.mongoose_ok(); + expect(doc).to.be.mongoose_count(1); + } catch (err) { + should.not.exist(err); + } + + }); + + it("updateManyDeleted() -> should update deleted document", async function () { + try { + const doc = await TestModel.updateManyDeleted({}, {name: 'Test 123'}, {multi: true}); + + expect(doc).to.be.mongoose_ok(); + expect(doc).to.be.mongoose_count(2); + } catch (err) { + should.not.exist(err); + } + }); + + it("updateOneWithDeleted(conditions, update, options, callback) -> should update first deleted document", async function () { + try { + const doc = await TestModel.updateOneWithDeleted({name: 'Luke Skywalker'}, {name: 'Luke Skywalker Test'}, {}); + + expect(doc).to.be.mongoose_ok(); + expect(doc).to.be.mongoose_count(1); + } catch (err) { + should.not.exist(err); + } + }); + + it("updateOneWithDeleted(conditions, update, options, callback) -> should update first deleted document", async function () { + try { + const doc = await TestModel.updateOneWithDeleted({name: 'Luke Skywalker'}, {name: 'Luke Skywalker Test'}, {upsert: true}); + + expect(doc.upserted).to.be.undefined; + + expect(doc).to.be.mongoose_ok(); + expect(doc).to.be.mongoose_count(1); + } catch (err) { + should.not.exist(err); + } + }); + + it("updateManyWithDeleted() -> should update all document", async function () { + try { + const doc = await TestModel.updateManyWithDeleted({}, {name: 'Test 654'}, {multi: true}); + + expect(doc).to.be.mongoose_ok(); + expect(doc).to.be.mongoose_count(3); + } catch (err) { + should.not.exist(err); + } + }); +}); + +describe("check the existence of override static methods: { overrideMethods: true }", function () { + var TestSchema = new Schema({name: String}, {collection: 'mongoose_delete_test'}); + TestSchema.plugin(mongoose_delete, {overrideMethods: true}); + var TestModel = mongoose.model('Test6', TestSchema); + + it("count() -> method should exist", function () { + expect(TestModel.count).to.exist; + }); + + it("countDeleted() -> method should exist", function () { + expect(TestModel.countDeleted).to.exist; + }); + + it("countWithDeleted() -> method should exist", function () { + expect(TestModel.countWithDeleted).to.exist; + }); + + it("countDocuments() -> method should exist", function () { + expect(TestModel.countDocuments).to.exist; + }); + + it("countDocumentsDeleted() -> method should exist", function () { + expect(TestModel.countDocumentsDeleted).to.exist; + }); + + it("countDocumentsWithDeleted() -> method should exist", function () { + expect(TestModel.countDocumentsWithDeleted).to.exist; + }); + + it("find() -> method should exist", function () { + expect(TestModel.find).to.exist; + }); + + it("findDeleted() -> method should exist", function () { + expect(TestModel.findDeleted).to.exist; + }); + + it("findWithDeleted() -> method should exist", function () { + expect(TestModel.findWithDeleted).to.exist; + }); + + it("findOne() -> method should exist", function () { + expect(TestModel.findOne).to.exist; + }); + + it("findOneDeleted() -> method should exist", function () { + expect(TestModel.findOneDeleted).to.exist; + }); + + it("findOneWithDeleted() -> method should exist", function () { + expect(TestModel.findOneWithDeleted).to.exist; + }); + + it("findOneAndUpdate() -> method should exist", function () { + expect(TestModel.findOneAndUpdate).to.exist; + }); + + it("findOneAndUpdateDeleted() -> method should exist", function () { + expect(TestModel.findOneAndUpdateDeleted).to.exist; + }); + + it("findOneAndUpdateWithDeleted() -> method should exist", function () { + expect(TestModel.findOneAndUpdateWithDeleted).to.exist; + }); + + it("update() -> method should exist", function () { + expect(TestModel.update).to.exist; + }); + + it("updateDeleted() -> method should exist", function () { + expect(TestModel.updateDeleted).to.exist; + }); + + it("updateWithDeleted() -> method should exist", function () { + expect(TestModel.updateWithDeleted).to.exist; + }); + + it("updateOne() -> method should exist", function () { + expect(TestModel.updateOne).to.exist; + }); + + it("updateOneDeleted() -> method should exist", function () { + expect(TestModel.updateOneDeleted).to.exist; + }); + + it("updateOneWithDeleted() -> method should exist", function () { + expect(TestModel.updateOneWithDeleted).to.exist; + }); + + it("updateMany() -> method should exist", function () { + expect(TestModel.updateMany).to.exist; + }); + + it("updateManyDeleted() -> method should exist", function () { + expect(TestModel.updateManyDeleted).to.exist; + }); + + it("updateManyWithDeleted() -> method should exist", function () { + expect(TestModel.updateManyWithDeleted).to.exist; + }); +}); + +describe("check the existence of override static methods: { overrideMethods: ['testError', 'count', 'countDocuments', 'find', 'findOne', 'findOneAndUpdate', 'update', 'updateOne', 'updateMany'] }", function () { + var TestSchema = new Schema({name: String}, {collection: 'mongoose_delete_test'}); + TestSchema.plugin(mongoose_delete, {overrideMethods: ['testError', 'count', 'countDocuments', 'find', 'findOne', 'findOneAndUpdate', 'update', 'updateOne', 'updateMany']}); + var TestModel = mongoose.model('Test7', TestSchema); + + it("testError() -> method should not exist", function () { + expect(TestModel.testError).to.not.exist; + }); + + it("count() -> method should exist", function () { + expect(TestModel.count).to.exist; + }); + + it("countDeleted() -> method should exist", function () { + expect(TestModel.countDeleted).to.exist; + }); + + it("countWithDeleted() -> method should exist", function () { + expect(TestModel.countWithDeleted).to.exist; + }); + + it("countDocuments() -> method should exist", function () { + expect(TestModel.countDocuments).to.exist; + }); + + it("countDocumentsDeleted() -> method should exist", function () { + expect(TestModel.countDocumentsDeleted).to.exist; + }); + + it("countDocumentsWithDeleted() -> method should exist", function () { + expect(TestModel.countDocumentsWithDeleted).to.exist; + }); + + it("find() -> method should exist", function () { + expect(TestModel.find).to.exist; + }); + + it("findDeleted() -> method should exist", function () { + expect(TestModel.findDeleted).to.exist; + }); + + it("findWithDeleted() -> method should exist", function () { + expect(TestModel.findWithDeleted).to.exist; + }); + + it("findOne() -> method should exist", function () { + expect(TestModel.findOne).to.exist; + }); + + it("findOneDeleted() -> method should exist", function () { + expect(TestModel.findOneDeleted).to.exist; + }); + + it("findOneWithDeleted() -> method should exist", function () { + expect(TestModel.findOneWithDeleted).to.exist; + }); + + it("findOneAndUpdate() -> method should exist", function () { + expect(TestModel.findOneAndUpdate).to.exist; + }); + + it("findOneAndUpdateDeleted() -> method should exist", function () { + expect(TestModel.findOneAndUpdateDeleted).to.exist; + }); + + it("findOneAndUpdateWithDeleted() -> method should exist", function () { + expect(TestModel.findOneAndUpdateWithDeleted).to.exist; + }); + + it("update() -> method should exist", function () { + expect(TestModel.update).to.exist; + }); + + it("updateDeleted() -> method should exist", function () { + expect(TestModel.updateDeleted).to.exist; + }); + + it("updateWithDeleted() -> method should exist", function () { + expect(TestModel.updateWithDeleted).to.exist; + }); + + it("updateOne() -> method should exist", function () { + expect(TestModel.updateOne).to.exist; + }); + + it("updateOneDeleted() -> method should exist", function () { + expect(TestModel.updateOneDeleted).to.exist; + }); + + it("updateOneWithDeleted() -> method should exist", function () { + expect(TestModel.updateOneWithDeleted).to.exist; + }); + + it("updateMany() -> method should exist", function () { + expect(TestModel.updateMany).to.exist; + }); + + it("updateManyDeleted() -> method should exist", function () { + expect(TestModel.updateManyDeleted).to.exist; + }); + + it("updateManyWithDeleted() -> method should exist", function () { + expect(TestModel.updateManyWithDeleted).to.exist; + }); +}); + +describe("check the existence of override static methods: { overrideMethods: ['count', 'countDocuments', 'find'] }", function () { + var TestSchema = new Schema({name: String}, {collection: 'mongoose_delete_test'}); + TestSchema.plugin(mongoose_delete, {overrideMethods: ['count', 'countDocuments', 'find']}); + var TestModel = mongoose.model('Test8', TestSchema); + + it("testError() -> method should not exist", function () { + expect(TestModel.testError).to.not.exist; + }); + + it("count() -> method should exist", function () { + expect(TestModel.count).to.exist; + }); + + it("countDeleted() -> method should exist", function () { + expect(TestModel.countDeleted).to.exist; + }); + + it("countWithDeleted() -> method should exist", function () { + expect(TestModel.countWithDeleted).to.exist; + }); + + it("countDocuments() -> method should exist", function () { + expect(TestModel.countDocuments).to.exist; + }); + + it("countDocumentsDeleted() -> method should exist", function () { + expect(TestModel.countDocumentsDeleted).to.exist; + }); + + it("countDocumentsWithDeleted() -> method should exist", function () { + expect(TestModel.countDocumentsWithDeleted).to.exist; + }); + + it("find() -> method should exist", function () { + expect(TestModel.find).to.exist; + }); + + it("findDeleted() -> method should exist", function () { + expect(TestModel.findDeleted).to.exist; + }); + + it("findWithDeleted() -> method should exist", function () { + expect(TestModel.findWithDeleted).to.exist; + }); + + it("findOne() -> method should exist", function () { + expect(TestModel.findOne).to.exist; + }); + + it("findOneDeleted() -> method should not exist", function () { + expect(TestModel.findOneDeleted).to.not.exist; + }); + + it("findOneWithDeleted() -> method should not exist", function () { + expect(TestModel.findOneWithDeleted).to.not.exist; + }); + + it("findOneAndUpdate() -> method should exist", function () { + expect(TestModel.findOneAndUpdate).to.exist; + }); + + it("findOneAndUpdateDeleted() -> method should not exist", function () { + expect(TestModel.findOneAndUpdateDeleted).to.not.exist; + }); + + it("findOneAndUpdateWithDeleted() -> method should not exist", function () { + expect(TestModel.findOneAndUpdateWithDeleted).to.not.exist; + }); + + it("update() -> method should exist", function () { + if (mongooseMajorVersion <= 6) { + expect(TestModel.update).to.exist; + } + }); + + it("updateDeleted() -> method should not exist", function () { + expect(TestModel.updateDeleted).to.not.exist; + }); + + it("updateWithDeleted() -> method should not exist", function () { + expect(TestModel.updateWithDeleted).to.not.exist; + }); + + it("updateOne() -> method should exist", function () { + expect(TestModel.updateOne).to.exist; + }); + + it("updateOneDeleted() -> method should not exist", function () { + expect(TestModel.updateOneDeleted).to.not.exist; + }); + + it("updateOneWithDeleted() -> method should not exist", function () { + expect(TestModel.updateOneWithDeleted).to.not.exist; + }); + + it("updateMany() -> method should exist", function () { + expect(TestModel.updateMany).to.exist; + }); + + it("updateManyDeleted() -> method should not exist", function () { + expect(TestModel.updateManyDeleted).to.not.exist; + }); + + it("updateManyWithDeleted() -> method should not exist", function () { + expect(TestModel.updateManyWithDeleted).to.not.exist; + }); +}); + +describe("delete multiple documents", function () { + var TestSchema = new Schema({name: String, side: Number}, {collection: 'mongoose_delete_test'}); + TestSchema.plugin(mongoose_delete, {overrideMethods: 'all', deletedAt: true, deletedBy: true}); + var TestModel = mongoose.model('Test14', TestSchema); + + beforeEach(async function () { + await TestModel.create( + [ + {name: 'Obi-Wan Kenobi', side: 0}, + {name: 'Darth Vader', side: 1}, + {name: 'Luke Skywalker', side: 0} + ]); + }); + + afterEach(async function () { + await mongoose.connection.db.dropCollection("mongoose_delete_test"); + }); + + var userId = getNewObjectId("53da93b16b4a6670076b16bf") + + it("delete() -> delete multiple documents", async function () { + try { + const documents = await TestModel.delete(); + + expect(documents).to.be.mongoose_ok(); + expect(documents).to.be.mongoose_count(3); + } catch (err) { + should.not.exist(err); + } + }); + + it("delete(query) -> delete multiple documents with conditions", async function () { + try { + const documents = await TestModel.delete({side: 0}); + + expect(documents).to.be.mongoose_ok(); + expect(documents).to.be.mongoose_count(2); + } catch (err) { + should.not.exist(err); + } + }); + + it("delete(query, deletedBy) -> delete multiple documents with conditions and user ID", async function () { + try { + const documents = await TestModel.delete({side: 1}, userId); + + expect(documents).to.be.mongoose_ok(); + expect(documents).to.be.mongoose_count(1); + } catch (err) { + should.not.exist(err); + } + }); + + it("delete().exec() -> delete all documents", async function () { + try { + const documents = await TestModel.delete().exec(); + + expect(documents).to.be.mongoose_ok(); + expect(documents).to.be.mongoose_count(3); + } catch (err) { + should.not.exist(err); + } + }); + + it("delete(query).exec() -> delete multiple documents with conditions", async function () { + try { + const documents = await TestModel.delete({side: 0}).exec(); + + expect(documents).to.be.mongoose_ok(); + expect(documents).to.be.mongoose_count(2); + } catch (err) { + should.not.exist(err); + } + }); + + it("delete(query, deletedBy).exec() -> delete multiple documents with conditions and user ID", async function () { + try { + const documents = await TestModel.delete({side: 1}, userId).exec(); + + expect(documents).to.be.mongoose_count(1); + expect(documents).to.be.mongoose_ok(); + } catch (err) { + should.not.exist(err); + } + }); + + it("delete({}, deletedBy).exec() -> delete all documents passing user ID", async function () { + try { + const documents = await TestModel.delete({}, userId).exec(); + + expect(documents).to.be.mongoose_count(3); + expect(documents).to.be.mongoose_ok(); + } catch (err) { + should.not.exist(err); + } + }); +}); + +describe("restore multiple documents", function () { + var TestSchema = new Schema({name: String, side: Number}, {collection: 'mongoose_restore_test'}); + TestSchema.plugin(mongoose_delete, {overrideMethods: 'all', deletedAt: true, deletedBy: true}); + var TestModel = mongoose.model('Test15', TestSchema); + + beforeEach(async function () { + await TestModel.create( + [ + {name: 'Obi-Wan Kenobi', side: 0}, + {name: 'Darth Vader', side: 1, deleted: true}, + {name: 'Luke Skywalker', side: 0, deleted: true, deletedAt: new Date()} + ] + ); + }); + + afterEach(async function () { + await mongoose.connection.db.dropCollection("mongoose_restore_test"); + }); + + it("restore() -> restore all documents", async function () { + try { + const documents = await TestModel.restore(); + + expect(documents).to.be.mongoose_ok(); + expect(documents).to.be.mongoose_count(3); + } catch (err) { + should.not.exist(err); + } + }); + + it("restore(query) -> restore multiple documents with conditions", async function () { + try { + const documents = await TestModel.restore({side: 0}); + + expect(documents).to.be.mongoose_ok(); + expect(documents).to.be.mongoose_count(2); + } catch (err) { + should.not.exist(err); + } + }); + + it("restore().exec() -> restore all documents", async function () { + try { + const documents = await TestModel.restore().exec(); + + expect(documents).to.be.mongoose_ok(); + expect(documents).to.be.mongoose_count(3); + } catch (err) { + should.not.exist(err); + } + }); + + it("restore(query).exec() -> restore multiple documents with conditions", async function () { + try { + const documents = await TestModel.restore({side: 0}).exec(); + + expect(documents).to.be.mongoose_ok(); + expect(documents).to.be.mongoose_count(2); + } catch (err) { + should.not.exist(err); + } + }); + +}); + +describe("model validation on delete (default): { validateBeforeDelete: true }", function () { + var TestSchema = new Schema({ name: {type: String, required: true}}, {collection: 'mongoose_restore_test'}); + TestSchema.plugin(mongoose_delete); + var TestModel = mongoose.model('Test17', TestSchema); + + beforeEach(async function () { + await TestModel.create( + [ + {name: 'Luke Skywalker'} + ]); + }); + + afterEach(async function () { + await mongoose.connection.db.dropCollection("mongoose_restore_test"); + }); + + it("delete() -> should raise ValidationError error", async function () { + try { + const luke = await TestModel.findOne({name: 'Luke Skywalker'}); + luke.name = ""; + try { + await luke.delete(); + } catch (e) { + e.should.exist; + e.name.should.exist; + e.name.should.equal('ValidationError'); + } + } catch (err) { + should.not.exist(err); + } + }); + + it("delete() -> should not raise ValidationError error", async function () { + try { + const luke = await TestModel.findOne({name: 'Luke Skywalker'}); + luke.name = "Test Name"; + try { + await luke.delete(); + } catch (e) { + should.not.exist(err); + } + } catch (err) { + should.not.exist(err); + } + }); +}); + +describe("model validation on delete: { validateBeforeDelete: false }", function () { + var TestSchema = new Schema({ + name: {type: String, required: true} + }, {collection: 'mongoose_restore_test'}); + TestSchema.plugin(mongoose_delete, {validateBeforeDelete: false}); + var TestModel = mongoose.model('Test18', TestSchema); + + beforeEach(async function () { + await TestModel.create( + [ + {name: 'Luke Skywalker'} + ]); + }); + + afterEach(async function () { + await mongoose.connection.db.dropCollection("mongoose_restore_test"); + }); + + it("delete() -> should not raise ValidationError error", async function () { + try { + const luke = await TestModel.findOne({name: 'Luke Skywalker'}) + luke.name = ""; + await luke.delete(); + } catch (err) { + should.not.exist(err); + } + }); + + it("delete() -> should not raise ValidationError error", async function () { + try { + const luke = await TestModel.findOne({name: 'Luke Skywalker'}) + luke.name = "Test Name"; + await luke.delete() + } catch (err) { + should.not.exist(err); + } + }); +}); + +describe("mongoose_delete indexFields options", function () { + + it("all fields must have index: { indexFields: true }", function () { + var TestSchema = new Schema({name: String}, {collection: 'mongoose_delete_test_indexFields'}); + TestSchema.plugin(mongoose_delete, {indexFields: true, deletedAt: true, deletedBy: true}); + var Test0 = mongoose.model('Test0_indexFields', TestSchema); + + expect(Test0.schema.paths.deleted._index).to.be.true; + expect(Test0.schema.paths.deletedAt._index).to.be.true; + expect(Test0.schema.paths.deletedBy._index).to.be.true; + }); + + it("all fields must have index: { indexFields: 'all' }", function () { + var TestSchema = new Schema({name: String}, {collection: 'mongoose_delete_test_indexFields'}); + TestSchema.plugin(mongoose_delete, {indexFields: 'all', deletedAt: true, deletedBy: true}); + var Test0 = mongoose.model('Test1_indexFields', TestSchema); + + expect(Test0.schema.paths.deleted._index).to.be.true; + expect(Test0.schema.paths.deletedAt._index).to.be.true; + expect(Test0.schema.paths.deletedBy._index).to.be.true; + }); + + it("only 'deleted' field must have index: { indexFields: ['deleted'] }", function () { + var TestSchema = new Schema({name: String}, {collection: 'mongoose_delete_test_indexFields'}); + TestSchema.plugin(mongoose_delete, {indexFields: ['deleted'], deletedAt: true, deletedBy: true}); + var Test0 = mongoose.model('Test2_indexFields', TestSchema); + + expect(Test0.schema.paths.deletedAt._index).to.be.false; + expect(Test0.schema.paths.deletedBy._index).to.be.false; + expect(Test0.schema.paths.deleted._index).to.be.true; + }); + + it("only 'deletedAt' and 'deletedBy' fields must have index: { indexFields: ['deletedAt', 'deletedBy'] }", function () { + var TestSchema = new Schema({name: String}, {collection: 'mongoose_delete_test_indexFields'}); + TestSchema.plugin(mongoose_delete, {indexFields: ['deletedAt', 'deletedBy'], deletedAt: true, deletedBy: true}); + var Test0 = mongoose.model('Test3_indexFields', TestSchema); + + expect(Test0.schema.paths.deleted._index).to.be.false; + expect(Test0.schema.paths.deletedAt._index).to.be.true; + expect(Test0.schema.paths.deletedBy._index).to.be.true; + }); +}); + +describe("check usage of $ne operator", function () { + var TestRawSchema = new Schema({name: String, deleted: Boolean}, {collection: 'mongoose_delete_test_ne'}); + var TestRawModel = mongoose.model('TestNeRaw', TestRawSchema); + + var TestSchema = new Schema({name: String}, {collection: 'mongoose_delete_test_ne'}); + TestSchema.plugin(mongoose_delete, {overrideMethods: 'all', use$neOperator: false}); + var TestModel = mongoose.model('Test55', TestSchema); + + before(async function () { + await TestRawModel.create( + [ + {name: 'One'}, + {name: 'Two', deleted: true}, + {name: 'Three', deleted: false} + ]); + }); + + after(async function () { + await mongoose.connection.db.dropCollection("mongoose_delete_test_ne"); + }); + + it("find() -> should return 1 documents", async function () { + try { + const documents = await TestModel.find(); + documents.length.should.equal(1); + } catch (err) { + should.not.exist(err); + } + }); + + it("findDeleted() -> should return 1 documents", async function () { + try { + const documents = await TestModel.findDeleted(); + documents.length.should.equal(1); + } catch (err) { + should.not.exist(err); + } + }); +}); + +describe("aggregate methods: { overrideMethods: ['aggregate'] }", function () { + var TestSchema = new Schema({ name: String }, { collection: 'mongoose_delete_test_aggregate' }); + TestSchema.plugin(mongoose_delete, { overrideMethods: ['aggregate'] }); + + var TestModel = mongoose.model('Test5_Aggregate', TestSchema); + + beforeEach(async function () { + await TestModel.create( + [ + { name: 'Obi-Wan Kenobi', deleted: true }, + { name: 'Darth Vader' }, + { name: 'Luke Skywalker', deleted: true } + ]); + }); + + afterEach(async function () { + await mongoose.connection.db.dropCollection("mongoose_delete_test_aggregate"); + }); + + it("aggregate([{$project : {name : 1} }]) -> should return 1 document", async function () { + try { + const documents = await TestModel.aggregate([{$project : { name : 1 }}]); + documents.length.should.equal(1); + } catch (err) { + should.not.exist(err); + } + }); + + it("aggregate([{$project : {name : 1} }]) -> should return 1 document (pipeline)", async function () { + try { + const documents = await TestModel + .aggregate() + .project({ name : 1 }); + + documents.length.should.equal(1); + } catch (err) { + should.not.exist(err); + } + }); + + it("aggregateDeleted([{$project : {name : 1} }]) -> should return deleted documents", async function () { + try { + const documents = await TestModel.aggregateDeleted([ + { + $project : { name : 1 } + } + ]); + + documents.length.should.equal(2); + } catch (err) { + should.not.exist(err); + } + }); + + it("aggregateDeleted([{$project : {name : 1} }]) -> should return deleted documents (pipeline)", async function () { + try { + const documents = await TestModel + .aggregateDeleted() + .project({ name : 1 }); + + documents.length.should.equal(2); + } catch (err) { + should.not.exist(err); + } + }); + + it("aggregateWithDeleted([{$project : {name : 1} }]) -> should return deleted documents", async function () { + try { + const documents = await TestModel.aggregateWithDeleted([ + { + $project : { name : 1 } + } + ]); + + documents.length.should.equal(3); + } catch (err) { + should.not.exist(err); + } + }); + + it("aggregateWithDeleted([{$project : {name : 1} }]) -> should return deleted documents (pipeline)", async function () { + try { + const documents = await TestModel + .aggregateWithDeleted() + .project({ name : 1 }); + + documents.length.should.equal(3); + } catch (err) { + should.not.exist(err); + } + }); +}); + +describe("mongoose_delete find method overridden with populate", function () { + var TestPopulateSchema1 = new Schema( + { name: String }, + { collection: 'TestPopulate1' } + ); + TestPopulateSchema1.plugin(mongoose_delete, { overrideMethods: 'all' }); + var TestPopulate1 = mongoose.model('TestPopulate1', TestPopulateSchema1); + + var TestPopulateSchema2 = new Schema( + { + name: String, + test: { type: mongoose.Types.ObjectId, ref: 'TestPopulate1' } + }, + { collection: 'TestPopulate2' } + ); + TestPopulateSchema2.plugin(mongoose_delete, { overrideMethods: 'all' }); + var TestPopulate2 = mongoose.model('TestPopulate2', TestPopulateSchema2); + + beforeEach(async function () { + await TestPopulate1.create( + [ + { name: 'Obi-Wan Kenobi', _id: getNewObjectId("53da93b16b4a6670076b16b1"), deleted: true }, + { name: 'Darth Vader', _id: getNewObjectId("53da93b16b4a6670076b16b2") }, + { name: 'Luke Skywalker', _id: getNewObjectId("53da93b16b4a6670076b16b3"), deleted: true } + ] + ); + await TestPopulate2.create( + [ + { name: 'Student 1', test: getNewObjectId("53da93b16b4a6670076b16b1") }, + { name: 'Student 2', test: getNewObjectId("53da93b16b4a6670076b16b2") }, + { name: 'Student 3', test: getNewObjectId("53da93b16b4a6670076b16b3"), deleted: true } + ] + ) + }); + + afterEach(async function () { + await mongoose.connection.db.dropCollection("TestPopulate1"); + await mongoose.connection.db.dropCollection("TestPopulate2"); + }); + + it("populate() -> should not return deleted sub-document", async function () { + try { + const document = await TestPopulate2 + .findOne({ name: 'Student 1' }) + .populate({ path: 'test' }); + + expect(document.test).to.be.null; + } catch (err) { + should.not.exist(err); + } + }); + + it("populate() -> should return the deleted sub-document using { withDeleted: true }", async function () { + try { + const document = await TestPopulate2 + .findOne({ name: 'Student 1' }) + .populate({ path: 'test', options: { withDeleted: true } }); + + expect(document.test).not.to.be.null; + document.test.deleted.should.equal(true); + } catch (err) { + should.not.exist(err); + } + }); + + it("populate() -> should not return deleted documents and sub-documents", async function () { + try { + const documents = await TestPopulate2 + .find({ }) + .populate({ path: 'test' }) + .exec(); + + var student1 = documents.findIndex(function(i) { return i.name === "Student 1" }); + var student2 = documents.findIndex(function(i) { return i.name === "Student 2" }); + + documents.length.should.equal(2) + expect(documents[student1].test).to.be.null; + expect(documents[student2].test).not.to.be.null; + } catch (err) { + should.not.exist(err); + } + }); + + it("populate() -> should return deleted documents and sub-documents", async function () { + try { + const documents = await TestPopulate2 + .findWithDeleted() + .populate({ path: 'test', options: { withDeleted: true } }) + .exec(); + + documents.length.should.equal(3); + + var student1 = documents.findIndex(function(i) { return i.name === "Student 1" }); + var student2 = documents.findIndex(function(i) { return i.name === "Student 2" }); + var student3 = documents.findIndex(function(i) { return i.name === "Student 3" }); + + expect(documents[student1].test).not.to.be.null; + expect(documents[student2].test).not.to.be.null; + expect(documents[student3].test).not.to.be.null; + } catch (err) { + should.not.exist(err); + } + }); +});