it('threadId access compatibility', function (done) { assert.isDefined(shareConn.threadId); assert.isTrue(shareConn.threadId !== -1); done(); });
describe('#setVerbose()', function () { it('shows verbose information', function () { var results = scanner().setVerbose(false).parseText('text total 6,000.00 date 2016-08-13') assert.isUndefined(results.verbose) results = scanner().setVerbose(true).parseText('text total 6,000.00 date 2016-08-13') assert.isDefined(results.verbose) }) })
it('Returns an error if the query is not well formed', function (done) { d.insert({ hello: 'world' }, function () { d.find({ $or: { hello: 'world' } }, function (err, docs) { assert.isDefined(err); assert.isUndefined(docs); d.findOne({ $or: { hello: 'world' } }, function (err, doc) { assert.isDefined(err); assert.isUndefined(doc); done(); }); }); }); });
describe('DSB Instance', function() { const DSB = require('../dist/index'); const dsb = new DSB(process.env.DSBUSERNAME, process.env.PASSWORD); it('should be a instanceof DSB', function() { expect(dsb).to.be.an.instanceOf(DSB); }); it('should have the right values', function() { expect(dsb).to.have.property('username', process.env.DSBUSERNAME); expect(dsb).to.have.property('password', process.env.PASSWORD); }); it('should have fetch()', function() { assert.isDefined(dsb.fetch); assert.isFunction(dsb.fetch); }); });
it('Can upsert a document even with modifiers', function (done) { d.update({ bloup: 'blap' }, { $set: { hello: 'world' } }, { upsert: true }, function (err, nr, newDoc) { assert.isNull(err); nr.should.equal(1); newDoc.bloup.should.equal('blap'); newDoc.hello.should.equal('world'); assert.isDefined(newDoc._id); d.find({}, function (err, docs) { docs.length.should.equal(1); Object.keys(docs[0]).length.should.equal(3); docs[0].hello.should.equal('world'); docs[0].bloup.should.equal('blap'); assert.isDefined(docs[0]._id); done(); }); }); });
it('If a unique constraint is violated, no index is changed, including the main one', function (done) { d.ensureIndex({ fieldName: 'a', unique: true }); d.insert({ a: 1, b: 'hello' }, function (err, doc1) { d.insert({ a: 1, b: 'si' }, function (err) { assert.isDefined(err); d.find({}, function (err, docs) { docs.length.should.equal(1); d.getAllData().length.should.equal(1); d.indexes._id.getMatching(doc1._id).length.should.equal(1); d.indexes.a.getMatching(1).length.should.equal(1); d.indexes._id.getMatching(doc1._id)[0].should.equal(d.indexes.a.getMatching(1)[0]); d.indexes.a.getMatching(2).length.should.equal(0); done(); }); }); }); });
it('Cannot insert a doc that has a field beginning with a $ sign', function (done) { d.insert({ $something: 'atest' }, function (err) { assert.isDefined(err); done(); }); });
it("When treating raw data, refuse to proceed if too much data is corrupt, to avoid data loss", function (done) { var corruptTestFilename = 'workspace/corruptTest.db' , fakeData = '{"_id":"one","hello":"world"}\n' + 'Some corrupt data\n' + '{"_id":"two","hello":"earth"}\n' + '{"_id":"three","hello":"you"}\n' , d ; fs.writeFileSync(corruptTestFilename, fakeData, "utf8"); // Default corruptAlertThreshold d = new Datastore({ filename: corruptTestFilename }); d.loadDatabase(function (err) { assert.isDefined(err); assert.isNotNull(err); fs.writeFileSync(corruptTestFilename, fakeData, "utf8"); d = new Datastore({ filename: corruptTestFilename, corruptAlertThreshold: 1 }); d.loadDatabase(function (err) { assert.isNull(err); fs.writeFileSync(corruptTestFilename, fakeData, "utf8"); d = new Datastore({ filename: corruptTestFilename, corruptAlertThreshold: 0 }); d.loadDatabase(function (err) { assert.isDefined(err); assert.isNotNull(err); done(); }); }); }); });
it('If an index constraint is violated by an update, all changes should be rolled back', function (done) { d.ensureIndex({ fieldName: 'a', unique: true }); d.insert({ a: 4 }, function (err, doc1) { d.insert({ a: 5 }, function (err, doc2) { // With this query, candidates are always returned in the order 4, 5, 'abc' so it's always the last one which fails d.update({ a: { $in: [4, 5, 'abc'] } }, { $set: { a: 10 } }, { multi: true }, function (err) { assert.isDefined(err); // Check that no index was modified _.each(d.indexes, function (index) { var docs = index.getAll() , d1 = _.find(docs, function (doc) { return doc._id === doc1._id }) , d2 = _.find(docs, function (doc) { return doc._id === doc2._id }) ; d1.a.should.equal(4); d2.a.should.equal(5); }); done(); }); }); }); });
it('Cant change the _id of a document', function (done) { d.insert({ a: 2 }, function (err, newDoc) { d.update({ a: 2 }, { a: 2, _id: 'nope' }, {}, function (err) { assert.isDefined(err); d.find({}, function (err, docs) { docs.length.should.equal(1); Object.keys(docs[0]).length.should.equal(2); docs[0].a.should.equal(2); docs[0]._id.should.equal(newDoc._id); d.update({ a: 2 }, { $set: { _id: 'nope' } }, {}, function (err) { assert.isDefined(err); d.find({}, function (err, docs) { docs.length.should.equal(1); Object.keys(docs[0]).length.should.equal(2); docs[0].a.should.equal(2); docs[0]._id.should.equal(newDoc._id); done(); }); }); }); }); }); });
it('If a multi update fails on one document, previous updates should be rolled back', function (done) { d.ensureIndex({ fieldName: 'a' }); d.insert({ a: 4 }, function (err, doc1) { d.insert({ a: 5 }, function (err, doc2) { d.insert({ a: 'abc' }, function (err, doc3) { // With this query, candidates are always returned in the order 4, 5, 'abc' so it's always the last one which fails d.update({ a: { $in: [4, 5, 'abc'] } }, { $inc: { a: 10 } }, { multi: true }, function (err) { assert.isDefined(err); // No index modified _.each(d.indexes, function (index) { var docs = index.getAll() , d1 = _.find(docs, function (doc) { return doc._id === doc1._id }) , d2 = _.find(docs, function (doc) { return doc._id === doc2._id }) , d3 = _.find(docs, function (doc) { return doc._id === doc3._id }) ; // All changes rolled back, including those that didn't trigger an error d1.a.should.equal(4); d2.a.should.equal(5); d3.a.should.equal('abc'); }); done(); }); }); }); }); });
// ==== End of 'Upserts' ==== // it('Cannot perform update if the update query is not either registered-modifiers-only or copy-only, or contain badly formatted fields', function (done) { d.insert({ something: 'yup' }, function () { d.update({}, { boom: { $badfield: 5 } }, { multi: false }, function (err) { assert.isDefined(err); d.update({}, { boom: { "bad.field": 5 } }, { multi: false }, function (err) { assert.isDefined(err); d.update({}, { $inc: { test: 5 }, mixed: 'rrr' }, { multi: false }, function (err) { assert.isDefined(err); d.update({}, { $inexistent: { test: 5 } }, { multi: false }, function (err) { assert.isDefined(err); done(); }); }); }); }); }); });
it('Returns an error if the query is not well formed', function (done) { d.insert({ hello: 'world' }, function () { d.remove({ $or: { hello: 'world' } }, {}, function (err, nr, upsert) { assert.isDefined(err); assert.isUndefined(nr); assert.isUndefined(upsert); done(); }); }); });
it('If an error is thrown by a modifier, the database state is not changed', function (done) { d.insert({ hello: 'world' }, function (err, newDoc) { d.update({}, { $inc: { hello: 4 } }, {}, function (err, nr) { assert.isDefined(err); assert.isUndefined(nr); d.find({}, function (err, docs) { assert.deepEqual(docs, [ { _id: newDoc._id, hello: 'world' } ]); done(); }); }); }); });
it('Returns an error if the query is not well formed', function (done) { d.insert({ hello: 'world' }, function () { d.update({ $or: { hello: 'world' } }, { a: 1 }, {}, function (err, nr, upsert) { assert.isDefined(err); assert.isUndefined(nr); assert.isUndefined(upsert); done(); }); }); });