it('Badly formatted lines have no impact on the treated data', function () { var now = new Date() , rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + 'garbage\n' + model.serialize({ _id: "3", nested: { today: now } }) , treatedData = d.persistence.treatRawData(rawData).data ; treatedData.sort(function (a, b) { return a._id - b._id; }); treatedData.length.should.equal(2); _.isEqual(treatedData[0], { _id: "1", a: 2, ages: [1, 5, 12] }).should.equal(true); _.isEqual(treatedData[1], { _id: "3", nested: { today: now } }).should.equal(true); });
function getCandidateClusters(failedClusterName) { return _.filter(clusters, function(cluster) { return !_.isEqual(cluster.name, failedClusterName); }); }
test("pick", function() { var result; result = _.pick({a:1, b:2, c:3}, 'a', 'c'); ok(_.isEqual(result, {a:1, c:3}), 'can restrict properties to those named'); result = _.pick({a:1, b:2, c:3}, ['b', 'c']); ok(_.isEqual(result, {b:2, c:3}), 'can restrict properties to those named in an array'); result = _.pick({a:1, b:2, c:3}, ['a'], 'b'); ok(_.isEqual(result, {a:1, b:2}), 'can restrict properties to those named in mixed args'); var Obj = function(){}; Obj.prototype = {a: 1, b: 2, c: 3}; ok(_.isEqual(_.pick(new Obj, 'a', 'c'), {a:1, c: 3}), 'include prototype props'); });
function findManualSwitchCluster(clusterName) { var name = f.existy(clusterName) ? clusterName : DEFAULT_CLUSTER_NAME; return _.find(clusters, function(cluster) { return _.isEqual(cluster.name, name); }); }
test("functions", function() { var obj = {a : 'dash', b : _.map, c : (/yo/), d : _.reduce}; ok(_.isEqual(['b', 'd'], _.functions(obj)), 'can grab the function names of any passed-in object'); var Animal = function(){}; Animal.prototype.run = function(){}; equal(_.functions(new Animal).join(''), 'run', 'also looks up functions on the prototype'); });
function waitUntilStopped(port) { return waitUntil( function(resp) { expect(resp).toEqual(0); }, function(resp) { return _.isEqual(resp, 0) }, getServerStatus(port) ); }
function toBeAnyOf(expecteds, actual, done) { for (var i = 0, l = expecteds.length; i < l; i++) { if (_.isEqual(actual.message, expecteds[i])) return; } done(new Error('[' + actual.message + '] is not any of: [' + expecteds + ']')); }
it('If a doc contains $$deleted: true, that means we need to remove it from the data', function () { var now = new Date() , rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + model.serialize({ _id: "2", hello: 'world' }) + '\n' + model.serialize({ _id: "1", $$deleted: true }) + '\n' + model.serialize({ _id: "3", today: now }) , treatedData = d.persistence.treatRawData(rawData).data ; treatedData.sort(function (a, b) { return a._id - b._id; }); treatedData.length.should.equal(2); _.isEqual(treatedData[0], { _id: "2", hello: 'world' }).should.equal(true); _.isEqual(treatedData[1], { _id: "3", today: now }).should.equal(true); });
test("invert", function() { var obj = {first: 'Moe', second: 'Larry', third: 'Curly'}; equal(_.keys(_.invert(obj)).join(' '), 'Moe Larry Curly', 'can invert an object'); ok(_.isEqual(_.invert(_.invert(obj)), obj), 'two inverts gets you back where you started'); var obj = {length: 3}; ok(_.invert(obj)['3'] == 'length', 'can invert an object with "length"') });
it('If two lines concern the same doc (= same _id), the last one is the good version', function () { var now = new Date() , rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + model.serialize({ _id: "2", hello: 'world' }) + '\n' + model.serialize({ _id: "1", nested: { today: now } }) , treatedData = d.persistence.treatRawData(rawData).data ; treatedData.sort(function (a, b) { return a._id - b._id; }); treatedData.length.should.equal(2); _.isEqual(treatedData[0], { _id: "1", nested: { today: now } }).should.equal(true); _.isEqual(treatedData[1], { _id: "2", hello: 'world' }).should.equal(true); });
ok(!_.isEqual(new Date(2009, 11, 13), { getTime: function(){ return 12606876e5; } }), "Date objects and objects with a `getTime` method are not equal");
test('object', function() { var result = _.object(['moe', 'larry', 'curly'], [30, 40, 50]); var shouldBe = {moe: 30, larry: 40, curly: 50}; ok(_.isEqual(result, shouldBe), 'two arrays zipped together into an object'); result = _.object([['one', 1], ['two', 2], ['three', 3]]); shouldBe = {one: 1, two: 2, three: 3}; ok(_.isEqual(result, shouldBe), 'an array of pairs zipped together into an object'); var stooges = {moe: 30, larry: 40, curly: 50}; ok(_.isEqual(_.object(_.pairs(stooges)), stooges), 'an object converted to pairs and back to an object'); ok(_.isEqual(_.object(null), {}), 'handles nulls'); });
test("omit", function() { var result; result = _.omit({a:1, b:2, c:3}, 'b'); ok(_.isEqual(result, {a:1, c:3}), 'can omit a single named property'); result = _.omit({a:1, b:2, c:3}, 'a', 'c'); ok(_.isEqual(result, {b:2}), 'can omit several named properties'); result = _.omit({a:1, b:2, c:3}, ['b', 'c']); ok(_.isEqual(result, {a:1}), 'can omit properties named in an array'); var Obj = function(){}; Obj.prototype = {a: 1, b: 2, c: 3}; ok(_.isEqual(_.omit(new Obj, 'b'), {a:1, c: 3}), 'include prototype props'); });
it('Well formatted lines that have no _id are not included in the data', function () { var now = new Date() , rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + model.serialize({ _id: "2", hello: 'world' }) + '\n' + model.serialize({ nested: { today: now } }) , treatedData = d.persistence.treatRawData(rawData).data ; treatedData.sort(function (a, b) { return a._id - b._id; }); treatedData.length.should.equal(2); _.isEqual(treatedData[0], { _id: "1", a: 2, ages: [1, 5, 12] }).should.equal(true); _.isEqual(treatedData[1], { _id: "2", hello: 'world' }).should.equal(true); });
it('If a doc contains $$deleted: true, no error is thrown if the doc wasnt in the list before', function () { var now = new Date() , rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + model.serialize({ _id: "2", $$deleted: true }) + '\n' + model.serialize({ _id: "3", today: now }) , treatedData = d.persistence.treatRawData(rawData).data ; treatedData.sort(function (a, b) { return a._id - b._id; }); treatedData.length.should.equal(2); _.isEqual(treatedData[0], { _id: "1", a: 2, ages: [1, 5, 12] }).should.equal(true); _.isEqual(treatedData[1], { _id: "3", today: now }).should.equal(true); });