def test_uniqueness(self): """ Seeds should be different from one another. """ seeds = [ rank.seed_for(models.Participant(pk=33), '22'), rank.seed_for(models.Participant(pk=22), '33'), rank.seed_for(models.Participant(pk=22), '22'), ] self.assertEqual(len(seeds), len(set(seeds)))
def test_weight_subtraction(self): """ Test the definition of affiliation-weighted randomness. Specifically, we take a random float from a particular seed, and subtract a known offset according to the participant's affiliation. """ # MIT undergraduates get an advantage: their number is more likely to be lower mit_undergrad = models.Participant(pk=12, affiliation='MU') seed = rank.seed_for(mit_undergrad, 'trip-142') random.seed(seed) self.assertEqual( random.random() - 0.3, rank.affiliation_weighted_rand(mit_undergrad, 'trip-142')) # Non-affiliates are just a random number non_affiliate = models.Participant(pk=24, affiliation='NA') seed = rank.seed_for(non_affiliate, 'trip-142') random.seed(seed) self.assertEqual( random.random(), rank.affiliation_weighted_rand(non_affiliate, 'trip-142'))
def test_weight_subtraction(self): """ Test the definition of affiliation-weighted randomness. Specifically, we take a random float from a particular seed, and subtract a known offset according to the participant's affiliation. """ # MIT undergraduates get an advantage: their number is more likely to be lower mit_undergrad = models.Participant(pk=12, affiliation='MU') seed = rank.seed_for(mit_undergrad, 'trip-142') random.seed(seed) self.assertEqual( random.random() - 0.3, rank.affiliation_weighted_rand(mit_undergrad, 'trip-142'), ) # Non-affiliates are just a random number non_affiliate = models.Participant(pk=24, affiliation='NA') seed = rank.seed_for(non_affiliate, 'trip-142') random.seed(seed) self.assertEqual( random.random(), rank.affiliation_weighted_rand(non_affiliate, 'trip-142') )
def test_seed_contains_secret(self): """ The seed should contain the secret that participant's don't know. """ seed = rank.seed_for(models.Participant(pk=33), 'some extra seed') self.assertIn(settings.PRNG_SEED_SECRET, seed)
def test_participant_must_be_saved_to_db(self): """ We can't come up with a fair seed for a participant that lacks a pk. """ with self.assertRaises(ValueError): rank.seed_for(models.Participant(name='Not Saved'), lottery_key='hi')