def post_process_instance(self, instance, commit): groups = instance.user_groups.all() valid_members = User.objects.exclude(usergroup__in=groups, ) generators.MultipleInstanceGenerator( AutoFixture( ProjectMembership, field_values={ 'project': instance, 'member': generators.InstanceSelector(valid_members), }, ), min_count=1, max_count=2, ).generate() region_generator = generators.WeightedGenerator([ ( generators.InstanceSelector( Region, limit_choices_to={'public': True}, ), 4, ), ( generators.InstanceGenerator( RegionAutoFixture(Region, field_values={'public': False})), 2, ), ]) regions = [ region_generator.generate() for _ in range(random.randint(1, 3)) ] instance.regions.set(list(regions))
def test_simple_weights(self): results = {"Red": 0, "Blue": 0} choices = [(generators.StaticGenerator("Red"), 50), (generators.StaticGenerator("Blue"), 50)] generate = generators.WeightedGenerator(choices) runs = 10000 for i in range(runs): results[generate()] += 1 MARGIN = 0.025 self.assertTrue( 0.5 - MARGIN < truediv(results["Red"], runs) < 0.5 + MARGIN) self.assertTrue( 0.5 - MARGIN < truediv(results["Blue"], runs) < 0.5 + MARGIN)
def test_complex_weights(self): results = {"frosh": 0, "soph": 0, "jr": 0, "sr": 0} choices = [(generators.StaticGenerator("frosh"), 35), (generators.StaticGenerator("soph"), 20), (generators.StaticGenerator("jr"), 30), (generators.StaticGenerator("sr"), 15)] generate = generators.WeightedGenerator(choices) runs = 10000 for i in range(runs): results[generate()] += 1 MARGIN = 0.025 self.assertTrue( 0.35 - MARGIN < truediv(results["frosh"], runs) < 0.35 + MARGIN, results["frosh"] / 1.0 * runs) self.assertTrue( 0.20 - MARGIN < truediv(results["soph"], runs) < 0.20 + MARGIN) self.assertTrue( 0.30 - MARGIN < truediv(results["jr"], runs) < 0.30 + MARGIN) self.assertTrue( 0.15 - MARGIN < truediv(results["sr"], runs) < 0.15 + MARGIN)