FSyncPointTests.m 34 KB

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