Skip to content

Commit

Permalink
Update the random number package so seeding can be implemented withou…
Browse files Browse the repository at this point in the history
…t seeding numbers forever. Implement seeding on the following functions:

* faker.random.number
* faker.random.array_element
* faker.commerce.productName

Added tests for seeding data and tests for array_element
  • Loading branch information
robscotts4rb committed Mar 13, 2015
1 parent 4f5cb5c commit d1c00c4
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 304 deletions.
26 changes: 16 additions & 10 deletions lib/commerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ var commerce = {
return categories[0];
},

productName: function() {
return faker.commerce.productAdjective() + " " +
faker.commerce.productMaterial() + " " +
faker.commerce.product();
productName: function(options) {
options = options || {};

if (typeof options.seed === 'undefined') {
options.seed = null;
}

return faker.commerce.productAdjective(options) + " " +
faker.commerce.productMaterial(options) + " " +
faker.commerce.product(options);
},

price: function(min, max, dec, symbol) {
Expand Down Expand Up @@ -62,16 +68,16 @@ var commerce = {
return [commaSeparated, categories[categories.length - 1]].join(separator);
},

productAdjective: function() {
return faker.random.array_element(faker.definitions.commerce.product_name.adjective);
productAdjective: function(options) {
return faker.random.array_element(faker.definitions.commerce.product_name.adjective, options);
},

productMaterial: function() {
return faker.random.array_element(faker.definitions.commerce.product_name.material);
productMaterial: function(options) {
return faker.random.array_element(faker.definitions.commerce.product_name.material, options);
},

product: function() {
return faker.random.array_element(faker.definitions.commerce.product_name.product);
product: function(options) {
return faker.random.array_element(faker.definitions.commerce.product_name.product, options);
}

};
Expand Down
29 changes: 23 additions & 6 deletions lib/random.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var mersenne = require('../vendor/mersenne');
var seedrandom = require('seedrandom');
var faker = require('../index');

var random = {
Expand All @@ -24,23 +24,40 @@ var random = {
options.precision = 1;
}

if (typeof options.seed === 'undefined') {
options.seed = null;
}

// Make the range inclusive of the max value
var max = options.max;
if (max > 0) {
max += options.precision;
}

}

rng = seedrandom(options.seed);

max = max / options.precision;
var min = options.min/options.precision;

var randomNumber = options.precision * Math.floor(
mersenne.rand(max / options.precision, options.min / options.precision));
Math.floor(rng() * (max - min) + min)
);

return randomNumber;

},

// takes an array and returns a random element of the array
array_element: function (array) {
array_element: function (array, options) {
array = array || ["a", "b", "c"];
var r = faker.random.number({ max: array.length - 1 });

options = options || {};

if (typeof options.seed === 'undefined') {
options.seed = null;
}

var r = faker.random.number({ max: array.length - 1, seed: options.seed });
return array[r];
},

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@
"sinon": "1.4.2",
"vinyl-transform": "0.0.1"
},
"main": "index.js"
"main": "index.js",
"dependencies": {
"seedrandom": "^2.3.11"
}
}
25 changes: 25 additions & 0 deletions test/commerce.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,31 @@ describe("commerce.js", function() {
faker.commerce.productMaterial.restore();
faker.commerce.product.restore();
});

it("should return a seed product name when passed a seed", function() {
sinon.spy(faker.random, 'array_element');
sinon.spy(faker.commerce, 'productAdjective');
sinon.spy(faker.commerce, 'productMaterial');
sinon.spy(faker.commerce, 'product');

var seed = 1234;
var name = faker.commerce.productName({seed: seed });

assert.ok(name.split(' ').length >= 3);

assert.ok(faker.random.array_element.calledThrice);
assert.ok(faker.commerce.productAdjective.calledOnce);
assert.ok(faker.commerce.productMaterial.calledOnce);
assert.ok(faker.commerce.product.calledOnce);

faker.random.array_element.restore();
faker.commerce.productAdjective.restore();
faker.commerce.productMaterial.restore();
faker.commerce.product.restore();

// Test the seed has worked after checking the function call amounts, otherwise it will skew the numbers
assert.strictEqual(name, faker.commerce.productName({seed: seed }));
})
});

describe("price(min, max, dec, symbol", function() {
Expand Down
37 changes: 37 additions & 0 deletions test/random.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,45 @@ describe("random.js", function () {
assert.equal(opts.min, min);
assert.equal(opts.max, max);
});

it("should return a seeded random number if passed the seed parameter", function() {
var max = 100;
var seed = 1234;

assert.ok(faker.random.number({ max: max, seed: seed }) <= max);
assert.strictEqual(faker.random.number({ max: max, seed: seed }), faker.random.number({ max: max, seed: seed }));
assert.notStrictEqual(faker.random.number({ max: max }), faker.random.number({ max: max }));
});
});

describe('array_element()', function() {
it("should return a default array if non passed in", function() {
var random = faker.random.array_element();
assert.ok(random);
assert.ok(_.contains(["a", "b", "c"], random));
});

it("should return a random array element", function() {
var array = [1, 2, 3, 4, 5];

var random = faker.random.array_element(array);

assert.ok(random);
assert.ok(_.contains(array, random));
});

it("should return a seeded array element", function() {
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var seed = 1234;

var random = faker.random.array_element(array, {seed: seed});

assert.ok(random);
assert.strictEqual(random, faker.random.array_element(array, {seed: seed}));
assert.ok(_.contains(array, random));
});
});

describe('UUID', function() {
it('should generate a valid UUID', function() {
var UUID = faker.random.uuid();
Expand Down
Loading

0 comments on commit d1c00c4

Please sign in to comment.