How to use test_invalid_body method in tavern

Best Python code snippet using tavern

test_urls.py

Source:test_urls.py Github

copy

Full Screen

1from random import choice2import pytest3from unittest.mock import patch, MagicMock4from typing import TYPE_CHECKING, List5from fastapi import status6from source.managers import ShortLinkClickManager, UserManager, URLManager7from source.models import UserModel, ShortLinkClicksModel, UrlMappingsModel8if TYPE_CHECKING:9 from fastapi.testclient import TestClient10class TestGenerateUrl:11 endpoint = '/api/urls'12 def test_generate_endpoint_integration(self, client: 'TestClient'):13 test_valid_body = {14 'target_url': 'https://www.google.com'15 }16 response = client.post(self.endpoint, json=test_valid_body)17 assert response.status_code == status.HTTP_200_OK18 assert all(19 item in response.json() for item in ['short_url', 'clicks_count']20 )21 @patch('source.api.urls.generate_short_url_based_on_hash')22 @patch.object(ShortLinkClickManager, 'handle_clicks_count_object')23 @patch.object(UserManager, 'get_or_create_user')24 @patch.object(URLManager, 'get_or_create_short_url_hash')25 def test_generate_endpoint_schema_success(26 self,27 get_or_create_short_url_hash_mock: MagicMock,28 get_or_create_user_mock: MagicMock,29 handle_clicks_count_object_mock: MagicMock,30 generate_short_url_based_on_hash_mock: MagicMock,31 generate_users: List[UserModel],32 generate_url_mapping_objects: List[UrlMappingsModel],33 generate_short_link_clicks_objects: List[ShortLinkClicksModel],34 client: 'TestClient'35 ) -> None:36 test_valid_body = {37 'target_url': 'https://www.google.com'38 }39 test_short_link = 'www.test.com/1'40 random_short_link_obj = choice(generate_short_link_clicks_objects)41 get_or_create_short_url_hash_mock.return_value = choice(42 generate_url_mapping_objects43 )44 get_or_create_user_mock.return_value = choice(get_or_create_user_mock)45 handle_clicks_count_object_mock.return_value = random_short_link_obj46 generate_short_url_based_on_hash_mock.return_value = test_short_link47 response = client.post(self.endpoint, json=test_valid_body)48 assert response.status_code == status.HTTP_200_OK49 assert response.json() == {'short_url': test_short_link,50 'clicks_count': random_short_link_obj.count}51 get_or_create_short_url_hash_mock.assert_called_once()52 get_or_create_user_mock.assert_called_once()53 handle_clicks_count_object_mock.assert_called_once()54 generate_short_url_based_on_hash_mock.assert_called_once()55 @pytest.mark.parametrize('key', ['', 'target_urlsc'])56 def test_generate_endpoint_schema_fail(self, client: 'TestClient',57 key: str) -> None:58 test_invalid_body = {59 key: 'https://www.google.com'60 }61 response = client.post(self.endpoint, json=test_invalid_body)62 assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY63class TestRedirectUrl:64 endpoint = '/api/{url_key}'65 @patch.object(ShortLinkClickManager, 'update_clicks_count')66 @patch.object(UserManager, 'get_or_create_user')67 @patch.object(URLManager, 'get_model_object')68 def test_redirect_endpoint_schema_success(69 self,70 get_model_object_mock: MagicMock,71 get_or_create_user_mock: MagicMock,72 update_clicks_count_mock: MagicMock,73 generate_users: List[UserModel],74 generate_url_mapping_objects:75 List[UrlMappingsModel],76 generate_short_link_clicks_objects:77 List[ShortLinkClicksModel],78 client: 'TestClient'79 ) -> None:80 valid_url_object = choice(generate_url_mapping_objects)81 get_model_object_mock.return_value = valid_url_object82 get_or_create_user_mock.return_value = choice(get_or_create_user_mock)83 response = client.get(84 self.endpoint.format(url_key=valid_url_object.hash_key)85 )86 assert (next(iter(response.history)).status_code ==87 status.HTTP_307_TEMPORARY_REDIRECT)88 get_model_object_mock.assert_called_once()89 get_or_create_user_mock.assert_called_once()90 update_clicks_count_mock.assert_called_once()91 @patch.object(URLManager, 'get_model_object', return_value=None)92 def test_redirect_endpoint_not_found(self,93 get_model_object_mock: MagicMock,94 client: 'TestClient') -> None:95 response = client.get(self.endpoint.format(url_key='123'))96 assert response.status_code == status.HTTP_404_NOT_FOUND97 get_model_object_mock.assert_called_once()98 def test_redirect_schema_fail(self, client: 'TestClient') -> None:99 response = client.get('/api/')100 assert response.status_code == status.HTTP_404_NOT_FOUND101class TestRenderView:102 endpoint = '/'103 def test_render_view_success(self, client: 'TestClient'):104 response = client.get(self.endpoint)105 assert response.status_code == status.HTTP_200_OK...

Full Screen

Full Screen

unit_tests.py

Source:unit_tests.py Github

copy

Full Screen

1import unittest2import app3class ApiTest(unittest.TestCase):4 def test_invalid_body(self):5 j = {6 "body" : {}7 }8 self.assertEqual(False, app.isInvalidBody(j))9 10if __name__ == "__main__":...

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 tavern 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