From afab409a3bd3fa38b3e2f58334fe3dd3c7092568 Mon Sep 17 00:00:00 2001 From: adrian-wojcik Date: Mon, 18 Mar 2024 13:52:52 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20Add=20and=20update=20test=20to?= =?UTF-8?q?=20sap=5Frfc=20after=20change=20in=20credential=20passing=20log?= =?UTF-8?q?ic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/integration/tasks/test_sap_rfc_to_df.py | 47 ++++++++++- tests/integration/test_sap_rfc.py | 83 ++++++++++++++++++- 2 files changed, 123 insertions(+), 7 deletions(-) diff --git a/tests/integration/tasks/test_sap_rfc_to_df.py b/tests/integration/tasks/test_sap_rfc_to_df.py index 4151adfdc..7007a978c 100644 --- a/tests/integration/tasks/test_sap_rfc_to_df.py +++ b/tests/integration/tasks/test_sap_rfc_to_df.py @@ -1,13 +1,52 @@ +import pytest +import logging + +from viadot.exceptions import CredentialError from viadot.config import local_config from viadot.tasks import SAPRFCToDF def test_sap_rfc_to_df_bbp(): - sap_test_creds = local_config.get("SAP").get("QA") task = SAPRFCToDF( - credentials=sap_test_creds, - query="SELECT MATNR, MATKL, MTART, LAEDA FROM MARA WHERE LAEDA LIKE '2022%'", + query="SELECT MATNR, MATKL, MTART, LAEDA FROM MARA WHERE LAEDA LIKE '20220110%'", func="BBP_RFC_READ_TABLE", ) - df = task.run() + df = task.run(sap_credentials_key="SAP", env="QA") assert len(df.columns) == 4 and not df.empty + + +def test_sap_rfc_to_df_wrong_sap_credential_key_bbp(caplog): + task = SAPRFCToDF( + query="SELECT MATNR, MATKL, MTART, LAEDA FROM MARA WHERE LAEDA LIKE '20220110%'", + func="BBP_RFC_READ_TABLE", + ) + with pytest.raises( + CredentialError, + match="Sap_credentials_key: SAP_test is not stored neither in KeyVault or Local Config!", + ): + task.run( + sap_credentials_key="SAP_test", + ) + assert ( + f"Getting credentials from Azure Key Vault was not possible. Either there is no key: SAP_test or env: DEV or there is not Key Vault in your environment." + in caplog.text + ) + + +def test_sap_rfc_to_df_wrong_env_bbp(caplog): + task = SAPRFCToDF( + query="SELECT MATNR, MATKL, MTART, LAEDA FROM MARA WHERE LAEDA LIKE '20220110%'", + func="BBP_RFC_READ_TABLE", + ) + with pytest.raises( + CredentialError, + match="Missing PROD_test credentials!", + ): + task.run( + sap_credentials_key="SAP", + env="PROD_test", + ) + assert ( + f"Getting credentials from Azure Key Vault was not possible. Either there is no key: SAP or env: PROD_test or there is not Key Vault in your environment." + in caplog.text + ) diff --git a/tests/integration/test_sap_rfc.py b/tests/integration/test_sap_rfc.py index 28ab044a2..842263fa8 100644 --- a/tests/integration/test_sap_rfc.py +++ b/tests/integration/test_sap_rfc.py @@ -1,6 +1,9 @@ -from collections import OrderedDict +import pytest +import logging +from collections import OrderedDict from viadot.sources import SAPRFC, SAPRFCV2 +from viadot.exceptions import CredentialError sap = SAPRFC() sap2 = SAPRFCV2() @@ -192,7 +195,7 @@ def test___build_pandas_filter_query_v2(): def test_default_credentials_warning_SAPRFC(caplog): _ = SAPRFC() assert ( - "Your credentials will use DEV environment. If you would like to use different one - please specified it." + f"Your credentials will use DEV environment from local config. If you would like to use different one - please specified it in sap_credentials parameter" in caplog.text ) @@ -200,6 +203,80 @@ def test_default_credentials_warning_SAPRFC(caplog): def test_default_credentials_warning_SAPRFCV2(caplog): _ = SAPRFCV2() assert ( - "Your credentials will use DEV environment. If you would like to use different one - please specified it." + f"Your credentials will use DEV environment from local config. If you would like to use different one - please specified it in sap_credentials parameter" + in caplog.text + ) + + +def test_credentials_dictionary_wrong_key_warning_SAPRFC(caplog): + _ = SAPRFC( + sap_credentials={ + "sysnr_test": "01", + "user": "AIA_RFC_USER", + "passwd": "gXy3!Tk06", + "ashost": "pep1v.velux.org", + } + ) + assert ( + f"Required key 'sysnr' not found in your 'sap_credentials' dictionary!" + in caplog.text + ) + assert ( + f"Your credentials will use DEV environment from local config. If you would like to use different one - please specified it in sap_credentials parameter" in caplog.text ) + + +def test_credentials_dictionary_wrong_key_warning_SAPRFCV2(caplog): + _ = SAPRFCV2( + sap_credentials={ + "sysnr_test": "01", + "user": "AIA_RFC_USER", + "passwd": "gXy3!Tk06", + "ashost": "pep1v.velux.org", + } + ) + assert ( + f"Required key 'sysnr' not found in your 'sap_credentials' dictionary!" + in caplog.text + ) + assert ( + f"Your credentials will use DEV environment from local config. If you would like to use different one - please specified it in sap_credentials parameter" + in caplog.text + ) + + +def test_sap_credentials_key_wrong_value_error_SAPRFC(caplog): + with pytest.raises( + CredentialError, + match="Sap_credentials_key: SAP_test is not stored neither in KeyVault or Local Config!", + ): + with caplog.at_level(logging.ERROR): + _ = SAPRFC(sap_credentials_key="SAP_test") + + +def test_sap_credentials_key_wrong_value_error_SAPRFCV2(caplog): + with pytest.raises( + CredentialError, + match="Sap_credentials_key: SAP_test is not stored neither in KeyVault or Local Config!", + ): + with caplog.at_level(logging.ERROR): + _ = SAPRFC(sap_credentials_key="SAP_test") + + +def test_env_wrong_value_error_SAPRFC(caplog): + with pytest.raises( + CredentialError, + match="Missing PROD_test credentials!", + ): + with caplog.at_level(logging.ERROR): + _ = SAPRFC(env="PROD_test") + + +def test_env_wrong_value_error_SAPRFCV2(caplog): + with pytest.raises( + CredentialError, + match="Missing PROD_test credentials!", + ): + with caplog.at_level(logging.ERROR): + _ = SAPRFC(env="PROD_test")