class Form(forms.Form): field_1 = forms.IntegerField(prompt="Enter field_1:") field_2 = forms.IntegerField(prompt="Enter field_2:") def on_init(self, update, cleaned_data): self.cleaned_data["chat_id"] = update.message.chat.chat_id def on_complete(self, update: Update, cleaned_data: dict): update.telegram_object.reply("Thank you!") def on_cancel(self, update: Update, cleaned_data: dict): update.telegram_object.reply("It is sad")
def test_to_python__invalid_value(self): field = forms.IntegerField(name="int_field") with self.assertRaises(ValidationError) as raised: field.to_python("4b") error = raised.exception self.assertEqual(error.code, "invalid")
class TestForm(forms.Form): on_save_message = "Fields saved successfully." first_field = forms.IntegerField(prompt="Enter first field:") second_field = SecondField(prompt="Enter second field:") def on_init(self, update, cleaned_data): self.cleaned_data["chat_id"] = update.message.chat.chat_id
def test_min_value__valid_value(self): update = Mock() cleaned_data = {} field = forms.IntegerField(name="int_field", min_value=0) field.clean('10', update, cleaned_data) self.assertEqual(field.value, 10) self.assertEqual(field.is_bound, True)
def test_clean__valid(self): update = Mock() cleaned_data = {} field = forms.IntegerField(name="int_field") field.clean("42", update, cleaned_data) self.assertEqual(field.value, 42) self.assertEqual(field.is_bound, True)
def test_max_value__valid_value(self): update = Mock() form = Mock() cleaned_data = {} field = forms.IntegerField(name="int_field", max_value=10) field.clean("10", update, cleaned_data, form) self.assertEqual(field.value, 10) self.assertEqual(field.is_bound, True)
def test_min_value__invalid_value(self): update = Mock() cleaned_data = {} field = forms.IntegerField(name="int_field", min_value=0) field.clean('-1', update, cleaned_data) self.assertEqual(field.value, -1) self.assertEqual(field.is_bound, False) self.assertEqual(field.errors[0].code, 'min_value')
def test_max_value__invalid_value(self): update = Mock() form = Mock() cleaned_data = {} field = forms.IntegerField(name="int_field", max_value=10) field.clean("11", update, cleaned_data, form) self.assertEqual(field.value, 11) self.assertEqual(field.is_bound, False) self.assertEqual(field.errors[0].code, "max_value")