product
This commit is contained in:
118
tests/test_data_mapper.py
Normal file
118
tests/test_data_mapper.py
Normal file
@@ -0,0 +1,118 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Unit tests for data mapper module.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
from processors.data_mapper import DataMapper
|
||||
from db.models import TransactionRecord
|
||||
|
||||
|
||||
class TestDataMapper:
|
||||
"""Test DataMapper functionality."""
|
||||
|
||||
def test_convert_date_valid(self):
|
||||
"""Test date conversion with valid input."""
|
||||
result = DataMapper.convert_date('19/01/26')
|
||||
assert result == date(2026, 1, 19)
|
||||
|
||||
def test_convert_date_different_month(self):
|
||||
"""Test date conversion with different month."""
|
||||
result = DataMapper.convert_date('05/12/25')
|
||||
assert result == date(2025, 12, 5)
|
||||
|
||||
def test_convert_date_invalid(self):
|
||||
"""Test date conversion with invalid input."""
|
||||
# Should return today's date on error
|
||||
result = DataMapper.convert_date('invalid')
|
||||
assert isinstance(result, date)
|
||||
|
||||
def test_calculate_txnind_credit(self):
|
||||
"""Test TXNIND calculation for credit (positive amount)."""
|
||||
assert DataMapper.calculate_txnind('100.50') == 'CR'
|
||||
assert DataMapper.calculate_txnind('1000') == 'CR'
|
||||
assert DataMapper.calculate_txnind('0') == 'CR'
|
||||
|
||||
def test_calculate_txnind_debit(self):
|
||||
"""Test TXNIND calculation for debit (negative amount)."""
|
||||
assert DataMapper.calculate_txnind('-50.00') == 'DR'
|
||||
assert DataMapper.calculate_txnind('-100') == 'DR'
|
||||
|
||||
def test_convert_amount(self):
|
||||
"""Test amount conversion."""
|
||||
assert DataMapper.convert_amount('100.50') == Decimal('100.50')
|
||||
assert DataMapper.convert_amount('-50.00') == Decimal('50.00') # Absolute value
|
||||
assert DataMapper.convert_amount('') == Decimal('0')
|
||||
|
||||
def test_map_transaction(self):
|
||||
"""Test complete transaction mapping."""
|
||||
parsed_txn = {
|
||||
'remarks': 'Test remark',
|
||||
'sys': '23-DEP-PROCESSED',
|
||||
'jrnl_no': '12345',
|
||||
'date': '19/01/26',
|
||||
'cust_acct': '1234567890',
|
||||
'amount': '1000.00'
|
||||
}
|
||||
|
||||
result = DataMapper.map_transaction(parsed_txn, 'HDFC')
|
||||
|
||||
assert isinstance(result, TransactionRecord)
|
||||
assert result.narration == 'Test remark'
|
||||
assert result.status == '23-DEP-PROCESSED'
|
||||
assert result.bankcode == 'HDFC'
|
||||
assert result.jrnl_id == '12345'
|
||||
assert result.tran_date == date(2026, 1, 19)
|
||||
assert result.cbs_acct == '1234567890'
|
||||
assert result.tran_amt == Decimal('1000.00')
|
||||
assert result.txnind == 'CR'
|
||||
|
||||
def test_map_transaction_with_negative_amount(self):
|
||||
"""Test transaction mapping with negative amount."""
|
||||
parsed_txn = {
|
||||
'remarks': 'Debit transaction',
|
||||
'sys': '23-DEP-PROCESSED',
|
||||
'jrnl_no': '54321',
|
||||
'date': '05/12/25',
|
||||
'cust_acct': '9876543210',
|
||||
'amount': '-500.50'
|
||||
}
|
||||
|
||||
result = DataMapper.map_transaction(parsed_txn, 'ICICI')
|
||||
|
||||
assert result.tran_amt == Decimal('500.50') # Absolute value
|
||||
assert result.txnind == 'DR'
|
||||
|
||||
def test_map_transactions(self):
|
||||
"""Test mapping multiple transactions."""
|
||||
parsed_txns = [
|
||||
{
|
||||
'remarks': 'Transaction 1',
|
||||
'sys': '23-DEP-PROCESSED',
|
||||
'jrnl_no': '001',
|
||||
'date': '19/01/26',
|
||||
'cust_acct': '1001',
|
||||
'amount': '100.00'
|
||||
},
|
||||
{
|
||||
'remarks': 'Transaction 2',
|
||||
'sys': '23-DEP-PROCESSED',
|
||||
'jrnl_no': '002',
|
||||
'date': '19/01/26',
|
||||
'cust_acct': '1002',
|
||||
'amount': '200.00'
|
||||
}
|
||||
]
|
||||
|
||||
results = DataMapper.map_transactions(parsed_txns, 'HDFC')
|
||||
|
||||
assert len(results) == 2
|
||||
assert all(isinstance(r, TransactionRecord) for r in results)
|
||||
assert results[0].jrnl_id == '001'
|
||||
assert results[1].jrnl_id == '002'
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pytest.main([__file__, '-v'])
|
||||
Reference in New Issue
Block a user