[IMP] account_statement_import_txt_xlsx: add tests

This commit is contained in:
Jordi Ballester Alomar
2022-12-18 14:29:39 +01:00
committed by Miquel Raïch
parent 0a0d55d4db
commit 84c1823fa1
3 changed files with 69 additions and 11 deletions

View File

@@ -102,11 +102,15 @@ class AccountStatementImportSheetParser(models.TransientModel):
else:
column_names_or_indexes = [mapping[column_name]]
for column_name_or_index in column_names_or_indexes:
if not column_name_or_index:
continue
column_index = None
if mapping.no_header:
column_index = (
column_name_or_index and int(column_name_or_index) or None
)
if column_index:
try:
column_index = int(column_name_or_index)
except Exception:
pass
if column_index is not None:
column_indexes.append(column_index)
else:
if column_name_or_index:
@@ -164,6 +168,8 @@ class AccountStatementImportSheetParser(models.TransientModel):
)
decoded_file = data_file.decode(detected_encoding)
csv_or_xlsx = reader(StringIO(decoded_file), **csv_options)
header = False
if not mapping.no_header:
if isinstance(csv_or_xlsx, tuple):
header = [str(value) for value in csv_or_xlsx[1].row_values(0)]
else:
@@ -179,9 +185,11 @@ class AccountStatementImportSheetParser(models.TransientModel):
content_l = []
max_index = len(values) - 1
for index in indexes:
if isinstance(index, int) and index <= max_index:
if isinstance(index, int):
if index <= max_index:
content_l.append(values[index])
else:
if index in values:
content_l.append(values[index])
if all(isinstance(content, str) for content in content_l):
return " ".join(content_l)

View File

@@ -0,0 +1 @@
"12/15/2018","Your payment","EUR","1,525.00","-1,000.00","Azure Interior","","INV0001"
1 12/15/2018 Your payment EUR 1,525.00 -1,000.00 Azure Interior INV0001

View File

@@ -224,6 +224,55 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
self.assertEqual(line.foreign_currency_id, self.currency_eur)
self.assertEqual(line.amount_currency, 1000.0)
def test_original_currency_no_header(self):
no_header_statement_map = self.AccountStatementImportSheetMapping.create(
{
"name": "Sample Statement",
"float_thousands_sep": "comma",
"float_decimal_sep": "dot",
"delimiter": "comma",
"quotechar": '"',
"timestamp_format": "%m/%d/%Y",
"no_header": True,
"timestamp_column": "0",
"amount_column": "3",
"original_currency_column": "2",
"original_amount_column": "4",
"description_column": "1,7",
"partner_name_column": "5",
"bank_account_column": "6",
}
)
journal = self.AccountJournal.create(
{
"name": "Bank",
"type": "bank",
"code": "BANK",
"currency_id": self.currency_usd.id,
"suspense_account_id": self.suspense_account.id,
}
)
data = self._data_file("fixtures/original_currency_no_header.csv", "utf-8")
wizard = self.AccountStatementImport.with_context(journal_id=journal.id).create(
{
"statement_filename": "fixtures/original_currency.csv",
"statement_file": data,
"sheet_mapping_id": no_header_statement_map.id,
}
)
wizard.with_context(
account_statement_import_txt_xlsx_test=True
).import_file_button()
statement = self.AccountBankStatement.search([("journal_id", "=", journal.id)])
self.assertEqual(len(statement), 1)
self.assertEqual(len(statement.line_ids), 1)
line = statement.line_ids
self.assertEqual(line.currency_id, self.currency_usd)
self.assertEqual(line.foreign_currency_id, self.currency_eur)
self.assertEqual(line.amount_currency, 1000.0)
self.assertEqual(line.payment_ref, "Your payment INV0001")
def test_original_currency_empty(self):
journal = self.AccountJournal.create(
{