def test_BaseView_form_attribute_must_be_instance_of_FlaskForm(): mocked_form = Mock(FlaskForm) BaseView.Meta.form = mocked_form # init new instance BaseView with form specified BaseView() # in init method BaseView must init form class and save form instance mocked_form.assert_called()
def test_BaseView_render_own_template_method_must_call_render_template_function_with_param_at_default(mock_render): """ Default param is: - template - title - form """ BaseView.Meta.form = Mock(return_value='form') BaseView.Meta.title = 'title' BaseView.Meta.template = 'template' expected_kwargs = { '__title__': 'title', 'form': 'form' } new_view = BaseView() new_view._render_own_template() mock_render.assert_called_once_with('template', **expected_kwargs)
def test_BaseView_template_property_must_return_attribute_from_Meta(self): """ template specified """ expected_template = 'some_template.html' BaseView.Meta.template = expected_template # view with template new_view = BaseView() self.assertEqual(new_view.template, expected_template)
def test_BaseView_default_page_property_must_return_url_for_home_if_Meta_default_page_not_specified(mocked_url): """ When default page is not specified property must return '/' """ BaseView.Meta.default_page = None expected_route = '/' new_view = BaseView() new_view.default_page mocked_url.assert_called_with(expected_route)
from django.conf.urls import url from django.urls import path from app.views import BaseView, sing_up_for_diagnostic, get_holded_time urlpatterns = [ path('', BaseView.as_view(), name='base'), path('sing_up_for_diagnostic/', sing_up_for_diagnostic, name='sing_up_for_diagnostic'), path('get_holded_time/', get_holded_time, name='get_holded_time'), ]
Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) """ from django.conf.urls import url from django.contrib import admin from app.views import OrderListView, GetMessageView, MyMessageView, MessageView, OrderView, CustomerDetailView, BaseView, UserLoginView, UserLogoutView, NewUserView urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^restauracja$', BaseView.as_view(), name='base'), url(r'^login$', UserLoginView.as_view(), name='login'), url(r'^logout$', UserLogoutView.as_view(), name='logout'), url(r'^new_user$', NewUserView.as_view(), name='new'), url(r'^customers_detail/(?P<user_id>(\d)+)', CustomerDetailView.as_view(), name='detale'), url(r'^new_order', OrderView.as_view(), name='order'), url(r'^message', MessageView.as_view(), name='message'), url(r'^my_message', MyMessageView.as_view(), name='my_message'), url(r'^get_message', GetMessageView.as_view(), name='get_message'), url(r'^order_list', OrderListView.as_view(), name='order_list') ]
class BaseViewTests(unittest.TestCase): def setUp(self): self.view = BaseView() def tearDown(self): # reset title BaseView.Meta.title = None # reset form BaseView.Meta.form = None # reset template BaseView.Meta.template = None def test_BaseView_title_property_must_return_None_if_not_specified_in_Meta(self): self.assertIsNone(self.view.Meta.title) self.assertIsNone(self.view.__title__) self.assertIsNone(self.view.title) def test_BaseView_title_property_must_setted_title(self): expected_title = 'title' self.view.title = expected_title # title not setted self.assertIsNotNone(self.view.__title__) # title is set self.assertEqual(self.view.title, expected_title) def test_BaseView_title_property_must_getted_from_Meta_if_specified(self): expected_title = 'title' # title not setted self.assertIsNone(self.view.title) # manually set title self.view.Meta.title = expected_title self.assertEqual(self.view.title, expected_title) def test_BaseView_form_attribute_must_be_None_if_not_specified_in_Meta(self): # View Form not specified in class Meta self.assertIsNone(self.view.Meta.form) self.assertIsNone(self.view.form) @staticmethod def test_BaseView_form_attribute_must_be_instance_of_FlaskForm(): mocked_form = Mock(FlaskForm) BaseView.Meta.form = mocked_form # init new instance BaseView with form specified BaseView() # in init method BaseView must init form class and save form instance mocked_form.assert_called() def test_BaseView_template_property_must_return_attribute_from_Meta(self): """ template not specified """ self.assertIsNone(self.view.Meta.template) self.assertIsNone(self.view.template) def test_BaseView_template_property_must_return_attribute_from_Meta(self): """ template specified """ expected_template = 'some_template.html' BaseView.Meta.template = expected_template # view with template new_view = BaseView() self.assertEqual(new_view.template, expected_template) @patch('app.views.url_for') def test_BaseView_default_page_property_must_return_url_for_home(self, mocked_url): """ When default page is not specified property must return '/' """ expected_route = 'home' self.view.default_page mocked_url.assert_called_with(expected_route) @staticmethod @patch('app.views.url_for') def test_BaseView_default_page_property_must_return_url_for_home_if_Meta_default_page_not_specified(mocked_url): """ When default page is not specified property must return '/' """ BaseView.Meta.default_page = None expected_route = '/' new_view = BaseView() new_view.default_page mocked_url.assert_called_with(expected_route) @patch('flask_wtf.FlaskForm.validate_on_submit') def test_BaseView_is_post_method_must_return_value_from_FlaskForm_validate_on_submit_call(self, mocked_submit): # set form to view self.view.form = FlaskForm # call Form.validate_on_submit() self.view.is_post() mocked_submit.assert_called_once() @patch('flask_wtf.FlaskForm.validate_on_submit', return_value=True) def test_BaseView_is_post_method_must_return_True(self, _): # set form to view self.view.form = FlaskForm # call Form.validate_on_submit() self.assertTrue(self.view.is_post()) @patch('flask_wtf.FlaskForm.validate_on_submit', return_value=False) def test_BaseView_is_post_method_must_return_True(self, _): # set form to view self.view.form = FlaskForm # call Form.validate_on_submit() self.assertFalse(self.view.is_post()) @patch('app.views.request', spec=request) def test_BaseView_is_get_method_must_return_True_for_GET(self, mocked_request): mocked_request.method = 'GET' self.assertTrue(self.view.is_get()) @patch('app.views.request', spec=request) def test_BaseView_is_get_method_must_return_True_for_GET(self, mocked_request): for method in ('POST', 'PUT', 'DELETE', 'OPTIONS'): # etc # check all other methods too with self.subTest(method=method): mocked_request.method = method self.assertFalse(self.view.is_get()) mocked_request.method = 'POST' self.assertFalse(self.view.is_get()) @staticmethod @patch('app.views.render_template') def test_BaseView_render_own_template_method_must_call_render_template_function_with_param_at_default(mock_render): """ Default param is: - template - title - form """ BaseView.Meta.form = Mock(return_value='form') BaseView.Meta.title = 'title' BaseView.Meta.template = 'template' expected_kwargs = { '__title__': 'title', 'form': 'form' } new_view = BaseView() new_view._render_own_template() mock_render.assert_called_once_with('template', **expected_kwargs) @staticmethod @patch('app.views.render_template') def test_BaseView_render_own_template_method_must_call_render_template_function_with_params(mock_render): """ Default param is: - template - title - form """ BaseView.Meta.form = Mock(return_value='form') BaseView.Meta.title = 'title' BaseView.Meta.template = 'template' expected_kwargs = { '__title__': 'title', 'form': 'form', } additional_kwargs_to_render = { 'param1': 'param1', 'param2': 'param2' } new_view = BaseView() new_view._render_own_template(**additional_kwargs_to_render) mock_render.assert_called_once_with('template', **expected_kwargs, **additional_kwargs_to_render)
def setUp(self): self.view = BaseView()