Last active
November 11, 2015 19:53
-
-
Save juanandresnyc/10eb77ce8fcdcfda127e to your computer and use it in GitHub Desktop.
Tests technique to check for uniqueness works
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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