
    aj                        d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ ddZddZddZddZddZdS )    )annotations)Path)import_accounts_csv)import_instruments_csv)import_transactions_csv)connect_memory)apply_migrationstmp_pathr   returnNonec                    |dz  }|                     dd           |dz  }|                     dd           t          | |dd           t          | |dd           d S )	Nzaccounts.csvzplatform_name,platform_type,account_name,account_type,currency,performance_included,is_health_reserve,notes
Demo Broker,broker,Demo Depot,brokerage,CHF,1,0,Synthetic only
utf-8encodingzinstruments.csvzasset_class,name,ticker,isin,exchange,currency,country,sector,data_provider_primary,notes
equity,Demo Global AG,DGA,CH0000000001,SIX,CHF,CH,Technology,dummy,Synthetic only
Tcommitsource_filename)
write_textr   r   )connr
   accountsinstrumentss       (tests/unit/test_transactions_importer.pyseed_reference_datar      s    .(H	K    
 ..K	^    
 ht^TTTT4TK\]]]]]]    c                   t                      }t          |           t          ||            | dz  }|                    dd           t	          ||dd          }|j        dk    sJ |                    d                                          }|d	         d
k    sJ t          |d                   dk    sJ |d         dk    sJ |                    d                                          d         dk    sJ d S )Ntransactions.csva  transaction_type,platform_name,account_name,trade_date,settlement_date,name,ticker,isin,quantity,price_original,gross_amount_original,fee_original,tax_original,net_amount_original,currency_original,fx_rate_to_chf,fx_source,external_transaction_id,notes
buy,Demo Broker,Demo Depot,2026-01-15,2026-01-17,Demo Global AG,DGA,CH0000000001,10,100,1000,5,0,1005,CHF,1,not_needed,demo-txn-001,Synthetic buy
r   r   Tr      zASELECT transaction_type, quantity, is_confirmed FROM transactionstransaction_typebuyquantity
   is_confirmedzCSELECT COUNT(*) AS n FROM audit_log WHERE entity_type='transaction'n)	r   r	   r   r   r   rows_newexecutefetchonefloatr
   r   pathresulttxns        r   8test_transactions_import_validates_and_writes_with_auditr,      s%   DTh'''((DOO	^     %T4N`aaaF?a
,,Z
[
[
d
d
f
fC!"e++++Z!!R''''~!####<<]^^ggiijmnrsssssssr   c                `   t                      }t          |           t          ||            | dz  }|                    dd           t	          ||dd          }|j        dk    sJ d|j        d	         v sJ |                    d
                                          d         d	k    sJ d S )Nzbad_transactions.csvaw  transaction_type,platform_name,account_name,trade_date,name,ticker,isin,quantity,price_original,gross_amount_original,fee_original,tax_original,net_amount_original,currency_original,fx_rate_to_chf,fx_source,external_transaction_id,notes
buy,Demo Broker,Demo Depot,2026-01-15,Demo Global AG,DGA,CH0000000001,0,100,1000,5,0,1005,CHF,1,not_needed,demo-txn-002,Synthetic bad buy
r   r   Tr   r   r    r   &SELECT COUNT(*) AS n FROM transactionsr#   )	r   r	   r   r   r   rows_failederrorsr%   r&   )r
   r   r)   r*   s       r   ?test_transactions_import_rejects_buy_with_non_positive_quantityr1   2   s    DTh''',,DOO	V     %T4NdeeeF""""q)))))<<@AAJJLLSQUVVVVVVVr   c                   t                      }t          |           t          ||            | dz  }|                    dd           t	          ||dd          }|j        dk    sJ |                    d                                          }|d	         d
k    sJ |d         dk    sJ |d         dk    sJ |                    d                                          d         dk    sJ d S )Nr   a{  transaction_type,platform_name,account_name,trade_date,name,ticker,isin,quantity,price_original,gross_amount_original,fee_original,tax_original,net_amount_original,currency_original,fx_rate_to_chf,fx_source,external_transaction_id,notes
buy,Demo Broker,Demo Depot,2026-01-15,Demo Global AG,DGA,CH0000000001,1,100,100,0,0,100,USD,,manual_missing,demo-txn-003,Synthetic missing fx
r   r   Tr   r   zESELECT currency_original, fx_status, quality_status FROM transactionscurrency_originalUSD	fx_statusmissingquality_status
incompletez:SELECT COUNT(*) AS n FROM alerts WHERE priority='kritisch'r#   )r   r	   r   r   r   r$   r%   r&   r(   s        r   >test_transactions_import_marks_missing_fx_for_foreign_currencyr9   D   s   DTh'''((DOO	Z     %T4N`aaaF?a
,,^
_
_
h
h
j
jC"#u,,,,{y(((( L0000<<TUU^^``adeijjjjjjjr   c                    t                      }t          |           t          ||            | dz  }|                    dd           t	          ||dd          }t	          ||dd          }|j        dk    sJ |j        dk    sJ |                    d                                          d	         dk    sJ |                    d
                                          }|d         dk    sJ d|d         v sJ d S )Nr   a  transaction_type,platform_name,account_name,trade_date,name,ticker,isin,quantity,price_original,gross_amount_original,fee_original,tax_original,net_amount_original,currency_original,fx_rate_to_chf,fx_source,external_transaction_id,notes
initial_position_snapshot,Demo Broker,Demo Depot,2026-01-15,Demo Global AG,DGA,CH0000000001,10,,,,,,CHF,1,not_needed,demo-snap-001,Synthetic initial position
r   r   Tr   r   r.   r#   z0SELECT transaction_type, notes FROM transactionsr   initial_position_snapshotzInitial snapshotnotes)	r   r	   r   r   r   r$   rows_existingr%   r&   )r
   r   r)   firstsecondrows         r   Btest_transactions_import_is_idempotent_by_external_id_and_row_hashrA   Y   s2   DTh'''((DOO	j     $D$tM_```E$T4N`aaaF>Q1$$$$<<@AAJJLLSQUVVVVV
,,I
J
J
S
S
U
UC!"&AAAAAW------r   N)r
   r   r   r   )
__future__r   pathlibr   (jarvis_finance.imports.accounts_importerr   +jarvis_finance.imports.instruments_importerr   ,jarvis_finance.imports.transactions_importerr   jarvis_finance.storage.databaser   !jarvis_finance.storage.migrationsr	   r   r,   r1   r9   rA    r   r   <module>rJ      s   " " " " " "       H H H H H H N N N N N N P P P P P P : : : : : : > > > > > >^ ^ ^ ^"t t t t*W W W W$k k k k*. . . . . .r   