How to use assertContext method in Django Test Plus

Best Python code snippet using django-test-plus_python

test_calculate.py

Source:test_calculate.py Github

copy

Full Screen

...21 url = url + "?lat=55.766557&lon=37.623429&name=Russia,%20Moscow,%20Boulevard%20Ring"22 response = self.client.get(url)23 self.assertStatus(response, 200)24 self.assertIn("Location is within MKAD", str(response.data))25 self.assertContext(26 "context",27 {28 'distance': -1, 29 'success': True, 30 'longitude': '37.623429', 31 'latitude': '55.766557', 32 'name': 'Russia, Moscow, Boulevard Ring'33 }34 )35 def test_for_location_outside_mkad_without_name_argument(self):36 """Test for a location that is outside the mkad boundary."""37 url = url_for("calculate.calculate")38 url = url + "?lat=55.821072&lon=37.837153"39 response = self.client.get(url)40 self.assertStatus(response, 200)41 self.assertIn("Lat: 55.821072", str(response.data))42 self.assertIn("Lon: 37.837153", str(response.data))43 self.assertIn("Moscow Ring Road", str(response.data))44 self.assertIn("16.41 km", str(response.data))45 self.assertContext(46 "context",47 {48 'distance': 16.41, 49 'success': True, 50 'longitude': '37.837153', 51 'latitude': '55.821072', 52 'name': None53 }54 )55 def test_for_location_on_the_mkad_boundary(self):56 """Test for a location on the boundary's edge."""57 url = url_for("calculate.calculate")58 url = url + "?lat=55.747399&lon=37.841828"59 response = self.client.get(url)60 self.assertStatus(response, 200)61 self.assertIn("Lat: 55.747399", str(response.data))62 self.assertIn("Lon: 37.841828", str(response.data))63 self.assertIn("Moscow Ring Road", str(response.data))64 self.assertIn("14.36 km", str(response.data))65 self.assertContext(66 "context",67 {68 'distance': 14.36, 69 'success': True, 70 'longitude': '37.841828', 71 'latitude': '55.747399', 72 'name': None73 }74 )75 def test_when_invalid_valid_input_is_passed_as_longitude_only(self):76 """Test when invalid input is passed to the view."""77 url = url_for("calculate.calculate")78 url = url + "?lat=55.747399&lon=invalid"79 response = self.client.get(url)80 self.assertStatus(response, 200)81 self.assertIn(82 "Error in Latitude and/or Longitude values, ex. 58.34553234 .", 83 str(response.data)84 )85 self.assertContext(86 "context",87 {88 'distance': 0, 89 'success': False, 90 'longitude': 'invalid', 91 'latitude': '55.747399', 92 'name': None93 }94 )95 def test_when_no_arguments_is_passed(self):96 """Test when no value is passed."""97 url = url_for("calculate.calculate")98 url = url + "?lat=invalid&lon=37.841828"99 response = self.client.get(url)100 self.assertStatus(response, 200)101 self.assertIn(102 "Error in Latitude and/or Longitude values, ex. 58.34553234 .", 103 str(response.data)104 )105 self.assertContext(106 "context",107 {108 'distance': 0, 109 'success': False, 110 'longitude': '37.841828', 111 'latitude': 'invalid', 112 'name': None113 }114 ) 115 def test_when_invalid_valid_input_is_passed_as_latitude_only(self):116 """Test when invalid input is passed to the view."""117 url = url_for("calculate.calculate")118 url = url + "?lat=invalid&lon=37.841828"119 response = self.client.get(url)120 self.assertStatus(response, 200)121 self.assertIn(122 "Error in Latitude and/or Longitude values, ex. 58.34553234 .", 123 str(response.data)124 )125 self.assertContext(126 "context",127 {128 'distance': 0, 129 'success': False, 130 'longitude': '37.841828', 131 'latitude': 'invalid', 132 'name': None133 }134 ) 135 def test_when_both_values_are_invalid(self):136 """Test when both parameters passed to the view is invalid."""137 url = url_for("calculate.calculate")138 url = url + "?lat=invalid&lon=invalid"139 response = self.client.get(url)140 self.assertStatus(response, 200)141 self.assertIn(142 "Error in Latitude and/or Longitude values, ex. 58.34553234 .", 143 str(response.data)144 )145 self.assertContext(146 "context",147 {148 'distance': 0, 149 'success': False, 150 'longitude': 'invalid', 151 'latitude': 'invalid', 152 'name': None153 }154 ) 155 def test_when_no_value_is_passed_for_longitude_only(self):156 """Test when there is no longitude value."""157 url = url_for("calculate.calculate")158 url = url + "?lat=57.3445&lon="159 response = self.client.get(url)160 self.assertStatus(response, 200)161 self.assertIn(162 "Error in Latitude and/or Longitude values, ex. 58.34553234 .", 163 str(response.data)164 )165 self.assertContext(166 "context",167 {168 'distance': 0, 169 'success': False, 170 'longitude': '', 171 'latitude': '57.3445', 172 'name': None173 }174 ) 175 def test_when_no_value_is_passed_for_latitude_only(self):176 """Test when there is no latitude value."""177 url = url_for("calculate.calculate")178 url = url + "?lat=&lon=57.3445"179 response = self.client.get(url)180 self.assertStatus(response, 200)181 self.assertIn(182 "Error in Latitude and/or Longitude values, ex. 58.34553234 .", 183 str(response.data)184 )185 self.assertContext(186 "context",187 {188 'distance': 0, 189 'success': False, 190 'longitude': '57.3445', 191 'latitude': '', 192 'name': None193 }194 ) 195 def test_when_both_arguments_are_ommited(self):196 """Test when there are no arguments passed in."""197 url = url_for("calculate.calculate")198 url = url + "?"199 response = self.client.get(url)200 self.assertStatus(response, 200)201 self.assertIn(202 "Error in Latitude and/or Longitude values, ex. 58.34553234 .", 203 str(response.data)204 )205 self.assertContext(206 "context",207 {208 'distance': 0, 209 'success': False, 210 'longitude': None, 211 'latitude': None, 212 'name': None213 }214 ) 215 216 def test_logging_was_done_when_a_location_tested_is_out_of_the_mkad_boundary(self):217 """Test logging functionality."""218 url = url_for("calculate.calculate")219 url = url + "?lat=55.821072&lon=37.837153"220 log_info_before_request = self.read_log_file_info()221 response = self.client.get(url)222 log_info_after_request = self.read_log_file_info()223 self.assertEqual(log_info_before_request["count"] + 1, log_info_after_request["count"])224 self.assertNotEqual(225 log_info_after_request["last_line"], 226 log_info_before_request["last_line"]227 )228 # Create a regex to match and verify the log message sent to the file.229 pattern = (230 r"\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}\]"231 + r" INFO in calculate: Distance from Lat: 55.821072 \| Lon: 37.837153"232 + r" to Moscow Ring Road: 16.41km ."""233 )234 match = re.match(pattern, log_info_after_request["last_line"])235 self.assertFalse(match is None)236 self.assertStatus(response, 200)237 self.assertIn("Lat: 55.821072", str(response.data))238 self.assertIn("Lon: 37.837153", str(response.data))239 self.assertIn("Moscow Ring Road", str(response.data))240 self.assertIn("16.41 km", str(response.data))241 self.assertContext(242 "context",243 {244 'distance': 16.41, 245 'success': True, 246 'longitude': '37.837153', 247 'latitude': '55.821072', 248 'name': None249 }250 )251 def test_logging_is_not_done_when_a_location_tested_is_within_the_mkad_boundary(self):252 """Testing logging is not done when the searched location is within the mkad."""253 url = url_for("calculate.calculate")254 url = url + "?lat=55.766557&lon=37.623429&name=Russia,%20Moscow,%20Boulevard%20Ring"255 log_info_before_request = self.read_log_file_info()256 response = self.client.get(url)257 log_info_after_request = self.read_log_file_info()258 self.assertEqual(log_info_after_request["count"], log_info_before_request["count"])259 self.assertEqual(260 log_info_after_request["last_line"], 261 log_info_before_request["last_line"]262 )263 self.assertStatus(response, 200)264 self.assertIn("Location is within MKAD", str(response.data))265 self.assertContext(266 "context",267 {268 'distance': -1, 269 'success': True, 270 'longitude': '37.623429', 271 'latitude': '55.766557', 272 'name': 'Russia, Moscow, Boulevard Ring'273 }274 )275 276 def read_log_file_info(self) -> dict:277 """Return the number of lines and last line of the log file "app.log" ."""278 line_count = 0279 last_line = None...

Full Screen

Full Screen

testcases.py

Source:testcases.py Github

copy

Full Screen

...69 for unwanted in ['http://localhost:80', 'http://testserver']:70 location = location.replace(unwanted, '')71 self.assertEqual(reverse(url_name, kwargs=kwargs), location)72 def assertNoAccess(self, response):73 self.assertContext(response)74 self.assertTrue(response.status_code in (http_client.NOT_FOUND,75 http_client.FORBIDDEN))76 def assertIsOk(self, response):77 self.assertEqual(http_client.OK, response.status_code)78 def assertContext(self, response):79 self.assertTrue(response.context is not None,80 'No context was returned')81 def assertInContext(self, response, key):82 self.assertContext(response)83 self.assertTrue(key in response.context,84 "Context should contain a variable '%s'" % key)85 def assertNotInContext(self, response, key):86 self.assertContext(response)87 self.assertTrue(key not in response.context,...

Full Screen

Full Screen

test_post_preview.py

Source:test_post_preview.py Github

copy

Full Screen

...19 state = json.dumps({"snap_name": self.snap_name, "images": [], "title": self.snap_name})20 response = self.client.post(self.endpoint_url, data={"state": state})21 self.assertEqual(response.status_code, 200)22 self.assertTemplateUsed("store/snap-details.html")23 self.assertContext("snap_name", self.snap_name)24 self.assertContext("is_preview", True)25 self.assertContext("screenshots", [])26 self.assertContext("icon_url", None)27 self.assertContext("videos", [])...

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