-
Notifications
You must be signed in to change notification settings - Fork 0
/
NEO4OCSpec.m
384 lines (327 loc) · 17.2 KB
/
NEO4OCSpec.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//
// NEO4OCSpec.m
//
//
// Created by Maxim Zaks on 04.06.12.
// Copyright 2012 Besitzer. All rights reserved.
//
#import <Kiwi/Kiwi.h>
#import <NEO4OC/NEO4OC.h>
SPEC_BEGIN(NEO4OCSpec)
#define START_WAIT __block int wait = 1
#define END_WAIT while(wait>0){}
describe(@"Neo4j is a graph database which can be reached through REST API", ^{
__block NEOGraphDatabase *graph;
context(@"Neo4j server is started on localhost:7474", ^{
it(@"create a representation of graph databse", ^{
graph = [[NEOGraphDatabase alloc]initWithURL:[NSURL URLWithString:@"http://localhost:7474"]];
[graph shouldNotBeNil];
});
it(@"get information about the database", ^{
__block NSDictionary *_info;
[graph getInfo:^(NSDictionary *info, NEOError *error) {
[error shouldBeNil];
_info = info;
NSLog(@"DB: %@", _info);
}];
[[expectFutureValue(_info) shouldEventually] beNonNil];
});
describe(@"A promise is a mechanism to provide data at later time and implement nonblocking behavior", ^{
__block NEOPromise *promise;
it(@"Promise has three states: 'WAITING', 'DONE', 'ERROR'", ^{
promise = [[NEOPromise alloc]init];
[[theValue(promise.status) should] equal:theValue(WAITING)];
[promise setValue:@"value"];
[[theValue(promise.status) should] equal:theValue(DONE)];
[promise setError:[[NEOError alloc]init]];
[[theValue(promise.status) should] equal:theValue(ERROR)];
});
it(@"Value may be set only once", ^{
promise = [[NEOPromise alloc]init];
[promise setValue:@"value"];
[[theBlock(^{ [promise setValue:@"value2"]; }) should] raise];
});
pending(@"A concreet promise blocks until value is not set. Afterwards it forwards method calls to the value",^{
});
pending(@"It is posible to register callback to perform when value get set", ^{
});
pending(@"It is posible to register callback to perform when value get set", ^{
});
pending(@"An error transfers error messages and ment to be for debuging purpose only", ^{
});
});
describe(@"A graph contains of Nodes and Edges (Relationships)", ^{
describe(@"Node is an entity which can carry flat data", ^{
__block id<NEONode> node;
it(@"create a node representation with data", ^{
NSDictionary *data = [NSDictionary dictionaryWithObject:@"mobile.cologne" forKey:@"name"];
node = [graph createNodeWithData:data];
[[[node nodeId] should] beNonNil];
[[[node data] should] equal:data];
});
it(@"query for node representation by id", ^{
NEONodePromise *nodePromise = [graph getNodeById:node.nodeId];
[nodePromise wait];
[[[nodePromise.data objectForKey:@"name"] should] equal:@"mobile.cologne"];
});
it(@"modify data in a node", ^{
NEONodePromise *nodePromise = [graph getNodeById:node.nodeId];
NSMutableDictionary *data = [NSMutableDictionary dictionaryWithDictionary:nodePromise.data];
[data setValue:@"http://www.mobilecologne.de/" forKey:@"url"];
START_WAIT;
[nodePromise setData:data withResultHandler:^(NEOError *error) {
[error shouldBeNil];
wait = NO;
}];
END_WAIT;
[[[nodePromise.data objectForKey:@"url"] should] equal:@"http://www.mobilecologne.de/"];
});
it(@"update stale data in a node representation", ^{
NSDictionary * _data = node.data;
[[_data objectForKey:@"url"] shouldBeNil];
START_WAIT;
[node fetchData:^(NSDictionary *data, NEOError *error){
[error shouldBeNil];
[data shouldNotBeNil];
wait = NO;
}];
END_WAIT;
[[[node.data objectForKey:@"url"] should] equal:@"http://www.mobilecologne.de/"];
});
it(@"delete node through node representation", ^{
START_WAIT;
[node deleteWithResultHandler:^(NEOError *error) {
[error shouldBeNil];
wait = NO;
}];
END_WAIT;
[node.nodeId shouldBeNil];
});
});
describe(@"Relationship is a typed entity which can connect one node with another and carry flat data", ^{
__block id<NEORelationship> rel;
__block id<NEONode> mCologne;
__block id<NEONode> maxim;
__block id<NEONode> lars;
beforeAll(^{
NSDictionary *nodeData1 = [NSDictionary dictionaryWithObject:@"name" forKey:@"mobile.cologne"];
mCologne = [graph createNodeWithData:nodeData1];
NSDictionary *nodeData2 = [NSDictionary dictionaryWithObject:@"name" forKey:@"Maxim"];
maxim = [graph createNodeWithData:nodeData2];
NSDictionary *nodeData3 = [NSDictionary dictionaryWithObject:@"name" forKey:@"Lars"];
lars = [graph createNodeWithData:nodeData3];
});
afterAll(^{
[mCologne deleteWithResultHandler:^(NEOError *error) {[error shouldBeNil];}];
[maxim deleteWithResultHandler:^(NEOError *error) {[error shouldBeNil];}];
});
it(@"create a relationship with data", ^{
NSDictionary *speaksAtData = [NSDictionary dictionaryWithObject:@"14.06.2012" forKey:@"date"];
rel = [maxim createRelationshipOfType:@"SPEAKS_AT" toNode:mCologne andData:speaksAtData];
[rel.relationshipId shouldNotBeNil];
});
it(@"query for relationship by id", ^{
NEORelationshipPromise *relPromise = [graph getRelationshipById:rel.relationshipId];
[relPromise wait];
[[relPromise.data should]equal:rel.data];
});
it(@"get start and end node", ^{
NEONodePromise *startNodePromise = [rel startNode];
NEONodePromise *endNodePromise = [rel endNode];
[NEOPromise waitForPromises:startNodePromise, endNodePromise, nil];
[[startNodePromise.data should]equal:maxim.data];
[[endNodePromise.data should]equal:mCologne.data];
});
it(@"query for all relationships of a node", ^{
NEORelationshipPromise *relPromise1 = [maxim createRelationshipOfType:@"KNOWS" toNode:lars andData:nil];
[relPromise1 wait];
NEORelationshipPromise *relPromise2 = [lars createRelationshipOfType:@"INVITED" toNode:maxim andData:nil];
[relPromise2 wait];
// Doesn't work because of transaction problem
// [NEOPromise waitForPromises:relPromise1, relPromise2, nil];
START_WAIT;
[maxim getAllRelationshipsOfTypes:nil withResultHandler:^(NSArray *relationships, NEOError *error) {
[error shouldBeNil];
[[relationships should]haveCountOf:3];
wait--;
}];
END_WAIT;
});
it(@"query for outgoing relationships of a node", ^{
START_WAIT;
[maxim getOutgoingRelationshipsOfTypes:nil withResultHandler:^(NSArray *relationships, NEOError *error) {
[error shouldBeNil];
[[relationships should]haveCountOf:2];
wait--;
}];
END_WAIT;
});
it(@"query for incoming relationships of a node", ^{
START_WAIT;
[maxim getIncomingRelationshipsOfTypes:nil withResultHandler:^(NSArray *relationships, NEOError *error) {
[error shouldBeNil];
[[relationships should]haveCountOf:1];
wait--;
}];
END_WAIT;
});
it(@"modify data in a relationship", ^{
NEORelationshipPromise *relPromise = [graph getRelationshipById:rel.relationshipId];
START_WAIT;
[relPromise setData:[NSDictionary dictionary] withResultHandler:^(NEOError *error) {
[error shouldBeNil];
wait--;
}];
END_WAIT;
NSDictionary *date = [relPromise.data objectForKey:@"date"];
[date shouldBeNil];
});
it(@"update stale data in a node representation", ^{
NSDictionary *data = rel.data;
[[[data objectForKey:@"date"] should]equal:@"14.06.2012"];
START_WAIT;
[rel fetchData:^(NSDictionary *data, NEOError *error) {
[error shouldBeNil];
wait--;
}];
END_WAIT;
NSDictionary *date1 = [rel.data objectForKey:@"date"];
[date1 shouldBeNil];
});
it(@"delete relationships through node representation", ^{
START_WAIT;
[maxim getAllRelationshipsOfTypes:nil withResultHandler:^(NSArray *relationships, NEOError *error) {
[error shouldBeNil];
wait +=relationships.count;
for(id<NEORelationship> _rel in relationships){
__block BOOL innerWait = true;
[_rel deleteWithResultHandler:^(NEOError *error) {
[error shouldBeNil];
innerWait = NO;
wait--;
}];
while(innerWait){}
}
wait--;
}];
END_WAIT;
});
});
});
describe(@"Cypher is a graph query language", ^{
__block NSNumber *larsId;
__block NSNumber *dariaId;
__block NEONodePromise *mobileCologne;
__block NEONodePromise *maxim;
__block NEONodePromise *lars;
__block NEONodePromise *daria;
beforeAll(^{
mobileCologne = [graph createNodeWithData:[NSDictionary dictionaryWithObject:@"mobile.cologne" forKey:@"name"]];
maxim = [graph createNodeWithData:[NSDictionary dictionaryWithObject:@"Maxim Z." forKey:@"name"]];
lars = [graph createNodeWithData:[NSDictionary dictionaryWithObject:@"Lars P." forKey:@"name"]];
daria = [graph createNodeWithData:[NSDictionary dictionaryWithObject:@"Daria S." forKey:@"name"]];
[[maxim createRelationshipOfType:@"SPEAK_AT" toNode:mobileCologne andData:nil] wait];
[[lars createRelationshipOfType:@"ORGANIZE" toNode:mobileCologne andData:nil] wait];
[[lars createRelationshipOfType:@"KNOWS" toNode:maxim andData:nil] wait];
[[maxim createRelationshipOfType:@"FRIEND" toNode:daria andData:nil] wait];
larsId = [NSNumber numberWithInt:lars.nodeId.intValue];
dariaId = [NSNumber numberWithInt:daria.nodeId.intValue];
});
it(@"should find node", ^{
NSString *query =
@"start n = node(*)"
"where n.name != 'Maxim Z.'"
"return n as node";
START_WAIT;
[graph queryCypher:query withParameters:nil andTypedResultHandler:^(NSArray *result, NEOError *error) {
[error shouldBeNil];
[[result should] haveCountOfAtLeast:1];
NSDictionary *result1 = [result objectAtIndex:0];
id<NEONode> node = [result1 objectForKey:@"node"];
[[[node.data objectForKey:@"name"] should] equal:@"Maxim Z."];
wait--;
}];
END_WAIT;
});
it(@"should find node wich relates to another", ^{
NSString *query =
@"start n = node(*) "
"match n-[:SPEAK_AT]->mc "
"return n as node, mc as mobileCologne";
START_WAIT;
[graph queryCypher:query withParameters:nil andTypedResultHandler:^(NSArray *result, NEOError *error) {
[error shouldBeNil];
[[result should] haveCountOfAtLeast:1];
NSDictionary *result1 = [result objectAtIndex:0];
id<NEONode> node1 = [result1 objectForKey:@"node"];
[[[node1.data objectForKey:@"name"] should] equal:@"Maxim Z."];
id<NEONode> node2 = [result1 objectForKey:@"mobileCologne"];
[[[node2.data objectForKey:@"name"] should] equal:@"mobile.cologne"];
wait--;
}];
END_WAIT;
});
it(@"should find path between two nodes", ^{
NSString *query = @"start l = node({larsId}), d = node({dariaId}) match path = l -[?*]- d return path";
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:dariaId, @"dariaId", larsId, @"larsId", nil];
NSLog(@"%@", [params description]);
START_WAIT;
[graph queryCypher:query withParameters:params andTypedResultHandler:^(NSArray *result, NEOError *error) {
[error shouldBeNil];
[[result should] haveCountOf:2];
NSLog(@"Paths from Lars to Daria %@", result);
wait--;
}];
END_WAIT;
});
it(@"should find shortest path between two nodes", ^{
NSString *query = @"start l = node({larsId}), d = node({dariaId}) match path = shortestPath(l -[?*]- d) return path";
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:dariaId, @"dariaId", larsId, @"larsId", nil];
START_WAIT;
[graph queryCypher:query withParameters:params andTypedResultHandler:^(NSArray *result, NEOError *error) {
[error shouldBeNil];
[[result should] haveCountOf:1];
NSLog(@"Shortest paths from Lars to Daria %@", result);
wait--;
}];
END_WAIT;
});
it(@"shoudl clean up", ^{
START_WAIT;
[maxim orphanNodeAndDeleteWithResultHandler:^(NEOError *error) {
[error shouldBeNil];
wait--;
}];
END_WAIT;
wait++;
[lars orphanNodeAndDeleteWithResultHandler:^(NEOError *error) {
[error shouldBeNil];
wait--;
}];
END_WAIT;
wait++;
[daria orphanNodeAndDeleteWithResultHandler:^(NEOError *error) {
[error shouldBeNil];
wait--;
}];
END_WAIT;
wait++;
[mobileCologne orphanNodeAndDeleteWithResultHandler:^(NEOError *error) {
[error shouldBeNil];
wait--;
}];
END_WAIT;
});
});
/*
// TODO:
describe(@"Index helps to organize and find nodes/relationships", ^{
});
describe(@"Traversals implement different algorithms for graph visiting", ^{
});
describe(@"Batch executes a list of operations in single request", ^{
});
*/
});
});
SPEC_END