
    jq!                        d dl mZ d dlmZ d dlZd dlmZ d dlmZm	Z	m
Z
mZ d dlmZmZmZmZmZ d dlmZmZmZ d dlmZ d d	lmZ d d
lmZ d Zd Zd Zd Zd Z d Z!d Z"dS )    )annotations)DecimalN)HTTPException)AccountValueConfirmRequestAccountValuePreviewRequestCashSnapshotConfirmRequestCashSnapshotPreviewRequest)CANONICAL_CASH_ACCOUNTSconfirm_cash_snapshotensure_canonical_cash_accountsget_cash_summarypreview_cash_snapshot)confirm_account_valuepreview_account_valuepreview_position)get_overview)connect_memory)apply_migrationsc                 @    t                      } t          |            | S )N)r   r   )conns    -tests/unit/test_cash_truewealth_management.pydbr      s     DTK    c                 x   t                      } t          | d          }|d         dk    sJ |d         g k    sJ |                     d                                          }d |D             }t	          |          d t
          D             k    sJ |d	         d
         dk    sJ |d         d
         dk    sJ |d         d
         dk    sJ |d         d         dk    sJ t          d |D                       sJ |                     d                                          }t          |          dk    sJ d S )Ntest)
created_bycreated   
duplicatesz
        SELECT a.account_name, a.account_type, a.currency, a.balance_mode, p.name AS platform
        FROM accounts a JOIN platforms p ON p.platform_id=a.platform_id
        WHERE a.account_type='cash'
        ORDER BY a.account_name
        c                     i | ]}|d          |S account_name .0rows     r   
<dictcomp>zZtest_canonical_cash_accounts_are_present_with_expected_modes_and_audit.<locals>.<dictcomp>0   s    888Cs>"C888r   c                    h | ]
}|d          S r!   r#   r%   items     r   	<setcomp>zYtest_canonical_cash_accounts_are_present_with_expected_modes_and_audit.<locals>.<setcomp>1   s    UUUTD0UUUr   
Raiffeisenbalance_modecsv_calculatedzAKB HaushaltskontoPostFinance Cash-KontomanualplatformzPostFinance Cashc              3  .   K   | ]}|d          dk    V  dS )currencyCHFNr#   r$   s     r   	<genexpr>zYtest_canonical_cash_accounts_are_present_with_expected_modes_and_audit.<locals>.<genexpr>6   s+      88Cs:%'888888r   zESELECT action FROM audit_log WHERE action='cash_account_seed_confirm')r   r   executefetchallsetr
   alllen)r   resultrowsby_nameauditss        r   Ftest_canonical_cash_accounts_are_present_with_expected_modes_and_auditr?   !   s{   44D+DVDDDF)!!!!,2%%%%<<	  hjj 	 984888Gw<<UU=TUUUUUUU< 04DDDDD'(8<LLLLL+,^<HHHH+,Z8<NNNNN88488888888\\abbkkmmFv;;!r   c                 ~   t                      } t          |            |                     d           |                     d           |                                  t          |           }|d         dk    sJ |d         dgk    sJ |                     d                                          d         }|dk    sJ d S )	NzINSERT INTO platforms(platform_id, name, platform_type, default_currency, created_at) VALUES ('plat_pf_duplicate', 'PostFinance Duplicate', 'bank', 'CHF', '2026-01-01')zINSERT INTO accounts(account_id, platform_id, account_name, account_type, currency, balance_mode, created_at) VALUES ('acct_dup_pf_cash', 'plat_pf_duplicate', 'PostFinance Cash-Konto', 'cash', 'CHF', 'manual', '2026-01-01')r   r   r   r/   zISELECT COUNT(*) FROM accounts WHERE account_name='PostFinance Cash-Konto'   )r   r   r6   commitfetchone)r   r;   counts      r   Ntest_duplicate_cash_accounts_return_consolidation_preview_without_silent_mergerE   ;   s    44D"4(((LL  |  }  }  }LL 	j   	KKMMM+D11F)!!!!,$<#=====LLdeennppqrsEA::::::r   c                    t                      } t          |            |                     d                                          d         }t	          | t          |dddd                    }|j        dk    sJ |j        dk    sJ t          | t          |dddd|j
        d	
                    }|j        dk    sJ |j                            d          sJ t          |           }t          d |j        D                       }|j        dk    sJ |j        dk    sJ |j        dk    sJ |j        dk    sJ |j        dk    sJ |j        dk    sJ d S )NzKSELECT account_id FROM accounts WHERE account_name='PostFinance Cash-Konto'r   manual_balance
2026-05-20z1234.56Abgleich)
account_idsnapshot_typebalance_date
amount_chfnote
not_neededTrJ   rK   rL   rM   rN   
preview_idconfirm	confirmedaudit_c              3  0   K   | ]}|j         d k    |V  dS )r/   Naccount_labelr)   s     r   r5   zitest_manual_cash_snapshot_preview_confirm_audit_and_dashboard_value_for_manual_account.<locals>.<genexpr>Z   s0      ddT5GKc5c5ct5c5c5c5cddr   r0   0.00)r   r   r6   rC   r   r	   rM   	fx_statusr   r   rQ   statusaudit_id
startswithr   next	positionsr-   manual_balance_chfused_value_chfcalculated_balance_chfdifference_chflast_manual_reconciliation)r   rJ   previewrS   summaryr&   s         r   Vtest_manual_cash_snapshot_preview_confirm_audit_and_dashboard_value_for_manual_accountrf   L   s   44D"4(((klluuwwxyzJ#D*DPZjz  JV  cl  s}  +~  +~  +~    G****,,,,%d,FR\l|  LX  en  u  LS  L^  hl  -m  -m  -m  n  nI{****((22222t$$G
dd 1ddd
d
dCx''''!Y....****%////****)\999999r   c                 d   t                      } t          |            |                     d                                          d         }t	          | t          |dddddd	                     |                     d
|f           t	          | t          |dddddd	                     t          d t          |           j        D                       }|j	        dk    sJ |j
        dk    sJ |j        dk    sJ |j        dk    sJ |j        dk    sJ |j        dk    sJ |j        dk    sJ d S )Nz?SELECT account_id FROM accounts WHERE account_name='Raiffeisen'r   csv_anchor_balance
2026-05-01z1000.00StartpTrP   a  INSERT INTO transactions(transaction_id, transaction_type, account_id, trade_date, gross_amount_original, net_amount_original, currency_original, fx_rate_to_chf, fx_status, gross_amount_chf, net_amount_chf, source_type, is_confirmed, quality_status, created_at) VALUES ('tx_csv_1', 'csv_import', ?, '2026-05-05', '250.00', '250.00', 'CHF', '1', 'ok', '250.00', '250.00', 'csv_import', 1, 'ok', '2026-05-05')rG   rH   z1300.00	Kontrollec              3  0   K   | ]}|j         d k    |V  dS )r,   NrV   r)   s     r   r5   zetest_csv_calculated_account_uses_calculated_value_and_manual_as_control_difference.<locals>.<genexpr>n   s0      ggDDVZfDfDftDfDfDfDfggr   r.   z1250.00z50.00zAbgleich offen)r   r   r6   rC   r   r   r]   r   r^   r-   csv_anchor_balance_chfra   r_   r`   rb   rZ   )r   rJ   r&   s      r   Rtest_csv_calculated_account_uses_calculated_value_and_manual_as_control_differencero   c   s   44D"4(((_``iikklmnJ$ :j`t  DP  ]f  mt  AD  NR  !S  !S  !S  T  T  TLL 	b	   $ :j`p  @L  Yb  it  AD  NR  !S  !S  !S  T  T  T
gg 0 6 6 @ggg
g
gC/////%2222%2222!Y....****((((:)))))))r   c                    t                      } d}d}|                     d|f           |                     d||f           |                     d           |                                  t          | |t	          ddd	                    }|j        dk    sJ t          | |t          ddd|j        d
                    }|j	        
                    d          sJ t          |           }|j        dk    sJ |j        dk    sJ t          j        t                     5 }t#          |  t%          dd|dddddd d d d ddd                                 d d d            n# 1 swxY w Y   d|j        j        v sJ d S )Nplat_truewealthacct_truewealthzINSERT INTO platforms(platform_id, name, platform_type, default_currency, created_at) VALUES (?, 'TrueWealth', 'roboadvisor', 'CHF', '2026-01-01')zINSERT INTO accounts(account_id, platform_id, account_name, account_type, currency, created_at) VALUES (?, ?, 'TrueWealth Gesamtwert', 'managed_portfolio', 'CHF', '2026-01-01')zINSERT INTO instruments(instrument_id, asset_class, name, currency, created_at) VALUES ('inst_vti', 'etf', 'Do not allow', 'USD', '2026-01-01')rH   z55555.55Monat)valuation_datetotal_value_chfrN   T)rt   ru   rN   rQ   rR   rT   Reqr#   1USDetfinst_vtibuy)rJ   quantityr3   
trade_dateasset_classinstrument_idcandidate_idnametickerisincost_basis_originaltransaction_typeEinzelpositionen)r   r6   rB   r   r   rM   r   r   rQ   r[   r\   r   truewealth_value_chfru   pytestraisesr   r   typevaluedetail)r   platform_idrJ   rd   rS   overviewexcs          r   7test_truewealth_manual_value_history_and_position_blockr   y   s   44D#K"JLL  f  it  hv  w  w  wLL  D  GQ  S^  F_  `  `  `LL  c  d  d  dKKMMM#D*6P`l  I  PW  7X  7X  7X  Y  YG++++%dJ8Rbn  AK  RY  fm  fx  BF  9G  9G  9G  H  HI((22222D!!H(J6666#z1111	}	%	% z   wtE2jVYgl  }I  Z_  r|  NR  \`  lp  z~  WZ  pu  0v  0v   w   w   y   y  	z  	z  	zz z z z z z z z z z z z z z z!1111111s   5E  E$'E$c                 $   t                      } t          |            |                     d                                          d         }|                     d                                          d         }t	          | t          |dddddd	
                     t	          | t          |dddddd	
                     t          |           }|j        dk    sJ |j        dk    sJ t          |j
                  t          d          k    sJ d S )NzJSELECT account_id FROM accounts WHERE account_name='AKB gemeinsames Konto'r   zGSELECT account_id FROM accounts WHERE account_name='AKB Haushaltskonto'rG   rH   z2000.00 rk   TrP   rh   ri   z3000.00z5000.00rX   )r   r   r6   rC   r   r   r   cash_value_chfr   r   ru   )r   	manual_idcsv_idr   s       r   Itest_portfolio_uses_cash_balance_modes_without_double_counting_truewealthr      sh   44D"4(((ijjssuuvwxI\\cddmmoopqrF$ :i_o  K  Xa  hj  wz  DH  !I  !I  !I  J  J  J$ :f\p  @L  Yb  ik  x{  EI  !J  !J  !J  K  K  KD!!H"i////(F22228+,,	0B0BBBBBBBr   )#
__future__r   decimalr   r   fastapir   $jarvis_finance.api.schemas.positionsr   r   r   r	   $jarvis_finance.services.cash_servicer
   r   r   r   r   ,jarvis_finance.services.manual_entry_servicer   r   r   )jarvis_finance.services.portfolio_servicer   jarvis_finance.storage.databaser   !jarvis_finance.storage.migrationsr   r   r?   rE   rf   ro   r   r   r#   r   r   <module>r      s   " " " " " "        ! ! ! ! ! !                         x w w w w w w w w w B B B B B B : : : : : : > > > > > >    4  ": : :.* * *,2 2 2,C C C C Cr   