|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch, MagicMock, ANY |
| 3 | +import importlib |
| 4 | +import sys |
| 5 | +import os |
| 6 | + |
| 7 | +class TestCibaSandbox(unittest.TestCase): |
| 8 | + def __init__(self, methodName='runTest', module_path=None): |
| 9 | + super(TestCibaSandbox, self).__init__(methodName) |
| 10 | + self.module_path = module_path |
| 11 | + |
| 12 | + @patch('opengateway_sandbox_sdk.Simswap') |
| 13 | + def test_simswap(self, MockSimswap): |
| 14 | + mock_simswap_client = MockSimswap.return_value |
| 15 | + mock_simswap_client.retrieve_date.return_value = MagicMock(strftime=lambda x: "December 23, 2024, 09:49:42 AM") |
| 16 | + |
| 17 | + # Importar el script después de aplicar los mocks |
| 18 | + module = importlib.import_module(self.module_path + 'CIBA_Sandbox_SDK_for_Python') |
| 19 | + |
| 20 | + # Verificar que los métodos fueron llamados correctamente |
| 21 | + MockSimswap.assert_called_once_with( |
| 22 | + 'your_client_id', |
| 23 | + 'your_client_secret', |
| 24 | + '+34555555555' |
| 25 | + ) |
| 26 | + mock_simswap_client.retrieve_date.assert_called_once() |
| 27 | + |
| 28 | + # Verificar la salida del script |
| 29 | + with patch('builtins.print') as mocked_print: |
| 30 | + importlib.reload(module) |
| 31 | + mocked_print.assert_called_once_with("SIM was swapped: December 23, 2024, 09:49:42 AM") |
| 32 | + |
| 33 | + @patch('opengateway_sandbox_sdk.DeviceLocation') |
| 34 | + @patch('opengateway_sandbox_sdk.ClientCredentials') |
| 35 | + def test_devicelocation(self, MockClientCredentials, MockDeviceLocation): |
| 36 | + mock_credentials = MockClientCredentials.return_value |
| 37 | + mock_devicelocation_client = MockDeviceLocation.return_value |
| 38 | + mock_devicelocation_client.verify.return_value = True |
| 39 | + |
| 40 | + module =importlib.import_module(self.module_path + 'CIBA_Sandbox_SDK_for_Python') |
| 41 | + |
| 42 | + # Verificar que los métodos fueron llamados correctamente |
| 43 | + MockClientCredentials.assert_called_once_with( |
| 44 | + client_id = 'your_client_id', |
| 45 | + client_secret = 'your_client_secret' |
| 46 | + ) |
| 47 | + MockDeviceLocation.assert_called_once_with( |
| 48 | + credentials=mock_credentials, |
| 49 | + phone_number=ANY |
| 50 | + ) |
| 51 | + mock_devicelocation_client.verify.assert_called_once_with(ANY, ANY, ANY, ANY) |
| 52 | + |
| 53 | + # Verificar la salida del script |
| 54 | + with patch('builtins.print') as mocked_print: |
| 55 | + importlib.reload(module) |
| 56 | + mocked_print.assert_called_once_with("Is the device in location? True") |
| 57 | + |
| 58 | +if __name__ == '__main__': |
| 59 | + base_dir = sys.argv[1] if len(sys.argv) > 1 else 'tmp/py' |
| 60 | + |
| 61 | + if not os.path.exists(base_dir): |
| 62 | + print(f"Error: La ruta especificada '{base_dir}' no existe.") |
| 63 | + sys.exit(1) |
| 64 | + |
| 65 | + suite = unittest.TestSuite() |
| 66 | + |
| 67 | + for api in os.listdir(base_dir): |
| 68 | + api_dir = os.path.join(base_dir, api) |
| 69 | + script_path = os.path.join(api_dir, 'CIBA_Sandbox_SDK_for_Python.py') |
| 70 | + |
| 71 | + if os.path.isfile(script_path): |
| 72 | + module_path = base_dir.replace('/', '.') + '.' + api + '.' |
| 73 | + TestCibaSandbox.script_path = script_path |
| 74 | + |
| 75 | + suite.addTest(TestCibaSandbox('test_' + api, module_path=module_path)) |
| 76 | + |
| 77 | + runner = unittest.TextTestRunner() |
| 78 | + runner.run(suite) |
0 commit comments