Best Python code snippet using pyshould_python
test__utils.py
Source:test__utils.py
...83 self.assertIsNone(resp)84 resp = middleware(self.rf.get('/iam/the/marmot/'))85 self.assertEqual(resp.status_code, 301)86 self.assertEqual(resp['Location'], '/coo/coo/cachoo/')87 def test_match_flags(self):88 """89 Should be able to set regex flags for redirect URL.90 """91 resolver = get_resolver([92 redirect(r'^iam/the/walrus/$', '/coo/coo/cachoo/'),93 no_redirect(r'^iam/the/walrus/$', re_flags='i'),94 ])95 middleware = RedirectsMiddleware(resolver=resolver)96 resp = middleware(self.rf.get('/IAm/The/Walrus/'))97 self.assertIsNone(resp)98 # also with locale99 resp = middleware(self.rf.get('/es-ES/Iam/The/Walrus/'))100 self.assertIsNone(resp)101 # sanity check102 resp = middleware(self.rf.get('/iam/the/walrus/'))103 self.assertEqual(resp.status_code, 301)104 self.assertEqual(resp['Location'], '/coo/coo/cachoo/')105class TestRedirectUrlPattern(TestCase):106 def setUp(self):107 self.rf = RequestFactory()108 def test_name(self):109 """110 Should return a URLPattern with a matching name attribute111 """112 url_pattern = redirect(r'^the/dude$', 'abides', name='Lebowski')113 self.assertTrue(isinstance(url_pattern, URLPattern))114 self.assertEqual(url_pattern.name, 'Lebowski')115 def test_no_query(self):116 """117 Should return a 301 redirect118 """119 pattern = redirect(r'^the/dude$', 'abides')120 request = self.rf.get('the/dude')121 response = pattern.callback(request)122 self.assertEqual(response.status_code, 301)123 self.assertEqual(response['Location'], 'abides')124 def test_preserve_query(self):125 """126 Should preserve querys from the original request by default127 """128 pattern = redirect(r'^the/dude$', 'abides')129 request = self.rf.get('the/dude?aggression=not_stand')130 response = pattern.callback(request)131 self.assertEqual(response.status_code, 301)132 self.assertEqual(response['Location'], 'abides?aggression=not_stand')133 def test_replace_query(self):134 """135 Should replace query params if any are provided136 """137 pattern = redirect(r'^the/dude$', 'abides',138 query={'aggression': 'not_stand'})139 request = self.rf.get('the/dude?aggression=unchecked')140 response = pattern.callback(request)141 self.assertEqual(response.status_code, 301)142 self.assertEqual(response['Location'], 'abides?aggression=not_stand')143 def test_merge_query(self):144 """145 Should merge query params if requested146 """147 pattern = redirect(r'^the/dude$', 'abides',148 query={'aggression': 'not_stand'}, merge_query=True)149 request = self.rf.get('the/dude?hates=the-eagles')150 response = pattern.callback(request)151 self.assertEqual(response.status_code, 301)152 url = urlparse(response['location'])153 query_dict = parse_qs(url.query)154 self.assertTrue(url.path, 'abides')155 self.assertEqual(query_dict, {'aggression': ['not_stand'], 'hates': ['the-eagles']})156 def test_empty_query(self):157 """158 Should strip query params if called with empty query159 """160 pattern = redirect(r'^the/dude$', 'abides', query={})161 request = self.rf.get('the/dude?white=russian')162 response = pattern.callback(request)163 self.assertEqual(response.status_code, 301)164 self.assertEqual(response['Location'], 'abides')165 def test_temporary_redirect(self):166 """167 Should use a temporary redirect (status code 302) if permanent == False168 """169 pattern = redirect(r'^the/dude$', 'abides', permanent=False)170 request = self.rf.get('the/dude')171 response = pattern.callback(request)172 self.assertEqual(response.status_code, 302)173 self.assertEqual(response['Location'], 'abides')174 def test_anchor(self):175 """176 Should append anchor text to the end, including after any querystring177 """178 pattern = redirect(r'^the/dude$', 'abides', anchor='toe')179 request = self.rf.get('the/dude?want=a')180 response = pattern.callback(request)181 self.assertEqual(response.status_code, 301)182 self.assertEqual(response['Location'], 'abides?want=a#toe')183 def test_callable(self):184 """185 Should use the return value of the callable as redirect location186 """187 def opinion(request):188 return '/just/your/opinion/man'189 pattern = redirect(r'^the/dude$', opinion)190 request = self.rf.get('the/dude')191 response = pattern.callback(request)192 self.assertEqual(response.status_code, 301)193 self.assertEqual(response['Location'], '/just/your/opinion/man')194 @patch('redirect_urls.utils.reverse')195 def test_to_view(self, mock_reverse):196 """197 Should use return value of reverse as redirect location198 """199 mock_reverse.return_value = '/just/your/opinion/man'200 pattern = redirect(r'^the/dude$', 'yeah.well.you.know.thats')201 request = self.rf.get('the/dude')202 response = pattern.callback(request)203 mock_reverse.assert_called_with('yeah.well.you.know.thats', args=None, kwargs=None)204 self.assertEqual(response.status_code, 301)205 self.assertEqual(response['Location'], '/just/your/opinion/man')206 @patch('redirect_urls.utils.reverse')207 def test_to_view_args_kwargs(self, mock_reverse):208 """209 Should call reverse with specified args and/or kwargs.210 """211 mock_reverse.return_value = '/just/your/opinion/man'212 pattern = redirect(r'^the/dude$', 'yeah.well.you.know.thats',213 to_args=['dude'], to_kwargs={'tapes': 'credence'})214 request = self.rf.get('the/dude')215 response = pattern.callback(request)216 mock_reverse.assert_called_with('yeah.well.you.know.thats',217 args=['dude'], kwargs={'tapes': 'credence'})218 self.assertEqual(response.status_code, 301)219 self.assertEqual(response['Location'], '/just/your/opinion/man')220 def test_cache_headers(self):221 """222 Should add cache headers based on argument.223 """224 pattern = redirect(r'^the/dude$', 'abides', cache_timeout=2)225 request = self.rf.get('the/dude')226 response = pattern.callback(request)227 self.assertEqual(response.status_code, 301)228 self.assertEqual(response['Location'], 'abides')229 self.assertEqual(response['cache-control'], 'max-age=7200') # 2 hours230 def test_vary_header(self):231 """232 Should add vary header based on argument.233 """234 pattern = redirect(r'^the/dude$', 'abides', vary='Accept-Language')235 request = self.rf.get('the/dude')236 response = pattern.callback(request)237 self.assertEqual(response.status_code, 301)238 self.assertEqual(response['Location'], 'abides')239 self.assertEqual(response['Vary'], 'Accept-Language')240 def test_value_capture_and_substitution(self):241 """242 Should be able to capture info from URL and use in redirection.243 """244 resolver = get_resolver([redirect(r'^iam/the/(?P<name>.+)/$', '/donnie/the/{name}/')])245 middleware = RedirectsMiddleware(resolver=resolver)246 resp = middleware(self.rf.get('/iam/the/walrus/'))247 self.assertEqual(resp.status_code, 301)248 self.assertEqual(resp['Location'], '/donnie/the/walrus/')249 def test_locale_value_capture(self):250 """251 Should prepend locale value automatically.252 """253 resolver = get_resolver([redirect(r'^iam/the/(?P<name>.+)/$',254 '/donnie/the/{name}/')])255 middleware = RedirectsMiddleware(resolver=resolver)256 resp = middleware(self.rf.get('/pt-BR/iam/the/walrus/'))257 self.assertEqual(resp.status_code, 301)258 self.assertEqual(resp['Location'], '/pt-BR/donnie/the/walrus/')259 def test_locale_value_capture_no_locale(self):260 """261 Should get locale value in kwargs and not break if no locale in URL.262 """263 resolver = get_resolver([redirect(r'^iam/the/(?P<name>.+)/$',264 '/donnie/the/{name}/')])265 middleware = RedirectsMiddleware(resolver=resolver)266 resp = middleware(self.rf.get('/iam/the/walrus/'))267 self.assertEqual(resp.status_code, 301)268 self.assertEqual(resp['Location'], '/donnie/the/walrus/')269 def test_locale_value_capture_ignore_locale(self):270 """271 Should be able to ignore the original locale.272 """273 resolver = get_resolver([redirect(r'^iam/the/(?P<name>.+)/$',274 '/donnie/the/{name}/', prepend_locale=False)])275 middleware = RedirectsMiddleware(resolver=resolver)276 resp = middleware(self.rf.get('/zh-TW/iam/the/walrus/'))277 self.assertEqual(resp.status_code, 301)278 self.assertEqual(resp['Location'], '/donnie/the/walrus/')279 def test_no_locale_prefix(self):280 """281 Should be able to define a redirect that ignores locale prefix.282 Also when not using any named captures (like implied locale) unnamed283 captures should work. For some reason Django only allows unnamed captures284 to pass through if there are no named ones.285 """286 resolver = get_resolver([redirect(r'^iam/the/(.+)/$', '/donnie/the/{}/',287 locale_prefix=False)])288 middleware = RedirectsMiddleware(resolver=resolver)289 resp = middleware(self.rf.get('/iam/the/walrus/'))290 self.assertEqual(resp.status_code, 301)291 self.assertEqual(resp['Location'], '/donnie/the/walrus/')292 def test_empty_unnamed_captures(self):293 """294 Should be able to define an optional unnamed capture.295 """296 resolver = get_resolver([redirect(r'^iam/the(/.+)?/$', '/donnie/the{}/',297 locale_prefix=False)])298 middleware = RedirectsMiddleware(resolver=resolver)299 resp = middleware(self.rf.get('/iam/the/'))300 self.assertEqual(resp.status_code, 301)301 self.assertEqual(resp['Location'], '/donnie/the/')302 def test_match_flags(self):303 """304 Should be able to set regex flags for redirect URL.305 """306 resolver = get_resolver([307 redirect(r'^iam/the/walrus/$', '/coo/coo/cachoo/'),308 redirect(r'^iam/the/walrus/$', '/dammit/donnie/', re_flags='i'),309 ])310 middleware = RedirectsMiddleware(resolver=resolver)311 resp = middleware(self.rf.get('/IAm/The/Walrus/'))312 self.assertEqual(resp.status_code, 301)313 self.assertEqual(resp['Location'], '/dammit/donnie/')314 # also with locale315 resp = middleware(self.rf.get('/es-ES/Iam/The/Walrus/'))316 self.assertEqual(resp.status_code, 301)...
test_util.py
Source:test_util.py
...78 self.assertIsNone(resp)79 resp = middleware.process_request(self.rf.get('/iam/the/marmot/'))80 eq_(resp.status_code, 301)81 eq_(resp['Location'], '/coo/coo/cachoo/')82 def test_match_flags(self):83 """84 Should be able to set regex flags for redirect URL.85 """86 resolver = get_resolver([87 redirect(r'^iam/the/walrus/$', '/coo/coo/cachoo/'),88 no_redirect(r'^iam/the/walrus/$', re_flags='i'),89 ])90 middleware = RedirectsMiddleware(resolver)91 resp = middleware.process_request(self.rf.get('/IAm/The/Walrus/'))92 self.assertIsNone(resp)93 # also with locale94 resp = middleware.process_request(self.rf.get('/es-ES/Iam/The/Walrus/'))95 self.assertIsNone(resp)96 # sanity check97 resp = middleware.process_request(self.rf.get('/iam/the/walrus/'))98 eq_(resp.status_code, 301)99 eq_(resp['Location'], '/coo/coo/cachoo/')100class TestRedirectUrlPattern(TestCase):101 def setUp(self):102 self.rf = RequestFactory()103 def test_name(self):104 """105 Should return a RegexURLPattern with a matching name attribute106 """107 url_pattern = redirect(r'^the/dude$', 'abides', name='Lebowski')108 ok_(isinstance(url_pattern, RegexURLPattern))109 eq_(url_pattern.name, 'Lebowski')110 def test_no_query(self):111 """112 Should return a 301 redirect113 """114 pattern = redirect(r'^the/dude$', 'abides')115 request = self.rf.get('the/dude')116 response = pattern.callback(request)117 eq_(response.status_code, 301)118 eq_(response['Location'], 'abides')119 def test_preserve_query(self):120 """121 Should preserve querys from the original request by default122 """123 pattern = redirect(r'^the/dude$', 'abides')124 request = self.rf.get('the/dude?aggression=not_stand')125 response = pattern.callback(request)126 eq_(response.status_code, 301)127 eq_(response['Location'], 'abides?aggression=not_stand')128 def test_replace_query(self):129 """130 Should replace query params if any are provided131 """132 pattern = redirect(r'^the/dude$', 'abides',133 query={'aggression': 'not_stand'})134 request = self.rf.get('the/dude?aggression=unchecked')135 response = pattern.callback(request)136 eq_(response.status_code, 301)137 eq_(response['Location'], 'abides?aggression=not_stand')138 def test_merge_query(self):139 """140 Should merge query params if requested141 """142 pattern = redirect(r'^the/dude$', 'abides',143 query={'aggression': 'not_stand'}, merge_query=True)144 request = self.rf.get('the/dude?hates=the-eagles')145 response = pattern.callback(request)146 eq_(response.status_code, 301)147 url = urlparse(response['location'])148 query_dict = parse_qs(url.query)149 self.assertTrue(url.path, 'abides')150 self.assertEqual(query_dict, {'aggression': ['not_stand'], 'hates': ['the-eagles']})151 def test_merge_query_no_url_query(self):152 """153 Should merge query params if requested and no params in URL154 """155 pattern = redirect(r'^the/dude$', 'abides',156 query={'aggression': 'not_stand'}, merge_query=True)157 request = self.rf.get('the/dude')158 response = pattern.callback(request)159 eq_(response.status_code, 301)160 url = urlparse(response['location'])161 query_dict = parse_qs(url.query)162 self.assertTrue(url.path, 'abides')163 self.assertEqual(query_dict, {'aggression': ['not_stand']})164 def test_empty_query(self):165 """166 Should strip query params if called with empty query167 """168 pattern = redirect(r'^the/dude$', 'abides', query={})169 request = self.rf.get('the/dude?white=russian')170 response = pattern.callback(request)171 eq_(response.status_code, 301)172 eq_(response['Location'], 'abides')173 def test_temporary_redirect(self):174 """175 Should use a temporary redirect (status code 302) if permanent == False176 """177 pattern = redirect(r'^the/dude$', 'abides', permanent=False)178 request = self.rf.get('the/dude')179 response = pattern.callback(request)180 eq_(response.status_code, 302)181 eq_(response['Location'], 'abides')182 def test_anchor(self):183 """184 Should append anchor text to the end, including after any querystring185 """186 pattern = redirect(r'^the/dude$', 'abides', anchor='toe')187 request = self.rf.get('the/dude?want=a')188 response = pattern.callback(request)189 eq_(response.status_code, 301)190 eq_(response['Location'], 'abides?want=a#toe')191 def test_callable(self):192 """193 Should use the return value of the callable as redirect location194 """195 def opinion(request):196 return '/just/your/opinion/man'197 pattern = redirect(r'^the/dude$', opinion)198 request = self.rf.get('the/dude')199 response = pattern.callback(request)200 eq_(response.status_code, 301)201 eq_(response['Location'], '/just/your/opinion/man')202 @patch('bedrock.redirects.util.reverse')203 def test_to_view(self, mock_reverse):204 """205 Should use return value of reverse as redirect location206 """207 mock_reverse.return_value = '/just/your/opinion/man'208 pattern = redirect(r'^the/dude$', 'yeah.well.you.know.thats')209 request = self.rf.get('the/dude')210 response = pattern.callback(request)211 mock_reverse.assert_called_with('yeah.well.you.know.thats', args=None, kwargs=None)212 eq_(response.status_code, 301)213 eq_(response['Location'], '/just/your/opinion/man')214 @patch('bedrock.redirects.util.reverse')215 def test_to_view_args_kwargs(self, mock_reverse):216 """217 Should call reverse with specified args and/or kwargs.218 """219 mock_reverse.return_value = '/just/your/opinion/man'220 pattern = redirect(r'^the/dude$', 'yeah.well.you.know.thats',221 to_args=['dude'], to_kwargs={'tapes': 'credence'})222 request = self.rf.get('the/dude')223 response = pattern.callback(request)224 mock_reverse.assert_called_with('yeah.well.you.know.thats',225 args=['dude'], kwargs={'tapes': 'credence'})226 eq_(response.status_code, 301)227 eq_(response['Location'], '/just/your/opinion/man')228 def test_cache_headers(self):229 """230 Should add cache headers based on argument.231 """232 pattern = redirect(r'^the/dude$', 'abides', cache_timeout=2)233 request = self.rf.get('the/dude')234 response = pattern.callback(request)235 eq_(response.status_code, 301)236 eq_(response['Location'], 'abides')237 eq_(response['cache-control'], 'max-age=7200') # 2 hours238 def test_vary_header(self):239 """240 Should add vary header based on argument.241 """242 pattern = redirect(r'^the/dude$', 'abides', vary='Accept-Language')243 request = self.rf.get('the/dude')244 response = pattern.callback(request)245 eq_(response.status_code, 301)246 eq_(response['Location'], 'abides')247 eq_(response['Vary'], 'Accept-Language')248 def test_value_capture_and_substitution(self):249 """250 Should be able to capture info from URL and use in redirection.251 """252 resolver = get_resolver([redirect(r'^iam/the/(?P<name>.+)/$', '/donnie/the/{name}/')])253 middleware = RedirectsMiddleware(resolver)254 resp = middleware.process_request(self.rf.get('/iam/the/walrus/'))255 eq_(resp.status_code, 301)256 eq_(resp['Location'], '/donnie/the/walrus/')257 def test_locale_value_capture(self):258 """259 Should prepend locale value automatically.260 """261 resolver = get_resolver([redirect(r'^iam/the/(?P<name>.+)/$',262 '/donnie/the/{name}/')])263 middleware = RedirectsMiddleware(resolver)264 resp = middleware.process_request(self.rf.get('/pt-BR/iam/the/walrus/'))265 eq_(resp.status_code, 301)266 eq_(resp['Location'], '/pt-BR/donnie/the/walrus/')267 def test_locale_value_capture_no_locale(self):268 """269 Should get locale value in kwargs and not break if no locale in URL.270 """271 resolver = get_resolver([redirect(r'^iam/the/(?P<name>.+)/$',272 '/donnie/the/{name}/')])273 middleware = RedirectsMiddleware(resolver)274 resp = middleware.process_request(self.rf.get('/iam/the/walrus/'))275 eq_(resp.status_code, 301)276 eq_(resp['Location'], '/donnie/the/walrus/')277 def test_locale_value_capture_ignore_locale(self):278 """279 Should be able to ignore the original locale.280 """281 resolver = get_resolver([redirect(r'^iam/the/(?P<name>.+)/$',282 '/donnie/the/{name}/', prepend_locale=False)])283 middleware = RedirectsMiddleware(resolver)284 resp = middleware.process_request(self.rf.get('/zh-TW/iam/the/walrus/'))285 eq_(resp.status_code, 301)286 eq_(resp['Location'], '/donnie/the/walrus/')287 def test_no_locale_prefix(self):288 """289 Should be able to define a redirect that ignores locale prefix.290 Also when not using any named captures (like implied locale) unnamed291 captures should work. For some reason Django only allows unnamed captures292 to pass through if there are no named ones.293 """294 resolver = get_resolver([redirect(r'^iam/the/(.+)/$', '/donnie/the/{}/',295 locale_prefix=False)])296 middleware = RedirectsMiddleware(resolver)297 resp = middleware.process_request(self.rf.get('/iam/the/walrus/'))298 eq_(resp.status_code, 301)299 eq_(resp['Location'], '/donnie/the/walrus/')300 def test_empty_unnamed_captures(self):301 """302 Should be able to define an optional unnamed capture.303 """304 resolver = get_resolver([redirect(r'^iam/the(/.+)?/$', '/donnie/the{}/',305 locale_prefix=False)])306 middleware = RedirectsMiddleware(resolver)307 resp = middleware.process_request(self.rf.get('/iam/the/'))308 eq_(resp.status_code, 301)309 eq_(resp['Location'], '/donnie/the/')310 def test_match_flags(self):311 """312 Should be able to set regex flags for redirect URL.313 """314 resolver = get_resolver([315 redirect(r'^iam/the/walrus/$', '/coo/coo/cachoo/'),316 redirect(r'^iam/the/walrus/$', '/dammit/donnie/', re_flags='i'),317 ])318 middleware = RedirectsMiddleware(resolver)319 resp = middleware.process_request(self.rf.get('/IAm/The/Walrus/'))320 eq_(resp.status_code, 301)321 eq_(resp['Location'], '/dammit/donnie/')322 # also with locale323 resp = middleware.process_request(self.rf.get('/es-ES/Iam/The/Walrus/'))324 eq_(resp.status_code, 301)...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!