Source code for tests.test_context_handler

from unittest.mock import Mock, patch, mock_open

from behave.runner import Context as BehaveContext
from steps.api.utils.context import Context, CloudContextHandler
from steps.api.utils.api import Api


[docs] def test_context_initialization(): context = Context() assert context.output_dir == "" assert context.logger is None assert context.apis == {} assert context.cloud is None assert context.request is None assert context.response is None
[docs] def test_cloud_context_handler_initialization(): # Create a mock behave context behave_context = Mock(spec=BehaveContext) # Initialize the handler handler = CloudContextHandler(behave_context) # Check that the behave context is set assert handler.context == behave_context # Check that Context.__init__ was called with the behave context # This is verified by checking that the context has the expected attributes assert hasattr(behave_context, 'output_dir') assert hasattr(behave_context, 'logger') assert hasattr(behave_context, 'apis') assert hasattr(behave_context, 'cloud') assert hasattr(behave_context, 'request') assert hasattr(behave_context, 'response')
[docs] def test_load_config(): # Test data config = { 'REQUEST_TIMEOUT': 5, 'USERS_NB_TO_CREATE': 3, 'LIGHT_VERSION': True, 'PATCH_OPEN_API': True } # Create a handler and load config behave_context = Mock(spec=BehaveContext) handler = CloudContextHandler(behave_context) handler.load_config(config) # Check that config values were set assert behave_context.timeout == 5 assert behave_context.users_nb_to_create == 3 assert behave_context.light_version is True assert behave_context.patch_open_api is True
[docs] def test_load_config_defaults(): # Test data with missing keys config = { 'REQUEST_TIMEOUT': 5, # Missing USERS_NB_TO_CREATE, LIGHT_VERSION, PATCH_OPEN_API } # Create a handler and load config behave_context = Mock(spec=BehaveContext) handler = CloudContextHandler(behave_context) handler.load_config(config) # Check that config values were set with defaults assert behave_context.timeout == 5 assert behave_context.users_nb_to_create == config.get('USERS_NB_TO_CREATE') # None assert behave_context.light_version is False assert behave_context.patch_open_api is False
@patch('builtins.open', new_callable=mock_open, read_data='type;api_url;swagger_file\napi1;https://api1.example.com;swagger1.json\napi2;https://api2.example.com;swagger2.json') # noqa: E501 @patch.object(Api, 'load_swagger')
[docs] def test_load_apis(mock_load_swagger, mock_file): # Create a handler and load APIs behave_context = Mock(spec=BehaveContext) behave_context.patch_open_api = False handler = CloudContextHandler(behave_context) handler.load_apis('api_file.csv') # Check that API instances were created and stored assert len(behave_context.apis) == 2 assert 'api1' in behave_context.apis assert 'api2' in behave_context.apis # Check that load_swagger was called for each API assert mock_load_swagger.call_count == 2 # Check that the APIs were created with the correct parameters api1 = behave_context.apis['api1'] assert api1.api_type == 'api1' assert api1.url == 'https://api1.example.com' assert api1.swagger_file == 'swagger1.json' api2 = behave_context.apis['api2'] assert api2.api_type == 'api2' assert api2.url == 'https://api2.example.com' assert api2.swagger_file == 'swagger2.json'
@patch('builtins.open', new_callable=mock_open, read_data='type;api_url;swagger_file\napi1;https://api1.example.com;swagger1.json\napi2;https://api2.example.com;swagger1.json') # noqa: E501 @patch.object(Api, 'load_swagger')
[docs] def test_load_apis_with_shared_swagger(mock_load_swagger, mock_file): # Create a handler and load APIs behave_context = Mock(spec=BehaveContext) behave_context.apis = {} behave_context.patch_open_api = False handler = CloudContextHandler(behave_context) handler.load_apis('api_file.csv') # Check that API instances were created and stored assert len(behave_context.apis) == 2 assert 'api1' in behave_context.apis assert 'api2' in behave_context.apis # Check that load_swagger was called only once (for the first API) assert mock_load_swagger.call_count == 1 # Check that the APIs were created with the correct parameters api1 = behave_context.apis['api1'] assert api1.api_type == 'api1' assert api1.url == 'https://api1.example.com' assert api1.swagger_file == 'swagger1.json' # Check that the second API reuses the same reference api2 = behave_context.apis['api2'] assert api2 == api1