mirror of
https://github.com/OCA/bank-statement-import.git
synced 2025-01-20 12:37:43 +02:00
[IMP] account_statement_import_txt_xlsx: add tests
This commit is contained in:
committed by
Miquel Raïch
parent
0a0d55d4db
commit
84c1823fa1
@@ -102,11 +102,15 @@ class AccountStatementImportSheetParser(models.TransientModel):
|
|||||||
else:
|
else:
|
||||||
column_names_or_indexes = [mapping[column_name]]
|
column_names_or_indexes = [mapping[column_name]]
|
||||||
for column_name_or_index in column_names_or_indexes:
|
for column_name_or_index in column_names_or_indexes:
|
||||||
|
if not column_name_or_index:
|
||||||
|
continue
|
||||||
|
column_index = None
|
||||||
if mapping.no_header:
|
if mapping.no_header:
|
||||||
column_index = (
|
try:
|
||||||
column_name_or_index and int(column_name_or_index) or None
|
column_index = int(column_name_or_index)
|
||||||
)
|
except Exception:
|
||||||
if column_index:
|
pass
|
||||||
|
if column_index is not None:
|
||||||
column_indexes.append(column_index)
|
column_indexes.append(column_index)
|
||||||
else:
|
else:
|
||||||
if column_name_or_index:
|
if column_name_or_index:
|
||||||
@@ -164,10 +168,12 @@ class AccountStatementImportSheetParser(models.TransientModel):
|
|||||||
)
|
)
|
||||||
decoded_file = data_file.decode(detected_encoding)
|
decoded_file = data_file.decode(detected_encoding)
|
||||||
csv_or_xlsx = reader(StringIO(decoded_file), **csv_options)
|
csv_or_xlsx = reader(StringIO(decoded_file), **csv_options)
|
||||||
if isinstance(csv_or_xlsx, tuple):
|
header = False
|
||||||
header = [str(value) for value in csv_or_xlsx[1].row_values(0)]
|
if not mapping.no_header:
|
||||||
else:
|
if isinstance(csv_or_xlsx, tuple):
|
||||||
header = [value.strip() for value in next(csv_or_xlsx)]
|
header = [str(value) for value in csv_or_xlsx[1].row_values(0)]
|
||||||
|
else:
|
||||||
|
header = [value.strip() for value in next(csv_or_xlsx)]
|
||||||
for column_name in self._get_column_names():
|
for column_name in self._get_column_names():
|
||||||
columns[column_name] = self._get_column_indexes(
|
columns[column_name] = self._get_column_indexes(
|
||||||
header, column_name, mapping
|
header, column_name, mapping
|
||||||
@@ -179,10 +185,12 @@ class AccountStatementImportSheetParser(models.TransientModel):
|
|||||||
content_l = []
|
content_l = []
|
||||||
max_index = len(values) - 1
|
max_index = len(values) - 1
|
||||||
for index in indexes:
|
for index in indexes:
|
||||||
if isinstance(index, int) and index <= max_index:
|
if isinstance(index, int):
|
||||||
content_l.append(values[index])
|
if index <= max_index:
|
||||||
|
content_l.append(values[index])
|
||||||
else:
|
else:
|
||||||
content_l.append(values[index])
|
if index in values:
|
||||||
|
content_l.append(values[index])
|
||||||
if all(isinstance(content, str) for content in content_l):
|
if all(isinstance(content, str) for content in content_l):
|
||||||
return " ".join(content_l)
|
return " ".join(content_l)
|
||||||
return content_l[0]
|
return content_l[0]
|
||||||
|
|||||||
1
account_statement_import_txt_xlsx/tests/fixtures/original_currency_no_header.csv
vendored
Normal file
1
account_statement_import_txt_xlsx/tests/fixtures/original_currency_no_header.csv
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
"12/15/2018","Your payment","EUR","1,525.00","-1,000.00","Azure Interior","","INV0001"
|
||||||
|
@@ -224,6 +224,55 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
|
|||||||
self.assertEqual(line.foreign_currency_id, self.currency_eur)
|
self.assertEqual(line.foreign_currency_id, self.currency_eur)
|
||||||
self.assertEqual(line.amount_currency, 1000.0)
|
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):
|
def test_original_currency_empty(self):
|
||||||
journal = self.AccountJournal.create(
|
journal = self.AccountJournal.create(
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user