'Test FeatureState\'s _choose_variant function.'

def test_choose_variant(self):

    no_variants = {}
    three_variants = {'remove_vote_counters': 5, 'control_1': 10, 'control_2': 5}
    three_variants_more = {'remove_vote_counters': 15.6, 'control_1': 10, 'control_2': 20}
    counters = collections.defaultdict(collections.Counter)
    for bucket in xrange(FeatureState.NUM_BUCKETS):
        variant = FeatureState._choose_variant(bucket, no_variants)
        if variant:
            counters['no_variants'][variant] += 1
        self.assertEqual(variant, FeatureState._choose_variant(bucket, no_variants))
        variant = FeatureState._choose_variant(bucket, three_variants)
        if variant:
            counters['three_variants'][variant] += 1
        self.assertEqual(variant, FeatureState._choose_variant(bucket, three_variants))
        previous_variant = variant
        variant = FeatureState._choose_variant(bucket, three_variants_more)
        if variant:
            counters['three_variants_more'][variant] += 1
        self.assertEqual(variant, FeatureState._choose_variant(bucket, three_variants_more))
        if previous_variant:
            self.assertEqual(variant, previous_variant)
    for (variant, percentage) in FeatureState.DEFAULT_CONTROL_GROUPS.items():
        count = counters['no_variants'][variant]
        scaled_percentage = (float(count) / (FeatureState.NUM_BUCKETS / 100))
        self.assertEqual(scaled_percentage, percentage)
    for (variant, percentage) in three_variants.items():
        count = counters['three_variants'][variant]
        scaled_percentage = (float(count) / (FeatureState.NUM_BUCKETS / 100))
        self.assertEqual(scaled_percentage, percentage)
    for (variant, percentage) in three_variants_more.items():
        count = counters['three_variants_more'][variant]
        scaled_percentage = (float(count) / (FeatureState.NUM_BUCKETS / 100))
        self.assertEqual(scaled_percentage, percentage)
    fifty_fifty = {'control_1': 50, 'control_2': 50}
    almost_fifty_fifty = {'control_1': 49, 'control_2': 51}
    for bucket in xrange(FeatureState.NUM_BUCKETS):
        variant = FeatureState._choose_variant(bucket, fifty_fifty)
        counters['fifty_fifty'][variant] += 1
        variant = FeatureState._choose_variant(bucket, almost_fifty_fifty)
        counters['almost_fifty_fifty'][variant] += 1
    count = counters['fifty_fifty']['control_1']
    scaled_percentage = (float(count) / (FeatureState.NUM_BUCKETS / 100))
    self.assertEqual(scaled_percentage, 50)
    count = counters['fifty_fifty']['control_2']
    scaled_percentage = (float(count) / (FeatureState.NUM_BUCKETS / 100))
    self.assertEqual(scaled_percentage, 50)
    count = counters['almost_fifty_fifty']['control_1']
    scaled_percentage = (float(count) / (FeatureState.NUM_BUCKETS / 100))
    self.assertEqual(scaled_percentage, 49)
    count = counters['almost_fifty_fifty']['control_2']
    scaled_percentage = (float(count) / (FeatureState.NUM_BUCKETS / 100))
    self.assertEqual(scaled_percentage, 50)
