FData.m 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FData.h"
  17. #import <FirebaseCore/FIRApp.h>
  18. #import <FirebaseCore/FIROptions.h>
  19. #import <limits.h>
  20. #import "FEventTester.h"
  21. #import "FIRDatabaseConfig_Private.h"
  22. #import "FIRDatabaseQuery_Private.h"
  23. #import "FIRServerValue.h"
  24. #import "FRepo_Private.h"
  25. #import "FTestHelpers.h"
  26. #import "FTupleEventTypeString.h"
  27. @implementation FData
  28. - (void)testGetNode {
  29. __unused FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  30. XCTAssertTrue(YES, @"Properly created node without throwing error");
  31. }
  32. - (void)testWriteData {
  33. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  34. [node setValue:@42];
  35. XCTAssertTrue(YES, @"Properly write to node without throwing error");
  36. }
  37. - (void)testWriteDataWithDebugLogging {
  38. [FIRDatabase setLoggingEnabled:YES];
  39. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  40. [node setValue:@42];
  41. [FIRDatabase setLoggingEnabled:NO];
  42. XCTAssertTrue(YES, @"Properly write to node without throwing error");
  43. }
  44. - (void)testWriteAndReadData {
  45. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  46. [node setValue:@42];
  47. [self snapWaiter:node
  48. withBlock:^(FIRDataSnapshot *snapshot) {
  49. XCTAssertEqualObjects(@42, [snapshot value], @"Properly saw correct value");
  50. }];
  51. }
  52. - (void)testProperParamChecking {
  53. // ios doesn't have an equivalent of this test
  54. }
  55. - (void)testNamespaceCaseInsensitivityWithinARepo {
  56. FIRDatabaseReference *ref1 =
  57. [[FIRDatabase database] referenceFromURL:[self.databaseURL uppercaseString]];
  58. FIRDatabaseReference *ref2 =
  59. [[FIRDatabase database] referenceFromURL:[self.databaseURL lowercaseString]];
  60. XCTAssertTrue([ref1.description isEqualToString:ref2.description], @"Descriptions should match");
  61. }
  62. - (void)testRootProperty {
  63. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  64. FIRDatabaseReference *root = node.root;
  65. XCTAssertTrue(root != nil, @"Should get a root");
  66. XCTAssertTrue([[root description] isEqualToString:self.databaseURL],
  67. @"Root is actually the root");
  68. }
  69. - (void)testValReturnsCompoundObjectWithChildren {
  70. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  71. [node setValue:@{@"foo" : @{@"bar" : @5}}];
  72. [self snapWaiter:node
  73. withBlock:^(FIRDataSnapshot *snapshot) {
  74. XCTAssertEqualObjects([[[snapshot value] objectForKey:@"foo"] objectForKey:@"bar"], @5,
  75. @"Properly saw compound object");
  76. }];
  77. }
  78. - (void)testWriteDataAndWaitForServerConfirmation {
  79. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  80. [self waitForCompletionOf:node setValue:@42];
  81. }
  82. - (void)testWriteAValueAndRead {
  83. // dupe of FEvent testWriteLeafExpectValueChanged
  84. }
  85. - (void)testWriteABunchOfDataAndRead {
  86. FTupleFirebase *tuple = [FTestHelpers getRandomNodePair];
  87. FIRDatabaseReference *writeNode = tuple.one;
  88. FIRDatabaseReference *readNode = tuple.two;
  89. __block BOOL done = NO;
  90. [[[[writeNode child:@"a"] child:@"b"] child:@"c"] setValue:@1];
  91. [[[[writeNode child:@"a"] child:@"d"] child:@"e"] setValue:@2];
  92. [[[[writeNode child:@"a"] child:@"d"] child:@"f"] setValue:@3];
  93. [[writeNode child:@"g"] setValue:@4
  94. withCompletionBlock:^(NSError *err, FIRDatabaseReference *ref) {
  95. done = YES;
  96. }];
  97. [self waitUntil:^BOOL {
  98. return done;
  99. }];
  100. [super snapWaiter:readNode
  101. withBlock:^(FIRDataSnapshot *s) {
  102. XCTAssertEqualObjects([[[[s childSnapshotForPath:@"a"] childSnapshotForPath:@"b"]
  103. childSnapshotForPath:@"c"] value],
  104. @1, @"Proper child value");
  105. XCTAssertEqualObjects([[[[s childSnapshotForPath:@"a"] childSnapshotForPath:@"d"]
  106. childSnapshotForPath:@"e"] value],
  107. @2, @"Proper child value");
  108. XCTAssertEqualObjects([[[[s childSnapshotForPath:@"a"] childSnapshotForPath:@"d"]
  109. childSnapshotForPath:@"f"] value],
  110. @3, @"Proper child value");
  111. XCTAssertEqualObjects([[s childSnapshotForPath:@"g"] value], @4, @"Proper child value");
  112. }];
  113. }
  114. - (void)testWriteABunchOfDataWithLeadingZeroesAndRead {
  115. FTupleFirebase *tuple = [FTestHelpers getRandomNodePair];
  116. FIRDatabaseReference *writeNode = tuple.one;
  117. FIRDatabaseReference *readNode = tuple.two;
  118. [self waitForCompletionOf:[writeNode child:@"1"] setValue:@1];
  119. [self waitForCompletionOf:[writeNode child:@"01"] setValue:@2];
  120. [self waitForCompletionOf:[writeNode child:@"001"] setValue:@3];
  121. [self waitForCompletionOf:[writeNode child:@"0001"] setValue:@4];
  122. [super
  123. snapWaiter:readNode
  124. withBlock:^(FIRDataSnapshot *s) {
  125. XCTAssertEqualObjects([[s childSnapshotForPath:@"1"] value], @1, @"Proper child value");
  126. XCTAssertEqualObjects([[s childSnapshotForPath:@"01"] value], @2, @"Proper child value");
  127. XCTAssertEqualObjects([[s childSnapshotForPath:@"001"] value], @3, @"Proper child value");
  128. XCTAssertEqualObjects([[s childSnapshotForPath:@"0001"] value], @4, @"Proper child value");
  129. }];
  130. }
  131. - (void)testLeadingZeroesTurnIntoDictionary {
  132. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  133. [self waitForCompletionOf:[ref child:@"1"] setValue:@1];
  134. [self waitForCompletionOf:[ref child:@"01"] setValue:@2];
  135. __block BOOL done = NO;
  136. __block FIRDataSnapshot *snap = nil;
  137. [ref observeEventType:FIRDataEventTypeValue
  138. withBlock:^(FIRDataSnapshot *snapshot) {
  139. snap = snapshot;
  140. done = YES;
  141. }];
  142. WAIT_FOR(done);
  143. XCTAssertTrue([snap.value isKindOfClass:[NSDictionary class]], @"Should be dictionary");
  144. XCTAssertEqualObjects([snap.value objectForKey:@"1"], @1, @"Proper child value");
  145. XCTAssertEqualObjects([snap.value objectForKey:@"01"], @2, @"Proper child value");
  146. }
  147. - (void)testLeadingZerosDontCollapseLocally {
  148. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  149. __block BOOL done = NO;
  150. __block FIRDataSnapshot *snap = nil;
  151. [ref observeEventType:FIRDataEventTypeValue
  152. withBlock:^(FIRDataSnapshot *snapshot) {
  153. snap = snapshot;
  154. done = (snapshot.childrenCount == 2);
  155. }];
  156. [[ref child:@"3"] setValue:@YES];
  157. [[ref child:@"03"] setValue:@NO];
  158. WAIT_FOR(done);
  159. XCTAssertEqualObjects([[snap childSnapshotForPath:@"3"] value], @YES, @"Proper child value");
  160. XCTAssertEqualObjects([[snap childSnapshotForPath:@"03"] value], @NO, @"Proper child value");
  161. }
  162. - (void)testSnapshotRef {
  163. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  164. __block BOOL done = NO;
  165. [ref observeEventType:FIRDataEventTypeValue
  166. withBlock:^(FIRDataSnapshot *snapshot) {
  167. [snapshot.ref observeSingleEventOfType:FIRDataEventTypeValue
  168. withBlock:^(FIRDataSnapshot *snapshot) {
  169. done = YES;
  170. }];
  171. }];
  172. WAIT_FOR(done);
  173. }
  174. - (void)testWriteLeafNodeOverwriteAtParentVerifyExpectedEvents {
  175. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  176. FIRDatabaseReference *connected = [[[FIRDatabase database] reference] child:@".info/connected"];
  177. __block BOOL ready = NO;
  178. [connected observeEventType:FIRDataEventTypeValue
  179. withBlock:^(FIRDataSnapshot *snapshot) {
  180. NSNumber *val = [snapshot value];
  181. ready = [val boolValue];
  182. }];
  183. WAIT_FOR(ready);
  184. NSArray *lookingFor = @[
  185. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a/aa"]
  186. withEvent:FIRDataEventTypeValue
  187. withString:nil], // 4
  188. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a"]
  189. withEvent:FIRDataEventTypeChildAdded
  190. withString:@"aa"], // 0
  191. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a/aa"]
  192. withEvent:FIRDataEventTypeValue
  193. withString:nil], // 4
  194. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a"]
  195. withEvent:FIRDataEventTypeChildChanged
  196. withString:@"aa"], // 2
  197. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a"]
  198. withEvent:FIRDataEventTypeValue
  199. withString:nil], // 4
  200. ];
  201. [[node repo]
  202. interrupt]; // Going offline ensures that local events get queued up before server events
  203. FEventTester *et = [[FEventTester alloc] initFrom:self];
  204. [et addLookingFor:lookingFor];
  205. [[node child:@"a/aa"] setValue:@1];
  206. [[node child:@"a"] setValue:@{@"aa" : @2}];
  207. [[node repo] resume];
  208. [et wait];
  209. }
  210. - (void)testWriteLeafNodeOverwriteAtParentMultipleTimesVerifyExpectedEvents {
  211. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  212. NSArray *lookingFor = @[
  213. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a/aa"]
  214. withEvent:FIRDataEventTypeValue
  215. withString:nil],
  216. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a"]
  217. withEvent:FIRDataEventTypeChildAdded
  218. withString:@"aa"],
  219. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a/aa"]
  220. withEvent:FIRDataEventTypeValue
  221. withString:nil],
  222. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a/bb"]
  223. withEvent:FIRDataEventTypeValue
  224. withString:nil],
  225. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a"]
  226. withEvent:FIRDataEventTypeChildChanged
  227. withString:@"aa"],
  228. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a"]
  229. withEvent:FIRDataEventTypeValue
  230. withString:nil],
  231. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a/aa"]
  232. withEvent:FIRDataEventTypeValue
  233. withString:nil],
  234. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a"]
  235. withEvent:FIRDataEventTypeChildChanged
  236. withString:@"aa"],
  237. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a"]
  238. withEvent:FIRDataEventTypeValue
  239. withString:nil],
  240. ];
  241. [[node repo]
  242. interrupt]; // Going offline ensures that local events get queued up before server events
  243. FEventTester *et = [[FEventTester alloc] initFrom:self];
  244. [et addLookingFor:lookingFor];
  245. [[node child:@"a/aa"] setValue:@1];
  246. [[node child:@"a"] setValue:@{@"aa" : @2}];
  247. [[node child:@"a"] setValue:@{@"aa" : @3}];
  248. [[node child:@"a"] setValue:@{@"aa" : @3}];
  249. [[node repo] resume];
  250. [et wait];
  251. }
  252. - (void)testWriteParentNodeOverwriteAtLeafVerifyExpectedEvents {
  253. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  254. NSArray *lookingFor = @[
  255. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a/aa"]
  256. withEvent:FIRDataEventTypeValue
  257. withString:nil],
  258. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a"]
  259. withEvent:FIRDataEventTypeChildAdded
  260. withString:@"aa"],
  261. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a"]
  262. withEvent:FIRDataEventTypeValue
  263. withString:nil],
  264. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a/aa"]
  265. withEvent:FIRDataEventTypeValue
  266. withString:nil],
  267. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a"]
  268. withEvent:FIRDataEventTypeChildChanged
  269. withString:@"aa"],
  270. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a"]
  271. withEvent:FIRDataEventTypeValue
  272. withString:nil],
  273. ];
  274. [[node repo]
  275. interrupt]; // Going offline ensures that local events get queued up before server events
  276. FEventTester *et = [[FEventTester alloc] initFrom:self];
  277. [et addLookingFor:lookingFor];
  278. [[node child:@"a"] setValue:@{@"aa" : @2}];
  279. [[node child:@"a/aa"] setValue:@1];
  280. [[node repo] resume];
  281. [et wait];
  282. }
  283. - (void)testWriteLeafNodeRemoveParentNodeVerifyExpectedEvents {
  284. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  285. FIRDatabaseReference *writer = refs.one;
  286. FIRDatabaseReference *reader = refs.two;
  287. NSArray *lookingFor = @[
  288. [[FTupleEventTypeString alloc] initWithFirebase:[writer child:@"a/aa"]
  289. withEvent:FIRDataEventTypeValue
  290. withString:nil],
  291. [[FTupleEventTypeString alloc] initWithFirebase:[writer child:@"a"]
  292. withEvent:FIRDataEventTypeChildAdded
  293. withString:@"aa"],
  294. [[FTupleEventTypeString alloc] initWithFirebase:[writer child:@"a"]
  295. withEvent:FIRDataEventTypeValue
  296. withString:nil],
  297. [[FTupleEventTypeString alloc] initWithFirebase:writer
  298. withEvent:FIRDataEventTypeChildAdded
  299. withString:@"a"],
  300. [[FTupleEventTypeString alloc] initWithFirebase:writer
  301. withEvent:FIRDataEventTypeValue
  302. withString:nil],
  303. ];
  304. FEventTester *et = [[FEventTester alloc] initFrom:self];
  305. [et addLookingFor:lookingFor];
  306. [[writer child:@"a/aa"] setValue:@42];
  307. // the local events
  308. [et wait];
  309. // the reader should get all of the events intermingled
  310. FEventTester *readerEvents = [[FEventTester alloc] initFrom:self];
  311. lookingFor = @[
  312. [[FTupleEventTypeString alloc] initWithFirebase:[reader child:@"a/aa"]
  313. withEvent:FIRDataEventTypeValue
  314. withString:nil],
  315. [[FTupleEventTypeString alloc] initWithFirebase:[reader child:@"a"]
  316. withEvent:FIRDataEventTypeChildAdded
  317. withString:@"aa"],
  318. [[FTupleEventTypeString alloc] initWithFirebase:[reader child:@"a"]
  319. withEvent:FIRDataEventTypeValue
  320. withString:nil],
  321. [[FTupleEventTypeString alloc] initWithFirebase:reader
  322. withEvent:FIRDataEventTypeChildAdded
  323. withString:@"a"],
  324. [[FTupleEventTypeString alloc] initWithFirebase:reader
  325. withEvent:FIRDataEventTypeValue
  326. withString:nil]
  327. ];
  328. [readerEvents addLookingFor:lookingFor];
  329. [readerEvents wait];
  330. lookingFor = @[
  331. [[FTupleEventTypeString alloc] initWithFirebase:[reader child:@"a/aa"]
  332. withEvent:FIRDataEventTypeValue
  333. withString:nil],
  334. [[FTupleEventTypeString alloc] initWithFirebase:[reader child:@"a"]
  335. withEvent:FIRDataEventTypeChildRemoved
  336. withString:@"aa"],
  337. [[FTupleEventTypeString alloc] initWithFirebase:[reader child:@"a"]
  338. withEvent:FIRDataEventTypeValue
  339. withString:nil],
  340. [[FTupleEventTypeString alloc] initWithFirebase:reader
  341. withEvent:FIRDataEventTypeChildRemoved
  342. withString:@"a"],
  343. [[FTupleEventTypeString alloc] initWithFirebase:reader
  344. withEvent:FIRDataEventTypeValue
  345. withString:nil]
  346. ];
  347. [readerEvents addLookingFor:lookingFor];
  348. lookingFor = @[
  349. [[FTupleEventTypeString alloc] initWithFirebase:[writer child:@"a/aa"]
  350. withEvent:FIRDataEventTypeValue
  351. withString:nil],
  352. [[FTupleEventTypeString alloc] initWithFirebase:[writer child:@"a"]
  353. withEvent:FIRDataEventTypeChildRemoved
  354. withString:@"aa"],
  355. [[FTupleEventTypeString alloc] initWithFirebase:[writer child:@"a"]
  356. withEvent:FIRDataEventTypeValue
  357. withString:nil],
  358. [[FTupleEventTypeString alloc] initWithFirebase:writer
  359. withEvent:FIRDataEventTypeChildRemoved
  360. withString:@"a"],
  361. [[FTupleEventTypeString alloc] initWithFirebase:writer
  362. withEvent:FIRDataEventTypeValue
  363. withString:nil]
  364. ];
  365. [et addLookingFor:lookingFor];
  366. [[writer child:@"a"] removeValue];
  367. [et wait];
  368. [readerEvents wait];
  369. [et unregister];
  370. [readerEvents unregister];
  371. // Ensure we can write a new value
  372. __block NSNumber *readVal = @0.0;
  373. __block NSNumber *writeVal = @0.0;
  374. [[reader child:@"a/aa"] observeEventType:FIRDataEventTypeValue
  375. withBlock:^(FIRDataSnapshot *snapshot) {
  376. id val = [snapshot value];
  377. if (val != [NSNull null]) {
  378. readVal = val;
  379. }
  380. }];
  381. [[writer child:@"a/aa"] observeEventType:FIRDataEventTypeValue
  382. withBlock:^(FIRDataSnapshot *snapshot) {
  383. id val = [snapshot value];
  384. if (val != [NSNull null]) {
  385. writeVal = val;
  386. }
  387. }];
  388. [[writer child:@"a/aa"] setValue:@3.1415];
  389. [self waitUntil:^BOOL {
  390. return fabs([readVal doubleValue] - 3.1415) < 0.001 &&
  391. fabs([writeVal doubleValue] - 3.1415) < 0.001;
  392. // return [readVal isEqualToNumber:@3.1415] && [writeVal isEqualToNumber:@3.1415];
  393. }];
  394. }
  395. - (void)testWriteLeafNodeRemoveLeafVerifyExpectedEvents {
  396. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  397. FIRDatabaseReference *writer = refs.one;
  398. FIRDatabaseReference *reader = refs.two;
  399. NSArray *lookingFor = @[
  400. [[FTupleEventTypeString alloc] initWithFirebase:[writer child:@"a/aa"]
  401. withEvent:FIRDataEventTypeValue
  402. withString:nil],
  403. [[FTupleEventTypeString alloc] initWithFirebase:[writer child:@"a"]
  404. withEvent:FIRDataEventTypeChildAdded
  405. withString:@"aa"],
  406. [[FTupleEventTypeString alloc] initWithFirebase:[writer child:@"a"]
  407. withEvent:FIRDataEventTypeValue
  408. withString:nil],
  409. [[FTupleEventTypeString alloc] initWithFirebase:writer
  410. withEvent:FIRDataEventTypeChildAdded
  411. withString:@"a"],
  412. [[FTupleEventTypeString alloc] initWithFirebase:writer
  413. withEvent:FIRDataEventTypeValue
  414. withString:nil],
  415. ];
  416. FEventTester *et = [[FEventTester alloc] initFrom:self];
  417. [et addLookingFor:lookingFor];
  418. [[writer child:@"a/aa"] setValue:@42];
  419. // the local events
  420. [et wait];
  421. // the reader should get all of the events intermingled
  422. FEventTester *readerEvents = [[FEventTester alloc] initFrom:self];
  423. lookingFor = @[
  424. [[FTupleEventTypeString alloc] initWithFirebase:[reader child:@"a/aa"]
  425. withEvent:FIRDataEventTypeValue
  426. withString:nil],
  427. [[FTupleEventTypeString alloc] initWithFirebase:[reader child:@"a"]
  428. withEvent:FIRDataEventTypeChildAdded
  429. withString:@"aa"],
  430. [[FTupleEventTypeString alloc] initWithFirebase:[reader child:@"a"]
  431. withEvent:FIRDataEventTypeValue
  432. withString:nil],
  433. [[FTupleEventTypeString alloc] initWithFirebase:reader
  434. withEvent:FIRDataEventTypeChildAdded
  435. withString:@"a"],
  436. [[FTupleEventTypeString alloc] initWithFirebase:reader
  437. withEvent:FIRDataEventTypeValue
  438. withString:nil]
  439. ];
  440. [readerEvents addLookingFor:lookingFor];
  441. [readerEvents wait];
  442. lookingFor = @[
  443. [[FTupleEventTypeString alloc] initWithFirebase:[reader child:@"a/aa"]
  444. withEvent:FIRDataEventTypeValue
  445. withString:nil],
  446. [[FTupleEventTypeString alloc] initWithFirebase:[reader child:@"a"]
  447. withEvent:FIRDataEventTypeChildRemoved
  448. withString:@"aa"],
  449. [[FTupleEventTypeString alloc] initWithFirebase:[reader child:@"a"]
  450. withEvent:FIRDataEventTypeValue
  451. withString:nil],
  452. [[FTupleEventTypeString alloc] initWithFirebase:reader
  453. withEvent:FIRDataEventTypeChildRemoved
  454. withString:@"a"],
  455. [[FTupleEventTypeString alloc] initWithFirebase:reader
  456. withEvent:FIRDataEventTypeValue
  457. withString:nil]
  458. ];
  459. [readerEvents addLookingFor:lookingFor];
  460. lookingFor = @[
  461. [[FTupleEventTypeString alloc] initWithFirebase:[writer child:@"a/aa"]
  462. withEvent:FIRDataEventTypeValue
  463. withString:nil],
  464. [[FTupleEventTypeString alloc] initWithFirebase:[writer child:@"a"]
  465. withEvent:FIRDataEventTypeChildRemoved
  466. withString:@"aa"],
  467. [[FTupleEventTypeString alloc] initWithFirebase:[writer child:@"a"]
  468. withEvent:FIRDataEventTypeValue
  469. withString:nil],
  470. [[FTupleEventTypeString alloc] initWithFirebase:writer
  471. withEvent:FIRDataEventTypeChildRemoved
  472. withString:@"a"],
  473. [[FTupleEventTypeString alloc] initWithFirebase:writer
  474. withEvent:FIRDataEventTypeValue
  475. withString:nil]
  476. ];
  477. [et addLookingFor:lookingFor];
  478. // remove just the leaf
  479. [[writer child:@"a/aa"] removeValue];
  480. [et wait];
  481. [readerEvents wait];
  482. [et unregister];
  483. [readerEvents unregister];
  484. // Ensure we can write a new value
  485. __block NSNumber *readVal = @0.0;
  486. __block NSNumber *writeVal = @0.0;
  487. [[reader child:@"a/aa"] observeEventType:FIRDataEventTypeValue
  488. withBlock:^(FIRDataSnapshot *snapshot) {
  489. id val = [snapshot value];
  490. if (val != [NSNull null]) {
  491. readVal = val;
  492. }
  493. }];
  494. [[writer child:@"a/aa"] observeEventType:FIRDataEventTypeValue
  495. withBlock:^(FIRDataSnapshot *snapshot) {
  496. id val = [snapshot value];
  497. if (val != [NSNull null]) {
  498. writeVal = val;
  499. }
  500. }];
  501. [[writer child:@"a/aa"] setValue:@3.1415];
  502. [self waitUntil:^BOOL {
  503. // NSLog(@"readVal: %@, writeVal: %@, vs %@", readVal, writeVal, @3.1415);
  504. // return [readVal isEqualToNumber:@3.1415] && [writeVal isEqualToNumber:@3.1415];
  505. return fabs([readVal doubleValue] - 3.1415) < 0.001 &&
  506. fabs([writeVal doubleValue] - 3.1415) < 0.001;
  507. }];
  508. }
  509. - (void)testWriteMultipleLeafNodesRemoveOnlyOneVerifyExpectedEvents {
  510. // XXX impl
  511. }
  512. - (void)testVerifyNodeNamesCantStartWithADot {
  513. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  514. XCTAssertThrows([ref child:@".foo"], @"not a valid .prefix");
  515. XCTAssertThrows([ref child:@"foo/.foo"], @"not a valid path");
  516. // Should not throw
  517. [[ref parent] child:@".info"];
  518. }
  519. - (void)testVerifyWritingToDotLengthAndDotKeysThrows {
  520. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  521. XCTAssertThrows([[ref child:@".keys"] setValue:@42], @"not a valid .prefix");
  522. XCTAssertThrows([[ref child:@".length"] setValue:@42], @"not a valid path");
  523. }
  524. - (void)testNumericKeysGetTurnedIntoArrays {
  525. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  526. [[ref child:@"0"] setValue:@"alpha"];
  527. [[ref child:@"1"] setValue:@"bravo"];
  528. [[ref child:@"2"] setValue:@"charlie"];
  529. [[ref child:@"3"] setValue:@"delta"];
  530. [[ref child:@"4"] setValue:@"echo"];
  531. __block BOOL ready = NO;
  532. [ref observeEventType:FIRDataEventTypeValue
  533. withBlock:^(FIRDataSnapshot *snapshot) {
  534. id val = [snapshot value];
  535. XCTAssertTrue([val isKindOfClass:[NSArray class]], @"Expected an array");
  536. NSArray *expected = @[ @"alpha", @"bravo", @"charlie", @"delta", @"echo" ];
  537. XCTAssertTrue([expected isEqualToArray:val], @"Did not get the correct array");
  538. ready = YES;
  539. }];
  540. [self waitUntil:^{
  541. return ready;
  542. }];
  543. }
  544. // This was an issue on 64-bit.
  545. - (void)testLargeNumericKeysDontGetTurnedIntoArrays {
  546. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  547. [[ref child:@"100003354884401"] setValue:@"alpha"];
  548. __block BOOL ready = NO;
  549. [ref observeSingleEventOfType:FIRDataEventTypeValue
  550. withBlock:^(FIRDataSnapshot *snapshot) {
  551. id val = [snapshot value];
  552. XCTAssertTrue([val isKindOfClass:[NSDictionary class]],
  553. @"Expected a dictionary.");
  554. ready = YES;
  555. }];
  556. [self waitUntil:^{
  557. return ready;
  558. }];
  559. }
  560. - (void)testWriteCompoundObjectAndGetItBack {
  561. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  562. NSDictionary *data = @{
  563. @"a" : @{@"aa" : @5, @"ab" : @3},
  564. @"b" : @{@"ba" : @"hey there!", @"bb" : @{@"bba" : @NO}},
  565. @"c" : @[ @0, @{@"c_1" : @4}, @"hey", @YES, @NO, @"dude" ]
  566. };
  567. __block FIRDataSnapshot *snap = nil;
  568. [node observeEventType:FIRDataEventTypeValue
  569. withBlock:^(FIRDataSnapshot *snapshot) {
  570. snap = snapshot;
  571. }];
  572. __block BOOL done = NO;
  573. [node setValue:data
  574. withCompletionBlock:^(NSError *err, FIRDatabaseReference *ref) {
  575. done = YES;
  576. }];
  577. [self waitUntil:^BOOL {
  578. return done;
  579. }];
  580. [self snapWaiter:node
  581. withBlock:^(FIRDataSnapshot *snapshot) {
  582. XCTAssertTrue([[[[snapshot value] objectForKey:@"c"] objectAtIndex:3] boolValue],
  583. @"Got proper boolean");
  584. }];
  585. }
  586. - (void)testCanPassValueToPush {
  587. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  588. FIRDatabaseReference *pushA = [node childByAutoId];
  589. [pushA setValue:@5];
  590. [self snapWaiter:pushA
  591. withBlock:^(FIRDataSnapshot *snapshot) {
  592. XCTAssertEqualObjects(@5, [snapshot value], @"Got proper value");
  593. }];
  594. FIRDatabaseReference *pushB = [node childByAutoId];
  595. [pushB setValue:@{@"a" : @5, @"b" : @6}];
  596. [self snapWaiter:pushB
  597. withBlock:^(FIRDataSnapshot *snapshot) {
  598. XCTAssertEqualObjects(@5, [[snapshot value] objectForKey:@"a"], @"Got proper value");
  599. XCTAssertEqualObjects(@6, [[snapshot value] objectForKey:@"b"], @"Got proper value");
  600. }];
  601. }
  602. // Dropped test that tested callbacks to push. Support was removed.
  603. - (void)testRemoveCallbackHit {
  604. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  605. __block BOOL setDone = NO;
  606. __block BOOL removeDone = NO;
  607. __block BOOL readDone = NO;
  608. [node setValue:@42
  609. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  610. setDone = YES;
  611. }];
  612. [self waitUntil:^BOOL {
  613. return setDone;
  614. }];
  615. [node observeEventType:FIRDataEventTypeValue
  616. withBlock:^(FIRDataSnapshot *snapshot) {
  617. id val = [snapshot value];
  618. if (val == [NSNull null]) {
  619. readDone = YES;
  620. }
  621. }];
  622. [node removeValueWithCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  623. XCTAssertTrue(error == nil, @"Should not be an error removing");
  624. removeDone = YES;
  625. }];
  626. [self waitUntil:^BOOL {
  627. return readDone && removeDone;
  628. }];
  629. }
  630. - (void)testRemoveCallbackIsHitForNodesThatAreAlreadyRemoved {
  631. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  632. __block int removes = 0;
  633. [node removeValueWithCompletionBlock:^(NSError *err, FIRDatabaseReference *ref) {
  634. removes = removes + 1;
  635. }];
  636. [node removeValueWithCompletionBlock:^(NSError *err, FIRDatabaseReference *ref) {
  637. removes = removes + 1;
  638. }];
  639. [self waitUntil:^BOOL {
  640. return removes == 2;
  641. }];
  642. }
  643. - (void)testUsingNumbersAsKeysDoesntCreateHugeSparseArrays {
  644. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  645. [[ref child:@"3024"] setValue:@5];
  646. __block BOOL ready = NO;
  647. [ref observeEventType:FIRDataEventTypeValue
  648. withBlock:^(FIRDataSnapshot *snapshot) {
  649. id val = [snapshot value];
  650. XCTAssertTrue(![val isKindOfClass:[NSArray class]], @"Should not be an array");
  651. ready = YES;
  652. }];
  653. [self waitUntil:^BOOL {
  654. return ready;
  655. }];
  656. }
  657. - (void)testOnceWithACallbackHitsServer {
  658. FTupleFirebase *tuple = [FTestHelpers getRandomNodeTriple];
  659. FIRDatabaseReference *writeNode = tuple.one;
  660. FIRDatabaseReference *readNode = tuple.two;
  661. FIRDatabaseReference *readNodeB = tuple.three;
  662. __block BOOL initialReadDone = NO;
  663. [readNode observeSingleEventOfType:FIRDataEventTypeValue
  664. withBlock:^(FIRDataSnapshot *snapshot) {
  665. XCTAssertTrue([[snapshot value] isEqual:[NSNull null]],
  666. @"First callback is null");
  667. initialReadDone = YES;
  668. }];
  669. [self waitUntil:^BOOL {
  670. return initialReadDone;
  671. }];
  672. __block BOOL writeDone = NO;
  673. [writeNode setValue:@42
  674. withCompletionBlock:^(NSError *err, FIRDatabaseReference *ref) {
  675. writeDone = YES;
  676. }];
  677. [self waitUntil:^BOOL {
  678. return writeDone;
  679. }];
  680. __block BOOL readDone = NO;
  681. [readNodeB observeSingleEventOfType:FIRDataEventTypeValue
  682. withBlock:^(FIRDataSnapshot *snapshot) {
  683. XCTAssertEqualObjects(@42, [snapshot value], @"Proper second read");
  684. readDone = YES;
  685. }];
  686. [self waitUntil:^BOOL {
  687. return readDone;
  688. }];
  689. }
  690. // Removed test of forEach aborting iteration. Support dropped, use for .. in syntax
  691. - (void)testSetAndThenListenForValueEventsAreCorrect {
  692. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  693. __block BOOL setDone = NO;
  694. [node setValue:@"moo"
  695. withCompletionBlock:^(NSError *err, FIRDatabaseReference *ref) {
  696. setDone = YES;
  697. }];
  698. __block int calls = 0;
  699. [node observeEventType:FIRDataEventTypeValue
  700. withBlock:^(FIRDataSnapshot *snapshot) {
  701. calls = calls + 1;
  702. XCTAssertTrue(calls == 1, @"Only called once");
  703. XCTAssertEqualObjects([snapshot value], @"moo", @"Proper snapshot value");
  704. }];
  705. [self waitUntil:^BOOL {
  706. return setDone && calls == 1;
  707. }];
  708. [node removeAllObservers];
  709. }
  710. - (void)testHasChildrenWorksCorrectly {
  711. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  712. [node setValue:@{@"one" : @42, @"two" : @{@"a" : @5}, @"three" : @{@"a" : @5, @"b" : @6}}];
  713. __block BOOL removedTwo = NO;
  714. __block BOOL done = NO;
  715. [node observeEventType:FIRDataEventTypeValue
  716. withBlock:^(FIRDataSnapshot *snapshot) {
  717. if (!removedTwo) {
  718. XCTAssertFalse([[snapshot childSnapshotForPath:@"one"] hasChildren], @"nope");
  719. XCTAssertTrue([[snapshot childSnapshotForPath:@"two"] hasChildren], @"nope");
  720. XCTAssertTrue([[snapshot childSnapshotForPath:@"three"] hasChildren], @"nope");
  721. XCTAssertFalse([[snapshot childSnapshotForPath:@"four"] hasChildren], @"nope");
  722. removedTwo = YES;
  723. [[node child:@"two"] removeValue];
  724. } else {
  725. XCTAssertFalse([[snapshot childSnapshotForPath:@"two"] hasChildren],
  726. @"Second time around");
  727. done = YES;
  728. }
  729. }];
  730. [self waitUntil:^BOOL {
  731. return done;
  732. }];
  733. }
  734. - (void)testNumChildrenWorksCorrectly {
  735. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  736. [node setValue:@{@"one" : @42, @"two" : @{@"a" : @5}, @"three" : @{@"a" : @5, @"b" : @6}}];
  737. __block BOOL removedTwo = NO;
  738. __block BOOL done = NO;
  739. [node observeEventType:FIRDataEventTypeValue
  740. withBlock:^(FIRDataSnapshot *snapshot) {
  741. if (!removedTwo) {
  742. XCTAssertTrue([snapshot childrenCount] == 3, @"Total children");
  743. XCTAssertTrue([[snapshot childSnapshotForPath:@"one"] childrenCount] == 0,
  744. @"Two's children");
  745. XCTAssertTrue([[snapshot childSnapshotForPath:@"two"] childrenCount] == 1,
  746. @"Two's children");
  747. XCTAssertTrue([[snapshot childSnapshotForPath:@"three"] childrenCount] == 2,
  748. @"Two's children");
  749. XCTAssertTrue([[snapshot childSnapshotForPath:@"four"] childrenCount] == 0,
  750. @"Two's children");
  751. removedTwo = YES;
  752. [[node child:@"two"] removeValue];
  753. } else {
  754. XCTAssertTrue([snapshot childrenCount] == 2, @"Total children");
  755. XCTAssertTrue([[snapshot childSnapshotForPath:@"two"] childrenCount] == 0,
  756. @"Two's children");
  757. done = YES;
  758. }
  759. }];
  760. [self waitUntil:^BOOL {
  761. return done;
  762. }];
  763. }
  764. #ifdef FLAKY_TEST
  765. - (void)testSettingANodeWithChildrenToAPrimitiveAndBack {
  766. // Can't tolerate stale data; so disable persistence.
  767. FTupleFirebase *tuple = [FTestHelpers getRandomNodePairWithoutPersistence];
  768. FIRDatabaseReference *writeNode = tuple.one;
  769. FIRDatabaseReference *readNode = tuple.two;
  770. __block BOOL done = NO;
  771. NSDictionary *compound = @{@"a" : @5, @"b" : @6};
  772. NSNumber *number = @76;
  773. [writeNode setValue:compound];
  774. [self snapWaiter:writeNode
  775. withBlock:^(FIRDataSnapshot *snapshot) {
  776. XCTAssertTrue([snapshot hasChildren], @"Has children");
  777. XCTAssertEqualObjects(@5, [[snapshot childSnapshotForPath:@"a"] value], @"First child");
  778. XCTAssertEqualObjects(@6, [[snapshot childSnapshotForPath:@"b"] value], @"First child");
  779. done = YES;
  780. }];
  781. [self waitUntil:^BOOL {
  782. return done;
  783. }];
  784. done = NO;
  785. [self snapWaiter:readNode
  786. withBlock:^(FIRDataSnapshot *snapshot) {
  787. XCTAssertTrue([snapshot hasChildren], @"has children");
  788. XCTAssertEqualObjects(@5, [[snapshot childSnapshotForPath:@"a"] value], @"First child");
  789. XCTAssertEqualObjects(@6, [[snapshot childSnapshotForPath:@"b"] value], @"First child");
  790. done = YES;
  791. }];
  792. [self waitUntil:^BOOL {
  793. return done;
  794. }];
  795. done = NO;
  796. [writeNode setValue:number
  797. withCompletionBlock:^(NSError *err, FIRDatabaseReference *ref) {
  798. done = YES;
  799. }];
  800. [self waitUntil:^BOOL {
  801. return done;
  802. }];
  803. done = NO;
  804. [self snapWaiter:readNode
  805. withBlock:^(FIRDataSnapshot *snapshot) {
  806. XCTAssertFalse([snapshot hasChildren], @"No more children");
  807. XCTAssertEqualObjects(number, [snapshot value], @"Proper non compound value");
  808. done = YES;
  809. }];
  810. [self waitUntil:^BOOL {
  811. return done;
  812. }];
  813. done = NO;
  814. [writeNode setValue:compound
  815. withCompletionBlock:^(NSError *err, FIRDatabaseReference *ref) {
  816. done = YES;
  817. }];
  818. [self waitUntil:^BOOL {
  819. return done;
  820. }];
  821. done = NO;
  822. [self snapWaiter:readNode
  823. withBlock:^(FIRDataSnapshot *snapshot) {
  824. XCTAssertTrue([snapshot hasChildren], @"Has children");
  825. XCTAssertEqualObjects(@5, [[snapshot childSnapshotForPath:@"a"] value], @"First child");
  826. XCTAssertEqualObjects(@6, [[snapshot childSnapshotForPath:@"b"] value], @"First child");
  827. done = YES;
  828. }];
  829. [self waitUntil:^BOOL {
  830. return done;
  831. }];
  832. XCTAssertTrue(done, @"Properly finished");
  833. }
  834. #endif
  835. - (void)testWriteLeafRemoveLeafAddChildToRemovedNode {
  836. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  837. FIRDatabaseReference *writer = refs.one;
  838. FIRDatabaseReference *reader = refs.two;
  839. __block BOOL ready = NO;
  840. [writer setValue:@5];
  841. [writer removeValue];
  842. [[writer child:@"abc"] setValue:@5
  843. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  844. ready = YES;
  845. }];
  846. [self waitUntil:^BOOL {
  847. return ready;
  848. }];
  849. __block NSDictionary *readVal = nil;
  850. [reader observeEventType:FIRDataEventTypeValue
  851. withBlock:^(FIRDataSnapshot *snapshot) {
  852. readVal = [snapshot value];
  853. }];
  854. [self waitUntil:^BOOL {
  855. return readVal != nil;
  856. }];
  857. NSNumber *five = [readVal objectForKey:@"abc"];
  858. XCTAssertTrue([five isEqualToNumber:@5], @"Should get 5");
  859. }
  860. - (void)testListenForValueAndThenWriteOnANodeWithExistingData {
  861. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  862. FIRDatabaseReference *writer = refs.one;
  863. FIRDatabaseReference *reader = refs.two;
  864. [self waitForCompletionOf:writer setValue:@{@"a" : @5, @"b" : @2}];
  865. __block int calls = 0;
  866. [reader observeEventType:FIRDataEventTypeValue
  867. withBlock:^(FIRDataSnapshot *snapshot) {
  868. calls++;
  869. if (calls == 1) {
  870. NSDictionary *val = [snapshot value];
  871. NSDictionary *expected = @{@"a" : @10, @"b" : @2};
  872. XCTAssertTrue([val isEqualToDictionary:expected], @"Got the correct value");
  873. } else {
  874. XCTFail(@"Should only be called once");
  875. }
  876. }];
  877. [[reader child:@"a"] setValue:@10];
  878. [self waitUntil:^BOOL {
  879. return calls == 1;
  880. }];
  881. [reader removeAllObservers];
  882. }
  883. - (void)testSetPriorityOnNonexistentNodeFails {
  884. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  885. __block BOOL ready = NO;
  886. [ref setPriority:@5
  887. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  888. XCTAssertTrue(error != nil, @"This should not succeed");
  889. ready = YES;
  890. }];
  891. [self waitUntil:^BOOL {
  892. return ready;
  893. }];
  894. }
  895. - (void)testSetPriorityOnExistentNodeSucceeds {
  896. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  897. __block BOOL ready = NO;
  898. [ref setValue:@"hello!"];
  899. [ref setPriority:@5
  900. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  901. XCTAssertTrue(error == nil, @"This should succeed");
  902. ready = YES;
  903. }];
  904. [self waitUntil:^BOOL {
  905. return ready;
  906. }];
  907. }
  908. - (void)testSetWithPrioritySetsValueAndPriority {
  909. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  910. FIRDatabaseReference *writer = refs.one;
  911. FIRDatabaseReference *reader = refs.two;
  912. [self waitForCompletionOf:writer setValue:@"hello" andPriority:@5];
  913. __block FIRDataSnapshot *writeSnap = nil;
  914. __block FIRDataSnapshot *readSnap = nil;
  915. [writer observeEventType:FIRDataEventTypeValue
  916. withBlock:^(FIRDataSnapshot *snapshot) {
  917. writeSnap = snapshot;
  918. }];
  919. [reader observeEventType:FIRDataEventTypeValue
  920. withBlock:^(FIRDataSnapshot *snapshot) {
  921. readSnap = snapshot;
  922. }];
  923. [self waitUntil:^BOOL {
  924. return readSnap != nil && writeSnap != nil;
  925. }];
  926. XCTAssertTrue([@"hello" isEqualToString:[readSnap value]], @"Got the value on the reader");
  927. XCTAssertTrue([@"hello" isEqualToString:[writeSnap value]], @"Got the value on the writer");
  928. XCTAssertTrue([@5 isEqualToNumber:[readSnap priority]], @"Got the priority on the reader");
  929. XCTAssertTrue([@5 isEqualToNumber:[writeSnap priority]], @"Got the priority on the writer");
  930. }
  931. - (void)testEffectsOfSetPriorityIsImmediatelyEvident {
  932. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  933. NSMutableArray *values = [[NSMutableArray alloc] init];
  934. NSMutableArray *priorities = [[NSMutableArray alloc] init];
  935. [ref observeSingleEventOfType:FIRDataEventTypeValue
  936. withBlock:^(FIRDataSnapshot *snapshot) {
  937. [values addObject:[snapshot value]];
  938. [priorities addObject:[snapshot priority]];
  939. }];
  940. [ref setValue:@5];
  941. [ref setPriority:@10];
  942. __block BOOL ready = NO;
  943. [ref observeEventType:FIRDataEventTypeValue
  944. withBlock:^(FIRDataSnapshot *snapshot) {
  945. [values addObject:[snapshot value]];
  946. [priorities addObject:[snapshot priority]];
  947. ready = YES;
  948. }];
  949. [self waitUntil:^BOOL {
  950. return ready;
  951. }];
  952. NSArray *expectedValues = @[ @5, @5 ];
  953. NSArray *expectedPriorites = @[ [NSNull null], @10 ];
  954. XCTAssertTrue([values isEqualToArray:expectedValues],
  955. @"Expected both listeners to get 5, got %@ instead", values);
  956. XCTAssertTrue([priorities isEqualToArray:expectedPriorites],
  957. @"The first listener should have missed the priority, got %@ instead", priorities);
  958. }
  959. - (void)testSetOverwritesPriorityOfTopLevelNodeAndSubnodes {
  960. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  961. FIRDatabaseReference *writer = refs.one;
  962. FIRDatabaseReference *reader = refs.two;
  963. __block BOOL ready = NO;
  964. [writer setValue:@{@"a" : @5}];
  965. [writer setPriority:@10];
  966. [[writer child:@"a"] setPriority:@18];
  967. [writer setValue:@{@"a" : @7}
  968. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  969. ready = YES;
  970. }];
  971. [self waitUntil:^BOOL {
  972. return ready;
  973. }];
  974. ready = NO;
  975. [reader observeEventType:FIRDataEventTypeValue
  976. withBlock:^(FIRDataSnapshot *snapshot) {
  977. id pri = [snapshot priority];
  978. XCTAssertTrue([NSNull null] == pri, @"Expected null priority");
  979. FIRDataSnapshot *child = [snapshot childSnapshotForPath:@"a"];
  980. XCTAssertTrue([NSNull null] == [child priority],
  981. @"Child priority should be null too");
  982. ready = YES;
  983. }];
  984. [self waitUntil:^BOOL {
  985. return ready;
  986. }];
  987. }
  988. - (void)testSetPriorityOfLeafSavesCorrectly {
  989. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  990. FIRDatabaseReference *writer = refs.one;
  991. FIRDatabaseReference *reader = refs.two;
  992. __block BOOL ready = NO;
  993. [writer setValue:@"testleaf"
  994. andPriority:@992
  995. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  996. ready = YES;
  997. }];
  998. [self waitUntil:^BOOL {
  999. return ready;
  1000. }];
  1001. ready = NO;
  1002. [reader observeEventType:FIRDataEventTypeValue
  1003. withBlock:^(FIRDataSnapshot *snapshot) {
  1004. id pri = [snapshot priority];
  1005. XCTAssertTrue([@992 isEqualToNumber:pri], @"Expected non-null priority");
  1006. ready = YES;
  1007. }];
  1008. [self waitUntil:^BOOL {
  1009. return ready;
  1010. }];
  1011. }
  1012. - (void)testSetPriorityOfObjectSavesCorrectly {
  1013. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  1014. FIRDatabaseReference *writer = refs.one;
  1015. FIRDatabaseReference *reader = refs.two;
  1016. __block BOOL ready = NO;
  1017. [writer setValue:@{@"a" : @5}
  1018. andPriority:@991
  1019. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1020. ready = YES;
  1021. }];
  1022. [self waitUntil:^BOOL {
  1023. return ready;
  1024. }];
  1025. ready = NO;
  1026. [reader observeEventType:FIRDataEventTypeValue
  1027. withBlock:^(FIRDataSnapshot *snapshot) {
  1028. id pri = [snapshot priority];
  1029. XCTAssertTrue([@991 isEqualToNumber:pri], @"Expected non-null priority");
  1030. ready = YES;
  1031. }];
  1032. [self waitUntil:^BOOL {
  1033. return ready;
  1034. }];
  1035. }
  1036. - (void)testSetWithPriorityFollowedBySetClearsPriority {
  1037. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  1038. FIRDatabaseReference *writer = refs.one;
  1039. FIRDatabaseReference *reader = refs.two;
  1040. __block BOOL ready = NO;
  1041. [writer setValue:@{@"a" : @5}
  1042. andPriority:@991
  1043. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1044. ready = YES;
  1045. }];
  1046. [self waitUntil:^BOOL {
  1047. return ready;
  1048. }];
  1049. ready = NO;
  1050. [reader setValue:@{@"a" : @19}
  1051. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1052. ready = YES;
  1053. }];
  1054. [self waitUntil:^BOOL {
  1055. return ready;
  1056. }];
  1057. ready = NO;
  1058. [reader observeEventType:FIRDataEventTypeValue
  1059. withBlock:^(FIRDataSnapshot *snapshot) {
  1060. id pri = [snapshot priority];
  1061. XCTAssertTrue([NSNull null] == pri, @"Expected null priority");
  1062. ready = YES;
  1063. }];
  1064. [self waitUntil:^BOOL {
  1065. return ready;
  1066. }];
  1067. }
  1068. - (void)testGetPriorityReturnsCorrectType {
  1069. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  1070. __block FIRDataSnapshot *snap = nil;
  1071. [ref observeEventType:FIRDataEventTypeValue
  1072. withBlock:^(FIRDataSnapshot *snapshot) {
  1073. snap = snapshot;
  1074. }];
  1075. [ref setValue:@"a"];
  1076. [self waitUntil:^BOOL {
  1077. return snap != nil;
  1078. }];
  1079. XCTAssertTrue([snap priority] == [NSNull null], @"Expect null priority");
  1080. snap = nil;
  1081. [ref setValue:@"b" andPriority:@5];
  1082. [self waitUntil:^BOOL {
  1083. return snap != nil;
  1084. }];
  1085. XCTAssertTrue([[snap priority] isEqualToNumber:@5], @"Expect priority");
  1086. snap = nil;
  1087. [ref setValue:@"c" andPriority:@"6"];
  1088. [self waitUntil:^BOOL {
  1089. return snap != nil;
  1090. }];
  1091. XCTAssertTrue([[snap priority] isEqualToString:@"6"], @"Expect priority");
  1092. snap = nil;
  1093. [ref setValue:@"d" andPriority:@7];
  1094. [self waitUntil:^BOOL {
  1095. return snap != nil;
  1096. }];
  1097. XCTAssertTrue([[snap priority] isEqualToNumber:@7], @"Expect priority");
  1098. snap = nil;
  1099. [ref setValue:@{@".value" : @"e", @".priority" : @8}];
  1100. [self waitUntil:^BOOL {
  1101. return snap != nil;
  1102. }];
  1103. XCTAssertTrue([[snap priority] isEqualToNumber:@8], @"Expect priority");
  1104. snap = nil;
  1105. [ref setValue:@{@".value" : @"f", @".priority" : @"8"}];
  1106. [self waitUntil:^BOOL {
  1107. return snap != nil;
  1108. }];
  1109. XCTAssertTrue([[snap priority] isEqualToString:@"8"], @"Expect priority");
  1110. snap = nil;
  1111. [ref setValue:@{@".value" : @"e", @".priority" : [NSNull null]}];
  1112. [self waitUntil:^BOOL {
  1113. return snap != nil;
  1114. }];
  1115. XCTAssertTrue([snap priority] == [NSNull null], @"Expect priority");
  1116. snap = nil;
  1117. }
  1118. - (void)testExportValIncludesPriorities {
  1119. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  1120. NSDictionary *contents =
  1121. @{@"foo" : @{@"bar" : @{@".value" : @5, @".priority" : @7}, @".priority" : @"hi"}};
  1122. __block FIRDataSnapshot *snap = nil;
  1123. [ref observeEventType:FIRDataEventTypeValue
  1124. withBlock:^(FIRDataSnapshot *snapshot) {
  1125. snap = snapshot;
  1126. }];
  1127. [ref setValue:contents];
  1128. [self waitUntil:^BOOL {
  1129. return snap != nil;
  1130. }];
  1131. XCTAssertTrue([contents isEqualToDictionary:[snap valueInExportFormat]],
  1132. @"Expected priorities in snapshot");
  1133. }
  1134. - (void)testPriorityIsOverwrittenByServer {
  1135. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  1136. FIRDatabaseReference *reader = refs.one;
  1137. FIRDatabaseReference *writer = refs.two;
  1138. __block int event = 0;
  1139. __block BOOL done = NO;
  1140. [reader observeEventType:FIRDataEventTypeValue
  1141. withBlock:^(FIRDataSnapshot *snapshot) {
  1142. NSLog(@"%@ Snapshot", snapshot);
  1143. id pri = [snapshot priority];
  1144. if (event == 0) {
  1145. XCTAssertTrue([@100 isEqualToNumber:pri],
  1146. @"Expect local priority. Got %@ instead.", pri);
  1147. } else if (event == 1) {
  1148. XCTAssertTrue(pri == [NSNull null], @"Expect remote priority. Got %@ instead.",
  1149. pri);
  1150. } else {
  1151. XCTFail(@"Extra event");
  1152. }
  1153. event++;
  1154. if (event == 2) {
  1155. done = YES;
  1156. }
  1157. }];
  1158. [writer
  1159. observeEventType:FIRDataEventTypeValue
  1160. withBlock:^(FIRDataSnapshot *snapshot) {
  1161. id pri = [snapshot priority];
  1162. if ([[pri class] isSubclassOfClass:[NSNumber class]] && [@100 isEqualToNumber:pri]) {
  1163. [writer setValue:@"whatever"];
  1164. }
  1165. }];
  1166. [reader setValue:@"hi" andPriority:@100];
  1167. [self waitUntil:^BOOL {
  1168. return done;
  1169. }];
  1170. }
  1171. - (void)testLargeNumericPrioritiesWork {
  1172. NSNumber *bigPriority = @1356721306842;
  1173. __block BOOL ready = NO;
  1174. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  1175. FIRDatabaseReference *reader = refs.one;
  1176. FIRDatabaseReference *writer = refs.two;
  1177. [self waitForCompletionOf:writer setValue:@5 andPriority:bigPriority];
  1178. __block NSNumber *serverPriority = @0;
  1179. [reader observeEventType:FIRDataEventTypeValue
  1180. withBlock:^(FIRDataSnapshot *snapshot) {
  1181. serverPriority = [snapshot priority];
  1182. ready = YES;
  1183. }];
  1184. [self waitUntil:^BOOL {
  1185. return ready;
  1186. }];
  1187. XCTAssertTrue([bigPriority isEqualToNumber:serverPriority], @"Expect big priority back");
  1188. }
  1189. - (void)testToString {
  1190. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  1191. FIRDatabaseReference *parent = [ref parent];
  1192. XCTAssertTrue([[parent description] isEqualToString:self.databaseURL], @"Expect domain");
  1193. FIRDatabaseReference *child = [parent child:@"a/b/c"];
  1194. NSString *expected = [NSString stringWithFormat:@"%@/a/b/c", self.databaseURL];
  1195. XCTAssertTrue([[child description] isEqualToString:expected], @"Expected path");
  1196. }
  1197. - (void)testURLEncodingOfDescriptionAndURLDecodingOfNewFirebase {
  1198. __block BOOL ready = NO;
  1199. NSString *test1 =
  1200. [NSString stringWithFormat:@"%@/a%%b&c@d/space: /non-ascii_character:ø", self.databaseURL];
  1201. NSString *expected1 = [NSString
  1202. stringWithFormat:@"%@/a%%25b%%26c%%40d/space%%3A%%20/non-ascii_character%%3A%%C3%%B8",
  1203. self.databaseURL];
  1204. FIRDatabaseReference *ref = [[FIRDatabase database] referenceFromURL:test1];
  1205. NSString *result = [ref description];
  1206. XCTAssertTrue([result isEqualToString:expected1], @"Encodes properly");
  1207. int rnd = arc4random_uniform(100000000);
  1208. NSString *path = [NSString stringWithFormat:@"%i", rnd];
  1209. [[ref child:path] setValue:@"testdata"
  1210. withCompletionBlock:^(NSError *error, FIRDatabaseReference *childRef) {
  1211. FIRDatabaseReference *other =
  1212. [[FIRDatabase database] referenceFromURL:[ref description]];
  1213. [[other child:path] observeEventType:FIRDataEventTypeValue
  1214. withBlock:^(FIRDataSnapshot *snapshot) {
  1215. NSString *val = snapshot.value;
  1216. XCTAssertTrue([val isEqualToString:@"testdata"],
  1217. @"Expected to get testdata back");
  1218. ready = YES;
  1219. }];
  1220. }];
  1221. [self waitUntil:^BOOL {
  1222. return ready;
  1223. }];
  1224. }
  1225. - (void)testNameAtRootAndNonRootLocations {
  1226. FIRDatabaseReference *ref = [[FIRDatabase database] referenceFromURL:self.databaseURL];
  1227. XCTAssertTrue(ref.key == nil, @"Root key should be nil");
  1228. FIRDatabaseReference *child = [ref child:@"a"];
  1229. XCTAssertTrue([child.key isEqualToString:@"a"], @"Should be 'a'");
  1230. FIRDatabaseReference *deeperChild = [child child:@"b/c"];
  1231. XCTAssertTrue([deeperChild.key isEqualToString:@"c"], @"Should be 'c'");
  1232. }
  1233. - (void)testNameAndRefOnSnapshotsForRootAndNonRootLocations {
  1234. FIRDatabaseReference *ref = [[FIRDatabase database] reference];
  1235. __block BOOL ready = NO;
  1236. [ref removeValueWithCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1237. ready = YES;
  1238. }];
  1239. [self waitUntil:^BOOL {
  1240. return ready;
  1241. }];
  1242. ready = NO;
  1243. [ref
  1244. observeEventType:FIRDataEventTypeValue
  1245. withBlock:^(FIRDataSnapshot *snapshot) {
  1246. XCTAssertTrue(snapshot.key == nil, @"Root snap should not have a key");
  1247. NSString *snapString = [snapshot.ref description];
  1248. XCTAssertTrue([snapString isEqualToString:snapString], @"Refs should be equivalent");
  1249. FIRDataSnapshot *childSnap = [snapshot childSnapshotForPath:@"a"];
  1250. XCTAssertTrue([childSnap.key isEqualToString:@"a"], @"Properly keys children");
  1251. FIRDatabaseReference *childRef = [ref child:@"a"];
  1252. NSString *refString = [childRef description];
  1253. snapString = [childSnap.ref description];
  1254. XCTAssertTrue([refString isEqualToString:snapString], @"Refs should be equivalent");
  1255. childSnap = [childSnap childSnapshotForPath:@"b/c"];
  1256. childRef = [childRef child:@"b/c"];
  1257. XCTAssertTrue([childSnap.key isEqualToString:@"c"], @"properly keys children");
  1258. refString = [childRef description];
  1259. snapString = [childSnap.ref description];
  1260. XCTAssertTrue([refString isEqualToString:snapString], @"Refs should be equivalent");
  1261. ready = YES;
  1262. }];
  1263. [self waitUntil:^BOOL {
  1264. return ready;
  1265. }];
  1266. ready = NO;
  1267. // generate value event at root
  1268. [ref setValue:@"foo"];
  1269. [self waitUntil:^BOOL {
  1270. return ready;
  1271. }];
  1272. }
  1273. - (void)testParentForRootAndNonRootLocations {
  1274. FIRDatabaseReference *ref = [[FIRDatabase database] reference];
  1275. XCTAssertTrue(ref.parent == nil, @"Parent of root should be nil");
  1276. FIRDatabaseReference *child = [ref child:@"a"];
  1277. XCTAssertTrue([[child.parent description] isEqualToString:[ref description]],
  1278. @"Should be equivalent locations");
  1279. child = [ref child:@"a/b/c"];
  1280. XCTAssertTrue([[child.parent.parent.parent description] isEqualToString:[ref description]],
  1281. @"Should be equivalent locations");
  1282. }
  1283. - (void)testSettingNumericKeysConvertsToStrings {
  1284. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  1285. NSDictionary *toSet = @{@4 : @"hi", @5 : @"test"};
  1286. XCTAssertThrows([ref setValue:toSet], @"Keys must be strings");
  1287. }
  1288. - (void)testSetChildAndListenAtRootRegressionTest {
  1289. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  1290. FIRDatabaseReference *writer = refs.one;
  1291. FIRDatabaseReference *reader = refs.two;
  1292. __block BOOL ready = NO;
  1293. [writer removeValue];
  1294. [[writer child:@"foo"] setValue:@"hi"
  1295. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1296. [reader observeEventType:FIRDataEventTypeValue
  1297. withBlock:^(FIRDataSnapshot *snapshot) {
  1298. NSDictionary *val = [snapshot value];
  1299. NSDictionary *expected = @{@"foo" : @"hi"};
  1300. XCTAssertTrue([val isEqualToDictionary:expected], @"Got child");
  1301. ready = YES;
  1302. }];
  1303. }];
  1304. [self waitUntil:^BOOL {
  1305. return ready;
  1306. }];
  1307. }
  1308. - (void)testAccessingInvalidPathsThrows {
  1309. NSArray *badPaths = @[ @".test", @"test.", @"fo$o", @"[what", @"ever]", @"ha#sh" ];
  1310. for (NSString *key in badPaths) {
  1311. NSString *url = [NSString stringWithFormat:@"%@/%@", self.databaseURL, key];
  1312. XCTAssertThrows(
  1313. ^{
  1314. FIRDatabaseReference *ref = [[FIRDatabase database] referenceFromURL:url];
  1315. XCTFail(@"Should not get here with ref: %@", ref);
  1316. }(),
  1317. @"should throw");
  1318. url = [NSString stringWithFormat:@"%@/TESTS/%@", self.databaseURL, key];
  1319. XCTAssertThrows(
  1320. ^{
  1321. FIRDatabaseReference *ref = [[FIRDatabase database] referenceFromURL:url];
  1322. XCTFail(@"Should not get here with ref: %@", ref);
  1323. }(),
  1324. @"should throw");
  1325. }
  1326. __block BOOL ready = NO;
  1327. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  1328. [ref observeEventType:FIRDataEventTypeValue
  1329. withBlock:^(FIRDataSnapshot *snapshot) {
  1330. for (NSString *key in badPaths) {
  1331. XCTAssertThrows([snapshot childSnapshotForPath:key], @"should throw");
  1332. XCTAssertThrows([snapshot hasChild:key], @"should throw");
  1333. }
  1334. ready = YES;
  1335. }];
  1336. [ref setValue:nil];
  1337. [self waitUntil:^BOOL {
  1338. return ready;
  1339. }];
  1340. }
  1341. - (void)testSettingObjectsAtInvalidKeysThrow {
  1342. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  1343. NSArray *badPaths = @[
  1344. @".test", @"test.", @"fo$o", @"[what", @"ever]", @"ha#sh", @"/thing", @"th/ing", @"thing/"
  1345. ];
  1346. NSMutableArray *badObjs = [[NSMutableArray alloc] init];
  1347. for (NSString *key in badPaths) {
  1348. [badObjs addObject:@{key : @"test"}];
  1349. [badObjs addObject:@{@"deeper" : @{key : @"test"}}];
  1350. }
  1351. for (NSDictionary *badObj in badObjs) {
  1352. XCTAssertThrows([ref setValue:badObj], @"Should throw");
  1353. XCTAssertThrows([ref setValue:badObj andPriority:@5], @"Should throw");
  1354. XCTAssertThrows([ref onDisconnectSetValue:badObj], @"Should throw");
  1355. XCTAssertThrows([ref onDisconnectSetValue:badObj andPriority:@5], @"Should throw");
  1356. // XXX transaction
  1357. }
  1358. }
  1359. - (void)testSettingInvalidObjectsThrow {
  1360. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  1361. XCTAssertThrows([ref setValue:[NSDate date]], @"Should throw");
  1362. NSDictionary *data = @{@"invalid" : @"data", @".sv" : @"timestamp"};
  1363. XCTAssertThrows([ref setValue:data], @"Should throw");
  1364. data = @{@".value" : @{}};
  1365. XCTAssertThrows([ref setValue:data], @"Should throw");
  1366. }
  1367. - (void)testInvalidUpdateThrow {
  1368. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  1369. NSArray *badUpdates = @[
  1370. @{@"/" : @"t", @"a" : @"t"}, @{@"a" : @"t", @"a/b" : @"t"}, @{@"/a" : @"t", @"a/b" : @"t"},
  1371. @{@"/a/b" : @"t", @"a" : @"t"}, @{@"/a/b/.priority" : @"t", @"/a/b" : @"t"},
  1372. @{@"/a/b/.sv" : @"timestamp"}, @{@"/a/b/.value" : @"t"}, @{@"/a/b/.priority" : @{@"x" : @"y"}}
  1373. ];
  1374. for (NSDictionary *update in badUpdates) {
  1375. XCTAssertThrows([ref updateChildValues:update], @"Should throw");
  1376. XCTAssertThrows([ref onDisconnectUpdateChildValues:update], @"Should throw");
  1377. }
  1378. }
  1379. - (void)testSettingNull {
  1380. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  1381. XCTAssertNoThrow([ref setValue:nil], @"Should not throw");
  1382. XCTAssertNoThrow([ref setValue:[NSNull null]], @"Should not throw");
  1383. }
  1384. - (void)testSettingNaN {
  1385. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  1386. XCTAssertThrows([ref setValue:[NSDecimalNumber notANumber]], @"Should throw");
  1387. }
  1388. - (void)testSettingInvalidPriority {
  1389. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  1390. XCTAssertThrows([ref setValue:@"3" andPriority:[NSDecimalNumber notANumber]], @"Should throw");
  1391. XCTAssertThrows([ref setValue:@"4" andPriority:@{}], @"Should throw");
  1392. XCTAssertThrows([ref setValue:@"5" andPriority:@[]], @"Should throw");
  1393. }
  1394. - (void)testRemoveFromOnMobileGraffitiBugAtAngelHack {
  1395. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  1396. __block BOOL done = NO;
  1397. [node observeEventType:FIRDataEventTypeChildAdded
  1398. withBlock:^(FIRDataSnapshot *snapshot) {
  1399. [[node child:[snapshot key]]
  1400. removeValueWithCompletionBlock:^(NSError *err, FIRDatabaseReference *ref) {
  1401. done = YES;
  1402. }];
  1403. }];
  1404. [[node childByAutoId] setValue:@"moo"];
  1405. [self waitUntil:^BOOL {
  1406. return done;
  1407. }];
  1408. XCTAssertTrue(done, @"Properly finished");
  1409. }
  1410. - (void)testSetANodeWithAQuotedKey {
  1411. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  1412. __block BOOL done = NO;
  1413. __block FIRDataSnapshot *snap;
  1414. [node observeEventType:FIRDataEventTypeValue
  1415. withBlock:^(FIRDataSnapshot *snapshot) {
  1416. snap = snapshot;
  1417. }];
  1418. [node setValue:@{@"\"herp\"" : @1234}
  1419. withCompletionBlock:^(NSError *err, FIRDatabaseReference *ref) {
  1420. done = YES;
  1421. XCTAssertEqualObjects(@1234, [[snap childSnapshotForPath:@"\"herp\""] value],
  1422. @"Got it back");
  1423. }];
  1424. [self waitUntil:^BOOL {
  1425. return done;
  1426. }];
  1427. XCTAssertTrue(done, @"Properly finished");
  1428. }
  1429. - (void)testSetANodeWithASingleQuoteKey {
  1430. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  1431. __block BOOL done = NO;
  1432. __block FIRDataSnapshot *snap;
  1433. [node observeEventType:FIRDataEventTypeValue
  1434. withBlock:^(FIRDataSnapshot *snapshot) {
  1435. snap = snapshot;
  1436. }];
  1437. [node setValue:@{@"\"" : @1234}
  1438. withCompletionBlock:^(NSError *err, FIRDatabaseReference *ref) {
  1439. done = YES;
  1440. XCTAssertEqualObjects(@1234, [[snap childSnapshotForPath:@"\""] value], @"Got it back");
  1441. }];
  1442. [self waitUntil:^BOOL {
  1443. return done;
  1444. }];
  1445. XCTAssertTrue(done, @"Properly finished");
  1446. }
  1447. - (void)testEmptyChildGetValueEventBeforeParent {
  1448. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  1449. NSArray *lookingFor = @[
  1450. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a/aa/aaa"]
  1451. withEvent:FIRDataEventTypeValue
  1452. withString:nil],
  1453. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a/aa"]
  1454. withEvent:FIRDataEventTypeValue
  1455. withString:nil],
  1456. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a"]
  1457. withEvent:FIRDataEventTypeValue
  1458. withString:nil],
  1459. ];
  1460. FEventTester *et = [[FEventTester alloc] initFrom:self];
  1461. [et addLookingFor:lookingFor];
  1462. [node setValue:@{@"b" : @5}];
  1463. [et wait];
  1464. }
  1465. // iOS behavior is different from what the recursive set test looks for. We don't raise events
  1466. // synchronously
  1467. - (void)testOnAfterSetWaitsForLatestData {
  1468. // We test here that we don't cache sets, but they would be persisted so make sure we are running
  1469. // without persistence
  1470. FTupleFirebase *refs = [FTestHelpers getRandomNodePairWithoutPersistence];
  1471. FIRDatabaseReference *node1 = refs.one;
  1472. FIRDatabaseReference *node2 = refs.two;
  1473. __block BOOL ready = NO;
  1474. [node1 setValue:@5
  1475. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1476. [node2 setValue:@42
  1477. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1478. ready = YES;
  1479. }];
  1480. }];
  1481. [self waitUntil:^BOOL {
  1482. return ready;
  1483. }];
  1484. ready = NO;
  1485. [node1 observeEventType:FIRDataEventTypeValue
  1486. withBlock:^(FIRDataSnapshot *snapshot) {
  1487. NSNumber *val = [snapshot value];
  1488. XCTAssertTrue([val isEqualToNumber:@42], @"Should not have cached earlier set");
  1489. ready = YES;
  1490. }];
  1491. [self waitUntil:^BOOL {
  1492. return ready;
  1493. }];
  1494. }
  1495. - (void)testOnceWaitsForLatestData {
  1496. // Can't tolerate stale data; so disable persistence.
  1497. FTupleFirebase *refs = [FTestHelpers getRandomNodePairWithoutPersistence];
  1498. FIRDatabaseReference *node1 = refs.one;
  1499. FIRDatabaseReference *node2 = refs.two;
  1500. __block BOOL ready = NO;
  1501. [node1 observeSingleEventOfType:FIRDataEventTypeValue
  1502. withBlock:^(FIRDataSnapshot *snapshot) {
  1503. id val = [snapshot value];
  1504. XCTAssertTrue([NSNull null] == val, @"First value should be null");
  1505. [node2 setValue:@5
  1506. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1507. [node1 observeSingleEventOfType:FIRDataEventTypeValue
  1508. withBlock:^(FIRDataSnapshot *snapshot) {
  1509. NSNumber *val = [snapshot value];
  1510. XCTAssertTrue(
  1511. [val isKindOfClass:[NSNumber class]] &&
  1512. [val isEqualToNumber:@5],
  1513. @"Should get first value");
  1514. ready = YES;
  1515. }];
  1516. }];
  1517. }];
  1518. [self waitUntil:^BOOL {
  1519. return ready;
  1520. }];
  1521. ready = NO;
  1522. [node2 setValue:@42
  1523. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1524. [node1 observeSingleEventOfType:FIRDataEventTypeValue
  1525. withBlock:^(FIRDataSnapshot *snapshot) {
  1526. NSNumber *val = [snapshot value];
  1527. XCTAssertTrue([val isEqualToNumber:@42], @"Got second number");
  1528. ready = YES;
  1529. }];
  1530. }];
  1531. [self waitUntil:^BOOL {
  1532. return ready;
  1533. }];
  1534. }
  1535. - (void)testMemoryFreeingOnUnlistenDoesNotCorruptData {
  1536. // Can't tolerate stale data; so disable persistence.
  1537. FTupleFirebase *refs = [FTestHelpers getRandomNodePairWithoutPersistence];
  1538. FIRDatabaseReference *node2 = [[refs.one root] childByAutoId];
  1539. __block BOOL hasRun = NO;
  1540. __block BOOL ready = NO;
  1541. FIRDatabaseHandle handle1 =
  1542. [refs.one observeEventType:FIRDataEventTypeValue
  1543. withBlock:^(FIRDataSnapshot *snapshot) {
  1544. if (!hasRun) {
  1545. hasRun = YES;
  1546. id val = [snapshot value];
  1547. XCTAssertTrue([NSNull null] == val, @"First time should be null");
  1548. [refs.one setValue:@"test"
  1549. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1550. ready = YES;
  1551. }];
  1552. }
  1553. }];
  1554. [self waitUntil:^BOOL {
  1555. return ready;
  1556. }];
  1557. [refs.one removeObserverWithHandle:handle1];
  1558. ready = NO;
  1559. [node2 setValue:@"hello"
  1560. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1561. [refs.one
  1562. observeSingleEventOfType:FIRDataEventTypeValue
  1563. withBlock:^(FIRDataSnapshot *snapshot) {
  1564. NSString *val = [snapshot value];
  1565. XCTAssertTrue([val isEqualToString:@"test"],
  1566. @"Get back the value we set above");
  1567. [refs.two
  1568. observeSingleEventOfType:FIRDataEventTypeValue
  1569. withBlock:^(FIRDataSnapshot *snapshot) {
  1570. NSString *val = [snapshot value];
  1571. XCTAssertTrue([val isEqualToString:@"test"],
  1572. @"Get back the value we set above");
  1573. ready = YES;
  1574. }];
  1575. }];
  1576. }];
  1577. [self waitUntil:^BOOL {
  1578. return ready;
  1579. }];
  1580. // write {x: 1, y : {t: 2, u: 3}}
  1581. // Listen at /. Then listen at /x/t
  1582. // unlisten at /y/t. Off at /. Once at /. Ensure data is still all there.
  1583. // Once at /y. Ensure data is still all there.
  1584. refs = [FTestHelpers getRandomNodePairWithoutPersistence];
  1585. ready = NO;
  1586. __block FIRDatabaseHandle deeplisten = NSNotFound;
  1587. __block FIRDatabaseHandle slashlisten = NSNotFound;
  1588. __weak FIRDatabaseReference *refOne = refs.one;
  1589. [refs.one setValue:@{@"x" : @1, @"y" : @{@"t" : @2, @"u" : @3}}
  1590. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1591. slashlisten = [refOne
  1592. observeEventType:FIRDataEventTypeValue
  1593. withBlock:^(FIRDataSnapshot *snapshot) {
  1594. deeplisten = [[refOne child:@"y/t"]
  1595. observeEventType:FIRDataEventTypeValue
  1596. withBlock:^(FIRDataSnapshot *snapshot) {
  1597. [[refOne child:@"y/t"] removeObserverWithHandle:deeplisten];
  1598. [refOne removeObserverWithHandle:slashlisten];
  1599. ready = YES;
  1600. }];
  1601. }];
  1602. }];
  1603. [self waitUntil:^BOOL {
  1604. return ready;
  1605. }];
  1606. ready = NO;
  1607. [[refs.one child:@"x"] setValue:@"test"
  1608. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1609. [refs.one observeSingleEventOfType:FIRDataEventTypeValue
  1610. withBlock:^(FIRDataSnapshot *snapshot) {
  1611. NSDictionary *val = [snapshot value];
  1612. NSDictionary *expected =
  1613. @{@"x" : @"test",
  1614. @"y" : @{@"t" : @2, @"u" : @3}};
  1615. XCTAssertTrue([val isEqualToDictionary:expected],
  1616. @"Got the final value");
  1617. ready = YES;
  1618. }];
  1619. }];
  1620. [self waitUntil:^BOOL {
  1621. return ready;
  1622. }];
  1623. }
  1624. - (void)testUpdateRaisesCorrectLocalEvents {
  1625. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  1626. __block FIRDataSnapshot *snap = nil;
  1627. [node observeSingleEventOfType:FIRDataEventTypeValue
  1628. withBlock:^(FIRDataSnapshot *snapshot) {
  1629. snap = snapshot;
  1630. }];
  1631. __block BOOL ready = NO;
  1632. [node setValue:@{@"a" : @1, @"b" : @2, @"c" : @3, @"d" : @4}
  1633. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1634. ready = YES;
  1635. }];
  1636. [self waitUntil:^BOOL {
  1637. return ready;
  1638. }];
  1639. FEventTester *et = [[FEventTester alloc] initFrom:self];
  1640. NSArray *expectations = @[
  1641. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a"]
  1642. withEvent:FIRDataEventTypeValue
  1643. withString:nil],
  1644. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"d"]
  1645. withEvent:FIRDataEventTypeValue
  1646. withString:nil],
  1647. [[FTupleEventTypeString alloc] initWithFirebase:node
  1648. withEvent:FIRDataEventTypeChildChanged
  1649. withString:@"a"],
  1650. [[FTupleEventTypeString alloc] initWithFirebase:node
  1651. withEvent:FIRDataEventTypeChildChanged
  1652. withString:@"d"],
  1653. [[FTupleEventTypeString alloc] initWithFirebase:node
  1654. withEvent:FIRDataEventTypeValue
  1655. withString:nil]
  1656. ];
  1657. [et addLookingFor:expectations];
  1658. [et waitForInitialization];
  1659. [node updateChildValues:@{@"a" : @4, @"d" : @1}];
  1660. [et wait];
  1661. }
  1662. - (void)testUpdateRaisesCorrectRemoteEvents {
  1663. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  1664. FIRDatabaseReference *reader = refs.one;
  1665. FIRDatabaseReference *writer = refs.two;
  1666. __block BOOL ready = NO;
  1667. [writer setValue:@{@"a" : @1, @"b" : @2, @"c" : @3, @"d" : @4}
  1668. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1669. ready = YES;
  1670. }];
  1671. [self waitUntil:^BOOL {
  1672. return ready;
  1673. }];
  1674. FEventTester *et = [[FEventTester alloc] initFrom:self];
  1675. NSArray *expectations = @[
  1676. [[FTupleEventTypeString alloc] initWithFirebase:[reader child:@"a"]
  1677. withEvent:FIRDataEventTypeValue
  1678. withString:nil],
  1679. [[FTupleEventTypeString alloc] initWithFirebase:[reader child:@"d"]
  1680. withEvent:FIRDataEventTypeValue
  1681. withString:nil],
  1682. [[FTupleEventTypeString alloc] initWithFirebase:reader
  1683. withEvent:FIRDataEventTypeChildChanged
  1684. withString:@"a"],
  1685. [[FTupleEventTypeString alloc] initWithFirebase:reader
  1686. withEvent:FIRDataEventTypeChildChanged
  1687. withString:@"d"],
  1688. [[FTupleEventTypeString alloc] initWithFirebase:reader
  1689. withEvent:FIRDataEventTypeValue
  1690. withString:nil]
  1691. ];
  1692. [et addLookingFor:expectations];
  1693. [et waitForInitialization];
  1694. [writer updateChildValues:@{@"a" : @4, @"d" : @1}];
  1695. [et wait];
  1696. ready = NO;
  1697. [reader observeEventType:FIRDataEventTypeValue
  1698. withBlock:^(FIRDataSnapshot *snapshot) {
  1699. NSDictionary *result = [snapshot value];
  1700. NSDictionary *expected = @{@"a" : @4, @"b" : @2, @"c" : @3, @"d" : @1};
  1701. XCTAssertTrue([result isEqualToDictionary:expected], @"Got expected results");
  1702. ready = YES;
  1703. }];
  1704. [self waitUntil:^BOOL {
  1705. return ready;
  1706. }];
  1707. }
  1708. - (void)testUpdateChangesAreStoredCorrectlyByTheServer {
  1709. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  1710. FIRDatabaseReference *reader = refs.one;
  1711. FIRDatabaseReference *writer = refs.two;
  1712. [self waitForCompletionOf:writer setValue:@{@"a" : @1, @"b" : @2, @"c" : @3, @"d" : @4}];
  1713. [self waitForCompletionOf:writer updateChildValues:@{@"a" : @42}];
  1714. [self snapWaiter:reader
  1715. withBlock:^(FIRDataSnapshot *snapshot) {
  1716. NSDictionary *result = [snapshot value];
  1717. NSDictionary *expected = @{@"a" : @42, @"b" : @2, @"c" : @3, @"d" : @4};
  1718. XCTAssertTrue([result isEqualToDictionary:expected], @"Expected updated value");
  1719. }];
  1720. }
  1721. - (void)testUpdateDoesntAffectPriorityLocally {
  1722. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  1723. __block FIRDataSnapshot *snap = nil;
  1724. [ref observeEventType:FIRDataEventTypeValue
  1725. withBlock:^(FIRDataSnapshot *snapshot) {
  1726. snap = snapshot;
  1727. }];
  1728. [ref setValue:@{@"a" : @1, @"b" : @2, @"c" : @3} andPriority:@"testpri"];
  1729. [self waitUntil:^BOOL {
  1730. return snap != nil;
  1731. }];
  1732. XCTAssertTrue([[snap priority] isEqualToString:@"testpri"], @"Got initial priority");
  1733. snap = nil;
  1734. [ref updateChildValues:@{@"a" : @4}];
  1735. [self waitUntil:^BOOL {
  1736. return snap != nil;
  1737. }];
  1738. XCTAssertTrue([[snap priority] isEqualToString:@"testpri"], @"Got initial priority");
  1739. }
  1740. - (void)testUpdateDoesntAffectPriorityRemotely {
  1741. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  1742. FIRDatabaseReference *reader = refs.one;
  1743. FIRDatabaseReference *writer = refs.two;
  1744. __block BOOL ready = NO;
  1745. [writer setValue:@{@"a" : @1, @"b" : @2, @"c" : @3}
  1746. andPriority:@"testpri"
  1747. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1748. ready = YES;
  1749. }];
  1750. [self waitUntil:^BOOL {
  1751. return ready;
  1752. }];
  1753. ready = NO;
  1754. [reader observeSingleEventOfType:FIRDataEventTypeValue
  1755. withBlock:^(FIRDataSnapshot *snapshot) {
  1756. NSString *result = [snapshot priority];
  1757. XCTAssertTrue([result isEqualToString:@"testpri"],
  1758. @"Expected initial priority");
  1759. ready = YES;
  1760. }];
  1761. [self waitUntil:^BOOL {
  1762. return ready;
  1763. }];
  1764. ready = NO;
  1765. [writer updateChildValues:@{@"a" : @4}
  1766. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1767. ready = YES;
  1768. }];
  1769. [self waitUntil:^BOOL {
  1770. return ready;
  1771. }];
  1772. ready = NO;
  1773. [reader observeSingleEventOfType:FIRDataEventTypeValue
  1774. withBlock:^(FIRDataSnapshot *snapshot) {
  1775. NSString *result = [snapshot priority];
  1776. XCTAssertTrue([result isEqualToString:@"testpri"],
  1777. @"Expected initial priority");
  1778. ready = YES;
  1779. }];
  1780. [self waitUntil:^BOOL {
  1781. return ready;
  1782. }];
  1783. }
  1784. - (void)testUpdateReplacesChildrenAndIsNotRecursive {
  1785. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  1786. FIRDatabaseReference *reader = refs.one;
  1787. FIRDatabaseReference *writer = refs.two;
  1788. __block FIRDataSnapshot *localSnap = nil;
  1789. __block BOOL ready = NO;
  1790. [writer observeEventType:FIRDataEventTypeValue
  1791. withBlock:^(FIRDataSnapshot *snapshot) {
  1792. localSnap = snapshot;
  1793. }];
  1794. [writer setValue:@{@"a" : @{@"aa" : @1, @"ab" : @2}}];
  1795. [writer updateChildValues:@{@"a" : @{@"aa" : @1}}
  1796. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1797. ready = YES;
  1798. }];
  1799. [self waitUntil:^BOOL {
  1800. return ready;
  1801. }];
  1802. ready = NO;
  1803. [reader observeSingleEventOfType:FIRDataEventTypeValue
  1804. withBlock:^(FIRDataSnapshot *snapshot) {
  1805. NSDictionary *result = [snapshot value];
  1806. NSDictionary *expected = @{@"a" : @{@"aa" : @1}};
  1807. XCTAssertTrue([result isEqualToDictionary:expected],
  1808. @"Should get new value");
  1809. ready = YES;
  1810. }];
  1811. [self waitUntil:^BOOL {
  1812. NSDictionary *result = [localSnap value];
  1813. NSDictionary *expected = @{@"a" : @{@"aa" : @1}};
  1814. return ready && [result isEqualToDictionary:expected];
  1815. }];
  1816. }
  1817. - (void)testDeepUpdatesWork {
  1818. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  1819. FIRDatabaseReference *reader = refs.one;
  1820. FIRDatabaseReference *writer = refs.two;
  1821. __block FIRDataSnapshot *localSnap = nil;
  1822. __block BOOL ready = NO;
  1823. [writer observeEventType:FIRDataEventTypeValue
  1824. withBlock:^(FIRDataSnapshot *snapshot) {
  1825. localSnap = snapshot;
  1826. }];
  1827. [writer setValue:@{@"a" : @{@"aa" : @1, @"ab" : @2}}];
  1828. [writer updateChildValues:@{
  1829. @"a/aa" : @10,
  1830. @".priority" : @3.0,
  1831. @"a/ab" : @{@".priority" : @2.0, @".value" : @20}
  1832. }
  1833. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1834. ready = YES;
  1835. }];
  1836. [self waitUntil:^BOOL {
  1837. return ready;
  1838. }];
  1839. ready = NO;
  1840. [reader observeSingleEventOfType:FIRDataEventTypeValue
  1841. withBlock:^(FIRDataSnapshot *snapshot) {
  1842. NSDictionary *result = [snapshot value];
  1843. NSDictionary *expected = @{@"a" : @{@"aa" : @10, @"ab" : @20}};
  1844. XCTAssertTrue([result isEqualToDictionary:expected],
  1845. @"Should get new value");
  1846. ready = YES;
  1847. }];
  1848. [self waitUntil:^BOOL {
  1849. NSDictionary *result = [localSnap value];
  1850. NSDictionary *expected = @{@"a" : @{@"aa" : @10, @"ab" : @20}};
  1851. return ready && [result isEqualToDictionary:expected];
  1852. }];
  1853. }
  1854. // Type signature means we don't need a test for updating scalars. They wouldn't compile
  1855. - (void)testEmptyUpdateWorks {
  1856. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  1857. __block BOOL ready = NO;
  1858. [ref updateChildValues:@{}
  1859. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1860. XCTAssertTrue(error == nil, @"Should not be an error");
  1861. ready = YES;
  1862. }];
  1863. [self waitUntil:^BOOL {
  1864. return ready;
  1865. }];
  1866. }
  1867. // XXX update stress test
  1868. - (void)testUpdateFiresCorrectEventWhenAChildIsDeleted {
  1869. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  1870. FIRDatabaseReference *reader = refs.one;
  1871. FIRDatabaseReference *writer = refs.two;
  1872. __block FIRDataSnapshot *localSnap = nil;
  1873. __block FIRDataSnapshot *remoteSnap = nil;
  1874. [self waitForCompletionOf:writer setValue:@{@"a" : @12, @"b" : @6}];
  1875. [writer observeEventType:FIRDataEventTypeValue
  1876. withBlock:^(FIRDataSnapshot *snapshot) {
  1877. localSnap = snapshot;
  1878. }];
  1879. [reader observeEventType:FIRDataEventTypeValue
  1880. withBlock:^(FIRDataSnapshot *snapshot) {
  1881. remoteSnap = snapshot;
  1882. }];
  1883. [self waitUntil:^BOOL {
  1884. return localSnap != nil && remoteSnap != nil;
  1885. }];
  1886. localSnap = nil;
  1887. remoteSnap = nil;
  1888. [writer updateChildValues:@{@"a" : [NSNull null]}];
  1889. [self waitUntil:^BOOL {
  1890. return localSnap != nil && remoteSnap != nil;
  1891. }];
  1892. NSDictionary *expected = @{@"b" : @6};
  1893. XCTAssertTrue([[remoteSnap value] isEqualToDictionary:expected], @"Removed child");
  1894. XCTAssertTrue([[localSnap value] isEqualToDictionary:expected], @"Removed child");
  1895. }
  1896. - (void)testUpdateFiresCorrectEventOnNewChildren {
  1897. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  1898. FIRDatabaseReference *reader = refs.one;
  1899. FIRDatabaseReference *writer = refs.two;
  1900. __block FIRDataSnapshot *localSnap = nil;
  1901. __block FIRDataSnapshot *remoteSnap = nil;
  1902. [[writer child:@"a"] observeEventType:FIRDataEventTypeValue
  1903. withBlock:^(FIRDataSnapshot *snapshot) {
  1904. localSnap = snapshot;
  1905. }];
  1906. [[reader child:@"a"] observeEventType:FIRDataEventTypeValue
  1907. withBlock:^(FIRDataSnapshot *snapshot) {
  1908. remoteSnap = snapshot;
  1909. }];
  1910. [self waitUntil:^BOOL {
  1911. return localSnap != nil && remoteSnap != nil;
  1912. }];
  1913. localSnap = nil;
  1914. remoteSnap = nil;
  1915. [writer updateChildValues:@{@"a" : @42}];
  1916. [self waitUntil:^BOOL {
  1917. return localSnap != nil && remoteSnap != nil;
  1918. }];
  1919. XCTAssertTrue([[remoteSnap value] isEqualToNumber:@42], @"Added child");
  1920. XCTAssertTrue([[localSnap value] isEqualToNumber:@42], @"Added child");
  1921. }
  1922. - (void)testUpdateFiresCorrectEventOnDeletedChildren {
  1923. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  1924. FIRDatabaseReference *reader = refs.one;
  1925. FIRDatabaseReference *writer = refs.two;
  1926. __block FIRDataSnapshot *localSnap = nil;
  1927. __block FIRDataSnapshot *remoteSnap = nil;
  1928. [self waitForCompletionOf:writer setValue:@{@"a" : @12}];
  1929. [[writer child:@"a"] observeEventType:FIRDataEventTypeValue
  1930. withBlock:^(FIRDataSnapshot *snapshot) {
  1931. localSnap = snapshot;
  1932. }];
  1933. [[reader child:@"a"] observeEventType:FIRDataEventTypeValue
  1934. withBlock:^(FIRDataSnapshot *snapshot) {
  1935. remoteSnap = snapshot;
  1936. }];
  1937. [self waitUntil:^BOOL {
  1938. return localSnap != nil && remoteSnap != nil;
  1939. }];
  1940. localSnap = nil;
  1941. remoteSnap = nil;
  1942. [writer updateChildValues:@{@"a" : [NSNull null]}];
  1943. [self waitUntil:^BOOL {
  1944. return localSnap != nil && remoteSnap != nil;
  1945. }];
  1946. XCTAssertTrue([remoteSnap value] == [NSNull null], @"Removed child");
  1947. XCTAssertTrue([localSnap value] == [NSNull null], @"Removed child");
  1948. }
  1949. - (void)testUpdateFiresCorrectEventOnChangedChildren {
  1950. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  1951. FIRDatabaseReference *reader = refs.one;
  1952. FIRDatabaseReference *writer = refs.two;
  1953. [self waitForCompletionOf:writer setValue:@{@"a" : @12}];
  1954. __block FIRDataSnapshot *localSnap = nil;
  1955. __block FIRDataSnapshot *remoteSnap = nil;
  1956. [[writer child:@"a"] observeEventType:FIRDataEventTypeValue
  1957. withBlock:^(FIRDataSnapshot *snapshot) {
  1958. localSnap = snapshot;
  1959. }];
  1960. [[reader child:@"a"] observeEventType:FIRDataEventTypeValue
  1961. withBlock:^(FIRDataSnapshot *snapshot) {
  1962. remoteSnap = snapshot;
  1963. }];
  1964. [self waitUntil:^BOOL {
  1965. return localSnap != nil && remoteSnap != nil;
  1966. }];
  1967. localSnap = nil;
  1968. remoteSnap = nil;
  1969. [self waitForCompletionOf:writer updateChildValues:@{@"a" : @11}];
  1970. [self waitUntil:^BOOL {
  1971. return localSnap != nil && remoteSnap != nil;
  1972. }];
  1973. XCTAssertTrue([[remoteSnap value] isEqualToNumber:@11], @"Changed child");
  1974. XCTAssertTrue([[localSnap value] isEqualToNumber:@11], @"Changed child");
  1975. }
  1976. - (void)testUpdateOfPriorityWorks {
  1977. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  1978. FIRDatabaseReference *reader = refs.one;
  1979. FIRDatabaseReference *writer = refs.two;
  1980. __block BOOL ready = NO;
  1981. [writer setValue:@{@"a" : @5, @".priority" : @"pri1"}];
  1982. [writer updateChildValues:@{
  1983. @"a" : @6,
  1984. @".priority" : @"pri2",
  1985. @"b" : @{@".priority" : @"pri3", @"c" : @10}
  1986. }
  1987. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1988. NSLog(@"error? %@", error);
  1989. ready = YES;
  1990. }];
  1991. [self waitUntil:^BOOL {
  1992. return ready;
  1993. }];
  1994. ready = NO;
  1995. [reader observeEventType:FIRDataEventTypeValue
  1996. withBlock:^(FIRDataSnapshot *snapshot) {
  1997. XCTAssertEqualObjects([[snapshot childSnapshotForPath:@"a"] value], @6,
  1998. @"Should match write values");
  1999. XCTAssertTrue([[snapshot priority] isEqualToString:@"pri2"],
  2000. @"Should get updated priority");
  2001. XCTAssertTrue(
  2002. [[[snapshot childSnapshotForPath:@"b"] priority] isEqualToString:@"pri3"],
  2003. @"Should get updated priority");
  2004. XCTAssertEqualObjects([[snapshot childSnapshotForPath:@"b/c"] value], @10,
  2005. @"Should match write values");
  2006. ready = YES;
  2007. }];
  2008. [self waitUntil:^BOOL {
  2009. return ready;
  2010. }];
  2011. }
  2012. - (void)testSetWithCircularReferenceFails {
  2013. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  2014. NSMutableDictionary *toSet = [[NSMutableDictionary alloc] init];
  2015. NSDictionary *lol = @{@"foo" : @"bar", @"circular" : toSet};
  2016. [toSet setObject:lol forKey:@"lol"];
  2017. XCTAssertThrows([ref setValue:toSet], @"Should not be able to set circular dictionary");
  2018. }
  2019. - (void)testLargeNumbers {
  2020. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  2021. long long jsMaxInt = 9007199254740992;
  2022. long jsMaxIntPlusOne = (long)jsMaxInt + 1;
  2023. NSNumber *toSet = [NSNumber numberWithLong:jsMaxIntPlusOne];
  2024. [ref setValue:toSet];
  2025. __block FIRDataSnapshot *snap = nil;
  2026. [ref observeEventType:FIRDataEventTypeValue
  2027. withBlock:^(FIRDataSnapshot *snapshot) {
  2028. snap = snapshot;
  2029. }];
  2030. [self waitUntil:^BOOL {
  2031. return snap != nil;
  2032. }];
  2033. NSNumber *result = [snap value];
  2034. XCTAssertTrue([result isEqualToNumber:toSet], @"Should get back same number");
  2035. toSet = [NSNumber numberWithLong:LONG_MAX];
  2036. snap = nil;
  2037. [ref setValue:toSet];
  2038. [self waitUntil:^BOOL {
  2039. return snap != nil;
  2040. }];
  2041. result = [snap value];
  2042. XCTAssertTrue([result isEqualToNumber:toSet], @"Should get back same number");
  2043. snap = nil;
  2044. toSet = [NSNumber numberWithDouble:DBL_MAX];
  2045. [ref setValue:toSet];
  2046. [self waitUntil:^BOOL {
  2047. return snap != nil;
  2048. }];
  2049. result = [snap value];
  2050. XCTAssertTrue([result isEqualToNumber:toSet], @"Should get back same number");
  2051. }
  2052. #ifdef FLAKY_TEST
  2053. - (void)testParentDeleteShadowsChildListeners {
  2054. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  2055. FIRDatabaseReference *writer = refs.one;
  2056. FIRDatabaseReference *deleter = refs.two;
  2057. NSString *childName = [writer childByAutoId].key;
  2058. __block BOOL called = NO;
  2059. [[deleter child:childName]
  2060. observeEventType:FIRDataEventTypeValue
  2061. withBlock:^(FIRDataSnapshot *snapshot) {
  2062. XCTAssertFalse(called, @"Should only be hit once");
  2063. called = YES;
  2064. XCTAssertTrue(snapshot.value == [NSNull null], @"Value should be null");
  2065. }];
  2066. WAIT_FOR(called);
  2067. __block BOOL done = NO;
  2068. [[writer child:childName] setValue:@"foo"];
  2069. [deleter removeValueWithCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  2070. done = YES;
  2071. }];
  2072. WAIT_FOR(done);
  2073. [deleter removeAllObservers];
  2074. }
  2075. #endif
  2076. - (void)testParentDeleteShadowsChildListenersWithNonDefaultQuery {
  2077. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  2078. FIRDatabaseReference *writer = refs.one;
  2079. FIRDatabaseReference *deleter = refs.two;
  2080. NSString *childName = [writer childByAutoId].key;
  2081. __block BOOL queryCalled = NO;
  2082. __block BOOL deepChildCalled = NO;
  2083. [[[[deleter child:childName] queryOrderedByPriority] queryStartingAtValue:nil childKey:@"b"]
  2084. observeEventType:FIRDataEventTypeValue
  2085. withBlock:^(FIRDataSnapshot *snapshot) {
  2086. XCTAssertFalse(queryCalled, @"Should only be hit once");
  2087. queryCalled = YES;
  2088. XCTAssertTrue(snapshot.value == [NSNull null], @"Value should be null");
  2089. }];
  2090. [[[deleter child:childName] child:@"a"]
  2091. observeEventType:FIRDataEventTypeValue
  2092. withBlock:^(FIRDataSnapshot *snapshot) {
  2093. XCTAssertFalse(deepChildCalled, @"Should only be hit once");
  2094. deepChildCalled = YES;
  2095. XCTAssertTrue(snapshot.value == [NSNull null], @"Value should be null");
  2096. }];
  2097. WAIT_FOR(deepChildCalled && queryCalled);
  2098. __block BOOL done = NO;
  2099. [[writer child:childName] setValue:@"foo"];
  2100. [deleter removeValueWithCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  2101. done = YES;
  2102. }];
  2103. WAIT_FOR(done);
  2104. }
  2105. - (void)testLocalServerTimestampEventuallyButNotImmediatelyMatchServer {
  2106. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  2107. FIRDatabaseReference *writer = refs.one;
  2108. FIRDatabaseReference *reader = refs.two;
  2109. __block int done = 0;
  2110. NSMutableArray *readSnaps = [[NSMutableArray alloc] init];
  2111. NSMutableArray *writeSnaps = [[NSMutableArray alloc] init];
  2112. [reader observeEventType:FIRDataEventTypeValue
  2113. withBlock:^(FIRDataSnapshot *snapshot) {
  2114. if ([snapshot value] != [NSNull null]) {
  2115. [readSnaps addObject:snapshot];
  2116. if (readSnaps.count == 1) {
  2117. done += 1;
  2118. }
  2119. }
  2120. }];
  2121. [writer observeEventType:FIRDataEventTypeValue
  2122. withBlock:^(FIRDataSnapshot *snapshot) {
  2123. if ([snapshot value] != [NSNull null]) {
  2124. [writeSnaps addObject:snapshot];
  2125. if (writeSnaps.count == 2) {
  2126. done += 1;
  2127. }
  2128. }
  2129. }];
  2130. [writer setValue:[FIRServerValue timestamp] andPriority:[FIRServerValue timestamp]];
  2131. [self waitUntil:^BOOL {
  2132. return done == 2;
  2133. }];
  2134. XCTAssertEqual((unsigned long)[readSnaps count], (unsigned long)1,
  2135. @"Should have received one snapshot on reader");
  2136. XCTAssertEqual((unsigned long)[writeSnaps count], (unsigned long)2,
  2137. @"Should have received two snapshots on writer");
  2138. FIRDataSnapshot *firstReadSnap = [readSnaps objectAtIndex:0];
  2139. FIRDataSnapshot *firstWriteSnap = [writeSnaps objectAtIndex:0];
  2140. FIRDataSnapshot *secondWriteSnap = [writeSnaps objectAtIndex:1];
  2141. NSNumber *now = [NSNumber numberWithDouble:round([[NSDate date] timeIntervalSince1970] * 1000)];
  2142. XCTAssertTrue([now doubleValue] - [firstWriteSnap.value doubleValue] < 3000,
  2143. @"Should have received a local event with a value close to timestamp");
  2144. XCTAssertTrue([now doubleValue] - [firstWriteSnap.priority doubleValue] < 3000,
  2145. @"Should have received a local event with a priority close to timestamp");
  2146. XCTAssertTrue([now doubleValue] - [secondWriteSnap.value doubleValue] < 3000,
  2147. @"Should have received a server event with a value close to timestamp");
  2148. XCTAssertTrue([now doubleValue] - [secondWriteSnap.priority doubleValue] < 3000,
  2149. @"Should have received a server event with a priority close to timestamp");
  2150. XCTAssertFalse([firstWriteSnap value] == [secondWriteSnap value],
  2151. @"Initial and future writer values should be different");
  2152. XCTAssertFalse([firstWriteSnap priority] == [secondWriteSnap priority],
  2153. @"Initial and future writer priorities should be different");
  2154. XCTAssertEqualObjects(firstReadSnap.value, secondWriteSnap.value,
  2155. @"Eventual reader and writer values should be equal");
  2156. XCTAssertEqualObjects(firstReadSnap.priority, secondWriteSnap.priority,
  2157. @"Eventual reader and writer priorities should be equal");
  2158. }
  2159. - (void)testServerTimestampSetWithPriorityRemoteEvents {
  2160. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  2161. FIRDatabaseReference *writer = refs.one;
  2162. FIRDatabaseReference *reader = refs.two;
  2163. NSDictionary *data = @{
  2164. @"a" : [FIRServerValue timestamp],
  2165. @"b" : @{@".value" : [FIRServerValue timestamp], @".priority" : [FIRServerValue timestamp]}
  2166. };
  2167. __block BOOL done = NO;
  2168. [writer setValue:data
  2169. andPriority:[FIRServerValue timestamp]
  2170. withCompletionBlock:^(NSError *err, FIRDatabaseReference *ref) {
  2171. done = YES;
  2172. }];
  2173. [self waitUntil:^BOOL {
  2174. return done;
  2175. }];
  2176. [self
  2177. snapWaiter:reader
  2178. withBlock:^(FIRDataSnapshot *snapshot) {
  2179. NSDictionary *value = [snapshot value];
  2180. NSNumber *now =
  2181. [NSNumber numberWithDouble:round([[NSDate date] timeIntervalSince1970] * 1000)];
  2182. NSNumber *timestamp = [snapshot priority];
  2183. XCTAssertTrue([[snapshot priority] isKindOfClass:[NSNumber class]],
  2184. @"Should get back number");
  2185. XCTAssertTrue([now doubleValue] - [timestamp doubleValue] < 2000,
  2186. @"Number should be no more than 2 seconds ago");
  2187. XCTAssertEqualObjects([snapshot priority], [value objectForKey:@"a"],
  2188. @"Should get back matching ServerValue.TIMESTAMP");
  2189. XCTAssertEqualObjects([snapshot priority], [value objectForKey:@"b"],
  2190. @"Should get back matching ServerValue.TIMESTAMP");
  2191. XCTAssertEqualObjects([snapshot priority], [[snapshot childSnapshotForPath:@"b"] priority],
  2192. @"Should get back matching ServerValue.TIMESTAMP");
  2193. }];
  2194. }
  2195. - (void)testServerTimestampSetPriorityRemoteEvents {
  2196. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  2197. FIRDatabaseReference *writer = refs.one;
  2198. FIRDatabaseReference *reader = refs.two;
  2199. __block FIRDataSnapshot *snap = nil;
  2200. [reader observeEventType:FIRDataEventTypeChildMoved
  2201. withBlock:^(FIRDataSnapshot *snapshot) {
  2202. snap = snapshot;
  2203. }];
  2204. [self waitForCompletionOf:[writer child:@"a"] setValue:@1 andPriority:nil];
  2205. [self waitForCompletionOf:[writer child:@"b"] setValue:@1 andPriority:@1];
  2206. [self waitForValueOf:[reader child:@"a"] toBe:@1];
  2207. __block BOOL done = NO;
  2208. [[writer child:@"a"] setPriority:[FIRServerValue timestamp]
  2209. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  2210. done = YES;
  2211. }];
  2212. [self waitUntil:^BOOL {
  2213. return done && snap != nil;
  2214. }];
  2215. NSNumber *now = [NSNumber numberWithDouble:round([[NSDate date] timeIntervalSince1970] * 1000)];
  2216. NSNumber *timestamp = [snap priority];
  2217. XCTAssertTrue([[snap priority] isKindOfClass:[NSNumber class]], @"Should get back number");
  2218. XCTAssertTrue([now doubleValue] - [timestamp doubleValue] < 2000,
  2219. @"Number should be no more than 2 seconds ago");
  2220. }
  2221. - (void)testServerTimestampUpdateRemoteEvents {
  2222. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  2223. FIRDatabaseReference *writer = refs.one;
  2224. FIRDatabaseReference *reader = refs.two;
  2225. __block FIRDataSnapshot *snap = nil;
  2226. __block BOOL done = NO;
  2227. [reader observeEventType:FIRDataEventTypeValue
  2228. withBlock:^(FIRDataSnapshot *snapshot) {
  2229. snap = snapshot;
  2230. if (snap && [[snap childSnapshotForPath:@"a/b/d"] value] != [NSNull null]) {
  2231. done = YES;
  2232. }
  2233. }];
  2234. [[writer child:@"a/b/c"] setValue:@1];
  2235. [[writer child:@"a"] updateChildValues:@{@"b" : @{@"c" : [FIRServerValue timestamp], @"d" : @1}}];
  2236. [self waitUntil:^BOOL {
  2237. return done;
  2238. }];
  2239. NSNumber *now = [NSNumber numberWithDouble:round([[NSDate date] timeIntervalSince1970] * 1000)];
  2240. NSNumber *timestamp = [[snap childSnapshotForPath:@"a/b/c"] value];
  2241. XCTAssertTrue([[[snap childSnapshotForPath:@"a/b/c"] value] isKindOfClass:[NSNumber class]],
  2242. @"Should get back number");
  2243. XCTAssertTrue([now doubleValue] - [timestamp doubleValue] < 2000,
  2244. @"Number should be no more than 2 seconds ago");
  2245. }
  2246. - (void)testServerTimestampSetWithPriorityLocalEvents {
  2247. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  2248. NSDictionary *data = @{
  2249. @"a" : [FIRServerValue timestamp],
  2250. @"b" : @{@".value" : [FIRServerValue timestamp], @".priority" : [FIRServerValue timestamp]}
  2251. };
  2252. __block FIRDataSnapshot *snap = nil;
  2253. [node observeEventType:FIRDataEventTypeValue
  2254. withBlock:^(FIRDataSnapshot *snapshot) {
  2255. snap = snapshot;
  2256. }];
  2257. __block BOOL done = NO;
  2258. [node setValue:data
  2259. andPriority:[FIRServerValue timestamp]
  2260. withCompletionBlock:^(NSError *err, FIRDatabaseReference *ref) {
  2261. done = YES;
  2262. }];
  2263. [self waitUntil:^BOOL {
  2264. return done;
  2265. }];
  2266. [self
  2267. snapWaiter:node
  2268. withBlock:^(FIRDataSnapshot *snapshot) {
  2269. NSDictionary *value = [snapshot value];
  2270. NSNumber *now =
  2271. [NSNumber numberWithDouble:round([[NSDate date] timeIntervalSince1970] * 1000)];
  2272. NSNumber *timestamp = [snapshot priority];
  2273. XCTAssertTrue([[snapshot priority] isKindOfClass:[NSNumber class]],
  2274. @"Should get back number");
  2275. XCTAssertTrue([now doubleValue] - [timestamp doubleValue] < 2000,
  2276. @"Number should be no more than 2 seconds ago");
  2277. XCTAssertEqualObjects([snapshot priority], [value objectForKey:@"a"],
  2278. @"Should get back matching ServerValue.TIMESTAMP");
  2279. XCTAssertEqualObjects([snapshot priority], [value objectForKey:@"b"],
  2280. @"Should get back matching ServerValue.TIMESTAMP");
  2281. XCTAssertEqualObjects([snapshot priority], [[snapshot childSnapshotForPath:@"b"] priority],
  2282. @"Should get back matching ServerValue.TIMESTAMP");
  2283. }];
  2284. }
  2285. - (void)testServerTimestampSetPriorityLocalEvents {
  2286. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  2287. __block FIRDataSnapshot *snap = nil;
  2288. [node observeEventType:FIRDataEventTypeChildMoved
  2289. withBlock:^(FIRDataSnapshot *snapshot) {
  2290. snap = snapshot;
  2291. }];
  2292. __block BOOL done = NO;
  2293. [[node child:@"a"] setValue:@1 andPriority:nil];
  2294. [[node child:@"b"] setValue:@1 andPriority:@1];
  2295. [[node child:@"a"] setPriority:[FIRServerValue timestamp]
  2296. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  2297. done = YES;
  2298. }];
  2299. [self waitUntil:^BOOL {
  2300. return done;
  2301. }];
  2302. NSNumber *now = [NSNumber numberWithDouble:round([[NSDate date] timeIntervalSince1970] * 1000)];
  2303. NSNumber *timestamp = [snap priority];
  2304. XCTAssertTrue([[snap priority] isKindOfClass:[NSNumber class]], @"Should get back number");
  2305. XCTAssertTrue([now doubleValue] - [timestamp doubleValue] < 2000,
  2306. @"Number should be no more than 2 seconds ago");
  2307. }
  2308. - (void)testServerTimestampUpdateLocalEvents {
  2309. FIRDatabaseReference *node1 = [FTestHelpers getRandomNode];
  2310. __block FIRDataSnapshot *snap1 = nil;
  2311. [node1 observeEventType:FIRDataEventTypeValue
  2312. withBlock:^(FIRDataSnapshot *snapshot) {
  2313. snap1 = snapshot;
  2314. }];
  2315. __block FIRDataSnapshot *snap2 = nil;
  2316. [node1 observeEventType:FIRDataEventTypeValue
  2317. withBlock:^(FIRDataSnapshot *snapshot) {
  2318. snap2 = snapshot;
  2319. }];
  2320. [node1 runTransactionBlock:^FIRTransactionResult *(FIRMutableData *currentData) {
  2321. [currentData setValue:[FIRServerValue timestamp]];
  2322. return [FIRTransactionResult successWithValue:currentData];
  2323. }];
  2324. [self waitUntil:^BOOL {
  2325. return snap1 != nil && snap2 != nil && [snap1 value] != nil && [snap2 value] != nil;
  2326. }];
  2327. NSNumber *now = [NSNumber numberWithDouble:round([[NSDate date] timeIntervalSince1970] * 1000)];
  2328. NSNumber *timestamp1 = [snap1 value];
  2329. XCTAssertTrue([[snap1 value] isKindOfClass:[NSNumber class]], @"Should get back number");
  2330. XCTAssertTrue([now doubleValue] - [timestamp1 doubleValue] < 2000,
  2331. @"Number should be no more than 2 seconds ago");
  2332. NSNumber *timestamp2 = [snap2 value];
  2333. XCTAssertTrue([[snap2 value] isKindOfClass:[NSNumber class]], @"Should get back number");
  2334. XCTAssertTrue([now doubleValue] - [timestamp2 doubleValue] < 2000,
  2335. @"Number should be no more than 2 seconds ago");
  2336. }
  2337. - (void)testServerTimestampTransactionLocalEvents {
  2338. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  2339. __block FIRDataSnapshot *snap = nil;
  2340. [node observeEventType:FIRDataEventTypeValue
  2341. withBlock:^(FIRDataSnapshot *snapshot) {
  2342. snap = snapshot;
  2343. }];
  2344. [[node child:@"a/b/c"] setValue:@1];
  2345. [[node child:@"a"] updateChildValues:@{@"b" : @{@"c" : [FIRServerValue timestamp], @"d" : @1}}];
  2346. [self waitUntil:^BOOL {
  2347. return snap != nil && [[snap childSnapshotForPath:@"a/b/d"] value] != nil;
  2348. }];
  2349. NSNumber *now = [NSNumber numberWithDouble:round([[NSDate date] timeIntervalSince1970] * 1000)];
  2350. NSNumber *timestamp = [[snap childSnapshotForPath:@"a/b/c"] value];
  2351. XCTAssertTrue([[[snap childSnapshotForPath:@"a/b/c"] value] isKindOfClass:[NSNumber class]],
  2352. @"Should get back number");
  2353. XCTAssertTrue([now doubleValue] - [timestamp doubleValue] < 2000,
  2354. @"Number should be no more than 2 seconds ago");
  2355. }
  2356. - (void)testServerIncrementOverwritesExistingDataOnline {
  2357. [self checkServerIncrementOverwritesExistingDataWhileOnline:true];
  2358. }
  2359. - (void)testServerIncrementOverwritesExistingDataOffline {
  2360. [self checkServerIncrementOverwritesExistingDataWhileOnline:false];
  2361. }
  2362. - (void)checkServerIncrementOverwritesExistingDataWhileOnline:(BOOL)online {
  2363. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  2364. __block NSMutableArray *found = [NSMutableArray new];
  2365. NSMutableArray *expected = [NSMutableArray new];
  2366. [ref observeEventType:FIRDataEventTypeValue
  2367. withBlock:^(FIRDataSnapshot *snap) {
  2368. [found addObject:snap.value];
  2369. }];
  2370. // Going offline ensures that local events get queued up before server events
  2371. if (!online) {
  2372. [ref.repo interrupt];
  2373. }
  2374. // null + incr
  2375. [ref setValue:[FIRServerValue increment:@1]];
  2376. [expected addObject:@1];
  2377. // number + incr
  2378. [ref setValue:@5];
  2379. [ref setValue:[FIRServerValue increment:@1]];
  2380. [expected addObject:@5];
  2381. [expected addObject:@6];
  2382. // string + incr
  2383. [ref setValue:@"hello"];
  2384. [ref setValue:[FIRServerValue increment:@1]];
  2385. [expected addObject:@"hello"];
  2386. [expected addObject:@1];
  2387. // object + incr
  2388. [ref setValue:@{@"hello" : @"world"}];
  2389. [ref setValue:[FIRServerValue increment:@1]];
  2390. [expected addObject:@{@"hello" : @"world"}];
  2391. [expected addObject:@1];
  2392. [self waitUntil:^BOOL {
  2393. return found.count == expected.count;
  2394. }];
  2395. XCTAssertEqualObjects(expected, found);
  2396. if (!online) {
  2397. [ref.repo resume];
  2398. }
  2399. }
  2400. - (void)testServerIncrementPriorityOnline {
  2401. [self checkServerIncrementPriorityWhileOnline:true];
  2402. }
  2403. - (void)testServerIncrementPriorityOffline {
  2404. [self checkServerIncrementPriorityWhileOnline:false];
  2405. }
  2406. - (void)checkServerIncrementPriorityWhileOnline:(BOOL)online {
  2407. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  2408. if (!online) {
  2409. [ref.repo interrupt];
  2410. }
  2411. __block NSMutableArray *found = [NSMutableArray new];
  2412. NSMutableArray *expected = [NSMutableArray new];
  2413. [ref observeEventType:FIRDataEventTypeValue
  2414. withBlock:^(FIRDataSnapshot *snap) {
  2415. [found addObject:snap.priority];
  2416. }];
  2417. // Going offline ensures that local events get queued up before server events
  2418. // Also necessary because increment may not be live yet in the server.
  2419. if (!online) {
  2420. [ref.repo interrupt];
  2421. }
  2422. // null + incr
  2423. [ref setValue:@0 andPriority:[FIRServerValue increment:@1]];
  2424. [expected addObject:@1];
  2425. [ref setValue:@0 andPriority:[FIRServerValue increment:@1.5]];
  2426. [expected addObject:@2.5];
  2427. [self waitUntil:^BOOL {
  2428. return found.count == expected.count;
  2429. }];
  2430. XCTAssertEqualObjects(expected, found);
  2431. if (!online) {
  2432. [ref.repo resume];
  2433. }
  2434. }
  2435. - (void)testServerIncrementOverflowAndTypeCoercion {
  2436. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  2437. __block NSMutableArray *found = [NSMutableArray new];
  2438. __block NSMutableArray *foundTypes = [NSMutableArray new];
  2439. NSMutableArray *expected = [NSMutableArray new];
  2440. NSMutableArray *expectedTypes = [NSMutableArray new];
  2441. [ref observeEventType:FIRDataEventTypeValue
  2442. withBlock:^(FIRDataSnapshot *snap) {
  2443. [found addObject:snap.value];
  2444. [foundTypes addObject:@([(NSNumber *)snap.value objCType])];
  2445. }];
  2446. // Going offline ensures that local events get queued up before server events
  2447. // Also necessary because increment may not be live yet in the server.
  2448. [ref.repo interrupt];
  2449. // long + double = double
  2450. [ref setValue:@1];
  2451. [ref setValue:[FIRServerValue increment:@1.0]];
  2452. [expected addObject:@1];
  2453. [expected addObject:@2.0];
  2454. [expectedTypes addObject:@(@encode(int))];
  2455. [expectedTypes addObject:@(@encode(double))];
  2456. // double + long = double
  2457. [ref setValue:@1.5];
  2458. [ref setValue:[FIRServerValue increment:@1]];
  2459. [expected addObject:@1.5];
  2460. [expected addObject:@2.5];
  2461. [expectedTypes addObject:@(@encode(double))];
  2462. [expectedTypes addObject:@(@encode(double))];
  2463. // long overflow = double
  2464. [ref setValue:@(1)];
  2465. [ref setValue:[FIRServerValue increment:@(LONG_MAX)]];
  2466. [expected addObject:@(1)];
  2467. [expected addObject:@(LONG_MAX + 1.0)];
  2468. [expectedTypes addObject:@(@encode(int))];
  2469. [expectedTypes addObject:@(@encode(double))];
  2470. // unsigned long long overflow = double
  2471. [ref setValue:@1];
  2472. [ref setValue:[FIRServerValue increment:@((unsigned long long)ULLONG_MAX)]];
  2473. [expected addObject:@1];
  2474. [expected addObject:@((double)ULLONG_MAX + 1)];
  2475. [expectedTypes addObject:@(@encode(int))];
  2476. [expectedTypes addObject:@(@encode(double))];
  2477. // long underflow = double
  2478. [ref setValue:@(-1)];
  2479. [ref setValue:[FIRServerValue increment:@(LONG_MIN)]];
  2480. [expected addObject:@(-1)];
  2481. [expected addObject:@(LONG_MIN - 1.0)];
  2482. [expectedTypes addObject:@(@encode(int))];
  2483. [expectedTypes addObject:@(@encode(double))];
  2484. [self waitUntil:^BOOL {
  2485. return found.count == expected.count && foundTypes.count == expectedTypes.count;
  2486. }];
  2487. XCTAssertEqualObjects(expectedTypes, foundTypes);
  2488. XCTAssertEqualObjects(expected, found);
  2489. [ref.repo resume];
  2490. }
  2491. - (void)testUpdateAfterChildSet {
  2492. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  2493. __block BOOL done = NO;
  2494. __weak FIRDatabaseReference *weakRef = node;
  2495. [node setValue:@{@"a" : @"a"}
  2496. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  2497. [weakRef observeEventType:FIRDataEventTypeValue
  2498. withBlock:^(FIRDataSnapshot *snapshot) {
  2499. if (snapshot.childrenCount == 3 && [snapshot hasChild:@"a"] &&
  2500. [snapshot hasChild:@"b"] && [snapshot hasChild:@"c"]) {
  2501. done = YES;
  2502. }
  2503. }];
  2504. [[weakRef child:@"b"] setValue:@"b"];
  2505. [weakRef updateChildValues:@{@"c" : @"c"}];
  2506. }];
  2507. [self waitUntil:^BOOL {
  2508. return done;
  2509. }];
  2510. }
  2511. - (void)testDeltaSyncNoDataUpdatesAfterReconnect {
  2512. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  2513. FIRDatabaseConfig *cfg = [FTestHelpers configForName:@"test-config"];
  2514. FIRDatabaseReference *ref2 = [[[FIRDatabaseReference alloc] initWithConfig:cfg] child:ref.key];
  2515. __block id data = @{@"a" : @1, @"b" : @2, @"c" : @{@".priority" : @3, @".value" : @3}, @"d" : @4};
  2516. [self waitForCompletionOf:ref setValue:data];
  2517. __block BOOL gotData = NO;
  2518. [ref2 observeEventType:FIRDataEventTypeValue
  2519. withBlock:^(FIRDataSnapshot *snapshot) {
  2520. XCTAssertFalse(gotData, @"event triggered twice.");
  2521. gotData = YES;
  2522. XCTAssertEqualObjects(snapshot.valueInExportFormat, data, @"Got wrong data.");
  2523. }];
  2524. [self waitUntil:^BOOL {
  2525. return gotData;
  2526. }];
  2527. __block BOOL done = NO;
  2528. XCTAssertEqual(ref2.repo.dataUpdateCount, 1L, @"Should have gotten one update.");
  2529. // Bounce connection
  2530. [FRepoManager interrupt:cfg];
  2531. [FRepoManager resume:cfg];
  2532. [[[ref2 root] child:@".info/connected"]
  2533. observeEventType:FIRDataEventTypeValue
  2534. withBlock:^(FIRDataSnapshot *snapshot) {
  2535. if ([snapshot.value boolValue]) {
  2536. // We're connected. Do one more round-trip to make sure all state restoration is
  2537. // done
  2538. [[[ref2 root] child:@"foobar/empty/blah"]
  2539. setValue:nil
  2540. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  2541. XCTAssertEqual(ref2.repo.dataUpdateCount, 1L,
  2542. @"Should have gotten one update.");
  2543. done = YES;
  2544. }];
  2545. }
  2546. }];
  2547. [self waitUntil:^BOOL {
  2548. return done;
  2549. }];
  2550. // cleanup
  2551. [FRepoManager interrupt:cfg];
  2552. [FRepoManager disposeRepos:cfg];
  2553. }
  2554. - (void)testServerTimestampEventualConsistencyBetweenLocalAndRemote {
  2555. FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
  2556. FIRDatabaseReference *writer = refs.one;
  2557. FIRDatabaseReference *reader = refs.two;
  2558. __block FIRDataSnapshot *writerSnap = nil;
  2559. __block FIRDataSnapshot *readerSnap = nil;
  2560. [reader observeEventType:FIRDataEventTypeValue
  2561. withBlock:^(FIRDataSnapshot *snapshot) {
  2562. readerSnap = snapshot;
  2563. }];
  2564. [writer observeEventType:FIRDataEventTypeValue
  2565. withBlock:^(FIRDataSnapshot *snapshot) {
  2566. writerSnap = snapshot;
  2567. }];
  2568. [writer setValue:[FIRServerValue timestamp] andPriority:[FIRServerValue timestamp]];
  2569. [self waitUntil:^BOOL {
  2570. if (readerSnap && writerSnap && [[readerSnap value] isKindOfClass:[NSNumber class]] &&
  2571. [[writerSnap value] isKindOfClass:[NSNumber class]]) {
  2572. if ([[readerSnap value] doubleValue] == [[writerSnap value] doubleValue]) {
  2573. return YES;
  2574. }
  2575. }
  2576. return NO;
  2577. }];
  2578. }
  2579. // Listens at a location and then creates a bunch of children, waiting for them all to complete.
  2580. - (void)testChildAddedPerf1 {
  2581. if (!runPerfTests) return;
  2582. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  2583. [ref observeEventType:FIRDataEventTypeChildAdded
  2584. withBlock:^(FIRDataSnapshot *snapshot){
  2585. }];
  2586. NSDate *start = [NSDate date];
  2587. int COUNT = 1000;
  2588. __block BOOL done = NO;
  2589. __block NSDate *finished = nil;
  2590. for (int i = 0; i < COUNT; i++) {
  2591. [[ref childByAutoId] setValue:@"01234567890123456789012345678901234567890123456789"
  2592. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  2593. if (i == (COUNT - 1)) {
  2594. finished = [NSDate date];
  2595. done = YES;
  2596. }
  2597. }];
  2598. }
  2599. [self
  2600. waitUntil:^BOOL {
  2601. return done;
  2602. }
  2603. timeout:300];
  2604. NSTimeInterval elapsed = [finished timeIntervalSinceDate:start];
  2605. NSLog(@"Elapsed: %f", elapsed);
  2606. }
  2607. // Listens at a location, then adds a bunch of grandchildren under a single child.
  2608. - (void)testDeepChildAddedPerf1 {
  2609. if (!runPerfTests) return;
  2610. FIRDatabaseReference *ref = [FTestHelpers getRandomNode], *childRef = [ref child:@"child"];
  2611. [ref observeEventType:FIRDataEventTypeChildAdded
  2612. withBlock:^(FIRDataSnapshot *snapshot){
  2613. }];
  2614. NSDate *start = [NSDate date];
  2615. int COUNT = 1000;
  2616. __block BOOL done = NO;
  2617. __block NSDate *finished = nil;
  2618. for (int i = 0; i < COUNT; i++) {
  2619. [[childRef childByAutoId] setValue:@"01234567890123456789012345678901234567890123456789"
  2620. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  2621. if (i == (COUNT - 1)) {
  2622. finished = [NSDate date];
  2623. done = YES;
  2624. }
  2625. }];
  2626. }
  2627. [self
  2628. waitUntil:^BOOL {
  2629. return done;
  2630. }
  2631. timeout:300];
  2632. NSTimeInterval elapsed = [finished timeIntervalSinceDate:start];
  2633. NSLog(@"Elapsed: %f", elapsed);
  2634. }
  2635. // Listens at a location, then adds a bunch of grandchildren under a single child, but does it with
  2636. // merges. NOTE[2015-07-14]: This test is still pretty slow, because [FWriteTree removeWriteId] ends
  2637. // up rebuilding the tree after every ack.
  2638. - (void)testDeepChildAddedPerfViaMerge1 {
  2639. if (!runPerfTests) return;
  2640. FIRDatabaseReference *ref = [FTestHelpers getRandomNode], *childRef = [ref child:@"child"];
  2641. [ref observeEventType:FIRDataEventTypeChildAdded
  2642. withBlock:^(FIRDataSnapshot *snapshot){
  2643. }];
  2644. NSDate *start = [NSDate date];
  2645. int COUNT = 250;
  2646. __block BOOL done = NO;
  2647. __block NSDate *finished = nil;
  2648. for (int i = 0; i < COUNT; i++) {
  2649. NSString *childName = [childRef childByAutoId].key;
  2650. [childRef updateChildValues:@{childName : @"01234567890123456789012345678901234567890123456789"}
  2651. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  2652. if (i == (COUNT - 1)) {
  2653. finished = [NSDate date];
  2654. done = YES;
  2655. }
  2656. }];
  2657. }
  2658. [self
  2659. waitUntil:^BOOL {
  2660. return done;
  2661. }
  2662. timeout:300];
  2663. NSTimeInterval elapsed = [finished timeIntervalSinceDate:start];
  2664. NSLog(@"Elapsed: %f", elapsed);
  2665. }
  2666. @end