williamkapke / mongo-mock

Let's pretend we have a real MongoDB
MIT License
240 stars 74 forks source link

Fix for removal of properties in nested object (Issue #55) #72

Closed pludov closed 6 years ago

pludov commented 6 years ago

Merge is unable to remove properties within nested object.

https://github.com/williamkapke/mongo-mock/issues/55

angelo-hub commented 6 years ago

So I'm going to run a bit more testing today, replacing assign with merge seems to also remove the need to have a RestoreObjectIDs() function in certain cases. Going to look into it, and make sure it's working as intended.

angelo-hub commented 6 years ago

@pludov Can you add the following tests to replace the test here: https://github.com/williamkapke/mongo-mock/blob/085f83bf0333149e856f9a0d130bb2dedbc40fa7/test/mock.test.js#L251-L264

    it('should update one (updateOne)', function (done) {
      //query, data, options, callback
      collection.updateOne({test:123}, { $set: { foo: { bar: "buzz", fang: "dang" } } }, function (err, opResult) {
        if(err) return done(err);
        opResult.result.n.should.equal(1);

        collection.findOne({test:123}, function (err, doc) {
          if(err) return done(err);
          (!!doc).should.be.true;
          doc.should.have.property("foo", { bar: "buzz", fang: "dang" });
          done();
        });
      });
    });

    it('should update one (updateOne) with shallow overwrite', function (done) {
      //query, data, options, callback
      collection.updateOne({ test: 123 }, { $set: { foo: { newValue: "bar" } } }, function (err, opResult) {
        if (err) return done(err);
        opResult.result.n.should.equal(1);

        collection.findOne({ test: 123 }, function (err, doc) {
          if (err) return done(err);
          (!!doc).should.be.true;
          doc.should.have.property("foo", { newValue: "bar" });
          done();
        });
      });
    });
angelo-hub commented 6 years ago

I'm closing this because I opened a PR with your changes adding a unit test