blob: 32a018f0370664d011ed5f5d6d77f4f5d43ea70a [file] [log] [blame]
Nils Diewald0b6c0412014-12-19 03:55:57 +00001// Helper method for building factories
2buildFactory = function (objClass, defaults) {
3 return {
4 create : function (overwrites) {
5 var newObj = {};
6 for (var prop in defaults) {
7 newObj[prop] = defaults[prop];
8 };
9 for (var prop in overwrites) {
10 newObj[prop] = overwrites[prop];
11 };
Nils Diewaldf219eb82015-01-07 20:15:42 +000012 if (objClass === KorAP.VirtualCollection)
13 return objClass.render(newObj);
14 else
15 return objClass.create().fromJson(newObj);
Nils Diewald0b6c0412014-12-19 03:55:57 +000016 }
17 }
18};
19
20describe('KorAP.Doc', function () {
21
22 // Create example factories
23 var stringFactory = buildFactory(KorAP.Doc, {
24 "key" : "author",
25 "value" : "Max Birkendale",
26 "@type" : "korap:doc"
27 });
28
29 // Create example factories
30 var dateFactory = buildFactory(KorAP.Doc, {
31 "key" : "pubDate",
32 "type" : "type:date",
33 "match" : "match:eq",
34 "value" : "2014-11-05",
35 "@type" : "korap:doc"
36 });
37
38 // Create example factories
39 var regexFactory = buildFactory(KorAP.Doc, {
40 "key" : "title",
41 "type" : "type:regex",
42 "value" : "[^b]ee.+?",
43 "@type" : "korap:doc"
44 });
45
46 it('should be initializable', function () {
47 var doc = KorAP.Doc.create();
48 expect(doc.matchop()).toEqual('eq');
49 expect(doc.key()).toBeUndefined();
50 expect(doc.value()).toBeUndefined();
51 expect(doc.type()).toEqual("string");
52 });
53
54 it('should be definable', function () {
55
56 // Empty doc
57 var doc = KorAP.Doc.create();
58
59 // Set values
60 doc.key("title");
61 doc.value("Der alte Mann");
62 expect(doc.matchop()).toEqual('eq');
63 expect(doc.key()).toEqual("title");
64 expect(doc.type()).toEqual("string");
65 expect(doc.value()).toEqual("Der alte Mann");
66 });
67
68
69 it('should deserialize JSON-LD string', function () {
70 var doc;
71
72 // String default
73 doc = stringFactory.create();
74 expect(doc.matchop()).toEqual('eq');
75 expect(doc.key()).toEqual("author");
76 expect(doc.type()).toEqual("string");
77 expect(doc.value()).toEqual("Max Birkendale");
78
79 // No valid string
80 doc = stringFactory.create({
81 value : undefined
82 });
83 expect(doc).toBeUndefined();
84
85 // No valid string
86 doc = stringFactory.create({
87 value : { "foo" : "bar" }
88 });
89 expect(doc).toBeUndefined();
90
91 // Change match type
92 doc = stringFactory.create({
93 "match" : "match:ne"
94 });
95
96 expect(doc.matchop()).toEqual('ne');
97 expect(doc.key()).toEqual("author");
98 expect(doc.type()).toEqual("string");
99 expect(doc.value()).toEqual("Max Birkendale");
100
101
102 // Invalid match type
103 doc = stringFactory.create({
104 "match" : { "foo" : "bar" }
105 });
106 expect(doc).toBeUndefined();
107 });
108
109 it('should deserialize JSON-LD regex', function () {
110 var doc = regexFactory.create();
111 expect(doc.key()).toEqual("title");
112 expect(doc.type()).toEqual("regex");
113 expect(doc.value()).toEqual("[^b]ee.+?");
114 expect(doc.matchop()).toEqual('eq');
115
116 // change matcher
117 doc = regexFactory.create({
118 match : "match:ne"
119 });
120 expect(doc.matchop()).toEqual('ne');
121
122 // Invalid matcher
123 doc = regexFactory.create({
124 match : "match:chook"
125 });
126 expect(doc).toBeUndefined();
127
128 // Invalid regex
129 doc = regexFactory.create({
130 value : "[^b"
131 });
132 expect(doc).toBeUndefined();
133 });
134
135 it('should deserialize JSON-LD date', function () {
136
137 // Normal date
138 doc = dateFactory.create({});
139
140 expect(doc.matchop()).toEqual('eq');
141 expect(doc.key()).toEqual("pubDate");
142 expect(doc.type()).toEqual("date");
143 expect(doc.value()).toEqual("2014-11-05");
144
145 // Short date 1
146 doc = dateFactory.create({
147 "value" : "2014-11"
148 });
149
150 expect(doc.matchop()).toEqual('eq');
151 expect(doc.key()).toEqual("pubDate");
152 expect(doc.type()).toEqual("date");
153 expect(doc.value()).toEqual("2014-11");
154
155 // Short date 2
156 doc = dateFactory.create({
157 "value" : "2014"
158 });
159
160 expect(doc.matchop()).toEqual('eq');
161 expect(doc.key()).toEqual("pubDate");
162 expect(doc.type()).toEqual("date");
163 expect(doc.value()).toEqual("2014");
164
165 // Invalid date!
166 doc = dateFactory.create({
167 "value" : "2014-11-050"
168 });
169 expect(doc).toBeUndefined();
170
171 // Invalid matcher!
172 doc = dateFactory.create({
173 "match" : "match:ne",
174 });
175 expect(doc).toBeUndefined();
176 });
177
Nils Diewaldf219eb82015-01-07 20:15:42 +0000178 it('should be serializale to JSON', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000179
180 // Empty doc
181 var doc = KorAP.Doc.create();
182 expect(doc.toJson()).toEqual(jasmine.any(Object));
183
184 // Serialize string
185 doc = stringFactory.create();
186 expect(doc.toJson()).toEqual(jasmine.objectContaining({
187 "@type" : "korap:doc",
188 "type" : "type:string",
189 "key" : "author",
190 "value" : "Max Birkendale",
191 "match" : "match:eq"
192 }));
193
194 // Serialize regex
195 doc = regexFactory.create();
196 expect(doc.toJson()).toEqual(jasmine.objectContaining({
197 "@type" : "korap:doc",
198 "type" : "type:regex",
199 "value" : "[^b]ee.+?",
200 "match" : "match:eq",
201 "key" : 'title'
202 }));
203
204 doc = regexFactory.create({
205 match: "match:ne"
206 });
207 expect(doc.toJson()).toEqual(jasmine.objectContaining({
208 "@type" : "korap:doc",
209 "type" : "type:regex",
210 "value" : "[^b]ee.+?",
211 "match" : "match:ne",
212 "key" : 'title'
213 }));
214
215 doc = dateFactory.create();
216 expect(doc.toJson()).toEqual(jasmine.objectContaining({
217 "@type" : "korap:doc",
218 "type" : "type:date",
219 "value" : "2014-11-05",
220 "match" : "match:eq",
221 "key" : 'pubDate'
222 }));
223
224 doc = dateFactory.create({
225 value : "2014"
226 });
227 expect(doc.toJson()).toEqual(jasmine.objectContaining({
228 "@type" : "korap:doc",
229 "type" : "type:date",
230 "value" : "2014",
231 "match" : "match:eq",
232 "key" : 'pubDate'
233 }));
234 });
Nils Diewaldf219eb82015-01-07 20:15:42 +0000235
236 it('should be serializale to String', function () {
237
238 // Empty doc
239 var doc = KorAP.Doc.create();
Nils Diewaldd599d542015-01-08 20:41:34 +0000240 expect(doc.toQuery()).toEqual("");
Nils Diewaldf219eb82015-01-07 20:15:42 +0000241
242 // Serialize string
243 doc = stringFactory.create();
Nils Diewaldd599d542015-01-08 20:41:34 +0000244 expect(doc.toQuery()).toEqual('author = "Max Birkendale"');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000245
246 // Serialize string with quotes
247 doc = stringFactory.create({ "value" : 'Max "Der Coole" Birkendate'});
Nils Diewaldd599d542015-01-08 20:41:34 +0000248 expect(doc.toQuery()).toEqual('author = "Max \\"Der Coole\\" Birkendate"');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000249
250 // Serialize regex
251 doc = regexFactory.create();
Nils Diewaldd599d542015-01-08 20:41:34 +0000252 expect(doc.toQuery()).toEqual('title = /[^b]ee.+?/');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000253
254 doc = regexFactory.create({
255 match: "match:ne"
256 });
Nils Diewaldd599d542015-01-08 20:41:34 +0000257 expect(doc.toQuery()).toEqual('title != /[^b]ee.+?/');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000258
259 doc = dateFactory.create();
Nils Diewaldd599d542015-01-08 20:41:34 +0000260 expect(doc.toQuery()).toEqual('pubDate in 2014-11-05');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000261
262 doc = dateFactory.create({
263 value : "2014"
264 });
Nils Diewaldd599d542015-01-08 20:41:34 +0000265 expect(doc.toQuery()).toEqual('pubDate in 2014');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000266 });
Nils Diewald0b6c0412014-12-19 03:55:57 +0000267});
268
269
270describe('KorAP.DocGroup', function () {
271 // Create example factories
272 var docFactory = buildFactory(
273 KorAP.Doc,
274 {
275 "@type" : "korap:doc",
276 "match":"match:eq",
277 "key" : "author",
278 "value" : "Max Birkendale"
279 }
280 );
281
282 var docGroupFactory = buildFactory(
283 KorAP.DocGroup, {
284 "@type" : "korap:docGroup",
285 "operation" : "operation:and",
286 "operands" : [
287 docFactory.create().toJson(),
288 docFactory.create({
289 "key" : "pubDate",
290 "type" : "type:date",
291 "value" : "2014-12-05"
292 }).toJson()
293 ]
294 });
295
Nils Diewald0b6c0412014-12-19 03:55:57 +0000296
Nils Diewald8f4e2542014-12-19 04:42:09 +0000297 it('should be initializable', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000298 // Create empty group
299 var docGroup = KorAP.DocGroup.create();
300 expect(docGroup.operation()).toEqual('and');
301
302 // Create empty group
Nils Diewald8f4e2542014-12-19 04:42:09 +0000303 docGroup = KorAP.DocGroup.create();
304 docGroup.operation('or');
Nils Diewald0b6c0412014-12-19 03:55:57 +0000305 expect(docGroup.operation()).toEqual('or');
306 });
307
308 it('should be definable', function () {
309
310 // Empty group
311 var docGroup = KorAP.DocGroup.create();
312 expect(docGroup.operation()).toEqual('and');
313
314 // Set values
315 docGroup.operation("or");
316 expect(docGroup.operation()).toEqual('or');
317
318 // Set invalid values
319 docGroup.operation("hui");
320 expect(docGroup.operation()).toEqual('or');
321 });
322
323 it('should be deserializable', function () {
324 var docGroup = docGroupFactory.create();
325 expect(docGroup.operation()).toEqual("and");
326 expect(docGroup.operands().length).toEqual(2);
327
328 var op1 = docGroup.getOperand(0);
329 expect(op1.type()).toEqual("string");
330 expect(op1.key()).toEqual("author");
331 expect(op1.value()).toEqual("Max Birkendale");
332 expect(op1.matchop()).toEqual("eq");
333
334 var op2 = docGroup.getOperand(1);
335 expect(op2.type()).toEqual("date");
336 expect(op2.key()).toEqual("pubDate");
337 expect(op2.value()).toEqual("2014-12-05");
338 expect(op2.matchop()).toEqual("eq");
339
Nils Diewaldf219eb82015-01-07 20:15:42 +0000340 // Append empty group
Nils Diewald966abf12014-12-20 02:27:45 +0000341 var newGroup = docGroup.append(KorAP.DocGroup.create());
Nils Diewald8f4e2542014-12-19 04:42:09 +0000342 newGroup.operation('or');
Nils Diewald966abf12014-12-20 02:27:45 +0000343 newGroup.append(docFactory.create());
344 newGroup.append(docFactory.create({
Nils Diewald0b6c0412014-12-19 03:55:57 +0000345 "type" : "type:regex",
346 "key" : "title",
347 "value" : "^e.+?$",
348 "match" : "match:ne"
349 }));
350
351 expect(docGroup.operation()).toEqual("and");
352 expect(docGroup.operands().length).toEqual(3);
353
354 var op1 = docGroup.getOperand(0);
355 expect(op1.ldType()).toEqual("doc");
356 expect(op1.type()).toEqual("string");
357 expect(op1.key()).toEqual("author");
358 expect(op1.value()).toEqual("Max Birkendale");
359 expect(op1.matchop()).toEqual("eq");
360
361 var op2 = docGroup.getOperand(1);
362 expect(op2.ldType()).toEqual("doc");
363 expect(op2.type()).toEqual("date");
364 expect(op2.key()).toEqual("pubDate");
365 expect(op2.value()).toEqual("2014-12-05");
366 expect(op2.matchop()).toEqual("eq");
367
368 var op3 = docGroup.getOperand(2);
369 expect(op3.ldType()).toEqual("docGroup");
370 expect(op3.operation()).toEqual("or");
371
372 var op4 = op3.getOperand(0);
373 expect(op4.ldType()).toEqual("doc");
374 expect(op4.type()).toEqual("string");
375 expect(op4.key()).toEqual("author");
376 expect(op4.value()).toEqual("Max Birkendale");
377 expect(op4.matchop()).toEqual("eq");
378
379 var op5 = op3.getOperand(1);
380 expect(op5.ldType()).toEqual("doc");
381 expect(op5.type()).toEqual("regex");
382 expect(op5.key()).toEqual("title");
383 expect(op5.value()).toEqual("^e.+?$");
384 expect(op5.matchop()).toEqual("ne");
385 });
386
Nils Diewaldf219eb82015-01-07 20:15:42 +0000387 it('should be serializable to JSON', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000388 var docGroup = docGroupFactory.create();
389
390 expect(docGroup.toJson()).toEqual(jasmine.objectContaining({
391 "@type" : "korap:docGroup",
392 "operation" : "operation:and",
393 "operands" : [
394 {
395 "@type": 'korap:doc',
396 "key" : 'author',
397 "match": 'match:eq',
398 "value": 'Max Birkendale',
399 "type": 'type:string'
400 },
401 {
402 "@type": 'korap:doc',
403 "key": 'pubDate',
404 "match": 'match:eq',
405 "value": '2014-12-05',
406 "type": 'type:date'
407 }
408 ]
409 }));
410 });
Nils Diewaldf219eb82015-01-07 20:15:42 +0000411
412 it('should be serializable to String', function () {
413 var docGroup = docGroupFactory.create();
Nils Diewaldd599d542015-01-08 20:41:34 +0000414 expect(docGroup.toQuery()).toEqual('author = "Max Birkendale" & pubDate in 2014-12-05');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000415
416 docGroup = docGroupFactory.create({
417 "@type" : "korap:docGroup",
418 "operation" : "operation:or",
419 "operands" : [
420 {
421 "@type": 'korap:doc',
422 "key" : 'author',
423 "match": 'match:eq',
424 "value": 'Max Birkendale',
425 "type": 'type:string'
426 },
427 {
428 "@type" : "korap:docGroup",
429 "operation" : "operation:and",
430 "operands" : [
431 {
432 "@type": 'korap:doc',
433 "key": 'pubDate',
434 "match": 'match:geq',
435 "value": '2014-05-12',
436 "type": 'type:date'
437 },
438 {
439 "@type": 'korap:doc',
440 "key": 'pubDate',
441 "match": 'match:leq',
442 "value": '2014-12-05',
443 "type": 'type:date'
444 },
445 {
446 "@type": 'korap:doc',
447 "key": 'foo',
448 "match": 'match:ne',
449 "value": '[a]?bar',
450 "type": 'type:regex'
451 }
452 ]
453 }
454 ]
455 });
Nils Diewaldd599d542015-01-08 20:41:34 +0000456 expect(docGroup.toQuery()).toEqual(
Nils Diewaldf219eb82015-01-07 20:15:42 +0000457 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05 & foo != /[a]?bar/)'
458 );
459 });
Nils Diewald0b6c0412014-12-19 03:55:57 +0000460});
461
Nils Diewald966abf12014-12-20 02:27:45 +0000462describe('KorAP.UnspecifiedDoc', function () {
463 it('should be initializable', function () {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000464 var doc = KorAP.UnspecifiedDoc.create();
465 var docElement = doc.element();
Nils Diewaldd599d542015-01-08 20:41:34 +0000466 expect(docElement.getAttribute('class')).toEqual('doc unspecified');
Nils Diewald966abf12014-12-20 02:27:45 +0000467 expect(docElement.firstChild.firstChild.data).toEqual('⋯');
Nils Diewalde15b7a22015-01-09 21:50:21 +0000468 expect(docElement.lastChild.lastChild.data).toEqual('⋯');
Nils Diewaldd599d542015-01-08 20:41:34 +0000469 expect(doc.toQuery()).toEqual('⋯');
Nils Diewald966abf12014-12-20 02:27:45 +0000470
Nils Diewald8f6b6102015-01-08 18:25:33 +0000471 // Only removable
Nils Diewald966abf12014-12-20 02:27:45 +0000472 expect(docElement.lastChild.children.length).toEqual(0);
473 });
474
475 it('should be removable, when no root', function () {
476 var docGroup = KorAP.DocGroup.create();
477 docGroup.operation('or');
478 expect(docGroup.operation()).toEqual('or');
479
480 docGroup.append({
481 "@type": 'korap:doc',
482 "key": 'pubDate',
483 "match": 'match:eq',
484 "value": '2014-12-05',
485 "type": 'type:date'
486 });
487
Nils Diewaldf219eb82015-01-07 20:15:42 +0000488 // Add unspecified object
489 docGroup.append();
490
Nils Diewald966abf12014-12-20 02:27:45 +0000491 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
492 expect(docGroup.element().children[0].getAttribute('class')).toEqual('doc');
493
Nils Diewald966abf12014-12-20 02:27:45 +0000494 var unspec = docGroup.element().children[1];
Nils Diewaldd599d542015-01-08 20:41:34 +0000495 expect(unspec.getAttribute('class')).toEqual('doc unspecified');
Nils Diewald966abf12014-12-20 02:27:45 +0000496
497 // Removable
498 expect(unspec.lastChild.children.length).toEqual(1);
499 expect(unspec.lastChild.children[0].getAttribute('class')).toEqual('delete');
500 });
501});
502
Nils Diewald8f4e2542014-12-19 04:42:09 +0000503describe('KorAP.Doc element', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000504 it('should be initializable', function () {
Nils Diewald8f4e2542014-12-19 04:42:09 +0000505 var docElement = KorAP.Doc.create(undefined, {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000506 "@type" : "korap:doc",
507 "key":"Titel",
508 "value":"Baum",
509 "match":"match:eq"
510 });
511 expect(docElement.key()).toEqual('Titel');
512 expect(docElement.matchop()).toEqual('eq');
513 expect(docElement.value()).toEqual('Baum');
514
515 var docE = docElement.element();
516 expect(docE.children[0].firstChild.data).toEqual('Titel');
517 expect(docE.children[1].firstChild.data).toEqual('eq');
518 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
519 expect(docE.children[2].firstChild.data).toEqual('Baum');
520 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
521
522 expect(docElement.toJson()).toEqual(jasmine.objectContaining({
523 "@type" : "korap:doc",
524 "key":"Titel",
525 "value":"Baum",
526 "match":"match:eq"
527 }));
528 });
529});
530
Nils Diewald8f4e2542014-12-19 04:42:09 +0000531describe('KorAP.DocGroup element', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000532 it('should be initializable', function () {
533
Nils Diewald8f4e2542014-12-19 04:42:09 +0000534 var docGroup = KorAP.DocGroup.create(undefined, {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000535 "@type" : "korap:docGroup",
536 "operation" : "operation:and",
537 "operands" : [
538 {
539 "@type": 'korap:doc',
540 "key" : 'author',
541 "match": 'match:eq',
542 "value": 'Max Birkendale',
543 "type": 'type:string'
544 },
545 {
546 "@type": 'korap:doc',
547 "key": 'pubDate',
548 "match": 'match:eq',
549 "value": '2014-12-05',
550 "type": 'type:date'
551 }
552 ]
553 });
554
Nils Diewald8f4e2542014-12-19 04:42:09 +0000555 expect(docGroup.operation()).toEqual('and');
556 var e = docGroup.element();
Nils Diewald0b6c0412014-12-19 03:55:57 +0000557 expect(e.getAttribute('class')).toEqual('docGroup');
558 expect(e.getAttribute('data-operation')).toEqual('and');
559
560 var first = e.children[0];
561 expect(first.getAttribute('class')).toEqual('doc');
562 expect(first.children[0].getAttribute('class')).toEqual('key');
563 expect(first.children[1].getAttribute('class')).toEqual('match');
564 expect(first.children[2].getAttribute('class')).toEqual('value');
565 expect(first.children[2].getAttribute('data-type')).toEqual('string');
566 expect(first.children[0].firstChild.data).toEqual('author');
567 expect(first.children[1].firstChild.data).toEqual('eq');
568 expect(first.children[2].firstChild.data).toEqual('Max Birkendale');
569
570 var second = e.children[1];
571 expect(second.getAttribute('class')).toEqual('doc');
572 expect(second.children[0].getAttribute('class')).toEqual('key');
573 expect(second.children[1].getAttribute('class')).toEqual('match');
574 expect(second.children[2].getAttribute('class')).toEqual('value');
575 expect(second.children[2].getAttribute('data-type')).toEqual('date');
576 expect(second.children[0].firstChild.data).toEqual('pubDate');
577 expect(second.children[1].firstChild.data).toEqual('eq');
578 expect(second.children[2].firstChild.data).toEqual('2014-12-05');
579
580 });
581
582 it('should be deserializable with nested groups', function () {
Nils Diewald8f4e2542014-12-19 04:42:09 +0000583 var docGroup = KorAP.DocGroup.create(undefined, {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000584 "@type" : "korap:docGroup",
585 "operation" : "operation:or",
586 "operands" : [
587 {
588 "@type": 'korap:doc',
589 "key" : 'author',
590 "match": 'match:eq',
591 "value": 'Max Birkendale',
592 "type": 'type:string'
593 },
594 {
595 "@type" : "korap:docGroup",
596 "operation" : "operation:and",
597 "operands" : [
598 {
599 "@type": 'korap:doc',
600 "key": 'pubDate',
601 "match": 'match:geq',
602 "value": '2014-05-12',
603 "type": 'type:date'
604 },
605 {
606 "@type": 'korap:doc',
607 "key": 'pubDate',
608 "match": 'match:leq',
609 "value": '2014-12-05',
610 "type": 'type:date'
611 }
612 ]
613 }
614 ]
615 });
616
Nils Diewald8f4e2542014-12-19 04:42:09 +0000617 expect(docGroup.operation()).toEqual('or');
618 var e = docGroup.element();
Nils Diewald0b6c0412014-12-19 03:55:57 +0000619 expect(e.getAttribute('class')).toEqual('docGroup');
620 expect(e.getAttribute('data-operation')).toEqual('or');
621
Nils Diewald966abf12014-12-20 02:27:45 +0000622 expect(e.children[0].getAttribute('class')).toEqual('doc');
623 var docop = e.children[0].lastChild;
624 expect(docop.getAttribute('class')).toEqual('operators');
625 expect(docop.children[0].getAttribute('class')).toEqual('and');
626 expect(docop.children[1].getAttribute('class')).toEqual('or');
627 expect(docop.children[2].getAttribute('class')).toEqual('delete');
628
629 expect(e.children[1].getAttribute('class')).toEqual('docGroup');
630 expect(e.children[1].getAttribute('data-operation')).toEqual('and');
631
632 // This and-operation can be "or"ed or "delete"d
633 var secop = e.children[1].children[2];
634 expect(secop.getAttribute('class')).toEqual('operators');
635 expect(secop.children[0].getAttribute('class')).toEqual('or');
636 expect(secop.children[1].getAttribute('class')).toEqual('delete');
637
638 // This or-operation can be "and"ed or "delete"d
639 expect(e.children[2].getAttribute('class')).toEqual('operators');
640 expect(e.lastChild.getAttribute('class')).toEqual('operators');
641 expect(e.lastChild.children[0].getAttribute('class')).toEqual('and');
642 expect(e.lastChild.children[1].getAttribute('class')).toEqual('delete');
Nils Diewald0b6c0412014-12-19 03:55:57 +0000643
644 });
645});
646
647describe('KorAP.VirtualCollection', function () {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000648
649 var simpleGroupFactory = buildFactory(KorAP.DocGroup, {
650 "@type" : "korap:docGroup",
651 "operation" : "operation:and",
652 "operands" : [
653 {
654 "@type": 'korap:doc',
655 "key" : 'author',
656 "match": 'match:eq',
657 "value": 'Max Birkendale',
658 "type": 'type:string'
659 },
660 {
661 "@type": 'korap:doc',
662 "key": 'pubDate',
663 "match": 'match:eq',
664 "value": '2014-12-05',
665 "type": 'type:date'
666 }
667 ]
668 });
669
670 var nestedGroupFactory = buildFactory(KorAP.VirtualCollection, {
671 "@type" : "korap:docGroup",
672 "operation" : "operation:or",
673 "operands" : [
674 {
675 "@type": 'korap:doc',
676 "key" : 'author',
677 "match": 'match:eq',
678 "value": 'Max Birkendale',
679 "type": 'type:string'
680 },
681 {
682 "@type" : "korap:docGroup",
683 "operation" : "operation:and",
684 "operands" : [
685 {
686 "@type": 'korap:doc',
687 "key": 'pubDate',
688 "match": 'match:geq',
689 "value": '2014-05-12',
690 "type": 'type:date'
691 },
692 {
693 "@type": 'korap:doc',
694 "key": 'pubDate',
695 "match": 'match:leq',
696 "value": '2014-12-05',
697 "type": 'type:date'
698 }
699 ]
700 }
701 ]
702 });
703
704 var flatGroupFactory = buildFactory(KorAP.VirtualCollection, {
705 "@type" : "korap:docGroup",
706 "operation" : "operation:and",
707 "operands" : [
708 {
709 "@type": 'korap:doc',
710 "key": 'pubDate',
711 "match": 'match:geq',
712 "value": '2014-05-12',
713 "type": 'type:date'
714 },
715 {
716 "@type": 'korap:doc',
717 "key": 'pubDate',
718 "match": 'match:leq',
719 "value": '2014-12-05',
720 "type": 'type:date'
721 },
722 {
723 "@type": 'korap:doc',
724 "key": 'foo',
725 "match": 'match:eq',
726 "value": 'bar',
727 "type": 'type:string'
728 }
729 ]
730 });
Nils Diewald4019bd22015-01-08 19:57:50 +0000731
732 var demoFactory = buildFactory(KorAP.VirtualCollection, {
733 "@type":"korap:docGroup",
734 "operation":"operation:or",
735 "operands":[
736 {
737 "@type":"korap:docGroup",
738 "operation":"operation:and",
739 "operands":[
740 {
741 "@type":"korap:doc",
742 "key":"Titel",
743 "value":"Baum",
744 "match":"match:eq"
745 },
746 {
747 "@type":"korap:doc",
748 "key":"Veröffentlichungsort",
749 "value":"hihi",
750 "match":"match:eq"
751 },
752 {
753 "@type":"korap:docGroup",
754 "operation":"operation:or",
755 "operands":[
756 {
757 "@type":"korap:doc",
758 "key":"Titel",
759 "value":"Baum",
760 "match":"match:eq"
761 },
762 {
763 "@type":"korap:doc",
764 "key":"Veröffentlichungsort",
765 "value":"hihi",
766 "match":"match:eq"
767 }
768 ]
769 }
770 ]
771 },
772 {
773 "@type":"korap:doc",
774 "key":"Untertitel",
775 "value":"huhu",
776 "match":"match:eq"
777 }
778 ]
779 });
Nils Diewaldf219eb82015-01-07 20:15:42 +0000780
Nils Diewald0b6c0412014-12-19 03:55:57 +0000781 it('should be initializable', function () {
782 var vc = KorAP.VirtualCollection.render();
783 expect(vc.element().getAttribute('class')).toEqual('vc');
Nils Diewaldd599d542015-01-08 20:41:34 +0000784 expect(vc.root().element().getAttribute('class')).toEqual('doc unspecified');
Nils Diewald966abf12014-12-20 02:27:45 +0000785
786 // Not removable
787 expect(vc.root().element().lastChild.children.length).toEqual(0);
Nils Diewald0b6c0412014-12-19 03:55:57 +0000788 });
789
790 it('should be based on a doc', function () {
791 var vc = KorAP.VirtualCollection.render({
792 "@type" : "korap:doc",
793 "key":"Titel",
794 "value":"Baum",
795 "match":"match:eq"
796 });
797
798 expect(vc.element().getAttribute('class')).toEqual('vc');
799 expect(vc.root().element().getAttribute('class')).toEqual('doc');
800 expect(vc.root().key()).toEqual('Titel');
801 expect(vc.root().value()).toEqual('Baum');
802 expect(vc.root().matchop()).toEqual('eq');
803
804 var docE = vc.root().element();
805 expect(docE.children[0].firstChild.data).toEqual('Titel');
806 expect(docE.children[1].firstChild.data).toEqual('eq');
807 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
808 expect(docE.children[2].firstChild.data).toEqual('Baum');
809 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
810 });
811
812 it('should be based on a docGroup', function () {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000813 var vc = KorAP.VirtualCollection.render(simpleGroupFactory.create().toJson());
Nils Diewald0b6c0412014-12-19 03:55:57 +0000814
815 expect(vc.element().getAttribute('class')).toEqual('vc');
816 expect(vc.root().element().getAttribute('class')).toEqual('docGroup');
817 expect(vc.root().operation()).toEqual('and');
818
819 var docGroup = vc.root();
820
821 var first = docGroup.getOperand(0);
822 expect(first.key()).toEqual('author');
823 expect(first.value()).toEqual('Max Birkendale');
824 expect(first.matchop()).toEqual('eq');
825
826 var second = docGroup.getOperand(1);
827 expect(second.key()).toEqual('pubDate');
828 expect(second.value()).toEqual('2014-12-05');
829 expect(second.matchop()).toEqual('eq');
830 });
831
Nils Diewald5c817a42015-01-06 01:08:56 +0000832
Nils Diewald966abf12014-12-20 02:27:45 +0000833 it('should be based on a nested docGroup', function () {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000834 var vc = nestedGroupFactory.create();
835
Nils Diewald5c817a42015-01-06 01:08:56 +0000836 expect(vc.element().getAttribute('class')).toEqual('vc');
837 expect(vc.element().firstChild.getAttribute('class')).toEqual('docGroup');
838 expect(vc.element().firstChild.children[0].getAttribute('class')).toEqual('doc');
839 var dg = vc.element().firstChild.children[1];
840 expect(dg.getAttribute('class')).toEqual('docGroup');
841 expect(dg.children[0].getAttribute('class')).toEqual('doc');
842 expect(dg.children[1].getAttribute('class')).toEqual('doc');
843 expect(dg.children[2].getAttribute('class')).toEqual('operators');
844 expect(vc.element().firstChild.children[2].getAttribute('class')).toEqual('operators');
845 });
846
Nils Diewaldf219eb82015-01-07 20:15:42 +0000847 it('should be modifiable by deletion in flat docGroups', function () {
848 var vc = flatGroupFactory.create();
Nils Diewald0297ba12015-01-05 21:56:12 +0000849 var docGroup = vc.root();
Nils Diewaldf219eb82015-01-07 20:15:42 +0000850
851 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
852
Nils Diewald0297ba12015-01-05 21:56:12 +0000853 var doc = docGroup.getOperand(1);
854 expect(doc.key()).toEqual("pubDate");
Nils Diewaldf219eb82015-01-07 20:15:42 +0000855 expect(doc.value()).toEqual("2014-12-05");
Nils Diewald5c817a42015-01-06 01:08:56 +0000856
857 // Remove operand 1
Nils Diewaldf219eb82015-01-07 20:15:42 +0000858 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
Nils Diewald5c817a42015-01-06 01:08:56 +0000859 expect(doc._element).toEqual(undefined);
860
Nils Diewald0297ba12015-01-05 21:56:12 +0000861 doc = docGroup.getOperand(1);
862 expect(doc.key()).toEqual("foo");
Nils Diewald0297ba12015-01-05 21:56:12 +0000863
Nils Diewald5c817a42015-01-06 01:08:56 +0000864 // Remove operand 1
Nils Diewaldf219eb82015-01-07 20:15:42 +0000865 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
Nils Diewald5c817a42015-01-06 01:08:56 +0000866 expect(doc._element).toEqual(undefined);
Nils Diewald0297ba12015-01-05 21:56:12 +0000867
Nils Diewaldf219eb82015-01-07 20:15:42 +0000868 // Only one operand left ...
Nils Diewald5c817a42015-01-06 01:08:56 +0000869 expect(docGroup.getOperand(1)).toBeUndefined();
Nils Diewaldf219eb82015-01-07 20:15:42 +0000870 // ... but there shouldn't be a group anymore at all!
871 expect(docGroup.getOperand(0)).toBeUndefined();
872
873 var obj = vc.root();
874 expect(obj.ldType()).toEqual("doc");
875 expect(obj.key()).toEqual("pubDate");
876 expect(obj.value()).toEqual("2014-05-12");
877
878 expect(obj.element().getAttribute('class')).toEqual('doc');
Nils Diewald5c817a42015-01-06 01:08:56 +0000879 });
Nils Diewaldf219eb82015-01-07 20:15:42 +0000880
881
882 it('should be modifiable by deletion in nested docGroups (root case)', function () {
883 var vc = nestedGroupFactory.create();
884
Nils Diewaldd599d542015-01-08 20:41:34 +0000885 expect(vc.toQuery()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +0000886 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05)'
887 );
888
Nils Diewaldf219eb82015-01-07 20:15:42 +0000889 var docGroup = vc.root();
890 expect(docGroup.ldType()).toEqual("docGroup");
891 expect(docGroup.operation()).toEqual("or");
892
893 var doc = docGroup.getOperand(0);
894 expect(doc.key()).toEqual("author");
895 expect(doc.value()).toEqual("Max Birkendale");
896
897 docGroup = docGroup.getOperand(1);
898 expect(docGroup.operation()).toEqual("and");
899
900 doc = docGroup.getOperand(0);
901 expect(doc.key()).toEqual("pubDate");
902 expect(doc.matchop()).toEqual("geq");
903 expect(doc.value()).toEqual("2014-05-12");
904 expect(doc.type()).toEqual("date");
905
906 doc = docGroup.getOperand(1);
907 expect(doc.key()).toEqual("pubDate");
908 expect(doc.matchop()).toEqual("leq");
909 expect(doc.value()).toEqual("2014-12-05");
910 expect(doc.type()).toEqual("date");
911
912 // Remove first operand so everything becomes root
913 expect(
914 vc.root().delOperand(
915 vc.root().getOperand(0)
916 ).update().ldType()
917 ).toEqual("docGroup");
Nils Diewald8e7182e2015-01-08 15:02:07 +0000918
Nils Diewaldf219eb82015-01-07 20:15:42 +0000919 expect(vc.root().ldType()).toEqual("docGroup");
920 expect(vc.root().operation()).toEqual("and");
Nils Diewald8e7182e2015-01-08 15:02:07 +0000921 expect(vc.root().getOperand(0).ldType()).toEqual("doc");
922
Nils Diewaldd599d542015-01-08 20:41:34 +0000923 expect(vc.toQuery()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +0000924 'pubDate since 2014-05-12 & pubDate until 2014-12-05'
925 );
Nils Diewaldf219eb82015-01-07 20:15:42 +0000926 });
927
928 it('should be modifiable by deletion in nested docGroups (resolve group case)', function () {
929 var vc = nestedGroupFactory.create();
930
931 // Get nested group
932 var firstGroup = vc.root().getOperand(1);
933 firstGroup.append(simpleGroupFactory.create({ "operation" : "operation:or" }));
934
935 // Structur is now:
936 // or(doc, and(doc, doc, or(doc, doc)))
937
938 // Get nested or in and
939 var orGroup = vc.root().getOperand(1).getOperand(2);
940 expect(orGroup.ldType()).toEqual("docGroup");
941 expect(orGroup.operation()).toEqual("or");
942
943 // Remove
944 // Structur is now:
945 // or(doc, and(doc, doc, doc)))
946 expect(orGroup.delOperand(orGroup.getOperand(0)).update().operation()).toEqual("and");
947 expect(vc.root().getOperand(1).operands().length).toEqual(3);
948 });
949
950 it('should be modifiable by deletion in nested docGroups (identical group case)', function () {
951 var vc = nestedGroupFactory.create();
952
953 // Get nested group
954 var firstGroup = vc.root().getOperand(1);
Nils Diewald8e7182e2015-01-08 15:02:07 +0000955 firstGroup.append(simpleGroupFactory.create({
956 "operation" : "operation:or"
957 }));
958 var oldAuthor = firstGroup.getOperand(2).getOperand(0);
959 oldAuthor.key("title");
960 oldAuthor.value("Der Birnbaum");
Nils Diewaldf219eb82015-01-07 20:15:42 +0000961
962 // Structur is now:
963 // or(doc, and(doc, doc, or(doc, doc)))
Nils Diewaldd599d542015-01-08 20:41:34 +0000964 expect(vc.toQuery()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +0000965 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05 & (title = "Der Birnbaum" | pubDate in 2014-12-05))'
Nils Diewaldf219eb82015-01-07 20:15:42 +0000966 );
967
968 var andGroup = vc.root().getOperand(1);
969
970 // Get leading docs in and
971 var doc1 = andGroup.getOperand(0);
972 expect(doc1.ldType()).toEqual("doc");
973 expect(doc1.value()).toEqual("2014-05-12");
974 var doc2 = andGroup.getOperand(1);
975 expect(doc2.ldType()).toEqual("doc");
976 expect(doc2.value()).toEqual("2014-12-05");
977
978 // Remove 2
979 expect(andGroup.delOperand(doc2).update().operation()).toEqual("and");
980 // Structur is now:
981 // or(doc, and(doc, or(doc, doc)))
982
Nils Diewaldd599d542015-01-08 20:41:34 +0000983 expect(vc.toQuery()).toEqual(
984 'author = "Max Birkendale"' +
985 ' | (pubDate since 2014-05-12 & ' +
986 '(title = "Der Birnbaum" | pubDate in 2014-12-05))'
Nils Diewaldf219eb82015-01-07 20:15:42 +0000987 );
988
989
990 // Remove 1
991 expect(andGroup.delOperand(doc1).update().operation()).toEqual("or");
992 // Structur is now:
993 // or(doc, doc, doc)
994
Nils Diewaldd599d542015-01-08 20:41:34 +0000995 expect(vc.toQuery()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +0000996 'author = "Max Birkendale" | title = "Der Birnbaum" | pubDate in 2014-12-05'
Nils Diewaldf219eb82015-01-07 20:15:42 +0000997 );
998 });
Nils Diewald4019bd22015-01-08 19:57:50 +0000999
1000 it('should be reducible to unspecification', function () {
1001 var vc = demoFactory.create();
1002
Nils Diewaldd599d542015-01-08 20:41:34 +00001003 expect(vc.toQuery()).toEqual(vc.root().toQuery());
Nils Diewald4019bd22015-01-08 19:57:50 +00001004
Nils Diewaldd599d542015-01-08 20:41:34 +00001005 expect(vc.toQuery()).toEqual(
1006 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & ' +
1007 '(Titel = "Baum" | Veröffentlichungsort = "hihi")) ' +
1008 '| Untertitel = "huhu"');
Nils Diewald4019bd22015-01-08 19:57:50 +00001009
1010 expect(vc.root().element().lastChild.children[0].firstChild.nodeValue).toEqual('and');
1011 expect(vc.root().element().lastChild.children[1].firstChild.nodeValue).toEqual('×');
Nils Diewald4019bd22015-01-08 19:57:50 +00001012 expect(vc.root().delOperand(vc.root().getOperand(0)).update()).not.toBeUndefined();
Nils Diewaldd599d542015-01-08 20:41:34 +00001013 expect(vc.toQuery()).toEqual('Untertitel = "huhu"');
Nils Diewald4019bd22015-01-08 19:57:50 +00001014
Nils Diewaldd599d542015-01-08 20:41:34 +00001015 var lc = vc.root().element().lastChild;
1016 expect(lc.children[0].firstChild.nodeValue).toEqual('and');
1017 expect(lc.children[1].firstChild.nodeValue).toEqual('or');
1018 expect(lc.children[2].firstChild.nodeValue).toEqual('×');
Nils Diewald4019bd22015-01-08 19:57:50 +00001019
1020 // Clean everything
1021 vc.clean();
Nils Diewaldd599d542015-01-08 20:41:34 +00001022 expect(vc.toQuery()).toEqual('⋯');
Nils Diewald4019bd22015-01-08 19:57:50 +00001023 });
Nils Diewald0b6c0412014-12-19 03:55:57 +00001024});
1025
1026describe('KorAP.Operators', function () {
1027 it('should be initializable', function () {
1028 var op = KorAP.Operators.create(true, false, false);
1029 expect(op.and()).toBeTruthy();
1030 expect(op.or()).not.toBeTruthy();
1031 expect(op.del()).not.toBeTruthy();
1032
1033 op.and(false);
1034 expect(op.and()).not.toBeTruthy();
1035 expect(op.or()).not.toBeTruthy();
1036 expect(op.del()).not.toBeTruthy();
1037
1038 op.or(true);
1039 op.del(true);
1040 expect(op.and()).not.toBeTruthy();
1041 expect(op.or()).toBeTruthy();
1042 expect(op.del()).toBeTruthy();
1043
1044 var e = op.element();
1045 expect(e.getAttribute('class')).toEqual('operators');
1046 expect(e.children[0].getAttribute('class')).toEqual('or');
1047 expect(e.children[0].firstChild.data).toEqual('or');
1048 expect(e.children[1].getAttribute('class')).toEqual('delete');
1049 expect(e.children[1].firstChild.data).toEqual('×');
1050
1051 op.and(true);
1052 op.del(false);
1053 op.update();
1054
1055 e = op.element();
1056 expect(e.getAttribute('class')).toEqual('operators');
1057 expect(e.children[0].getAttribute('class')).toEqual('and');
1058 expect(e.children[0].firstChild.data).toEqual('and');
1059 expect(e.children[1].getAttribute('class')).toEqual('or');
1060 expect(e.children[1].firstChild.data).toEqual('or');
1061 });
1062});
Nils Diewalde15b7a22015-01-09 21:50:21 +00001063
1064describe('KorAP._delete (event)', function () {
1065 var complexVCFactory = buildFactory(KorAP.VirtualCollection,{
1066 "@type": 'korap:docGroup',
1067 'operation' : 'operation:and',
1068 'operands' : [
1069 {
1070 "@type": 'korap:doc',
1071 "key": 'pubDate',
1072 "match": 'match:eq',
1073 "value": '2014-12-05',
1074 "type": 'type:date'
1075 },
1076 {
1077 "@type" : 'korap:docGroup',
1078 'operation' : 'operation:or',
1079 'operands' : [
1080 {
1081 '@type' : 'korap:doc',
1082 'key' : 'title',
1083 'value' : 'Hello World!'
1084 },
1085 {
1086 '@type' : 'korap:doc',
1087 'key' : 'foo',
1088 'value' : 'bar'
1089 }
1090 ]
1091 }
1092 ]
1093 });
1094
1095 it('should clean on root docs', function () {
1096 var vc = KorAP.VirtualCollection.render({
1097 "@type": 'korap:doc',
1098 "key": 'pubDate',
1099 "match": 'match:eq',
1100 "value": '2014-12-05',
1101 "type": 'type:date'
1102 });
1103 expect(vc.root().toQuery()).toEqual('pubDate in 2014-12-05');
1104 expect(vc.root().element().lastChild.getAttribute('class')).toEqual('operators');
1105
1106 // Clean with delete from root
1107 expect(vc.root().element().lastChild.lastChild.getAttribute('class')).toEqual('delete');
1108 KorAP._delete.bind(vc.root().element().lastChild.lastChild).apply();
1109 expect(vc.root().toQuery()).toEqual('⋯');
1110 expect(vc.root().element().lastChild.lastChild.data).toEqual('⋯');
1111 });
1112
1113 it ('should remove on nested docs', function () {
1114 var vc = KorAP.VirtualCollection.render(
1115 {
1116 "@type": 'korap:docGroup',
1117 'operation' : 'operation:and',
1118 'operands' : [
1119 {
1120 "@type": 'korap:doc',
1121 "key": 'pubDate',
1122 "match": 'match:eq',
1123 "value": '2014-12-05',
1124 "type": 'type:date'
1125 },
1126 {
1127 "@type" : 'korap:doc',
1128 'key' : 'foo',
1129 'value' : 'bar'
1130 }
1131 ]
1132 }
1133 );
1134
1135 // Delete with direct element access
1136 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1137 KorAP._delete.bind(vc.root().getOperand(0).element().lastChild.lastChild).apply();
1138 expect(vc.toQuery()).toEqual('foo = "bar"');
1139 expect(vc.root().ldType()).toEqual('doc');
1140 });
1141
1142 it ('should clean on doc groups', function () {
1143 var vc = KorAP.VirtualCollection.render(
1144 {
1145 "@type": 'korap:docGroup',
1146 'operation' : 'operation:and',
1147 'operands' : [
1148 {
1149 "@type": 'korap:doc',
1150 "key": 'pubDate',
1151 "match": 'match:eq',
1152 "value": '2014-12-05',
1153 "type": 'type:date'
1154 },
1155 {
1156 "@type" : 'korap:doc',
1157 'key' : 'foo',
1158 'value' : 'bar'
1159 }
1160 ]
1161 }
1162 );
1163
1164 // Cleanwith direct element access
1165 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1166 KorAP._delete.bind(vc.root().element().lastChild.lastChild).apply();
1167 expect(vc.toQuery()).toEqual('⋯');
1168 expect(vc.root().ldType()).toEqual('non');
1169 });
1170
1171 it ('should remove on nested doc groups (case of ungrouping 1)', function () {
1172 var vc = complexVCFactory.create();
1173
1174 // Delete with direct element access
1175 expect(vc.toQuery()).toEqual(
1176 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1177 );
1178
1179 // Remove hello world:
1180 KorAP._delete.bind(vc.root().getOperand(1).getOperand(0).element().lastChild.lastChild).apply();
1181 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1182 expect(vc.root().ldType()).toEqual('docGroup');
1183 });
1184
1185 it ('should remove on nested doc groups (case of ungrouping 2)', function () {
1186 var vc = complexVCFactory.create();
1187
1188 // Delete with direct element access
1189 expect(vc.toQuery()).toEqual(
1190 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1191 );
1192
1193 // Remove bar
1194 KorAP._delete.bind(vc.root().getOperand(1).getOperand(1).element().lastChild.lastChild).apply();
1195 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & title = "Hello World!"');
1196 expect(vc.root().ldType()).toEqual('docGroup');
1197 expect(vc.root().operation()).toEqual('and');
1198 });
1199
1200 it ('should remove on nested doc groups (case of root changing)', function () {
1201 var vc = complexVCFactory.create();
1202
1203 // Delete with direct element access
1204 expect(vc.toQuery()).toEqual(
1205 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1206 );
1207
1208 // Remove bar
1209 KorAP._delete.bind(vc.root().getOperand(0).element().lastChild.lastChild).apply();
1210 expect(vc.toQuery()).toEqual('title = "Hello World!" | foo = "bar"');
1211 expect(vc.root().ldType()).toEqual('docGroup');
1212 expect(vc.root().operation()).toEqual('or');
1213 });
1214
1215 it ('should remove on nested doc groups (list flattening)', function () {
1216 var vc = KorAP.VirtualCollection.render(
1217 {
1218 "@type": 'korap:docGroup',
1219 'operation' : 'operation:or',
1220 'operands' : [
1221 {
1222 "@type": 'korap:doc',
1223 "key": 'pubDate',
1224 "match": 'match:eq',
1225 "value": '2014-12-05',
1226 "type": 'type:date'
1227 },
1228 {
1229 "@type" : 'korap:doc',
1230 'key' : 'foo',
1231 'value' : 'bar'
1232 },
1233 {
1234 "@type": 'korap:docGroup',
1235 'operation' : 'operation:and',
1236 'operands' : [
1237 {
1238 "@type": 'korap:doc',
1239 "key": 'pubDate',
1240 "match": 'match:eq',
1241 "value": '2014-12-05',
1242 "type": 'type:date'
1243 },
1244 {
1245 "@type" : 'korap:docGroup',
1246 'operation' : 'operation:or',
1247 'operands' : [
1248 {
1249 '@type' : 'korap:doc',
1250 'key' : 'title',
1251 'value' : 'Hello World!'
1252 },
1253 {
1254 '@type' : 'korap:doc',
1255 'key' : 'yeah',
1256 'value' : 'juhu'
1257 }
1258 ]
1259 }
1260 ]
1261 }
1262 ]
1263 }
1264 );
1265
1266 // Delete with direct element access
1267 expect(vc.toQuery()).toEqual(
1268 'pubDate in 2014-12-05 | foo = "bar" | ' +
1269 '(pubDate in 2014-12-05 & ' +
1270 '(title = "Hello World!" | yeah = "juhu"))'
1271 );
1272
1273 expect(vc.root().ldType()).toEqual('docGroup');
1274 expect(vc.root().operation()).toEqual('or');
1275
1276 // Operands and operators
1277 expect(vc.element().firstChild.children.length).toEqual(4);
1278 expect(vc.element().firstChild.lastChild.getAttribute('class')).toEqual('operators');
1279
1280 // Remove inner group and flatten
1281 KorAP._delete.bind(
1282 vc.root().getOperand(2).getOperand(0).element().lastChild.lastChild
1283 ).apply();
1284
1285 expect(vc.toQuery()).toEqual(
1286 'pubDate in 2014-12-05 | foo = "bar" | title = "Hello World!" | yeah = "juhu"'
1287 );
1288 expect(vc.root().ldType()).toEqual('docGroup');
1289 expect(vc.root().operation()).toEqual('or');
1290
1291 // Operands and operators
1292 expect(vc.element().firstChild.children.length).toEqual(5);
1293 expect(vc.element().firstChild.lastChild.getAttribute('class')).toEqual('operators');
1294 });
1295});
1296
1297describe('KorAP._add (event)', function () {
1298 var complexVCFactory = buildFactory(KorAP.VirtualCollection,{
1299 "@type": 'korap:docGroup',
1300 'operation' : 'operation:and',
1301 'operands' : [
1302 {
1303 "@type": 'korap:doc',
1304 "key": 'pubDate',
1305 "match": 'match:eq',
1306 "value": '2014-12-05',
1307 "type": 'type:date'
1308 },
1309 {
1310 "@type" : 'korap:docGroup',
1311 'operation' : 'operation:or',
1312 'operands' : [
1313 {
1314 '@type' : 'korap:doc',
1315 'key' : 'title',
1316 'value' : 'Hello World!'
1317 },
1318 {
1319 '@type' : 'korap:doc',
1320 'key' : 'foo',
1321 'value' : 'bar'
1322 }
1323 ]
1324 }
1325 ]
1326 });
1327
1328 it ('should add new unspecified doc with "and"', function () {
1329 var vc = KorAP.VirtualCollection.render(
1330 {
1331 "@type": 'korap:docGroup',
1332 'operation' : 'operation:and',
1333 'operands' : [
1334 {
1335 "@type": 'korap:doc',
1336 "key": 'pubDate',
1337 "match": 'match:eq',
1338 "value": '2014-12-05',
1339 "type": 'type:date'
1340 },
1341 {
1342 "@type" : 'korap:doc',
1343 'key' : 'foo',
1344 'value' : 'bar'
1345 }
1346 ]
1347 }
1348 );
1349
1350 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1351
1352 var fc = vc.element().firstChild;
1353 expect(fc.getAttribute('data-operation')).toEqual('and');
1354 expect(fc.children.length).toEqual(3);
1355 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1356 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1357 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1358
1359 // add with 'and' in the middle
1360 KorAP._and.bind(vc.root().getOperand(0).element().lastChild.lastChild).apply();
1361 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1362
1363 fc = vc.element().firstChild;
1364 expect(fc.getAttribute('data-operation')).toEqual('and');
1365 expect(fc.children.length).toEqual(4);
1366 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1367
1368 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1369 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
1370 expect(fc.children[2].getAttribute('class')).toEqual('doc');
1371 });
1372
1373 it ('should add new unspecified doc with "or"', function () {
1374 var vc = KorAP.VirtualCollection.render(
1375 {
1376 "@type": 'korap:docGroup',
1377 'operation' : 'operation:and',
1378 'operands' : [
1379 {
1380 "@type": 'korap:doc',
1381 "key": 'pubDate',
1382 "match": 'match:eq',
1383 "value": '2014-12-05',
1384 "type": 'type:date'
1385 },
1386 {
1387 "@type" : 'korap:doc',
1388 'key' : 'foo',
1389 'value' : 'bar'
1390 }
1391 ]
1392 }
1393 );
1394
1395 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1396
1397 var fc = vc.element().firstChild;
1398 expect(fc.children.length).toEqual(3);
1399 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1400 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1401 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1402
1403 // add with 'or' in the middle
1404 KorAP._or.bind(vc.root().getOperand(0).element().lastChild.lastChild).apply();
1405 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1406 fc = vc.element().firstChild;
1407
1408 expect(fc.getAttribute('data-operation')).toEqual('and');
1409 expect(fc.children.length).toEqual(3);
1410 expect(fc.children[0].getAttribute('class')).toEqual('docGroup');
1411 expect(fc.children[0].getAttribute('data-operation')).toEqual('or');
1412 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1413 expect(fc.children[2].getAttribute('class')).toEqual('operators');
1414 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1415
1416 fc = vc.element().firstChild.firstChild;
1417 expect(fc.children.length).toEqual(3);
1418 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1419 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
1420 expect(fc.children[2].getAttribute('class')).toEqual('operators');
1421 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1422 });
1423
1424 // Todo: wrap on root!!
1425});
1426
1427/*
1428 Todo: test event sequences:
1429 - In a nested group with a 'doc' and a 'non', remove the 'doc',
1430 so the 'non' needs to be flattened!
1431*/