blob: da5ddaa03879b0dd3e2b51f3613ba2dfe393e93c [file] [log] [blame]
Nils Diewald6ac292b2015-01-15 21:33:21 +00001/*
2Todo: In demoSpec: Create "and" on the last element of the top "or"-Group
3
4*/
5
6
Nils Diewald0b6c0412014-12-19 03:55:57 +00007// Helper method for building factories
8buildFactory = function (objClass, defaults) {
9 return {
10 create : function (overwrites) {
11 var newObj = {};
12 for (var prop in defaults) {
13 newObj[prop] = defaults[prop];
14 };
15 for (var prop in overwrites) {
16 newObj[prop] = overwrites[prop];
17 };
Nils Diewaldf219eb82015-01-07 20:15:42 +000018 if (objClass === KorAP.VirtualCollection)
19 return objClass.render(newObj);
20 else
21 return objClass.create().fromJson(newObj);
Nils Diewald0b6c0412014-12-19 03:55:57 +000022 }
23 }
24};
25
Nils Diewald52f7eb12015-01-12 17:30:04 +000026function _andOn (obj) {
27 KorAP._and.bind(obj.element().lastChild.firstChild).apply();
28};
29
30function _orOn (obj) {
31 KorAP._or.bind(obj.element().lastChild.firstChild).apply();
32};
33
34function _delOn (obj) {
35 KorAP._delete.bind(obj.element().lastChild.firstChild).apply();
36};
37
38var demoFactory = buildFactory(KorAP.VirtualCollection, {
39 "@type":"korap:docGroup",
40 "operation":"operation:or",
41 "operands":[
42 {
43 "@type":"korap:docGroup",
44 "operation":"operation:and",
45 "operands":[
46 {
47 "@type":"korap:doc",
48 "key":"Titel",
49 "value":"Baum",
50 "match":"match:eq"
51 },
52 {
53 "@type":"korap:doc",
54 "key":"Veröffentlichungsort",
55 "value":"hihi",
56 "match":"match:eq"
57 },
58 {
59 "@type":"korap:docGroup",
60 "operation":"operation:or",
61 "operands":[
62 {
63 "@type":"korap:doc",
64 "key":"Titel",
65 "value":"Baum",
66 "match":"match:eq"
67 },
68 {
69 "@type":"korap:doc",
70 "key":"Veröffentlichungsort",
71 "value":"hihi",
72 "match":"match:eq"
73 }
74 ]
75 }
76 ]
77 },
78 {
79 "@type":"korap:doc",
80 "key":"Untertitel",
81 "value":"huhu",
82 "match":"match:eq"
83 }
84 ]
85});
86
87
Nils Diewald0b6c0412014-12-19 03:55:57 +000088describe('KorAP.Doc', function () {
89
90 // Create example factories
91 var stringFactory = buildFactory(KorAP.Doc, {
92 "key" : "author",
93 "value" : "Max Birkendale",
94 "@type" : "korap:doc"
95 });
96
97 // Create example factories
98 var dateFactory = buildFactory(KorAP.Doc, {
99 "key" : "pubDate",
100 "type" : "type:date",
101 "match" : "match:eq",
102 "value" : "2014-11-05",
103 "@type" : "korap:doc"
104 });
105
106 // Create example factories
107 var regexFactory = buildFactory(KorAP.Doc, {
108 "key" : "title",
109 "type" : "type:regex",
110 "value" : "[^b]ee.+?",
111 "@type" : "korap:doc"
112 });
113
114 it('should be initializable', function () {
115 var doc = KorAP.Doc.create();
116 expect(doc.matchop()).toEqual('eq');
117 expect(doc.key()).toBeUndefined();
118 expect(doc.value()).toBeUndefined();
119 expect(doc.type()).toEqual("string");
120 });
121
122 it('should be definable', function () {
123
124 // Empty doc
125 var doc = KorAP.Doc.create();
126
127 // Set values
128 doc.key("title");
129 doc.value("Der alte Mann");
130 expect(doc.matchop()).toEqual('eq');
131 expect(doc.key()).toEqual("title");
132 expect(doc.type()).toEqual("string");
133 expect(doc.value()).toEqual("Der alte Mann");
134 });
135
136
137 it('should deserialize JSON-LD string', function () {
138 var doc;
139
140 // String default
141 doc = stringFactory.create();
142 expect(doc.matchop()).toEqual('eq');
143 expect(doc.key()).toEqual("author");
144 expect(doc.type()).toEqual("string");
145 expect(doc.value()).toEqual("Max Birkendale");
146
147 // No valid string
148 doc = stringFactory.create({
149 value : undefined
150 });
151 expect(doc).toBeUndefined();
152
153 // No valid string
154 doc = stringFactory.create({
155 value : { "foo" : "bar" }
156 });
157 expect(doc).toBeUndefined();
158
159 // Change match type
160 doc = stringFactory.create({
161 "match" : "match:ne"
162 });
163
164 expect(doc.matchop()).toEqual('ne');
165 expect(doc.key()).toEqual("author");
166 expect(doc.type()).toEqual("string");
167 expect(doc.value()).toEqual("Max Birkendale");
168
169
170 // Invalid match type
171 doc = stringFactory.create({
172 "match" : { "foo" : "bar" }
173 });
174 expect(doc).toBeUndefined();
175 });
176
177 it('should deserialize JSON-LD regex', function () {
178 var doc = regexFactory.create();
179 expect(doc.key()).toEqual("title");
180 expect(doc.type()).toEqual("regex");
181 expect(doc.value()).toEqual("[^b]ee.+?");
182 expect(doc.matchop()).toEqual('eq');
183
184 // change matcher
185 doc = regexFactory.create({
186 match : "match:ne"
187 });
188 expect(doc.matchop()).toEqual('ne');
189
190 // Invalid matcher
191 doc = regexFactory.create({
192 match : "match:chook"
193 });
194 expect(doc).toBeUndefined();
195
196 // Invalid regex
197 doc = regexFactory.create({
198 value : "[^b"
199 });
200 expect(doc).toBeUndefined();
201 });
202
203 it('should deserialize JSON-LD date', function () {
204
205 // Normal date
206 doc = dateFactory.create({});
207
208 expect(doc.matchop()).toEqual('eq');
209 expect(doc.key()).toEqual("pubDate");
210 expect(doc.type()).toEqual("date");
211 expect(doc.value()).toEqual("2014-11-05");
212
213 // Short date 1
214 doc = dateFactory.create({
215 "value" : "2014-11"
216 });
217
218 expect(doc.matchop()).toEqual('eq');
219 expect(doc.key()).toEqual("pubDate");
220 expect(doc.type()).toEqual("date");
221 expect(doc.value()).toEqual("2014-11");
222
223 // Short date 2
224 doc = dateFactory.create({
225 "value" : "2014"
226 });
227
228 expect(doc.matchop()).toEqual('eq');
229 expect(doc.key()).toEqual("pubDate");
230 expect(doc.type()).toEqual("date");
231 expect(doc.value()).toEqual("2014");
232
233 // Invalid date!
234 doc = dateFactory.create({
235 "value" : "2014-11-050"
236 });
237 expect(doc).toBeUndefined();
238
239 // Invalid matcher!
240 doc = dateFactory.create({
241 "match" : "match:ne",
242 });
243 expect(doc).toBeUndefined();
244 });
245
Nils Diewaldf219eb82015-01-07 20:15:42 +0000246 it('should be serializale to JSON', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000247
248 // Empty doc
249 var doc = KorAP.Doc.create();
250 expect(doc.toJson()).toEqual(jasmine.any(Object));
251
252 // Serialize string
253 doc = stringFactory.create();
254 expect(doc.toJson()).toEqual(jasmine.objectContaining({
255 "@type" : "korap:doc",
256 "type" : "type:string",
257 "key" : "author",
258 "value" : "Max Birkendale",
259 "match" : "match:eq"
260 }));
261
262 // Serialize regex
263 doc = regexFactory.create();
264 expect(doc.toJson()).toEqual(jasmine.objectContaining({
265 "@type" : "korap:doc",
266 "type" : "type:regex",
267 "value" : "[^b]ee.+?",
268 "match" : "match:eq",
269 "key" : 'title'
270 }));
271
272 doc = regexFactory.create({
273 match: "match:ne"
274 });
275 expect(doc.toJson()).toEqual(jasmine.objectContaining({
276 "@type" : "korap:doc",
277 "type" : "type:regex",
278 "value" : "[^b]ee.+?",
279 "match" : "match:ne",
280 "key" : 'title'
281 }));
282
283 doc = dateFactory.create();
284 expect(doc.toJson()).toEqual(jasmine.objectContaining({
285 "@type" : "korap:doc",
286 "type" : "type:date",
287 "value" : "2014-11-05",
288 "match" : "match:eq",
289 "key" : 'pubDate'
290 }));
291
292 doc = dateFactory.create({
293 value : "2014"
294 });
295 expect(doc.toJson()).toEqual(jasmine.objectContaining({
296 "@type" : "korap:doc",
297 "type" : "type:date",
298 "value" : "2014",
299 "match" : "match:eq",
300 "key" : 'pubDate'
301 }));
302 });
Nils Diewaldf219eb82015-01-07 20:15:42 +0000303
304 it('should be serializale to String', function () {
305
306 // Empty doc
307 var doc = KorAP.Doc.create();
Nils Diewaldd599d542015-01-08 20:41:34 +0000308 expect(doc.toQuery()).toEqual("");
Nils Diewaldf219eb82015-01-07 20:15:42 +0000309
310 // Serialize string
311 doc = stringFactory.create();
Nils Diewaldd599d542015-01-08 20:41:34 +0000312 expect(doc.toQuery()).toEqual('author = "Max Birkendale"');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000313
314 // Serialize string with quotes
315 doc = stringFactory.create({ "value" : 'Max "Der Coole" Birkendate'});
Nils Diewaldd599d542015-01-08 20:41:34 +0000316 expect(doc.toQuery()).toEqual('author = "Max \\"Der Coole\\" Birkendate"');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000317
318 // Serialize regex
319 doc = regexFactory.create();
Nils Diewaldd599d542015-01-08 20:41:34 +0000320 expect(doc.toQuery()).toEqual('title = /[^b]ee.+?/');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000321
322 doc = regexFactory.create({
323 match: "match:ne"
324 });
Nils Diewaldd599d542015-01-08 20:41:34 +0000325 expect(doc.toQuery()).toEqual('title != /[^b]ee.+?/');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000326
327 doc = dateFactory.create();
Nils Diewaldd599d542015-01-08 20:41:34 +0000328 expect(doc.toQuery()).toEqual('pubDate in 2014-11-05');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000329
330 doc = dateFactory.create({
331 value : "2014"
332 });
Nils Diewaldd599d542015-01-08 20:41:34 +0000333 expect(doc.toQuery()).toEqual('pubDate in 2014');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000334 });
Nils Diewald0b6c0412014-12-19 03:55:57 +0000335});
336
337
338describe('KorAP.DocGroup', function () {
339 // Create example factories
340 var docFactory = buildFactory(
341 KorAP.Doc,
342 {
343 "@type" : "korap:doc",
344 "match":"match:eq",
345 "key" : "author",
346 "value" : "Max Birkendale"
347 }
348 );
349
350 var docGroupFactory = buildFactory(
351 KorAP.DocGroup, {
352 "@type" : "korap:docGroup",
353 "operation" : "operation:and",
354 "operands" : [
355 docFactory.create().toJson(),
356 docFactory.create({
357 "key" : "pubDate",
358 "type" : "type:date",
359 "value" : "2014-12-05"
360 }).toJson()
361 ]
362 });
363
Nils Diewald0b6c0412014-12-19 03:55:57 +0000364
Nils Diewald8f4e2542014-12-19 04:42:09 +0000365 it('should be initializable', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000366 // Create empty group
367 var docGroup = KorAP.DocGroup.create();
368 expect(docGroup.operation()).toEqual('and');
369
370 // Create empty group
Nils Diewald8f4e2542014-12-19 04:42:09 +0000371 docGroup = KorAP.DocGroup.create();
372 docGroup.operation('or');
Nils Diewald0b6c0412014-12-19 03:55:57 +0000373 expect(docGroup.operation()).toEqual('or');
374 });
375
376 it('should be definable', function () {
377
378 // Empty group
379 var docGroup = KorAP.DocGroup.create();
380 expect(docGroup.operation()).toEqual('and');
381
382 // Set values
383 docGroup.operation("or");
384 expect(docGroup.operation()).toEqual('or');
385
386 // Set invalid values
387 docGroup.operation("hui");
388 expect(docGroup.operation()).toEqual('or');
389 });
390
391 it('should be deserializable', function () {
392 var docGroup = docGroupFactory.create();
393 expect(docGroup.operation()).toEqual("and");
394 expect(docGroup.operands().length).toEqual(2);
395
396 var op1 = docGroup.getOperand(0);
397 expect(op1.type()).toEqual("string");
398 expect(op1.key()).toEqual("author");
399 expect(op1.value()).toEqual("Max Birkendale");
400 expect(op1.matchop()).toEqual("eq");
401
402 var op2 = docGroup.getOperand(1);
403 expect(op2.type()).toEqual("date");
404 expect(op2.key()).toEqual("pubDate");
405 expect(op2.value()).toEqual("2014-12-05");
406 expect(op2.matchop()).toEqual("eq");
407
Nils Diewaldf219eb82015-01-07 20:15:42 +0000408 // Append empty group
Nils Diewald966abf12014-12-20 02:27:45 +0000409 var newGroup = docGroup.append(KorAP.DocGroup.create());
Nils Diewald8f4e2542014-12-19 04:42:09 +0000410 newGroup.operation('or');
Nils Diewald966abf12014-12-20 02:27:45 +0000411 newGroup.append(docFactory.create());
412 newGroup.append(docFactory.create({
Nils Diewald0b6c0412014-12-19 03:55:57 +0000413 "type" : "type:regex",
414 "key" : "title",
415 "value" : "^e.+?$",
416 "match" : "match:ne"
417 }));
418
419 expect(docGroup.operation()).toEqual("and");
420 expect(docGroup.operands().length).toEqual(3);
421
422 var op1 = docGroup.getOperand(0);
423 expect(op1.ldType()).toEqual("doc");
424 expect(op1.type()).toEqual("string");
425 expect(op1.key()).toEqual("author");
426 expect(op1.value()).toEqual("Max Birkendale");
427 expect(op1.matchop()).toEqual("eq");
428
429 var op2 = docGroup.getOperand(1);
430 expect(op2.ldType()).toEqual("doc");
431 expect(op2.type()).toEqual("date");
432 expect(op2.key()).toEqual("pubDate");
433 expect(op2.value()).toEqual("2014-12-05");
434 expect(op2.matchop()).toEqual("eq");
435
436 var op3 = docGroup.getOperand(2);
437 expect(op3.ldType()).toEqual("docGroup");
438 expect(op3.operation()).toEqual("or");
439
440 var op4 = op3.getOperand(0);
441 expect(op4.ldType()).toEqual("doc");
442 expect(op4.type()).toEqual("string");
443 expect(op4.key()).toEqual("author");
444 expect(op4.value()).toEqual("Max Birkendale");
445 expect(op4.matchop()).toEqual("eq");
446
447 var op5 = op3.getOperand(1);
448 expect(op5.ldType()).toEqual("doc");
449 expect(op5.type()).toEqual("regex");
450 expect(op5.key()).toEqual("title");
451 expect(op5.value()).toEqual("^e.+?$");
452 expect(op5.matchop()).toEqual("ne");
453 });
454
Nils Diewaldf219eb82015-01-07 20:15:42 +0000455 it('should be serializable to JSON', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000456 var docGroup = docGroupFactory.create();
457
458 expect(docGroup.toJson()).toEqual(jasmine.objectContaining({
459 "@type" : "korap:docGroup",
460 "operation" : "operation:and",
461 "operands" : [
462 {
463 "@type": 'korap:doc',
464 "key" : 'author',
465 "match": 'match:eq',
466 "value": 'Max Birkendale',
467 "type": 'type:string'
468 },
469 {
470 "@type": 'korap:doc',
471 "key": 'pubDate',
472 "match": 'match:eq',
473 "value": '2014-12-05',
474 "type": 'type:date'
475 }
476 ]
477 }));
478 });
Nils Diewaldf219eb82015-01-07 20:15:42 +0000479
480 it('should be serializable to String', function () {
481 var docGroup = docGroupFactory.create();
Nils Diewaldd599d542015-01-08 20:41:34 +0000482 expect(docGroup.toQuery()).toEqual('author = "Max Birkendale" & pubDate in 2014-12-05');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000483
484 docGroup = docGroupFactory.create({
485 "@type" : "korap:docGroup",
486 "operation" : "operation:or",
487 "operands" : [
488 {
489 "@type": 'korap:doc',
490 "key" : 'author',
491 "match": 'match:eq',
492 "value": 'Max Birkendale',
493 "type": 'type:string'
494 },
495 {
496 "@type" : "korap:docGroup",
497 "operation" : "operation:and",
498 "operands" : [
499 {
500 "@type": 'korap:doc',
501 "key": 'pubDate',
502 "match": 'match:geq',
503 "value": '2014-05-12',
504 "type": 'type:date'
505 },
506 {
507 "@type": 'korap:doc',
508 "key": 'pubDate',
509 "match": 'match:leq',
510 "value": '2014-12-05',
511 "type": 'type:date'
512 },
513 {
514 "@type": 'korap:doc',
515 "key": 'foo',
516 "match": 'match:ne',
517 "value": '[a]?bar',
518 "type": 'type:regex'
519 }
520 ]
521 }
522 ]
523 });
Nils Diewaldd599d542015-01-08 20:41:34 +0000524 expect(docGroup.toQuery()).toEqual(
Nils Diewaldf219eb82015-01-07 20:15:42 +0000525 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05 & foo != /[a]?bar/)'
526 );
527 });
Nils Diewald0b6c0412014-12-19 03:55:57 +0000528});
529
Nils Diewald966abf12014-12-20 02:27:45 +0000530describe('KorAP.UnspecifiedDoc', function () {
531 it('should be initializable', function () {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000532 var doc = KorAP.UnspecifiedDoc.create();
533 var docElement = doc.element();
Nils Diewaldd599d542015-01-08 20:41:34 +0000534 expect(docElement.getAttribute('class')).toEqual('doc unspecified');
Nils Diewald966abf12014-12-20 02:27:45 +0000535 expect(docElement.firstChild.firstChild.data).toEqual('⋯');
Nils Diewalde15b7a22015-01-09 21:50:21 +0000536 expect(docElement.lastChild.lastChild.data).toEqual('⋯');
Nils Diewaldd599d542015-01-08 20:41:34 +0000537 expect(doc.toQuery()).toEqual('⋯');
Nils Diewald966abf12014-12-20 02:27:45 +0000538
Nils Diewald8f6b6102015-01-08 18:25:33 +0000539 // Only removable
Nils Diewald966abf12014-12-20 02:27:45 +0000540 expect(docElement.lastChild.children.length).toEqual(0);
541 });
542
543 it('should be removable, when no root', function () {
544 var docGroup = KorAP.DocGroup.create();
545 docGroup.operation('or');
546 expect(docGroup.operation()).toEqual('or');
547
548 docGroup.append({
549 "@type": 'korap:doc',
550 "key": 'pubDate',
551 "match": 'match:eq',
552 "value": '2014-12-05',
553 "type": 'type:date'
554 });
555
Nils Diewaldf219eb82015-01-07 20:15:42 +0000556 // Add unspecified object
557 docGroup.append();
558
Nils Diewald966abf12014-12-20 02:27:45 +0000559 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
560 expect(docGroup.element().children[0].getAttribute('class')).toEqual('doc');
561
Nils Diewald966abf12014-12-20 02:27:45 +0000562 var unspec = docGroup.element().children[1];
Nils Diewaldd599d542015-01-08 20:41:34 +0000563 expect(unspec.getAttribute('class')).toEqual('doc unspecified');
Nils Diewald966abf12014-12-20 02:27:45 +0000564
565 // Removable
566 expect(unspec.lastChild.children.length).toEqual(1);
567 expect(unspec.lastChild.children[0].getAttribute('class')).toEqual('delete');
568 });
569});
570
Nils Diewald8f4e2542014-12-19 04:42:09 +0000571describe('KorAP.Doc element', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000572 it('should be initializable', function () {
Nils Diewald8f4e2542014-12-19 04:42:09 +0000573 var docElement = KorAP.Doc.create(undefined, {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000574 "@type" : "korap:doc",
575 "key":"Titel",
576 "value":"Baum",
577 "match":"match:eq"
578 });
579 expect(docElement.key()).toEqual('Titel');
580 expect(docElement.matchop()).toEqual('eq');
581 expect(docElement.value()).toEqual('Baum');
582
583 var docE = docElement.element();
584 expect(docE.children[0].firstChild.data).toEqual('Titel');
585 expect(docE.children[1].firstChild.data).toEqual('eq');
586 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
587 expect(docE.children[2].firstChild.data).toEqual('Baum');
588 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
589
590 expect(docElement.toJson()).toEqual(jasmine.objectContaining({
591 "@type" : "korap:doc",
592 "key":"Titel",
593 "value":"Baum",
594 "match":"match:eq"
595 }));
596 });
597});
598
Nils Diewald8f4e2542014-12-19 04:42:09 +0000599describe('KorAP.DocGroup element', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000600 it('should be initializable', function () {
601
Nils Diewald8f4e2542014-12-19 04:42:09 +0000602 var docGroup = KorAP.DocGroup.create(undefined, {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000603 "@type" : "korap:docGroup",
604 "operation" : "operation:and",
605 "operands" : [
606 {
607 "@type": 'korap:doc',
608 "key" : 'author',
609 "match": 'match:eq',
610 "value": 'Max Birkendale',
611 "type": 'type:string'
612 },
613 {
614 "@type": 'korap:doc',
615 "key": 'pubDate',
616 "match": 'match:eq',
617 "value": '2014-12-05',
618 "type": 'type:date'
619 }
620 ]
621 });
622
Nils Diewald8f4e2542014-12-19 04:42:09 +0000623 expect(docGroup.operation()).toEqual('and');
624 var e = docGroup.element();
Nils Diewald0b6c0412014-12-19 03:55:57 +0000625 expect(e.getAttribute('class')).toEqual('docGroup');
626 expect(e.getAttribute('data-operation')).toEqual('and');
627
628 var first = e.children[0];
629 expect(first.getAttribute('class')).toEqual('doc');
630 expect(first.children[0].getAttribute('class')).toEqual('key');
631 expect(first.children[1].getAttribute('class')).toEqual('match');
632 expect(first.children[2].getAttribute('class')).toEqual('value');
633 expect(first.children[2].getAttribute('data-type')).toEqual('string');
634 expect(first.children[0].firstChild.data).toEqual('author');
635 expect(first.children[1].firstChild.data).toEqual('eq');
636 expect(first.children[2].firstChild.data).toEqual('Max Birkendale');
637
638 var second = e.children[1];
639 expect(second.getAttribute('class')).toEqual('doc');
640 expect(second.children[0].getAttribute('class')).toEqual('key');
641 expect(second.children[1].getAttribute('class')).toEqual('match');
642 expect(second.children[2].getAttribute('class')).toEqual('value');
643 expect(second.children[2].getAttribute('data-type')).toEqual('date');
644 expect(second.children[0].firstChild.data).toEqual('pubDate');
645 expect(second.children[1].firstChild.data).toEqual('eq');
646 expect(second.children[2].firstChild.data).toEqual('2014-12-05');
647
648 });
649
650 it('should be deserializable with nested groups', function () {
Nils Diewald8f4e2542014-12-19 04:42:09 +0000651 var docGroup = KorAP.DocGroup.create(undefined, {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000652 "@type" : "korap:docGroup",
653 "operation" : "operation:or",
654 "operands" : [
655 {
656 "@type": 'korap:doc',
657 "key" : 'author',
658 "match": 'match:eq',
659 "value": 'Max Birkendale',
660 "type": 'type:string'
661 },
662 {
663 "@type" : "korap:docGroup",
664 "operation" : "operation:and",
665 "operands" : [
666 {
667 "@type": 'korap:doc',
668 "key": 'pubDate',
669 "match": 'match:geq',
670 "value": '2014-05-12',
671 "type": 'type:date'
672 },
673 {
674 "@type": 'korap:doc',
675 "key": 'pubDate',
676 "match": 'match:leq',
677 "value": '2014-12-05',
678 "type": 'type:date'
679 }
680 ]
681 }
682 ]
683 });
684
Nils Diewald8f4e2542014-12-19 04:42:09 +0000685 expect(docGroup.operation()).toEqual('or');
686 var e = docGroup.element();
Nils Diewald0b6c0412014-12-19 03:55:57 +0000687 expect(e.getAttribute('class')).toEqual('docGroup');
688 expect(e.getAttribute('data-operation')).toEqual('or');
689
Nils Diewald966abf12014-12-20 02:27:45 +0000690 expect(e.children[0].getAttribute('class')).toEqual('doc');
691 var docop = e.children[0].lastChild;
692 expect(docop.getAttribute('class')).toEqual('operators');
693 expect(docop.children[0].getAttribute('class')).toEqual('and');
694 expect(docop.children[1].getAttribute('class')).toEqual('or');
695 expect(docop.children[2].getAttribute('class')).toEqual('delete');
696
697 expect(e.children[1].getAttribute('class')).toEqual('docGroup');
698 expect(e.children[1].getAttribute('data-operation')).toEqual('and');
699
700 // This and-operation can be "or"ed or "delete"d
701 var secop = e.children[1].children[2];
702 expect(secop.getAttribute('class')).toEqual('operators');
703 expect(secop.children[0].getAttribute('class')).toEqual('or');
704 expect(secop.children[1].getAttribute('class')).toEqual('delete');
705
706 // This or-operation can be "and"ed or "delete"d
707 expect(e.children[2].getAttribute('class')).toEqual('operators');
708 expect(e.lastChild.getAttribute('class')).toEqual('operators');
709 expect(e.lastChild.children[0].getAttribute('class')).toEqual('and');
710 expect(e.lastChild.children[1].getAttribute('class')).toEqual('delete');
Nils Diewald0b6c0412014-12-19 03:55:57 +0000711
712 });
713});
714
715describe('KorAP.VirtualCollection', function () {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000716
717 var simpleGroupFactory = buildFactory(KorAP.DocGroup, {
718 "@type" : "korap:docGroup",
719 "operation" : "operation:and",
720 "operands" : [
721 {
722 "@type": 'korap:doc',
723 "key" : 'author',
724 "match": 'match:eq',
725 "value": 'Max Birkendale',
726 "type": 'type:string'
727 },
728 {
729 "@type": 'korap:doc',
730 "key": 'pubDate',
731 "match": 'match:eq',
732 "value": '2014-12-05',
733 "type": 'type:date'
734 }
735 ]
736 });
737
738 var nestedGroupFactory = buildFactory(KorAP.VirtualCollection, {
739 "@type" : "korap:docGroup",
740 "operation" : "operation:or",
741 "operands" : [
742 {
743 "@type": 'korap:doc',
744 "key" : 'author',
745 "match": 'match:eq',
746 "value": 'Max Birkendale',
747 "type": 'type:string'
748 },
749 {
750 "@type" : "korap:docGroup",
751 "operation" : "operation:and",
752 "operands" : [
753 {
754 "@type": 'korap:doc',
755 "key": 'pubDate',
756 "match": 'match:geq',
757 "value": '2014-05-12',
758 "type": 'type:date'
759 },
760 {
761 "@type": 'korap:doc',
762 "key": 'pubDate',
763 "match": 'match:leq',
764 "value": '2014-12-05',
765 "type": 'type:date'
766 }
767 ]
768 }
769 ]
770 });
771
772 var flatGroupFactory = buildFactory(KorAP.VirtualCollection, {
773 "@type" : "korap:docGroup",
774 "operation" : "operation:and",
775 "operands" : [
776 {
777 "@type": 'korap:doc',
778 "key": 'pubDate',
779 "match": 'match:geq',
780 "value": '2014-05-12',
781 "type": 'type:date'
782 },
783 {
784 "@type": 'korap:doc',
785 "key": 'pubDate',
786 "match": 'match:leq',
787 "value": '2014-12-05',
788 "type": 'type:date'
789 },
790 {
791 "@type": 'korap:doc',
792 "key": 'foo',
793 "match": 'match:eq',
794 "value": 'bar',
795 "type": 'type:string'
796 }
797 ]
798 });
799
Nils Diewald0b6c0412014-12-19 03:55:57 +0000800 it('should be initializable', function () {
801 var vc = KorAP.VirtualCollection.render();
802 expect(vc.element().getAttribute('class')).toEqual('vc');
Nils Diewaldd599d542015-01-08 20:41:34 +0000803 expect(vc.root().element().getAttribute('class')).toEqual('doc unspecified');
Nils Diewald966abf12014-12-20 02:27:45 +0000804
805 // Not removable
806 expect(vc.root().element().lastChild.children.length).toEqual(0);
Nils Diewald0b6c0412014-12-19 03:55:57 +0000807 });
808
809 it('should be based on a doc', function () {
810 var vc = KorAP.VirtualCollection.render({
811 "@type" : "korap:doc",
812 "key":"Titel",
813 "value":"Baum",
814 "match":"match:eq"
815 });
816
817 expect(vc.element().getAttribute('class')).toEqual('vc');
818 expect(vc.root().element().getAttribute('class')).toEqual('doc');
819 expect(vc.root().key()).toEqual('Titel');
820 expect(vc.root().value()).toEqual('Baum');
821 expect(vc.root().matchop()).toEqual('eq');
822
823 var docE = vc.root().element();
824 expect(docE.children[0].firstChild.data).toEqual('Titel');
825 expect(docE.children[1].firstChild.data).toEqual('eq');
826 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
827 expect(docE.children[2].firstChild.data).toEqual('Baum');
828 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
829 });
830
831 it('should be based on a docGroup', function () {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000832 var vc = KorAP.VirtualCollection.render(simpleGroupFactory.create().toJson());
Nils Diewald0b6c0412014-12-19 03:55:57 +0000833
834 expect(vc.element().getAttribute('class')).toEqual('vc');
835 expect(vc.root().element().getAttribute('class')).toEqual('docGroup');
836 expect(vc.root().operation()).toEqual('and');
837
838 var docGroup = vc.root();
839
840 var first = docGroup.getOperand(0);
841 expect(first.key()).toEqual('author');
842 expect(first.value()).toEqual('Max Birkendale');
843 expect(first.matchop()).toEqual('eq');
844
845 var second = docGroup.getOperand(1);
846 expect(second.key()).toEqual('pubDate');
847 expect(second.value()).toEqual('2014-12-05');
848 expect(second.matchop()).toEqual('eq');
849 });
850
Nils Diewald5c817a42015-01-06 01:08:56 +0000851
Nils Diewald966abf12014-12-20 02:27:45 +0000852 it('should be based on a nested docGroup', function () {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000853 var vc = nestedGroupFactory.create();
854
Nils Diewald5c817a42015-01-06 01:08:56 +0000855 expect(vc.element().getAttribute('class')).toEqual('vc');
856 expect(vc.element().firstChild.getAttribute('class')).toEqual('docGroup');
857 expect(vc.element().firstChild.children[0].getAttribute('class')).toEqual('doc');
858 var dg = vc.element().firstChild.children[1];
859 expect(dg.getAttribute('class')).toEqual('docGroup');
860 expect(dg.children[0].getAttribute('class')).toEqual('doc');
861 expect(dg.children[1].getAttribute('class')).toEqual('doc');
862 expect(dg.children[2].getAttribute('class')).toEqual('operators');
863 expect(vc.element().firstChild.children[2].getAttribute('class')).toEqual('operators');
864 });
865
Nils Diewaldf219eb82015-01-07 20:15:42 +0000866 it('should be modifiable by deletion in flat docGroups', function () {
867 var vc = flatGroupFactory.create();
Nils Diewald0297ba12015-01-05 21:56:12 +0000868 var docGroup = vc.root();
Nils Diewaldf219eb82015-01-07 20:15:42 +0000869
870 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
871
Nils Diewald0297ba12015-01-05 21:56:12 +0000872 var doc = docGroup.getOperand(1);
873 expect(doc.key()).toEqual("pubDate");
Nils Diewaldf219eb82015-01-07 20:15:42 +0000874 expect(doc.value()).toEqual("2014-12-05");
Nils Diewald5c817a42015-01-06 01:08:56 +0000875
876 // Remove operand 1
Nils Diewaldf219eb82015-01-07 20:15:42 +0000877 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
Nils Diewald5c817a42015-01-06 01:08:56 +0000878 expect(doc._element).toEqual(undefined);
879
Nils Diewald0297ba12015-01-05 21:56:12 +0000880 doc = docGroup.getOperand(1);
881 expect(doc.key()).toEqual("foo");
Nils Diewald0297ba12015-01-05 21:56:12 +0000882
Nils Diewald5c817a42015-01-06 01:08:56 +0000883 // Remove operand 1
Nils Diewaldf219eb82015-01-07 20:15:42 +0000884 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
Nils Diewald5c817a42015-01-06 01:08:56 +0000885 expect(doc._element).toEqual(undefined);
Nils Diewald0297ba12015-01-05 21:56:12 +0000886
Nils Diewaldf219eb82015-01-07 20:15:42 +0000887 // Only one operand left ...
Nils Diewald5c817a42015-01-06 01:08:56 +0000888 expect(docGroup.getOperand(1)).toBeUndefined();
Nils Diewaldf219eb82015-01-07 20:15:42 +0000889 // ... but there shouldn't be a group anymore at all!
890 expect(docGroup.getOperand(0)).toBeUndefined();
891
892 var obj = vc.root();
893 expect(obj.ldType()).toEqual("doc");
894 expect(obj.key()).toEqual("pubDate");
895 expect(obj.value()).toEqual("2014-05-12");
896
897 expect(obj.element().getAttribute('class')).toEqual('doc');
Nils Diewald5c817a42015-01-06 01:08:56 +0000898 });
Nils Diewaldf219eb82015-01-07 20:15:42 +0000899
900
901 it('should be modifiable by deletion in nested docGroups (root case)', function () {
902 var vc = nestedGroupFactory.create();
903
Nils Diewaldd599d542015-01-08 20:41:34 +0000904 expect(vc.toQuery()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +0000905 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05)'
906 );
907
Nils Diewaldf219eb82015-01-07 20:15:42 +0000908 var docGroup = vc.root();
909 expect(docGroup.ldType()).toEqual("docGroup");
910 expect(docGroup.operation()).toEqual("or");
911
912 var doc = docGroup.getOperand(0);
913 expect(doc.key()).toEqual("author");
914 expect(doc.value()).toEqual("Max Birkendale");
915
916 docGroup = docGroup.getOperand(1);
917 expect(docGroup.operation()).toEqual("and");
918
919 doc = docGroup.getOperand(0);
920 expect(doc.key()).toEqual("pubDate");
921 expect(doc.matchop()).toEqual("geq");
922 expect(doc.value()).toEqual("2014-05-12");
923 expect(doc.type()).toEqual("date");
924
925 doc = docGroup.getOperand(1);
926 expect(doc.key()).toEqual("pubDate");
927 expect(doc.matchop()).toEqual("leq");
928 expect(doc.value()).toEqual("2014-12-05");
929 expect(doc.type()).toEqual("date");
930
931 // Remove first operand so everything becomes root
932 expect(
933 vc.root().delOperand(
934 vc.root().getOperand(0)
935 ).update().ldType()
936 ).toEqual("docGroup");
Nils Diewald8e7182e2015-01-08 15:02:07 +0000937
Nils Diewaldf219eb82015-01-07 20:15:42 +0000938 expect(vc.root().ldType()).toEqual("docGroup");
939 expect(vc.root().operation()).toEqual("and");
Nils Diewald8e7182e2015-01-08 15:02:07 +0000940 expect(vc.root().getOperand(0).ldType()).toEqual("doc");
941
Nils Diewaldd599d542015-01-08 20:41:34 +0000942 expect(vc.toQuery()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +0000943 'pubDate since 2014-05-12 & pubDate until 2014-12-05'
944 );
Nils Diewaldf219eb82015-01-07 20:15:42 +0000945 });
946
947 it('should be modifiable by deletion in nested docGroups (resolve group case)', function () {
948 var vc = nestedGroupFactory.create();
949
950 // Get nested group
951 var firstGroup = vc.root().getOperand(1);
952 firstGroup.append(simpleGroupFactory.create({ "operation" : "operation:or" }));
953
954 // Structur is now:
955 // or(doc, and(doc, doc, or(doc, doc)))
956
957 // Get nested or in and
958 var orGroup = vc.root().getOperand(1).getOperand(2);
959 expect(orGroup.ldType()).toEqual("docGroup");
960 expect(orGroup.operation()).toEqual("or");
961
962 // Remove
963 // Structur is now:
964 // or(doc, and(doc, doc, doc)))
965 expect(orGroup.delOperand(orGroup.getOperand(0)).update().operation()).toEqual("and");
966 expect(vc.root().getOperand(1).operands().length).toEqual(3);
967 });
968
969 it('should be modifiable by deletion in nested docGroups (identical group case)', function () {
970 var vc = nestedGroupFactory.create();
971
972 // Get nested group
973 var firstGroup = vc.root().getOperand(1);
Nils Diewald8e7182e2015-01-08 15:02:07 +0000974 firstGroup.append(simpleGroupFactory.create({
975 "operation" : "operation:or"
976 }));
977 var oldAuthor = firstGroup.getOperand(2).getOperand(0);
978 oldAuthor.key("title");
979 oldAuthor.value("Der Birnbaum");
Nils Diewaldf219eb82015-01-07 20:15:42 +0000980
981 // Structur is now:
982 // or(doc, and(doc, doc, or(doc, doc)))
Nils Diewaldd599d542015-01-08 20:41:34 +0000983 expect(vc.toQuery()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +0000984 '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 +0000985 );
986
987 var andGroup = vc.root().getOperand(1);
988
989 // Get leading docs in and
990 var doc1 = andGroup.getOperand(0);
991 expect(doc1.ldType()).toEqual("doc");
992 expect(doc1.value()).toEqual("2014-05-12");
993 var doc2 = andGroup.getOperand(1);
994 expect(doc2.ldType()).toEqual("doc");
995 expect(doc2.value()).toEqual("2014-12-05");
996
997 // Remove 2
998 expect(andGroup.delOperand(doc2).update().operation()).toEqual("and");
999 // Structur is now:
1000 // or(doc, and(doc, or(doc, doc)))
1001
Nils Diewaldd599d542015-01-08 20:41:34 +00001002 expect(vc.toQuery()).toEqual(
1003 'author = "Max Birkendale"' +
1004 ' | (pubDate since 2014-05-12 & ' +
1005 '(title = "Der Birnbaum" | pubDate in 2014-12-05))'
Nils Diewaldf219eb82015-01-07 20:15:42 +00001006 );
1007
1008
1009 // Remove 1
1010 expect(andGroup.delOperand(doc1).update().operation()).toEqual("or");
1011 // Structur is now:
1012 // or(doc, doc, doc)
1013
Nils Diewaldd599d542015-01-08 20:41:34 +00001014 expect(vc.toQuery()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +00001015 'author = "Max Birkendale" | title = "Der Birnbaum" | pubDate in 2014-12-05'
Nils Diewaldf219eb82015-01-07 20:15:42 +00001016 );
1017 });
Nils Diewald4019bd22015-01-08 19:57:50 +00001018
1019 it('should be reducible to unspecification', function () {
1020 var vc = demoFactory.create();
1021
Nils Diewaldd599d542015-01-08 20:41:34 +00001022 expect(vc.toQuery()).toEqual(vc.root().toQuery());
Nils Diewald4019bd22015-01-08 19:57:50 +00001023
Nils Diewaldd599d542015-01-08 20:41:34 +00001024 expect(vc.toQuery()).toEqual(
1025 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & ' +
1026 '(Titel = "Baum" | Veröffentlichungsort = "hihi")) ' +
1027 '| Untertitel = "huhu"');
Nils Diewald4019bd22015-01-08 19:57:50 +00001028
1029 expect(vc.root().element().lastChild.children[0].firstChild.nodeValue).toEqual('and');
1030 expect(vc.root().element().lastChild.children[1].firstChild.nodeValue).toEqual('×');
Nils Diewald4019bd22015-01-08 19:57:50 +00001031 expect(vc.root().delOperand(vc.root().getOperand(0)).update()).not.toBeUndefined();
Nils Diewaldd599d542015-01-08 20:41:34 +00001032 expect(vc.toQuery()).toEqual('Untertitel = "huhu"');
Nils Diewald4019bd22015-01-08 19:57:50 +00001033
Nils Diewaldd599d542015-01-08 20:41:34 +00001034 var lc = vc.root().element().lastChild;
1035 expect(lc.children[0].firstChild.nodeValue).toEqual('and');
1036 expect(lc.children[1].firstChild.nodeValue).toEqual('or');
1037 expect(lc.children[2].firstChild.nodeValue).toEqual('×');
Nils Diewald4019bd22015-01-08 19:57:50 +00001038
1039 // Clean everything
1040 vc.clean();
Nils Diewaldd599d542015-01-08 20:41:34 +00001041 expect(vc.toQuery()).toEqual('⋯');
Nils Diewald4019bd22015-01-08 19:57:50 +00001042 });
Nils Diewald0b6c0412014-12-19 03:55:57 +00001043});
1044
1045describe('KorAP.Operators', function () {
1046 it('should be initializable', function () {
1047 var op = KorAP.Operators.create(true, false, false);
1048 expect(op.and()).toBeTruthy();
1049 expect(op.or()).not.toBeTruthy();
1050 expect(op.del()).not.toBeTruthy();
1051
1052 op.and(false);
1053 expect(op.and()).not.toBeTruthy();
1054 expect(op.or()).not.toBeTruthy();
1055 expect(op.del()).not.toBeTruthy();
1056
1057 op.or(true);
1058 op.del(true);
1059 expect(op.and()).not.toBeTruthy();
1060 expect(op.or()).toBeTruthy();
1061 expect(op.del()).toBeTruthy();
1062
1063 var e = op.element();
1064 expect(e.getAttribute('class')).toEqual('operators');
1065 expect(e.children[0].getAttribute('class')).toEqual('or');
1066 expect(e.children[0].firstChild.data).toEqual('or');
1067 expect(e.children[1].getAttribute('class')).toEqual('delete');
1068 expect(e.children[1].firstChild.data).toEqual('×');
1069
1070 op.and(true);
1071 op.del(false);
1072 op.update();
1073
1074 e = op.element();
1075 expect(e.getAttribute('class')).toEqual('operators');
1076 expect(e.children[0].getAttribute('class')).toEqual('and');
1077 expect(e.children[0].firstChild.data).toEqual('and');
1078 expect(e.children[1].getAttribute('class')).toEqual('or');
1079 expect(e.children[1].firstChild.data).toEqual('or');
1080 });
1081});
Nils Diewalde15b7a22015-01-09 21:50:21 +00001082
1083describe('KorAP._delete (event)', function () {
1084 var complexVCFactory = buildFactory(KorAP.VirtualCollection,{
1085 "@type": 'korap:docGroup',
1086 'operation' : 'operation:and',
1087 'operands' : [
1088 {
1089 "@type": 'korap:doc',
1090 "key": 'pubDate',
1091 "match": 'match:eq',
1092 "value": '2014-12-05',
1093 "type": 'type:date'
1094 },
1095 {
1096 "@type" : 'korap:docGroup',
1097 'operation' : 'operation:or',
1098 'operands' : [
1099 {
1100 '@type' : 'korap:doc',
1101 'key' : 'title',
1102 'value' : 'Hello World!'
1103 },
1104 {
1105 '@type' : 'korap:doc',
1106 'key' : 'foo',
1107 'value' : 'bar'
1108 }
1109 ]
1110 }
1111 ]
1112 });
1113
1114 it('should clean on root docs', function () {
1115 var vc = KorAP.VirtualCollection.render({
1116 "@type": 'korap:doc',
1117 "key": 'pubDate',
1118 "match": 'match:eq',
1119 "value": '2014-12-05',
1120 "type": 'type:date'
1121 });
1122 expect(vc.root().toQuery()).toEqual('pubDate in 2014-12-05');
1123 expect(vc.root().element().lastChild.getAttribute('class')).toEqual('operators');
1124
1125 // Clean with delete from root
1126 expect(vc.root().element().lastChild.lastChild.getAttribute('class')).toEqual('delete');
Nils Diewald52f7eb12015-01-12 17:30:04 +00001127 _delOn(vc.root());
Nils Diewalde15b7a22015-01-09 21:50:21 +00001128 expect(vc.root().toQuery()).toEqual('⋯');
1129 expect(vc.root().element().lastChild.lastChild.data).toEqual('⋯');
1130 });
1131
1132 it ('should remove on nested docs', function () {
1133 var vc = KorAP.VirtualCollection.render(
1134 {
1135 "@type": 'korap:docGroup',
1136 'operation' : 'operation:and',
1137 'operands' : [
1138 {
1139 "@type": 'korap:doc',
1140 "key": 'pubDate',
1141 "match": 'match:eq',
1142 "value": '2014-12-05',
1143 "type": 'type:date'
1144 },
1145 {
1146 "@type" : 'korap:doc',
1147 'key' : 'foo',
1148 'value' : 'bar'
1149 }
1150 ]
1151 }
1152 );
1153
1154 // Delete with direct element access
1155 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
Nils Diewald52f7eb12015-01-12 17:30:04 +00001156 _delOn(vc.root().getOperand(0));
1157
Nils Diewalde15b7a22015-01-09 21:50:21 +00001158 expect(vc.toQuery()).toEqual('foo = "bar"');
1159 expect(vc.root().ldType()).toEqual('doc');
1160 });
1161
1162 it ('should clean on doc groups', function () {
1163 var vc = KorAP.VirtualCollection.render(
1164 {
1165 "@type": 'korap:docGroup',
1166 'operation' : 'operation:and',
1167 'operands' : [
1168 {
1169 "@type": 'korap:doc',
1170 "key": 'pubDate',
1171 "match": 'match:eq',
1172 "value": '2014-12-05',
1173 "type": 'type:date'
1174 },
1175 {
1176 "@type" : 'korap:doc',
1177 'key' : 'foo',
1178 'value' : 'bar'
1179 }
1180 ]
1181 }
1182 );
1183
1184 // Cleanwith direct element access
1185 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
Nils Diewald52f7eb12015-01-12 17:30:04 +00001186 _delOn(vc.root());
Nils Diewalde15b7a22015-01-09 21:50:21 +00001187 expect(vc.toQuery()).toEqual('⋯');
1188 expect(vc.root().ldType()).toEqual('non');
1189 });
1190
1191 it ('should remove on nested doc groups (case of ungrouping 1)', function () {
1192 var vc = complexVCFactory.create();
1193
1194 // Delete with direct element access
1195 expect(vc.toQuery()).toEqual(
1196 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1197 );
1198
1199 // Remove hello world:
Nils Diewald52f7eb12015-01-12 17:30:04 +00001200 _delOn(vc.root().getOperand(1).getOperand(0));
Nils Diewalde15b7a22015-01-09 21:50:21 +00001201 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1202 expect(vc.root().ldType()).toEqual('docGroup');
1203 });
1204
1205 it ('should remove on nested doc groups (case of ungrouping 2)', function () {
1206 var vc = complexVCFactory.create();
1207
1208 // Delete with direct element access
1209 expect(vc.toQuery()).toEqual(
1210 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1211 );
1212
1213 // Remove bar
Nils Diewald52f7eb12015-01-12 17:30:04 +00001214 _delOn(vc.root().getOperand(1).getOperand(1));
Nils Diewalde15b7a22015-01-09 21:50:21 +00001215 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & title = "Hello World!"');
1216 expect(vc.root().ldType()).toEqual('docGroup');
1217 expect(vc.root().operation()).toEqual('and');
1218 });
1219
1220 it ('should remove on nested doc groups (case of root changing)', function () {
1221 var vc = complexVCFactory.create();
1222
1223 // Delete with direct element access
1224 expect(vc.toQuery()).toEqual(
1225 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1226 );
1227
1228 // Remove bar
Nils Diewald52f7eb12015-01-12 17:30:04 +00001229 _delOn(vc.root().getOperand(0));
Nils Diewalde15b7a22015-01-09 21:50:21 +00001230 expect(vc.toQuery()).toEqual('title = "Hello World!" | foo = "bar"');
1231 expect(vc.root().ldType()).toEqual('docGroup');
1232 expect(vc.root().operation()).toEqual('or');
1233 });
1234
1235 it ('should remove on nested doc groups (list flattening)', function () {
1236 var vc = KorAP.VirtualCollection.render(
1237 {
1238 "@type": 'korap:docGroup',
1239 'operation' : 'operation:or',
1240 'operands' : [
1241 {
1242 "@type": 'korap:doc',
1243 "key": 'pubDate',
1244 "match": 'match:eq',
1245 "value": '2014-12-05',
1246 "type": 'type:date'
1247 },
1248 {
1249 "@type" : 'korap:doc',
1250 'key' : 'foo',
1251 'value' : 'bar'
1252 },
1253 {
1254 "@type": 'korap:docGroup',
1255 'operation' : 'operation:and',
1256 'operands' : [
1257 {
1258 "@type": 'korap:doc',
1259 "key": 'pubDate',
1260 "match": 'match:eq',
1261 "value": '2014-12-05',
1262 "type": 'type:date'
1263 },
1264 {
1265 "@type" : 'korap:docGroup',
1266 'operation' : 'operation:or',
1267 'operands' : [
1268 {
1269 '@type' : 'korap:doc',
1270 'key' : 'title',
1271 'value' : 'Hello World!'
1272 },
1273 {
1274 '@type' : 'korap:doc',
1275 'key' : 'yeah',
1276 'value' : 'juhu'
1277 }
1278 ]
1279 }
1280 ]
1281 }
1282 ]
1283 }
1284 );
1285
1286 // Delete with direct element access
1287 expect(vc.toQuery()).toEqual(
1288 'pubDate in 2014-12-05 | foo = "bar" | ' +
1289 '(pubDate in 2014-12-05 & ' +
1290 '(title = "Hello World!" | yeah = "juhu"))'
1291 );
1292
1293 expect(vc.root().ldType()).toEqual('docGroup');
1294 expect(vc.root().operation()).toEqual('or');
1295
1296 // Operands and operators
1297 expect(vc.element().firstChild.children.length).toEqual(4);
1298 expect(vc.element().firstChild.lastChild.getAttribute('class')).toEqual('operators');
1299
1300 // Remove inner group and flatten
Nils Diewald52f7eb12015-01-12 17:30:04 +00001301 _delOn(vc.root().getOperand(2).getOperand(0));
Nils Diewalde15b7a22015-01-09 21:50:21 +00001302
1303 expect(vc.toQuery()).toEqual(
1304 'pubDate in 2014-12-05 | foo = "bar" | title = "Hello World!" | yeah = "juhu"'
1305 );
1306 expect(vc.root().ldType()).toEqual('docGroup');
1307 expect(vc.root().operation()).toEqual('or');
1308
1309 // Operands and operators
1310 expect(vc.element().firstChild.children.length).toEqual(5);
1311 expect(vc.element().firstChild.lastChild.getAttribute('class')).toEqual('operators');
1312 });
1313});
1314
1315describe('KorAP._add (event)', function () {
1316 var complexVCFactory = buildFactory(KorAP.VirtualCollection,{
1317 "@type": 'korap:docGroup',
1318 'operation' : 'operation:and',
1319 'operands' : [
1320 {
1321 "@type": 'korap:doc',
1322 "key": 'pubDate',
1323 "match": 'match:eq',
1324 "value": '2014-12-05',
1325 "type": 'type:date'
1326 },
1327 {
1328 "@type" : 'korap:docGroup',
1329 'operation' : 'operation:or',
1330 'operands' : [
1331 {
1332 '@type' : 'korap:doc',
1333 'key' : 'title',
1334 'value' : 'Hello World!'
1335 },
1336 {
1337 '@type' : 'korap:doc',
1338 'key' : 'foo',
1339 'value' : 'bar'
1340 }
1341 ]
1342 }
1343 ]
1344 });
1345
1346 it ('should add new unspecified doc with "and"', function () {
1347 var vc = KorAP.VirtualCollection.render(
1348 {
1349 "@type": 'korap:docGroup',
1350 'operation' : 'operation:and',
1351 'operands' : [
1352 {
1353 "@type": 'korap:doc',
1354 "key": 'pubDate',
1355 "match": 'match:eq',
1356 "value": '2014-12-05',
1357 "type": 'type:date'
1358 },
1359 {
1360 "@type" : 'korap:doc',
1361 'key' : 'foo',
1362 'value' : 'bar'
1363 }
1364 ]
1365 }
1366 );
1367
1368 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1369
1370 var fc = vc.element().firstChild;
1371 expect(fc.getAttribute('data-operation')).toEqual('and');
1372 expect(fc.children.length).toEqual(3);
1373 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1374 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1375 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1376
1377 // add with 'and' in the middle
Nils Diewald52f7eb12015-01-12 17:30:04 +00001378 _andOn(vc.root().getOperand(0));
Nils Diewalde15b7a22015-01-09 21:50:21 +00001379 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1380
1381 fc = vc.element().firstChild;
1382 expect(fc.getAttribute('data-operation')).toEqual('and');
1383 expect(fc.children.length).toEqual(4);
1384 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1385
1386 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1387 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
1388 expect(fc.children[2].getAttribute('class')).toEqual('doc');
1389 });
1390
1391 it ('should add new unspecified doc with "or"', function () {
1392 var vc = KorAP.VirtualCollection.render(
1393 {
1394 "@type": 'korap:docGroup',
1395 'operation' : 'operation:and',
1396 'operands' : [
1397 {
1398 "@type": 'korap:doc',
1399 "key": 'pubDate',
1400 "match": 'match:eq',
1401 "value": '2014-12-05',
1402 "type": 'type:date'
1403 },
1404 {
1405 "@type" : 'korap:doc',
1406 'key' : 'foo',
1407 'value' : 'bar'
1408 }
1409 ]
1410 }
1411 );
1412
1413 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1414
1415 var fc = vc.element().firstChild;
1416 expect(fc.children.length).toEqual(3);
1417 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1418 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1419 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1420
1421 // add with 'or' in the middle
Nils Diewald52f7eb12015-01-12 17:30:04 +00001422 _orOn(vc.root().getOperand(0));
Nils Diewalde15b7a22015-01-09 21:50:21 +00001423 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1424 fc = vc.element().firstChild;
1425
1426 expect(fc.getAttribute('data-operation')).toEqual('and');
1427 expect(fc.children.length).toEqual(3);
1428 expect(fc.children[0].getAttribute('class')).toEqual('docGroup');
1429 expect(fc.children[0].getAttribute('data-operation')).toEqual('or');
1430 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1431 expect(fc.children[2].getAttribute('class')).toEqual('operators');
1432 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1433
1434 fc = vc.element().firstChild.firstChild;
1435 expect(fc.children.length).toEqual(3);
1436 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1437 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
1438 expect(fc.children[2].getAttribute('class')).toEqual('operators');
1439 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1440 });
1441
Nils Diewald52f7eb12015-01-12 17:30:04 +00001442 it ('should add new unspecified doc with "and" before group', function () {
1443 var vc = demoFactory.create();
1444
1445 // Wrap with direct element access
1446 expect(vc.toQuery()).toEqual(
1447 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & (Titel = "Baum" | Veröffentlichungsort = "hihi")) | Untertitel = "huhu"'
1448 );
1449
1450 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
1451 expect(vc.root().getOperand(0).operation()).toEqual('and');
1452 expect(vc.root().getOperand(0).operands().length).toEqual(3);
1453
1454 // Add unspecified on the second doc
1455 var secDoc = vc.root().getOperand(0).getOperand(1);
1456 expect(secDoc.value()).toEqual('hihi');
1457
1458 // Add
1459 _andOn(secDoc);
1460
1461 var fo = vc.root().getOperand(0);
1462
1463 expect(fo.ldType()).toEqual('docGroup');
1464 expect(fo.operation()).toEqual('and');
1465 expect(fo.operands().length).toEqual(4);
1466
1467 expect(fo.getOperand(0).ldType()).toEqual('doc');
1468 expect(fo.getOperand(1).ldType()).toEqual('doc');
1469 expect(fo.getOperand(2).ldType()).toEqual('non');
1470 expect(fo.getOperand(3).ldType()).toEqual('docGroup');
1471 });
1472
1473
1474 it ('should remove a doc with an unspecified doc in a nested group', function () {
1475 var vc = demoFactory.create();
1476
1477 // Wrap with direct element access
1478 expect(vc.toQuery()).toEqual(
1479 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & (Titel = "Baum" | Veröffentlichungsort = "hihi")) | Untertitel = "huhu"'
1480 );
1481
1482 var fo = vc.root().getOperand(0).getOperand(0);
1483 expect(fo.key()).toEqual('Titel');
1484 expect(fo.value()).toEqual('Baum');
1485
1486 // Add unspecified on the root group
1487 _orOn(fo);
1488
1489 fo = vc.root().getOperand(0).getOperand(0);
1490
1491 expect(fo.operation()).toEqual('or');
1492 expect(fo.getOperand(0).ldType()).toEqual('doc');
1493 expect(fo.getOperand(1).ldType()).toEqual('non');
1494
1495 // Delete document
1496 _delOn(fo.getOperand(0));
1497
1498 // The operand is now non
1499 expect(vc.root().getOperand(0).getOperand(0).ldType()).toEqual('non');
1500 expect(vc.root().getOperand(0).getOperand(1).ldType()).toEqual('doc');
1501 expect(vc.root().getOperand(0).getOperand(2).ldType()).toEqual('docGroup');
1502 });
1503
1504 it ('should remove an unspecified doc with an doc in a nested group', function () {
1505 var vc = demoFactory.create();
1506
1507 // Wrap with direct element access
1508 expect(vc.toQuery()).toEqual(
1509 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & (Titel = "Baum" | Veröffentlichungsort = "hihi")) | Untertitel = "huhu"'
1510 );
1511
1512 var fo = vc.root().getOperand(0).getOperand(0);
1513 expect(fo.key()).toEqual('Titel');
1514 expect(fo.value()).toEqual('Baum');
1515
1516 // Add unspecified on the root group
1517 _orOn(fo);
1518
1519 fo = vc.root().getOperand(0).getOperand(0);
1520
1521 expect(fo.operation()).toEqual('or');
1522 expect(fo.getOperand(0).ldType()).toEqual('doc');
1523 expect(fo.getOperand(1).ldType()).toEqual('non');
1524
1525 // Delete unspecified doc
1526 _delOn(fo.getOperand(1));
1527
1528 // The operand is now non
1529 fo = vc.root().getOperand(0);
1530 expect(fo.getOperand(0).ldType()).toEqual('doc');
1531 expect(fo.getOperand(0).key()).toEqual('Titel');
1532 expect(fo.getOperand(0).value()).toEqual('Baum');
1533 expect(fo.getOperand(1).ldType()).toEqual('doc');
1534 expect(fo.getOperand(2).ldType()).toEqual('docGroup');
1535 });
1536
1537
1538 it ('should add on parent group (case "and")', function () {
Nils Diewaldd5070b02015-01-11 01:44:47 +00001539 var vc = complexVCFactory.create();
1540
1541 // Wrap with direct element access
1542 expect(vc.toQuery()).toEqual(
1543 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1544 );
1545
Nils Diewald52f7eb12015-01-12 17:30:04 +00001546 expect(vc.root().operands().length).toEqual(2);
1547
1548 // Add unspecified on the root group
1549 _andOn(vc.root().getOperand(1));
Nils Diewaldd5070b02015-01-11 01:44:47 +00001550 expect(vc.toQuery()).toEqual(
1551 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1552 );
Nils Diewald52f7eb12015-01-12 17:30:04 +00001553
Nils Diewaldd5070b02015-01-11 01:44:47 +00001554 expect(vc.root().ldType()).toEqual('docGroup');
Nils Diewald52f7eb12015-01-12 17:30:04 +00001555 expect(vc.root().operands().length).toEqual(3);
1556 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
Nils Diewaldd5070b02015-01-11 01:44:47 +00001557 expect(vc.root().getOperand(1).ldType()).toEqual('docGroup');
Nils Diewald52f7eb12015-01-12 17:30:04 +00001558 expect(vc.root().getOperand(1).operation()).toEqual('or');
1559 expect(vc.root().getOperand(2).ldType()).toEqual('non');
1560
1561 // Add another unspecified on the root group
1562 _andOn(vc.root().getOperand(1));
1563
1564 expect(vc.root().operands().length).toEqual(4);
1565 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
1566 expect(vc.root().getOperand(1).ldType()).toEqual('docGroup');
1567 expect(vc.root().getOperand(2).ldType()).toEqual('non');
1568 expect(vc.root().getOperand(3).ldType()).toEqual('non');
1569
1570 // Add another unspecified after the first doc
1571 _andOn(vc.root().getOperand(0));
1572
1573 expect(vc.root().operands().length).toEqual(5);
1574 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
1575 expect(vc.root().getOperand(1).ldType()).toEqual('non');
1576 expect(vc.root().getOperand(2).ldType()).toEqual('docGroup');
1577 expect(vc.root().getOperand(3).ldType()).toEqual('non');
1578 expect(vc.root().getOperand(4).ldType()).toEqual('non');
Nils Diewaldd5070b02015-01-11 01:44:47 +00001579 });
1580
Nils Diewaldd5070b02015-01-11 01:44:47 +00001581 it ('should wrap on root', function () {
1582 var vc = KorAP.VirtualCollection.render(
1583 {
1584 "@type": 'korap:docGroup',
1585 'operation' : 'operation:and',
1586 'operands' : [
1587 {
1588 "@type": 'korap:doc',
1589 "key": 'pubDate',
1590 "match": 'match:eq',
1591 "value": '2014-12-05',
1592 "type": 'type:date'
1593 },
1594 {
1595 "@type" : 'korap:doc',
1596 'key' : 'foo',
1597 'value' : 'bar'
1598 }
1599 ]
1600 }
1601 );
1602
Nils Diewald52f7eb12015-01-12 17:30:04 +00001603 // Wrap on root
Nils Diewaldd5070b02015-01-11 01:44:47 +00001604 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
Nils Diewald52f7eb12015-01-12 17:30:04 +00001605 expect(vc.root().ldType()).toEqual('docGroup');
1606 expect(vc.root().operation()).toEqual('and');
1607 _orOn(vc.root());
1608 expect(vc.root().ldType()).toEqual('docGroup');
1609 expect(vc.root().operation()).toEqual('or');
1610
1611 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
1612 expect(vc.root().getOperand(0).operation()).toEqual('and');
1613 });
1614
1615 it ('should add on root (case "and")', function () {
1616 var vc = KorAP.VirtualCollection.render(
1617 {
1618 "@type": 'korap:doc',
1619 "key": 'pubDate',
1620 "match": 'match:eq',
1621 "value": '2014-12-05',
1622 "type": 'type:date'
1623 }
1624 );
1625
1626 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
1627 expect(vc.root().ldType()).toEqual('doc');
1628 expect(vc.root().key()).toEqual('pubDate');
1629 expect(vc.root().value()).toEqual('2014-12-05');
1630
1631 // Wrap on root
1632 _andOn(vc.root());
1633 expect(vc.root().ldType()).toEqual('docGroup');
1634 expect(vc.root().operation()).toEqual('and');
1635 });
1636
1637 it ('should add on root (case "or")', function () {
1638 var vc = KorAP.VirtualCollection.render(
1639 {
1640 "@type": 'korap:doc',
1641 "key": 'pubDate',
1642 "match": 'match:eq',
1643 "value": '2014-12-05',
1644 "type": 'type:date'
1645 }
1646 );
1647
1648 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
1649 expect(vc.root().key()).toEqual('pubDate');
1650 expect(vc.root().value()).toEqual('2014-12-05');
1651
1652 // Wrap on root
1653 _orOn(vc.root());
Nils Diewaldd5070b02015-01-11 01:44:47 +00001654 expect(vc.root().ldType()).toEqual('docGroup');
1655 expect(vc.root().operation()).toEqual('or');
1656 });
Nils Diewald6ac292b2015-01-15 21:33:21 +00001657
1658 it ('should support multiple sub groups per group', function () {
1659 var vc = KorAP.VirtualCollection.render(
1660 {
1661 "@type": 'korap:docGroup',
1662 'operation' : 'operation:or',
1663 'operands' : [
1664 {
1665 "@type": 'korap:docGroup',
1666 'operation' : 'operation:and',
1667 'operands' : [
1668 {
1669 "@type": 'korap:doc',
1670 "key": 'title',
1671 "value": 't1',
1672 },
1673 {
1674 "@type" : 'korap:doc',
1675 'key' : 'title',
1676 'value' : 't2'
1677 }
1678 ]
1679 },
1680 {
1681 "@type": 'korap:docGroup',
1682 'operation' : 'operation:and',
1683 'operands' : [
1684 {
1685 "@type": 'korap:doc',
1686 "key": 'title',
1687 "value": 't3',
1688 },
1689 {
1690 "@type" : 'korap:doc',
1691 'key' : 'title',
1692 'value' : 't4'
1693 }
1694 ]
1695 }
1696 ]
1697 }
1698 );
1699
1700 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
1701 expect(vc.root().key()).toEqual('pubDate');
1702 expect(vc.root().value()).toEqual('2014-12-05');
1703
1704 // Wrap on root
1705 _orOn(vc.root());
1706 expect(vc.root().ldType()).toEqual('docGroup');
1707 expect(vc.root().operation()).toEqual('or');
1708 });
Nils Diewalde15b7a22015-01-09 21:50:21 +00001709});