def any_field_choices(field, **kwargs): """ Selection from field.choices >>> CHOICES = [('YNG', 'Child'), ('OLD', 'Parent')] >>> result = any_field(models.CharField(max_length=3, choices=CHOICES)) >>> result in ['YNG', 'OLD'] True """ if getattr(field, 'choices', False): return Value(random.choice(list(valid_choices(field.choices))))
def choice_field_data(field, **kwargs): """ Return random value for ChoiceField >>> CHOICES = [('YNG', 'Child'), ('OLD', 'Parent')] >>> result = any_form_field(forms.ChoiceField(choices=CHOICES)) >>> type(result) <type 'str'> >>> result in ['YNG', 'OLD'] True >>> typed_result = any_form_field(forms.TypedChoiceField(choices=CHOICES)) >>> typed_result in ['YNG', 'OLD'] True """ if field.choices: return str(random.choice(list(valid_choices(field.choices)))) return 'None'
def multiple_choice_field_data(field, **kwargs): """ Return random value for MultipleChoiceField >>> CHOICES = [('YNG', 'Child'), ('MIDDLE', 'Parent') ,('OLD', 'GrandParent')] >>> result = any_form_field(forms.MultipleChoiceField(choices=CHOICES)) >>> type(result) <type 'str'> """ if field.choices: l = list(valid_choices(field.choices)) random.shuffle(l) choices = [] count = xunit.any_int(min_value=1, max_value=len(field.choices)) for i in xrange(0, count): choices.append(l[i]) return ' '.join(choices) return 'None'
def field_choices_attibute(field, **kwargs): """ Selection from field.choices """ if hasattr(field.widget, 'choices'): return Value(random.choice(list(valid_choices(field.widget.choices))))