FData.m 110 KB

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