FData.m 115 KB

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