package Ġio . nova foundation . nova . core _ db . dao Ċ Ċ import Ġandroidx . test . ext . junit . runners . Android JUnit 4 Ċ import Ġio . nova foundation . nova . common . utils . Collection Differ Ċ import Ġio . nova foundation . nova . core _ db . App Database Ċ import Ġio . nova foundation . nova . core _ db . model . chain . Joined Chain Info Ċ import Ġkotlinx . coroutines . run Blocking Ċ import Ġorg . junit . Assert . assertEquals Ċ import Ġorg . junit . Assert . assertNotNull Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ Ċ @ RunWith ( Android JUnit 4 :: class ) Ċ class ĠChain Dao Test Ġ: ĠDao Test < Chain Dao >( App Database :: chain Dao ) Ġ{ ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Insert Whole Chain () Ġ= Ġrun Blocking Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġchain Info Ġ= Ġcreate Test Chain (" 0 x 0 0 ") ĊĊĠĠĠĠĠĠĠ Ġdao . add Chain ( chain Info ) ĊĊĠĠĠĠĠĠĠ Ġval Ġchains From Db Ġ= Ġdao . get Join Chain Info () ĊĊĠĠĠĠĠĠĠ ĠassertEquals ( 1 , Ġchains From Db . size ) ĊĊĠĠĠĠĠĠĠ Ġval Ġchain From Db Ġ= Ġchains From Db . first () ĊĊĠĠĠĠĠĠĠ ĠassertEquals ( chain Info . assets . size , Ġchain From Db . assets . size ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( chain Info . nodes . size , Ġchain From Db . nodes . size ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Delete Chain With Cascade () Ġ= Ġrun Blocking Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġchain Info Ġ= Ġcreate Test Chain (" 0 x 0 0 ") ĊĊĠĠĠĠĠĠĠ Ġdao . add Chain ( chain Info ) ĊĠĠĠĠĠĠĠ Ġdao . remove Chain ( chain Info ) ĊĊĠĠĠĠĠĠĠ Ġval Ġassets Cursor Ġ= Ġdb . query (" SELECT Ġ* ĠFROM Ġchain _ assets ", Ġempty Array ()) ĊĠĠĠĠĠĠĠ ĠassertEquals ( 0 , Ġassets Cursor . count ) ĊĊĠĠĠĠĠĠĠ Ġval Ġnodes Cursor Ġ= Ġdb . query (" SELECT Ġ* ĠFROM Ġchain _ nodes ", Ġempty Array ()) ĊĠĠĠĠĠĠĠ ĠassertEquals ( 0 , Ġnodes Cursor . count ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Not Delete Runtime Cache Entry After Chain Update () Ġ= Ġrun Blocking Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġchain Info Ġ= Ġcreate Test Chain (" 0 x 0 0 ") ĊĊĠĠĠĠĠĠĠ Ġdao . add Chain ( chain Info ) ĊĠĠĠĠĠĠĠ Ġdao . update Remote Runtime Version If Chain Exists ( chain Info . chain . id , Ġremote Version Ġ= Ġ 1 ) ĊĊĠĠĠĠĠĠĠ Ġdao . update Chain ( chain Info ) ĊĊĠĠĠĠĠĠĠ Ġval Ġruntime Entry Ġ= Ġdao . runtime Info ( chain Info . chain . id ) ĊĊĠĠĠĠĠĠĠ ĠassertNotNull ( runtime Entry ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Delete Removed Nested Fields () Ġ= Ġrun Blocking Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġchain Info Ġ= Ġcreate Test Chain (" 0 x 0 0 ", Ġnodes Count Ġ= Ġ 3 , Ġassets Count Ġ= Ġ 3 ) ĊĊĠĠĠĠĠĠĠ Ġdao . add Chain ( chain Info ) ĊĊĠĠĠĠĠĠĠ Ġdao . apply Diff ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġchain Diff Ġ= Ġupdated Diff ( chain Info . chain ), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġassets Diff Ġ= ĠCollection Differ . Diff ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġadded Ġ= Ġempty List (), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġupdated Ġ= Ġempty List (), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġremoved Ġ= Ġchain Info . assets . take Last ( 1 ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġnodes Diff Ġ= ĠCollection Differ . Diff ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġadded Ġ= Ġempty List (), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġupdated Ġ= Ġempty List (), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġremoved Ġ= Ġchain Info . nodes . take Last ( 1 ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġexplor ers Diff Ġ= Ġempty Diff () ĊĠĠĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠĠĠ Ġval Ġchain From Db 2 Ġ= Ġdao . get Join Chain Info (). first () ĊĊĠĠĠĠĠĠĠ ĠassertEquals ( 2 , Ġchain From Db 2 . nodes . size ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( 2 , Ġchain From Db 2 . assets . size ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Update () Ġ= Ġrun Blocking Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġto Be Removed Ġ= ĠlistOf ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcreate Test Chain (" to Ġbe Ġremoved Ġ 1 "), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcreate Test Chain (" to Ġbe Ġremoved Ġ 2 "), ĊĠĠĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠĠĠ Ġval Ġstay The Same Ġ= ĠlistOf ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcreate Test Chain (" st ay Ġthe Ġsame ") ĊĠĠĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠĠĠ Ġval Ġchains Initial Ġ= ĠlistOf ( create Test Chain (" to Ġbe Ġchanged ")) Ġ+ Ġstay The Same Ġ+ Ġto Be Removed ĊĊĠĠĠĠĠĠĠ Ġdao . add Ch ains ( chains Initial ) ĊĊĠĠĠĠĠĠĠ Ġval Ġadded Ġ= ĠlistOf ( create Test Chain (" to Ġbe Ġadded ")) ĊĠĠĠĠĠĠĠ Ġval Ġupdated Ġ= ĠlistOf ( create Test Chain (" to Ġbe Ġchanged ", Ġ" new Ġname ")) ĊĊĠĠĠĠĠĠĠ Ġval Ġexpected Result Ġ= Ġstay The Same Ġ+ Ġadded Ġ+ Ġupdated ĊĊĠĠĠĠĠĠĠ Ġdao . apply Diff ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġchain Diff Ġ= ĠCollection Differ . Diff ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġadded Ġ= Ġadded . map ( Joined Chain Info :: chain ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġupdated Ġ= Ġupdated . map ( Joined Chain Info :: chain ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġremoved Ġ= Ġto Be Removed . map ( Joined Chain Info :: chain ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġassets Diff Ġ= Ġempty Diff (), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġnodes Diff Ġ= Ġempty Diff (), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġexplor ers Diff Ġ= Ġempty Diff () ĊĠĠĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠĠĠ Ġval Ġchains From Db Ġ= Ġdao . get Join Chain Info () ĊĊĠĠĠĠĠĠĠ ĠassertEquals ( expected Result . size , Ġchains From Db . size ) ĊĠĠĠĠĠĠĠ Ġexpected Result . forEach Ġ{ Ġexpected Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġtry Find Ġ= Ġchains From Db . first OrNull Ġ{ Ġactual Ġ-> Ġexpected . chain . id Ġ== Ġactual . chain . id Ġ&& Ġexpected . chain . name Ġ== Ġactual . chain . name Ġ} ĊĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertNotNull (" Did Ġnot Ġfind Ġ${ expected . chain . id } Ġin Ġresult Ġset ", Ġtry Find ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Update Runtime Versions () Ġ{ ĊĠĠĠĠĠĠĠ Ġrun Blocking Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġchain Id Ġ= Ġ" 0 x 0 0 " ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġdao . add Chain ( create Test Chain ( chain Id )) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġdao . update Remote Runtime Version If Chain Exists ( chain Id , Ġ 1 ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġcheck Runtime Versions ( remote Ġ= Ġ 1 , Ġsynced Ġ= Ġ 0 ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġdao . update Sync ed Runtime Version ( chain Id , Ġ 1 ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġcheck Runtime Versions ( remote Ġ= Ġ 1 , Ġsynced Ġ= Ġ 1 ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġdao . update Remote Runtime Version If Chain Exists ( chain Id , Ġ 2 ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġcheck Runtime Versions ( remote Ġ= Ġ 2 , Ġsynced Ġ= Ġ 1 ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġsuspend Ġfun Ġcheck Runtime Versions ( remote : ĠInt , Ġsynced : ĠInt ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġruntime Info Ġ= Ġdao . runtime Info (" 0 x 0 0 ") ĊĊĠĠĠĠĠĠĠ Ġrequire NotNull ( runtime Info ) ĊĊĠĠĠĠĠĠĠ ĠassertEquals ( runtime Info . remote Version , Ġremote ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( runtime Info . sync ed Version , Ġsynced ) ĊĠĠĠ Ġ} Ċ } Ċ