FData.m 98 KB

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