package Ġtest Ċ Ċ import ĠId CacheKey Generator Ċ import Ġcom . ap ol log raph ql . apollo 3 . A pol lo Client Ċ import Ġcom . ap ol log raph ql . apollo 3 . api . Optional Ċ import Ġcom . ap ol log raph ql . apollo 3 . cache . normalized . A pol lo Store Ċ import Ġcom . ap ol log raph ql . apollo 3 . cache . normalized . Fetch Policy Ċ import Ġcom . ap ol log raph ql . apollo 3 . cache . normalized . api . Memory Cache Factory Ċ import Ġcom . ap ol log raph ql . apollo 3 . cache . normalized . fetch Policy Ċ import Ġcom . ap ol log raph ql . apollo 3 . cache . normalized . optim istic Updates Ċ import Ġcom . ap ol log raph ql . apollo 3 . cache . normalized . ref etch Policy Ċ import Ġcom . ap ol log raph ql . apollo 3 . cache . normalized . store Ċ import Ġcom . ap ol log raph ql . apollo 3 . cache . normalized . watch Ċ import Ġcom . ap ol log raph ql . apollo 3 . integration . normal izer . Hero And Friends Names Query Ċ import Ġcom . ap ol log raph ql . apollo 3 . integration . normal izer . Hero And Friends Names With IDs Query Ċ import Ġcom . ap ol log raph ql . apollo 3 . integration . normal izer . Hero Name With Id Query Ċ import Ġcom . ap ol log raph ql . apollo 3 . integration . normal izer . Reviews By Episode Query Ċ import Ġcom . ap ol log raph ql . apollo 3 . integration . normal izer . Update Review Mutation Ċ import Ġcom . ap ol log raph ql . apollo 3 . integration . normal izer . type . Color Input Ċ import Ġcom . ap ol log raph ql . apollo 3 . integration . normal izer . type . Episode Ċ import Ġcom . ap ol log raph ql . apollo 3 . integration . normal izer . type . Review Input Ċ import Ġcom . ap ol log raph ql . apollo 3 . mock server . Mock Server Ċ import Ġcom . ap ol log raph ql . apollo 3 . mock server . enqueue Ċ import Ġcom . ap ol log raph ql . apollo 3 . testing . receive Or Timeout Ċ import Ġcom . ap ol log raph ql . apollo 3 . testing . run Test Ċ import Ġcom . ben ash er 4 4 . uuid . uuid 4 Ċ import Ġkotlinx . coroutines . channels . Channel Ċ import Ġkotlinx . coroutines . launch Ċ import Ġtest Fixture To Utf 8 Ċ import Ġkotlin . test . Test Ċ import ĠassertEquals 2 Ġas ĠassertEquals Ċ Ċ class ĠOptim istic Cache Test Ġ{ ĊĠ Ġprivate Ġlateinit Ġvar Ġmock Server : ĠMock Server ĊĠ Ġprivate Ġlateinit Ġvar Ġap ollo Client : ĠApollo Client ĊĠ Ġprivate Ġlateinit Ġvar Ġstore : ĠApollo Store ĊĊĠ Ġprivate Ġsuspend Ġfun ĠsetUp () Ġ{ ĊĠĠĠ Ġstore Ġ= ĠApollo Store ( Memory Cache Factory (), Ġcache Key Generator Ġ= ĠId CacheKey Generator ) ĊĠĠĠ Ġmock Server Ġ= ĠMock Server () ĊĠĠĠ Ġap ollo Client Ġ= ĠApollo Client . Builder (). server Url ( mock Server . url ()). store ( store ). build () ĊĠ Ġ} ĊĊĠ Ġprivate Ġsuspend Ġfun ĠtearDown () Ġ{ ĊĠĠĠ Ġmock Server . stop () ĊĠ Ġ} ĊĊĠ Ġ/** ĊĠĠ Ġ* ĠWrite Ġthe Ġupdates Ġprogram matically , Ġmake Ġsure Ġthey Ġare Ġseen , ĊĠĠ Ġ* Ġroll Ġthem Ġback , Ġmake Ġsure Ġwe 're Ġback Ġto Ġthe Ġinitial Ġstate ĊĠĠ Ġ*/ ĊĠ Ġ@ Test ĊĠ Ġfun Ġprogram matic Opt imit ic Updates () Ġ= Ġrun Test ( before Ġ= Ġ{ ĠsetUp () Ġ}, Ġafter Ġ= Ġ{ ĠtearDown () Ġ}) Ġ{ ĊĠĠĠ Ġval Ġquery Ġ= ĠHero And Friends Names Query ( Episode . J ED I ) ĊĊĠĠĠ Ġmock Server . enqueue ( test Fixture To Utf 8 (" Hero And Friends Name Response . json ")) ĊĠĠĠ Ġap ollo Client . query ( query ). fetch Policy ( Fetch Policy . Network Only ). execute () ĊĊĠĠĠ Ġval Ġmutation Id Ġ= Ġuuid 4 () ĊĠĠĠ Ġval Ġdata Ġ= ĠHero And Friends Names Query . Data ( Hero And Friends Names Query . Hero ( ĊĠĠĠĠĠĠĠ Ġ" R 2 2 2 - D 2 2 2 ", ĊĠĠĠĠĠĠĠ ĠlistOf ( ĊĠĠĠĠĠĠĠĠĠĠĠ ĠHero And Friends Names Query . Friend ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" Super Man " ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ), ĊĠĠĠĠĠĠĠĠĠĠĠ ĠHero And Friends Names Query . Friend ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" B at man " ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ)) ĊĠĠĠ Ġstore . write Optim istic Updates ( ĊĠĠĠĠĠĠĠ Ġoperation Ġ= Ġquery , ĊĠĠĠĠĠĠĠ Ġoperation Data Ġ= Ġdata , ĊĠĠĠĠĠĠĠ Ġmutation Id Ġ= Ġmutation Id , ĊĠĠĠĠĠĠĠ Ġpublish Ġ= Ġtrue ) ĊĊĠĠĠ Ġvar Ġresponse Ġ= Ġap ollo Client . query ( query ). fetch Policy ( Fetch Policy . Cache Only ). execute () ĊĊĠĠĠ ĠassertEquals ( response . data ?. hero ?. name , Ġ" R 2 2 2 - D 2 2 2 ") ĊĠĠĠ ĠassertEquals ( response . data ?. hero ?. friends ?. size , Ġ 2 ) ĊĠĠĠ ĠassertEquals ( response . data ?. hero ?. friends ?. get ( 0 )?. name , Ġ" Super Man ") ĊĠĠĠ ĠassertEquals ( response . data ?. hero ?. friends ?. get ( 1 )?. name , Ġ" B at man ") ĊĊĠĠĠ Ġstore . rollback Optim istic Updates ( mutation Id , Ġfalse ) ĊĠĠĠ Ġresponse Ġ= Ġap ollo Client . query ( query ). fetch Policy ( Fetch Policy . Cache Only ). execute () ĊĊĠĠĠ ĠassertEquals ( response . data ?. hero ?. name , Ġ" R 2 - D 2 ") ĊĠĠĠ ĠassertEquals ( response . data ?. hero ?. friends ?. size , Ġ 3 ) ĊĠĠĠ ĠassertEquals ( response . data ?. hero ?. friends ?. get ( 0 )?. name , Ġ" Lu ke ĠSky walker ") ĊĠĠĠ ĠassertEquals ( response . data ?. hero ?. friends ?. get ( 1 )?. name , Ġ" Han ĠSo lo ") ĊĠĠĠ ĠassertEquals ( response . data ?. hero ?. friends ?. get ( 2 )?. name , Ġ" Le ia ĠOrg ana ") ĊĠ Ġ} ĊĊĠ Ġ/** ĊĠĠ Ġ* ĠA Ġmore Ġcomplex Ġscenario Ġwhere Ġwe Ġstack Ġoptim istic Ġupdates ĊĠĠ Ġ*/ ĊĠ Ġ@ Test ĊĠ Ġfun Ġtwo _ optim istic _ two _ rollback () Ġ= Ġrun Test ( before Ġ= Ġ{ ĠsetUp () Ġ}, Ġafter Ġ= Ġ{ ĠtearDown () Ġ}) Ġ{ ĊĠĠĠ Ġval Ġquery 1 Ġ= ĠHero And Friends Names With IDs Query ( Episode . J ED I ) ĊĠĠĠ Ġval Ġmutation Id 1 Ġ= Ġuuid 4 () ĊĊĠĠĠ Ġ// Ġexecute Ġquery 1 Ġfrom Ġthe Ġnetwork ĊĠĠĠ Ġmock Server . enqueue ( test Fixture To Utf 8 (" Hero And Friends Name With Ids Response . json ")) ĊĠĠĠ Ġap ollo Client . query ( query 1 ). fetch Policy ( Fetch Policy . Network Only ). execute () ĊĊĠĠĠ Ġ// Ġnow Ġwrite Ġsome Ġoptim istic Ġupdates Ġfor Ġquery 1 ĊĠĠĠ Ġval Ġdata 1 Ġ= ĠHero And Friends Names With IDs Query . Data ( ĊĠĠĠĠĠĠĠ ĠHero And Friends Names With IDs Query . Hero ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ" 2 0 0 1 ", ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ" R 2 2 2 - D 2 2 2 ", ĊĠĠĠĠĠĠĠĠĠĠĠ ĠlistOf ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠHero And Friends Names With IDs Query . Friend ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" 1 0 0 0 ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" Super Man " ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠHero And Friends Names With IDs Query . Friend ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" 1 0 0 3 ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" B at man " ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĠĠĠ Ġstore . write Optim istic Updates ( ĊĠĠĠĠĠĠĠ Ġoperation Ġ= Ġquery 1 , ĊĠĠĠĠĠĠĠ Ġoperation Data Ġ= Ġdata 1 , ĊĠĠĠĠĠĠĠ Ġmutation Id Ġ= Ġmutation Id 1 , ĊĠĠĠĠĠĠĠ Ġpublish Ġ= Ġtrue ) ĊĊĠĠĠ Ġ// Ġcheck Ġif Ġquery 1 Ġsee Ġoptim istic Ġupdates ĊĠĠĠ Ġvar Ġresponse 1 Ġ= Ġap ollo Client . query ( query 1 ). fetch Policy ( Fetch Policy . Cache Only ). execute () ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. id , Ġ" 2 0 0 1 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. name , Ġ" R 2 2 2 - D 2 2 2 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. size , Ġ 2 ) ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 0 )?. id , Ġ" 1 0 0 0 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 0 )?. name , Ġ" Super Man ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 1 )?. id , Ġ" 1 0 0 3 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 1 )?. name , Ġ" B at man ") ĊĊĠĠĠ Ġ// Ġexecute Ġquery 2 ĊĠĠĠ Ġval Ġquery 2 Ġ= ĠHero Name With Id Query () ĊĠĠĠ Ġval Ġmutation Id 2 Ġ= Ġuuid 4 () ĊĊĠĠĠ Ġmock Server . enqueue ( test Fixture To Utf 8 (" Hero Name With Id Response . json ")) ĊĠĠĠ Ġap ollo Client . query ( query 2 ). execute () ĊĊĠĠĠ Ġ// Ġwrite Ġoptim istic Ġdata 2 ĊĠĠĠ Ġval Ġdata 2 Ġ= ĠHero Name With Id Query . Data ( Hero Name With Id Query . Hero ( ĊĠĠĠĠĠĠĠ Ġ" 1 0 0 0 ", ĊĠĠĠĠĠĠĠ Ġ" Be ast " ĊĠĠĠ Ġ)) ĊĠĠĠ Ġstore . write Optim istic Updates ( ĊĠĠĠĠĠĠĠ Ġoperation Ġ= Ġquery 2 , ĊĠĠĠĠĠĠĠ Ġoperation Data Ġ= Ġdata 2 , ĊĠĠĠĠĠĠĠ Ġmutation Id Ġ= Ġmutation Id 2 , ĊĠĠĠĠĠĠĠ Ġpublish Ġ= Ġtrue ) ĊĊĠĠĠ Ġ// Ġcheck Ġif Ġquery 1 Ġsees Ġdata 2 ĊĠĠĠ Ġresponse 1 Ġ= Ġap ollo Client . query ( query 1 ). fetch Policy ( Fetch Policy . Cache Only ). execute () ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. id , Ġ" 2 0 0 1 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. name , Ġ" R 2 2 2 - D 2 2 2 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. size , Ġ 2 ) ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 0 )?. id , Ġ" 1 0 0 0 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 0 )?. name , Ġ" Be ast ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 1 )?. id , Ġ" 1 0 0 3 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 1 )?. name , Ġ" B at man ") ĊĊĠĠĠ Ġ// Ġcheck Ġif Ġquery 2 Ġsees Ġdata 2 ĊĠĠĠ Ġvar Ġresponse 2 Ġ= Ġap ollo Client . query ( query 2 ). fetch Policy ( Fetch Policy . Cache Only ). execute () ĊĠĠĠ ĠassertEquals ( response 2 . data ?. hero ?. id , Ġ" 1 0 0 0 ") ĊĠĠĠ ĠassertEquals ( response 2 . data ?. hero ?. name , Ġ" Be ast ") ĊĊĠĠĠ Ġ// Ġrollback Ġdata 1 ĊĠĠĠ Ġstore . rollback Optim istic Updates ( mutation Id 1 , Ġfalse ) ĊĊĠĠĠ Ġ// Ġcheck Ġif Ġquery 2 Ġsees Ġthe Ġrollback ĊĠĠĠ Ġresponse 1 Ġ= Ġap ollo Client . query ( query 1 ). fetch Policy ( Fetch Policy . Cache Only ). execute () ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. id , Ġ" 2 0 0 1 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. name , Ġ" R 2 - D 2 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. size , Ġ 3 ) ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 0 )?. id , Ġ" 1 0 0 0 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 0 )?. name , Ġ" Be ast ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 1 )?. id , Ġ" 1 0 0 2 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 1 )?. name , Ġ" Han ĠSo lo ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 2 )?. id , Ġ" 1 0 0 3 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 2 )?. name , Ġ" Le ia ĠOrg ana ") ĊĊĠĠĠ Ġ// Ġcheck Ġif Ġquery 2 Ġsee Ġthe Ġlatest Ġoptim istic Ġupdates ĊĠĠĠ Ġresponse 2 Ġ= Ġap ollo Client . query ( query 2 ). fetch Policy ( Fetch Policy . Cache Only ). execute () ĊĠĠĠ ĠassertEquals ( response 2 . data ?. hero ?. id , Ġ" 1 0 0 0 ") ĊĠĠĠ ĠassertEquals ( response 2 . data ?. hero ?. name , Ġ" Be ast ") ĊĊĠĠĠ Ġ// Ġrollback Ġquery 2 Ġoptim istic Ġupdates ĊĠĠĠ Ġstore . rollback Optim istic Updates ( mutation Id 2 , Ġfalse ) ĊĊĠĠĠ Ġ// Ġcheck Ġif Ġquery 2 Ġsee Ġthe Ġlatest Ġoptim istic Ġupdates ĊĠĠĠ Ġresponse 2 Ġ= Ġap ollo Client . query ( query 2 ). fetch Policy ( Fetch Policy . Cache Only ). execute () ĊĠĠĠ ĠassertEquals ( response 2 . data ?. hero ?. id , Ġ" 1 0 0 0 ") ĊĠĠĠ ĠassertEquals ( response 2 . data ?. hero ?. name , Ġ" Super Man ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġmutation _ and _ query _ watcher () Ġ= Ġrun Test ( before Ġ= Ġ{ ĠsetUp () Ġ}, Ġafter Ġ= Ġ{ ĠtearDown () Ġ}) Ġ{ ĊĠĠĠ Ġmock Server . enqueue ( test Fixture To Utf 8 (" Reviews Emp ire Episode Response . json ")) ĊĠĠĠ Ġval Ġchannel Ġ= ĠChannel < Reviews By Episode Query . Data ?> () ĊĠĠĠ Ġval Ġjob Ġ= Ġlaunch Ġ{ ĊĠĠĠĠĠ Ġap ollo Client . query ( Reviews By Episode Query ( Episode . EM PI RE )) ĊĠĠĠĠĠĠĠĠĠ Ġ. fetch Policy ( Fetch Policy . Network Only ) ĊĠĠĠĠĠĠĠĠĠ Ġ. ref etch Policy ( Fetch Policy . Cache Only ) ĊĠĠĠĠĠĠĠĠĠ Ġ. watch () ĊĠĠĠĠĠĠĠĠĠ Ġ. collect Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġchannel . send ( it . data ) ĊĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġvar Ġwatcher Data Ġ= Ġchannel . receive () ĊĊĠĠĠ Ġ// Ġbefore Ġmutation Ġand Ġoptim istic Ġupdates ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. size , Ġ 3 ) ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 0 )?. id , Ġ" emp ire Review 1 ") ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 0 )?. stars , Ġ 1 ) ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 0 )?. comment ary , Ġ" B oring ") ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 1 )?. id , Ġ" emp ire Review 2 ") ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 1 )?. stars , Ġ 2 ) ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 1 )?. comment ary , Ġ" So - so ") ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 2 )?. id , Ġ" emp ire Review 3 ") ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 2 )?. stars , Ġ 5 ) ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 2 )?. comment ary , Ġ" A mazing ") ĊĊĠĠĠ Ġ/** ĊĠĠĠĠ Ġ* ĠThere Ġis Ġa Ġsmall Ġpotential Ġfor Ġa Ġrace Ġcondition Ġhere . ĠThe Ġchanged Keys Ġevent Ġfrom Ġthe Ġoptim istic Ġupdates Ġmight ĊĠĠĠĠ Ġ* Ġbe Ġreceived Ġafter Ġthe Ġnetwork Ġresponse Ġhas Ġbeen Ġwritten Ġand Ġtherefore Ġthe Ġref etch Ġwill Ġsee Ġthe Ġnew Ġdata Ġright Ġahead . ĊĠĠĠĠ Ġ* ĊĠĠĠĠ Ġ* ĠTo Ġlimit Ġthe Ġocc uren ce Ġof Ġthis Ġhappening , Ġwe Ġintroduce Ġa Ġsmall Ġdelay Ġin Ġthe Ġnetwork Ġresponse Ġhere . ĊĠĠĠĠ Ġ*/ ĊĠĠĠ Ġmock Server . enqueue ( test Fixture To Utf 8 (" Update Review Response . json "), Ġ 1 0 0 ) ĊĠĠĠ Ġval Ġupdate Review Mutation Ġ= ĠUpdate Review Mutation ( ĊĠĠĠĠĠĠĠ Ġ" emp ire Review 2 ", ĊĠĠĠĠĠĠĠ ĠReview Input ( ĊĠĠĠĠĠĠĠĠĠĠĠĠ 4 , ĊĠĠĠĠĠĠĠĠĠĠĠ ĠOptional . Present (" Not ĠBad "), ĊĠĠĠĠĠĠĠĠĠĠĠ ĠColor Input ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠOptional . Absent , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠOptional . Absent , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠOptional . Absent ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĠĠĠ Ġap ollo Client . mutation ( update Review Mutation ). optim istic Updates ( ĊĠĠĠĠĠĠĠ ĠUpdate Review Mutation . Data ( ĊĠĠĠĠĠĠĠĠĠĠĠ ĠUpdate Review Mutation . Update Review ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" emp ire Review 2 ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ 5 , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" Great " ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ). execute () ĊĊĠĠĠ Ġ/** ĊĠĠĠĠ Ġ* Ġoptim istic Ġupdates ĊĠĠĠĠ Ġ*/ ĊĠĠĠ Ġwatcher Data Ġ= Ġchannel . receive () ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. size , Ġ 3 ) ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 0 )?. id , Ġ" emp ire Review 1 ") ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 0 )?. stars , Ġ 1 ) ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 0 )?. comment ary , Ġ" B oring ") ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 1 )?. id , Ġ" emp ire Review 2 ") ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 1 )?. stars , Ġ 5 ) ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 1 )?. comment ary , Ġ" Great ") ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 2 )?. id , Ġ" emp ire Review 3 ") ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 2 )?. stars , Ġ 5 ) ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 2 )?. comment ary , Ġ" A mazing ") ĊĊĠĠĠ Ġ// Ġafter Ġmutation Ġwith Ġrolled Ġback Ġoptim istic Ġupdates ĊĠĠĠ Ġwatcher Data Ġ= Ġchannel . receive Or Timeout () ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. size , Ġ 3 ) ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 0 )?. id , Ġ" emp ire Review 1 ") ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 0 )?. stars , Ġ 1 ) ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 0 )?. comment ary , Ġ" B oring ") ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 1 )?. id , Ġ" emp ire Review 2 ") ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 1 )?. stars , Ġ 4 ) ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 1 )?. comment ary , Ġ" Not ĠBad ") ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 2 )?. id , Ġ" emp ire Review 3 ") ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 2 )?. stars , Ġ 5 ) ĊĠĠĠ ĠassertEquals ( watcher Data ?. reviews ?. get ( 2 )?. comment ary , Ġ" A mazing ") ĊĊĠĠĠ Ġjob . cancel () ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġ@ Throws ( Exception :: class ) ĊĠ Ġfun Ġtwo _ optim istic _ reverse _ rollback _ order () Ġ= Ġrun Test ( before Ġ= Ġ{ ĠsetUp () Ġ}, Ġafter Ġ= Ġ{ ĠtearDown () Ġ}) Ġ{ ĊĠĠĠ Ġval Ġquery 1 Ġ= ĠHero And Friends Names With IDs Query ( Episode . J ED I ) ĊĠĠĠ Ġval Ġmutation Id 1 Ġ= Ġuuid 4 () ĊĠĠĠ Ġval Ġquery 2 Ġ= ĠHero Name With Id Query () ĊĠĠĠ Ġval Ġmutation Id 2 Ġ= Ġuuid 4 () ĊĊĠĠĠ Ġmock Server . enqueue ( test Fixture To Utf 8 (" Hero And Friends Name With Ids Response . json ")) ĊĠĠĠ Ġap ollo Client . query ( query 1 ). execute () ĊĊĠĠĠ Ġmock Server . enqueue ( test Fixture To Utf 8 (" Hero Name With Id Response . json ")) ĊĠĠĠ Ġap ollo Client . query ( query 2 ). execute () ĊĊĠĠĠ Ġval Ġdata 1 Ġ= ĠHero And Friends Names With IDs Query . Data ( ĊĠĠĠĠĠĠĠ ĠHero And Friends Names With IDs Query . Hero ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ" 2 0 0 1 ", ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ" R 2 2 2 - D 2 2 2 ", ĊĠĠĠĠĠĠĠĠĠĠĠ ĠlistOf ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠHero And Friends Names With IDs Query . Friend ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" 1 0 0 0 ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" Ro bo cop " ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠHero And Friends Names With IDs Query . Friend ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" 1 0 0 3 ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" B at man " ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĠĠĠ Ġstore . write Optim istic Updates ( ĊĠĠĠĠĠĠĠ Ġoperation Ġ= Ġquery 1 , ĊĠĠĠĠĠĠĠ Ġoperation Data Ġ= Ġdata 1 , ĊĠĠĠĠĠĠĠ Ġmutation Id Ġ= Ġmutation Id 1 , ĊĠĠĠĠĠĠĠ Ġpublish Ġ= Ġtrue ) ĊĠĠĠ Ġval Ġdata 2 Ġ= ĠHero Name With Id Query . Data ( Hero Name With Id Query . Hero ( ĊĠĠĠĠĠĠĠ Ġ" 1 0 0 0 ", ĊĠĠĠĠĠĠĠ Ġ" Spider man " ĊĠĠĠ Ġ)) ĊĠĠĠ Ġstore . write Optim istic Updates ( ĊĠĠĠĠĠĠĠ Ġoperation Ġ= Ġquery 2 , ĊĠĠĠĠĠĠĠ Ġoperation Data Ġ= Ġdata 2 , ĊĠĠĠĠĠĠĠ Ġmutation Id Ġ= Ġmutation Id 2 , ĊĠĠĠĠĠĠĠ Ġpublish Ġ= Ġtrue ) ĊĊĠĠĠ Ġ// Ġcheck Ġif Ġquery 1 Ġsee Ġoptim istic Ġupdates ĊĠĠĠ Ġvar Ġresponse 1 Ġ= Ġap ollo Client . query ( query 1 ). fetch Policy ( Fetch Policy . Cache Only ). execute () ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. id , Ġ" 2 0 0 1 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. name , Ġ" R 2 2 2 - D 2 2 2 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. size , Ġ 2 ) ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 0 )?. id , Ġ" 1 0 0 0 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 0 )?. name , Ġ" Spider man ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 1 )?. id , Ġ" 1 0 0 3 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 1 )?. name , Ġ" B at man ") ĊĊĊĠĠĠ Ġ// Ġcheck Ġif Ġquery 2 Ġsee Ġthe Ġlatest Ġoptim istic Ġupdates ĊĠĠĠ Ġvar Ġresponse 2 Ġ= Ġap ollo Client . query ( query 2 ). fetch Policy ( Fetch Policy . Cache Only ). execute () ĊĠĠĠ ĠassertEquals ( response 2 . data ?. hero ?. id , Ġ" 1 0 0 0 ") ĊĠĠĠ ĠassertEquals ( response 2 . data ?. hero ?. name , Ġ" Spider man ") ĊĊĠĠĠ Ġ// Ġrollback Ġquery 2 Ġoptim istic Ġupdates ĊĠĠĠ Ġstore . rollback Optim istic Updates ( mutation Id 2 , Ġfalse ) ĊĊĠĠĠ Ġ// Ġcheck Ġif Ġquery 1 Ġsee Ġthe Ġlatest Ġoptim istic Ġupdates ĊĠĠĠ Ġresponse 1 Ġ= Ġap ollo Client . query ( query 1 ). fetch Policy ( Fetch Policy . Cache Only ). execute () ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. id , Ġ" 2 0 0 1 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. name , Ġ" R 2 2 2 - D 2 2 2 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. size , Ġ 2 ) ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 0 )?. id , Ġ" 1 0 0 0 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 0 )?. name , Ġ" Ro bo cop ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 1 )?. id , Ġ" 1 0 0 3 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 1 )?. name , Ġ" B at man ") ĊĊĊĠĠĠ Ġ// Ġcheck Ġif Ġquery 2 Ġsee Ġthe Ġlatest Ġoptim istic Ġupdates ĊĠĠĠ Ġresponse 2 Ġ= Ġap ollo Client . query ( query 2 ). fetch Policy ( Fetch Policy . Cache Only ). execute () ĊĠĠĠ ĠassertEquals ( response 2 . data ?. hero ?. id , Ġ" 1 0 0 0 ") ĊĠĠĠ ĠassertEquals ( response 2 . data ?. hero ?. name , Ġ" Ro bo cop ") ĊĊĠĠĠ Ġ// Ġrollback Ġquery 1 Ġoptim istic Ġupdates ĊĠĠĠ Ġstore . rollback Optim istic Updates ( mutation Id 1 , Ġfalse ) ĊĊĠĠĠ Ġ// Ġcheck Ġif Ġquery 1 Ġsee Ġthe Ġlatest Ġnon - optim istic Ġupdates ĊĠĠĠ Ġresponse 1 Ġ= Ġap ollo Client . query ( query 1 ). fetch Policy ( Fetch Policy . Cache Only ). execute () ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. id , Ġ" 2 0 0 1 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. name , Ġ" R 2 - D 2 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. size , Ġ 3 ) ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 0 )?. id , Ġ" 1 0 0 0 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 0 )?. name , Ġ" Super Man ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 1 )?. id , Ġ" 1 0 0 2 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 1 )?. name , Ġ" Han ĠSo lo ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 2 )?. id , Ġ" 1 0 0 3 ") ĊĠĠĠ ĠassertEquals ( response 1 . data ?. hero ?. friends ?. get ( 2 )?. name , Ġ" Le ia ĠOrg ana ") ĊĊĊĠĠĠ Ġ// Ġcheck Ġif Ġquery 2 Ġsee Ġthe Ġlatest Ġnon - optim istic Ġupdates ĊĠĠĠ Ġresponse 2 Ġ= Ġap ollo Client . query ( query 2 ). fetch Policy ( Fetch Policy . Cache Only ). execute () ĊĠĠĠ ĠassertEquals ( response 2 . data ?. hero ?. id , Ġ" 1 0 0 0 ") ĊĠĠĠ ĠassertEquals ( response 2 . data ?. hero ?. name , Ġ" Super Man ") ĊĠ Ġ} Ċ } Ċ