blob: 88b47347213b3ee077ee520a91dd125cd110445c [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
Nils Diewald52f7eb12015-01-12 17:30:04 +000020function _andOn (obj) {
21 KorAP._and.bind(obj.element().lastChild.firstChild).apply();
22};
23
24function _orOn (obj) {
25 KorAP._or.bind(obj.element().lastChild.firstChild).apply();
26};
27
28function _delOn (obj) {
29 KorAP._delete.bind(obj.element().lastChild.firstChild).apply();
30};
31
32var demoFactory = buildFactory(KorAP.VirtualCollection, {
33 "@type":"korap:docGroup",
34 "operation":"operation:or",
35 "operands":[
36 {
37 "@type":"korap:docGroup",
38 "operation":"operation:and",
39 "operands":[
40 {
41 "@type":"korap:doc",
42 "key":"Titel",
43 "value":"Baum",
44 "match":"match:eq"
45 },
46 {
47 "@type":"korap:doc",
48 "key":"Veröffentlichungsort",
49 "value":"hihi",
50 "match":"match:eq"
51 },
52 {
53 "@type":"korap:docGroup",
54 "operation":"operation:or",
55 "operands":[
56 {
57 "@type":"korap:doc",
58 "key":"Titel",
59 "value":"Baum",
60 "match":"match:eq"
61 },
62 {
63 "@type":"korap:doc",
64 "key":"Veröffentlichungsort",
65 "value":"hihi",
66 "match":"match:eq"
67 }
68 ]
69 }
70 ]
71 },
72 {
73 "@type":"korap:doc",
74 "key":"Untertitel",
75 "value":"huhu",
76 "match":"match:eq"
77 }
78 ]
79});
80
81
Nils Diewald0b6c0412014-12-19 03:55:57 +000082describe('KorAP.Doc', function () {
83
84 // Create example factories
85 var stringFactory = buildFactory(KorAP.Doc, {
86 "key" : "author",
87 "value" : "Max Birkendale",
88 "@type" : "korap:doc"
89 });
90
91 // Create example factories
92 var dateFactory = buildFactory(KorAP.Doc, {
93 "key" : "pubDate",
94 "type" : "type:date",
95 "match" : "match:eq",
96 "value" : "2014-11-05",
97 "@type" : "korap:doc"
98 });
99
100 // Create example factories
101 var regexFactory = buildFactory(KorAP.Doc, {
102 "key" : "title",
103 "type" : "type:regex",
104 "value" : "[^b]ee.+?",
105 "@type" : "korap:doc"
106 });
107
108 it('should be initializable', function () {
109 var doc = KorAP.Doc.create();
110 expect(doc.matchop()).toEqual('eq');
111 expect(doc.key()).toBeUndefined();
112 expect(doc.value()).toBeUndefined();
113 expect(doc.type()).toEqual("string");
114 });
115
116 it('should be definable', function () {
117
118 // Empty doc
119 var doc = KorAP.Doc.create();
120
121 // Set values
122 doc.key("title");
123 doc.value("Der alte Mann");
124 expect(doc.matchop()).toEqual('eq');
125 expect(doc.key()).toEqual("title");
126 expect(doc.type()).toEqual("string");
127 expect(doc.value()).toEqual("Der alte Mann");
128 });
129
130
131 it('should deserialize JSON-LD string', function () {
132 var doc;
133
134 // String default
135 doc = stringFactory.create();
136 expect(doc.matchop()).toEqual('eq');
137 expect(doc.key()).toEqual("author");
138 expect(doc.type()).toEqual("string");
139 expect(doc.value()).toEqual("Max Birkendale");
140
141 // No valid string
142 doc = stringFactory.create({
143 value : undefined
144 });
145 expect(doc).toBeUndefined();
146
147 // No valid string
148 doc = stringFactory.create({
149 value : { "foo" : "bar" }
150 });
151 expect(doc).toBeUndefined();
152
153 // Change match type
154 doc = stringFactory.create({
155 "match" : "match:ne"
156 });
157
158 expect(doc.matchop()).toEqual('ne');
159 expect(doc.key()).toEqual("author");
160 expect(doc.type()).toEqual("string");
161 expect(doc.value()).toEqual("Max Birkendale");
162
163
164 // Invalid match type
165 doc = stringFactory.create({
166 "match" : { "foo" : "bar" }
167 });
168 expect(doc).toBeUndefined();
169 });
170
171 it('should deserialize JSON-LD regex', function () {
172 var doc = regexFactory.create();
173 expect(doc.key()).toEqual("title");
174 expect(doc.type()).toEqual("regex");
175 expect(doc.value()).toEqual("[^b]ee.+?");
176 expect(doc.matchop()).toEqual('eq');
177
178 // change matcher
179 doc = regexFactory.create({
180 match : "match:ne"
181 });
182 expect(doc.matchop()).toEqual('ne');
183
184 // Invalid matcher
185 doc = regexFactory.create({
186 match : "match:chook"
187 });
188 expect(doc).toBeUndefined();
189
190 // Invalid regex
191 doc = regexFactory.create({
192 value : "[^b"
193 });
194 expect(doc).toBeUndefined();
195 });
196
197 it('should deserialize JSON-LD date', function () {
198
199 // Normal date
200 doc = dateFactory.create({});
201
202 expect(doc.matchop()).toEqual('eq');
203 expect(doc.key()).toEqual("pubDate");
204 expect(doc.type()).toEqual("date");
205 expect(doc.value()).toEqual("2014-11-05");
206
207 // Short date 1
208 doc = dateFactory.create({
209 "value" : "2014-11"
210 });
211
212 expect(doc.matchop()).toEqual('eq');
213 expect(doc.key()).toEqual("pubDate");
214 expect(doc.type()).toEqual("date");
215 expect(doc.value()).toEqual("2014-11");
216
217 // Short date 2
218 doc = dateFactory.create({
219 "value" : "2014"
220 });
221
222 expect(doc.matchop()).toEqual('eq');
223 expect(doc.key()).toEqual("pubDate");
224 expect(doc.type()).toEqual("date");
225 expect(doc.value()).toEqual("2014");
226
227 // Invalid date!
228 doc = dateFactory.create({
229 "value" : "2014-11-050"
230 });
231 expect(doc).toBeUndefined();
232
233 // Invalid matcher!
234 doc = dateFactory.create({
235 "match" : "match:ne",
236 });
237 expect(doc).toBeUndefined();
238 });
239
Nils Diewaldf219eb82015-01-07 20:15:42 +0000240 it('should be serializale to JSON', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000241
242 // Empty doc
243 var doc = KorAP.Doc.create();
244 expect(doc.toJson()).toEqual(jasmine.any(Object));
245
246 // Serialize string
247 doc = stringFactory.create();
248 expect(doc.toJson()).toEqual(jasmine.objectContaining({
249 "@type" : "korap:doc",
250 "type" : "type:string",
251 "key" : "author",
252 "value" : "Max Birkendale",
253 "match" : "match:eq"
254 }));
255
256 // Serialize regex
257 doc = regexFactory.create();
258 expect(doc.toJson()).toEqual(jasmine.objectContaining({
259 "@type" : "korap:doc",
260 "type" : "type:regex",
261 "value" : "[^b]ee.+?",
262 "match" : "match:eq",
263 "key" : 'title'
264 }));
265
266 doc = regexFactory.create({
267 match: "match:ne"
268 });
269 expect(doc.toJson()).toEqual(jasmine.objectContaining({
270 "@type" : "korap:doc",
271 "type" : "type:regex",
272 "value" : "[^b]ee.+?",
273 "match" : "match:ne",
274 "key" : 'title'
275 }));
276
277 doc = dateFactory.create();
278 expect(doc.toJson()).toEqual(jasmine.objectContaining({
279 "@type" : "korap:doc",
280 "type" : "type:date",
281 "value" : "2014-11-05",
282 "match" : "match:eq",
283 "key" : 'pubDate'
284 }));
285
286 doc = dateFactory.create({
287 value : "2014"
288 });
289 expect(doc.toJson()).toEqual(jasmine.objectContaining({
290 "@type" : "korap:doc",
291 "type" : "type:date",
292 "value" : "2014",
293 "match" : "match:eq",
294 "key" : 'pubDate'
295 }));
296 });
Nils Diewaldf219eb82015-01-07 20:15:42 +0000297
298 it('should be serializale to String', function () {
299
300 // Empty doc
301 var doc = KorAP.Doc.create();
Nils Diewaldd599d542015-01-08 20:41:34 +0000302 expect(doc.toQuery()).toEqual("");
Nils Diewaldf219eb82015-01-07 20:15:42 +0000303
304 // Serialize string
305 doc = stringFactory.create();
Nils Diewaldd599d542015-01-08 20:41:34 +0000306 expect(doc.toQuery()).toEqual('author = "Max Birkendale"');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000307
308 // Serialize string with quotes
309 doc = stringFactory.create({ "value" : 'Max "Der Coole" Birkendate'});
Nils Diewaldd599d542015-01-08 20:41:34 +0000310 expect(doc.toQuery()).toEqual('author = "Max \\"Der Coole\\" Birkendate"');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000311
312 // Serialize regex
313 doc = regexFactory.create();
Nils Diewaldd599d542015-01-08 20:41:34 +0000314 expect(doc.toQuery()).toEqual('title = /[^b]ee.+?/');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000315
316 doc = regexFactory.create({
317 match: "match:ne"
318 });
Nils Diewaldd599d542015-01-08 20:41:34 +0000319 expect(doc.toQuery()).toEqual('title != /[^b]ee.+?/');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000320
321 doc = dateFactory.create();
Nils Diewaldd599d542015-01-08 20:41:34 +0000322 expect(doc.toQuery()).toEqual('pubDate in 2014-11-05');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000323
324 doc = dateFactory.create({
325 value : "2014"
326 });
Nils Diewaldd599d542015-01-08 20:41:34 +0000327 expect(doc.toQuery()).toEqual('pubDate in 2014');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000328 });
Nils Diewald0b6c0412014-12-19 03:55:57 +0000329});
330
331
332describe('KorAP.DocGroup', function () {
333 // Create example factories
334 var docFactory = buildFactory(
335 KorAP.Doc,
336 {
337 "@type" : "korap:doc",
338 "match":"match:eq",
339 "key" : "author",
340 "value" : "Max Birkendale"
341 }
342 );
343
344 var docGroupFactory = buildFactory(
345 KorAP.DocGroup, {
346 "@type" : "korap:docGroup",
347 "operation" : "operation:and",
348 "operands" : [
349 docFactory.create().toJson(),
350 docFactory.create({
351 "key" : "pubDate",
352 "type" : "type:date",
353 "value" : "2014-12-05"
354 }).toJson()
355 ]
356 });
357
Nils Diewald0b6c0412014-12-19 03:55:57 +0000358
Nils Diewald8f4e2542014-12-19 04:42:09 +0000359 it('should be initializable', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000360 // Create empty group
361 var docGroup = KorAP.DocGroup.create();
362 expect(docGroup.operation()).toEqual('and');
363
364 // Create empty group
Nils Diewald8f4e2542014-12-19 04:42:09 +0000365 docGroup = KorAP.DocGroup.create();
366 docGroup.operation('or');
Nils Diewald0b6c0412014-12-19 03:55:57 +0000367 expect(docGroup.operation()).toEqual('or');
368 });
369
370 it('should be definable', function () {
371
372 // Empty group
373 var docGroup = KorAP.DocGroup.create();
374 expect(docGroup.operation()).toEqual('and');
375
376 // Set values
377 docGroup.operation("or");
378 expect(docGroup.operation()).toEqual('or');
379
380 // Set invalid values
381 docGroup.operation("hui");
382 expect(docGroup.operation()).toEqual('or');
383 });
384
385 it('should be deserializable', function () {
386 var docGroup = docGroupFactory.create();
387 expect(docGroup.operation()).toEqual("and");
388 expect(docGroup.operands().length).toEqual(2);
389
390 var op1 = docGroup.getOperand(0);
391 expect(op1.type()).toEqual("string");
392 expect(op1.key()).toEqual("author");
393 expect(op1.value()).toEqual("Max Birkendale");
394 expect(op1.matchop()).toEqual("eq");
395
396 var op2 = docGroup.getOperand(1);
397 expect(op2.type()).toEqual("date");
398 expect(op2.key()).toEqual("pubDate");
399 expect(op2.value()).toEqual("2014-12-05");
400 expect(op2.matchop()).toEqual("eq");
401
Nils Diewaldf219eb82015-01-07 20:15:42 +0000402 // Append empty group
Nils Diewald966abf12014-12-20 02:27:45 +0000403 var newGroup = docGroup.append(KorAP.DocGroup.create());
Nils Diewald8f4e2542014-12-19 04:42:09 +0000404 newGroup.operation('or');
Nils Diewald966abf12014-12-20 02:27:45 +0000405 newGroup.append(docFactory.create());
406 newGroup.append(docFactory.create({
Nils Diewald0b6c0412014-12-19 03:55:57 +0000407 "type" : "type:regex",
408 "key" : "title",
409 "value" : "^e.+?$",
410 "match" : "match:ne"
411 }));
412
413 expect(docGroup.operation()).toEqual("and");
414 expect(docGroup.operands().length).toEqual(3);
415
416 var op1 = docGroup.getOperand(0);
417 expect(op1.ldType()).toEqual("doc");
418 expect(op1.type()).toEqual("string");
419 expect(op1.key()).toEqual("author");
420 expect(op1.value()).toEqual("Max Birkendale");
421 expect(op1.matchop()).toEqual("eq");
422
423 var op2 = docGroup.getOperand(1);
424 expect(op2.ldType()).toEqual("doc");
425 expect(op2.type()).toEqual("date");
426 expect(op2.key()).toEqual("pubDate");
427 expect(op2.value()).toEqual("2014-12-05");
428 expect(op2.matchop()).toEqual("eq");
429
430 var op3 = docGroup.getOperand(2);
431 expect(op3.ldType()).toEqual("docGroup");
432 expect(op3.operation()).toEqual("or");
433
434 var op4 = op3.getOperand(0);
435 expect(op4.ldType()).toEqual("doc");
436 expect(op4.type()).toEqual("string");
437 expect(op4.key()).toEqual("author");
438 expect(op4.value()).toEqual("Max Birkendale");
439 expect(op4.matchop()).toEqual("eq");
440
441 var op5 = op3.getOperand(1);
442 expect(op5.ldType()).toEqual("doc");
443 expect(op5.type()).toEqual("regex");
444 expect(op5.key()).toEqual("title");
445 expect(op5.value()).toEqual("^e.+?$");
446 expect(op5.matchop()).toEqual("ne");
447 });
448
Nils Diewaldf219eb82015-01-07 20:15:42 +0000449 it('should be serializable to JSON', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000450 var docGroup = docGroupFactory.create();
451
452 expect(docGroup.toJson()).toEqual(jasmine.objectContaining({
453 "@type" : "korap:docGroup",
454 "operation" : "operation:and",
455 "operands" : [
456 {
457 "@type": 'korap:doc',
458 "key" : 'author',
459 "match": 'match:eq',
460 "value": 'Max Birkendale',
461 "type": 'type:string'
462 },
463 {
464 "@type": 'korap:doc',
465 "key": 'pubDate',
466 "match": 'match:eq',
467 "value": '2014-12-05',
468 "type": 'type:date'
469 }
470 ]
471 }));
472 });
Nils Diewaldf219eb82015-01-07 20:15:42 +0000473
474 it('should be serializable to String', function () {
475 var docGroup = docGroupFactory.create();
Nils Diewaldd599d542015-01-08 20:41:34 +0000476 expect(docGroup.toQuery()).toEqual('author = "Max Birkendale" & pubDate in 2014-12-05');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000477
478 docGroup = docGroupFactory.create({
479 "@type" : "korap:docGroup",
480 "operation" : "operation:or",
481 "operands" : [
482 {
483 "@type": 'korap:doc',
484 "key" : 'author',
485 "match": 'match:eq',
486 "value": 'Max Birkendale',
487 "type": 'type:string'
488 },
489 {
490 "@type" : "korap:docGroup",
491 "operation" : "operation:and",
492 "operands" : [
493 {
494 "@type": 'korap:doc',
495 "key": 'pubDate',
496 "match": 'match:geq',
497 "value": '2014-05-12',
498 "type": 'type:date'
499 },
500 {
501 "@type": 'korap:doc',
502 "key": 'pubDate',
503 "match": 'match:leq',
504 "value": '2014-12-05',
505 "type": 'type:date'
506 },
507 {
508 "@type": 'korap:doc',
509 "key": 'foo',
510 "match": 'match:ne',
511 "value": '[a]?bar',
512 "type": 'type:regex'
513 }
514 ]
515 }
516 ]
517 });
Nils Diewaldd599d542015-01-08 20:41:34 +0000518 expect(docGroup.toQuery()).toEqual(
Nils Diewaldf219eb82015-01-07 20:15:42 +0000519 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05 & foo != /[a]?bar/)'
520 );
521 });
Nils Diewald0b6c0412014-12-19 03:55:57 +0000522});
523
Nils Diewald966abf12014-12-20 02:27:45 +0000524describe('KorAP.UnspecifiedDoc', function () {
525 it('should be initializable', function () {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000526 var doc = KorAP.UnspecifiedDoc.create();
527 var docElement = doc.element();
Nils Diewaldd599d542015-01-08 20:41:34 +0000528 expect(docElement.getAttribute('class')).toEqual('doc unspecified');
Nils Diewald966abf12014-12-20 02:27:45 +0000529 expect(docElement.firstChild.firstChild.data).toEqual('⋯');
Nils Diewalde15b7a22015-01-09 21:50:21 +0000530 expect(docElement.lastChild.lastChild.data).toEqual('⋯');
Nils Diewaldd599d542015-01-08 20:41:34 +0000531 expect(doc.toQuery()).toEqual('⋯');
Nils Diewald966abf12014-12-20 02:27:45 +0000532
Nils Diewald8f6b6102015-01-08 18:25:33 +0000533 // Only removable
Nils Diewald966abf12014-12-20 02:27:45 +0000534 expect(docElement.lastChild.children.length).toEqual(0);
535 });
536
537 it('should be removable, when no root', function () {
538 var docGroup = KorAP.DocGroup.create();
539 docGroup.operation('or');
540 expect(docGroup.operation()).toEqual('or');
541
542 docGroup.append({
543 "@type": 'korap:doc',
544 "key": 'pubDate',
545 "match": 'match:eq',
546 "value": '2014-12-05',
547 "type": 'type:date'
548 });
549
Nils Diewaldf219eb82015-01-07 20:15:42 +0000550 // Add unspecified object
551 docGroup.append();
552
Nils Diewald966abf12014-12-20 02:27:45 +0000553 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
554 expect(docGroup.element().children[0].getAttribute('class')).toEqual('doc');
555
Nils Diewald966abf12014-12-20 02:27:45 +0000556 var unspec = docGroup.element().children[1];
Nils Diewaldd599d542015-01-08 20:41:34 +0000557 expect(unspec.getAttribute('class')).toEqual('doc unspecified');
Nils Diewald966abf12014-12-20 02:27:45 +0000558
559 // Removable
560 expect(unspec.lastChild.children.length).toEqual(1);
561 expect(unspec.lastChild.children[0].getAttribute('class')).toEqual('delete');
562 });
563});
564
Nils Diewald8f4e2542014-12-19 04:42:09 +0000565describe('KorAP.Doc element', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000566 it('should be initializable', function () {
Nils Diewald8f4e2542014-12-19 04:42:09 +0000567 var docElement = KorAP.Doc.create(undefined, {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000568 "@type" : "korap:doc",
569 "key":"Titel",
570 "value":"Baum",
571 "match":"match:eq"
572 });
573 expect(docElement.key()).toEqual('Titel');
574 expect(docElement.matchop()).toEqual('eq');
575 expect(docElement.value()).toEqual('Baum');
576
577 var docE = docElement.element();
578 expect(docE.children[0].firstChild.data).toEqual('Titel');
579 expect(docE.children[1].firstChild.data).toEqual('eq');
580 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
581 expect(docE.children[2].firstChild.data).toEqual('Baum');
582 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
583
584 expect(docElement.toJson()).toEqual(jasmine.objectContaining({
585 "@type" : "korap:doc",
586 "key":"Titel",
587 "value":"Baum",
588 "match":"match:eq"
589 }));
590 });
591});
592
Nils Diewald8f4e2542014-12-19 04:42:09 +0000593describe('KorAP.DocGroup element', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000594 it('should be initializable', function () {
595
Nils Diewald8f4e2542014-12-19 04:42:09 +0000596 var docGroup = KorAP.DocGroup.create(undefined, {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000597 "@type" : "korap:docGroup",
598 "operation" : "operation:and",
599 "operands" : [
600 {
601 "@type": 'korap:doc',
602 "key" : 'author',
603 "match": 'match:eq',
604 "value": 'Max Birkendale',
605 "type": 'type:string'
606 },
607 {
608 "@type": 'korap:doc',
609 "key": 'pubDate',
610 "match": 'match:eq',
611 "value": '2014-12-05',
612 "type": 'type:date'
613 }
614 ]
615 });
616
Nils Diewald8f4e2542014-12-19 04:42:09 +0000617 expect(docGroup.operation()).toEqual('and');
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('and');
621
622 var first = e.children[0];
623 expect(first.getAttribute('class')).toEqual('doc');
624 expect(first.children[0].getAttribute('class')).toEqual('key');
625 expect(first.children[1].getAttribute('class')).toEqual('match');
626 expect(first.children[2].getAttribute('class')).toEqual('value');
627 expect(first.children[2].getAttribute('data-type')).toEqual('string');
628 expect(first.children[0].firstChild.data).toEqual('author');
629 expect(first.children[1].firstChild.data).toEqual('eq');
630 expect(first.children[2].firstChild.data).toEqual('Max Birkendale');
631
632 var second = e.children[1];
633 expect(second.getAttribute('class')).toEqual('doc');
634 expect(second.children[0].getAttribute('class')).toEqual('key');
635 expect(second.children[1].getAttribute('class')).toEqual('match');
636 expect(second.children[2].getAttribute('class')).toEqual('value');
637 expect(second.children[2].getAttribute('data-type')).toEqual('date');
638 expect(second.children[0].firstChild.data).toEqual('pubDate');
639 expect(second.children[1].firstChild.data).toEqual('eq');
640 expect(second.children[2].firstChild.data).toEqual('2014-12-05');
641
642 });
643
644 it('should be deserializable with nested groups', function () {
Nils Diewald8f4e2542014-12-19 04:42:09 +0000645 var docGroup = KorAP.DocGroup.create(undefined, {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000646 "@type" : "korap:docGroup",
647 "operation" : "operation:or",
648 "operands" : [
649 {
650 "@type": 'korap:doc',
651 "key" : 'author',
652 "match": 'match:eq',
653 "value": 'Max Birkendale',
654 "type": 'type:string'
655 },
656 {
657 "@type" : "korap:docGroup",
658 "operation" : "operation:and",
659 "operands" : [
660 {
661 "@type": 'korap:doc',
662 "key": 'pubDate',
663 "match": 'match:geq',
664 "value": '2014-05-12',
665 "type": 'type:date'
666 },
667 {
668 "@type": 'korap:doc',
669 "key": 'pubDate',
670 "match": 'match:leq',
671 "value": '2014-12-05',
672 "type": 'type:date'
673 }
674 ]
675 }
676 ]
677 });
678
Nils Diewald8f4e2542014-12-19 04:42:09 +0000679 expect(docGroup.operation()).toEqual('or');
680 var e = docGroup.element();
Nils Diewald0b6c0412014-12-19 03:55:57 +0000681 expect(e.getAttribute('class')).toEqual('docGroup');
682 expect(e.getAttribute('data-operation')).toEqual('or');
683
Nils Diewald966abf12014-12-20 02:27:45 +0000684 expect(e.children[0].getAttribute('class')).toEqual('doc');
685 var docop = e.children[0].lastChild;
686 expect(docop.getAttribute('class')).toEqual('operators');
687 expect(docop.children[0].getAttribute('class')).toEqual('and');
688 expect(docop.children[1].getAttribute('class')).toEqual('or');
689 expect(docop.children[2].getAttribute('class')).toEqual('delete');
690
691 expect(e.children[1].getAttribute('class')).toEqual('docGroup');
692 expect(e.children[1].getAttribute('data-operation')).toEqual('and');
693
694 // This and-operation can be "or"ed or "delete"d
695 var secop = e.children[1].children[2];
696 expect(secop.getAttribute('class')).toEqual('operators');
697 expect(secop.children[0].getAttribute('class')).toEqual('or');
698 expect(secop.children[1].getAttribute('class')).toEqual('delete');
699
700 // This or-operation can be "and"ed or "delete"d
701 expect(e.children[2].getAttribute('class')).toEqual('operators');
702 expect(e.lastChild.getAttribute('class')).toEqual('operators');
703 expect(e.lastChild.children[0].getAttribute('class')).toEqual('and');
704 expect(e.lastChild.children[1].getAttribute('class')).toEqual('delete');
Nils Diewald0b6c0412014-12-19 03:55:57 +0000705
706 });
707});
708
709describe('KorAP.VirtualCollection', function () {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000710
711 var simpleGroupFactory = buildFactory(KorAP.DocGroup, {
712 "@type" : "korap:docGroup",
713 "operation" : "operation:and",
714 "operands" : [
715 {
716 "@type": 'korap:doc',
717 "key" : 'author',
718 "match": 'match:eq',
719 "value": 'Max Birkendale',
720 "type": 'type:string'
721 },
722 {
723 "@type": 'korap:doc',
724 "key": 'pubDate',
725 "match": 'match:eq',
726 "value": '2014-12-05',
727 "type": 'type:date'
728 }
729 ]
730 });
731
732 var nestedGroupFactory = buildFactory(KorAP.VirtualCollection, {
733 "@type" : "korap:docGroup",
734 "operation" : "operation:or",
735 "operands" : [
736 {
737 "@type": 'korap:doc',
738 "key" : 'author',
739 "match": 'match:eq',
740 "value": 'Max Birkendale',
741 "type": 'type:string'
742 },
743 {
744 "@type" : "korap:docGroup",
745 "operation" : "operation:and",
746 "operands" : [
747 {
748 "@type": 'korap:doc',
749 "key": 'pubDate',
750 "match": 'match:geq',
751 "value": '2014-05-12',
752 "type": 'type:date'
753 },
754 {
755 "@type": 'korap:doc',
756 "key": 'pubDate',
757 "match": 'match:leq',
758 "value": '2014-12-05',
759 "type": 'type:date'
760 }
761 ]
762 }
763 ]
764 });
765
766 var flatGroupFactory = buildFactory(KorAP.VirtualCollection, {
767 "@type" : "korap:docGroup",
768 "operation" : "operation:and",
769 "operands" : [
770 {
771 "@type": 'korap:doc',
772 "key": 'pubDate',
773 "match": 'match:geq',
774 "value": '2014-05-12',
775 "type": 'type:date'
776 },
777 {
778 "@type": 'korap:doc',
779 "key": 'pubDate',
780 "match": 'match:leq',
781 "value": '2014-12-05',
782 "type": 'type:date'
783 },
784 {
785 "@type": 'korap:doc',
786 "key": 'foo',
787 "match": 'match:eq',
788 "value": 'bar',
789 "type": 'type:string'
790 }
791 ]
792 });
793
Nils Diewald0b6c0412014-12-19 03:55:57 +0000794 it('should be initializable', function () {
795 var vc = KorAP.VirtualCollection.render();
796 expect(vc.element().getAttribute('class')).toEqual('vc');
Nils Diewaldd599d542015-01-08 20:41:34 +0000797 expect(vc.root().element().getAttribute('class')).toEqual('doc unspecified');
Nils Diewald966abf12014-12-20 02:27:45 +0000798
799 // Not removable
800 expect(vc.root().element().lastChild.children.length).toEqual(0);
Nils Diewald0b6c0412014-12-19 03:55:57 +0000801 });
802
803 it('should be based on a doc', function () {
804 var vc = KorAP.VirtualCollection.render({
805 "@type" : "korap:doc",
806 "key":"Titel",
807 "value":"Baum",
808 "match":"match:eq"
809 });
810
811 expect(vc.element().getAttribute('class')).toEqual('vc');
812 expect(vc.root().element().getAttribute('class')).toEqual('doc');
813 expect(vc.root().key()).toEqual('Titel');
814 expect(vc.root().value()).toEqual('Baum');
815 expect(vc.root().matchop()).toEqual('eq');
816
817 var docE = vc.root().element();
818 expect(docE.children[0].firstChild.data).toEqual('Titel');
819 expect(docE.children[1].firstChild.data).toEqual('eq');
820 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
821 expect(docE.children[2].firstChild.data).toEqual('Baum');
822 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
823 });
824
825 it('should be based on a docGroup', function () {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000826 var vc = KorAP.VirtualCollection.render(simpleGroupFactory.create().toJson());
Nils Diewald0b6c0412014-12-19 03:55:57 +0000827
828 expect(vc.element().getAttribute('class')).toEqual('vc');
829 expect(vc.root().element().getAttribute('class')).toEqual('docGroup');
830 expect(vc.root().operation()).toEqual('and');
831
832 var docGroup = vc.root();
833
834 var first = docGroup.getOperand(0);
835 expect(first.key()).toEqual('author');
836 expect(first.value()).toEqual('Max Birkendale');
837 expect(first.matchop()).toEqual('eq');
838
839 var second = docGroup.getOperand(1);
840 expect(second.key()).toEqual('pubDate');
841 expect(second.value()).toEqual('2014-12-05');
842 expect(second.matchop()).toEqual('eq');
843 });
844
Nils Diewald5c817a42015-01-06 01:08:56 +0000845
Nils Diewald966abf12014-12-20 02:27:45 +0000846 it('should be based on a nested docGroup', function () {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000847 var vc = nestedGroupFactory.create();
848
Nils Diewald5c817a42015-01-06 01:08:56 +0000849 expect(vc.element().getAttribute('class')).toEqual('vc');
850 expect(vc.element().firstChild.getAttribute('class')).toEqual('docGroup');
851 expect(vc.element().firstChild.children[0].getAttribute('class')).toEqual('doc');
852 var dg = vc.element().firstChild.children[1];
853 expect(dg.getAttribute('class')).toEqual('docGroup');
854 expect(dg.children[0].getAttribute('class')).toEqual('doc');
855 expect(dg.children[1].getAttribute('class')).toEqual('doc');
856 expect(dg.children[2].getAttribute('class')).toEqual('operators');
857 expect(vc.element().firstChild.children[2].getAttribute('class')).toEqual('operators');
858 });
859
Nils Diewaldf219eb82015-01-07 20:15:42 +0000860 it('should be modifiable by deletion in flat docGroups', function () {
861 var vc = flatGroupFactory.create();
Nils Diewald0297ba12015-01-05 21:56:12 +0000862 var docGroup = vc.root();
Nils Diewaldf219eb82015-01-07 20:15:42 +0000863
864 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
865
Nils Diewald0297ba12015-01-05 21:56:12 +0000866 var doc = docGroup.getOperand(1);
867 expect(doc.key()).toEqual("pubDate");
Nils Diewaldf219eb82015-01-07 20:15:42 +0000868 expect(doc.value()).toEqual("2014-12-05");
Nils Diewald5c817a42015-01-06 01:08:56 +0000869
870 // Remove operand 1
Nils Diewaldf219eb82015-01-07 20:15:42 +0000871 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
Nils Diewald5c817a42015-01-06 01:08:56 +0000872 expect(doc._element).toEqual(undefined);
873
Nils Diewald0297ba12015-01-05 21:56:12 +0000874 doc = docGroup.getOperand(1);
875 expect(doc.key()).toEqual("foo");
Nils Diewald0297ba12015-01-05 21:56:12 +0000876
Nils Diewald5c817a42015-01-06 01:08:56 +0000877 // Remove operand 1
Nils Diewaldf219eb82015-01-07 20:15:42 +0000878 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
Nils Diewald5c817a42015-01-06 01:08:56 +0000879 expect(doc._element).toEqual(undefined);
Nils Diewald0297ba12015-01-05 21:56:12 +0000880
Nils Diewaldf219eb82015-01-07 20:15:42 +0000881 // Only one operand left ...
Nils Diewald5c817a42015-01-06 01:08:56 +0000882 expect(docGroup.getOperand(1)).toBeUndefined();
Nils Diewaldf219eb82015-01-07 20:15:42 +0000883 // ... but there shouldn't be a group anymore at all!
884 expect(docGroup.getOperand(0)).toBeUndefined();
885
886 var obj = vc.root();
887 expect(obj.ldType()).toEqual("doc");
888 expect(obj.key()).toEqual("pubDate");
889 expect(obj.value()).toEqual("2014-05-12");
890
891 expect(obj.element().getAttribute('class')).toEqual('doc');
Nils Diewald5c817a42015-01-06 01:08:56 +0000892 });
Nils Diewaldf219eb82015-01-07 20:15:42 +0000893
894
895 it('should be modifiable by deletion in nested docGroups (root case)', function () {
896 var vc = nestedGroupFactory.create();
897
Nils Diewaldd599d542015-01-08 20:41:34 +0000898 expect(vc.toQuery()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +0000899 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05)'
900 );
901
Nils Diewaldf219eb82015-01-07 20:15:42 +0000902 var docGroup = vc.root();
903 expect(docGroup.ldType()).toEqual("docGroup");
904 expect(docGroup.operation()).toEqual("or");
905
906 var doc = docGroup.getOperand(0);
907 expect(doc.key()).toEqual("author");
908 expect(doc.value()).toEqual("Max Birkendale");
909
910 docGroup = docGroup.getOperand(1);
911 expect(docGroup.operation()).toEqual("and");
912
913 doc = docGroup.getOperand(0);
914 expect(doc.key()).toEqual("pubDate");
915 expect(doc.matchop()).toEqual("geq");
916 expect(doc.value()).toEqual("2014-05-12");
917 expect(doc.type()).toEqual("date");
918
919 doc = docGroup.getOperand(1);
920 expect(doc.key()).toEqual("pubDate");
921 expect(doc.matchop()).toEqual("leq");
922 expect(doc.value()).toEqual("2014-12-05");
923 expect(doc.type()).toEqual("date");
924
925 // Remove first operand so everything becomes root
926 expect(
927 vc.root().delOperand(
928 vc.root().getOperand(0)
929 ).update().ldType()
930 ).toEqual("docGroup");
Nils Diewald8e7182e2015-01-08 15:02:07 +0000931
Nils Diewaldf219eb82015-01-07 20:15:42 +0000932 expect(vc.root().ldType()).toEqual("docGroup");
933 expect(vc.root().operation()).toEqual("and");
Nils Diewald8e7182e2015-01-08 15:02:07 +0000934 expect(vc.root().getOperand(0).ldType()).toEqual("doc");
935
Nils Diewaldd599d542015-01-08 20:41:34 +0000936 expect(vc.toQuery()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +0000937 'pubDate since 2014-05-12 & pubDate until 2014-12-05'
938 );
Nils Diewaldf219eb82015-01-07 20:15:42 +0000939 });
940
941 it('should be modifiable by deletion in nested docGroups (resolve group case)', function () {
942 var vc = nestedGroupFactory.create();
943
944 // Get nested group
945 var firstGroup = vc.root().getOperand(1);
946 firstGroup.append(simpleGroupFactory.create({ "operation" : "operation:or" }));
947
948 // Structur is now:
949 // or(doc, and(doc, doc, or(doc, doc)))
950
951 // Get nested or in and
952 var orGroup = vc.root().getOperand(1).getOperand(2);
953 expect(orGroup.ldType()).toEqual("docGroup");
954 expect(orGroup.operation()).toEqual("or");
955
956 // Remove
957 // Structur is now:
958 // or(doc, and(doc, doc, doc)))
959 expect(orGroup.delOperand(orGroup.getOperand(0)).update().operation()).toEqual("and");
960 expect(vc.root().getOperand(1).operands().length).toEqual(3);
961 });
962
963 it('should be modifiable by deletion in nested docGroups (identical group case)', function () {
964 var vc = nestedGroupFactory.create();
965
966 // Get nested group
967 var firstGroup = vc.root().getOperand(1);
Nils Diewald8e7182e2015-01-08 15:02:07 +0000968 firstGroup.append(simpleGroupFactory.create({
969 "operation" : "operation:or"
970 }));
971 var oldAuthor = firstGroup.getOperand(2).getOperand(0);
972 oldAuthor.key("title");
973 oldAuthor.value("Der Birnbaum");
Nils Diewaldf219eb82015-01-07 20:15:42 +0000974
975 // Structur is now:
976 // or(doc, and(doc, doc, or(doc, doc)))
Nils Diewaldd599d542015-01-08 20:41:34 +0000977 expect(vc.toQuery()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +0000978 '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 +0000979 );
980
981 var andGroup = vc.root().getOperand(1);
982
983 // Get leading docs in and
984 var doc1 = andGroup.getOperand(0);
985 expect(doc1.ldType()).toEqual("doc");
986 expect(doc1.value()).toEqual("2014-05-12");
987 var doc2 = andGroup.getOperand(1);
988 expect(doc2.ldType()).toEqual("doc");
989 expect(doc2.value()).toEqual("2014-12-05");
990
991 // Remove 2
992 expect(andGroup.delOperand(doc2).update().operation()).toEqual("and");
993 // Structur is now:
994 // or(doc, and(doc, or(doc, doc)))
995
Nils Diewaldd599d542015-01-08 20:41:34 +0000996 expect(vc.toQuery()).toEqual(
997 'author = "Max Birkendale"' +
998 ' | (pubDate since 2014-05-12 & ' +
999 '(title = "Der Birnbaum" | pubDate in 2014-12-05))'
Nils Diewaldf219eb82015-01-07 20:15:42 +00001000 );
1001
1002
1003 // Remove 1
1004 expect(andGroup.delOperand(doc1).update().operation()).toEqual("or");
1005 // Structur is now:
1006 // or(doc, doc, doc)
1007
Nils Diewaldd599d542015-01-08 20:41:34 +00001008 expect(vc.toQuery()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +00001009 'author = "Max Birkendale" | title = "Der Birnbaum" | pubDate in 2014-12-05'
Nils Diewaldf219eb82015-01-07 20:15:42 +00001010 );
1011 });
Nils Diewald4019bd22015-01-08 19:57:50 +00001012
1013 it('should be reducible to unspecification', function () {
1014 var vc = demoFactory.create();
1015
Nils Diewaldd599d542015-01-08 20:41:34 +00001016 expect(vc.toQuery()).toEqual(vc.root().toQuery());
Nils Diewald4019bd22015-01-08 19:57:50 +00001017
Nils Diewaldd599d542015-01-08 20:41:34 +00001018 expect(vc.toQuery()).toEqual(
1019 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & ' +
1020 '(Titel = "Baum" | Veröffentlichungsort = "hihi")) ' +
1021 '| Untertitel = "huhu"');
Nils Diewald4019bd22015-01-08 19:57:50 +00001022
1023 expect(vc.root().element().lastChild.children[0].firstChild.nodeValue).toEqual('and');
1024 expect(vc.root().element().lastChild.children[1].firstChild.nodeValue).toEqual('×');
Nils Diewald4019bd22015-01-08 19:57:50 +00001025 expect(vc.root().delOperand(vc.root().getOperand(0)).update()).not.toBeUndefined();
Nils Diewaldd599d542015-01-08 20:41:34 +00001026 expect(vc.toQuery()).toEqual('Untertitel = "huhu"');
Nils Diewald4019bd22015-01-08 19:57:50 +00001027
Nils Diewaldd599d542015-01-08 20:41:34 +00001028 var lc = vc.root().element().lastChild;
1029 expect(lc.children[0].firstChild.nodeValue).toEqual('and');
1030 expect(lc.children[1].firstChild.nodeValue).toEqual('or');
1031 expect(lc.children[2].firstChild.nodeValue).toEqual('×');
Nils Diewald4019bd22015-01-08 19:57:50 +00001032
1033 // Clean everything
1034 vc.clean();
Nils Diewaldd599d542015-01-08 20:41:34 +00001035 expect(vc.toQuery()).toEqual('⋯');
Nils Diewald4019bd22015-01-08 19:57:50 +00001036 });
Nils Diewald0b6c0412014-12-19 03:55:57 +00001037});
1038
1039describe('KorAP.Operators', function () {
1040 it('should be initializable', function () {
1041 var op = KorAP.Operators.create(true, false, false);
1042 expect(op.and()).toBeTruthy();
1043 expect(op.or()).not.toBeTruthy();
1044 expect(op.del()).not.toBeTruthy();
1045
1046 op.and(false);
1047 expect(op.and()).not.toBeTruthy();
1048 expect(op.or()).not.toBeTruthy();
1049 expect(op.del()).not.toBeTruthy();
1050
1051 op.or(true);
1052 op.del(true);
1053 expect(op.and()).not.toBeTruthy();
1054 expect(op.or()).toBeTruthy();
1055 expect(op.del()).toBeTruthy();
1056
1057 var e = op.element();
1058 expect(e.getAttribute('class')).toEqual('operators');
1059 expect(e.children[0].getAttribute('class')).toEqual('or');
1060 expect(e.children[0].firstChild.data).toEqual('or');
1061 expect(e.children[1].getAttribute('class')).toEqual('delete');
1062 expect(e.children[1].firstChild.data).toEqual('×');
1063
1064 op.and(true);
1065 op.del(false);
1066 op.update();
1067
1068 e = op.element();
1069 expect(e.getAttribute('class')).toEqual('operators');
1070 expect(e.children[0].getAttribute('class')).toEqual('and');
1071 expect(e.children[0].firstChild.data).toEqual('and');
1072 expect(e.children[1].getAttribute('class')).toEqual('or');
1073 expect(e.children[1].firstChild.data).toEqual('or');
1074 });
1075});
Nils Diewalde15b7a22015-01-09 21:50:21 +00001076
1077describe('KorAP._delete (event)', function () {
1078 var complexVCFactory = buildFactory(KorAP.VirtualCollection,{
1079 "@type": 'korap:docGroup',
1080 'operation' : 'operation:and',
1081 'operands' : [
1082 {
1083 "@type": 'korap:doc',
1084 "key": 'pubDate',
1085 "match": 'match:eq',
1086 "value": '2014-12-05',
1087 "type": 'type:date'
1088 },
1089 {
1090 "@type" : 'korap:docGroup',
1091 'operation' : 'operation:or',
1092 'operands' : [
1093 {
1094 '@type' : 'korap:doc',
1095 'key' : 'title',
1096 'value' : 'Hello World!'
1097 },
1098 {
1099 '@type' : 'korap:doc',
1100 'key' : 'foo',
1101 'value' : 'bar'
1102 }
1103 ]
1104 }
1105 ]
1106 });
1107
1108 it('should clean on root docs', function () {
1109 var vc = KorAP.VirtualCollection.render({
1110 "@type": 'korap:doc',
1111 "key": 'pubDate',
1112 "match": 'match:eq',
1113 "value": '2014-12-05',
1114 "type": 'type:date'
1115 });
1116 expect(vc.root().toQuery()).toEqual('pubDate in 2014-12-05');
1117 expect(vc.root().element().lastChild.getAttribute('class')).toEqual('operators');
1118
1119 // Clean with delete from root
1120 expect(vc.root().element().lastChild.lastChild.getAttribute('class')).toEqual('delete');
Nils Diewald52f7eb12015-01-12 17:30:04 +00001121 _delOn(vc.root());
Nils Diewalde15b7a22015-01-09 21:50:21 +00001122 expect(vc.root().toQuery()).toEqual('⋯');
1123 expect(vc.root().element().lastChild.lastChild.data).toEqual('⋯');
1124 });
1125
1126 it ('should remove on nested docs', function () {
1127 var vc = KorAP.VirtualCollection.render(
1128 {
1129 "@type": 'korap:docGroup',
1130 'operation' : 'operation:and',
1131 'operands' : [
1132 {
1133 "@type": 'korap:doc',
1134 "key": 'pubDate',
1135 "match": 'match:eq',
1136 "value": '2014-12-05',
1137 "type": 'type:date'
1138 },
1139 {
1140 "@type" : 'korap:doc',
1141 'key' : 'foo',
1142 'value' : 'bar'
1143 }
1144 ]
1145 }
1146 );
1147
1148 // Delete with direct element access
1149 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
Nils Diewald52f7eb12015-01-12 17:30:04 +00001150 _delOn(vc.root().getOperand(0));
1151
Nils Diewalde15b7a22015-01-09 21:50:21 +00001152 expect(vc.toQuery()).toEqual('foo = "bar"');
1153 expect(vc.root().ldType()).toEqual('doc');
1154 });
1155
1156 it ('should clean on doc groups', function () {
1157 var vc = KorAP.VirtualCollection.render(
1158 {
1159 "@type": 'korap:docGroup',
1160 'operation' : 'operation:and',
1161 'operands' : [
1162 {
1163 "@type": 'korap:doc',
1164 "key": 'pubDate',
1165 "match": 'match:eq',
1166 "value": '2014-12-05',
1167 "type": 'type:date'
1168 },
1169 {
1170 "@type" : 'korap:doc',
1171 'key' : 'foo',
1172 'value' : 'bar'
1173 }
1174 ]
1175 }
1176 );
1177
1178 // Cleanwith direct element access
1179 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
Nils Diewald52f7eb12015-01-12 17:30:04 +00001180 _delOn(vc.root());
Nils Diewalde15b7a22015-01-09 21:50:21 +00001181 expect(vc.toQuery()).toEqual('⋯');
1182 expect(vc.root().ldType()).toEqual('non');
1183 });
1184
1185 it ('should remove on nested doc groups (case of ungrouping 1)', 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 hello world:
Nils Diewald52f7eb12015-01-12 17:30:04 +00001194 _delOn(vc.root().getOperand(1).getOperand(0));
Nils Diewalde15b7a22015-01-09 21:50:21 +00001195 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1196 expect(vc.root().ldType()).toEqual('docGroup');
1197 });
1198
1199 it ('should remove on nested doc groups (case of ungrouping 2)', function () {
1200 var vc = complexVCFactory.create();
1201
1202 // Delete with direct element access
1203 expect(vc.toQuery()).toEqual(
1204 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1205 );
1206
1207 // Remove bar
Nils Diewald52f7eb12015-01-12 17:30:04 +00001208 _delOn(vc.root().getOperand(1).getOperand(1));
Nils Diewalde15b7a22015-01-09 21:50:21 +00001209 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & title = "Hello World!"');
1210 expect(vc.root().ldType()).toEqual('docGroup');
1211 expect(vc.root().operation()).toEqual('and');
1212 });
1213
1214 it ('should remove on nested doc groups (case of root changing)', function () {
1215 var vc = complexVCFactory.create();
1216
1217 // Delete with direct element access
1218 expect(vc.toQuery()).toEqual(
1219 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1220 );
1221
1222 // Remove bar
Nils Diewald52f7eb12015-01-12 17:30:04 +00001223 _delOn(vc.root().getOperand(0));
Nils Diewalde15b7a22015-01-09 21:50:21 +00001224 expect(vc.toQuery()).toEqual('title = "Hello World!" | foo = "bar"');
1225 expect(vc.root().ldType()).toEqual('docGroup');
1226 expect(vc.root().operation()).toEqual('or');
1227 });
1228
1229 it ('should remove on nested doc groups (list flattening)', function () {
1230 var vc = KorAP.VirtualCollection.render(
1231 {
1232 "@type": 'korap:docGroup',
1233 'operation' : 'operation:or',
1234 'operands' : [
1235 {
1236 "@type": 'korap:doc',
1237 "key": 'pubDate',
1238 "match": 'match:eq',
1239 "value": '2014-12-05',
1240 "type": 'type:date'
1241 },
1242 {
1243 "@type" : 'korap:doc',
1244 'key' : 'foo',
1245 'value' : 'bar'
1246 },
1247 {
1248 "@type": 'korap:docGroup',
1249 'operation' : 'operation:and',
1250 'operands' : [
1251 {
1252 "@type": 'korap:doc',
1253 "key": 'pubDate',
1254 "match": 'match:eq',
1255 "value": '2014-12-05',
1256 "type": 'type:date'
1257 },
1258 {
1259 "@type" : 'korap:docGroup',
1260 'operation' : 'operation:or',
1261 'operands' : [
1262 {
1263 '@type' : 'korap:doc',
1264 'key' : 'title',
1265 'value' : 'Hello World!'
1266 },
1267 {
1268 '@type' : 'korap:doc',
1269 'key' : 'yeah',
1270 'value' : 'juhu'
1271 }
1272 ]
1273 }
1274 ]
1275 }
1276 ]
1277 }
1278 );
1279
1280 // Delete with direct element access
1281 expect(vc.toQuery()).toEqual(
1282 'pubDate in 2014-12-05 | foo = "bar" | ' +
1283 '(pubDate in 2014-12-05 & ' +
1284 '(title = "Hello World!" | yeah = "juhu"))'
1285 );
1286
1287 expect(vc.root().ldType()).toEqual('docGroup');
1288 expect(vc.root().operation()).toEqual('or');
1289
1290 // Operands and operators
1291 expect(vc.element().firstChild.children.length).toEqual(4);
1292 expect(vc.element().firstChild.lastChild.getAttribute('class')).toEqual('operators');
1293
1294 // Remove inner group and flatten
Nils Diewald52f7eb12015-01-12 17:30:04 +00001295 _delOn(vc.root().getOperand(2).getOperand(0));
Nils Diewalde15b7a22015-01-09 21:50:21 +00001296
1297 expect(vc.toQuery()).toEqual(
1298 'pubDate in 2014-12-05 | foo = "bar" | title = "Hello World!" | yeah = "juhu"'
1299 );
1300 expect(vc.root().ldType()).toEqual('docGroup');
1301 expect(vc.root().operation()).toEqual('or');
1302
1303 // Operands and operators
1304 expect(vc.element().firstChild.children.length).toEqual(5);
1305 expect(vc.element().firstChild.lastChild.getAttribute('class')).toEqual('operators');
1306 });
1307});
1308
1309describe('KorAP._add (event)', function () {
1310 var complexVCFactory = buildFactory(KorAP.VirtualCollection,{
1311 "@type": 'korap:docGroup',
1312 'operation' : 'operation:and',
1313 'operands' : [
1314 {
1315 "@type": 'korap:doc',
1316 "key": 'pubDate',
1317 "match": 'match:eq',
1318 "value": '2014-12-05',
1319 "type": 'type:date'
1320 },
1321 {
1322 "@type" : 'korap:docGroup',
1323 'operation' : 'operation:or',
1324 'operands' : [
1325 {
1326 '@type' : 'korap:doc',
1327 'key' : 'title',
1328 'value' : 'Hello World!'
1329 },
1330 {
1331 '@type' : 'korap:doc',
1332 'key' : 'foo',
1333 'value' : 'bar'
1334 }
1335 ]
1336 }
1337 ]
1338 });
1339
1340 it ('should add new unspecified doc with "and"', function () {
1341 var vc = KorAP.VirtualCollection.render(
1342 {
1343 "@type": 'korap:docGroup',
1344 'operation' : 'operation:and',
1345 'operands' : [
1346 {
1347 "@type": 'korap:doc',
1348 "key": 'pubDate',
1349 "match": 'match:eq',
1350 "value": '2014-12-05',
1351 "type": 'type:date'
1352 },
1353 {
1354 "@type" : 'korap:doc',
1355 'key' : 'foo',
1356 'value' : 'bar'
1357 }
1358 ]
1359 }
1360 );
1361
1362 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1363
1364 var fc = vc.element().firstChild;
1365 expect(fc.getAttribute('data-operation')).toEqual('and');
1366 expect(fc.children.length).toEqual(3);
1367 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1368 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1369 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1370
1371 // add with 'and' in the middle
Nils Diewald52f7eb12015-01-12 17:30:04 +00001372 _andOn(vc.root().getOperand(0));
Nils Diewalde15b7a22015-01-09 21:50:21 +00001373 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1374
1375 fc = vc.element().firstChild;
1376 expect(fc.getAttribute('data-operation')).toEqual('and');
1377 expect(fc.children.length).toEqual(4);
1378 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1379
1380 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1381 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
1382 expect(fc.children[2].getAttribute('class')).toEqual('doc');
1383 });
1384
1385 it ('should add new unspecified doc with "or"', function () {
1386 var vc = KorAP.VirtualCollection.render(
1387 {
1388 "@type": 'korap:docGroup',
1389 'operation' : 'operation:and',
1390 'operands' : [
1391 {
1392 "@type": 'korap:doc',
1393 "key": 'pubDate',
1394 "match": 'match:eq',
1395 "value": '2014-12-05',
1396 "type": 'type:date'
1397 },
1398 {
1399 "@type" : 'korap:doc',
1400 'key' : 'foo',
1401 'value' : 'bar'
1402 }
1403 ]
1404 }
1405 );
1406
1407 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1408
1409 var fc = vc.element().firstChild;
1410 expect(fc.children.length).toEqual(3);
1411 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1412 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1413 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1414
1415 // add with 'or' in the middle
Nils Diewald52f7eb12015-01-12 17:30:04 +00001416 _orOn(vc.root().getOperand(0));
Nils Diewalde15b7a22015-01-09 21:50:21 +00001417 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1418 fc = vc.element().firstChild;
1419
1420 expect(fc.getAttribute('data-operation')).toEqual('and');
1421 expect(fc.children.length).toEqual(3);
1422 expect(fc.children[0].getAttribute('class')).toEqual('docGroup');
1423 expect(fc.children[0].getAttribute('data-operation')).toEqual('or');
1424 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1425 expect(fc.children[2].getAttribute('class')).toEqual('operators');
1426 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1427
1428 fc = vc.element().firstChild.firstChild;
1429 expect(fc.children.length).toEqual(3);
1430 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1431 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
1432 expect(fc.children[2].getAttribute('class')).toEqual('operators');
1433 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1434 });
1435
Nils Diewald52f7eb12015-01-12 17:30:04 +00001436 it ('should add new unspecified doc with "and" before group', function () {
1437 var vc = demoFactory.create();
1438
1439 // Wrap with direct element access
1440 expect(vc.toQuery()).toEqual(
1441 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & (Titel = "Baum" | Veröffentlichungsort = "hihi")) | Untertitel = "huhu"'
1442 );
1443
1444 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
1445 expect(vc.root().getOperand(0).operation()).toEqual('and');
1446 expect(vc.root().getOperand(0).operands().length).toEqual(3);
1447
1448 // Add unspecified on the second doc
1449 var secDoc = vc.root().getOperand(0).getOperand(1);
1450 expect(secDoc.value()).toEqual('hihi');
1451
1452 // Add
1453 _andOn(secDoc);
1454
1455 var fo = vc.root().getOperand(0);
1456
1457 expect(fo.ldType()).toEqual('docGroup');
1458 expect(fo.operation()).toEqual('and');
1459 expect(fo.operands().length).toEqual(4);
1460
1461 expect(fo.getOperand(0).ldType()).toEqual('doc');
1462 expect(fo.getOperand(1).ldType()).toEqual('doc');
1463 expect(fo.getOperand(2).ldType()).toEqual('non');
1464 expect(fo.getOperand(3).ldType()).toEqual('docGroup');
1465 });
1466
1467
1468 it ('should remove a doc with an unspecified doc in a nested group', function () {
1469 var vc = demoFactory.create();
1470
1471 // Wrap with direct element access
1472 expect(vc.toQuery()).toEqual(
1473 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & (Titel = "Baum" | Veröffentlichungsort = "hihi")) | Untertitel = "huhu"'
1474 );
1475
1476 var fo = vc.root().getOperand(0).getOperand(0);
1477 expect(fo.key()).toEqual('Titel');
1478 expect(fo.value()).toEqual('Baum');
1479
1480 // Add unspecified on the root group
1481 _orOn(fo);
1482
1483 fo = vc.root().getOperand(0).getOperand(0);
1484
1485 expect(fo.operation()).toEqual('or');
1486 expect(fo.getOperand(0).ldType()).toEqual('doc');
1487 expect(fo.getOperand(1).ldType()).toEqual('non');
1488
1489 // Delete document
1490 _delOn(fo.getOperand(0));
1491
1492 // The operand is now non
1493 expect(vc.root().getOperand(0).getOperand(0).ldType()).toEqual('non');
1494 expect(vc.root().getOperand(0).getOperand(1).ldType()).toEqual('doc');
1495 expect(vc.root().getOperand(0).getOperand(2).ldType()).toEqual('docGroup');
1496 });
1497
1498 it ('should remove an unspecified doc with an doc in a nested group', function () {
1499 var vc = demoFactory.create();
1500
1501 // Wrap with direct element access
1502 expect(vc.toQuery()).toEqual(
1503 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & (Titel = "Baum" | Veröffentlichungsort = "hihi")) | Untertitel = "huhu"'
1504 );
1505
1506 var fo = vc.root().getOperand(0).getOperand(0);
1507 expect(fo.key()).toEqual('Titel');
1508 expect(fo.value()).toEqual('Baum');
1509
1510 // Add unspecified on the root group
1511 _orOn(fo);
1512
1513 fo = vc.root().getOperand(0).getOperand(0);
1514
1515 expect(fo.operation()).toEqual('or');
1516 expect(fo.getOperand(0).ldType()).toEqual('doc');
1517 expect(fo.getOperand(1).ldType()).toEqual('non');
1518
1519 // Delete unspecified doc
1520 _delOn(fo.getOperand(1));
1521
1522 // The operand is now non
1523 fo = vc.root().getOperand(0);
1524 expect(fo.getOperand(0).ldType()).toEqual('doc');
1525 expect(fo.getOperand(0).key()).toEqual('Titel');
1526 expect(fo.getOperand(0).value()).toEqual('Baum');
1527 expect(fo.getOperand(1).ldType()).toEqual('doc');
1528 expect(fo.getOperand(2).ldType()).toEqual('docGroup');
1529 });
1530
1531
1532 it ('should add on parent group (case "and")', function () {
Nils Diewaldd5070b02015-01-11 01:44:47 +00001533 var vc = complexVCFactory.create();
1534
1535 // Wrap with direct element access
1536 expect(vc.toQuery()).toEqual(
1537 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1538 );
1539
Nils Diewald52f7eb12015-01-12 17:30:04 +00001540 expect(vc.root().operands().length).toEqual(2);
1541
1542 // Add unspecified on the root group
1543 _andOn(vc.root().getOperand(1));
Nils Diewaldd5070b02015-01-11 01:44:47 +00001544 expect(vc.toQuery()).toEqual(
1545 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1546 );
Nils Diewald52f7eb12015-01-12 17:30:04 +00001547
Nils Diewaldd5070b02015-01-11 01:44:47 +00001548 expect(vc.root().ldType()).toEqual('docGroup');
Nils Diewald52f7eb12015-01-12 17:30:04 +00001549 expect(vc.root().operands().length).toEqual(3);
1550 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
Nils Diewaldd5070b02015-01-11 01:44:47 +00001551 expect(vc.root().getOperand(1).ldType()).toEqual('docGroup');
Nils Diewald52f7eb12015-01-12 17:30:04 +00001552 expect(vc.root().getOperand(1).operation()).toEqual('or');
1553 expect(vc.root().getOperand(2).ldType()).toEqual('non');
1554
1555 // Add another unspecified on the root group
1556 _andOn(vc.root().getOperand(1));
1557
1558 expect(vc.root().operands().length).toEqual(4);
1559 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
1560 expect(vc.root().getOperand(1).ldType()).toEqual('docGroup');
1561 expect(vc.root().getOperand(2).ldType()).toEqual('non');
1562 expect(vc.root().getOperand(3).ldType()).toEqual('non');
1563
1564 // Add another unspecified after the first doc
1565 _andOn(vc.root().getOperand(0));
1566
1567 expect(vc.root().operands().length).toEqual(5);
1568 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
1569 expect(vc.root().getOperand(1).ldType()).toEqual('non');
1570 expect(vc.root().getOperand(2).ldType()).toEqual('docGroup');
1571 expect(vc.root().getOperand(3).ldType()).toEqual('non');
1572 expect(vc.root().getOperand(4).ldType()).toEqual('non');
Nils Diewaldd5070b02015-01-11 01:44:47 +00001573 });
1574
Nils Diewaldd5070b02015-01-11 01:44:47 +00001575 it ('should wrap on root', function () {
1576 var vc = KorAP.VirtualCollection.render(
1577 {
1578 "@type": 'korap:docGroup',
1579 'operation' : 'operation:and',
1580 'operands' : [
1581 {
1582 "@type": 'korap:doc',
1583 "key": 'pubDate',
1584 "match": 'match:eq',
1585 "value": '2014-12-05',
1586 "type": 'type:date'
1587 },
1588 {
1589 "@type" : 'korap:doc',
1590 'key' : 'foo',
1591 'value' : 'bar'
1592 }
1593 ]
1594 }
1595 );
1596
Nils Diewald52f7eb12015-01-12 17:30:04 +00001597 // Wrap on root
Nils Diewaldd5070b02015-01-11 01:44:47 +00001598 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
Nils Diewald52f7eb12015-01-12 17:30:04 +00001599 expect(vc.root().ldType()).toEqual('docGroup');
1600 expect(vc.root().operation()).toEqual('and');
1601 _orOn(vc.root());
1602 expect(vc.root().ldType()).toEqual('docGroup');
1603 expect(vc.root().operation()).toEqual('or');
1604
1605 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
1606 expect(vc.root().getOperand(0).operation()).toEqual('and');
1607 });
1608
1609 it ('should add on root (case "and")', function () {
1610 var vc = KorAP.VirtualCollection.render(
1611 {
1612 "@type": 'korap:doc',
1613 "key": 'pubDate',
1614 "match": 'match:eq',
1615 "value": '2014-12-05',
1616 "type": 'type:date'
1617 }
1618 );
1619
1620 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
1621 expect(vc.root().ldType()).toEqual('doc');
1622 expect(vc.root().key()).toEqual('pubDate');
1623 expect(vc.root().value()).toEqual('2014-12-05');
1624
1625 // Wrap on root
1626 _andOn(vc.root());
1627 expect(vc.root().ldType()).toEqual('docGroup');
1628 expect(vc.root().operation()).toEqual('and');
1629 });
1630
1631 it ('should add on root (case "or")', function () {
1632 var vc = KorAP.VirtualCollection.render(
1633 {
1634 "@type": 'korap:doc',
1635 "key": 'pubDate',
1636 "match": 'match:eq',
1637 "value": '2014-12-05',
1638 "type": 'type:date'
1639 }
1640 );
1641
1642 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
1643 expect(vc.root().key()).toEqual('pubDate');
1644 expect(vc.root().value()).toEqual('2014-12-05');
1645
1646 // Wrap on root
1647 _orOn(vc.root());
Nils Diewaldd5070b02015-01-11 01:44:47 +00001648 expect(vc.root().ldType()).toEqual('docGroup');
1649 expect(vc.root().operation()).toEqual('or');
1650 });
Nils Diewalde15b7a22015-01-09 21:50:21 +00001651});