def test_form(self): """Test for modifying existing entity with ModelForm.""" # first create a new entity f = TestModelForm() params = {"number": "12", "data_field": "data string", "is_active": "False", "string_list_field": "list"} self.assertEqual(f.validate(params), True) f.save() self.assertEqual(TestModel.all().count(), 1) entity = TestModel.all().get() self.assertEqual(entity.number, 12) # modify with TestModelForm f = TestModelForm(instance=entity) params = {"number": "13", "data_field": "modified data", "is_active": "True", "string_list_field": "line 1\nline 2"} self.assertEqual(f.validate(params), True) f.save() # check values self.assertEqual(TestModel.all().count(), 1) entity = TestModel.all().get() self.assertEqual(entity.number, 13) self.assertEqual(entity.data_field, "modified data") self.assertEqual(entity.is_active, True) self.assertEqual(entity.string_list_field, ["line 1", "line 2"]) """Form validation test with ModelForm.""" os.environ['REQUEST_METHOD'] = 'POST' local.request = Request(get_env()) f = TestModelForm() params = {"number": "12"} # In your view, you can validate the form data with: # f.validate(request.form) # or with(If you have FileField): # f.validate(request.form, request.files) self.assertEqual(f.validate(params), False) f.reset() params = {"number": "12", "data_field": "data string longer than 20 characters", "is_active": "False", "string_list_field": "test"} self.assertEqual(f.validate(params), False) f.reset() params = {"number": "12", "data_field": "data string", "is_active": "False", "string_list_field": ""} self.assertEqual(f.validate(params), False) # create a new entity f.reset() params = {"number": "12", "data_field": "data string", "is_active": "False", "string_list_field": "list"} self.assertEqual(f.validate(params), True) f.save() self.assertEqual(TestModel.all().count(), 2)
def test_form(self): """Form validation test with context_validate.""" os.environ['REQUEST_METHOD'] = 'POST' local.request = Request(get_env()) f = TestForm() f.model_field.set_query(TestModel2.all().filter('is_active =', True)) params = {'username': '******'} self.assertEqual(f.validate(params), False) params = { 'username': '******', 'password': '******', 'password_again': 'fugafuga', 'string_list_field': 'hoge', 'model_field': str(TestModel2.all().filter('is_active =', True).get().key()) } result = f.validate(params) self.assertEqual(result, True) params['password_again'] = 'moge' result = f.validate(params) self.assertEqual(result, False)
def setUp(self): os.environ['REQUEST_METHOD'] = 'POST' local.request = Request(get_env())