FSyncPointTests.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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 <Foundation/Foundation.h>
  17. #import <FirebaseCore/FIRLogger.h>
  18. #import "FSyncPointTests.h"
  19. #import "FListenProvider.h"
  20. #import "FQuerySpec.h"
  21. #import "FQueryParams.h"
  22. #import "FPathIndex.h"
  23. #import "FKeyIndex.h"
  24. #import "FPriorityIndex.h"
  25. #import "FIRDatabaseQuery_Private.h"
  26. #import "FSyncTree.h"
  27. #import "FChange.h"
  28. #import "FDataEvent.h"
  29. #import "FIRDataSnapshot_Private.h"
  30. #import "FCancelEvent.h"
  31. #import "FSnapshotUtilities.h"
  32. #import "FEventRegistration.h"
  33. #import "FCompoundWrite.h"
  34. #import "FEmptyNode.h"
  35. #import "FTestClock.h"
  36. #import "FIRDatabaseConfig_Private.h"
  37. #import "FSnapshotUtilities.h"
  38. typedef NSDictionary* (^fbt_nsdictionary_void)(void);
  39. @interface FTestEventRegistration : NSObject<FEventRegistration>
  40. @property (nonatomic, strong) NSDictionary *spec;
  41. @property (nonatomic, strong) FQuerySpec *query;
  42. @end
  43. @implementation FTestEventRegistration
  44. - (id) initWithSpec:(NSDictionary *)eventSpec query:(FQuerySpec *)query {
  45. self = [super init];
  46. if (self) {
  47. self.spec = eventSpec;
  48. self.query = query;
  49. }
  50. return self;
  51. }
  52. - (BOOL) responseTo:(FIRDataEventType)eventType {
  53. return YES;
  54. }
  55. - (FDataEvent *) createEventFrom:(FChange *)change query:(FQuerySpec *)query {
  56. FIRDataSnapshot *snap = nil;
  57. FIRDatabaseReference *ref = [[FIRDatabaseReference alloc] initWithRepo:nil path:query.path];
  58. if (change.type == FIRDataEventTypeValue) {
  59. snap = [[FIRDataSnapshot alloc] initWithRef:ref indexedNode:change.indexedNode];
  60. } else {
  61. snap = [[FIRDataSnapshot alloc] initWithRef:[ref child:change.childKey]
  62. indexedNode:change.indexedNode];
  63. }
  64. return [[FDataEvent alloc] initWithEventType:change.type eventRegistration:self dataSnapshot:snap prevName:change.prevKey];
  65. }
  66. - (BOOL) matches:(id<FEventRegistration>)other {
  67. if (![other isKindOfClass:[FTestEventRegistration class]]) {
  68. return NO;
  69. } else {
  70. FTestEventRegistration *otherRegistration = other;
  71. if (self.spec[@"callbackId"] && otherRegistration.spec[@"callbackId"] &&
  72. [self.spec[@"callbackId"] isEqualToNumber:otherRegistration.spec[@"callbackId"]]) {
  73. return YES;
  74. } else {
  75. return NO;
  76. }
  77. }
  78. }
  79. - (void) fireEvent:(id<FEvent>)event queue:(dispatch_queue_t)queue {
  80. [NSException raise:@"NotImplementedError" format:@"Method not implemneted."];
  81. }
  82. - (FCancelEvent *) createCancelEventFromError:(NSError *)error path:(FPath *)path {
  83. [NSException raise:@"NotImplementedError" format:@"Method not implemneted."];
  84. return nil;
  85. }
  86. - (FIRDatabaseHandle) handle {
  87. [NSException raise:@"NotImplementedError" format:@"Method not implemneted."];
  88. return 0;
  89. }
  90. @end
  91. @implementation FSyncPointTests
  92. - (NSString *) queryKeyForQuery:(FQuerySpec *)query tagId:(NSNumber *)tagId {
  93. return [NSString stringWithFormat:@"%@|%@|%@", query.path, query.params, tagId];
  94. }
  95. - (void) actualEvent:(FDataEvent *)actual equalsExpected:(NSDictionary *)expected {
  96. XCTAssertEqual(actual.eventType, [self stringToEventType:expected[@"type"]], @"Event type should be equal");
  97. if (actual.eventType != FIRDataEventTypeValue) {
  98. NSString *childName = actual.snapshot.key;
  99. XCTAssertEqualObjects(childName, expected[@"name"], @"Snapshot name should be equal");
  100. if (expected[@"prevName"] == [NSNull null]) {
  101. XCTAssertNil(actual.prevName, @"prevName should be nil");
  102. } else {
  103. XCTAssertEqualObjects(actual.prevName, expected[@"prevName"], @"prevName should be equal");
  104. }
  105. }
  106. NSString *actualHash = [actual.snapshot.node.node dataHash];
  107. NSString *expectedHash = [[FSnapshotUtilities nodeFrom:expected[@"data"]] dataHash];
  108. XCTAssertEqualObjects(actualHash, expectedHash, @"Data hash should be equal");
  109. }
  110. /**
  111. * @param actual is an array of id<FEvent>
  112. * @param expected is an array of dictionaries?
  113. */
  114. - (void) actualEvents:(NSArray *)actual exactMatchesExpected:(NSArray *)expected {
  115. if ([expected count] < [actual count]) {
  116. XCTFail(@"Got extra events: %@", actual);
  117. } else if ([expected count] > [actual count]) {
  118. XCTFail(@"Missing events: %@", actual);
  119. } else {
  120. NSUInteger i = 0;
  121. for (i = 0; i < [expected count]; i++) {
  122. FDataEvent *actualEvent = actual[i];
  123. NSDictionary *expectedEvent = expected[i];
  124. [self actualEvent:actualEvent equalsExpected:expectedEvent];
  125. }
  126. }
  127. }
  128. - (void)assertOrderedFirstEvent:(FIRDataEventType)e1 secondEvent:(FIRDataEventType)e2 {
  129. static NSArray *eventOrdering = nil;
  130. if (!eventOrdering) {
  131. eventOrdering = @[
  132. [NSNumber numberWithInteger:FIRDataEventTypeChildRemoved],
  133. [NSNumber numberWithInteger:FIRDataEventTypeChildAdded],
  134. [NSNumber numberWithInteger:FIRDataEventTypeChildMoved],
  135. [NSNumber numberWithInteger:FIRDataEventTypeChildChanged],
  136. [NSNumber numberWithInteger:FIRDataEventTypeValue]
  137. ];
  138. }
  139. NSUInteger idx1 = [eventOrdering indexOfObject:[NSNumber numberWithInteger:e1]];
  140. NSUInteger idx2 = [eventOrdering indexOfObject:[NSNumber numberWithInteger:e2]];
  141. if (idx1 > idx2) {
  142. XCTFail(@"Received %d after %d", (int)e2, (int)e1);
  143. }
  144. }
  145. - (FIRDataEventType)stringToEventType:(NSString *)stringType {
  146. if ([stringType isEqualToString:@"child_added"]) {
  147. return FIRDataEventTypeChildAdded;
  148. } else if ([stringType isEqualToString:@"child_removed"]) {
  149. return FIRDataEventTypeChildRemoved;
  150. } else if ([stringType isEqualToString:@"child_changed"]) {
  151. return FIRDataEventTypeChildChanged;
  152. } else if ([stringType isEqualToString:@"child_moved"]) {
  153. return FIRDataEventTypeChildMoved;
  154. } else if ([stringType isEqualToString:@"value"]) {
  155. return FIRDataEventTypeValue;
  156. } else {
  157. XCTFail(@"Unknown event type %@", stringType);
  158. return FIRDataEventTypeValue;
  159. }
  160. }
  161. - (void) actualEventSet:(id)actual matchesExpected:(id)expected atBasePath:(NSString *)basePathStr {
  162. // don't worry about order for now
  163. XCTAssertEqual([expected count], [actual count], @"Mismatched lengths.\nExpected: %@\nActual: %@", expected, actual);
  164. NSArray *currentExpected = expected;
  165. NSArray *currentActual = actual;
  166. FPath *basePath = basePathStr != nil ? [[FPath alloc] initWith:basePathStr] : [FPath empty];
  167. while ([currentExpected count] > 0) {
  168. // Step 1: find location range in expected
  169. // we expect all events for a particular path to be in a group
  170. FPath *currentPath = [basePath childFromString:currentExpected[0][@"path"]];
  171. NSUInteger i = 1;
  172. while (i < [currentExpected count]) {
  173. FPath *otherPath = [basePath childFromString:currentExpected[i][@"path"]];
  174. if ([currentPath isEqual:otherPath]) {
  175. i++;
  176. } else {
  177. break;
  178. }
  179. }
  180. // Step 2: foreach in actual, asserting location
  181. NSUInteger j = 0;
  182. for (j = 0; j < i; j++) {
  183. FDataEvent *actualEventData = currentActual[j];
  184. FTestEventRegistration *eventRegistration = actualEventData.eventRegistration;
  185. NSDictionary *specStep = eventRegistration.spec;
  186. FPath *actualPath = [basePath childFromString:specStep[@"path"]];
  187. if (![currentPath isEqual:actualPath]) {
  188. XCTFail(@"Expected path %@ to equal %@", actualPath, currentPath);
  189. }
  190. }
  191. // Step 3: slice each array
  192. NSMutableArray *expectedSlice = [[currentExpected subarrayWithRange:NSMakeRange(0, i)] mutableCopy];
  193. NSArray *actualSlice = [currentActual subarrayWithRange:NSMakeRange(0, i)];
  194. // foreach in actual, stack up to enforce ordering, find in expected
  195. NSMutableDictionary *actualMap = [[NSMutableDictionary alloc] init];
  196. for (FDataEvent *actualEvent in actualSlice) {
  197. FTestEventRegistration *eventRegistration = actualEvent.eventRegistration;
  198. FQuerySpec *query = eventRegistration.query;
  199. NSDictionary *spec = eventRegistration.spec;
  200. NSString *listenId = [NSString stringWithFormat:@"%@|%@", [basePath childFromString:spec[@"path"]], query];
  201. if (actualMap[listenId]) {
  202. // stack this event up, and make sure it obeys ordering constraints
  203. NSMutableArray *eventStack = actualMap[listenId];
  204. FDataEvent *prevEvent = eventStack[[eventStack count] - 1];
  205. [self assertOrderedFirstEvent:prevEvent.eventType secondEvent:actualEvent.eventType];
  206. [eventStack addObject:actualEvent];
  207. } else {
  208. // this is the first event for this listen, just initialize it
  209. actualMap[listenId] = [[NSMutableArray alloc] initWithObjects:actualEvent, nil];
  210. }
  211. // Ordering has been enforced, make sure we can find this in the expected events
  212. __block NSUInteger indexToRemove = NSNotFound;
  213. [expectedSlice enumerateObjectsUsingBlock:^(NSDictionary *expectedEvent, NSUInteger idx, BOOL *stop) {
  214. if ([self stringToEventType:expectedEvent[@"type"]] == actualEvent.eventType) {
  215. if ([self stringToEventType:expectedEvent[@"type"]] != FIRDataEventTypeValue) {
  216. if (![expectedEvent[@"name"] isEqualToString:actualEvent.snapshot.key]) {
  217. return; // short circuit, not a match
  218. }
  219. if ([self stringToEventType:expectedEvent[@"type"]] != FIRDataEventTypeChildRemoved &&
  220. !(expectedEvent[@"prevName"] == [NSNull null] && actualEvent.prevName == nil) &&
  221. !(expectedEvent[@"prevName"] != [NSNull null] && [expectedEvent[@"prevName"] isEqualToString:actualEvent.prevName])) {
  222. return; // short circuit, not a match
  223. }
  224. }
  225. // make sure the snapshots match
  226. NSString *snapHash = [actualEvent.snapshot.node.node dataHash];
  227. NSString *expectedHash = [[FSnapshotUtilities nodeFrom:expectedEvent[@"data"]] dataHash];
  228. if ([snapHash isEqualToString:expectedHash]) {
  229. indexToRemove = idx;
  230. *stop = YES;
  231. }
  232. }
  233. }];
  234. XCTAssertFalse(indexToRemove == NSNotFound, @"Could not find matching expected event for %@", actualEvent);
  235. [expectedSlice removeObjectAtIndex:indexToRemove];
  236. }
  237. currentExpected = [currentExpected subarrayWithRange:NSMakeRange(i, [currentExpected count] - i)];
  238. currentActual = [currentActual subarrayWithRange:NSMakeRange(i, [currentActual count] - i)];
  239. }
  240. }
  241. - (FQuerySpec *)parseParams:(NSDictionary *)specParams forPath:(FPath *)path {
  242. FQueryParams *query = [[FQueryParams alloc] init];
  243. NSMutableDictionary *params;
  244. if (specParams) {
  245. params = [specParams mutableCopy];
  246. if (!params[@"tag"]) {
  247. XCTFail(@"Error: Non-default queries must have tag");
  248. }
  249. } else {
  250. params = [NSMutableDictionary dictionary];
  251. }
  252. if (params[@"orderBy"]) {
  253. FPath *indexPath = [FPath pathWithString:params[@"orderBy"]];
  254. id<FIndex> index = [[FPathIndex alloc] initWithPath:indexPath];
  255. query = [query orderBy:index];
  256. [params removeObjectForKey:@"orderBy"];
  257. }
  258. if (params[@"orderByKey"]) {
  259. query = [query orderBy:[FKeyIndex keyIndex]];
  260. [params removeObjectForKey:@"orderByKey"];
  261. }
  262. if (params[@"orderByPriority"]) {
  263. query = [query orderBy:[FPriorityIndex priorityIndex]];
  264. [params removeObjectForKey:@"orderByPriority"];
  265. }
  266. if (params[@"startAt"]) {
  267. id<FNode> node = [FSnapshotUtilities nodeFrom:params[@"startAt"][@"index"]];
  268. if (params[@"startAt"][@"name"]) {
  269. query = [query startAt:node childKey:params[@"startAt"][@"name"]];
  270. } else {
  271. query = [query startAt:node];
  272. }
  273. [params removeObjectForKey:@"startAt"];
  274. }
  275. if (params[@"endAt"]) {
  276. id<FNode> node = [FSnapshotUtilities nodeFrom:params[@"endAt"][@"index"]];
  277. if (params[@"endAt"][@"name"]) {
  278. query = [query endAt:node childKey:params[@"endAt"][@"name"]];
  279. } else {
  280. query = [query endAt:node];
  281. }
  282. [params removeObjectForKey:@"endAt"];
  283. }
  284. if (params[@"equalTo"]) {
  285. id<FNode> node = [FSnapshotUtilities nodeFrom:params[@"equalTo"][@"index"]];
  286. if (params[@"equalTo"][@"name"]) {
  287. NSString *name = params[@"equalTo"][@"name"];
  288. query = [[query startAt:node childKey:name] endAt:node childKey:name];
  289. } else {
  290. query = [[query startAt:node] endAt:node];
  291. }
  292. [params removeObjectForKey:@"equalTo"];
  293. }
  294. if (params[@"limitToFirst"]) {
  295. query = [query limitToFirst:[params[@"limitToFirst"] integerValue]];
  296. [params removeObjectForKey:@"limitToFirst"];
  297. }
  298. if (params[@"limitToLast"]) {
  299. query = [query limitToLast:[params[@"limitToLast"] integerValue]];
  300. [params removeObjectForKey:@"limitToLast"];
  301. }
  302. [params removeObjectForKey:@"tag"];
  303. if ([params count] > 0) {
  304. XCTFail(@"Unsupported query parameter: %@", params);
  305. }
  306. return [[FQuerySpec alloc] initWithPath:path params:query];
  307. }
  308. - (void) runTest:(NSDictionary *)testSpec atBasePath:(NSString *)basePath {
  309. NSMutableDictionary *listens = [[NSMutableDictionary alloc] init];
  310. __weak FSyncPointTests *weakSelf = self;
  311. FListenProvider *listenProvider = [[FListenProvider alloc] init];
  312. listenProvider.startListening = ^(FQuerySpec *query, NSNumber *tagId, id<FSyncTreeHash> hash, fbt_nsarray_nsstring onComplete) {
  313. FQueryParams *queryParams = query.params;
  314. FPath *path = query.path;
  315. NSString *logTag = [NSString stringWithFormat:@"%@ (%@)", queryParams, tagId];
  316. NSString *key = [weakSelf queryKeyForQuery:query tagId:tagId];
  317. FFLog(@"I-RDB143001", @"Listening at %@ for %@", path, logTag);
  318. id existing = listens[key];
  319. NSAssert(existing == nil, @"Duplicate listen");
  320. listens[key] = @YES;
  321. return @[];
  322. };
  323. listenProvider.stopListening = ^(FQuerySpec *query, NSNumber *tagId) {
  324. FQueryParams *queryParams = query.params;
  325. FPath *path = query.path;
  326. NSString *logTag = [NSString stringWithFormat:@"%@ (%@)", queryParams, tagId];
  327. NSString *key = [weakSelf queryKeyForQuery:query tagId:tagId];
  328. FFLog(@"I-RDB143002", @"Stop listening at %@ for %@", path, logTag);
  329. id existing = listens[key];
  330. XCTAssertTrue(existing != nil, @"Missing record of query that we're removing");
  331. [listens removeObjectForKey:key];
  332. };
  333. FSyncTree *syncTree = [[FSyncTree alloc] initWithListenProvider:listenProvider];
  334. NSLog(@"Running %@", testSpec[@"name"]);
  335. NSInteger currentWriteId = 0;
  336. for (NSDictionary *step in testSpec[@"steps"]) {
  337. NSMutableDictionary *spec = [step mutableCopy];
  338. if (spec[@".comment"]) {
  339. NSLog(@" > %@", spec[@".comment"]);
  340. }
  341. if (spec[@"debug"] != nil) {
  342. // TODO: Ideally we'd pause the debugger somehow (like "debugger;" in JS).
  343. NSLog(@"Start debugging");
  344. }
  345. // Almost everything has a path...
  346. FPath *path = [FPath empty];
  347. if (basePath != nil) {
  348. path = [path childFromString:basePath];
  349. }
  350. if (spec[@"path"] != nil) {
  351. path = [path childFromString:spec[@"path"]];
  352. }
  353. NSArray *events;
  354. if ([spec[@"type"] isEqualToString:@"listen"]) {
  355. FQuerySpec *query = [self parseParams:spec[@"params"] forPath:path];
  356. FTestEventRegistration *eventRegistration = [[FTestEventRegistration alloc] initWithSpec:spec query:query];
  357. events = [syncTree addEventRegistration:eventRegistration forQuery:query];
  358. [self actualEvents:events exactMatchesExpected:spec[@"events"]];
  359. } else if ([spec[@"type"] isEqualToString:@"unlisten"]) {
  360. FQuerySpec *query = [self parseParams:spec[@"params"] forPath:path];
  361. FTestEventRegistration *eventRegistration = [[FTestEventRegistration alloc] initWithSpec:spec query:query];
  362. events = [syncTree removeEventRegistration:eventRegistration forQuery:query cancelError:nil];
  363. [self actualEvents:events exactMatchesExpected:spec[@"events"]];
  364. } else if ([spec[@"type"] isEqualToString:@"serverUpdate"]) {
  365. id<FNode> update = [FSnapshotUtilities nodeFrom:spec[@"data"]];
  366. if (spec[@"tag"]) {
  367. events = [syncTree applyTaggedQueryOverwriteAtPath:path newData:update tagId:spec[@"tag"]];
  368. } else {
  369. events = [syncTree applyServerOverwriteAtPath:path newData:update];
  370. }
  371. [self actualEventSet:events matchesExpected:spec[@"events"] atBasePath:basePath];
  372. } else if ([spec[@"type"] isEqualToString:@"serverMerge"]) {
  373. FCompoundWrite *compoundWrite = [FCompoundWrite compoundWriteWithValueDictionary:spec[@"data"]];
  374. if (spec[@"tag"]) {
  375. events = [syncTree applyTaggedQueryMergeAtPath:path changedChildren:compoundWrite tagId:spec[@"tag"]];
  376. } else {
  377. events = [syncTree applyServerMergeAtPath:path changedChildren:compoundWrite];
  378. }
  379. [self actualEventSet:events matchesExpected:spec[@"events"] atBasePath:basePath];
  380. } else if ([spec[@"type"] isEqualToString:@"set"]) {
  381. id<FNode> toSet = [FSnapshotUtilities nodeFrom:spec[@"data"]];
  382. BOOL visible = (spec[@"visible"] != nil) ? [spec[@"visible"] boolValue] : YES;
  383. events = [syncTree applyUserOverwriteAtPath:path newData:toSet writeId:currentWriteId++ isVisible:visible];
  384. [self actualEventSet:events matchesExpected:spec[@"events"] atBasePath:basePath];
  385. } else if ([spec[@"type"] isEqualToString:@"update"]) {
  386. FCompoundWrite *compoundWrite = [FCompoundWrite compoundWriteWithValueDictionary:spec[@"data"]];
  387. events = [syncTree applyUserMergeAtPath:path changedChildren:compoundWrite writeId:currentWriteId++];
  388. [self actualEventSet:events matchesExpected:spec[@"events"] atBasePath:basePath];
  389. } else if ([spec[@"type"] isEqualToString:@"ackUserWrite"]) {
  390. NSInteger writeId = [spec[@"writeId"] integerValue];
  391. BOOL revert = [spec[@"revert"] boolValue];
  392. events = [syncTree ackUserWriteWithWriteId:writeId revert:revert persist:YES clock:[[FTestClock alloc] init]];
  393. [self actualEventSet:events matchesExpected:spec[@"events"] atBasePath:basePath];
  394. } else if ([spec[@"type"] isEqualToString:@"suppressWarning"]) {
  395. // Do nothing. This is a hack so JS's Jasmine tests don't throw warnings for "expect no errors" tests.
  396. } else {
  397. XCTFail(@"Unknown step: %@", spec[@"type"]);
  398. }
  399. }
  400. }
  401. - (NSArray *) loadSpecs {
  402. static NSArray *json;
  403. if (json == nil) {
  404. NSString *syncPointSpec = [[NSBundle bundleForClass:[FSyncPointTests class]] pathForResource:@"syncPointSpec" ofType:@"json"];
  405. NSLog(@"%@", syncPointSpec);
  406. NSData *specData = [NSData dataWithContentsOfFile:syncPointSpec];
  407. NSError *error = nil;
  408. json = [NSJSONSerialization JSONObjectWithData:specData options:kNilOptions error:&error];
  409. if (error) {
  410. XCTFail(@"Error occurred parsing JSON: %@", error);
  411. }
  412. }
  413. return json;
  414. }
  415. - (NSDictionary *) specsForName:(NSString *)name {
  416. for (NSDictionary *spec in [self loadSpecs]) {
  417. if ([name isEqualToString:spec[@"name"]]) {
  418. return spec;
  419. }
  420. }
  421. XCTFail(@"No such test: %@", name);
  422. return nil;
  423. }
  424. - (void) runTestForName:(NSString *)name {
  425. NSDictionary *spec = [self specsForName:name];
  426. [self runTest:spec atBasePath:nil];
  427. // run again at a deeper location
  428. [self runTest:spec atBasePath:@"/foo/bar/baz"];
  429. }
  430. - (void) testAll {
  431. NSArray *specs = [self loadSpecs];
  432. for (NSDictionary *spec in specs) {
  433. [self runTest:spec atBasePath:nil];
  434. // run again at a deeper location
  435. [self runTest:spec atBasePath:@"/foo/bar/baz"];
  436. }
  437. }
  438. - (void) testDefaultListenHandlesParentSet {
  439. [self runTestForName:@"Default listen handles a parent set"];
  440. }
  441. - (void) testDefaultListenHandlesASetAtTheSameLevel {
  442. [self runTestForName:@"Default listen handles a set at the same level"];
  443. }
  444. - (void) testAQueryCanGetACompleteCacheThenAMerge {
  445. [self runTestForName:@"A query can get a complete cache then a merge"];
  446. }
  447. - (void) testServerMergeOnListenerWithCompleteChildren {
  448. [self runTestForName:@"Server merge on listener with complete children"];
  449. }
  450. - (void) testDeepMergeOnListenerWithCompleteChildren {
  451. [self runTestForName:@"Deep merge on listener with complete children"];
  452. }
  453. - (void) testUpdateChildListenerTwice {
  454. [self runTestForName:@"Update child listener twice"];
  455. }
  456. - (void) testChildOfDefaultListenThatAlreadyHasACompleteCache {
  457. [self runTestForName:@"Update child of default listen that already has a complete cache"];
  458. }
  459. - (void) testUpdateChildOfDefaultListenThatHasNoCache {
  460. [self runTestForName:@"Update child of default listen that has no cache"];
  461. }
  462. // failing
  463. - (void) testUpdateTheChildOfACoLocatedDefaultListenerAndQuery {
  464. [self runTestForName:@"Update (via set) the child of a co-located default listener and query"];
  465. }
  466. - (void) testUpdateTheChildOfAQueryWithAFullCache {
  467. [self runTestForName:@"Update (via set) the child of a query with a full cache"];
  468. }
  469. - (void) testUpdateAChildBelowAnEmptyQuery {
  470. [self runTestForName:@"Update (via set) a child below an empty query"];
  471. }
  472. - (void) testUpdateDescendantOfDefaultListenerWithFullCache {
  473. [self runTestForName:@"Update descendant of default listener with full cache"];
  474. }
  475. - (void) testDescendantSetBelowAnEmptyDefaultLIstenerIsIgnored {
  476. [self runTestForName:@"Descendant set below an empty default listener is ignored"];
  477. }
  478. - (void) testUpdateOfAChild {
  479. [self runTestForName:@"Update of a child. This can happen if a child listener is added and removed"];
  480. }
  481. - (void) testRevertSetWithOnlyChildCaches {
  482. [self runTestForName:@"Revert set with only child caches"];
  483. }
  484. - (void) testCanRevertADuplicateChildSet {
  485. [self runTestForName:@"Can revert a duplicate child set"];
  486. }
  487. - (void) testCanRevertAChildSetAndSeeTheUnderlyingData {
  488. [self runTestForName:@"Can revert a child set and see the underlying data"];
  489. }
  490. - (void) testRevertChildSetWithNoServerData {
  491. [self runTestForName:@"Revert child set with no server data"];
  492. }
  493. - (void) testRevertDeepSetWithNoServerData {
  494. [self runTestForName:@"Revert deep set with no server data"];
  495. }
  496. - (void) testRevertSetCoveredByNonvisibleTransaction {
  497. [self runTestForName:@"Revert set covered by non-visible transaction"];
  498. }
  499. - (void) testClearParentShadowingServerValuesSetWithServerChildren {
  500. [self runTestForName:@"Clear parent shadowing server values set with server children"];
  501. }
  502. - (void) testClearChildShadowingServerValuesSetWithServerChildren {
  503. [self runTestForName:@"Clear child shadowing server values set with server children"];
  504. }
  505. - (void) testUnrelatedMergeDoesntShadowServerUpdates {
  506. [self runTestForName:@"Unrelated merge doesn't shadow server updates"];
  507. }
  508. - (void) testCanSetAlongsideARemoteMerge {
  509. [self runTestForName:@"Can set alongside a remote merge"];
  510. }
  511. - (void) testSetPriorityOnALocationWithNoCache {
  512. [self runTestForName:@"setPriority on a location with no cache"];
  513. }
  514. - (void) testDeepUpdateDeletesChildFromLimitWindowAndPullsInNewChild {
  515. [self runTestForName:@"deep update deletes child from limit window and pulls in new child"];
  516. }
  517. - (void) testDeepSetDeletesChildFromLimitWindowAndPullsInNewChild {
  518. [self runTestForName:@"deep set deletes child from limit window and pulls in new child"];
  519. }
  520. - (void) testEdgeCaseInNewChildForChange {
  521. [self runTestForName:@"Edge case in newChildForChange_"];
  522. }
  523. - (void) testRevertSetInQueryWindow {
  524. [self runTestForName:@"Revert set in query window"];
  525. }
  526. - (void) testHandlesAServerValueMovingAChildOutOfAQueryWindow {
  527. [self runTestForName:@"Handles a server value moving a child out of a query window"];
  528. }
  529. - (void) testUpdateOfIndexedChildWorks {
  530. [self runTestForName:@"Update of indexed child works"];
  531. }
  532. - (void) testMergeAppliedToEmptyLimit {
  533. [self runTestForName:@"Merge applied to empty limit"];
  534. }
  535. - (void) testLimitIsRefilledFromServerDataAfterMerge {
  536. [self runTestForName:@"Limit is refilled from server data after merge"];
  537. }
  538. - (void) testHandleRepeatedListenWithMergeAsFirstUpdate {
  539. [self runTestForName:@"Handle repeated listen with merge as first update"];
  540. }
  541. - (void) testLimitIsRefilledFromServerDataAfterSet {
  542. [self runTestForName:@"Limit is refilled from server data after set"];
  543. }
  544. - (void) testQueryOnWeirdPath {
  545. [self runTestForName:@"query on weird path."];
  546. }
  547. - (void) testRunsRound2 {
  548. [self runTestForName:@"runs, round2"];
  549. }
  550. - (void) testHandlesNestedListens {
  551. [self runTestForName:@"handles nested listens"];
  552. }
  553. - (void) testHandlesASetBelowAListen {
  554. [self runTestForName:@"Handles a set below a listen"];
  555. }
  556. - (void) testDoesNonDefaultQueries {
  557. [self runTestForName:@"does non-default queries"];
  558. }
  559. - (void) testHandlesCoLocatedDefaultListenerAndQuery {
  560. [self runTestForName:@"handles a co-located default listener and query"];
  561. }
  562. - (void) testDefaultAndNonDefaultListenerAtSameLocationWithServerUpdate {
  563. [self runTestForName:@"Default and non-default listener at same location with server update"];
  564. }
  565. - (void) testAddAParentListenerToACompleteChildListenerExpectChildEvent {
  566. [self runTestForName:@"Add a parent listener to a complete child listener, expect child event"];
  567. }
  568. - (void) testAddListensToASetExpectCorrectEventsIncludingAChildEvent {
  569. [self runTestForName:@"Add listens to a set, expect correct events, including a child event"];
  570. }
  571. - (void) testServerUpdateToAChildListenerRaisesChildEventsAtParent {
  572. [self runTestForName:@"ServerUpdate to a child listener raises child events at parent"];
  573. }
  574. - (void) testServerUpdateToAChildListenerRaisesChildEventsAtParentQuery {
  575. [self runTestForName:@"ServerUpdate to a child listener raises child events at parent query"];
  576. }
  577. - (void) testMultipleCompleteChildrenAreHandleProperly {
  578. [self runTestForName:@"Multiple complete children are handled properly"];
  579. }
  580. - (void) testWriteLeafNodeOverwriteAtParentNode {
  581. [self runTestForName:@"Write leaf node, overwrite at parent node"];
  582. }
  583. - (void) testConfirmCompleteChildrenFromTheServer {
  584. [self runTestForName:@"Confirm complete children from the server"];
  585. }
  586. - (void) testWriteLeafOverwriteFromParent {
  587. [self runTestForName:@"Write leaf, overwrite from parent"];
  588. }
  589. - (void) testBasicUpdateTest {
  590. [self runTestForName:@"Basic update test"];
  591. }
  592. - (void) testNoDoubleValueEventsForUserAck {
  593. [self runTestForName:@"No double value events for user ack"];
  594. }
  595. - (void) testBasicKeyIndexSanityCheck {
  596. [self runTestForName:@"Basic key index sanity check"];
  597. }
  598. - (void) testCollectCorrectSubviewsToListenOn {
  599. [self runTestForName:@"Collect correct subviews to listen on"];
  600. }
  601. - (void) testLimitToFirstOneOnOrderedQuery {
  602. [self runTestForName:@"Limit to first one on ordered query"];
  603. }
  604. - (void) testLimitToLastOneOnOrderedQuery {
  605. [self runTestForName:@"Limit to last one on ordered query"];
  606. }
  607. - (void) testUpdateIndexedValueOnExistingChildFromLimitedQuery {
  608. [self runTestForName:@"Update indexed value on existing child from limited query"];
  609. }
  610. - (void) testCanCreateStartAtEndAtEqualToQueriesWithBool {
  611. [self runTestForName:@"Can create startAt, endAt, equalTo queries with bool"];
  612. }
  613. - (void) testQueryWithExistingServerSnap {
  614. [self runTestForName:@"Query with existing server snap"];
  615. }
  616. - (void) testServerDataIsNotPurgedForNonServerIndexedQueries {
  617. [self runTestForName:@"Server data is not purged for non-server-indexed queries"];
  618. }
  619. - (void) testStartAtEndAtDominatesLimit {
  620. [self runTestForName:@"startAt/endAt dominates limit"];
  621. }
  622. - (void) testUpdateToSingleChildThatMovesOutOfWindow {
  623. [self runTestForName:@"Update to single child that moves out of window"];
  624. }
  625. - (void) testLimitedQueryDoesntPullInOutOfRangeChild {
  626. [self runTestForName:@"Limited query doesn't pull in out of range child"];
  627. }
  628. - (void) testWithCustomOrderByIsRefilledWithCorrectItem {
  629. [self runTestForName:@"Limit with custom orderBy is refilled with correct item"];
  630. }
  631. - (void) testMergeForLocationWithDefaultAndLimitedListener {
  632. [self runTestForName:@"Merge for location with default and limited listener"];
  633. }
  634. - (void) testUserMergePullsInCorrectValues {
  635. [self runTestForName:@"User merge pulls in correct values"];
  636. }
  637. - (void) testUserDeepSetPullsInCorrectValues {
  638. [self runTestForName:@"User deep set pulls in correct values"];
  639. }
  640. - (void) testQueriesWithEqualToNullWork {
  641. [self runTestForName:@"Queries with equalTo(null) work"];
  642. }
  643. - (void) testRevertedWritesUpdateQuery {
  644. [self runTestForName:@"Reverted writes update query"];
  645. }
  646. - (void) testDeepSetForNonLocalDataDoesntRaiseEvents {
  647. [self runTestForName:@"Deep set for non-local data doesn't raise events"];
  648. }
  649. - (void) testUserUpdateWithNewChildrenTriggersEvents {
  650. [self runTestForName:@"User update with new children triggers events"];
  651. }
  652. - (void) testUserWriteWithDeepOverwrite {
  653. [self runTestForName:@"User write with deep user overwrite"];
  654. }
  655. - (void) testServerUpdatesPriority {
  656. [self runTestForName:@"Server updates priority"];
  657. }
  658. - (void) testRevertFullUnderlyingWrite {
  659. [self runTestForName:@"Revert underlying full overwrite"];
  660. }
  661. - (void) testUserChildOverwriteForNonexistentServerNode {
  662. [self runTestForName:@"User child overwrite for non-existent server node"];
  663. }
  664. - (void) testRevertUserOverwriteOfChildOnLeafNode {
  665. [self runTestForName:@"Revert user overwrite of child on leaf node"];
  666. }
  667. - (void) testServerOverwriteWithDeepUserDelete {
  668. [self runTestForName:@"Server overwrite with deep user delete"];
  669. }
  670. - (void) testUserOverwritesLeafNodeWithPriority {
  671. [self runTestForName:@"User overwrites leaf node with priority"];
  672. }
  673. - (void) testUserOverwritesInheritPriorityValuesFromLeafNodes {
  674. [self runTestForName:@"User overwrites inherit priority values from leaf nodes"];
  675. }
  676. - (void) testUserUpdateOnUserSetLeafNodeWithPriorityAfterServerUpdate {
  677. [self runTestForName:@"User update on user set leaf node with priority after server update"];
  678. }
  679. - (void) testServerDeepDeleteOnLeafNode {
  680. [self runTestForName:@"Server deep delete on leaf node"];
  681. }
  682. - (void) testUserSetsRootPriority {
  683. [self runTestForName:@"User sets root priority"];
  684. }
  685. - (void) testUserUpdatesPriorityOnEmptyRoot {
  686. [self runTestForName:@"User updates priority on empty root"];
  687. }
  688. - (void) testRevertSetAtRootWithPriority {
  689. [self runTestForName:@"Revert set at root with priority"];
  690. }
  691. - (void) testServerUpdatesPriorityAfterUserSetsPriority {
  692. [self runTestForName:@"Server updates priority after user sets priority"];
  693. }
  694. - (void) testEmptySetDoesntPreventServerUpdates {
  695. [self runTestForName:@"Empty set doesn't prevent server updates"];
  696. }
  697. - (void) testUserUpdatesPriorityTwiceFirstIsReverted {
  698. [self runTestForName:@"User updates priority twice, first is reverted"];
  699. }
  700. - (void) testServerAcksRootPrioritySetAfterUserDeletesRootNode {
  701. [self runTestForName:@"Server acks root priority set after user deletes root node"];
  702. }
  703. - (void) testADeleteInAMergeDoesntPushOutNodes {
  704. [self runTestForName:@"A delete in a merge doesn't push out nodes"];
  705. }
  706. - (void) testATaggedQueryFiresEventsEventually {
  707. [self runTestForName:@"A tagged query fires events eventually"];
  708. }
  709. - (void) testUserWriteOutsideOfLimitIsIgnoredForTaggedQueries {
  710. [self runTestForName:@"User write outside of limit is ignored for tagged queries"];
  711. }
  712. - (void) testAckForMergeDoesntRaiseValueEventForLaterListen {
  713. [self runTestForName:@"Ack for merge doesn't raise value event for later listen"];
  714. }
  715. - (void) testClearParentShadowingServerValuesMergeWithServerChildren {
  716. [self runTestForName:@"Clear parent shadowing server values merge with server children"];
  717. }
  718. - (void) testPrioritiesDontMakeMeSick {
  719. [self runTestForName:@"Priorities don't make me sick"];
  720. }
  721. - (void) testMergeThatMovesChildFromWindowToBoundaryDoesNotCauseChildToBeReadded {
  722. [self runTestForName:@"Merge that moves child from window to boundary does not cause child to be readded"];
  723. }
  724. - (void) testDeepMergeAckIsHandledCorrectly {
  725. [self runTestForName:@"Deep merge ack is handled correctly."];
  726. }
  727. - (void) testDeepMergeAckOnIncompleteDataAndWithServerValues {
  728. [self runTestForName:@"Deep merge ack (on incomplete data, and with server values)"];
  729. }
  730. - (void) testLimitQueryHandlesDeepServerMergeForOutOfViewItem {
  731. [self runTestForName:@"Limit query handles deep server merge for out-of-view item."];
  732. }
  733. - (void) testLimitQueryHandlesDeepUserMergeForOutOfViewItem {
  734. [self runTestForName:@"Limit query handles deep user merge for out-of-view item."];
  735. }
  736. - (void) testLimitQueryHandlesDeepUserMergeForOutOfViewItemFollowedByServerUpdate {
  737. [self runTestForName:@"Limit query handles deep user merge for out-of-view item followed by server update."];
  738. }
  739. - (void) testUnrelatedUntaggedUpdateIsNotCachedInTaggedListen {
  740. [self runTestForName:@"Unrelated, untagged update is not cached in tagged listen"];
  741. }
  742. - (void) testUnrelatedAckedSetIsNotCachedInTaggedListen {
  743. [self runTestForName:@"Unrelated, acked set is not cached in tagged listen"];
  744. }
  745. - (void) testUnrelatedAckedUpdateIsNotCachedInTaggedListen {
  746. [self runTestForName:@"Unrelated, acked update is not cached in tagged listen"];
  747. }
  748. - (void) testdeepUpdateRaisesImmediateEventsOnlyIfHasCompleteData {
  749. [self runTestForName:@"Deep update raises immediate events only if has complete data"];
  750. }
  751. - (void) testdeepUpdateReturnsMinimumDataRequired {
  752. [self runTestForName:@"Deep update returns minimum data required"];
  753. }
  754. - (void) testdeepUpdateRaisesAllEvents {
  755. [self runTestForName:@"Deep update raises all events"];
  756. }
  757. @end