Skip to content

Instantly share code, notes, and snippets.

@juanandresnyc
Last active November 11, 2015 19:53
Show Gist options
  • Save juanandresnyc/10eb77ce8fcdcfda127e to your computer and use it in GitHub Desktop.
Save juanandresnyc/10eb77ce8fcdcfda127e to your computer and use it in GitHub Desktop.
Tests technique to check for uniqueness works
require('./../lib/env');
Product.get('prototype-movie-2151', function(err, product) {
if (err) throw err;
console.log(testUniqueness(product, 1000, 1000, 300) ? 'passed' : 'failed');
});
function populateProductWithFakeUniqueVariants(product, count) {
product.variants = {};
for (var i = 0; i < count; i++) {
var variantId = product.newVariantId();
if (variantId === -1) {
throw new Error('no unique id returned');
}
product.variants[variantId] = true;
}
}
function testUniqueness(product, nTests, nUniqueTests, sampleSize) {
// Tests
for (var i = 0; i < nTests; i++) {
populateProductWithFakeUniqueVariants(product, sampleSize);
// Testing for new entries j times for each sample
for (var j = 0; j < nUniqueTests; j++) {
var variantId = product.newVariantId();
if (variantId === -1 || product.variants[variantId]) return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment