blob: 4223e463bea1b2a5d1cd01edef96bfe9480e300a [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();
240 expect(doc.toString()).toEqual("");
241
242 // Serialize string
243 doc = stringFactory.create();
244 expect(doc.toString()).toEqual('author = "Max Birkendale"');
245
246 // Serialize string with quotes
247 doc = stringFactory.create({ "value" : 'Max "Der Coole" Birkendate'});
248 expect(doc.toString()).toEqual('author = "Max \\"Der Coole\\" Birkendate"');
249
250 // Serialize regex
251 doc = regexFactory.create();
252 expect(doc.toString()).toEqual('title = /[^b]ee.+?/');
253
254 doc = regexFactory.create({
255 match: "match:ne"
256 });
257 expect(doc.toString()).toEqual('title != /[^b]ee.+?/');
258
259 doc = dateFactory.create();
260 expect(doc.toString()).toEqual('pubDate in 2014-11-05');
261
262 doc = dateFactory.create({
263 value : "2014"
264 });
265 expect(doc.toString()).toEqual('pubDate in 2014');
266 });
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();
414 expect(docGroup.toString()).toEqual('author = "Max Birkendale" & pubDate in 2014-12-05');
415
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 });
456 expect(docGroup.toString()).toEqual(
457 '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 () {
464 var docElement = KorAP.UnspecifiedDoc.create().element();
465 expect(docElement.getAttribute('class')).toEqual('unspecified');
466 expect(docElement.firstChild.firstChild.data).toEqual('⋯');
467 expect(docElement.lastChild.getAttribute('class')).toEqual('operators');
468
469 // Not removable
470 expect(docElement.lastChild.children.length).toEqual(0);
471 });
472
473 it('should be removable, when no root', function () {
474 var docGroup = KorAP.DocGroup.create();
475 docGroup.operation('or');
476 expect(docGroup.operation()).toEqual('or');
477
478 docGroup.append({
479 "@type": 'korap:doc',
480 "key": 'pubDate',
481 "match": 'match:eq',
482 "value": '2014-12-05',
483 "type": 'type:date'
484 });
485
Nils Diewaldf219eb82015-01-07 20:15:42 +0000486 // Add unspecified object
487 docGroup.append();
488
Nils Diewald966abf12014-12-20 02:27:45 +0000489 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
490 expect(docGroup.element().children[0].getAttribute('class')).toEqual('doc');
491
Nils Diewald966abf12014-12-20 02:27:45 +0000492 var unspec = docGroup.element().children[1];
493 expect(unspec.getAttribute('class')).toEqual('unspecified');
494
495 // Removable
496 expect(unspec.lastChild.children.length).toEqual(1);
497 expect(unspec.lastChild.children[0].getAttribute('class')).toEqual('delete');
498 });
499});
500
Nils Diewald8f4e2542014-12-19 04:42:09 +0000501describe('KorAP.Doc element', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000502 it('should be initializable', function () {
Nils Diewald8f4e2542014-12-19 04:42:09 +0000503 var docElement = KorAP.Doc.create(undefined, {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000504 "@type" : "korap:doc",
505 "key":"Titel",
506 "value":"Baum",
507 "match":"match:eq"
508 });
509 expect(docElement.key()).toEqual('Titel');
510 expect(docElement.matchop()).toEqual('eq');
511 expect(docElement.value()).toEqual('Baum');
512
513 var docE = docElement.element();
514 expect(docE.children[0].firstChild.data).toEqual('Titel');
515 expect(docE.children[1].firstChild.data).toEqual('eq');
516 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
517 expect(docE.children[2].firstChild.data).toEqual('Baum');
518 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
519
520 expect(docElement.toJson()).toEqual(jasmine.objectContaining({
521 "@type" : "korap:doc",
522 "key":"Titel",
523 "value":"Baum",
524 "match":"match:eq"
525 }));
526 });
527});
528
Nils Diewald8f4e2542014-12-19 04:42:09 +0000529describe('KorAP.DocGroup element', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000530 it('should be initializable', function () {
531
Nils Diewald8f4e2542014-12-19 04:42:09 +0000532 var docGroup = KorAP.DocGroup.create(undefined, {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000533 "@type" : "korap:docGroup",
534 "operation" : "operation:and",
535 "operands" : [
536 {
537 "@type": 'korap:doc',
538 "key" : 'author',
539 "match": 'match:eq',
540 "value": 'Max Birkendale',
541 "type": 'type:string'
542 },
543 {
544 "@type": 'korap:doc',
545 "key": 'pubDate',
546 "match": 'match:eq',
547 "value": '2014-12-05',
548 "type": 'type:date'
549 }
550 ]
551 });
552
Nils Diewald8f4e2542014-12-19 04:42:09 +0000553 expect(docGroup.operation()).toEqual('and');
554 var e = docGroup.element();
Nils Diewald0b6c0412014-12-19 03:55:57 +0000555 expect(e.getAttribute('class')).toEqual('docGroup');
556 expect(e.getAttribute('data-operation')).toEqual('and');
557
558 var first = e.children[0];
559 expect(first.getAttribute('class')).toEqual('doc');
560 expect(first.children[0].getAttribute('class')).toEqual('key');
561 expect(first.children[1].getAttribute('class')).toEqual('match');
562 expect(first.children[2].getAttribute('class')).toEqual('value');
563 expect(first.children[2].getAttribute('data-type')).toEqual('string');
564 expect(first.children[0].firstChild.data).toEqual('author');
565 expect(first.children[1].firstChild.data).toEqual('eq');
566 expect(first.children[2].firstChild.data).toEqual('Max Birkendale');
567
568 var second = e.children[1];
569 expect(second.getAttribute('class')).toEqual('doc');
570 expect(second.children[0].getAttribute('class')).toEqual('key');
571 expect(second.children[1].getAttribute('class')).toEqual('match');
572 expect(second.children[2].getAttribute('class')).toEqual('value');
573 expect(second.children[2].getAttribute('data-type')).toEqual('date');
574 expect(second.children[0].firstChild.data).toEqual('pubDate');
575 expect(second.children[1].firstChild.data).toEqual('eq');
576 expect(second.children[2].firstChild.data).toEqual('2014-12-05');
577
578 });
579
580 it('should be deserializable with nested groups', function () {
Nils Diewald8f4e2542014-12-19 04:42:09 +0000581 var docGroup = KorAP.DocGroup.create(undefined, {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000582 "@type" : "korap:docGroup",
583 "operation" : "operation:or",
584 "operands" : [
585 {
586 "@type": 'korap:doc',
587 "key" : 'author',
588 "match": 'match:eq',
589 "value": 'Max Birkendale',
590 "type": 'type:string'
591 },
592 {
593 "@type" : "korap:docGroup",
594 "operation" : "operation:and",
595 "operands" : [
596 {
597 "@type": 'korap:doc',
598 "key": 'pubDate',
599 "match": 'match:geq',
600 "value": '2014-05-12',
601 "type": 'type:date'
602 },
603 {
604 "@type": 'korap:doc',
605 "key": 'pubDate',
606 "match": 'match:leq',
607 "value": '2014-12-05',
608 "type": 'type:date'
609 }
610 ]
611 }
612 ]
613 });
614
Nils Diewald8f4e2542014-12-19 04:42:09 +0000615 expect(docGroup.operation()).toEqual('or');
616 var e = docGroup.element();
Nils Diewald0b6c0412014-12-19 03:55:57 +0000617 expect(e.getAttribute('class')).toEqual('docGroup');
618 expect(e.getAttribute('data-operation')).toEqual('or');
619
Nils Diewald966abf12014-12-20 02:27:45 +0000620 expect(e.children[0].getAttribute('class')).toEqual('doc');
621 var docop = e.children[0].lastChild;
622 expect(docop.getAttribute('class')).toEqual('operators');
623 expect(docop.children[0].getAttribute('class')).toEqual('and');
624 expect(docop.children[1].getAttribute('class')).toEqual('or');
625 expect(docop.children[2].getAttribute('class')).toEqual('delete');
626
627 expect(e.children[1].getAttribute('class')).toEqual('docGroup');
628 expect(e.children[1].getAttribute('data-operation')).toEqual('and');
629
630 // This and-operation can be "or"ed or "delete"d
631 var secop = e.children[1].children[2];
632 expect(secop.getAttribute('class')).toEqual('operators');
633 expect(secop.children[0].getAttribute('class')).toEqual('or');
634 expect(secop.children[1].getAttribute('class')).toEqual('delete');
635
636 // This or-operation can be "and"ed or "delete"d
637 expect(e.children[2].getAttribute('class')).toEqual('operators');
638 expect(e.lastChild.getAttribute('class')).toEqual('operators');
639 expect(e.lastChild.children[0].getAttribute('class')).toEqual('and');
640 expect(e.lastChild.children[1].getAttribute('class')).toEqual('delete');
Nils Diewald0b6c0412014-12-19 03:55:57 +0000641
642 });
643});
644
645describe('KorAP.VirtualCollection', function () {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000646
647 var simpleGroupFactory = buildFactory(KorAP.DocGroup, {
648 "@type" : "korap:docGroup",
649 "operation" : "operation:and",
650 "operands" : [
651 {
652 "@type": 'korap:doc',
653 "key" : 'author',
654 "match": 'match:eq',
655 "value": 'Max Birkendale',
656 "type": 'type:string'
657 },
658 {
659 "@type": 'korap:doc',
660 "key": 'pubDate',
661 "match": 'match:eq',
662 "value": '2014-12-05',
663 "type": 'type:date'
664 }
665 ]
666 });
667
668 var nestedGroupFactory = buildFactory(KorAP.VirtualCollection, {
669 "@type" : "korap:docGroup",
670 "operation" : "operation:or",
671 "operands" : [
672 {
673 "@type": 'korap:doc',
674 "key" : 'author',
675 "match": 'match:eq',
676 "value": 'Max Birkendale',
677 "type": 'type:string'
678 },
679 {
680 "@type" : "korap:docGroup",
681 "operation" : "operation:and",
682 "operands" : [
683 {
684 "@type": 'korap:doc',
685 "key": 'pubDate',
686 "match": 'match:geq',
687 "value": '2014-05-12',
688 "type": 'type:date'
689 },
690 {
691 "@type": 'korap:doc',
692 "key": 'pubDate',
693 "match": 'match:leq',
694 "value": '2014-12-05',
695 "type": 'type:date'
696 }
697 ]
698 }
699 ]
700 });
701
702 var flatGroupFactory = buildFactory(KorAP.VirtualCollection, {
703 "@type" : "korap:docGroup",
704 "operation" : "operation:and",
705 "operands" : [
706 {
707 "@type": 'korap:doc',
708 "key": 'pubDate',
709 "match": 'match:geq',
710 "value": '2014-05-12',
711 "type": 'type:date'
712 },
713 {
714 "@type": 'korap:doc',
715 "key": 'pubDate',
716 "match": 'match:leq',
717 "value": '2014-12-05',
718 "type": 'type:date'
719 },
720 {
721 "@type": 'korap:doc',
722 "key": 'foo',
723 "match": 'match:eq',
724 "value": 'bar',
725 "type": 'type:string'
726 }
727 ]
728 });
729
Nils Diewald0b6c0412014-12-19 03:55:57 +0000730 it('should be initializable', function () {
731 var vc = KorAP.VirtualCollection.render();
732 expect(vc.element().getAttribute('class')).toEqual('vc');
Nils Diewald966abf12014-12-20 02:27:45 +0000733 expect(vc.root().element().getAttribute('class')).toEqual('unspecified');
734
735 // Not removable
736 expect(vc.root().element().lastChild.children.length).toEqual(0);
Nils Diewald0b6c0412014-12-19 03:55:57 +0000737 });
738
739 it('should be based on a doc', function () {
740 var vc = KorAP.VirtualCollection.render({
741 "@type" : "korap:doc",
742 "key":"Titel",
743 "value":"Baum",
744 "match":"match:eq"
745 });
746
747 expect(vc.element().getAttribute('class')).toEqual('vc');
748 expect(vc.root().element().getAttribute('class')).toEqual('doc');
749 expect(vc.root().key()).toEqual('Titel');
750 expect(vc.root().value()).toEqual('Baum');
751 expect(vc.root().matchop()).toEqual('eq');
752
753 var docE = vc.root().element();
754 expect(docE.children[0].firstChild.data).toEqual('Titel');
755 expect(docE.children[1].firstChild.data).toEqual('eq');
756 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
757 expect(docE.children[2].firstChild.data).toEqual('Baum');
758 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
759 });
760
761 it('should be based on a docGroup', function () {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000762 var vc = KorAP.VirtualCollection.render(simpleGroupFactory.create().toJson());
Nils Diewald0b6c0412014-12-19 03:55:57 +0000763
764 expect(vc.element().getAttribute('class')).toEqual('vc');
765 expect(vc.root().element().getAttribute('class')).toEqual('docGroup');
766 expect(vc.root().operation()).toEqual('and');
767
768 var docGroup = vc.root();
769
770 var first = docGroup.getOperand(0);
771 expect(first.key()).toEqual('author');
772 expect(first.value()).toEqual('Max Birkendale');
773 expect(first.matchop()).toEqual('eq');
774
775 var second = docGroup.getOperand(1);
776 expect(second.key()).toEqual('pubDate');
777 expect(second.value()).toEqual('2014-12-05');
778 expect(second.matchop()).toEqual('eq');
779 });
780
Nils Diewald5c817a42015-01-06 01:08:56 +0000781
Nils Diewald966abf12014-12-20 02:27:45 +0000782 it('should be based on a nested docGroup', function () {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000783 var vc = nestedGroupFactory.create();
784
Nils Diewald5c817a42015-01-06 01:08:56 +0000785 expect(vc.element().getAttribute('class')).toEqual('vc');
786 expect(vc.element().firstChild.getAttribute('class')).toEqual('docGroup');
787 expect(vc.element().firstChild.children[0].getAttribute('class')).toEqual('doc');
788 var dg = vc.element().firstChild.children[1];
789 expect(dg.getAttribute('class')).toEqual('docGroup');
790 expect(dg.children[0].getAttribute('class')).toEqual('doc');
791 expect(dg.children[1].getAttribute('class')).toEqual('doc');
792 expect(dg.children[2].getAttribute('class')).toEqual('operators');
793 expect(vc.element().firstChild.children[2].getAttribute('class')).toEqual('operators');
794 });
795
Nils Diewaldf219eb82015-01-07 20:15:42 +0000796 it('should be modifiable by deletion in flat docGroups', function () {
797 var vc = flatGroupFactory.create();
Nils Diewald0297ba12015-01-05 21:56:12 +0000798 var docGroup = vc.root();
Nils Diewaldf219eb82015-01-07 20:15:42 +0000799
800 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
801
Nils Diewald0297ba12015-01-05 21:56:12 +0000802 var doc = docGroup.getOperand(1);
803 expect(doc.key()).toEqual("pubDate");
Nils Diewaldf219eb82015-01-07 20:15:42 +0000804 expect(doc.value()).toEqual("2014-12-05");
Nils Diewald5c817a42015-01-06 01:08:56 +0000805
806 // Remove operand 1
Nils Diewaldf219eb82015-01-07 20:15:42 +0000807 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
Nils Diewald5c817a42015-01-06 01:08:56 +0000808 expect(doc._element).toEqual(undefined);
809
Nils Diewald0297ba12015-01-05 21:56:12 +0000810 doc = docGroup.getOperand(1);
811 expect(doc.key()).toEqual("foo");
Nils Diewald0297ba12015-01-05 21:56:12 +0000812
Nils Diewald5c817a42015-01-06 01:08:56 +0000813 // Remove operand 1
Nils Diewaldf219eb82015-01-07 20:15:42 +0000814 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
Nils Diewald5c817a42015-01-06 01:08:56 +0000815 expect(doc._element).toEqual(undefined);
Nils Diewald0297ba12015-01-05 21:56:12 +0000816
Nils Diewaldf219eb82015-01-07 20:15:42 +0000817 // Only one operand left ...
Nils Diewald5c817a42015-01-06 01:08:56 +0000818 expect(docGroup.getOperand(1)).toBeUndefined();
Nils Diewaldf219eb82015-01-07 20:15:42 +0000819 // ... but there shouldn't be a group anymore at all!
820 expect(docGroup.getOperand(0)).toBeUndefined();
821
822 var obj = vc.root();
823 expect(obj.ldType()).toEqual("doc");
824 expect(obj.key()).toEqual("pubDate");
825 expect(obj.value()).toEqual("2014-05-12");
826
827 expect(obj.element().getAttribute('class')).toEqual('doc');
Nils Diewald5c817a42015-01-06 01:08:56 +0000828 });
Nils Diewaldf219eb82015-01-07 20:15:42 +0000829
830
831 it('should be modifiable by deletion in nested docGroups (root case)', function () {
832 var vc = nestedGroupFactory.create();
833
Nils Diewald8e7182e2015-01-08 15:02:07 +0000834 expect(vc.toString()).toEqual(
835 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05)'
836 );
837
Nils Diewaldf219eb82015-01-07 20:15:42 +0000838 var docGroup = vc.root();
839 expect(docGroup.ldType()).toEqual("docGroup");
840 expect(docGroup.operation()).toEqual("or");
841
842 var doc = docGroup.getOperand(0);
843 expect(doc.key()).toEqual("author");
844 expect(doc.value()).toEqual("Max Birkendale");
845
846 docGroup = docGroup.getOperand(1);
847 expect(docGroup.operation()).toEqual("and");
848
849 doc = docGroup.getOperand(0);
850 expect(doc.key()).toEqual("pubDate");
851 expect(doc.matchop()).toEqual("geq");
852 expect(doc.value()).toEqual("2014-05-12");
853 expect(doc.type()).toEqual("date");
854
855 doc = docGroup.getOperand(1);
856 expect(doc.key()).toEqual("pubDate");
857 expect(doc.matchop()).toEqual("leq");
858 expect(doc.value()).toEqual("2014-12-05");
859 expect(doc.type()).toEqual("date");
860
861 // Remove first operand so everything becomes root
862 expect(
863 vc.root().delOperand(
864 vc.root().getOperand(0)
865 ).update().ldType()
866 ).toEqual("docGroup");
Nils Diewald8e7182e2015-01-08 15:02:07 +0000867
Nils Diewaldf219eb82015-01-07 20:15:42 +0000868 expect(vc.root().ldType()).toEqual("docGroup");
869 expect(vc.root().operation()).toEqual("and");
Nils Diewald8e7182e2015-01-08 15:02:07 +0000870 expect(vc.root().getOperand(0).ldType()).toEqual("doc");
871
872 expect(vc.toString()).toEqual(
873 'pubDate since 2014-05-12 & pubDate until 2014-12-05'
874 );
875
Nils Diewaldf219eb82015-01-07 20:15:42 +0000876 });
877
878 it('should be modifiable by deletion in nested docGroups (resolve group case)', function () {
879 var vc = nestedGroupFactory.create();
880
881 // Get nested group
882 var firstGroup = vc.root().getOperand(1);
883 firstGroup.append(simpleGroupFactory.create({ "operation" : "operation:or" }));
884
885 // Structur is now:
886 // or(doc, and(doc, doc, or(doc, doc)))
887
888 // Get nested or in and
889 var orGroup = vc.root().getOperand(1).getOperand(2);
890 expect(orGroup.ldType()).toEqual("docGroup");
891 expect(orGroup.operation()).toEqual("or");
892
893 // Remove
894 // Structur is now:
895 // or(doc, and(doc, doc, doc)))
896 expect(orGroup.delOperand(orGroup.getOperand(0)).update().operation()).toEqual("and");
897 expect(vc.root().getOperand(1).operands().length).toEqual(3);
898 });
899
900 it('should be modifiable by deletion in nested docGroups (identical group case)', function () {
901 var vc = nestedGroupFactory.create();
902
903 // Get nested group
904 var firstGroup = vc.root().getOperand(1);
Nils Diewald8e7182e2015-01-08 15:02:07 +0000905 firstGroup.append(simpleGroupFactory.create({
906 "operation" : "operation:or"
907 }));
908 var oldAuthor = firstGroup.getOperand(2).getOperand(0);
909 oldAuthor.key("title");
910 oldAuthor.value("Der Birnbaum");
Nils Diewaldf219eb82015-01-07 20:15:42 +0000911
912 // Structur is now:
913 // or(doc, and(doc, doc, or(doc, doc)))
914 expect(vc.toString()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +0000915 '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 +0000916 );
917
918 var andGroup = vc.root().getOperand(1);
919
920 // Get leading docs in and
921 var doc1 = andGroup.getOperand(0);
922 expect(doc1.ldType()).toEqual("doc");
923 expect(doc1.value()).toEqual("2014-05-12");
924 var doc2 = andGroup.getOperand(1);
925 expect(doc2.ldType()).toEqual("doc");
926 expect(doc2.value()).toEqual("2014-12-05");
927
928 // Remove 2
929 expect(andGroup.delOperand(doc2).update().operation()).toEqual("and");
930 // Structur is now:
931 // or(doc, and(doc, or(doc, doc)))
932
933 expect(vc.toString()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +0000934 'author = "Max Birkendale" | (pubDate since 2014-05-12 & (title = "Der Birnbaum" | pubDate in 2014-12-05))'
Nils Diewaldf219eb82015-01-07 20:15:42 +0000935 );
936
937
938 // Remove 1
939 expect(andGroup.delOperand(doc1).update().operation()).toEqual("or");
940 // Structur is now:
941 // or(doc, doc, doc)
942
943 expect(vc.toString()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +0000944 'author = "Max Birkendale" | title = "Der Birnbaum" | pubDate in 2014-12-05'
Nils Diewaldf219eb82015-01-07 20:15:42 +0000945 );
946 });
Nils Diewald0b6c0412014-12-19 03:55:57 +0000947});
948
949describe('KorAP.Operators', function () {
950 it('should be initializable', function () {
951 var op = KorAP.Operators.create(true, false, false);
952 expect(op.and()).toBeTruthy();
953 expect(op.or()).not.toBeTruthy();
954 expect(op.del()).not.toBeTruthy();
955
956 op.and(false);
957 expect(op.and()).not.toBeTruthy();
958 expect(op.or()).not.toBeTruthy();
959 expect(op.del()).not.toBeTruthy();
960
961 op.or(true);
962 op.del(true);
963 expect(op.and()).not.toBeTruthy();
964 expect(op.or()).toBeTruthy();
965 expect(op.del()).toBeTruthy();
966
967 var e = op.element();
968 expect(e.getAttribute('class')).toEqual('operators');
969 expect(e.children[0].getAttribute('class')).toEqual('or');
970 expect(e.children[0].firstChild.data).toEqual('or');
971 expect(e.children[1].getAttribute('class')).toEqual('delete');
972 expect(e.children[1].firstChild.data).toEqual('×');
973
974 op.and(true);
975 op.del(false);
976 op.update();
977
978 e = op.element();
979 expect(e.getAttribute('class')).toEqual('operators');
980 expect(e.children[0].getAttribute('class')).toEqual('and');
981 expect(e.children[0].firstChild.data).toEqual('and');
982 expect(e.children[1].getAttribute('class')).toEqual('or');
983 expect(e.children[1].firstChild.data).toEqual('or');
984 });
985});