
    mj;                        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ZddZddZddZdS )    )annotationsN)Path)import_accounts_csv)$set_performance_scope_classification)connect_memory)apply_migrationstmp_pathr   returnNonec                
   | dz  }|                     dd           t                      }t          |           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}}|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}}|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 S )Naccounts.csvplatform_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encodingFcommitsource_filename   ==)z2%(py2)s
{%(py2)s = %(py0)s.rows_total
} == %(py5)sresultpy0py2py5assert %(py7)spy7z0%(py2)s
{%(py2)s = %(py0)s.rows_new
} == %(py5)sr   z5%(py2)s
{%(py2)s = %(py0)s.rows_existing
} == %(py5)sz3%(py2)s
{%(py2)s = %(py0)s.rows_failed
} == %(py5)s
dry_run_okz.%(py2)s
{%(py2)s = %(py0)s.status
} == %(py5)s#SELECT COUNT(*) AS n FROM platformsnz%(py1)s == %(py4)spy1py4assert %(py6)spy6)SELECT COUNT(*) AS n FROM import_sessions)
write_textr   r   r   
rows_total
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprAssertionError_format_explanationrows_newrows_existingrows_failedstatusexecutefetchoner	   pathconnr   @py_assert1@py_assert4@py_assert3@py_format6@py_format8@py_assert0@py_assert2@py_format5@py_format7s                X/home/agent/.hermes/repos/FinanceManager/tests/unit/test_importers_platforms_accounts.py+test_accounts_import_dry_run_does_not_writerJ      sQ   n$DOO	K    
 DT tE>ZZZF!!!!!!!!!!!!!!!!!!!!!!!!6!!!!!6!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!?a?a?a66?a$1$1$$$$$$$$$$1$$$$$$$$$$$$6$$$$$6$$$$$$$$$$1$$$$$$$$$$$$$$$$$$$$$$$$$$$""""""""""""""""""""""""6"""""6"""""""""""""""""""""""""""""""""""""=(L(=L((((((((((=L((((((((((((6(((((6((((((=((((L(((((((((((((((((((((((((((<<=>>GGII#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<<CDDMMOOPSTYXYYTXYYYYYYYYYYYTXYYYYYYTYYYYXYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY    c                   | dz  }|                     dd           t                      }t          |           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}}|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         }
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         }
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   r   r   r   Tr   r   r   r   firstr   r   r   r   secondr    r$   r%   r&   r'   r*   r+   "SELECT COUNT(*) AS n FROM accountsz)SELECT performance_included FROM accountsz6SELECT COUNT(*) FROM performance_scope_classificationsr,      )r-   r   r   r   r7   r/   r0   r1   r2   r3   r4   r5   r6   r8   r;   r<   )r	   r>   r?   rM   rN   r@   rA   rB   rC   rD   rE   rF   rG   rH   s                 rI   )test_accounts_import_commit_is_idempotentrQ       s   n$DOO	K    
 DTd4XXXE tD.YYYF>Q>Q>Q55>Q?a?a?a66?a$1$1$$$$$$$$$$1$$$$$$$$$$$$6$$$$$6$$$$$$$$$$1$$$$$$$$$$$$$$$$$$$$$$$$$$$<<=>>GGII#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<<<==FFHHMRQRRMQRRRRRRRRRRRMQRRRRRRMRRRRQRRRRRRRRRRRRRRRRRRRRRRRRRRRR<<CDDMMOOPQRWVWWRVWWWWWWWWWWWRVWWWWWWRWWWWVWWWWWWWWWWWWWWWWWWWWWWWWWWWW<<PQQZZ\\]^_dcdd_cddddddddddd_cdddddd_ddddcdddddddddddddddddddddddddddd<<CDDMMOOPSTYXYYTXYYYYYYYYYYYTXYYYYYYTYYYYXYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYrK   c                   | dz  }|                     dd           t                      }t          |           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}	|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_accounts.csvz`platform_name,platform_type,account_name,account_type,currency
,broker,Demo Depot,brokerage,CHF
r   r   Tr   r   r   r!   r   r   r   r   failedr#   platform_namer   )in)z%(py1)s in %(py4)sr'   r*   r+   rO   r%   r&   )r-   r   r   r   r9   r/   r0   r1   r2   r3   r4   r5   r6   r:   errorsr;   r<   r=   s                rI   /test_accounts_import_collects_validation_errorsrW   7   s   ((DOO	-    
 DT tDJ\]]]F""""""""""""""""""""""""6"""""6"""""""""""""""""""""""""""""""""""""=$H$=H$$$$$$$$$$=H$$$$$$$$$$$$6$$$$$6$$$$$$=$$$$H$$$$$$$$$$$$$$$$$$$$$$$$$$$.fmA..?...........?......?................................<<<==FFHHMRQRRMQRRRRRRRRRRRMQRRRRRRMRRRRQRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRrK   c                H	   | dz  }|                     dd           t                      }t          |           |                    d           |                    d           t	          |ddd	d
dd           t          |                    d                                                    }t          ||d|j                  }|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} |
|          }|j        } |            }t          |          }||k    }|s|t          j        d|fd||f          dt          j                    v st          j        t
                    rt          j        t
                    nddt          j                    v st          j        |          rt          j        |          ndt          j        |
          t          j        |          t          j        |          t          j        |          t          j        |          t          j        |          dt          j                    v st          j        |          rt          j        |          ndd	z  }dd|iz  }t!          t          j        |                    d x}
x}x}x}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 S )%Nzhousehold_accounts.csvzplatform_name,platform_type,account_name,account_type,currency,performance_included,is_health_reserve,notes
Household Bank,bank,Daily Cash,cash,CHF,1,0,Synthetic only
r   r   zqINSERT INTO platforms(platform_id,name,platform_type,created_at) VALUES('pf','PostFinance','broker','2026-01-01')zINSERT INTO accounts(account_id,platform_id,account_name,account_type,currency,
             performance_included,created_at)
           VALUES('pf-depot','pf','PostFinance E-Trading','brokerage','CHF',0,'2026-01-01')zpf-depotTpostfinance_etrading_depottestzapproved synthetic rolez2026-01-01T00:00:00Z)
account_idincludedclassification_rolesourcenoteclassified_atzSELECT a.performance_included,c.included,c.classification_role,c.audit_id
           FROM accounts a JOIN performance_scope_classifications c USING(account_id)
           WHERE a.account_id='pf-depot'r   	committedr   r#   r   r   r   r   )z%(py13)s
{%(py13)s = %(py0)s(%(py11)s
{%(py11)s = %(py9)s
{%(py9)s = %(py7)s
{%(py7)s = %(py3)s
{%(py3)s = %(py1)s.execute
}(%(py5)s)
}.fetchone
}()
})
} == %(py15)stupler?   before)	r   r(   py3r   r   py9py11py13py15zassert %(py17)spy17zTSELECT performance_included FROM accounts
           WHERE account_name='Daily Cash'r   r&   r'   r*   r+   zSELECT COUNT(*) FROM performance_scope_classifications c
           JOIN accounts a USING(account_id) WHERE a.account_name='Daily Cash')r-   r   r   r;   r   rb   r<   r   namer:   r/   r0   r1   r2   r3   r4   r5   r6   )r	   r>   r?   rc   r   r@   rA   rB   rC   rD   rF   @py_assert6@py_assert8@py_assert10@py_assert12@py_assert14@py_format16@py_format18	householdrE   rG   rH   s                         rI   Itest_household_account_import_cannot_change_an_existing_performance_scopers   I   s   ..DOO	G    
 DTLL{   	LL	_  
 )8&,    4<<	,  hjj	 F !tD$)TTTF='K'=K''''''''''=K''''''''''''6'''''6''''''=''''K''''''''''''''''''''''''''' 	,	,    	     5      	     	     	     	     	      	        	     	      	       	     	,   	    	    	    	     	     V	    V	                		                   
 	.  hjj  Q<1<1<1<1<<	R  hjj                      q                           rK   )r	   r   r
   r   )
__future__r   builtinsr1   _pytest.assertion.rewrite	assertionrewriter/   pathlibr   (jarvis_finance.imports.accounts_importerr   )jarvis_finance.services.performance_scoper   jarvis_finance.storage.databaser   !jarvis_finance.storage.migrationsr   rJ   rQ   rW   rs    rK   rI   <module>r      s    " " " " " "                     H H H H H H Z Z Z Z Z Z : : : : : : > > > > > >Z Z Z Z*Z Z Z Z.S S S S$0 0 0 0 0 0rK   