Best Python code snippet using autotest_python
test_api_views.py
Source:test_api_views.py
...49 f'/api/v1/moderation/report-video/{video.id}/', body, format='json'50 )51 assert response.status_code == BAD_REQUEST52 assert response.json() == exp_json53 def test_throttling(self, api_client_no_auth, video):54 api_client = api_client_no_auth55 max_reports_per_day = 1056 body = {57 'reason': 'porn',58 'body': 'it is porn',59 }60 for _ in range(max_reports_per_day + 1):61 response = api_client.post(62 f'/api/v1/moderation/report-video/{video.id}/',63 body,64 format='json',65 )66 assert response.status_code == TOO_MANY_REQUESTS67class TestVideoCommentReportAPIView:68 @pytest.fixture69 def video_comment(self, video, video_comment_factory):70 return video_comment_factory(video_id=video.id)71 def test_post(self, api_client_no_auth, video_comment):72 api_client = api_client_no_auth73 body = {74 'reason': 'porn',75 'body': 'it is porn',76 }77 response = api_client.post(78 f'/api/v1/moderation/report-video-comment/{video_comment.id}/',79 body,80 format='json',81 )82 assert response.status_code == CREATED83 @pytest.mark.parametrize(84 'body, exp_json',85 [86 (87 {88 'reason': None,89 'body': 'it is porn',90 },91 {'reason': ['This field may not be null.']},92 ),93 (94 {95 'reason': None,96 'body': 'x' * 5001,97 },98 {99 'reason': ['This field may not be null.'],100 'body': [101 'Ensure this field has no more than 500 characters.'102 ],103 },104 ),105 ],106 )107 def test_invalid_post_returns_400(108 self, api_client_no_auth, video_comment, body, exp_json109 ):110 api_client = api_client_no_auth111 response = api_client.post(112 f'/api/v1/moderation/report-video-comment/{video_comment.id}/',113 body,114 format='json',115 )116 assert response.status_code == BAD_REQUEST117 assert response.json() == exp_json118 def test_throttling(self, api_client_no_auth, video_comment):119 api_client = api_client_no_auth120 max_reports_per_day = 10121 body = {122 'reason': 'porn',123 'body': 'it is porn',124 }125 for _ in range(max_reports_per_day + 1):126 response = api_client.post(127 f'/api/v1/moderation/report-video-comment/{video_comment.id}/',128 body,129 format='json',130 )...
tests.py
Source:tests.py
...19 expected_response = {"detail": "This address doesn't exists"}20 response = self.client.post(url, data, format='json')21 self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)22 self.assertEqual(json.loads(response.content), expected_response)23 def test_throttling(self):24 url = reverse('address-to-geocode')25 _ = self.client.post(url, {}, format='json')26 response = self.client.post(url, {}, format='json')27 self.assertEqual(response.status_code, status.HTTP_429_TOO_MANY_REQUESTS)28class GeocodeToAddressTests(APITestCase):29 def setUp(self):30 cache.clear()31 def test_correct_geocode(self):32 url = reverse('geocode-to-address')33 data = {"latlng": [37.4218651, -122.0846744]}34 expected_response = {"latlng": [37.4218651, -122.0846744], "address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA"}35 response = self.client.post(url, data, format='json')36 self.assertEqual(response.status_code, status.HTTP_200_OK)37 self.assertEqual(json.loads(response.content), expected_response)38 def test_incorrect_geocode(self):39 url = reverse('geocode-to-address')40 data = {"latlng": [91, 0]}41 expected_response = {"detail": "Coords are not within the world's geographical boundary"}42 response = self.client.post(url, data, format='json')43 self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)44 self.assertEqual(json.loads(response.content), expected_response)45 def test_throttling(self):46 url = reverse('geocode-to-address')47 _ = self.client.post(url, {}, format='json')48 response = self.client.post(url, {}, format='json')49 self.assertEqual(response.status_code, status.HTTP_429_TOO_MANY_REQUESTS)50class CalcDistanceTests(APITestCase):51 def setUp(self):52 cache.clear()53 def test_simple_correct_coordinates(self):54 url = reverse('calc-distance')55 data = {"coordinates": [{"latlng": [0, 0]}, {"latlng": [0, 0]}]}56 expected_response = {"distance": 0}57 response = self.client.post(url, data, format='json')58 self.assertEqual(response.status_code, status.HTTP_200_OK)59 self.assertEqual(json.loads(response.content), expected_response)60 def test_correct_coordinates(self):61 url = reverse('calc-distance')62 data = {"coordinates": [{"latlng": [37.4218651, -122.0846744]}, {"latlng": [51.5216889, -0.1260132]}]}63 expected_response = {"distance": 8654.68964661197}64 response = self.client.post(url, data, format='json')65 self.assertEqual(response.status_code, status.HTTP_200_OK)66 self.assertEqual(json.loads(response.content), expected_response)67 def test_incorrect_geocode(self):68 url = reverse('calc-distance')69 data = {"coordinates": [{"latlng": [91, 0]}, {"latlng": [-91, 0]}]}70 expected_response = {"detail": "Latitude must be in the [-90; 90] range."}71 response = self.client.post(url, data, format='json')72 self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)73 self.assertEqual(json.loads(response.content), expected_response)74 def test_throttling(self):75 url = reverse('calc-distance')76 _ = self.client.post(url, {}, format='json')77 response = self.client.post(url, {}, format='json')...
urls.py
Source:urls.py
1from django.conf.urls import url2from api.test import views3urlpatterns = [4 url(r'^throttle/', views.test_throttling, name='test-throttling'),...
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!!