diff --git a/tests/test_data.py b/tests/test_data.py index b964922..d6963f3 100644 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -1,93 +1,156 @@ -import datetime +import unittest +from pain001.core.core import process_files +from pain001.csv.load_csv_data import load_csv_data +from pain001.csv.validate_csv_data import validate_csv_data -def validate_csv_data(data): - """Validate the CSV data before processing it. +class TestLoadCsvData(unittest.TestCase): + def test_load_valid_csv(self): + file_path = "tests/data/valid_data.csv" + data = load_csv_data(file_path) + self.assertEqual(len(data), 2) + self.assertEqual(data[0]["id"], "1") - Args: - data (list): A list of dictionaries containing the CSV data. + def test_load_empty_csv(self): + file_path = "tests/data/empty.csv" + with self.assertRaises(ValueError): + load_csv_data(file_path) - Returns: - bool: True if the data is valid, False otherwise. - """ - required_columns = { - "id": int, - "date": datetime.datetime, - "nb_of_txs": int, - "ctrl_sum": float, - "initiator_name": str, - "payment_information_id": str, - "payment_method": str, - "batch_booking": bool, - "service_level_code": str, - "requested_execution_date": datetime.datetime, - "debtor_name": str, - "debtor_account_IBAN": str, - "debtor_agent_BIC": str, - "forwarding_agent_BIC": str, - "charge_bearer": str, - "payment_id": str, - "payment_amount": float, - "currency": str, - "creditor_agent_BIC": str, - "creditor_name": str, - "creditor_account_IBAN": str, - "remittance_information": str, - } + def test_load_csv_with_invalid_data(self): + file_path = "tests/data/invalid_data.csv" + data = load_csv_data(file_path) + self.assertFalse(validate_csv_data(data)) - if not data: - print("Error: The CSV data is empty.") - return False + def test_load_single_row_csv(self): + file_path = "tests/data/single_row.csv" + data = load_csv_data(file_path) + self.assertEqual(len(data), 1) + self.assertEqual(data[0]["id"], "1") - is_valid = True + def test_load_single_column_csv(self): + file_path = "tests/data/single_column.csv" + data = load_csv_data(file_path) + self.assertEqual(len(data), 3) + self.assertEqual(data[0]["id"], "1") - for row in data: - missing_columns = [] - invalid_columns = [] - for column, data_type in required_columns.items(): - value = row.get(column) - if value is None or value.strip() == "": - missing_columns.append(column) - is_valid = False - else: - try: - if data_type == int: - int(value) - elif data_type == float: - float(value) - elif data_type == bool: - if value.strip().lower() not in [ - "true", - "false", - ]: - raise ValueError - elif data_type == datetime.datetime: - try: - if value.endswith("Z"): - value = value[:-1] + "+00:00" - datetime.datetime.fromisoformat(value) - except ValueError: - datetime.datetime.strptime( - value, "%Y-%m-%d" - ) - else: - str(value) - except ValueError: - invalid_columns.append(column) - is_valid = False - if missing_columns: - print( - f"Error: Missing value(s) for column(s) {missing_columns} " - f"in row: {row}" - ) - if invalid_columns: - expected_types = [ - required_columns[col].__name__ - for col in invalid_columns - ] - print( - f"Error: Invalid data type for column(s) {invalid_columns}, " - f"expected {expected_types} in row: {row}" - ) - return is_valid +class TestProcessFiles(unittest.TestCase): + def test_successful_execution(self): + process_files( + "pain.001.001.03", + "tests/data/template.xml", + "tests/data/template.xsd", + "tests/data/template.csv", + ) + + def test_valid_xml_message_type(self): + process_files( + "pain.001.001.03", + "tests/data/template.xml", + "tests/data/template.xsd", + "tests/data/template.csv", + ) + + +class TestValidateCsvData(unittest.TestCase): + def test_validate_csv_with_valid_data(self): + data = [ + { + "id": "1", + "date": "2023-03-10T15:30:47.000Z", + "nb_of_txs": "2", + "initiator_name": "John Doe", + "initiator_street_name": "John's Street", + "initiator_building_number": "1", + "initiator_postal_code": "12345", + "initiator_town_name": "John's Town", + "initiator_country_code": "DE", + "payment_information_id": "Payment-Info-12345", + "payment_method": "TRF", + "batch_booking": "true", + "requested_execution_date": "2023-03-12", + "debtor_name": "Acme Corp", + "debtor_street_name": "Acme Street", + "debtor_building_number": "2", + "debtor_postal_code": "67890", + "debtor_town_name": "Acme Town", + "debtor_country_code": "DE", + "debtor_account_IBAN": "DE75512108001245126162", + "debtor_agent_BIC": "BANKDEFFXXX", + "charge_bearer": "SLEV", + "payment_id": "PaymentID6789", + "payment_amount": "150", + "currency": "EUR", + "payment_currency": "EUR", + "ctrl_sum": "15000", + "creditor_agent_BIC": "SPUEDE2UXXX", + "creditor_name": "Global Tech", + "creditor_street_name": "Global Street", + "creditor_building_number": "3", + "creditor_postal_code": "11223", + "creditor_town_name": "Global Town", + "creditor_country_code": "DE", + "creditor_account_IBAN": "DE68210501700024690959", + "purpose_code": "OTHR", + "reference_number": "Invoice-98765", + "reference_date": "2023-03-09", + "service_level_code": "SEPA", + "forwarding_agent_BIC": "SPUEDE2UXXX", + "remittance_information": "Invoice-12345", + "charge_account_IBAN": "CHARGE-IBAN-12345", + } + ] + self.assertTrue(validate_csv_data(data)) + + def test_validate_csv_with_invalid_data(self): + data = [ + { + "id": "1", + "date": "not-a-date", + "nb_of_txs": "2", + "initiator_name": "John Doe", + "initiator_street_name": "John's Street", + "initiator_building_number": "1", + "initiator_postal_code": "12345", + "initiator_town_name": "John's Town", + "initiator_country_code": "DE", + "payment_information_id": "Payment-Info-12345", + "payment_method": "TRF", + "batch_booking": "true", + "requested_execution_date": "2023-03-12", + "debtor_name": "Acme Corp", + "debtor_street_name": "Acme Street", + "debtor_building_number": "2", + "debtor_postal_code": "67890", + "debtor_town_name": "Acme Town", + "debtor_country_code": "DE", + "debtor_account_IBAN": "DE75512108001245126162", + "debtor_agent_BIC": "BANKDEFFXXX", + "charge_bearer": "SLEV", + "payment_id": "PaymentID6789", + "payment_amount": "150", + "currency": "EUR", + "payment_currency": "EUR", + "ctrl_sum": "15000", + "creditor_agent_BIC": "SPUEDE2UXXX", + "creditor_name": "Global Tech", + "creditor_street_name": "Global Street", + "creditor_building_number": "3", + "creditor_postal_code": "11223", + "creditor_town_name": "Global Town", + "creditor_country_code": "DE", + "creditor_account_IBAN": "DE68210501700024690959", + "purpose_code": "OTHR", + "reference_number": "Invoice-98765", + "reference_date": "2023-03-09", + "service_level_code": "SEPA", + "forwarding_agent_BIC": "SPUEDE2UXXX", + "remittance_information": "Invoice-12345", + "charge_account_IBAN": "CHARGE-IBAN-12345", + } + ] + self.assertFalse(validate_csv_data(data)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_validate_csv_data.py b/tests/test_validate_csv_data.py index 1b473be..3975d14 100644 --- a/tests/test_validate_csv_data.py +++ b/tests/test_validate_csv_data.py @@ -1,260 +1,106 @@ import unittest from pain001.csv.validate_csv_data import validate_csv_data -# Test if the CSV data is validated correctly - class TestValidateCsvData(unittest.TestCase): - def test_valid_data(self): - """ - Test the validate_csv_data function with valid CSV data. - - Args: - self (TestValidateCsvData): - The instance of the TestValidateCsvData class. - - Returns: - None - """ - # Test valid data + def test_validate_csv_with_valid_data(self): data = [ { "id": "1", - "date": "2022-01-01", - "nb_of_txs": "1", + "date": "2023-03-10T15:30:47.000Z", + "nb_of_txs": "2", "initiator_name": "John Doe", - "payment_information_id": "12345", + "initiator_street_name": "John's Street", + "initiator_building_number": "1", + "initiator_postal_code": "12345", + "initiator_town_name": "John's Town", + "initiator_country_code": "DE", + "payment_information_id": "Payment-Info-12345", "payment_method": "TRF", - "batch_booking": "false", - "ctrl_sum": "100", - "service_level_code": "SEPA", - "requested_execution_date": "2022-01-01", - "debtor_name": "John Doe", - "debtor_account_IBAN": "DE89370400440532013000", - "debtor_agent_BIC": "DEUTDEDBFRA", - "forwarding_agent_BIC": "FORWARD", - "charge_bearer": "SHA", - "payment_id": "12345", - "payment_amount": "100.00", + "batch_booking": "true", + "requested_execution_date": "2023-03-12", + "debtor_name": "Acme Corp", + "debtor_street_name": "Acme Street", + "debtor_building_number": "2", + "debtor_postal_code": "67890", + "debtor_town_name": "Acme Town", + "debtor_country_code": "DE", + "debtor_account_IBAN": "DE75512108001245126162", + "debtor_agent_BIC": "BANKDEFFXXX", + "charge_bearer": "SLEV", + "payment_id": "PaymentID6789", + "payment_amount": "150", "currency": "EUR", - "creditor_agent_BIC": "DABADEHHXXX", - "creditor_name": "Jane Doe", - "creditor_account_IBAN": "DE89370400440532013001", - "remittance_information": "Invoice 1234", - }, - { - "id": "2", - "date": "2022-01-02", - "nb_of_txs": "1", - "initiator_name": "Jane Doe", - "payment_information_id": "67890", - "payment_method": "TRF", - "batch_booking": "false", - "ctrl_sum": "200", + "payment_currency": "EUR", + "ctrl_sum": "15000", + "creditor_agent_BIC": "SPUEDE2UXXX", + "creditor_name": "Global Tech", + "creditor_street_name": "Global Street", + "creditor_building_number": "3", + "creditor_postal_code": "11223", + "creditor_town_name": "Global Town", + "creditor_country_code": "DE", + "creditor_account_IBAN": "DE68210501700024690959", + "purpose_code": "OTHR", + "reference_number": "Invoice-98765", + "reference_date": "2023-03-09", "service_level_code": "SEPA", - "requested_execution_date": "2022-01-02", - "debtor_name": "Jane Doe", - "debtor_account_IBAN": "DE89370400440532013001", - "debtor_agent_BIC": "DEUTDEDBFRA", - "forwarding_agent_BIC": "FORWARD2", - "charge_bearer": "SHA", - "payment_id": "67890", - "payment_amount": "200.00", - "currency": "EUR", - "creditor_agent_BIC": "DABADEHHXXX", - "creditor_name": "John Doe", - "creditor_account_IBAN": "DE89370400440532013000", - "remittance_information": "Invoice 5678", - }, + "forwarding_agent_BIC": "SPUEDE2UXXX", + "remittance_information": "Invoice-12345", + "charge_account_IBAN": "CHARGE-IBAN-12345", + } ] + self.assertTrue(validate_csv_data(data)) - assert validate_csv_data(data) is True - - def test_missing_required_columns(self): - """ - Test the validate_csv_data function with CSV data missing - required columns. - - Args: - self (TestValidateCsvData): - The instance of the TestValidateCsvData class. - - Returns: - None - """ - # Test missing required columns + def test_validate_csv_with_invalid_data(self): data = [ { "id": "1", - "date": "2022-01-01", - "nb_of_txs": "1", - # "initiator_name": "John Doe", - "payment_information_id": "12345", - "payment_method": "TRF", - "batch_booking": "false", - "ctrl_sum": "100", - "service_level_code": "SEPA", - "requested_execution_date": "2022-01-01", - "debtor_name": "John Doe", - "debtor_account_IBAN": "DE89370400440532013000", - "debtor_agent_BIC": "DEUTDEDBFRA", - "forwarding_agent_BIC": "FORWARD", - "charge_bearer": "SHA", - "payment_id": "12345", - "payment_amount": "100.00", - "currency": "EUR", - "creditor_agent_BIC": "DABADEHHXXX", - "creditor_name": "Jane Doe", - "creditor_account_IBAN": "DE89370400440532013001", - "remittance_information": "Invoice 1234", - }, - { - "id": "2", - "date": "2022-01-02", - "nb_of_txs": "1", - "initiator_name": "Jane Doe", - "payment_information_id": "67890", - "payment_method": "TRF", - "batch_booking": "false", - "ctrl_sum": "200", - "service_level_code": "SEPA", - "requested_execution_date": "2022-01-02", - "debtor_name": "Jane Doe", - "debtor_account_IBAN": "DE89370400440532013001", - "debtor_agent_BIC": "DEUTDEDBFRA", - "forwarding_agent_BIC": "FORWARD", - "charge_bearer": "SHA", - "payment_id": "67890", - "payment_amount": "200.00", - "currency": "EUR", - "creditor_agent_BIC": "DABADEHHXXX", - "creditor_name": "John Doe", - "creditor_account_IBAN": "DE89370400440532013000", - "remittance_information": "Invoice 5678", - }, - ] - - assert not validate_csv_data(data) - - def test_invalid_data_types(self): - """ - Test the validate_csv_data function with CSV data containing - invalid data types. - - Args: - self (TestValidateCsvData): - The instance of the TestValidateCsvData class. - - Returns: - None - """ - # Test invalid data types - data = [ - { - "id": "1", - "date": "2022-01-01", - "nb_of_txs": "one", # Invalid data type + "date": "not-a-date", + "nb_of_txs": "2", "initiator_name": "John Doe", - "payment_information_id": "12345", + "initiator_street_name": "John's Street", + "initiator_building_number": "1", + "initiator_postal_code": "12345", + "initiator_town_name": "John's Town", + "initiator_country_code": "DE", + "payment_information_id": "Payment-Info-12345", "payment_method": "TRF", - "batch_booking": "false", - "ctrl_sum": "100", - "service_level_code": "SEPA", - "requested_execution_date": "2022-01-01", - "debtor_name": "John Doe", - "debtor_account_IBAN": "DE89370400440532013000", - "debtor_agent_BIC": "DEUTDEDBFRA", - "forwarding_agent_BIC": "FORWARD", - "charge_bearer": "SHA", - "payment_id": "12345", - "payment_amount": "100.00", + "batch_booking": "true", + "requested_execution_date": "2023-03-12", + "debtor_name": "Acme Corp", + "debtor_street_name": "Acme Street", + "debtor_building_number": "2", + "debtor_postal_code": "67890", + "debtor_town_name": "Acme Town", + "debtor_country_code": "DE", + "debtor_account_IBAN": "DE75512108001245126162", + "debtor_agent_BIC": "BANKDEFFXXX", + "charge_bearer": "SLEV", + "payment_id": "PaymentID6789", + "payment_amount": "150", "currency": "EUR", - "creditor_agent_BIC": "DABADEHHXXX", - "creditor_name": "Jane Doe", - "creditor_account_IBAN": "DE89370400440532013001", - "remittance_information": "Invoice 1234", - }, - { - "id": "2", - "date": "2022-01-02", - "nb_of_txs": "1", - "initiator_name": "Jane Doe", - "payment_information_id": "67890", - "payment_method": "TRF", - "batch_booking": "false", - "ctrl_sum": "two hundred", # Invalid data type + "payment_currency": "EUR", + "ctrl_sum": "15000", + "creditor_agent_BIC": "SPUEDE2UXXX", + "creditor_name": "Global Tech", + "creditor_street_name": "Global Street", + "creditor_building_number": "3", + "creditor_postal_code": "11223", + "creditor_town_name": "Global Town", + "creditor_country_code": "DE", + "creditor_account_IBAN": "DE68210501700024690959", + "purpose_code": "OTHR", + "reference_number": "Invoice-98765", + "reference_date": "2023-03-09", "service_level_code": "SEPA", - "requested_execution_date": "2022-01-02", - "debtor_name": "Jane Doe", - "debtor_account_IBAN": "DE89370400440532013001", - "debtor_agent_BIC": "DEUTDEDBFRA", - "forwarding_agent_BIC": "FORWARD", - "charge_bearer": "SHA", - "payment_id": "67890", - "payment_amount": "200.00", - "currency": "EUR", - "creditor_agent_BIC": "DABADEHHXXX", - "creditor_name": "John Doe", - "creditor_account_IBAN": "DE89370400440532013000", - "remittance_information": "Invoice 5678", - }, - ] - - assert not validate_csv_data(data) - - def test_empty_csv(self): - """ - Test the validate_csv_data function with an empty CSV data list. - - Args: - self (TestValidateCsvData): - The instance of the TestValidateCsvData class. - - Returns: - None - """ - # Test empty CSV data - data = [] - - assert not validate_csv_data(data) - - def test_single_row_csv(self): - """ - Test the validate_csv_data function with a single row CSV data list. - - Args: - self (TestValidateCsvData): - The instance of the TestValidateCsvData class. - - Returns: - None - """ - # Test single row CSV data - data = [ - { - "id": "1", - "date": "2022-01-01", - "nb_of_txs": "1", - "initiator_name": "John Doe", - "payment_information_id": "12345", - "payment_method": "TRF", - "batch_booking": "false", - "ctrl_sum": "100", - "service_level_code": "SEPA", - "requested_execution_date": "2022-01-01", - "debtor_name": "John Doe", - "debtor_account_IBAN": "DE89370400440532013000", - "debtor_agent_BIC": "DEUTDEDBFRA", - "forwarding_agent_BIC": "FORWARD", - "charge_bearer": "SHA", - "payment_id": "12345", - "payment_amount": "100.00", - "currency": "EUR", - "creditor_agent_BIC": "DABADEHHXXX", - "creditor_name": "Jane Doe", - "creditor_account_IBAN": "DE89370400440532013001", - "remittance_information": "Invoice 1234", + "forwarding_agent_BIC": "SPUEDE2UXXX", + "remittance_information": "Invoice-12345", + "charge_account_IBAN": "CHARGE-IBAN-12345", } ] + self.assertFalse(validate_csv_data(data)) + - assert validate_csv_data(data) is True +if __name__ == "__main__": + unittest.main()