blob: c532d329efab312613ba29f6cca84a331eb0ca9e [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 () {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000464 var doc = KorAP.UnspecifiedDoc.create();
465 var docElement = doc.element();
Nils Diewald966abf12014-12-20 02:27:45 +0000466 expect(docElement.getAttribute('class')).toEqual('unspecified');
467 expect(docElement.firstChild.firstChild.data).toEqual('⋯');
468 expect(docElement.lastChild.getAttribute('class')).toEqual('operators');
Nils Diewald8f6b6102015-01-08 18:25:33 +0000469 expect(doc.toString()).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];
495 expect(unspec.getAttribute('class')).toEqual('unspecified');
496
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 });
731
Nils Diewald0b6c0412014-12-19 03:55:57 +0000732 it('should be initializable', function () {
733 var vc = KorAP.VirtualCollection.render();
734 expect(vc.element().getAttribute('class')).toEqual('vc');
Nils Diewald966abf12014-12-20 02:27:45 +0000735 expect(vc.root().element().getAttribute('class')).toEqual('unspecified');
736
737 // Not removable
738 expect(vc.root().element().lastChild.children.length).toEqual(0);
Nils Diewald0b6c0412014-12-19 03:55:57 +0000739 });
740
741 it('should be based on a doc', function () {
742 var vc = KorAP.VirtualCollection.render({
743 "@type" : "korap:doc",
744 "key":"Titel",
745 "value":"Baum",
746 "match":"match:eq"
747 });
748
749 expect(vc.element().getAttribute('class')).toEqual('vc');
750 expect(vc.root().element().getAttribute('class')).toEqual('doc');
751 expect(vc.root().key()).toEqual('Titel');
752 expect(vc.root().value()).toEqual('Baum');
753 expect(vc.root().matchop()).toEqual('eq');
754
755 var docE = vc.root().element();
756 expect(docE.children[0].firstChild.data).toEqual('Titel');
757 expect(docE.children[1].firstChild.data).toEqual('eq');
758 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
759 expect(docE.children[2].firstChild.data).toEqual('Baum');
760 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
761 });
762
763 it('should be based on a docGroup', function () {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000764 var vc = KorAP.VirtualCollection.render(simpleGroupFactory.create().toJson());
Nils Diewald0b6c0412014-12-19 03:55:57 +0000765
766 expect(vc.element().getAttribute('class')).toEqual('vc');
767 expect(vc.root().element().getAttribute('class')).toEqual('docGroup');
768 expect(vc.root().operation()).toEqual('and');
769
770 var docGroup = vc.root();
771
772 var first = docGroup.getOperand(0);
773 expect(first.key()).toEqual('author');
774 expect(first.value()).toEqual('Max Birkendale');
775 expect(first.matchop()).toEqual('eq');
776
777 var second = docGroup.getOperand(1);
778 expect(second.key()).toEqual('pubDate');
779 expect(second.value()).toEqual('2014-12-05');
780 expect(second.matchop()).toEqual('eq');
781 });
782
Nils Diewald5c817a42015-01-06 01:08:56 +0000783
Nils Diewald966abf12014-12-20 02:27:45 +0000784 it('should be based on a nested docGroup', function () {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000785 var vc = nestedGroupFactory.create();
786
Nils Diewald5c817a42015-01-06 01:08:56 +0000787 expect(vc.element().getAttribute('class')).toEqual('vc');
788 expect(vc.element().firstChild.getAttribute('class')).toEqual('docGroup');
789 expect(vc.element().firstChild.children[0].getAttribute('class')).toEqual('doc');
790 var dg = vc.element().firstChild.children[1];
791 expect(dg.getAttribute('class')).toEqual('docGroup');
792 expect(dg.children[0].getAttribute('class')).toEqual('doc');
793 expect(dg.children[1].getAttribute('class')).toEqual('doc');
794 expect(dg.children[2].getAttribute('class')).toEqual('operators');
795 expect(vc.element().firstChild.children[2].getAttribute('class')).toEqual('operators');
796 });
797
Nils Diewaldf219eb82015-01-07 20:15:42 +0000798 it('should be modifiable by deletion in flat docGroups', function () {
799 var vc = flatGroupFactory.create();
Nils Diewald0297ba12015-01-05 21:56:12 +0000800 var docGroup = vc.root();
Nils Diewaldf219eb82015-01-07 20:15:42 +0000801
802 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
803
Nils Diewald0297ba12015-01-05 21:56:12 +0000804 var doc = docGroup.getOperand(1);
805 expect(doc.key()).toEqual("pubDate");
Nils Diewaldf219eb82015-01-07 20:15:42 +0000806 expect(doc.value()).toEqual("2014-12-05");
Nils Diewald5c817a42015-01-06 01:08:56 +0000807
808 // Remove operand 1
Nils Diewaldf219eb82015-01-07 20:15:42 +0000809 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
Nils Diewald5c817a42015-01-06 01:08:56 +0000810 expect(doc._element).toEqual(undefined);
811
Nils Diewald0297ba12015-01-05 21:56:12 +0000812 doc = docGroup.getOperand(1);
813 expect(doc.key()).toEqual("foo");
Nils Diewald0297ba12015-01-05 21:56:12 +0000814
Nils Diewald5c817a42015-01-06 01:08:56 +0000815 // Remove operand 1
Nils Diewaldf219eb82015-01-07 20:15:42 +0000816 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
Nils Diewald5c817a42015-01-06 01:08:56 +0000817 expect(doc._element).toEqual(undefined);
Nils Diewald0297ba12015-01-05 21:56:12 +0000818
Nils Diewaldf219eb82015-01-07 20:15:42 +0000819 // Only one operand left ...
Nils Diewald5c817a42015-01-06 01:08:56 +0000820 expect(docGroup.getOperand(1)).toBeUndefined();
Nils Diewaldf219eb82015-01-07 20:15:42 +0000821 // ... but there shouldn't be a group anymore at all!
822 expect(docGroup.getOperand(0)).toBeUndefined();
823
824 var obj = vc.root();
825 expect(obj.ldType()).toEqual("doc");
826 expect(obj.key()).toEqual("pubDate");
827 expect(obj.value()).toEqual("2014-05-12");
828
829 expect(obj.element().getAttribute('class')).toEqual('doc');
Nils Diewald5c817a42015-01-06 01:08:56 +0000830 });
Nils Diewaldf219eb82015-01-07 20:15:42 +0000831
832
833 it('should be modifiable by deletion in nested docGroups (root case)', function () {
834 var vc = nestedGroupFactory.create();
835
Nils Diewald8e7182e2015-01-08 15:02:07 +0000836 expect(vc.toString()).toEqual(
837 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05)'
838 );
839
Nils Diewaldf219eb82015-01-07 20:15:42 +0000840 var docGroup = vc.root();
841 expect(docGroup.ldType()).toEqual("docGroup");
842 expect(docGroup.operation()).toEqual("or");
843
844 var doc = docGroup.getOperand(0);
845 expect(doc.key()).toEqual("author");
846 expect(doc.value()).toEqual("Max Birkendale");
847
848 docGroup = docGroup.getOperand(1);
849 expect(docGroup.operation()).toEqual("and");
850
851 doc = docGroup.getOperand(0);
852 expect(doc.key()).toEqual("pubDate");
853 expect(doc.matchop()).toEqual("geq");
854 expect(doc.value()).toEqual("2014-05-12");
855 expect(doc.type()).toEqual("date");
856
857 doc = docGroup.getOperand(1);
858 expect(doc.key()).toEqual("pubDate");
859 expect(doc.matchop()).toEqual("leq");
860 expect(doc.value()).toEqual("2014-12-05");
861 expect(doc.type()).toEqual("date");
862
863 // Remove first operand so everything becomes root
864 expect(
865 vc.root().delOperand(
866 vc.root().getOperand(0)
867 ).update().ldType()
868 ).toEqual("docGroup");
Nils Diewald8e7182e2015-01-08 15:02:07 +0000869
Nils Diewaldf219eb82015-01-07 20:15:42 +0000870 expect(vc.root().ldType()).toEqual("docGroup");
871 expect(vc.root().operation()).toEqual("and");
Nils Diewald8e7182e2015-01-08 15:02:07 +0000872 expect(vc.root().getOperand(0).ldType()).toEqual("doc");
873
874 expect(vc.toString()).toEqual(
875 'pubDate since 2014-05-12 & pubDate until 2014-12-05'
876 );
Nils Diewaldf219eb82015-01-07 20:15:42 +0000877 });
878
879 it('should be modifiable by deletion in nested docGroups (resolve group case)', function () {
880 var vc = nestedGroupFactory.create();
881
882 // Get nested group
883 var firstGroup = vc.root().getOperand(1);
884 firstGroup.append(simpleGroupFactory.create({ "operation" : "operation:or" }));
885
886 // Structur is now:
887 // or(doc, and(doc, doc, or(doc, doc)))
888
889 // Get nested or in and
890 var orGroup = vc.root().getOperand(1).getOperand(2);
891 expect(orGroup.ldType()).toEqual("docGroup");
892 expect(orGroup.operation()).toEqual("or");
893
894 // Remove
895 // Structur is now:
896 // or(doc, and(doc, doc, doc)))
897 expect(orGroup.delOperand(orGroup.getOperand(0)).update().operation()).toEqual("and");
898 expect(vc.root().getOperand(1).operands().length).toEqual(3);
899 });
900
901 it('should be modifiable by deletion in nested docGroups (identical group case)', function () {
902 var vc = nestedGroupFactory.create();
903
904 // Get nested group
905 var firstGroup = vc.root().getOperand(1);
Nils Diewald8e7182e2015-01-08 15:02:07 +0000906 firstGroup.append(simpleGroupFactory.create({
907 "operation" : "operation:or"
908 }));
909 var oldAuthor = firstGroup.getOperand(2).getOperand(0);
910 oldAuthor.key("title");
911 oldAuthor.value("Der Birnbaum");
Nils Diewaldf219eb82015-01-07 20:15:42 +0000912
913 // Structur is now:
914 // or(doc, and(doc, doc, or(doc, doc)))
915 expect(vc.toString()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +0000916 '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 +0000917 );
918
919 var andGroup = vc.root().getOperand(1);
920
921 // Get leading docs in and
922 var doc1 = andGroup.getOperand(0);
923 expect(doc1.ldType()).toEqual("doc");
924 expect(doc1.value()).toEqual("2014-05-12");
925 var doc2 = andGroup.getOperand(1);
926 expect(doc2.ldType()).toEqual("doc");
927 expect(doc2.value()).toEqual("2014-12-05");
928
929 // Remove 2
930 expect(andGroup.delOperand(doc2).update().operation()).toEqual("and");
931 // Structur is now:
932 // or(doc, and(doc, or(doc, doc)))
933
934 expect(vc.toString()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +0000935 'author = "Max Birkendale" | (pubDate since 2014-05-12 & (title = "Der Birnbaum" | pubDate in 2014-12-05))'
Nils Diewaldf219eb82015-01-07 20:15:42 +0000936 );
937
938
939 // Remove 1
940 expect(andGroup.delOperand(doc1).update().operation()).toEqual("or");
941 // Structur is now:
942 // or(doc, doc, doc)
943
944 expect(vc.toString()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +0000945 'author = "Max Birkendale" | title = "Der Birnbaum" | pubDate in 2014-12-05'
Nils Diewaldf219eb82015-01-07 20:15:42 +0000946 );
947 });
Nils Diewald0b6c0412014-12-19 03:55:57 +0000948});
949
950describe('KorAP.Operators', function () {
951 it('should be initializable', function () {
952 var op = KorAP.Operators.create(true, false, false);
953 expect(op.and()).toBeTruthy();
954 expect(op.or()).not.toBeTruthy();
955 expect(op.del()).not.toBeTruthy();
956
957 op.and(false);
958 expect(op.and()).not.toBeTruthy();
959 expect(op.or()).not.toBeTruthy();
960 expect(op.del()).not.toBeTruthy();
961
962 op.or(true);
963 op.del(true);
964 expect(op.and()).not.toBeTruthy();
965 expect(op.or()).toBeTruthy();
966 expect(op.del()).toBeTruthy();
967
968 var e = op.element();
969 expect(e.getAttribute('class')).toEqual('operators');
970 expect(e.children[0].getAttribute('class')).toEqual('or');
971 expect(e.children[0].firstChild.data).toEqual('or');
972 expect(e.children[1].getAttribute('class')).toEqual('delete');
973 expect(e.children[1].firstChild.data).toEqual('×');
974
975 op.and(true);
976 op.del(false);
977 op.update();
978
979 e = op.element();
980 expect(e.getAttribute('class')).toEqual('operators');
981 expect(e.children[0].getAttribute('class')).toEqual('and');
982 expect(e.children[0].firstChild.data).toEqual('and');
983 expect(e.children[1].getAttribute('class')).toEqual('or');
984 expect(e.children[1].firstChild.data).toEqual('or');
985 });
986});