def setUp(self): self.context = testing.DummyModel() self.request = testing.DummyRequest() self.request.api = get_template_api(self.context, self.request) self.controller = BaseController(self.context,self.request) # a simple schema to test class Schema(MappingSchema): field = SchemaNode(String()) self.controller.Schema = Schema # stub out the handle_submit method with mock self.controller.handle_submit = Mock()
class TestBaseController(TestCase): def setUp(self): self.context = testing.DummyModel() self.request = testing.DummyRequest() self.request.api = get_template_api(self.context, self.request) self.controller = BaseController(self.context,self.request) # a simple schema to test class Schema(MappingSchema): field = SchemaNode(String()) self.controller.Schema = Schema # stub out the handle_submit method with mock self.controller.handle_submit = Mock() def test_api(self): self.assertTrue(self.controller.api is self.request.api) def test_data(self): self.assertEqual( self.controller.data, dict(api=self.request.api, actions=()) ) def test_call_get(self): result = self.controller() self.assertTrue(result is self.controller.data) self.assertTrue(result['form'].startswith('<form id="deform"')) self.assertFalse(self.controller.handle_submit.called) def test_call_cancel(self): self.request.POST['cancel']='cancel' result = self.controller() self.assertTrue(isinstance(result,HTTPFound)) self.assertEqual(result.location,'http://example.com/') self.assertFalse(self.controller.handle_submit.called) def test_call_save_validation_failed(self): self.request.POST['save']='save' result = self.controller() self.assertTrue(result is self.controller.data) self.assertTrue(result['form'].startswith('<form id="deform"')) self.assertTrue('Errors have been highlighted' in result['form']) self.assertFalse(self.controller.handle_submit.called) def test_call_save_okay(self): self.request.POST['save']='save' self.request.POST['field']='something' result = self.controller() self.assertTrue( result is self.controller.handle_submit.return_value ) self.assertTrue(self.controller.handle_submit.called) def test_different_buttons(self): with Replacer() as r: self.controller.buttons = ('one','two') Form = Mock() r.replace('opencore.views.forms.Form',Form) self.controller() Form.assert_called_with( C(SchemaNode), buttons = ('one','two'), ) def test_default_form_defaults(self): self.assertEqual(self.controller.form_defaults(),null) def test_form_defaults(self): with Replacer() as r: self.controller.form_defaults = Mock() defaults = self.controller.form_defaults.return_value Form = Mock() Form.return_value = form = Mock() r.replace('opencore.views.forms.Form',Form) self.controller() compare(form.method_calls,[ ('render', (defaults,), {}) ]) def test_call_save_different_buttons(self): # check that we use the last button provided # as the trigger to process the submit self.controller.buttons = ('one','two') self.request.POST['two']='two' result = self.controller() self.assertTrue(result is self.controller.data) self.assertTrue(result['form'].startswith('<form id="deform"')) self.assertTrue('Errors have been highlighted' in result['form']) self.assertFalse(self.controller.handle_submit.called)