How to use _which_response method in Django Test Plus

Best Python code snippet using django-test-plus_python

taskflow_testcase.py

Source:taskflow_testcase.py Github

copy

Full Screen

...141 def options(self, url_name, *args, **kwargs):142 return self.request('options', url_name, *args, **kwargs)143 def delete(self, url_name, *args, **kwargs):144 return self.request('delete', url_name, *args, **kwargs)145 def _which_response(self, response=None):146 if response is None and self.last_response is not None:147 return self.last_response148 else:149 return response150 def _assert_response_code(self, status_code, response=None, msg=None):151 response = self._which_response(response)152 self.assertEqual(response.status_code, status_code, msg)153 def response_200(self, response=None, msg=None):154 """Given response has status_code 200"""155 self._assert_response_code(200, response, msg)156 def response_201(self, response=None, msg=None):157 """Given response has status_code 201"""158 self._assert_response_code(201, response, msg)159 def response_204(self, response=None, msg=None):160 """Given response has status_code 204"""161 self._assert_response_code(204, response, msg)162 def response_301(self, response=None, msg=None):163 """Given response has status_code 301"""164 self._assert_response_code(301, response, msg)165 def response_302(self, response=None, msg=None):166 """Given response has status_code 302"""167 self._assert_response_code(302, response, msg)168 def response_400(self, response=None, msg=None):169 """Given response has status_code 400"""170 self._assert_response_code(400, response, msg)171 def response_401(self, response=None, msg=None):172 """Given response has status_code 401"""173 self._assert_response_code(401, response, msg)174 def response_403(self, response=None, msg=None):175 """Given response has status_code 403"""176 self._assert_response_code(403, response, msg)177 def response_404(self, response=None, msg=None):178 """Given response has status_code 404"""179 self._assert_response_code(404, response, msg)180 def response_405(self, response=None, msg=None):181 """Given response has status_code 405"""182 self._assert_response_code(405, response, msg)183 def response_409(self, response=None, msg=None):184 """Given response has status_code 409"""185 self._assert_response_code(409, response, msg)186 def response_410(self, response=None, msg=None):187 """Given response has status_code 410"""188 self._assert_response_code(410, response, msg)189 def get_check_200(self, url, *args, **kwargs):190 """Test that we can GET a page and it returns a 200"""191 response = self.get(url, *args, **kwargs)192 self.response_200(response)193 return response194 def assertLoginRequired(self, url, *args, **kwargs):195 """Ensure login is required to GET this URL"""196 response = self.get(url, *args, **kwargs)197 reversed_url = reverse(url, args=args, kwargs=kwargs)198 login_url = str(resolve_url(settings.LOGIN_URL))199 expected_url = "{0}?next={1}".format(login_url, reversed_url)200 self.assertRedirects(response, expected_url)201 def login(self, *args, **credentials):202 """Login a user"""203 return login(self, *args, **credentials)204 def reverse(self, name, *args, **kwargs):205 """Reverse a url, convenience to avoid having to import reverse in tests"""206 return reverse(name, args=args, kwargs=kwargs)207 @classmethod208 def make_user(cls, username='testuser', password='password', perms=None):209 """210 Build a user with <username> and password of 'password' for testing211 purposes.212 """213 User = get_user_model()214 if cls.user_factory:215 USERNAME_FIELD = getattr(216 cls.user_factory._meta.model, 'USERNAME_FIELD', 'username'217 )218 test_user = cls.user_factory(219 **{220 USERNAME_FIELD: username,221 }222 )223 test_user.set_password(password)224 test_user.save()225 else:226 test_user = User.objects.create_user(227 username,228 '{0}@example.com'.format(username),229 password,230 )231 if perms:232 from django.contrib.auth.models import Permission233 _filter = Q()234 for perm in perms:235 if '.' not in perm:236 raise ImproperlyConfigured(237 'The permission in the perms argument needs to be either '238 'app_label.codename or app_label.* (e.g. accounts.change_user or accounts.*)'239 )240 app_label, codename = perm.split('.')241 if codename == '*':242 _filter = _filter | Q(content_type__app_label=app_label)243 else:244 _filter = _filter | Q(245 content_type__app_label=app_label, codename=codename246 )247 test_user.user_permissions.add(248 *list(Permission.objects.filter(_filter))249 )250 return test_user251 def assertNumQueriesLessThan(self, num, *args, **kwargs):252 func = kwargs.pop('func', None)253 using = kwargs.pop("using", DEFAULT_DB_ALIAS)254 conn = connections[using]255 context = _AssertNumQueriesLessThanContext(self, num, conn)256 if func is None:257 return context258 with context:259 func(*args, **kwargs)260 def assertGoodView(self, url_name, *args, **kwargs):261 """262 Quick-n-dirty testing of a given url name.263 Ensures URL returns a 200 status and that generates less than 50264 database queries.265 """266 query_count = kwargs.pop('test_query_count', 50)267 with self.assertNumQueriesLessThan(query_count):268 response = self.get(url_name, *args, **kwargs)269 self.response_200(response)270 return response271 def assertInContext(self, key):272 if self.last_response is not None:273 self.assertTrue(key in self.last_response.context)274 else:275 raise NoPreviousResponse("There isn't a previous response to query")276 def assertResponseContains(self, text, response=None, html=True, **kwargs):277 """Convenience wrapper for assertContains"""278 response = self._which_response(response)279 self.assertContains(response, text, html=html, **kwargs)280 def assertResponseNotContains(281 self, text, response=None, html=True, **kwargs282 ):283 """Convenience wrapper for assertNotContains"""284 response = self._which_response(response)285 self.assertNotContains(response, text, html=html, **kwargs)286 def assertResponseHeaders(self, headers, response=None):287 """288 Check that the headers in the response are as expected.289 Only headers defined in `headers` are compared, other keys present on290 the `response` will be ignored.291 :param headers: Mapping of header names to expected values292 :type headers: :class:`collections.Mapping`293 :param response: Response to check headers against294 :type response: :class:`django.http.response.HttpResponse`295 """296 response = self._which_response(response)297 compare = {h: response.get(h) for h in headers}298 self.assertEqual(compare, headers)299 def get_context(self, key):300 if self.last_response is not None:301 self.assertTrue(key in self.last_response.context)302 return self.last_response.context[key]303 else:304 raise NoPreviousResponse("There isn't a previous response to query")305 def assertContext(self, key, value):306 if self.last_response is not None:307 self.assertEqual(self.last_response.context[key], value)308 else:309 raise NoPreviousResponse("There isn't a previous response to query")310class TestCase(DjangoTestCase, BaseTestCase):...

Full Screen

Full Screen

test.py

Source:test.py Github

copy

Full Screen

...122 def options(self, url_name, *args, **kwargs):123 return self.request('options', url_name, *args, **kwargs)124 def delete(self, url_name, *args, **kwargs):125 return self.request('delete', url_name, *args, **kwargs)126 def _which_response(self, response=None):127 if response is None and self.last_response is not None:128 return self.last_response129 else:130 return response131 def response_200(self, response=None, msg=None):132 """ Given response has status_code 200 """133 response = self._which_response(response)134 self.assertEqual(response.status_code, 200, msg)135 def response_201(self, response=None, msg=None):136 """ Given response has status_code 201 """137 response = self._which_response(response)138 self.assertEqual(response.status_code, 201, msg)139 def response_204(self, response=None, msg=None):140 """ Given response has status_code 204 """141 response = self._which_response(response)142 self.assertEqual(response.status_code, 204, msg)143 def response_301(self, response=None, msg=None):144 """ Given response has status_code 301 """145 response = self._which_response(response)146 self.assertEqual(response.status_code, 301, msg)147 def response_302(self, response=None, msg=None):148 """ Given response has status_code 302 """149 response = self._which_response(response)150 self.assertEqual(response.status_code, 302, msg)151 def response_400(self, response=None, msg=None):152 """ Given response has status_code 400 """153 response = self._which_response(response)154 self.assertEqual(response.status_code, 400, msg)155 def response_401(self, response=None, msg=None):156 """ Given response has status_code 401 """157 response = self._which_response(response)158 self.assertEqual(response.status_code, 401, msg)159 def response_403(self, response=None, msg=None):160 """ Given response has status_code 403 """161 response = self._which_response(response)162 self.assertEqual(response.status_code, 403, msg)163 def response_404(self, response=None, msg=None):164 """ Given response has status_code 404 """165 response = self._which_response(response)166 self.assertEqual(response.status_code, 404, msg)167 def response_405(self, response=None, msg=None):168 """ Given response has status_code 405 """169 response = self._which_response(response)170 self.assertEqual(response.status_code, 405, msg)171 def response_410(self, response=None, msg=None):172 """ Given response has status_code 410 """173 response = self._which_response(response)174 self.assertEqual(response.status_code, 410, msg)175 def get_check_200(self, url, *args, **kwargs):176 """ Test that we can GET a page and it returns a 200 """177 response = self.get(url, *args, **kwargs)178 self.response_200(response)179 return response180 def assertLoginRequired(self, url, *args, **kwargs):181 """ Ensure login is required to GET this URL """182 response = self.get(url, *args, **kwargs)183 reversed_url = reverse(url, args=args, kwargs=kwargs)184 login_url = str(resolve_url(settings.LOGIN_URL))185 expected_url = "{0}?next={1}".format(login_url, reversed_url)186 self.assertRedirects(response, expected_url)187 def login(self, *args, **credentials):188 """ Login a user """189 return login(self, *args, **credentials)190 def reverse(self, name, *args, **kwargs):191 """ Reverse a url, convenience to avoid having to import reverse in tests """192 return reverse(name, args=args, kwargs=kwargs)193 def make_user(self, username='testuser', password='password', perms=None):194 """195 Build a user with <username> and password of 'password' for testing196 purposes.197 """198 User = get_user_model()199 if self.user_factory:200 USERNAME_FIELD = getattr(201 self.user_factory._meta.model, 'USERNAME_FIELD', 'username')202 test_user = self.user_factory(**{203 USERNAME_FIELD: username,204 })205 test_user.set_password(password)206 test_user.save()207 else:208 test_user = User.objects.create_user(209 username,210 '{0}@example.com'.format(username),211 password,212 )213 if perms:214 from django.contrib.auth.models import Permission215 _filter = Q()216 for perm in perms:217 if '.' not in perm:218 raise ImproperlyConfigured(219 'The permission in the perms argument needs to be either '220 'app_label.codename or app_label.* (e.g. accounts.change_user or accounts.*)'221 )222 app_label, codename = perm.split('.')223 if codename == '*':224 _filter = _filter | Q(content_type__app_label=app_label)225 else:226 _filter = _filter | Q(content_type__app_label=app_label, codename=codename)227 test_user.user_permissions.add(*list(Permission.objects.filter(_filter)))228 return test_user229 def assertNumQueriesLessThan(self, num, *args, **kwargs):230 func = kwargs.pop('func', None)231 using = kwargs.pop("using", DEFAULT_DB_ALIAS)232 conn = connections[using]233 context = _AssertNumQueriesLessThanContext(self, num, conn)234 if func is None:235 return context236 with context:237 func(*args, **kwargs)238 def assertGoodView(self, url_name, *args, **kwargs):239 """240 Quick-n-dirty testing of a given url name.241 Ensures URL returns a 200 status and that generates less than 50242 database queries.243 """244 query_count = kwargs.pop('test_query_count', 50)245 with self.assertNumQueriesLessThan(query_count):246 response = self.get(url_name, *args, **kwargs)247 self.response_200(response)248 return response249 def assertInContext(self, key):250 if self.last_response is not None:251 self.assertTrue(key in self.last_response.context)252 else:253 raise NoPreviousResponse("There isn't a previous response to query")254 def assertResponseContains(self, text, response=None, html=True, **kwargs):255 """ Convenience wrapper for assertContains """256 response = self._which_response(response)257 self.assertContains(response, text, html=html, **kwargs)258 def assertResponseNotContains(self, text, response=None, html=True, **kwargs):259 """ Convenience wrapper for assertNotContains """260 response = self._which_response(response)261 self.assertNotContains(response, text, html=html, **kwargs)262 def assertResponseHeaders(self, headers, response=None):263 """264 Check that the headers in the response are as expected.265 Only headers defined in `headers` are compared, other keys present on266 the `response` will be ignored.267 :param headers: Mapping of header names to expected values268 :type headers: :class:`collections.Mapping`269 :param response: Response to check headers against270 :type response: :class:`django.http.response.HttpResponse`271 """272 response = self._which_response(response)273 compare = {h: response.get(h) for h in headers}274 self.assertEqual(compare, headers)275 def get_context(self, key):276 if self.last_response is not None:277 self.assertTrue(key in self.last_response.context)278 return self.last_response.context[key]279 else:280 raise NoPreviousResponse("There isn't a previous response to query")281 def assertContext(self, key, value):282 if self.last_response is not None:283 self.assertEqual(self.last_response.context[key], value)284 else:285 raise NoPreviousResponse("There isn't a previous response to query")286class TestCase(DjangoTestCase, BaseTestCase):...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Django Test Plus automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful