
    Lrdj                        d dl mZ d dlZd dlmc 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 )    )annotationsN)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       z/home/agent/.hermes/worktrees/FinanceManager-sprint7-postfinance-portfolio-source/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    }|st          j        d|fd	||f          d
t          j	                    v st          j
        |          rt          j        |          nd
t          j        |          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}x}}|                    d                                          }	|	d         }
d}|
|k    }|slt          j        d|fd|
|f          t          j        |
          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}
x}}|	d         }t!          |          }d}||k    }|st          j        d|fd||f          dt          j	                    v st          j
        t                     rt          j        t                     ndt          j        |          t          j        |          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}x}x}}|	d         }
d}|
|k    }|slt          j        d|fd|
|f          t          j        |
          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}
x}}|                    d                                          d         }
d}|
|k    }|slt          j        d|fd|
|f          t          j        |
          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}
x}}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      ==z0%(py2)s
{%(py2)s = %(py0)s.rows_new
} == %(py5)sresultpy0py2py5assert %(py7)spy7zASELECT transaction_type, quantity, is_confirmed FROM transactionstransaction_typebuyz%(py1)s == %(py4)spy1py4assert %(py6)spy6quantity
   )z0%(py4)s
{%(py4)s = %(py0)s(%(py2)s)
} == %(py7)sfloat)r#   r$   r-   r'   zassert %(py9)spy9is_confirmedzCSELECT COUNT(*) AS n FROM audit_log WHERE entity_type='transaction'n)r   r	   r   r   r   rows_new
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprAssertionError_format_explanationexecutefetchoner2   )r
   r   pathr!   @py_assert1@py_assert4@py_assert3@py_format6@py_format8txn@py_assert0@py_assert2@py_format5@py_format7@py_assert6@py_assert5@py_format10s                    r   8test_transactions_import_validates_and_writes_with_auditrO      s   DTh'''((DOO	^     %T4N`aaaF?a?a?a66?a
,,Z
[
[
d
d
f
fC!"+e+"e++++++++++"e+++++"++++e+++++++++++++++++++++++++++Z'5!!'R'!R''''''''''!R''''''''''''5'''''5''''''''''!''''R'''''''''''''''''''''''''''''~#!#!##########!#########!###########################<<]^^ggiijmnsrssnrsssssssssssnrssssssnssssrssssssssssssssssssssssssssssssr   c                   t                      }t          |           t          ||            | dz  }|                    dd           t	          ||dd          }|j        }d}||k    }|st          j        d|fd	||f          d
t          j	                    v st          j
        |          rt          j        |          nd
t          j        |          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}x}}d}	|j        d         }|	|v }
|
slt          j        d|
fd|	|f          t          j        |	          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}	x}
}|                    d                                          d         }	d}|	|k    }
|
slt          j        d|
fd|	|f          t          j        |	          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}	x}
}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   )z3%(py2)s
{%(py2)s = %(py0)s.rows_failed
} == %(py5)sr!   r"   r&   r'   r0   r   inz%(py1)s in %(py4)sr+   r.   r/   &SELECT COUNT(*) AS n FROM transactionsr5   r*   )r   r	   r   r   r   rows_failedr7   r8   r9   r:   r;   r<   r=   r>   errorsr?   r@   )r
   r   rA   r!   rB   rC   rD   rE   rF   rH   rI   rJ   rK   s                r   ?test_transactions_import_rejects_buy_with_non_positive_quantityrW   2   s   DTh''',,DOO	V     %T4NdeeeF""""""""""""""""""""""""6"""""6""""""""""""""""""""""""""""""""""""")q)):))))))))))):)))))):))))))))))))))))))))))))))))))))<<@AAJJLLSQVUVVQUVVVVVVVVVVVQUVVVVVVQVVVVUVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVr   c                   t                      }t          |           t          ||            | dz  }|                    dd           t	          ||dd          }|j        }d}||k    }|st          j        d|fd	||f          d
t          j	                    v st          j
        |          rt          j        |          nd
t          j        |          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}x}}|                    d                                          }	|	d         }
d}|
|k    }|slt          j        d|fd|
|f          t          j        |
          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}
x}}|	d         }
d}|
|k    }|slt          j        d|fd|
|f          t          j        |
          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}
x}}|	d         }
d}|
|k    }|slt          j        d|fd|
|f          t          j        |
          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}
x}}|                    d                                          d         }
d}|
|k    }|slt          j        d|fd|
|f          t          j        |
          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}
x}}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   r   r    r!   r"   r&   r'   zESELECT currency_original, fx_status, quality_status FROM transactionscurrency_originalUSDr*   r+   r.   r/   	fx_statusmissingquality_status
incompletez:SELECT COUNT(*) AS n FROM alerts WHERE priority='kritisch'r5   )r   r	   r   r   r   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   )r
   r   rA   r!   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   s                 r   >test_transactions_import_marks_missing_fx_for_foreign_currencyr_   D   s8   DTh'''((DOO	Z     %T4N`aaaF?a?a?a66?a
,,^
_
_
h
h
j
jC"#,u,#u,,,,,,,,,,#u,,,,,#,,,,u,,,,,,,,,,,,,,,,,,,,,,,,,,,{(y(y((((((((((y(((((((((y((((((((((((((((((((((((((( 0L0 L0000000000 L00000 0000L000000000000000000000000000<<TUU^^``adejijjeijjjjjjjjjjjeijjjjjjejjjjijjjjjjjjjjjjjjjjjjjjjjjjjjjjjjr   c                   t                      }t          |           t          ||            | dz  }|                    dd           t	          ||dd          }t	          ||dd          }|j        }d}||k    }|st          j        d|fd	||f          d
t          j	                    v st          j
        |          rt          j        |          nd
t          j        |          t          j        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}}|j        }d}||k    }|st          j        d|fd||f          dt          j	                    v st          j
        |          rt          j        |          ndt          j        |          t          j        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}}|                    d                                          d         }
d}|
|k    }|slt          j        d|fd|
|f          t          j        |
          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}
x}}|                    d                                          }|d         }
d}|
|k    }|slt          j        d|fd|
|f          t          j        |
          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}
x}}d}
|d         }|
|v }|slt          j        d|fd|
|f          t          j        |
          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}
x}}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    firstr"   r&   r'   )z5%(py2)s
{%(py2)s = %(py0)s.rows_existing
} == %(py5)ssecondrT   r5   r*   r+   r.   r/   z0SELECT transaction_type, notes FROM transactionsr(   initial_position_snapshotzInitial snapshotnotesrQ   rS   )r   r	   r   r   r   r6   r7   r8   r9   r:   r;   r<   r=   r>   rows_existingr?   r@   )r
   r   rA   ra   rb   rB   rC   rD   rE   rF   rH   rI   rJ   rK   rows                  r   Btest_transactions_import_is_idempotent_by_external_id_and_row_hashrg   Y   s   DTh'''((DOO	j     $D$tM_```E$T4N`aaaF>Q>Q>Q55>Q$1$1$$$$$$$$$$1$$$$$$$$$$$$6$$$$$6$$$$$$$$$$1$$$$$$$$$$$$$$$$$$$$$$$$$$$<<@AAJJLLSQVUVVQUVVVVVVVVVVVQUVVVVVVQVVVVUVVVVVVVVVVVVVVVVVVVVVVVVVVVV
,,I
J
J
S
S
U
UC!"A&AA"&AAAAAAAAAAA"&AAAAAA"AAAA&AAAAAAAAAAAAAAAAAAAAAAAAAAAA-W-------------------------------------------------r   )r
   r   r   r   )
__future__r   builtinsr9   _pytest.assertion.rewrite	assertionrewriter7   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   rO   rW   r_   rg    r   r   <module>rt      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   