
    Yaj%                        d dl mZ d dlZd dlmc mZ d dlm	Z	 d dl
Z
d dlmZ d dlmZ d dlmZmZmZ d dlmZ d dlmZ dddZddZddZddZddZddZdS )    )annotationsN)Path)main)create_crypto_asset)backup_runtime_dbrestore_runtime_dbverify_backup)connect)apply_migrationsdemodb_pathr   markerstrreturnNonec                    t          |           }t          |           t          |d| |                                d d         d|            |                                 d S )Nz
Synthetic    z
synthetic-	coin_namesymbolcoingecko_id)r
   r   r   upperclose)r   r   conns      \/home/agent/.hermes/worktrees/FinanceManager-test-baseline/tests/unit/test_runtime_backup.py_seed_dbr      sl    7DT(=V(=(=fllnnUWVWUWFXg|tzg|g|}}}}JJLLLLL    tmp_pathc                f   | dz  }|                                  | dz  }|dz  dz  }|dz  }t          |           t          |||d          }|j        }|j        } |            }|sdd	t          j                    v st          j        |          rt          j	        |          nd	t          j	        |          t          j	        |          t          j	        |          d
z  }	t          t          j        |	                    d x}x}}|j        }|j        } |            }|sdd	t          j                    v st          j        |          rt          j	        |          nd	t          j	        |          t          j	        |          t          j	        |          d
z  }	t          t          j        |	                    d x}x}}|j        }|j        }|j        }
 |
            }||k    }|st          j        d|fd||f          d	t          j                    v st          j        |          rt          j	        |          nd	t          j	        |          t          j	        |          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}x}x}
}|j        }|j        } |            }|j        }
|j        } |            } |
|          }| }|s(dd	t          j                    v st          j        |          rt          j	        |          nd	t          j	        |          t          j	        |          t          j	        |          t          j	        |
          dt          j                    v st          j        |          rt          j	        |          ndt          j	        |          t          j	        |          t          j	        |          d	z  }t          t          j        |                    d x}x}x}x}
x}x}x}}|j        }t%          |          }d}
||
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	        |
          dz  }dd|iz  }t          t          j        |                    d x}x}x}}
d S )Nreporuntimedatafinance.sqlite3backups20260101T000000Z)r   backups_dir	repo_root	timestampzbassert %(py6)s
{%(py6)s = %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.backup_file
}.exists
}()
}result)py0py2py4py6zdassert %(py6)s
{%(py6)s = %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.checksum_file
}.exists
}()
}==)z%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.backup_file
}.parent
} == %(py10)s
{%(py10)s = %(py8)s
{%(py8)s = %(py6)s.resolve
}()
})r*   r+   r,   r-   py8py10zassert %(py12)spy12zassert not %(py15)s
{%(py15)s = %(py8)s
{%(py8)s = %(py6)s
{%(py6)s = %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.backup_file
}.resolve
}()
}.is_relative_to
}(%(py13)s
{%(py13)s = %(py11)s
{%(py11)s = %(py9)s.resolve
}()
})
})	r*   r+   r,   r-   r0   py9py11py13py15@   )zL%(py5)s
{%(py5)s = %(py0)s(%(py3)s
{%(py3)s = %(py1)s.sha256
})
} == %(py8)slen)r*   py1py3py5r0   assert %(py10)sr1   )mkdirr   r   backup_fileexists@py_builtinslocals
@pytest_ar_should_repr_global_name	_safereprAssertionError_format_explanationchecksum_fileparentresolve_call_reprcompareis_relative_tosha256r8   )r   r    r!   dbr$   r)   @py_assert1@py_assert3@py_assert5@py_format7@py_assert7@py_assert9@py_format11@py_format13@py_assert10@py_assert12@py_assert14@py_assert16@py_format17@py_assert2@py_assert4@py_assert6@py_format9s                          r   2test_backup_runtime_db_creates_backup_outside_repor_      s$   fDJJLLL"G	6	-	-B	!GRLLLrw$ZlmmmF&$&$$&&&&&&&&&&&&&&&6&&&&&6&&&&&&&&&&$&&&&&&&&&&&&&&&&&&&&&&&&&&(&(&&(((((((((((((((6(((((6((((((((((&((((((((((((((((((((((((((9$99(9(99$(99999999999$(999999999999969999969999999999$99999999999999999999999999(99999999999999999999999999999999!J!)J))++J+:J4<J<<>>J::>JJJJJJJJJJJJJJJJJJvJJJJJv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J4JJJJJ4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JJJJJJJJJJJJJJJJJJ}#3}########################3#####3#############v#####v######}#######################################r   c                   | dz  }|                                  | dz  }|dz  dz  }t          |           t          ||dz  |          }t          |j        |          }|j        }d}||u }|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}||u }|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                            |j                                        dz              t          |j        |          }|j        }d}||u }|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 S )Nr    r!   r"   r#   r$   r   r&   r'   )r>   r'   Tis)z.%(py2)s
{%(py2)s = %(py0)s.exists
} is %(py5)sverifiedr*   r+   r;   assert %(py7)spy7)z3%(py2)s
{%(py2)s = %(py0)s.checksum_ok
} is %(py5)ss   tamperFtampered)r=   r   r   r	   r>   r?   rB   rJ   r@   rA   rC   rD   rE   rF   checksum_okwrite_bytes
read_bytes)r   r    r!   rM   backuprd   rN   r\   rO   @py_format6@py_format8rh   s               r   "test_verify_backup_checks_checksumro   &   sR   fDJJLLL"G	6	-	-BRLLLrw7JVZ[[[F);tLLLH?"d"?d""""""""""?d""""""""""""8"""""8""""""?""""d"""""""""""""""""""""""""""'4'4''''''''''4''''''''''''8'''''8''''''''''4'''''''''''''''''''''''''''
""6#5#@#@#B#BY#NOOO);tLLLH(5(5((((((((((5((((((((((((8(((((8((((((((((5(((((((((((((((((((((((((((((r   c                `   | dz  }|                                  | dz  }|dz  dz  }t          |d           t          ||dz  |          }t          |          }|                    d	           |                                 |                                 t          j        t                    5  t          |j        ||d
           d d d            n# 1 swxY w Y   t          |j        ||d          }|j        }d}||u }	|	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}	}t          |          }|                    d                                          d         }|                                 d}||k    }|st          j        d|fd||f          dt          j                    v st          j        |          rt          j        |          ndt          j        |          dz  }dd|iz  }
t'          t          j        |
                    d x}}d S )Nr    r!   r"   r#   beforer   r$   ra   DELETE FROM crypto_assetsFr>   r   r'   yesTrb   )z0%(py2)s
{%(py2)s = %(py0)s.verified
} is %(py5)sr)   re   rf   rg   'SELECT COUNT(*) AS n FROM crypto_assetsn   r.   z%(py0)s == %(py3)scountr*   r:   assert %(py5)sr;   )r=   r   r   r
   executecommitr   pytestraisesPermissionErrorr   r>   rd   rB   rJ   r@   rA   rC   rD   rE   rF   fetchone)r   r    r!   rM   rl   r   r)   rN   r\   rO   rm   rn   rz   r[   @py_format4s                  r   9test_restore_runtime_db_requires_yes_and_restores_test_dbr   7   s&   fDJJLLL"G	6	-	-BR!!!!rw7JVZ[[[F2;;DLL,---KKMMMJJLLL		'	' b bv'92QU[`aaaab b b b b b b b b b b b b b b  F,>VZ`deeeF?"d"?d""""""""""?d""""""""""""6"""""6""""""?""""d"""""""""""""""""""""""""""2;;DLLBCCLLNNsSEJJLLL5A:5A55As   2CCCc                d   | dz  }|                                  | dz  }|dz  dz  }|dz  dz  }t          |d           t          ||d	z  |
          }t          |          }t	          |ddd           |                                 t          |j        ||d          }|j        }|j	        }	 |	            }
||
k    }|st          j        d|fd||
f          dt          j                    v st          j        |          rt          j        |          ndt          j        |          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}x}	}
t          |          }t          |          }|                    d                                          d         }|                    d                                          d         }|                                 |                                 d}||k    }|st          j        d|fd||f          dt          j                    v st          j        |          rt          j        |          ndt          j        |          dz  }dd|iz  }t!          t          j        |                    d x}}d }||k    }|st          j        d|fd||f          d!t          j                    v st          j        |          rt          j        |          nd!t          j        |          dz  }dd|iz  }t!          t          j        |                    d x}}d S )"Nr    r!   r"   r#   zrestore-testszfinance-restored.sqlite3prodrr   r$   ra   zSynthetic After BackupAFTERzsynthetic-afterr   Trt   r.   )zg%(py2)s
{%(py2)s = %(py0)s.restored_to
} == %(py8)s
{%(py8)s = %(py6)s
{%(py6)s = %(py4)s.resolve
}()
}r)   
restore_db)r*   r+   r,   r-   r0   r<   r1   rv   rw      ry   
prod_countr{   r|   r;   rx   restored_count)r=   r   r   r
   r   r   r   r>   restored_torI   rB   rJ   r@   rA   rC   rD   rE   rF   r}   r   )r   r    r!   productive_dbr   rl   r   r)   rN   rP   rR   rO   r^   rT   	prod_connrestored_connr   r   r[   r   rm   s                        r   Etest_restore_backup_to_separate_test_db_does_not_modify_productive_dbr   O   s   fDJJLLL"Gf$'88M?*-GGJ]6****}'IBUaefffF=!!D(@_pqqqqJJLLLF,>
^bhlmmmF5!35!3!3!5!55!55555555555!555555555555565555565555555555555555555555555555!35555!555555555555555555555555555555&&IJ''M""#LMMVVXXY\]J"**+TUU^^``adeNOO:?:::>Q>Q>>Qr   c                   |dz  }|                      dt          |                     |dz  dz  }t          |           t          dg          }|                                j        }d}||k    }|st          j        d|fd||f          d	t          j	                    v st          j
        |          rt          j        |          nd	t          j        |          d
z  }	dd|	iz  }
t          t          j        |
                    d x}}d}||v }|st          j        d|fd||f          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}}t          d |                                D                       }d}||z  }t          |          }||v }|s0t          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        |          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}}t          dd|g          }|                                j        }d}||k    }|st          j        d|fd||f          dt          j	                    v st          j
        |          rt          j        |          ndt          j        |          d
z  }	dd|	iz  }
t          t          j        |
                    d x}}d}||v }|st          j        d|fd||f          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}}t!          |          }|                    d           |                                 |                                 t          d d!|d"g          }|                                j        }d}||k    }|st          j        d|fd||f          d#t          j	                    v st          j
        |          rt          j        |          nd#t          j        |          d
z  }	dd|	iz  }
t          t          j        |
                    d x}}d$}||v }|st          j        d|fd||f          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}}t!          |          }|                    d&                                          d'         }|                                 d(}||k    }|st          j        d|fd||f          d)t          j	                    v st          j
        |          rt          j        |          nd)t          j        |          d
z  }	dd|	iz  }
t          t          j        |
                    d x}}d S )*Nr!   JARVIS_FINANCE_RUNTIME_DIRr"   r#   zbackup-runtime-dbr   r.   ry   	backup_rcr{   r|   r;   RUNTIME_DB_BACKUP)in)z%(py1)s in %(py3)s
backup_out)r9   r:   c              3  x   K   | ]5}|                     d           |                    dd          d         V  6dS )zbackup_file==rx   N)
startswithsplit).0parts     r   	<genexpr>z5test_backup_cli_verify_and_restore.<locals>.<genexpr>s   sG      oot_mOnOnotzz#q))!,oooooor   r$   )z<%(py6)s
{%(py6)s = %(py0)s((%(py1)s / %(py3)s))
} in %(py8)sr   r>   )r*   r9   r:   r-   r0   r<   r1   zverify-backupz--file	verify_rczchecksum_ok=true
verify_outrs   zrestore-runtime-dbz--fromz--yes
restore_rcRUNTIME_DB_RESTORErestore_outrv   rw   rx   rz   )setenvr   r   r   
readouterroutrB   rJ   r@   rA   rC   rD   rE   rF   nextr   r
   r}   r~   r   r   )monkeypatchr   capsysr!   rM   r   r   r[   rN   r   rm   @py_assert0r>   r\   rP   rR   r^   rT   r   r   r   r   r   rz   s                           r   "test_backup_cli_verify_and_restorer   i   s   "G3S\\BBB	6	-	-BRLLL)*++I""$$(J9>999,*,,,,,,,,,,*,,,,,,,,,,,,,,,,*,,,,,*,,,,,,,,,,,,,,,,,,,,,,,,,,,oo9I9I9K9KoooooK"2w"23"##2#{2222222222#{22222222222232222232222222222222w22222w2222222222#22222222222{22222{2222222222222222222222222222222ox=>>I""$$(J9>999+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++2;;DLL,---KKMMMJJLLL+X{GLMMJ##%%)K:?:::.;..........;................;.....;...........................2;;DLLBCCLLNNsSEJJLLL5A:5A55Ar   )r   )r   r   r   r   r   r   )r   r   r   r   )
__future__r   builtinsr@   _pytest.assertion.rewrite	assertionrewriterB   pathlibr   r   jarvis_finance.cli.mainr   jarvis_finance.crypto.assetsr   jarvis_finance.runtime.backupr   r   r	   jarvis_finance.storage.databaser
   !jarvis_finance.storage.migrationsr   r   r_   ro   r   r   r    r   r   <module>r      s:   " " " " " "                      ( ( ( ( ( ( < < < < < < ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 3 3 3 3 3 3 > > > > > >    $ $ $ $") ) ) )"   0   4     r   