FData.m 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687
  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 observeEventType: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. }
  537. - (void) testHasChildrenWorksCorrectly {
  538. FIRDatabaseReference * node = [FTestHelpers getRandomNode];
  539. [node setValue:@{@"one" : @42, @"two": @{@"a": @5}, @"three": @{@"a": @5, @"b": @6}}];
  540. __block BOOL removedTwo = NO;
  541. __block BOOL done = NO;
  542. [node observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  543. if (!removedTwo) {
  544. XCTAssertFalse([[snapshot childSnapshotForPath:@"one"] hasChildren], @"nope");
  545. XCTAssertTrue([[snapshot childSnapshotForPath:@"two"] hasChildren], @"nope");
  546. XCTAssertTrue([[snapshot childSnapshotForPath:@"three"] hasChildren], @"nope");
  547. XCTAssertFalse([[snapshot childSnapshotForPath:@"four"] hasChildren], @"nope");
  548. removedTwo = YES;
  549. [[node child:@"two"] removeValue];
  550. }
  551. else {
  552. XCTAssertFalse([[snapshot childSnapshotForPath:@"two"] hasChildren], @"Second time around");
  553. done = YES;
  554. }
  555. }];
  556. [self waitUntil:^BOOL{
  557. return done;
  558. }];
  559. }
  560. - (void) testNumChildrenWorksCorrectly {
  561. FIRDatabaseReference * node = [FTestHelpers getRandomNode];
  562. [node setValue:@{@"one" : @42, @"two": @{@"a": @5}, @"three": @{@"a": @5, @"b": @6}}];
  563. __block BOOL removedTwo = NO;
  564. __block BOOL done = NO;
  565. [node observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  566. if (!removedTwo) {
  567. XCTAssertTrue([snapshot childrenCount] == 3, @"Total children");
  568. XCTAssertTrue([[snapshot childSnapshotForPath:@"one"] childrenCount] == 0, @"Two's children");
  569. XCTAssertTrue([[snapshot childSnapshotForPath:@"two"] childrenCount] == 1, @"Two's children");
  570. XCTAssertTrue([[snapshot childSnapshotForPath:@"three"] childrenCount] == 2, @"Two's children");
  571. XCTAssertTrue([[snapshot childSnapshotForPath:@"four"] childrenCount] == 0, @"Two's children");
  572. removedTwo = YES;
  573. [[node child:@"two"] removeValue];
  574. }
  575. else {
  576. XCTAssertTrue([snapshot childrenCount] == 2, @"Total children");
  577. XCTAssertTrue([[snapshot childSnapshotForPath:@"two"] childrenCount] == 0, @"Two's children");
  578. done = YES;
  579. }
  580. }];
  581. [self waitUntil:^BOOL{
  582. return done;
  583. }];
  584. }
  585. - (void) testSettingANodeWithChildrenToAPrimitiveAndBack {
  586. // Can't tolerate stale data; so disable persistence.
  587. FTupleFirebase* tuple = [FTestHelpers getRandomNodePairWithoutPersistence];
  588. FIRDatabaseReference * writeNode = tuple.one;
  589. FIRDatabaseReference * readNode = tuple.two;
  590. __block BOOL done = NO;
  591. NSDictionary* compound = @{@"a": @5, @"b": @6};
  592. NSNumber* number = @76;
  593. [writeNode setValue:compound];
  594. [self snapWaiter:writeNode withBlock:^(FIRDataSnapshot *snapshot) {
  595. XCTAssertTrue([snapshot hasChildren], @"Has children");
  596. XCTAssertEqualObjects(@5, [[snapshot childSnapshotForPath:@"a"] value], @"First child");
  597. XCTAssertEqualObjects(@6, [[snapshot childSnapshotForPath:@"b"] value], @"First child");
  598. done = YES;
  599. }];
  600. [self waitUntil:^BOOL{
  601. return done;
  602. }];
  603. done = NO;
  604. [self snapWaiter:readNode withBlock:^(FIRDataSnapshot *snapshot) {
  605. XCTAssertTrue([snapshot hasChildren], @"has children");
  606. XCTAssertEqualObjects(@5, [[snapshot childSnapshotForPath:@"a"] value], @"First child");
  607. XCTAssertEqualObjects(@6, [[snapshot childSnapshotForPath:@"b"] value], @"First child");
  608. done = YES;
  609. }];
  610. [self waitUntil:^BOOL{
  611. return done;
  612. }];
  613. done = NO;
  614. [writeNode setValue:number withCompletionBlock:^(NSError* err, FIRDatabaseReference * ref) {
  615. done = YES;
  616. }];
  617. [self waitUntil:^BOOL{
  618. return done;
  619. }];
  620. done = NO;
  621. [self snapWaiter:readNode withBlock:^(FIRDataSnapshot *snapshot) {
  622. XCTAssertFalse([snapshot hasChildren], @"No more children");
  623. XCTAssertEqualObjects(number, [snapshot value], @"Proper non compound value");
  624. done = YES;
  625. }];
  626. [self waitUntil:^BOOL{
  627. return done;
  628. }];
  629. done = NO;
  630. [writeNode setValue:compound withCompletionBlock:^(NSError* err, FIRDatabaseReference * ref) {
  631. done = YES;
  632. }];
  633. [self waitUntil:^BOOL{
  634. return done;
  635. }];
  636. done = NO;
  637. [self snapWaiter:readNode withBlock:^(FIRDataSnapshot *snapshot) {
  638. XCTAssertTrue([snapshot hasChildren], @"Has children");
  639. XCTAssertEqualObjects(@5, [[snapshot childSnapshotForPath:@"a"] value], @"First child");
  640. XCTAssertEqualObjects(@6, [[snapshot childSnapshotForPath:@"b"] value], @"First child");
  641. done = YES;
  642. }];
  643. [self waitUntil:^BOOL{
  644. return done;
  645. }];
  646. XCTAssertTrue(done, @"Properly finished");
  647. }
  648. - (void) testWriteLeafRemoveLeafAddChildToRemovedNode {
  649. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  650. FIRDatabaseReference * writer = refs.one;
  651. FIRDatabaseReference * reader = refs.two;
  652. __block BOOL ready = NO;
  653. [writer setValue:@5];
  654. [writer removeValue];
  655. [[writer child:@"abc"] setValue:@5 withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  656. ready = YES;
  657. }];
  658. [self waitUntil:^BOOL{
  659. return ready;
  660. }];
  661. __block NSDictionary* readVal = nil;
  662. [reader observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  663. readVal = [snapshot value];
  664. }];
  665. [self waitUntil:^BOOL{
  666. return readVal != nil;
  667. }];
  668. NSNumber* five = [readVal objectForKey:@"abc"];
  669. XCTAssertTrue([five isEqualToNumber:@5], @"Should get 5");
  670. }
  671. - (void) testListenForValueAndThenWriteOnANodeWithExistingData {
  672. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  673. FIRDatabaseReference * writer = refs.one;
  674. FIRDatabaseReference * reader = refs.two;
  675. [self waitForCompletionOf:writer setValue:@{@"a": @5, @"b": @2}];
  676. __block int calls = 0;
  677. [reader observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  678. calls++;
  679. if (calls == 1) {
  680. NSDictionary *val = [snapshot value];
  681. NSDictionary *expected = @{@"a" : @10, @"b" : @2};
  682. XCTAssertTrue([val isEqualToDictionary:expected], @"Got the correct value");
  683. } else {
  684. XCTFail(@"Should only be called once");
  685. }
  686. }];
  687. [[reader child:@"a"] setValue:@10];
  688. [self waitUntil:^BOOL{
  689. return calls == 1;
  690. }];
  691. }
  692. - (void) testSetPriorityOnNonexistentNodeFails {
  693. FIRDatabaseReference * ref = [FTestHelpers getRandomNode];
  694. __block BOOL ready = NO;
  695. [ref setPriority:@5 withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  696. XCTAssertTrue(error != nil, @"This should not succeed");
  697. ready = YES;
  698. }];
  699. [self waitUntil:^BOOL{
  700. return ready;
  701. }];
  702. }
  703. - (void) testSetPriorityOnExistentNodeSucceeds {
  704. FIRDatabaseReference * ref = [FTestHelpers getRandomNode];
  705. __block BOOL ready = NO;
  706. [ref setValue:@"hello!"];
  707. [ref setPriority:@5 withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  708. XCTAssertTrue(error == nil, @"This should succeed");
  709. ready = YES;
  710. }];
  711. [self waitUntil:^BOOL{
  712. return ready;
  713. }];
  714. }
  715. - (void) testSetWithPrioritySetsValueAndPriority {
  716. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  717. FIRDatabaseReference * writer = refs.one;
  718. FIRDatabaseReference * reader = refs.two;
  719. [self waitForCompletionOf:writer setValue:@"hello" andPriority:@5];
  720. __block FIRDataSnapshot * writeSnap = nil;
  721. __block FIRDataSnapshot * readSnap = nil;
  722. [writer observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  723. writeSnap = snapshot;
  724. }];
  725. [reader observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  726. readSnap = snapshot;
  727. }];
  728. [self waitUntil:^BOOL{
  729. return readSnap != nil && writeSnap != nil;
  730. }];
  731. XCTAssertTrue([@"hello" isEqualToString:[readSnap value]], @"Got the value on the reader");
  732. XCTAssertTrue([@"hello" isEqualToString:[writeSnap value]], @"Got the value on the writer");
  733. XCTAssertTrue([@5 isEqualToNumber:[readSnap priority]], @"Got the priority on the reader");
  734. XCTAssertTrue([@5 isEqualToNumber:[writeSnap priority]], @"Got the priority on the writer");
  735. }
  736. - (void) testEffectsOfSetPriorityIsImmediatelyEvident {
  737. FIRDatabaseReference * ref = [FTestHelpers getRandomNode];
  738. NSMutableArray* values = [[NSMutableArray alloc] init];
  739. NSMutableArray* priorities = [[NSMutableArray alloc] init];
  740. [ref observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  741. [values addObject:[snapshot value]];
  742. [priorities addObject:[snapshot priority]];
  743. }];
  744. [ref setValue:@5];
  745. [ref setPriority:@10];
  746. __block BOOL ready = NO;
  747. [ref observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  748. [values addObject:[snapshot value]];
  749. [priorities addObject:[snapshot priority]];
  750. ready = YES;
  751. }];
  752. [self waitUntil:^BOOL{
  753. return ready;
  754. }];
  755. NSArray* expectedValues = @[@5, @5];
  756. NSArray* expectedPriorites = @[[NSNull null], @10];
  757. XCTAssertTrue([values isEqualToArray:expectedValues], @"Expected both listeners to get 5, got %@ instead", values);
  758. XCTAssertTrue([priorities isEqualToArray:expectedPriorites], @"The first listener should have missed the priority, got %@ instead", priorities);
  759. }
  760. - (void) testSetOverwritesPriorityOfTopLevelNodeAndSubnodes {
  761. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  762. FIRDatabaseReference * writer = refs.one;
  763. FIRDatabaseReference * reader = refs.two;
  764. __block BOOL ready = NO;
  765. [writer setValue:@{@"a": @5}];
  766. [writer setPriority:@10];
  767. [[writer child:@"a"] setPriority:@18];
  768. [writer setValue:@{@"a": @7} withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  769. ready = YES;
  770. }];
  771. [self waitUntil:^BOOL{
  772. return ready;
  773. }];
  774. ready = NO;
  775. [reader observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  776. id pri = [snapshot priority];
  777. XCTAssertTrue([NSNull null] == pri, @"Expected null priority");
  778. FIRDataSnapshot *child = [snapshot childSnapshotForPath:@"a"];
  779. XCTAssertTrue([NSNull null] == [child priority], @"Child priority should be null too");
  780. ready = YES;
  781. }];
  782. [self waitUntil:^BOOL{
  783. return ready;
  784. }];
  785. }
  786. - (void) testSetPriorityOfLeafSavesCorrectly {
  787. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  788. FIRDatabaseReference * writer = refs.one;
  789. FIRDatabaseReference * reader = refs.two;
  790. __block BOOL ready = NO;
  791. [writer setValue:@"testleaf" andPriority:@992 withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  792. ready = YES;
  793. }];
  794. [self waitUntil:^BOOL{
  795. return ready;
  796. }];
  797. ready = NO;
  798. [reader observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  799. id pri = [snapshot priority];
  800. XCTAssertTrue([@992 isEqualToNumber:pri], @"Expected non-null priority");
  801. ready = YES;
  802. }];
  803. [self waitUntil:^BOOL{
  804. return ready;
  805. }];
  806. }
  807. - (void) testSetPriorityOfObjectSavesCorrectly {
  808. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  809. FIRDatabaseReference * writer = refs.one;
  810. FIRDatabaseReference * reader = refs.two;
  811. __block BOOL ready = NO;
  812. [writer setValue:@{@"a": @5} andPriority:@991 withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  813. ready = YES;
  814. }];
  815. [self waitUntil:^BOOL{
  816. return ready;
  817. }];
  818. ready = NO;
  819. [reader observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  820. id pri = [snapshot priority];
  821. XCTAssertTrue([@991 isEqualToNumber:pri], @"Expected non-null priority");
  822. ready = YES;
  823. }];
  824. [self waitUntil:^BOOL{
  825. return ready;
  826. }];
  827. }
  828. - (void) testSetWithPriorityFollowedBySetClearsPriority {
  829. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  830. FIRDatabaseReference * writer = refs.one;
  831. FIRDatabaseReference * reader = refs.two;
  832. __block BOOL ready = NO;
  833. [writer setValue:@{@"a": @5} andPriority:@991 withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  834. ready = YES;
  835. }];
  836. [self waitUntil:^BOOL{
  837. return ready;
  838. }];
  839. ready = NO;
  840. [reader setValue:@{@"a": @19} withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  841. ready = YES;
  842. }];
  843. [self waitUntil:^BOOL{
  844. return ready;
  845. }];
  846. ready = NO;
  847. [reader observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  848. id pri = [snapshot priority];
  849. XCTAssertTrue([NSNull null] == pri, @"Expected null priority");
  850. ready = YES;
  851. }];
  852. [self waitUntil:^BOOL{
  853. return ready;
  854. }];
  855. }
  856. - (void) testGetPriorityReturnsCorrectType {
  857. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  858. __block FIRDataSnapshot * snap = nil;
  859. [ref observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  860. snap = snapshot;
  861. }];
  862. [ref setValue:@"a"];
  863. [self waitUntil:^BOOL{
  864. return snap != nil;
  865. }];
  866. XCTAssertTrue([snap priority] == [NSNull null], @"Expect null priority");
  867. snap = nil;
  868. [ref setValue:@"b" andPriority:@5];
  869. [self waitUntil:^BOOL{
  870. return snap != nil;
  871. }];
  872. XCTAssertTrue([[snap priority] isEqualToNumber:@5], @"Expect priority");
  873. snap = nil;
  874. [ref setValue:@"c" andPriority:@"6"];
  875. [self waitUntil:^BOOL{
  876. return snap != nil;
  877. }];
  878. XCTAssertTrue([[snap priority] isEqualToString:@"6"], @"Expect priority");
  879. snap = nil;
  880. [ref setValue:@"d" andPriority:@7];
  881. [self waitUntil:^BOOL{
  882. return snap != nil;
  883. }];
  884. XCTAssertTrue([[snap priority] isEqualToNumber:@7], @"Expect priority");
  885. snap = nil;
  886. [ref setValue:@{@".value": @"e", @".priority": @8}];
  887. [self waitUntil:^BOOL{
  888. return snap != nil;
  889. }];
  890. XCTAssertTrue([[snap priority] isEqualToNumber:@8], @"Expect priority");
  891. snap = nil;
  892. [ref setValue:@{@".value": @"f", @".priority": @"8"}];
  893. [self waitUntil:^BOOL{
  894. return snap != nil;
  895. }];
  896. XCTAssertTrue([[snap priority] isEqualToString:@"8"], @"Expect priority");
  897. snap = nil;
  898. [ref setValue:@{@".value": @"e", @".priority": [NSNull null]}];
  899. [self waitUntil:^BOOL{
  900. return snap != nil;
  901. }];
  902. XCTAssertTrue([snap priority] == [NSNull null], @"Expect priority");
  903. snap = nil;
  904. }
  905. - (void) testExportValIncludesPriorities {
  906. FIRDatabaseReference * ref = [FTestHelpers getRandomNode];
  907. NSDictionary* contents = @{@"foo": @{@"bar": @{@".value": @5, @".priority": @7}, @".priority": @"hi"}};
  908. __block FIRDataSnapshot * snap = nil;
  909. [ref observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  910. snap = snapshot;
  911. }];
  912. [ref setValue:contents];
  913. [self waitUntil:^BOOL{
  914. return snap != nil;
  915. }];
  916. XCTAssertTrue([contents isEqualToDictionary:[snap valueInExportFormat]], @"Expected priorities in snapshot");
  917. }
  918. - (void) testPriorityIsOverwrittenByServer {
  919. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  920. FIRDatabaseReference * reader = refs.one;
  921. FIRDatabaseReference * writer = refs.two;
  922. __block int event = 0;
  923. __block BOOL done = NO;
  924. [reader observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  925. NSLog(@"%@ Snapshot", snapshot);
  926. id pri = [snapshot priority];
  927. if (event == 0) {
  928. XCTAssertTrue([@100 isEqualToNumber:pri], @"Expect local priority. Got %@ instead.", pri);
  929. } else if (event == 1) {
  930. XCTAssertTrue(pri == [NSNull null], @"Expect remote priority. Got %@ instead.", pri);
  931. } else {
  932. XCTFail(@"Extra event");
  933. }
  934. event++;
  935. if (event == 2) {
  936. done = YES;
  937. }
  938. }];
  939. [writer observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  940. id pri = [snapshot priority];
  941. if ([[pri class] isSubclassOfClass:[NSNumber class]] && [@100 isEqualToNumber:pri]) {
  942. [writer setValue:@"whatever"];
  943. }
  944. }];
  945. [reader setValue:@"hi" andPriority:@100];
  946. [self waitUntil:^BOOL{
  947. return done;
  948. }];
  949. }
  950. - (void) testLargeNumericPrioritiesWork {
  951. NSNumber* bigPriority = @1356721306842;
  952. __block BOOL ready = NO;
  953. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  954. FIRDatabaseReference * reader = refs.one;
  955. FIRDatabaseReference * writer = refs.two;
  956. [self waitForCompletionOf:writer setValue:@5 andPriority:bigPriority];
  957. __block NSNumber* serverPriority = @0;
  958. [reader observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  959. serverPriority = [snapshot priority];
  960. ready = YES;
  961. }];
  962. [self waitUntil:^BOOL{
  963. return ready;
  964. }];
  965. XCTAssertTrue([bigPriority isEqualToNumber:serverPriority], @"Expect big priority back");
  966. }
  967. - (void) testToString {
  968. FIRDatabaseReference * ref = [FTestHelpers getRandomNode];
  969. FIRDatabaseReference * parent = [ref parent];
  970. XCTAssertTrue([[parent description] isEqualToString:self.databaseURL], @"Expect domain");
  971. FIRDatabaseReference * child = [parent child:@"a/b/c"];
  972. NSString* expected = [NSString stringWithFormat:@"%@/a/b/c", self.databaseURL];
  973. XCTAssertTrue([[child description] isEqualToString:expected], @"Expected path");
  974. }
  975. - (void) testURLEncodingOfDescriptionAndURLDecodingOfNewFirebase {
  976. __block BOOL ready = NO;
  977. NSString* test1 = [NSString stringWithFormat:@"%@/a%%b&c@d/space: /non-ascii_character:ø", self.databaseURL];
  978. NSString* expected1 = [NSString stringWithFormat:@"%@/a%%25b%%26c%%40d/space%%3A%%20/non-ascii_character%%3A%%C3%%B8", self.databaseURL];
  979. FIRDatabaseReference * ref = [[FIRDatabase database] referenceFromURL:test1];
  980. NSString* result = [ref description];
  981. XCTAssertTrue([result isEqualToString:expected1], @"Encodes properly");
  982. int rnd = arc4random_uniform(100000000);
  983. NSString* path = [NSString stringWithFormat:@"%i", rnd];
  984. [[ref child:path] setValue:@"testdata" withCompletionBlock:^(NSError* error, FIRDatabaseReference * childRef) {
  985. FIRDatabaseReference * other = [[FIRDatabase database] referenceFromURL:[ref description]];
  986. [[other child:path] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  987. NSString *val = snapshot.value;
  988. XCTAssertTrue([val isEqualToString:@"testdata"], @"Expected to get testdata back");
  989. ready = YES;
  990. }];
  991. }];
  992. [self waitUntil:^BOOL{
  993. return ready;
  994. }];
  995. }
  996. - (void) testNameAtRootAndNonRootLocations {
  997. FIRDatabaseReference * ref = [[FIRDatabase database] referenceFromURL:self.databaseURL];
  998. XCTAssertTrue(ref.key == nil, @"Root key should be nil");
  999. FIRDatabaseReference * child = [ref child:@"a"];
  1000. XCTAssertTrue([child.key isEqualToString:@"a"], @"Should be 'a'");
  1001. FIRDatabaseReference * deeperChild = [child child:@"b/c"];
  1002. XCTAssertTrue([deeperChild.key isEqualToString:@"c"], @"Should be 'c'");
  1003. }
  1004. - (void) testNameAndRefOnSnapshotsForRootAndNonRootLocations {
  1005. FIRDatabaseReference * ref = [[FIRDatabase database] reference];
  1006. __block BOOL ready = NO;
  1007. [ref removeValueWithCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  1008. ready = YES;
  1009. }];
  1010. [self waitUntil:^BOOL{
  1011. return ready;
  1012. }];
  1013. ready = NO;
  1014. [ref observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1015. XCTAssertTrue(snapshot.key == nil, @"Root snap should not have a key");
  1016. NSString *snapString = [snapshot.ref description];
  1017. XCTAssertTrue([snapString isEqualToString:snapString], @"Refs should be equivalent");
  1018. FIRDataSnapshot *childSnap = [snapshot childSnapshotForPath:@"a"];
  1019. XCTAssertTrue([childSnap.key isEqualToString:@"a"], @"Properly keys children");
  1020. FIRDatabaseReference *childRef = [ref child:@"a"];
  1021. NSString *refString = [childRef description];
  1022. snapString = [childSnap.ref description];
  1023. XCTAssertTrue([refString isEqualToString:snapString], @"Refs should be equivalent");
  1024. childSnap = [childSnap childSnapshotForPath:@"b/c"];
  1025. childRef = [childRef child:@"b/c"];
  1026. XCTAssertTrue([childSnap.key isEqualToString:@"c"], @"properly keys children");
  1027. refString = [childRef description];
  1028. snapString = [childSnap.ref description];
  1029. XCTAssertTrue([refString isEqualToString:snapString], @"Refs should be equivalent");
  1030. ready = YES;
  1031. }];
  1032. [self waitUntil:^BOOL{
  1033. return ready;
  1034. }];
  1035. ready = NO;
  1036. // generate value event at root
  1037. [ref setValue:@"foo"];
  1038. [self waitUntil:^BOOL{
  1039. return ready;
  1040. }];
  1041. }
  1042. - (void) testParentForRootAndNonRootLocations {
  1043. FIRDatabaseReference * ref = [[FIRDatabase database] reference];
  1044. XCTAssertTrue(ref.parent == nil, @"Parent of root should be nil");
  1045. FIRDatabaseReference * child = [ref child:@"a"];
  1046. XCTAssertTrue([[child.parent description] isEqualToString:[ref description]], @"Should be equivalent locations");
  1047. child = [ref child:@"a/b/c"];
  1048. XCTAssertTrue([[child.parent.parent.parent description] isEqualToString:[ref description]], @"Should be equivalent locations");
  1049. }
  1050. - (void) testSettingNumericKeysConvertsToStrings {
  1051. FIRDatabaseReference * ref = [FTestHelpers getRandomNode];
  1052. NSDictionary* toSet = @{@4: @"hi", @5: @"test"};
  1053. XCTAssertThrows([ref setValue:toSet], @"Keys must be strings");
  1054. }
  1055. - (void) testSetChildAndListenAtRootRegressionTest {
  1056. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  1057. FIRDatabaseReference * writer = refs.one;
  1058. FIRDatabaseReference * reader = refs.two;
  1059. __block BOOL ready = NO;
  1060. [writer removeValue];
  1061. [[writer child:@"foo"] setValue:@"hi" withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  1062. [reader observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1063. NSDictionary *val = [snapshot value];
  1064. NSDictionary *expected = @{@"foo" : @"hi"};
  1065. XCTAssertTrue([val isEqualToDictionary:expected], @"Got child");
  1066. ready = YES;
  1067. }];
  1068. }];
  1069. [self waitUntil:^BOOL{
  1070. return ready;
  1071. }];
  1072. }
  1073. - (void) testAccessingInvalidPathsThrows {
  1074. NSArray* badPaths = @[
  1075. @".test",
  1076. @"test.",
  1077. @"fo$o",
  1078. @"[what",
  1079. @"ever]",
  1080. @"ha#sh"
  1081. ];
  1082. for (NSString* key in badPaths) {
  1083. NSString* url = [NSString stringWithFormat:@"%@/%@", self.databaseURL, key];
  1084. XCTAssertThrows(^{
  1085. FIRDatabaseReference * ref = [[FIRDatabase database] referenceFromURL:url];
  1086. XCTFail(@"Should not get here with ref: %@", ref);
  1087. }(), @"should throw");
  1088. url = [NSString stringWithFormat:@"%@/TESTS/%@", self.databaseURL, key];
  1089. XCTAssertThrows(^{
  1090. FIRDatabaseReference * ref = [[FIRDatabase database] referenceFromURL:url];
  1091. XCTFail(@"Should not get here with ref: %@", ref);
  1092. }(), @"should throw");
  1093. }
  1094. __block BOOL ready = NO;
  1095. FIRDatabaseReference * ref = [FTestHelpers getRandomNode];
  1096. [ref observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1097. for (NSString *key in badPaths) {
  1098. XCTAssertThrows([snapshot childSnapshotForPath:key], @"should throw");
  1099. XCTAssertThrows([snapshot hasChild:key], @"should throw");
  1100. }
  1101. ready = YES;
  1102. }];
  1103. [ref setValue:nil];
  1104. [self waitUntil:^BOOL{
  1105. return ready;
  1106. }];
  1107. }
  1108. - (void) testSettingObjectsAtInvalidKeysThrow {
  1109. FIRDatabaseReference * ref = [FTestHelpers getRandomNode];
  1110. NSArray* badPaths = @[
  1111. @".test",
  1112. @"test.",
  1113. @"fo$o",
  1114. @"[what",
  1115. @"ever]",
  1116. @"ha#sh",
  1117. @"/thing",
  1118. @"th/ing",
  1119. @"thing/"
  1120. ];
  1121. NSMutableArray* badObjs = [[NSMutableArray alloc] init];
  1122. for (NSString* key in badPaths) {
  1123. [badObjs addObject:@{key: @"test"}];
  1124. [badObjs addObject:@{@"deeper": @{key: @"test"}}];
  1125. }
  1126. for (NSDictionary* badObj in badObjs) {
  1127. XCTAssertThrows([ref setValue:badObj], @"Should throw");
  1128. XCTAssertThrows([ref setValue:badObj andPriority:@5], @"Should throw");
  1129. XCTAssertThrows([ref onDisconnectSetValue:badObj], @"Should throw");
  1130. XCTAssertThrows([ref onDisconnectSetValue:badObj andPriority:@5], @"Should throw");
  1131. // XXX transaction
  1132. }
  1133. }
  1134. - (void) testSettingInvalidObjectsThrow {
  1135. FIRDatabaseReference * ref = [FTestHelpers getRandomNode];
  1136. XCTAssertThrows([ref setValue:[NSDate date]], @"Should throw");
  1137. NSDictionary *data = @{@"invalid":@"data", @".sv":@"timestamp"};
  1138. XCTAssertThrows([ref setValue:data], @"Should throw");
  1139. data = @{@".value": @{}};
  1140. XCTAssertThrows([ref setValue:data], @"Should throw");
  1141. }
  1142. - (void) testInvalidUpdateThrow {
  1143. FIRDatabaseReference * ref = [FTestHelpers getRandomNode];
  1144. NSArray *badUpdates = @[
  1145. @{@"/":@"t", @"a":@"t"},
  1146. @{@"a":@"t", @"a/b":@"t"},
  1147. @{@"/a":@"t", @"a/b":@"t"},
  1148. @{@"/a/b":@"t", @"a":@"t"},
  1149. @{@"/a/b/.priority":@"t", @"/a/b":@"t"},
  1150. @{@"/a/b/.sv":@"timestamp"},
  1151. @{@"/a/b/.value":@"t"},
  1152. @{@"/a/b/.priority":@{@"x": @"y"}}];
  1153. for (NSDictionary* update in badUpdates) {
  1154. XCTAssertThrows([ref updateChildValues:update], @"Should throw");
  1155. XCTAssertThrows([ref onDisconnectUpdateChildValues:update], @"Should throw");
  1156. }
  1157. }
  1158. - (void) testSettingNull {
  1159. FIRDatabaseReference * ref = [FTestHelpers getRandomNode];
  1160. XCTAssertNoThrow([ref setValue:nil], @"Should not throw");
  1161. XCTAssertNoThrow([ref setValue:[NSNull null]], @"Should not throw");
  1162. }
  1163. - (void) testSettingNaN {
  1164. FIRDatabaseReference * ref = [FTestHelpers getRandomNode];
  1165. XCTAssertThrows([ref setValue:[NSDecimalNumber notANumber]], @"Should throw");
  1166. }
  1167. - (void) testSettingInvalidPriority {
  1168. FIRDatabaseReference * ref = [FTestHelpers getRandomNode];
  1169. XCTAssertThrows([ref setValue:@"3" andPriority:[NSDecimalNumber notANumber]], @"Should throw");
  1170. XCTAssertThrows([ref setValue:@"4" andPriority:@{}], @"Should throw");
  1171. XCTAssertThrows([ref setValue:@"5" andPriority:@[]], @"Should throw");
  1172. }
  1173. - (void) testRemoveFromOnMobileGraffitiBugAtAngelHack {
  1174. FIRDatabaseReference * node = [FTestHelpers getRandomNode];
  1175. __block BOOL done = NO;
  1176. [node observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot *snapshot) {
  1177. [[node child:[snapshot key]] removeValueWithCompletionBlock:^(NSError *err, FIRDatabaseReference *ref) {
  1178. done = YES;
  1179. }];
  1180. }];
  1181. [[node childByAutoId] setValue:@"moo"];
  1182. [self waitUntil:^BOOL{
  1183. return done;
  1184. }];
  1185. XCTAssertTrue(done, @"Properly finished");
  1186. }
  1187. - (void) testSetANodeWithAQuotedKey {
  1188. FIRDatabaseReference * node = [FTestHelpers getRandomNode];
  1189. __block BOOL done = NO;
  1190. __block FIRDataSnapshot * snap;
  1191. [node observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1192. snap = snapshot;
  1193. }];
  1194. [node setValue:@{@"\"herp\"": @1234} withCompletionBlock:^(NSError* err, FIRDatabaseReference * ref) {
  1195. done = YES;
  1196. XCTAssertEqualObjects(@1234, [[snap childSnapshotForPath:@"\"herp\""] value], @"Got it back");
  1197. }];
  1198. [self waitUntil:^BOOL{
  1199. return done;
  1200. }];
  1201. XCTAssertTrue(done, @"Properly finished");
  1202. }
  1203. - (void) testSetANodeWithASingleQuoteKey {
  1204. FIRDatabaseReference * node = [FTestHelpers getRandomNode];
  1205. __block BOOL done = NO;
  1206. __block FIRDataSnapshot * snap;
  1207. [node observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1208. snap = snapshot;
  1209. }];
  1210. [node setValue:@{@"\"": @1234} withCompletionBlock:^(NSError* err, FIRDatabaseReference * ref) {
  1211. done = YES;
  1212. XCTAssertEqualObjects(@1234, [[snap childSnapshotForPath:@"\""] value], @"Got it back");
  1213. }];
  1214. [self waitUntil:^BOOL{
  1215. return done;
  1216. }];
  1217. XCTAssertTrue(done, @"Properly finished");
  1218. }
  1219. - (void) testEmptyChildGetValueEventBeforeParent {
  1220. FIRDatabaseReference * node = [FTestHelpers getRandomNode];
  1221. NSArray* lookingFor = @[
  1222. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a/aa/aaa"] withEvent:FIRDataEventTypeValue withString:nil],
  1223. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a/aa"] withEvent:FIRDataEventTypeValue withString:nil],
  1224. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a"] withEvent:FIRDataEventTypeValue withString:nil],
  1225. ];
  1226. FEventTester* et = [[FEventTester alloc] initFrom:self];
  1227. [et addLookingFor:lookingFor];
  1228. [node setValue:@{@"b": @5}];
  1229. [et wait];
  1230. }
  1231. // iOS behavior is different from what the recursive set test looks for. We don't raise events synchronously
  1232. - (void) testOnAfterSetWaitsForLatestData {
  1233. // We test here that we don't cache sets, but they would be persisted so make sure we are running without
  1234. // persistence
  1235. FTupleFirebase* refs = [FTestHelpers getRandomNodePairWithoutPersistence];
  1236. FIRDatabaseReference * node1 = refs.one;
  1237. FIRDatabaseReference * node2 = refs.two;
  1238. __block BOOL ready = NO;
  1239. [node1 setValue:@5 withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  1240. [node2 setValue:@42 withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  1241. ready = YES;
  1242. }];
  1243. }];
  1244. [self waitUntil:^BOOL{
  1245. return ready;
  1246. }];
  1247. ready = NO;
  1248. [node1 observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1249. NSNumber *val = [snapshot value];
  1250. XCTAssertTrue([val isEqualToNumber:@42], @"Should not have cached earlier set");
  1251. ready = YES;
  1252. }];
  1253. [self waitUntil:^BOOL{
  1254. return ready;
  1255. }];
  1256. }
  1257. - (void) testOnceWaitsForLatestData {
  1258. // Can't tolerate stale data; so disable persistence.
  1259. FTupleFirebase* refs = [FTestHelpers getRandomNodePairWithoutPersistence];
  1260. FIRDatabaseReference * node1 = refs.one;
  1261. FIRDatabaseReference * node2 = refs.two;
  1262. __block BOOL ready = NO;
  1263. [node1 observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1264. id val = [snapshot value];
  1265. XCTAssertTrue([NSNull null] == val, @"First value should be null");
  1266. [node2 setValue:@5 withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1267. [node1 observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1268. NSNumber *val = [snapshot value];
  1269. XCTAssertTrue([val isKindOfClass:[NSNumber class]] && [val isEqualToNumber:@5], @"Should get first value");
  1270. ready = YES;
  1271. }];
  1272. }];
  1273. }];
  1274. [self waitUntil:^BOOL{
  1275. return ready;
  1276. }];
  1277. ready = NO;
  1278. [node2 setValue:@42 withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  1279. [node1 observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1280. NSNumber *val = [snapshot value];
  1281. XCTAssertTrue([val isEqualToNumber:@42], @"Got second number");
  1282. ready = YES;
  1283. }];
  1284. }];
  1285. [self waitUntil:^BOOL{
  1286. return ready;
  1287. }];
  1288. }
  1289. - (void) testMemoryFreeingOnUnlistenDoesNotCorruptData {
  1290. // Can't tolerate stale data; so disable persistence.
  1291. FTupleFirebase* refs = [FTestHelpers getRandomNodePairWithoutPersistence];
  1292. FIRDatabaseReference * node2 = [[refs.one root] childByAutoId];
  1293. __block BOOL hasRun = NO;
  1294. __block BOOL ready = NO;
  1295. FIRDatabaseHandle handle1 = [refs.one observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1296. if (!hasRun) {
  1297. hasRun = YES;
  1298. id val = [snapshot value];
  1299. XCTAssertTrue([NSNull null] == val, @"First time should be null");
  1300. [refs.one setValue:@"test" withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1301. ready = YES;
  1302. }];
  1303. }
  1304. }];
  1305. [self waitUntil:^BOOL{
  1306. return ready;
  1307. }];
  1308. [refs.one removeObserverWithHandle:handle1];
  1309. ready = NO;
  1310. [node2 setValue:@"hello" withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  1311. [refs.one observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1312. NSString *val = [snapshot value];
  1313. XCTAssertTrue([val isEqualToString:@"test"], @"Get back the value we set above");
  1314. [refs.two observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1315. NSString *val = [snapshot value];
  1316. XCTAssertTrue([val isEqualToString:@"test"], @"Get back the value we set above");
  1317. ready = YES;
  1318. }];
  1319. }];
  1320. }];
  1321. [self waitUntil:^BOOL{
  1322. return ready;
  1323. }];
  1324. //write {x: 1, y : {t: 2, u: 3}}
  1325. //Listen at /. Then listen at /x/t
  1326. //unlisten at /y/t. Off at /. Once at /. Ensure data is still all there.
  1327. //Once at /y. Ensure data is still all there.
  1328. refs = [FTestHelpers getRandomNodePairWithoutPersistence];
  1329. ready = NO;
  1330. __block FIRDatabaseHandle deeplisten = NSNotFound;
  1331. __block FIRDatabaseHandle slashlisten = NSNotFound;
  1332. __weak FIRDatabaseReference * refOne = refs.one;
  1333. [refs.one setValue:@{@"x": @1, @"y": @{@"t": @2, @"u": @3}} withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  1334. slashlisten = [refOne observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1335. deeplisten = [[refOne child:@"y/t"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1336. [[refOne child:@"y/t"] removeObserverWithHandle:deeplisten];
  1337. [refOne removeObserverWithHandle:slashlisten];
  1338. ready = YES;
  1339. }];
  1340. }];
  1341. }];
  1342. [self waitUntil:^BOOL{
  1343. return ready;
  1344. }];
  1345. ready = NO;
  1346. [[refs.one child:@"x"] setValue:@"test" withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  1347. [refs.one observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1348. NSDictionary *val = [snapshot value];
  1349. NSDictionary *expected = @{@"x" : @"test", @"y" : @{@"t" : @2, @"u" : @3}};
  1350. XCTAssertTrue([val isEqualToDictionary:expected], @"Got the final value");
  1351. ready = YES;
  1352. }];
  1353. }];
  1354. [self waitUntil:^BOOL{
  1355. return ready;
  1356. }];
  1357. }
  1358. - (void) testUpdateRaisesCorrectLocalEvents {
  1359. FIRDatabaseReference * node = [FTestHelpers getRandomNode];
  1360. __block FIRDataSnapshot * snap = nil;
  1361. [node observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1362. snap = snapshot;
  1363. }];
  1364. __block BOOL ready = NO;
  1365. [node setValue:@{@"a": @1, @"b": @2, @"c": @3, @"d": @4} withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  1366. ready = YES;
  1367. }];
  1368. [self waitUntil:^BOOL{
  1369. return ready;
  1370. }];
  1371. FEventTester* et = [[FEventTester alloc] initFrom:self];
  1372. NSArray* expectations = @[
  1373. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"a"] withEvent:FIRDataEventTypeValue withString:nil],
  1374. [[FTupleEventTypeString alloc] initWithFirebase:[node child:@"d"] withEvent:FIRDataEventTypeValue withString:nil],
  1375. [[FTupleEventTypeString alloc] initWithFirebase:node withEvent:FIRDataEventTypeChildChanged withString:@"a"],
  1376. [[FTupleEventTypeString alloc] initWithFirebase:node withEvent:FIRDataEventTypeChildChanged withString:@"d"],
  1377. [[FTupleEventTypeString alloc] initWithFirebase:node withEvent:FIRDataEventTypeValue withString:nil]
  1378. ];
  1379. [et addLookingFor:expectations];
  1380. [et waitForInitialization];
  1381. [node updateChildValues:@{@"a": @4, @"d": @1}];
  1382. [et wait];
  1383. }
  1384. - (void) testUpdateRaisesCorrectRemoteEvents {
  1385. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  1386. FIRDatabaseReference * reader = refs.one;
  1387. FIRDatabaseReference * writer = refs.two;
  1388. __block BOOL ready = NO;
  1389. [writer setValue:@{@"a": @1, @"b": @2, @"c": @3, @"d": @4} withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  1390. ready = YES;
  1391. }];
  1392. [self waitUntil:^BOOL{
  1393. return ready;
  1394. }];
  1395. FEventTester* et = [[FEventTester alloc] initFrom:self];
  1396. NSArray* expectations = @[
  1397. [[FTupleEventTypeString alloc] initWithFirebase:[reader child:@"a"] withEvent:FIRDataEventTypeValue withString:nil],
  1398. [[FTupleEventTypeString alloc] initWithFirebase:[reader child:@"d"] withEvent:FIRDataEventTypeValue withString:nil],
  1399. [[FTupleEventTypeString alloc] initWithFirebase:reader withEvent:FIRDataEventTypeChildChanged withString:@"a"],
  1400. [[FTupleEventTypeString alloc] initWithFirebase:reader withEvent:FIRDataEventTypeChildChanged withString:@"d"],
  1401. [[FTupleEventTypeString alloc] initWithFirebase:reader withEvent:FIRDataEventTypeValue withString:nil]
  1402. ];
  1403. [et addLookingFor:expectations];
  1404. [et waitForInitialization];
  1405. [writer updateChildValues:@{@"a": @4, @"d": @1}];
  1406. [et wait];
  1407. ready = NO;
  1408. [reader observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1409. NSDictionary *result = [snapshot value];
  1410. NSDictionary *expected = @{@"a" : @4, @"b" : @2, @"c" : @3, @"d" : @1};
  1411. XCTAssertTrue([result isEqualToDictionary:expected], @"Got expected results");
  1412. ready = YES;
  1413. }];
  1414. [self waitUntil:^BOOL{
  1415. return ready;
  1416. }];
  1417. }
  1418. - (void) testUpdateChangesAreStoredCorrectlyByTheServer {
  1419. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  1420. FIRDatabaseReference * reader = refs.one;
  1421. FIRDatabaseReference * writer = refs.two;
  1422. [self waitForCompletionOf:writer setValue:@{@"a": @1, @"b": @2, @"c": @3, @"d": @4}];
  1423. [self waitForCompletionOf:writer updateChildValues:@{@"a": @42}];
  1424. [self snapWaiter:reader withBlock:^(FIRDataSnapshot *snapshot) {
  1425. NSDictionary* result = [snapshot value];
  1426. NSDictionary* expected = @{@"a": @42, @"b": @2, @"c": @3, @"d": @4};
  1427. XCTAssertTrue([result isEqualToDictionary:expected], @"Expected updated value");
  1428. }];
  1429. }
  1430. - (void) testUpdateDoesntAffectPriorityLocally {
  1431. FIRDatabaseReference * ref = [FTestHelpers getRandomNode];
  1432. __block FIRDataSnapshot * snap = nil;
  1433. [ref observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1434. snap = snapshot;
  1435. }];
  1436. [ref setValue:@{@"a": @1, @"b": @2, @"c": @3} andPriority:@"testpri"];
  1437. [self waitUntil:^BOOL{
  1438. return snap != nil;
  1439. }];
  1440. XCTAssertTrue([[snap priority] isEqualToString:@"testpri"], @"Got initial priority");
  1441. snap = nil;
  1442. [ref updateChildValues:@{@"a": @4}];
  1443. [self waitUntil:^BOOL{
  1444. return snap != nil;
  1445. }];
  1446. XCTAssertTrue([[snap priority] isEqualToString:@"testpri"], @"Got initial priority");
  1447. }
  1448. - (void) testUpdateDoesntAffectPriorityRemotely {
  1449. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  1450. FIRDatabaseReference * reader = refs.one;
  1451. FIRDatabaseReference * writer = refs.two;
  1452. __block BOOL ready = NO;
  1453. [writer setValue:@{@"a": @1, @"b": @2, @"c": @3} andPriority:@"testpri" withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  1454. ready = YES;
  1455. }];
  1456. [self waitUntil:^BOOL{
  1457. return ready;
  1458. }];
  1459. ready = NO;
  1460. [reader observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1461. NSString *result = [snapshot priority];
  1462. XCTAssertTrue([result isEqualToString:@"testpri"], @"Expected initial priority");
  1463. ready = YES;
  1464. }];
  1465. [self waitUntil:^BOOL{
  1466. return ready;
  1467. }];
  1468. ready = NO;
  1469. [writer updateChildValues:@{@"a": @4} withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  1470. ready = YES;
  1471. }];
  1472. [self waitUntil:^BOOL{
  1473. return ready;
  1474. }];
  1475. ready = NO;
  1476. [reader observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1477. NSString *result = [snapshot priority];
  1478. XCTAssertTrue([result isEqualToString:@"testpri"], @"Expected initial priority");
  1479. ready = YES;
  1480. }];
  1481. [self waitUntil:^BOOL{
  1482. return ready;
  1483. }];
  1484. }
  1485. - (void) testUpdateReplacesChildrenAndIsNotRecursive {
  1486. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  1487. FIRDatabaseReference * reader = refs.one;
  1488. FIRDatabaseReference * writer = refs.two;
  1489. __block FIRDataSnapshot * localSnap = nil;
  1490. __block BOOL ready = NO;
  1491. [writer observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1492. localSnap = snapshot;
  1493. }];
  1494. [writer setValue:@{@"a": @{@"aa": @1, @"ab": @2}}];
  1495. [writer updateChildValues:@{@"a": @{@"aa": @1}} withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  1496. ready = YES;
  1497. }];
  1498. [self waitUntil:^BOOL{
  1499. return ready;
  1500. }];
  1501. ready = NO;
  1502. [reader observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1503. NSDictionary *result = [snapshot value];
  1504. NSDictionary *expected = @{@"a" : @{@"aa" : @1}};
  1505. XCTAssertTrue([result isEqualToDictionary:expected], @"Should get new value");
  1506. ready = YES;
  1507. }];
  1508. [self waitUntil:^BOOL{
  1509. NSDictionary* result = [localSnap value];
  1510. NSDictionary* expected = @{@"a": @{@"aa": @1}};
  1511. return ready && [result isEqualToDictionary:expected];
  1512. }];
  1513. }
  1514. - (void) testDeepUpdatesWork {
  1515. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  1516. FIRDatabaseReference * reader = refs.one;
  1517. FIRDatabaseReference * writer = refs.two;
  1518. __block FIRDataSnapshot * localSnap = nil;
  1519. __block BOOL ready = NO;
  1520. [writer observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1521. localSnap = snapshot;
  1522. }];
  1523. [writer setValue:@{@"a": @{@"aa": @1, @"ab": @2}}];
  1524. [writer updateChildValues:@{@"a/aa": @10,
  1525. @".priority": @3.0,
  1526. @"a/ab": @{@".priority": @2.0,
  1527. @".value": @20}}
  1528. withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  1529. ready = YES;
  1530. }];
  1531. [self waitUntil:^BOOL{
  1532. return ready;
  1533. }];
  1534. ready = NO;
  1535. [reader observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1536. NSDictionary *result = [snapshot value];
  1537. NSDictionary *expected = @{@"a" : @{@"aa" : @10, @"ab" : @20}};
  1538. XCTAssertTrue([result isEqualToDictionary:expected], @"Should get new value");
  1539. ready = YES;
  1540. }];
  1541. [self waitUntil:^BOOL{
  1542. NSDictionary* result = [localSnap value];
  1543. NSDictionary* expected = @{@"a": @{@"aa": @10, @"ab": @20}};
  1544. return ready && [result isEqualToDictionary:expected];
  1545. }];
  1546. }
  1547. // Type signature means we don't need a test for updating scalars. They wouldn't compile
  1548. - (void) testEmptyUpdateWorks {
  1549. FIRDatabaseReference * ref = [FTestHelpers getRandomNode];
  1550. __block BOOL ready = NO;
  1551. [ref updateChildValues:@{} withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  1552. XCTAssertTrue(error == nil, @"Should not be an error");
  1553. ready = YES;
  1554. }];
  1555. [self waitUntil:^BOOL{
  1556. return ready;
  1557. }];
  1558. }
  1559. // XXX update stress test
  1560. - (void) testUpdateFiresCorrectEventWhenAChildIsDeleted {
  1561. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  1562. FIRDatabaseReference * reader = refs.one;
  1563. FIRDatabaseReference * writer = refs.two;
  1564. __block FIRDataSnapshot * localSnap = nil;
  1565. __block FIRDataSnapshot * remoteSnap = nil;
  1566. [self waitForCompletionOf:writer setValue:@{@"a": @12, @"b": @6}];
  1567. [writer observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1568. localSnap = snapshot;
  1569. }];
  1570. [reader observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1571. remoteSnap = snapshot;
  1572. }];
  1573. [self waitUntil:^BOOL{
  1574. return localSnap != nil && remoteSnap != nil;
  1575. }];
  1576. localSnap = nil;
  1577. remoteSnap = nil;
  1578. [writer updateChildValues:@{@"a": [NSNull null]}];
  1579. [self waitUntil:^BOOL{
  1580. return localSnap != nil && remoteSnap != nil;
  1581. }];
  1582. NSDictionary* expected = @{@"b": @6};
  1583. XCTAssertTrue([[remoteSnap value] isEqualToDictionary:expected], @"Removed child");
  1584. XCTAssertTrue([[localSnap value] isEqualToDictionary:expected], @"Removed child");
  1585. }
  1586. - (void) testUpdateFiresCorrectEventOnNewChildren {
  1587. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  1588. FIRDatabaseReference * reader = refs.one;
  1589. FIRDatabaseReference * writer = refs.two;
  1590. __block FIRDataSnapshot * localSnap = nil;
  1591. __block FIRDataSnapshot * remoteSnap = nil;
  1592. [[writer child:@"a"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1593. localSnap = snapshot;
  1594. }];
  1595. [[reader child:@"a"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1596. remoteSnap = snapshot;
  1597. }];
  1598. [self waitUntil:^BOOL{
  1599. return localSnap != nil && remoteSnap != nil;
  1600. }];
  1601. localSnap = nil;
  1602. remoteSnap = nil;
  1603. [writer updateChildValues:@{@"a": @42}];
  1604. [self waitUntil:^BOOL{
  1605. return localSnap != nil && remoteSnap != nil;
  1606. }];
  1607. XCTAssertTrue([[remoteSnap value] isEqualToNumber:@42], @"Added child");
  1608. XCTAssertTrue([[localSnap value] isEqualToNumber:@42], @"Added child");
  1609. }
  1610. - (void) testUpdateFiresCorrectEventOnDeletedChildren {
  1611. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  1612. FIRDatabaseReference * reader = refs.one;
  1613. FIRDatabaseReference * writer = refs.two;
  1614. __block FIRDataSnapshot * localSnap = nil;
  1615. __block FIRDataSnapshot * remoteSnap = nil;
  1616. [self waitForCompletionOf:writer setValue:@{@"a": @12}];
  1617. [[writer child:@"a"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1618. localSnap = snapshot;
  1619. }];
  1620. [[reader child:@"a"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1621. remoteSnap = snapshot;
  1622. }];
  1623. [self waitUntil:^BOOL{
  1624. return localSnap != nil && remoteSnap != nil;
  1625. }];
  1626. localSnap = nil;
  1627. remoteSnap = nil;
  1628. [writer updateChildValues:@{@"a": [NSNull null]}];
  1629. [self waitUntil:^BOOL{
  1630. return localSnap != nil && remoteSnap != nil;
  1631. }];
  1632. XCTAssertTrue([remoteSnap value] == [NSNull null], @"Removed child");
  1633. XCTAssertTrue([localSnap value] == [NSNull null], @"Removed child");
  1634. }
  1635. - (void) testUpdateFiresCorrectEventOnChangedChildren {
  1636. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  1637. FIRDatabaseReference * reader = refs.one;
  1638. FIRDatabaseReference * writer = refs.two;
  1639. [self waitForCompletionOf:writer setValue:@{@"a": @12}];
  1640. __block FIRDataSnapshot * localSnap = nil;
  1641. __block FIRDataSnapshot * remoteSnap = nil;
  1642. [[writer child:@"a"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1643. localSnap = snapshot;
  1644. }];
  1645. [[reader child:@"a"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1646. remoteSnap = snapshot;
  1647. }];
  1648. [self waitUntil:^BOOL{
  1649. return localSnap != nil && remoteSnap != nil;
  1650. }];
  1651. localSnap = nil;
  1652. remoteSnap = nil;
  1653. [self waitForCompletionOf:writer updateChildValues:@{@"a": @11}];
  1654. [self waitUntil:^BOOL{
  1655. return localSnap != nil && remoteSnap != nil;
  1656. }];
  1657. XCTAssertTrue([[remoteSnap value] isEqualToNumber:@11], @"Changed child");
  1658. XCTAssertTrue([[localSnap value] isEqualToNumber:@11], @"Changed child");
  1659. }
  1660. - (void) testUpdateOfPriorityWorks {
  1661. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  1662. FIRDatabaseReference * reader = refs.one;
  1663. FIRDatabaseReference * writer = refs.two;
  1664. __block BOOL ready = NO;
  1665. [writer setValue:@{@"a": @5, @".priority": @"pri1"}];
  1666. [writer updateChildValues:@{@"a": @6, @".priority": @"pri2", @"b": @{ @".priority": @"pri3", @"c": @10 } } withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  1667. NSLog(@"error? %@", error);
  1668. ready = YES;
  1669. }];
  1670. [self waitUntil:^BOOL{
  1671. return ready;
  1672. }];
  1673. ready = NO;
  1674. [reader observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1675. XCTAssertEqualObjects([[snapshot childSnapshotForPath:@"a"] value], @6, @"Should match write values");
  1676. XCTAssertTrue([[snapshot priority] isEqualToString:@"pri2"], @"Should get updated priority");
  1677. XCTAssertTrue([[[snapshot childSnapshotForPath:@"b"] priority] isEqualToString:@"pri3"], @"Should get updated priority");
  1678. XCTAssertEqualObjects([[snapshot childSnapshotForPath:@"b/c"] value], @10, @"Should match write values");
  1679. ready = YES;
  1680. }];
  1681. [self waitUntil:^BOOL{
  1682. return ready;
  1683. }];
  1684. }
  1685. - (void) testSetWithCircularReferenceFails {
  1686. FIRDatabaseReference * ref = [FTestHelpers getRandomNode];
  1687. NSMutableDictionary* toSet = [[NSMutableDictionary alloc] init];
  1688. NSDictionary* lol = @{@"foo": @"bar", @"circular": toSet};
  1689. [toSet setObject:lol forKey:@"lol"];
  1690. XCTAssertThrows([ref setValue:toSet], @"Should not be able to set circular dictionary");
  1691. }
  1692. - (void) testLargeNumbers {
  1693. FIRDatabaseReference * ref = [FTestHelpers getRandomNode];
  1694. long long jsMaxInt = 9007199254740992;
  1695. long jsMaxIntPlusOne = jsMaxInt + 1;
  1696. NSNumber* toSet = [NSNumber numberWithLong:jsMaxIntPlusOne];
  1697. [ref setValue:toSet];
  1698. __block FIRDataSnapshot * snap = nil;
  1699. [ref observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1700. snap = snapshot;
  1701. }];
  1702. [self waitUntil:^BOOL{
  1703. return snap != nil;
  1704. }];
  1705. NSNumber* result = [snap value];
  1706. XCTAssertTrue([result isEqualToNumber:toSet], @"Should get back same number");
  1707. toSet = [NSNumber numberWithLong:LONG_MAX];
  1708. snap = nil;
  1709. [ref setValue:toSet];
  1710. [self waitUntil:^BOOL{
  1711. return snap != nil;
  1712. }];
  1713. result = [snap value];
  1714. XCTAssertTrue([result isEqualToNumber:toSet], @"Should get back same number");
  1715. snap = nil;
  1716. toSet = [NSNumber numberWithDouble:DBL_MAX];
  1717. [ref setValue:toSet];
  1718. [self waitUntil:^BOOL{
  1719. return snap != nil;
  1720. }];
  1721. result = [snap value];
  1722. XCTAssertTrue([result isEqualToNumber:toSet], @"Should get back same number");
  1723. }
  1724. - (void) testParentDeleteShadowsChildListeners {
  1725. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  1726. FIRDatabaseReference * writer = refs.one;
  1727. FIRDatabaseReference * deleter = refs.two;
  1728. NSString* childName = [writer childByAutoId].key;
  1729. __block BOOL called = NO;
  1730. [[deleter child:childName] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1731. XCTAssertFalse(called, @"Should only be hit once");
  1732. called = YES;
  1733. XCTAssertTrue(snapshot.value == [NSNull null], @"Value should be null");
  1734. }];
  1735. WAIT_FOR(called);
  1736. __block BOOL done = NO;
  1737. [[writer child:childName] setValue:@"foo"];
  1738. [deleter removeValueWithCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  1739. done = YES;
  1740. }];
  1741. WAIT_FOR(done);
  1742. }
  1743. - (void) testParentDeleteShadowsChildListenersWithNonDefaultQuery {
  1744. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  1745. FIRDatabaseReference * writer = refs.one;
  1746. FIRDatabaseReference * deleter = refs.two;
  1747. NSString* childName = [writer childByAutoId].key;
  1748. __block BOOL queryCalled = NO;
  1749. __block BOOL deepChildCalled = NO;
  1750. [[[[deleter child:childName] queryOrderedByPriority] queryStartingAtValue:nil childKey:@"b"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1751. XCTAssertFalse(queryCalled, @"Should only be hit once");
  1752. queryCalled = YES;
  1753. XCTAssertTrue(snapshot.value == [NSNull null], @"Value should be null");
  1754. }];
  1755. [[[deleter child:childName] child:@"a"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1756. XCTAssertFalse(deepChildCalled, @"Should only be hit once");
  1757. deepChildCalled = YES;
  1758. XCTAssertTrue(snapshot.value == [NSNull null], @"Value should be null");
  1759. }];
  1760. WAIT_FOR(deepChildCalled && queryCalled);
  1761. __block BOOL done = NO;
  1762. [[writer child:childName] setValue:@"foo"];
  1763. [deleter removeValueWithCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  1764. done = YES;
  1765. }];
  1766. WAIT_FOR(done);
  1767. }
  1768. - (void) testLocalServerValuesEventuallyButNotImmediatelyMatchServer {
  1769. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  1770. FIRDatabaseReference* writer = refs.one;
  1771. FIRDatabaseReference* reader = refs.two;
  1772. __block int done = 0;
  1773. NSMutableArray* readSnaps = [[NSMutableArray alloc] init];
  1774. NSMutableArray* writeSnaps = [[NSMutableArray alloc] init];
  1775. [reader observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1776. if ([snapshot value] != [NSNull null]) {
  1777. [readSnaps addObject:snapshot];
  1778. if (readSnaps.count == 1) {
  1779. done += 1;
  1780. }
  1781. }
  1782. }];
  1783. [writer observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1784. if ([snapshot value] != [NSNull null]) {
  1785. [writeSnaps addObject:snapshot];
  1786. if (writeSnaps.count == 2) {
  1787. done += 1;
  1788. }
  1789. }
  1790. }];
  1791. [writer setValue:[FIRServerValue timestamp] andPriority:[FIRServerValue timestamp]];
  1792. [self waitUntil:^BOOL{
  1793. return done == 2;
  1794. }];
  1795. XCTAssertEqual((unsigned long)[readSnaps count], (unsigned long)1, @"Should have received one snapshot on reader");
  1796. XCTAssertEqual((unsigned long)[writeSnaps count], (unsigned long)2, @"Should have received two snapshots on writer");
  1797. FIRDataSnapshot * firstReadSnap = [readSnaps objectAtIndex:0];
  1798. FIRDataSnapshot * firstWriteSnap = [writeSnaps objectAtIndex:0];
  1799. FIRDataSnapshot * secondWriteSnap = [writeSnaps objectAtIndex:1];
  1800. NSNumber* now = [NSNumber numberWithDouble:round([[NSDate date] timeIntervalSince1970]*1000)];
  1801. XCTAssertTrue([now doubleValue] - [firstWriteSnap.value doubleValue] < 3000, @"Should have received a local event with a value close to timestamp");
  1802. XCTAssertTrue([now doubleValue] - [firstWriteSnap.priority doubleValue] < 3000, @"Should have received a local event with a priority close to timestamp");
  1803. XCTAssertTrue([now doubleValue] - [secondWriteSnap.value doubleValue] < 3000, @"Should have received a server event with a value close to timestamp");
  1804. XCTAssertTrue([now doubleValue] - [secondWriteSnap.priority doubleValue] < 3000, @"Should have received a server event with a priority close to timestamp");
  1805. XCTAssertFalse([firstWriteSnap value] == [secondWriteSnap value], @"Initial and future writer values should be different");
  1806. XCTAssertFalse([firstWriteSnap priority] == [secondWriteSnap priority], @"Initial and future writer priorities should be different");
  1807. XCTAssertEqualObjects(firstReadSnap.value, secondWriteSnap.value, @"Eventual reader and writer values should be equal");
  1808. XCTAssertEqualObjects(firstReadSnap.priority, secondWriteSnap.priority, @"Eventual reader and writer priorities should be equal");
  1809. }
  1810. - (void) testServerValuesSetWithPriorityRemoteEvents {
  1811. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  1812. FIRDatabaseReference * writer = refs.one;
  1813. FIRDatabaseReference * reader = refs.two;
  1814. NSDictionary* data = @{
  1815. @"a": [FIRServerValue timestamp],
  1816. @"b": @{
  1817. @".value": [FIRServerValue timestamp],
  1818. @".priority": [FIRServerValue timestamp]
  1819. }
  1820. };
  1821. __block BOOL done = NO;
  1822. [writer setValue:data andPriority:[FIRServerValue timestamp] withCompletionBlock:^(NSError* err, FIRDatabaseReference * ref) { done = YES; }];
  1823. [self waitUntil:^BOOL{
  1824. return done;
  1825. }];
  1826. [self snapWaiter:reader withBlock:^(FIRDataSnapshot *snapshot) {
  1827. NSDictionary* value = [snapshot value];
  1828. NSNumber* now = [NSNumber numberWithDouble:round([[NSDate date] timeIntervalSince1970]*1000)];
  1829. NSNumber* timestamp = [snapshot priority];
  1830. XCTAssertTrue([[snapshot priority] isKindOfClass:[NSNumber class]], @"Should get back number");
  1831. XCTAssertTrue([now doubleValue] - [timestamp doubleValue] < 2000, @"Number should be no more than 2 seconds ago");
  1832. XCTAssertEqualObjects([snapshot priority], [value objectForKey:@"a"], @"Should get back matching ServerValue.TIMESTAMP");
  1833. XCTAssertEqualObjects([snapshot priority], [value objectForKey:@"b"], @"Should get back matching ServerValue.TIMESTAMP");
  1834. XCTAssertEqualObjects([snapshot priority], [[snapshot childSnapshotForPath:@"b"] priority], @"Should get back matching ServerValue.TIMESTAMP");
  1835. }];
  1836. }
  1837. - (void) testServerValuesSetPriorityRemoteEvents {
  1838. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  1839. FIRDatabaseReference * writer = refs.one;
  1840. FIRDatabaseReference * reader = refs.two;
  1841. __block FIRDataSnapshot *snap = nil;
  1842. [reader observeEventType:FIRDataEventTypeChildMoved withBlock:^(FIRDataSnapshot *snapshot) {
  1843. snap = snapshot;
  1844. }];
  1845. [self waitForCompletionOf:[writer child:@"a"] setValue:@1 andPriority:nil];
  1846. [self waitForCompletionOf:[writer child:@"b"] setValue:@1 andPriority:@1];
  1847. [self waitForValueOf:[reader child:@"a"] toBe:@1];
  1848. __block BOOL done = NO;
  1849. [[writer child:@"a"] setPriority:[FIRServerValue timestamp] withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  1850. done = YES;
  1851. }];
  1852. [self waitUntil:^BOOL{
  1853. return done && snap != nil;
  1854. }];
  1855. NSNumber* now = [NSNumber numberWithDouble:round([[NSDate date] timeIntervalSince1970]*1000)];
  1856. NSNumber* timestamp = [snap priority];
  1857. XCTAssertTrue([[snap priority] isKindOfClass:[NSNumber class]], @"Should get back number");
  1858. XCTAssertTrue([now doubleValue] - [timestamp doubleValue] < 2000, @"Number should be no more than 2 seconds ago");
  1859. }
  1860. - (void) testServerValuesUpdateRemoteEvents {
  1861. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  1862. FIRDatabaseReference * writer = refs.one;
  1863. FIRDatabaseReference * reader = refs.two;
  1864. __block FIRDataSnapshot *snap = nil;
  1865. __block BOOL done = NO;
  1866. [reader observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1867. snap = snapshot;
  1868. if (snap && [[snap childSnapshotForPath:@"a/b/d"] value] != [NSNull null]) {
  1869. done = YES;
  1870. }
  1871. }];
  1872. [[writer child:@"a/b/c"] setValue:@1];
  1873. [[writer child:@"a"] updateChildValues:@{ @"b": @{ @"c": [FIRServerValue timestamp], @"d":@1 } }];
  1874. [self waitUntil:^BOOL{
  1875. return done;
  1876. }];
  1877. NSNumber* now = [NSNumber numberWithDouble:round([[NSDate date] timeIntervalSince1970]*1000)];
  1878. NSNumber* timestamp = [[snap childSnapshotForPath:@"a/b/c"] value];
  1879. XCTAssertTrue([[[snap childSnapshotForPath:@"a/b/c"] value] isKindOfClass:[NSNumber class]], @"Should get back number");
  1880. XCTAssertTrue([now doubleValue] - [timestamp doubleValue] < 2000, @"Number should be no more than 2 seconds ago");
  1881. }
  1882. - (void) testServerValuesSetWithPriorityLocalEvents {
  1883. FIRDatabaseReference * node = [FTestHelpers getRandomNode];
  1884. NSDictionary* data = @{
  1885. @"a": [FIRServerValue timestamp],
  1886. @"b": @{
  1887. @".value": [FIRServerValue timestamp],
  1888. @".priority": [FIRServerValue timestamp]
  1889. }
  1890. };
  1891. __block FIRDataSnapshot *snap = nil;
  1892. [node observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1893. snap = snapshot;
  1894. }];
  1895. __block BOOL done = NO;
  1896. [node setValue:data andPriority:[FIRServerValue timestamp] withCompletionBlock:^(NSError* err, FIRDatabaseReference * ref) { done = YES; }];
  1897. [self waitUntil:^BOOL{
  1898. return done;
  1899. }];
  1900. [self snapWaiter:node withBlock:^(FIRDataSnapshot *snapshot) {
  1901. NSDictionary* value = [snapshot value];
  1902. NSNumber* now = [NSNumber numberWithDouble:round([[NSDate date] timeIntervalSince1970]*1000)];
  1903. NSNumber* timestamp = [snapshot priority];
  1904. XCTAssertTrue([[snapshot priority] isKindOfClass:[NSNumber class]], @"Should get back number");
  1905. XCTAssertTrue([now doubleValue] - [timestamp doubleValue] < 2000, @"Number should be no more than 2 seconds ago");
  1906. XCTAssertEqualObjects([snapshot priority], [value objectForKey:@"a"], @"Should get back matching ServerValue.TIMESTAMP");
  1907. XCTAssertEqualObjects([snapshot priority], [value objectForKey:@"b"], @"Should get back matching ServerValue.TIMESTAMP");
  1908. XCTAssertEqualObjects([snapshot priority], [[snapshot childSnapshotForPath:@"b"] priority], @"Should get back matching ServerValue.TIMESTAMP");
  1909. }];
  1910. }
  1911. - (void) testServerValuesSetPriorityLocalEvents {
  1912. FIRDatabaseReference * node = [FTestHelpers getRandomNode];
  1913. __block FIRDataSnapshot *snap = nil;
  1914. [node observeEventType:FIRDataEventTypeChildMoved withBlock:^(FIRDataSnapshot *snapshot) {
  1915. snap = snapshot;
  1916. }];
  1917. __block BOOL done = NO;
  1918. [[node child:@"a"] setValue:@1 andPriority:nil];
  1919. [[node child:@"b"] setValue:@1 andPriority:@1];
  1920. [[node child:@"a"] setPriority:[FIRServerValue timestamp] withCompletionBlock:^(NSError* error, FIRDatabaseReference * ref) {
  1921. done = YES;
  1922. }];
  1923. [self waitUntil:^BOOL{
  1924. return done;
  1925. }];
  1926. NSNumber* now = [NSNumber numberWithDouble:round([[NSDate date] timeIntervalSince1970]*1000)];
  1927. NSNumber* timestamp = [snap priority];
  1928. XCTAssertTrue([[snap priority] isKindOfClass:[NSNumber class]], @"Should get back number");
  1929. XCTAssertTrue([now doubleValue] - [timestamp doubleValue] < 2000, @"Number should be no more than 2 seconds ago");
  1930. }
  1931. - (void) testServerValuesUpdateLocalEvents {
  1932. FIRDatabaseReference * node1 = [FTestHelpers getRandomNode];
  1933. __block FIRDataSnapshot *snap1 = nil;
  1934. [node1 observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1935. snap1 = snapshot;
  1936. }];
  1937. __block FIRDataSnapshot *snap2 = nil;
  1938. [node1 observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1939. snap2 = snapshot;
  1940. }];
  1941. [node1 runTransactionBlock:^FIRTransactionResult *(FIRMutableData *currentData) {
  1942. [currentData setValue:[FIRServerValue timestamp]];
  1943. return [FIRTransactionResult successWithValue:currentData];
  1944. }];
  1945. [self waitUntil:^BOOL{
  1946. return snap1 != nil && snap2 != nil && [snap1 value] != nil && [snap2 value] != nil;
  1947. }];
  1948. NSNumber* now = [NSNumber numberWithDouble:round([[NSDate date] timeIntervalSince1970]*1000)];
  1949. NSNumber* timestamp1 = [snap1 value];
  1950. XCTAssertTrue([[snap1 value] isKindOfClass:[NSNumber class]], @"Should get back number");
  1951. XCTAssertTrue([now doubleValue] - [timestamp1 doubleValue] < 2000, @"Number should be no more than 2 seconds ago");
  1952. NSNumber* timestamp2 = [snap2 value];
  1953. XCTAssertTrue([[snap2 value] isKindOfClass:[NSNumber class]], @"Should get back number");
  1954. XCTAssertTrue([now doubleValue] - [timestamp2 doubleValue] < 2000, @"Number should be no more than 2 seconds ago");
  1955. }
  1956. - (void) testServerValuesTransactionLocalEvents {
  1957. FIRDatabaseReference * node = [FTestHelpers getRandomNode];
  1958. __block FIRDataSnapshot *snap = nil;
  1959. [node observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1960. snap = snapshot;
  1961. }];
  1962. [[node child:@"a/b/c"] setValue:@1];
  1963. [[node child:@"a"] updateChildValues:@{ @"b": @{ @"c": [FIRServerValue timestamp], @"d":@1 } }];
  1964. [self waitUntil:^BOOL{
  1965. return snap != nil && [[snap childSnapshotForPath:@"a/b/d"] value] != nil;
  1966. }];
  1967. NSNumber* now = [NSNumber numberWithDouble:round([[NSDate date] timeIntervalSince1970]*1000)];
  1968. NSNumber* timestamp = [[snap childSnapshotForPath:@"a/b/c"] value];
  1969. XCTAssertTrue([[[snap childSnapshotForPath:@"a/b/c"] value] isKindOfClass:[NSNumber class]], @"Should get back number");
  1970. XCTAssertTrue([now doubleValue] - [timestamp doubleValue] < 2000, @"Number should be no more than 2 seconds ago");
  1971. }
  1972. - (void) testUpdateAfterChildSet {
  1973. FIRDatabaseReference *node = [FTestHelpers getRandomNode];
  1974. __block BOOL done = NO;
  1975. __weak FIRDatabaseReference *weakRef = node;
  1976. [node setValue:@{@"a": @"a"} withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  1977. [weakRef observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1978. if (snapshot.childrenCount == 3 && [snapshot hasChild:@"a"] && [snapshot hasChild:@"b"] && [snapshot hasChild:@"c"]) {
  1979. done = YES;
  1980. }
  1981. }];
  1982. [[weakRef child:@"b"] setValue:@"b"];
  1983. [weakRef updateChildValues:@{@"c" : @"c"}];
  1984. }];
  1985. [self waitUntil:^BOOL{
  1986. return done;
  1987. }];
  1988. }
  1989. - (void) testDeltaSyncNoDataUpdatesAfterReconnect {
  1990. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  1991. FIRDatabaseConfig *cfg = [FIRDatabaseConfig configForName:@"test-config"];
  1992. FIRDatabaseReference * ref2 = [[[FIRDatabaseReference alloc] initWithConfig:cfg] child:ref.key];
  1993. __block id data = @{ @"a": @1, @"b": @2, @"c": @{ @".priority": @3, @".value": @3}, @"d": @4 };
  1994. [self waitForCompletionOf:ref setValue:data];
  1995. __block BOOL gotData = NO;
  1996. [ref2 observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  1997. XCTAssertFalse(gotData, @"event triggered twice.");
  1998. gotData = YES;
  1999. XCTAssertEqualObjects(snapshot.valueInExportFormat, data, @"Got wrong data.");
  2000. }];
  2001. [self waitUntil:^BOOL{ return gotData; }];
  2002. __block BOOL done = NO;
  2003. XCTAssertEqual(ref2.repo.dataUpdateCount, 1L, @"Should have gotten one update.");
  2004. // Bounce connection
  2005. [FRepoManager interrupt:cfg];
  2006. [FRepoManager resume:cfg];
  2007. [[[ref2 root] child:@".info/connected"] observeEventType:FIRDataEventTypeValue
  2008. withBlock:^(FIRDataSnapshot *snapshot) {
  2009. if ([snapshot.value boolValue]) {
  2010. // We're connected. Do one more round-trip to make sure all state restoration is done
  2011. [[[ref2 root] child:@"foobar/empty/blah"] setValue:nil withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  2012. XCTAssertEqual(ref2.repo.dataUpdateCount, 1L, @"Should have gotten one update.");
  2013. done = YES;
  2014. }];
  2015. }
  2016. }
  2017. ];
  2018. [self waitUntil:^BOOL{ return done; }];
  2019. // cleanup
  2020. [FRepoManager interrupt:cfg];
  2021. [FRepoManager disposeRepos:cfg];
  2022. }
  2023. - (void) testServerValuesEventualConsistencyBetweenLocalAndRemote {
  2024. FTupleFirebase* refs = [FTestHelpers getRandomNodePair];
  2025. FIRDatabaseReference * writer = refs.one;
  2026. FIRDatabaseReference * reader = refs.two;
  2027. __block FIRDataSnapshot *writerSnap = nil;
  2028. __block FIRDataSnapshot *readerSnap = nil;
  2029. [reader observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  2030. readerSnap = snapshot;
  2031. }];
  2032. [writer observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  2033. writerSnap = snapshot;
  2034. }];
  2035. [writer setValue:[FIRServerValue timestamp] andPriority:[FIRServerValue timestamp]];
  2036. [self waitUntil:^BOOL{
  2037. if (readerSnap && writerSnap && [[readerSnap value] isKindOfClass:[NSNumber class]] && [[writerSnap value] isKindOfClass:[NSNumber class]]) {
  2038. if ([[readerSnap value] doubleValue] == [[writerSnap value] doubleValue]) {
  2039. return YES;
  2040. }
  2041. }
  2042. return NO;
  2043. }];
  2044. }
  2045. // Listens at a location and then creates a bunch of children, waiting for them all to complete.
  2046. - (void) testChildAddedPerf1 {
  2047. if (!runPerfTests) return;
  2048. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  2049. [ref observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot *snapshot) {
  2050. }];
  2051. NSDate *start = [NSDate date];
  2052. int COUNT = 1000;
  2053. __block BOOL done = NO;
  2054. __block NSDate *finished = nil;
  2055. for(int i = 0; i < COUNT; i++) {
  2056. [[ref childByAutoId] setValue:@"01234567890123456789012345678901234567890123456789" withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  2057. if (i == (COUNT - 1)) {
  2058. finished = [NSDate date];
  2059. done = YES;
  2060. }
  2061. }];
  2062. }
  2063. [self waitUntil:^BOOL {
  2064. return done;
  2065. } timeout:300];
  2066. NSTimeInterval elapsed = [finished timeIntervalSinceDate:start];
  2067. NSLog(@"Elapsed: %f", elapsed);
  2068. }
  2069. // Listens at a location, then adds a bunch of grandchildren under a single child.
  2070. - (void) testDeepChildAddedPerf1 {
  2071. if (!runPerfTests) return;
  2072. FIRDatabaseReference *ref = [FTestHelpers getRandomNode],
  2073. *childRef = [ref child:@"child"];
  2074. [ref observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot *snapshot) {
  2075. }];
  2076. NSDate *start = [NSDate date];
  2077. int COUNT = 1000;
  2078. __block BOOL done = NO;
  2079. __block NSDate *finished = nil;
  2080. for(int i = 0; i < COUNT; i++) {
  2081. [[childRef childByAutoId] setValue:@"01234567890123456789012345678901234567890123456789" withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  2082. if (i == (COUNT - 1)) {
  2083. finished = [NSDate date];
  2084. done = YES;
  2085. }
  2086. }];
  2087. }
  2088. [self waitUntil:^BOOL {
  2089. return done;
  2090. } timeout:300];
  2091. NSTimeInterval elapsed = [finished timeIntervalSinceDate:start];
  2092. NSLog(@"Elapsed: %f", elapsed);
  2093. }
  2094. // Listens at a location, then adds a bunch of grandchildren under a single child, but does it with merges.
  2095. // NOTE[2015-07-14]: This test is still pretty slow, because [FWriteTree removeWriteId] ends up rebuilding the tree after
  2096. // every ack.
  2097. - (void) testDeepChildAddedPerfViaMerge1 {
  2098. if (!runPerfTests) return;
  2099. FIRDatabaseReference *ref = [FTestHelpers getRandomNode],
  2100. *childRef = [ref child:@"child"];
  2101. [ref observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot *snapshot) {
  2102. }];
  2103. NSDate *start = [NSDate date];
  2104. int COUNT = 250;
  2105. __block BOOL done = NO;
  2106. __block NSDate *finished = nil;
  2107. for(int i = 0; i < COUNT; i++) {
  2108. NSString *childName = [childRef childByAutoId].key;
  2109. [childRef updateChildValues:@{
  2110. childName: @"01234567890123456789012345678901234567890123456789"
  2111. } withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  2112. if (i == (COUNT - 1)) {
  2113. finished = [NSDate date];
  2114. done = YES;
  2115. }
  2116. }];
  2117. }
  2118. [self waitUntil:^BOOL {
  2119. return done;
  2120. } timeout:300];
  2121. NSTimeInterval elapsed = [finished timeIntervalSinceDate:start];
  2122. NSLog(@"Elapsed: %f", elapsed);
  2123. }
  2124. @end