From cda515249186e3cc05079ebf86473fff4556ba23 Mon Sep 17 00:00:00 2001 From: David Glasser Date: Mon, 17 Mar 2014 01:14:14 -0700 Subject: [PATCH] Refactor a test to remove magic numbers --- packages/minimongo/minimongo_tests.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/minimongo/minimongo_tests.js b/packages/minimongo/minimongo_tests.js index f159cb37e8..e5f1280212 100644 --- a/packages/minimongo/minimongo_tests.js +++ b/packages/minimongo/minimongo_tests.js @@ -1791,17 +1791,22 @@ Tinytest.add("minimongo - array sort", function (test) { c.insert({up: 3, down: 3, selected: 1, a: {x: 2.5}}); c.insert({up: 4, down: 0, selected: 3, a: {x: 5}}); - test.equal( - _.pluck(c.find({}, {sort: {'a.x': 1}}).fetch(), 'up'), - _.range(5)); + // Test that the the documents in "cursor" contain values with the name + // "field" running from 0 to the max value of that name in the collection. + var testCursorMatchesField = function (cursor, field) { + var fieldValues = []; + c.find().forEach(function (doc) { + if (_.has(doc, field)) + fieldValues.push(doc[field]); + }); + test.equal(_.pluck(cursor.fetch(), field), + _.range(_.max(fieldValues) + 1)); + }; - test.equal( - _.pluck(c.find({}, {sort: {'a.x': -1}}).fetch(), 'down'), - _.range(5)); - - test.equal( - _.pluck(c.find({'a.x': {$gt: 1}}, {sort: {'a.x': 1}}).fetch(), 'selected'), - _.range(4)); + testCursorMatchesField(c.find({}, {sort: {'a.x': 1}}), 'up'); + testCursorMatchesField(c.find({}, {sort: {'a.x': -1}}), 'down'); + testCursorMatchesField(c.find({'a.x': {$gt: 1}}, {sort: {'a.x': 1}}), + 'selected'); }); Tinytest.add("minimongo - sort keys", function (test) {