blob: 3dd0749fe0a5704a13274a4ac03c6089b86904dc [file] [log] [blame]
Nils Diewald6ac292b2015-01-15 21:33:21 +00001/*
Nils Diewald7c8ced22015-04-15 19:21:00 +00002 * Todo: In demoSpec: Create "and" on the last element of the top "or"-Group
3 */
4define(['vc'], function () {
Nils Diewald6ac292b2015-01-15 21:33:21 +00005
Nils Diewald7c8ced22015-04-15 19:21:00 +00006 var vcClass = require('vc');
7 var docClass = require('vc/doc');
8 var docGroupClass = require('vc/docgroup');
9 var unspecifiedClass = require('vc/unspecified');
10 var operatorsClass = require('vc/operators');
11 var rewriteClass = require('vc/rewrite');
Nils Diewald6ac292b2015-01-15 21:33:21 +000012
Nils Diewald7c8ced22015-04-15 19:21:00 +000013 // Helper method for building factories
14 buildFactory = function (objClass, defaults) {
15 return {
16 create : function (overwrites) {
17 var newObj = {};
18 for (var prop in defaults) {
19 newObj[prop] = defaults[prop];
20 };
21 for (var prop in overwrites) {
22 newObj[prop] = overwrites[prop];
23 };
24 if (objClass === vcClass)
25 return objClass.render(newObj);
26 else
27 return objClass.create().fromJson(newObj);
28 }
Nils Diewald0b6c0412014-12-19 03:55:57 +000029 }
Nils Diewald7c8ced22015-04-15 19:21:00 +000030 };
Nils Diewald0b6c0412014-12-19 03:55:57 +000031
Nils Diewald7c8ced22015-04-15 19:21:00 +000032 function _andOn (obj) {
33 KorAP._and.bind(obj.element().lastChild.firstChild).apply();
34 };
Nils Diewald52f7eb12015-01-12 17:30:04 +000035
Nils Diewald7c8ced22015-04-15 19:21:00 +000036 function _orOn (obj) {
37 KorAP._or.bind(obj.element().lastChild.firstChild).apply();
38 };
Nils Diewald52f7eb12015-01-12 17:30:04 +000039
Nils Diewald7c8ced22015-04-15 19:21:00 +000040 function _delOn (obj) {
41 KorAP._delete.bind(obj.element().lastChild.firstChild).apply();
42 };
Nils Diewald52f7eb12015-01-12 17:30:04 +000043
Nils Diewald7c8ced22015-04-15 19:21:00 +000044 var demoFactory = buildFactory(vcClass, {
45 "@type":"koral:docGroup",
46 "operation":"operation:or",
47 "operands":[
Nils Diewaldf219eb82015-01-07 20:15:42 +000048 {
Nils Diewald7c8ced22015-04-15 19:21:00 +000049 "@type":"koral:docGroup",
50 "operation":"operation:and",
51 "operands":[
52 {
53 "@type":"koral:doc",
54 "key":"Titel",
55 "value":"Baum",
56 "match":"match:eq"
57 },
58 {
59 "@type":"koral:doc",
60 "key":"Veröffentlichungsort",
61 "value":"hihi",
62 "match":"match:eq"
63 },
64 {
65 "@type":"koral:docGroup",
66 "operation":"operation:or",
67 "operands":[
68 {
69 "@type":"koral:doc",
70 "key":"Titel",
71 "value":"Baum",
72 "match":"match:eq"
73 },
74 {
75 "@type":"koral:doc",
76 "key":"Veröffentlichungsort",
77 "value":"hihi",
78 "match":"match:eq"
79 }
80 ]
81 }
82 ]
Nils Diewaldf219eb82015-01-07 20:15:42 +000083 },
84 {
Nils Diewald7c8ced22015-04-15 19:21:00 +000085 "@type":"koral:doc",
86 "key":"Untertitel",
87 "value":"huhu",
88 "match":"match:eq"
Nils Diewaldf219eb82015-01-07 20:15:42 +000089 }
90 ]
91 });
92
Nils Diewald7c8ced22015-04-15 19:21:00 +000093 describe('KorAP.Doc', function () {
94 // Create example factories
95 var stringFactory = buildFactory(docClass, {
96 "key" : "author",
97 "value" : "Max Birkendale",
98 "@type" : "koral:doc"
99 });
100
101 // Create example factories
102 var dateFactory = buildFactory(docClass, {
103 "key" : "pubDate",
104 "type" : "type:date",
105 "match" : "match:eq",
106 "value" : "2014-11-05",
107 "@type" : "koral:doc"
108 });
109
110 // Create example factories
111 var regexFactory = buildFactory(docClass, {
112 "key" : "title",
113 "type" : "type:regex",
114 "value" : "[^b]ee.+?",
115 "@type" : "koral:doc"
116 });
117
118 it('should be initializable', function () {
119 var doc = docClass.create();
120 expect(doc.matchop()).toEqual('eq');
121 expect(doc.key()).toBeUndefined();
122 expect(doc.value()).toBeUndefined();
123 expect(doc.type()).toEqual("string");
124 });
125
126 it('should be definable', function () {
127
128 // Empty doc
129 var doc = docClass.create();
130
131 // Set values
132 doc.key("title");
133 doc.value("Der alte Mann");
134 expect(doc.matchop()).toEqual('eq');
135 expect(doc.key()).toEqual("title");
136 expect(doc.type()).toEqual("string");
137 expect(doc.value()).toEqual("Der alte Mann");
138 });
139
140
141 it('should deserialize JSON-LD string', function () {
142 var doc;
143
144 // String default
145 doc = stringFactory.create();
146 expect(doc.matchop()).toEqual('eq');
147 expect(doc.key()).toEqual("author");
148 expect(doc.type()).toEqual("string");
149 expect(doc.value()).toEqual("Max Birkendale");
150
151 // No valid string
152 doc = stringFactory.create({
153 value : undefined
154 });
155 expect(doc).toBeUndefined();
156
157 // No valid string
158 doc = stringFactory.create({
159 value : { "foo" : "bar" }
160 });
161 expect(doc).toBeUndefined();
162
163 // Change match type
164 doc = stringFactory.create({
165 "match" : "match:ne"
166 });
167
168 expect(doc.matchop()).toEqual('ne');
169 expect(doc.key()).toEqual("author");
170 expect(doc.type()).toEqual("string");
171 expect(doc.value()).toEqual("Max Birkendale");
172
173 // Invalid match type
174 doc = stringFactory.create({
175 "match" : { "foo" : "bar" }
176 });
177 expect(doc).toBeUndefined();
178 });
179
180 it('should deserialize JSON-LD regex', function () {
181 var doc = regexFactory.create();
182 expect(doc.key()).toEqual("title");
183 expect(doc.type()).toEqual("regex");
184 expect(doc.value()).toEqual("[^b]ee.+?");
185 expect(doc.matchop()).toEqual('eq');
186
187 // change matcher
188 doc = regexFactory.create({
189 match : "match:ne"
190 });
191 expect(doc.matchop()).toEqual('ne');
192
193 // Invalid matcher
194 doc = regexFactory.create({
195 match : "match:chook"
196 });
197 expect(doc).toBeUndefined();
198
199 // Invalid regex
200 doc = regexFactory.create({
201 value : "[^b"
202 });
203 expect(doc).toBeUndefined();
204 });
205
206 it('should deserialize JSON-LD date', function () {
207
208 // Normal date
209 doc = dateFactory.create({});
210
211 expect(doc.matchop()).toEqual('eq');
212 expect(doc.key()).toEqual("pubDate");
213 expect(doc.type()).toEqual("date");
214 expect(doc.value()).toEqual("2014-11-05");
215
216 // Short date 1
217 doc = dateFactory.create({
218 "value" : "2014-11"
219 });
220
221 expect(doc.matchop()).toEqual('eq');
222 expect(doc.key()).toEqual("pubDate");
223 expect(doc.type()).toEqual("date");
224 expect(doc.value()).toEqual("2014-11");
225
226 // Short date 2
227 doc = dateFactory.create({
228 "value" : "2014"
229 });
230
231 expect(doc.matchop()).toEqual('eq');
232 expect(doc.key()).toEqual("pubDate");
233 expect(doc.type()).toEqual("date");
234 expect(doc.value()).toEqual("2014");
235
236 // Invalid date!
237 doc = dateFactory.create({
238 "value" : "2014-11-050"
239 });
240 expect(doc).toBeUndefined();
241
242 // Invalid matcher!
243 doc = dateFactory.create({
244 "match" : "match:ne",
245 });
246 expect(doc).toBeUndefined();
247 });
248
249 it('should be serializale to JSON', function () {
250
251 // Empty doc
252 var doc = docClass.create();
253 expect(doc.toJson()).toEqual(jasmine.any(Object));
254
255 // Serialize string
256 doc = stringFactory.create();
257 expect(doc.toJson()).toEqual(jasmine.objectContaining({
258 "@type" : "koral:doc",
259 "type" : "type:string",
260 "key" : "author",
261 "value" : "Max Birkendale",
262 "match" : "match:eq"
263 }));
264
265 // Serialize regex
266 doc = regexFactory.create();
267 expect(doc.toJson()).toEqual(jasmine.objectContaining({
268 "@type" : "koral:doc",
269 "type" : "type:regex",
270 "value" : "[^b]ee.+?",
271 "match" : "match:eq",
272 "key" : 'title'
273 }));
274
275 doc = regexFactory.create({
276 match: "match:ne"
277 });
278 expect(doc.toJson()).toEqual(jasmine.objectContaining({
279 "@type" : "koral:doc",
280 "type" : "type:regex",
281 "value" : "[^b]ee.+?",
282 "match" : "match:ne",
283 "key" : 'title'
284 }));
285
286 doc = dateFactory.create();
287 expect(doc.toJson()).toEqual(jasmine.objectContaining({
288 "@type" : "koral:doc",
289 "type" : "type:date",
290 "value" : "2014-11-05",
291 "match" : "match:eq",
292 "key" : 'pubDate'
293 }));
294
295 doc = dateFactory.create({
296 value : "2014"
297 });
298 expect(doc.toJson()).toEqual(jasmine.objectContaining({
299 "@type" : "koral:doc",
300 "type" : "type:date",
301 "value" : "2014",
302 "match" : "match:eq",
303 "key" : 'pubDate'
304 }));
305 });
306
307
308 it('should be serializale to String', function () {
309 // Empty doc
310 var doc = docClass.create();
311 expect(doc.toQuery()).toEqual("");
312
313 // Serialize string
314 doc = stringFactory.create();
315 expect(doc.toQuery()).toEqual('author = "Max Birkendale"');
316
317 // Serialize string with quotes
318 doc = stringFactory.create({ "value" : 'Max "Der Coole" Birkendate'});
319 expect(doc.toQuery()).toEqual('author = "Max \\"Der Coole\\" Birkendate"');
320
321 // Serialize regex
322 doc = regexFactory.create();
323 expect(doc.toQuery()).toEqual('title = /[^b]ee.+?/');
324
325 doc = regexFactory.create({
326 match: "match:ne"
327 });
328 expect(doc.toQuery()).toEqual('title != /[^b]ee.+?/');
329
330 doc = dateFactory.create();
331 expect(doc.toQuery()).toEqual('pubDate in 2014-11-05');
332
333 doc = dateFactory.create({
334 value : "2014"
335 });
336 expect(doc.toQuery()).toEqual('pubDate in 2014');
337 });
338 });
339
340
341 describe('KorAP.DocGroup', function () {
342 // Create example factories
343 var docFactory = buildFactory(
344 docClass,
Nils Diewaldf219eb82015-01-07 20:15:42 +0000345 {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000346 "@type" : "koral:doc",
347 "match":"match:eq",
348 "key" : "author",
349 "value" : "Max Birkendale"
350 }
351 );
352
353 var docGroupFactory = buildFactory(
354 docGroupClass, {
355 "@type" : "koral:docGroup",
356 "operation" : "operation:and",
357 "operands" : [
358 docFactory.create().toJson(),
359 docFactory.create({
360 "key" : "pubDate",
361 "type" : "type:date",
362 "value" : "2014-12-05"
363 }).toJson()
364 ]
365 });
366
367 it('should be initializable', function () {
368 // Create empty group
369 var docGroup = docGroupClass.create();
370 expect(docGroup.operation()).toEqual('and');
371
372 // Create empty group
373 docGroup = docGroupClass.create();
374 docGroup.operation('or');
375 expect(docGroup.operation()).toEqual('or');
376 });
377
378 it('should be definable', function () {
379
380 // Empty group
381 var docGroup = docGroupClass.create();
382 expect(docGroup.operation()).toEqual('and');
383
384 // Set values
385 docGroup.operation("or");
386 expect(docGroup.operation()).toEqual('or');
387
388 // Set invalid values
389 docGroup.operation("hui");
390 expect(docGroup.operation()).toEqual('or');
391 });
392
393 it('should be deserializable', function () {
394 var docGroup = docGroupFactory.create();
395 expect(docGroup.operation()).toEqual("and");
396 expect(docGroup.operands().length).toEqual(2);
397
398 var op1 = docGroup.getOperand(0);
399 expect(op1.type()).toEqual("string");
400 expect(op1.key()).toEqual("author");
401 expect(op1.value()).toEqual("Max Birkendale");
402 expect(op1.matchop()).toEqual("eq");
403
404 var op2 = docGroup.getOperand(1);
405 expect(op2.type()).toEqual("date");
406 expect(op2.key()).toEqual("pubDate");
407 expect(op2.value()).toEqual("2014-12-05");
408 expect(op2.matchop()).toEqual("eq");
409
410 // Append empty group
411 var newGroup = docGroup.append(docGroupClass.create());
412 newGroup.operation('or');
413 newGroup.append(docFactory.create());
414 newGroup.append(docFactory.create({
415 "type" : "type:regex",
416 "key" : "title",
417 "value" : "^e.+?$",
418 "match" : "match:ne"
419 }));
420
421 expect(docGroup.operation()).toEqual("and");
422 expect(docGroup.operands().length).toEqual(3);
423
424 var op1 = docGroup.getOperand(0);
425 expect(op1.ldType()).toEqual("doc");
426 expect(op1.type()).toEqual("string");
427 expect(op1.key()).toEqual("author");
428 expect(op1.value()).toEqual("Max Birkendale");
429 expect(op1.matchop()).toEqual("eq");
430
431 var op2 = docGroup.getOperand(1);
432 expect(op2.ldType()).toEqual("doc");
433 expect(op2.type()).toEqual("date");
434 expect(op2.key()).toEqual("pubDate");
435 expect(op2.value()).toEqual("2014-12-05");
436 expect(op2.matchop()).toEqual("eq");
437
438 var op3 = docGroup.getOperand(2);
439 expect(op3.ldType()).toEqual("docGroup");
440 expect(op3.operation()).toEqual("or");
441
442 var op4 = op3.getOperand(0);
443 expect(op4.ldType()).toEqual("doc");
444 expect(op4.type()).toEqual("string");
445 expect(op4.key()).toEqual("author");
446 expect(op4.value()).toEqual("Max Birkendale");
447 expect(op4.matchop()).toEqual("eq");
448
449 var op5 = op3.getOperand(1);
450 expect(op5.ldType()).toEqual("doc");
451 expect(op5.type()).toEqual("regex");
452 expect(op5.key()).toEqual("title");
453 expect(op5.value()).toEqual("^e.+?$");
454 expect(op5.matchop()).toEqual("ne");
455 });
456
457 it('should be serializable to JSON', function () {
458 var docGroup = docGroupFactory.create();
459
460 expect(docGroup.toJson()).toEqual(jasmine.objectContaining({
Nils Diewald2fe12e12015-03-06 16:47:06 +0000461 "@type" : "koral:docGroup",
Nils Diewaldf219eb82015-01-07 20:15:42 +0000462 "operation" : "operation:and",
463 "operands" : [
464 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000465 "@type": 'koral:doc',
Nils Diewald7c8ced22015-04-15 19:21:00 +0000466 "key" : 'author',
467 "match": 'match:eq',
468 "value": 'Max Birkendale',
469 "type": 'type:string'
Nils Diewaldf219eb82015-01-07 20:15:42 +0000470 },
471 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000472 "@type": 'koral:doc',
Nils Diewaldf219eb82015-01-07 20:15:42 +0000473 "key": 'pubDate',
Nils Diewalde15b7a22015-01-09 21:50:21 +0000474 "match": 'match:eq',
475 "value": '2014-12-05',
476 "type": 'type:date'
Nils Diewalde15b7a22015-01-09 21:50:21 +0000477 }
478 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000479 }));
480 });
Nils Diewalde15b7a22015-01-09 21:50:21 +0000481
Nils Diewald7c8ced22015-04-15 19:21:00 +0000482 it('should be serializable to String', function () {
483 var docGroup = docGroupFactory.create();
484 expect(docGroup.toQuery()).toEqual(
485 'author = "Max Birkendale" & pubDate in 2014-12-05'
486 );
Nils Diewald52f7eb12015-01-12 17:30:04 +0000487
Nils Diewald7c8ced22015-04-15 19:21:00 +0000488 docGroup = docGroupFactory.create({
489 "@type" : "koral:docGroup",
490 "operation" : "operation:or",
491 "operands" : [
Nils Diewalde15b7a22015-01-09 21:50:21 +0000492 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000493 "@type": 'koral:doc',
Nils Diewald7c8ced22015-04-15 19:21:00 +0000494 "key" : 'author',
Nils Diewalde15b7a22015-01-09 21:50:21 +0000495 "match": 'match:eq',
Nils Diewald7c8ced22015-04-15 19:21:00 +0000496 "value": 'Max Birkendale',
497 "type": 'type:string'
Nils Diewalde15b7a22015-01-09 21:50:21 +0000498 },
499 {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000500 "@type" : "koral:docGroup",
501 "operation" : "operation:and",
502 "operands" : [
Nils Diewalde15b7a22015-01-09 21:50:21 +0000503 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000504 "@type": 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +0000505 "key": 'pubDate',
Nils Diewald7c8ced22015-04-15 19:21:00 +0000506 "match": 'match:geq',
507 "value": '2014-05-12',
508 "type": 'type:date'
509 },
510 {
511 "@type": 'koral:doc',
512 "key": 'pubDate',
513 "match": 'match:leq',
Nils Diewalde15b7a22015-01-09 21:50:21 +0000514 "value": '2014-12-05',
515 "type": 'type:date'
516 },
517 {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000518 "@type": 'koral:doc',
519 "key": 'foo',
520 "match": 'match:ne',
521 "value": '[a]?bar',
522 "type": 'type:regex'
523 }
524 ]
525 }
526 ]
527 });
528 expect(docGroup.toQuery()).toEqual(
529 'author = "Max Birkendale" | ' +
530 '(pubDate since 2014-05-12 & ' +
531 'pubDate until 2014-12-05 & foo != /[a]?bar/)'
532 );
533 });
534 });
535
536 describe('KorAP.UnspecifiedDoc', function () {
537 it('should be initializable', function () {
538 var doc = unspecifiedClass.create();
539 var docElement = doc.element();
540 expect(docElement.getAttribute('class')).toEqual('doc unspecified');
541 expect(docElement.firstChild.firstChild.data).toEqual('⋯');
542 expect(docElement.lastChild.lastChild.data).toEqual('⋯');
543 expect(doc.toQuery()).toEqual('');
544
545 // Only removable
546 expect(docElement.lastChild.children.length).toEqual(0);
547 });
548
549 it('should be removable, when no root', function () {
550 var docGroup = docGroupClass.create();
551 docGroup.operation('or');
552 expect(docGroup.operation()).toEqual('or');
553
554 docGroup.append({
555 "@type": 'koral:doc',
556 "key": 'pubDate',
557 "match": 'match:eq',
558 "value": '2014-12-05',
559 "type": 'type:date'
560 });
561
562 // Add unspecified object
563 docGroup.append();
564
565 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
566 expect(docGroup.element().children[0].getAttribute('class')).toEqual('doc');
567
568 var unspec = docGroup.element().children[1];
569 expect(unspec.getAttribute('class')).toEqual('doc unspecified');
570
571 // Removable
572 expect(unspec.lastChild.children.length).toEqual(1);
573 expect(unspec.lastChild.children[0].getAttribute('class')).toEqual('delete');
574 });
575
576 it('should be replaceable by a doc', function () {
577 var doc = unspecifiedClass.create();
578 expect(doc.ldType()).toEqual("non");
579 // No parent, therefor not updateable
580 expect(doc.key("baum")).toBeNull();
581
582 var docGroup = docGroupClass.create();
583 docGroup.operation('or');
584 expect(docGroup.operation()).toEqual('or');
585
586 docGroup.append({
587 "@type": 'koral:doc',
588 "key": 'pubDate',
589 "match": 'match:eq',
590 "value": '2014-12-05',
591 "type": 'type:date'
592 });
593
594 expect(docGroup.toQuery()).toEqual("pubDate in 2014-12-05");
595 docGroup.append();
596
597 expect(docGroup.getOperand(0).ldType()).toEqual("doc");
598 expect(docGroup.getOperand(1).ldType()).toEqual("non");
599
600 var op = docGroup.getOperand(1).element().lastChild;
601 expect(op.getAttribute('class')).toEqual('operators');
602 expect(op.children[0].getAttribute('class')).toEqual('delete');
603 expect(op.children.length).toEqual(1);
604
605 // Replace unspecified doc
606 expect(docGroup.getOperand(1).key("name")).not.toBeNull();
607 expect(docGroup.getOperand(1).ldType()).toEqual("doc");
608 expect(docGroup.getOperand(1).key()).toEqual("name");
609 expect(docGroup.getOperand(1).value()).toEqual("");
610
611 op = docGroup.getOperand(1).element().lastChild;
612 expect(op.getAttribute('class')).toEqual('operators');
613 expect(op.children[0].getAttribute('class')).toEqual('and');
614 expect(op.children[1].getAttribute('class')).toEqual('or');
615 expect(op.children[2].getAttribute('class')).toEqual('delete');
616 expect(op.children.length).toEqual(3);
617
618 docGroup.getOperand(1).value("Pachelbel");
619 expect(docGroup.getOperand(1).value()).toEqual("Pachelbel");
620 expect(docGroup.getOperand(1).type()).toEqual("string");
621 expect(docGroup.getOperand(1).matchop()).toEqual("eq");
622
623 // Specified!
624 expect(docGroup.toQuery()).toEqual('pubDate in 2014-12-05 | name = "Pachelbel"');
625 });
626
627 it('should be replaceable on root', function () {
628 var vc = vcClass.render();
629 expect(vc.toQuery()).toEqual("");
630
631 expect(vc.root().ldType()).toEqual("non");
632
633 // No operators on root
634 op = vc.root().element().lastChild;
635 expect(op.lastChild.textContent).toEqual('⋯');
636
637 // Replace
638 expect(vc.root().key("baum")).not.toBeNull();
639 expect(vc.root().ldType()).toEqual("doc");
640
641 op = vc.root().element().lastChild;
642 expect(op.getAttribute('class')).toEqual('operators');
643 expect(op.children[0].getAttribute('class')).toEqual('and');
644 expect(op.children[1].getAttribute('class')).toEqual('or');
645 expect(op.children[2].getAttribute('class')).toEqual('delete');
646 expect(op.children.length).toEqual(3);
647 });
648 });
649
650 describe('KorAP.Doc element', function () {
651 it('should be initializable', function () {
652 var docElement = docClass.create(undefined, {
653 "@type" : "koral:doc",
654 "key":"Titel",
655 "value":"Baum",
656 "match":"match:eq"
657 });
658 expect(docElement.key()).toEqual('Titel');
659 expect(docElement.matchop()).toEqual('eq');
660 expect(docElement.value()).toEqual('Baum');
661
662 var docE = docElement.element();
663 expect(docE.children[0].firstChild.data).toEqual('Titel');
664 expect(docE.children[1].firstChild.data).toEqual('eq');
665 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
666 expect(docE.children[2].firstChild.data).toEqual('Baum');
667 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
668
669 expect(docElement.toJson()).toEqual(jasmine.objectContaining({
670 "@type" : "koral:doc",
671 "key":"Titel",
672 "value":"Baum",
673 "match":"match:eq"
674 }));
675 });
676 });
677
678 describe('KorAP.DocGroup element', function () {
679 it('should be initializable', function () {
680
681 var docGroup = docGroupClass.create(undefined, {
682 "@type" : "koral:docGroup",
683 "operation" : "operation:and",
684 "operands" : [
685 {
686 "@type": 'koral:doc',
687 "key" : 'author',
688 "match": 'match:eq',
689 "value": 'Max Birkendale',
690 "type": 'type:string'
691 },
692 {
693 "@type": 'koral:doc',
694 "key": 'pubDate',
695 "match": 'match:eq',
696 "value": '2014-12-05',
697 "type": 'type:date'
698 }
699 ]
700 });
701
702 expect(docGroup.operation()).toEqual('and');
703 var e = docGroup.element();
704 expect(e.getAttribute('class')).toEqual('docGroup');
705 expect(e.getAttribute('data-operation')).toEqual('and');
706
707 var first = e.children[0];
708 expect(first.getAttribute('class')).toEqual('doc');
709 expect(first.children[0].getAttribute('class')).toEqual('key');
710 expect(first.children[1].getAttribute('class')).toEqual('match');
711 expect(first.children[2].getAttribute('class')).toEqual('value');
712 expect(first.children[2].getAttribute('data-type')).toEqual('string');
713 expect(first.children[0].firstChild.data).toEqual('author');
714 expect(first.children[1].firstChild.data).toEqual('eq');
715 expect(first.children[2].firstChild.data).toEqual('Max Birkendale');
716
717 var second = e.children[1];
718 expect(second.getAttribute('class')).toEqual('doc');
719 expect(second.children[0].getAttribute('class')).toEqual('key');
720 expect(second.children[1].getAttribute('class')).toEqual('match');
721 expect(second.children[2].getAttribute('class')).toEqual('value');
722 expect(second.children[2].getAttribute('data-type')).toEqual('date');
723 expect(second.children[0].firstChild.data).toEqual('pubDate');
724 expect(second.children[1].firstChild.data).toEqual('eq');
725 expect(second.children[2].firstChild.data).toEqual('2014-12-05');
726 });
727
728 it('should be deserializable with nested groups', function () {
729 var docGroup = docGroupClass.create(undefined, {
730 "@type" : "koral:docGroup",
731 "operation" : "operation:or",
732 "operands" : [
733 {
734 "@type": 'koral:doc',
735 "key" : 'author',
736 "match": 'match:eq',
737 "value": 'Max Birkendale',
738 "type": 'type:string'
739 },
740 {
741 "@type" : "koral:docGroup",
742 "operation" : "operation:and",
743 "operands" : [
744 {
745 "@type": 'koral:doc',
746 "key": 'pubDate',
747 "match": 'match:geq',
748 "value": '2014-05-12',
749 "type": 'type:date'
750 },
751 {
752 "@type": 'koral:doc',
753 "key": 'pubDate',
754 "match": 'match:leq',
755 "value": '2014-12-05',
756 "type": 'type:date'
757 }
758 ]
759 }
760 ]
761 });
762
763 expect(docGroup.operation()).toEqual('or');
764 var e = docGroup.element();
765 expect(e.getAttribute('class')).toEqual('docGroup');
766 expect(e.getAttribute('data-operation')).toEqual('or');
767
768 expect(e.children[0].getAttribute('class')).toEqual('doc');
769 var docop = e.children[0].lastChild;
770 expect(docop.getAttribute('class')).toEqual('operators');
771 expect(docop.children[0].getAttribute('class')).toEqual('and');
772 expect(docop.children[1].getAttribute('class')).toEqual('or');
773 expect(docop.children[2].getAttribute('class')).toEqual('delete');
774
775 expect(e.children[1].getAttribute('class')).toEqual('docGroup');
776 expect(e.children[1].getAttribute('data-operation')).toEqual('and');
777
778 // This and-operation can be "or"ed or "delete"d
779 var secop = e.children[1].children[2];
780 expect(secop.getAttribute('class')).toEqual('operators');
781 expect(secop.children[0].getAttribute('class')).toEqual('or');
782 expect(secop.children[1].getAttribute('class')).toEqual('delete');
783
784 // This or-operation can be "and"ed or "delete"d
785 expect(e.children[2].getAttribute('class')).toEqual('operators');
786 expect(e.lastChild.getAttribute('class')).toEqual('operators');
787 expect(e.lastChild.children[0].getAttribute('class')).toEqual('and');
788 expect(e.lastChild.children[1].getAttribute('class')).toEqual('delete');
789 });
790 });
791
792 describe('KorAP.VirtualCollection', function () {
793 var simpleGroupFactory = buildFactory(docGroupClass, {
794 "@type" : "koral:docGroup",
795 "operation" : "operation:and",
796 "operands" : [
797 {
798 "@type": 'koral:doc',
799 "key" : 'author',
800 "match": 'match:eq',
801 "value": 'Max Birkendale',
802 "type": 'type:string'
803 },
804 {
805 "@type": 'koral:doc',
806 "key": 'pubDate',
807 "match": 'match:eq',
808 "value": '2014-12-05',
809 "type": 'type:date'
810 }
811 ]
812 });
813
814 var nestedGroupFactory = buildFactory(vcClass, {
815 "@type" : "koral:docGroup",
816 "operation" : "operation:or",
817 "operands" : [
818 {
819 "@type": 'koral:doc',
820 "key" : 'author',
821 "match": 'match:eq',
822 "value": 'Max Birkendale',
823 "type": 'type:string'
824 },
825 {
826 "@type" : "koral:docGroup",
827 "operation" : "operation:and",
828 "operands" : [
829 {
830 "@type": 'koral:doc',
831 "key": 'pubDate',
832 "match": 'match:geq',
833 "value": '2014-05-12',
834 "type": 'type:date'
835 },
836 {
837 "@type": 'koral:doc',
838 "key": 'pubDate',
839 "match": 'match:leq',
840 "value": '2014-12-05',
841 "type": 'type:date'
842 }
843 ]
844 }
845 ]
846 });
847
848 var flatGroupFactory = buildFactory(vcClass, {
849 "@type" : "koral:docGroup",
850 "operation" : "operation:and",
851 "operands" : [
852 {
853 "@type": 'koral:doc',
854 "key": 'pubDate',
855 "match": 'match:geq',
856 "value": '2014-05-12',
857 "type": 'type:date'
858 },
859 {
860 "@type": 'koral:doc',
861 "key": 'pubDate',
862 "match": 'match:leq',
863 "value": '2014-12-05',
864 "type": 'type:date'
865 },
866 {
867 "@type": 'koral:doc',
868 "key": 'foo',
869 "match": 'match:eq',
870 "value": 'bar',
871 "type": 'type:string'
872 }
873 ]
874 });
875
876 it('should be initializable', function () {
877 var vc = vcClass.render();
878 expect(vc.element().getAttribute('class')).toEqual('vc');
879 expect(vc.root().element().getAttribute('class')).toEqual('doc unspecified');
880
881 // Not removable
882 expect(vc.root().element().lastChild.children.length).toEqual(0);
883 });
884
885 it('should be based on a doc', function () {
886 var vc = vcClass.render({
887 "@type" : "koral:doc",
888 "key":"Titel",
889 "value":"Baum",
890 "match":"match:eq"
891 });
892
893 expect(vc.element().getAttribute('class')).toEqual('vc');
894 expect(vc.root().element().getAttribute('class')).toEqual('doc');
895 expect(vc.root().key()).toEqual('Titel');
896 expect(vc.root().value()).toEqual('Baum');
897 expect(vc.root().matchop()).toEqual('eq');
898
899 var docE = vc.root().element();
900 expect(docE.children[0].firstChild.data).toEqual('Titel');
901 expect(docE.children[1].firstChild.data).toEqual('eq');
902 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
903 expect(docE.children[2].firstChild.data).toEqual('Baum');
904 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
905 });
906
907 it('should be based on a docGroup', function () {
908 var vc = vcClass.render(simpleGroupFactory.create().toJson());
909
910 expect(vc.element().getAttribute('class')).toEqual('vc');
911 expect(vc.root().element().getAttribute('class')).toEqual('docGroup');
912 expect(vc.root().operation()).toEqual('and');
913
914 var docGroup = vc.root();
915
916 var first = docGroup.getOperand(0);
917 expect(first.key()).toEqual('author');
918 expect(first.value()).toEqual('Max Birkendale');
919 expect(first.matchop()).toEqual('eq');
920
921 var second = docGroup.getOperand(1);
922 expect(second.key()).toEqual('pubDate');
923 expect(second.value()).toEqual('2014-12-05');
924 expect(second.matchop()).toEqual('eq');
925 });
926
927
928 it('should be based on a nested docGroup', function () {
929 var vc = nestedGroupFactory.create();
930
931 expect(vc.element().getAttribute('class')).toEqual('vc');
932 expect(vc.element().firstChild.getAttribute('class')).toEqual('docGroup');
933 expect(vc.element().firstChild.children[0].getAttribute('class')).toEqual('doc');
934 var dg = vc.element().firstChild.children[1];
935 expect(dg.getAttribute('class')).toEqual('docGroup');
936 expect(dg.children[0].getAttribute('class')).toEqual('doc');
937 expect(dg.children[1].getAttribute('class')).toEqual('doc');
938 expect(dg.children[2].getAttribute('class')).toEqual('operators');
939 expect(vc.element().firstChild.children[2].getAttribute('class')).toEqual('operators');
940 });
941
942 it('should be modifiable by deletion in flat docGroups', function () {
943 var vc = flatGroupFactory.create();
944 var docGroup = vc.root();
945
946 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
947
948 var doc = docGroup.getOperand(1);
949 expect(doc.key()).toEqual("pubDate");
950 expect(doc.value()).toEqual("2014-12-05");
951
952 // Remove operand 1
953 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
954 expect(doc._element).toEqual(undefined);
955
956 doc = docGroup.getOperand(1);
957 expect(doc.key()).toEqual("foo");
958
959 // Remove operand 1
960 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
961 expect(doc._element).toEqual(undefined);
962
963 // Only one operand left ...
964 expect(docGroup.getOperand(1)).toBeUndefined();
965 // ... but there shouldn't be a group anymore at all!
966 expect(docGroup.getOperand(0)).toBeUndefined();
967
968 var obj = vc.root();
969 expect(obj.ldType()).toEqual("doc");
970 expect(obj.key()).toEqual("pubDate");
971 expect(obj.value()).toEqual("2014-05-12");
972
973 expect(obj.element().getAttribute('class')).toEqual('doc');
974 });
975
976
977 it('should be modifiable by deletion in nested docGroups (root case)', function () {
978 var vc = nestedGroupFactory.create();
979
980 expect(vc.toQuery()).toEqual(
981 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05)'
982 );
983
984 var docGroup = vc.root();
985 expect(docGroup.ldType()).toEqual("docGroup");
986 expect(docGroup.operation()).toEqual("or");
987
988 var doc = docGroup.getOperand(0);
989 expect(doc.key()).toEqual("author");
990 expect(doc.value()).toEqual("Max Birkendale");
991
992 docGroup = docGroup.getOperand(1);
993 expect(docGroup.operation()).toEqual("and");
994
995 doc = docGroup.getOperand(0);
996 expect(doc.key()).toEqual("pubDate");
997 expect(doc.matchop()).toEqual("geq");
998 expect(doc.value()).toEqual("2014-05-12");
999 expect(doc.type()).toEqual("date");
1000
1001 doc = docGroup.getOperand(1);
1002 expect(doc.key()).toEqual("pubDate");
1003 expect(doc.matchop()).toEqual("leq");
1004 expect(doc.value()).toEqual("2014-12-05");
1005 expect(doc.type()).toEqual("date");
1006
1007 // Remove first operand so everything becomes root
1008 expect(
1009 vc.root().delOperand(
1010 vc.root().getOperand(0)
1011 ).update().ldType()
1012 ).toEqual("docGroup");
1013
1014 expect(vc.root().ldType()).toEqual("docGroup");
1015 expect(vc.root().operation()).toEqual("and");
1016 expect(vc.root().getOperand(0).ldType()).toEqual("doc");
1017
1018 expect(vc.toQuery()).toEqual(
1019 'pubDate since 2014-05-12 & pubDate until 2014-12-05'
1020 );
1021 });
1022
1023 it('should be modifiable by deletion in nested docGroups (resolve group case)', function () {
1024 var vc = nestedGroupFactory.create();
1025
1026 // Get nested group
1027 var firstGroup = vc.root().getOperand(1);
1028 firstGroup.append(simpleGroupFactory.create({ "operation" : "operation:or" }));
1029
1030 // Structur is now:
1031 // or(doc, and(doc, doc, or(doc, doc)))
1032
1033 // Get nested or in and
1034 var orGroup = vc.root().getOperand(1).getOperand(2);
1035 expect(orGroup.ldType()).toEqual("docGroup");
1036 expect(orGroup.operation()).toEqual("or");
1037
1038 // Remove
1039 // Structur is now:
1040 // or(doc, and(doc, doc, doc)))
1041 expect(orGroup.delOperand(orGroup.getOperand(0)).update().operation()).toEqual("and");
1042 expect(vc.root().getOperand(1).operands().length).toEqual(3);
1043 });
1044
1045 it('should be modifiable by deletion in nested docGroups (identical group case)', function () {
1046 var vc = nestedGroupFactory.create();
1047
1048 // Get nested group
1049 var firstGroup = vc.root().getOperand(1);
1050 firstGroup.append(simpleGroupFactory.create({
1051 "operation" : "operation:or"
1052 }));
1053 var oldAuthor = firstGroup.getOperand(2).getOperand(0);
1054 oldAuthor.key("title");
1055 oldAuthor.value("Der Birnbaum");
1056
1057 // Structur is now:
1058 // or(doc, and(doc, doc, or(doc, doc)))
1059 expect(vc.toQuery()).toEqual(
1060 'author = "Max Birkendale" | ' +
1061 '(pubDate since 2014-05-12 & ' +
1062 'pubDate until 2014-12-05 & ' +
1063 '(title = "Der Birnbaum" | ' +
1064 'pubDate in 2014-12-05))'
1065 );
1066
1067 var andGroup = vc.root().getOperand(1);
1068
1069 // Get leading docs in and
1070 var doc1 = andGroup.getOperand(0);
1071 expect(doc1.ldType()).toEqual("doc");
1072 expect(doc1.value()).toEqual("2014-05-12");
1073 var doc2 = andGroup.getOperand(1);
1074 expect(doc2.ldType()).toEqual("doc");
1075 expect(doc2.value()).toEqual("2014-12-05");
1076
1077 // Remove 2
1078 expect(
1079 andGroup.delOperand(doc2).update().operation()
1080 ).toEqual("and");
1081 // Structur is now:
1082 // or(doc, and(doc, or(doc, doc)))
1083
1084 expect(vc.toQuery()).toEqual(
1085 'author = "Max Birkendale"' +
1086 ' | (pubDate since 2014-05-12 & ' +
1087 '(title = "Der Birnbaum" | pubDate in 2014-12-05))'
1088 );
1089
1090
1091 // Remove 1
1092 expect(andGroup.delOperand(doc1).update().operation()).toEqual("or");
1093 // Structur is now:
1094 // or(doc, doc, doc)
1095
1096 expect(vc.toQuery()).toEqual(
1097 'author = "Max Birkendale" | title = "Der Birnbaum" | pubDate in 2014-12-05'
1098 );
1099 });
1100
1101 it('should be reducible to unspecification', function () {
1102 var vc = demoFactory.create();
1103
1104 expect(vc.toQuery()).toEqual(vc.root().toQuery());
1105 expect(vc.toQuery()).toEqual(
1106 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & ' +
1107 '(Titel = "Baum" | Veröffentlichungsort = "hihi")) ' +
1108 '| Untertitel = "huhu"');
1109 expect(vc.root().element().lastChild.children[0].firstChild.nodeValue).toEqual('and');
1110 expect(vc.root().element().lastChild.children[1].firstChild.nodeValue).toEqual('×');
1111 expect(vc.root().delOperand(vc.root().getOperand(0)).update()).not.toBeUndefined();
1112 expect(vc.toQuery()).toEqual('Untertitel = "huhu"');
1113
1114 var lc = vc.root().element().lastChild;
1115 expect(lc.children[0].firstChild.nodeValue).toEqual('and');
1116 expect(lc.children[1].firstChild.nodeValue).toEqual('or');
1117 expect(lc.children[2].firstChild.nodeValue).toEqual('×');
1118
1119 // Clean everything
1120 vc.clean();
1121 expect(vc.toQuery()).toEqual('');
1122 });
1123
1124 it('should flatten on import', function () {
1125 var vc = vcClass.create().render({
1126 "@type":"koral:docGroup",
1127 "operation":"operation:or",
1128 "operands":[
1129 {
1130 "@type":"koral:docGroup",
1131 "operation":"operation:or",
1132 "operands":[
1133 {
1134 "@type":"koral:doc",
1135 "key":"Titel",
1136 "value":"Baum",
1137 "match":"match:eq"
1138 },
1139 {
1140 "@type":"koral:doc",
1141 "key":"Veröffentlichungsort",
1142 "value":"hihi",
1143 "match":"match:eq"
1144 },
1145 {
1146 "@type":"koral:docGroup",
1147 "operation":"operation:or",
1148 "operands":[
Nils Diewalde15b7a22015-01-09 21:50:21 +00001149 {
Nils Diewald7c8ced22015-04-15 19:21:00 +00001150 "@type":"koral:doc",
1151 "key":"Titel",
1152 "value":"Baum",
1153 "match":"match:eq"
Nils Diewalde15b7a22015-01-09 21:50:21 +00001154 },
1155 {
Nils Diewald7c8ced22015-04-15 19:21:00 +00001156 "@type":"koral:doc",
1157 "key":"Veröffentlichungsort",
1158 "value":"hihi",
1159 "match":"match:eq"
Nils Diewalde15b7a22015-01-09 21:50:21 +00001160 }
1161 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +00001162 }
Nils Diewald6ac292b2015-01-15 21:33:21 +00001163 ]
1164 },
1165 {
Nils Diewald7c8ced22015-04-15 19:21:00 +00001166 "@type":"koral:doc",
1167 "key":"Untertitel",
1168 "value":"huhu",
1169 "match":"match:eq"
Nils Diewald6ac292b2015-01-15 21:33:21 +00001170 }
1171 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +00001172 });
Nils Diewaldfda29d92015-01-22 17:28:01 +00001173
Nils Diewald7c8ced22015-04-15 19:21:00 +00001174 expect(vc.toQuery()).toEqual(
1175 'Titel = "Baum" | Veröffentlichungsort = "hihi" | Untertitel = "huhu"'
1176 );
Nils Diewald86dad5b2015-01-28 15:09:07 +00001177 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00001178 });
1179
Nils Diewald7c8ced22015-04-15 19:21:00 +00001180 describe('KorAP.Operators', function () {
1181 it('should be initializable', function () {
1182 var op = operatorsClass.create(true, false, false);
1183 expect(op.and()).toBeTruthy();
1184 expect(op.or()).not.toBeTruthy();
1185 expect(op.del()).not.toBeTruthy();
1186
1187 op.and(false);
1188 expect(op.and()).not.toBeTruthy();
1189 expect(op.or()).not.toBeTruthy();
1190 expect(op.del()).not.toBeTruthy();
1191
1192 op.or(true);
1193 op.del(true);
1194 expect(op.and()).not.toBeTruthy();
1195 expect(op.or()).toBeTruthy();
1196 expect(op.del()).toBeTruthy();
1197
1198 var e = op.element();
1199 expect(e.getAttribute('class')).toEqual('operators');
1200 expect(e.children[0].getAttribute('class')).toEqual('or');
1201 expect(e.children[0].firstChild.data).toEqual('or');
1202 expect(e.children[1].getAttribute('class')).toEqual('delete');
1203 expect(e.children[1].firstChild.data).toEqual('×');
1204
1205 op.and(true);
1206 op.del(false);
1207 op.update();
1208
1209 e = op.element();
1210 expect(e.getAttribute('class')).toEqual('operators');
1211 expect(e.children[0].getAttribute('class')).toEqual('and');
1212 expect(e.children[0].firstChild.data).toEqual('and');
1213 expect(e.children[1].getAttribute('class')).toEqual('or');
1214 expect(e.children[1].firstChild.data).toEqual('or');
1215 });
1216 });
1217
1218 describe('KorAP._delete (event)', function () {
1219 var complexVCFactory = buildFactory(vcClass,{
1220 "@type": 'koral:docGroup',
1221 'operation' : 'operation:and',
1222 'operands' : [
1223 {
1224 "@type": 'koral:doc',
1225 "key": 'pubDate',
1226 "match": 'match:eq',
1227 "value": '2014-12-05',
1228 "type": 'type:date'
1229 },
1230 {
1231 "@type" : 'koral:docGroup',
1232 'operation' : 'operation:or',
1233 'operands' : [
1234 {
1235 '@type' : 'koral:doc',
1236 'key' : 'title',
1237 'value' : 'Hello World!'
1238 },
1239 {
1240 '@type' : 'koral:doc',
1241 'key' : 'foo',
1242 'value' : 'bar'
1243 }
1244 ]
1245 }
1246 ]
1247 });
1248
1249 it('should clean on root docs', function () {
1250 var vc = vcClass.render({
1251 "@type": 'koral:doc',
1252 "key": 'pubDate',
1253 "match": 'match:eq',
1254 "value": '2014-12-05',
1255 "type": 'type:date'
Nils Diewald86dad5b2015-01-28 15:09:07 +00001256 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00001257 expect(vc.root().toQuery()).toEqual('pubDate in 2014-12-05');
1258 expect(vc.root().element().lastChild.getAttribute('class')).toEqual('operators');
Nils Diewald86dad5b2015-01-28 15:09:07 +00001259
Nils Diewald7c8ced22015-04-15 19:21:00 +00001260 // Clean with delete from root
1261 expect(vc.root().element().lastChild.lastChild.getAttribute('class')).toEqual('delete');
1262 _delOn(vc.root());
1263 expect(vc.root().toQuery()).toEqual('');
1264 expect(vc.root().element().lastChild.lastChild.data).toEqual('⋯');
1265 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00001266
Nils Diewald7c8ced22015-04-15 19:21:00 +00001267 it('should remove on nested docs', function () {
1268 var vc = vcClass.render(
1269 {
1270 "@type": 'koral:docGroup',
1271 'operation' : 'operation:and',
1272 'operands' : [
1273 {
1274 "@type": 'koral:doc',
1275 "key": 'pubDate',
1276 "match": 'match:eq',
1277 "value": '2014-12-05',
1278 "type": 'type:date'
1279 },
1280 {
1281 "@type" : 'koral:doc',
1282 'key' : 'foo',
1283 'value' : 'bar'
1284 }
1285 ]
1286 }
1287 );
1288
1289 // Delete with direct element access
1290 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1291 _delOn(vc.root().getOperand(0));
1292
1293 expect(vc.toQuery()).toEqual('foo = "bar"');
1294 expect(vc.root().ldType()).toEqual('doc');
1295 });
1296
1297 it('should clean on doc groups', function () {
1298 var vc = vcClass.render(
1299 {
1300 "@type": 'koral:docGroup',
1301 'operation' : 'operation:and',
1302 'operands' : [
1303 {
1304 "@type": 'koral:doc',
1305 "key": 'pubDate',
1306 "match": 'match:eq',
1307 "value": '2014-12-05',
1308 "type": 'type:date'
1309 },
1310 {
1311 "@type" : 'koral:doc',
1312 'key' : 'foo',
1313 'value' : 'bar'
1314 }
1315 ]
1316 }
1317 );
1318
1319 // Cleanwith direct element access
1320 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1321 _delOn(vc.root());
1322 expect(vc.toQuery()).toEqual('');
1323 expect(vc.root().ldType()).toEqual('non');
1324 });
1325
1326 it('should remove on nested doc groups (case of ungrouping 1)', function () {
1327 var vc = complexVCFactory.create();
1328
1329 // Delete with direct element access
1330 expect(vc.toQuery()).toEqual(
1331 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1332 );
1333
1334 // Remove hello world:
1335 _delOn(vc.root().getOperand(1).getOperand(0));
1336 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1337 expect(vc.root().ldType()).toEqual('docGroup');
1338 });
1339
1340 it('should remove on nested doc groups (case of ungrouping 2)', function () {
1341 var vc = complexVCFactory.create();
1342
1343 // Delete with direct element access
1344 expect(vc.toQuery()).toEqual(
1345 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1346 );
1347
1348 // Remove bar
1349 _delOn(vc.root().getOperand(1).getOperand(1));
1350 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & title = "Hello World!"');
1351 expect(vc.root().ldType()).toEqual('docGroup');
1352 expect(vc.root().operation()).toEqual('and');
1353 });
1354
1355 it('should remove on nested doc groups (case of root changing)', function () {
1356 var vc = complexVCFactory.create();
1357
1358 // Delete with direct element access
1359 expect(vc.toQuery()).toEqual(
1360 'pubDate in 2014-12-05 & ' +
1361 '(title = "Hello World!" | foo = "bar")'
1362 );
1363
1364 // Remove bar
1365 _delOn(vc.root().getOperand(0));
1366 expect(vc.toQuery()).toEqual('title = "Hello World!" | foo = "bar"');
1367 expect(vc.root().ldType()).toEqual('docGroup');
1368 expect(vc.root().operation()).toEqual('or');
1369 });
1370
1371 it('should remove on nested doc groups (list flattening)', function () {
1372 var vc = vcClass.render(
1373 {
1374 "@type": 'koral:docGroup',
1375 'operation' : 'operation:or',
1376 'operands' : [
1377 {
1378 "@type": 'koral:doc',
1379 "key": 'pubDate',
1380 "match": 'match:eq',
1381 "value": '2014-12-05',
1382 "type": 'type:date'
1383 },
1384 {
1385 "@type" : 'koral:doc',
1386 'key' : 'foo',
1387 'value' : 'bar'
1388 },
1389 {
1390 "@type": 'koral:docGroup',
1391 'operation' : 'operation:and',
1392 'operands' : [
1393 {
1394 "@type": 'koral:doc',
1395 "key": 'pubDate',
1396 "match": 'match:eq',
1397 "value": '2014-12-05',
1398 "type": 'type:date'
1399 },
1400 {
1401 "@type" : 'koral:docGroup',
1402 'operation' : 'operation:or',
1403 'operands' : [
1404 {
1405 '@type' : 'koral:doc',
1406 'key' : 'title',
1407 'value' : 'Hello World!'
1408 },
1409 {
1410 '@type' : 'koral:doc',
1411 'key' : 'yeah',
1412 'value' : 'juhu'
1413 }
1414 ]
1415 }
1416 ]
1417 }
1418 ]
1419 }
1420 );
1421
1422 // Delete with direct element access
1423 expect(vc.toQuery()).toEqual(
1424 'pubDate in 2014-12-05 | foo = "bar" | ' +
1425 '(pubDate in 2014-12-05 & ' +
1426 '(title = "Hello World!" | yeah = "juhu"))'
1427 );
1428
1429 expect(vc.root().ldType()).toEqual('docGroup');
1430 expect(vc.root().operation()).toEqual('or');
1431
1432 // Operands and operators
1433 expect(vc.element().firstChild.children.length).toEqual(4);
1434 expect(vc.element().firstChild.lastChild.getAttribute('class')).toEqual('operators');
1435
1436 // Remove inner group and flatten
1437 _delOn(vc.root().getOperand(2).getOperand(0));
1438
1439 expect(vc.toQuery()).toEqual(
1440 'pubDate in 2014-12-05 | foo = "bar" | title = "Hello World!" | yeah = "juhu"'
1441 );
1442 expect(vc.root().ldType()).toEqual('docGroup');
1443 expect(vc.root().operation()).toEqual('or');
1444
1445 // Operands and operators
1446 expect(vc.element().firstChild.children.length).toEqual(5);
1447 expect(vc.element().firstChild.lastChild.getAttribute('class')).toEqual('operators');
1448 });
1449 });
1450
1451 describe('KorAP._add (event)', function () {
1452 var complexVCFactory = buildFactory(vcClass,{
1453 "@type": 'koral:docGroup',
1454 'operation' : 'operation:and',
1455 'operands' : [
1456 {
1457 "@type": 'koral:doc',
1458 "key": 'pubDate',
1459 "match": 'match:eq',
1460 "value": '2014-12-05',
1461 "type": 'type:date'
1462 },
1463 {
1464 "@type" : 'koral:docGroup',
1465 'operation' : 'operation:or',
1466 'operands' : [
1467 {
1468 '@type' : 'koral:doc',
1469 'key' : 'title',
1470 'value' : 'Hello World!'
1471 },
1472 {
1473 '@type' : 'koral:doc',
1474 'key' : 'foo',
1475 'value' : 'bar'
1476 }
1477 ]
1478 }
1479 ]
1480 });
1481
1482 it('should add new unspecified doc with "and"', function () {
1483 var vc = vcClass.render(
1484 {
1485 "@type": 'koral:docGroup',
1486 'operation' : 'operation:and',
1487 'operands' : [
1488 {
1489 "@type": 'koral:doc',
1490 "key": 'pubDate',
1491 "match": 'match:eq',
1492 "value": '2014-12-05',
1493 "type": 'type:date'
1494 },
1495 {
1496 "@type" : 'koral:doc',
1497 'key' : 'foo',
1498 'value' : 'bar'
1499 }
1500 ]
1501 }
1502 );
1503
1504 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1505
1506 var fc = vc.element().firstChild;
1507 expect(fc.getAttribute('data-operation')).toEqual('and');
1508 expect(fc.children.length).toEqual(3);
1509 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1510 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1511 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1512
1513 // add with 'and' in the middle
1514 _andOn(vc.root().getOperand(0));
1515 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1516
1517 fc = vc.element().firstChild;
1518 expect(fc.getAttribute('data-operation')).toEqual('and');
1519 expect(fc.children.length).toEqual(4);
1520 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1521
1522 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1523 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
1524 expect(fc.children[2].getAttribute('class')).toEqual('doc');
1525 });
1526
1527 it('should add new unspecified doc with "or"', function () {
1528 var vc = vcClass.render(
1529 {
1530 "@type": 'koral:docGroup',
1531 'operation' : 'operation:and',
1532 'operands' : [
1533 {
1534 "@type": 'koral:doc',
1535 "key": 'pubDate',
1536 "match": 'match:eq',
1537 "value": '2014-12-05',
1538 "type": 'type:date'
1539 },
1540 {
1541 "@type" : 'koral:doc',
1542 'key' : 'foo',
1543 'value' : 'bar'
1544 }
1545 ]
1546 }
1547 );
1548
1549 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1550
1551 var fc = vc.element().firstChild;
1552 expect(fc.children.length).toEqual(3);
1553 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1554 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1555 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1556
1557 // add with 'or' in the middle
1558 _orOn(vc.root().getOperand(0));
1559 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1560 fc = vc.element().firstChild;
1561
1562 expect(fc.getAttribute('data-operation')).toEqual('and');
1563 expect(fc.children.length).toEqual(3);
1564 expect(fc.children[0].getAttribute('class')).toEqual('docGroup');
1565 expect(fc.children[0].getAttribute('data-operation')).toEqual('or');
1566 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1567 expect(fc.children[2].getAttribute('class')).toEqual('operators');
1568 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1569
1570 fc = vc.element().firstChild.firstChild;
1571 expect(fc.children.length).toEqual(3);
1572 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1573 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
1574 expect(fc.children[2].getAttribute('class')).toEqual('operators');
1575 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1576 });
1577
1578 it('should add new unspecified doc with "and" before group', function () {
1579 var vc = demoFactory.create();
1580
1581 // Wrap with direct element access
1582 expect(vc.toQuery()).toEqual(
1583 '(Titel = "Baum" & ' +
1584 'Veröffentlichungsort = "hihi" & ' +
1585 '(Titel = "Baum" | ' +
1586 'Veröffentlichungsort = "hihi")) | ' +
1587 'Untertitel = "huhu"'
1588 );
1589
1590 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
1591 expect(vc.root().getOperand(0).operation()).toEqual('and');
1592 expect(vc.root().getOperand(0).operands().length).toEqual(3);
1593
1594 // Add unspecified on the second doc
1595 var secDoc = vc.root().getOperand(0).getOperand(1);
1596 expect(secDoc.value()).toEqual('hihi');
1597
1598 // Add
1599 _andOn(secDoc);
1600
1601 var fo = vc.root().getOperand(0);
1602
1603 expect(fo.ldType()).toEqual('docGroup');
1604 expect(fo.operation()).toEqual('and');
1605 expect(fo.operands().length).toEqual(4);
1606
1607 expect(fo.getOperand(0).ldType()).toEqual('doc');
1608 expect(fo.getOperand(1).ldType()).toEqual('doc');
1609 expect(fo.getOperand(2).ldType()).toEqual('non');
1610 expect(fo.getOperand(3).ldType()).toEqual('docGroup');
1611 });
1612
1613
1614 it('should remove a doc with an unspecified doc in a nested group', function () {
1615 var vc = demoFactory.create();
1616
1617 // Wrap with direct element access
1618 expect(vc.toQuery()).toEqual(
1619 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & (Titel = "Baum" | Veröffentlichungsort = "hihi")) | Untertitel = "huhu"'
1620 );
1621
1622 var fo = vc.root().getOperand(0).getOperand(0);
1623 expect(fo.key()).toEqual('Titel');
1624 expect(fo.value()).toEqual('Baum');
1625
1626 // Add unspecified on the root group
1627 _orOn(fo);
1628
1629 fo = vc.root().getOperand(0).getOperand(0);
1630
1631 expect(fo.operation()).toEqual('or');
1632 expect(fo.getOperand(0).ldType()).toEqual('doc');
1633 expect(fo.getOperand(1).ldType()).toEqual('non');
1634
1635 // Delete document
1636 _delOn(fo.getOperand(0));
1637
1638 // The operand is now non
1639 expect(vc.root().getOperand(0).getOperand(0).ldType()).toEqual('non');
1640 expect(vc.root().getOperand(0).getOperand(1).ldType()).toEqual('doc');
1641 expect(vc.root().getOperand(0).getOperand(2).ldType()).toEqual('docGroup');
1642 });
1643
1644
1645 it('should remove an unspecified doc with an doc in a nested group', function () {
1646 var vc = demoFactory.create();
1647
1648 // Wrap with direct element access
1649 expect(vc.toQuery()).toEqual(
1650 '(Titel = "Baum" & ' +
1651 'Veröffentlichungsort = "hihi" & ' +
1652 '(Titel = "Baum" ' +
1653 '| Veröffentlichungsort = "hihi")) | ' +
1654 'Untertitel = "huhu"'
1655 );
1656
1657 var fo = vc.root().getOperand(0).getOperand(0);
1658 expect(fo.key()).toEqual('Titel');
1659 expect(fo.value()).toEqual('Baum');
1660
1661 // Add unspecified on the root group
1662 _orOn(fo);
1663
1664 fo = vc.root().getOperand(0).getOperand(0);
1665
1666 expect(fo.operation()).toEqual('or');
1667 expect(fo.getOperand(0).ldType()).toEqual('doc');
1668 expect(fo.getOperand(1).ldType()).toEqual('non');
1669
1670 // Delete unspecified doc
1671 _delOn(fo.getOperand(1));
1672
1673 // The operand is now non
1674 fo = vc.root().getOperand(0);
1675 expect(fo.getOperand(0).ldType()).toEqual('doc');
1676 expect(fo.getOperand(0).key()).toEqual('Titel');
1677 expect(fo.getOperand(0).value()).toEqual('Baum');
1678 expect(fo.getOperand(1).ldType()).toEqual('doc');
1679 expect(fo.getOperand(2).ldType()).toEqual('docGroup');
1680 });
1681
1682
1683 it('should add on parent group (case "and")', function () {
1684 var vc = complexVCFactory.create();
1685
1686 // Wrap with direct element access
1687 expect(vc.toQuery()).toEqual(
1688 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1689 );
1690
1691 expect(vc.root().operands().length).toEqual(2);
1692
1693 // Add unspecified on the root group
1694 _andOn(vc.root().getOperand(1));
1695 expect(vc.toQuery()).toEqual(
1696 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1697 );
1698
1699 expect(vc.root().ldType()).toEqual('docGroup');
1700 expect(vc.root().operands().length).toEqual(3);
1701 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
1702 expect(vc.root().getOperand(1).ldType()).toEqual('docGroup');
1703 expect(vc.root().getOperand(1).operation()).toEqual('or');
1704 expect(vc.root().getOperand(2).ldType()).toEqual('non');
1705
1706 // Add another unspecified on the root group
1707 _andOn(vc.root().getOperand(1));
1708
1709 expect(vc.root().operands().length).toEqual(4);
1710 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
1711 expect(vc.root().getOperand(1).ldType()).toEqual('docGroup');
1712 expect(vc.root().getOperand(2).ldType()).toEqual('non');
1713 expect(vc.root().getOperand(3).ldType()).toEqual('non');
1714
1715 // Add another unspecified after the first doc
1716 _andOn(vc.root().getOperand(0));
1717
1718 expect(vc.root().operands().length).toEqual(5);
1719 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
1720 expect(vc.root().getOperand(1).ldType()).toEqual('non');
1721 expect(vc.root().getOperand(2).ldType()).toEqual('docGroup');
1722 expect(vc.root().getOperand(3).ldType()).toEqual('non');
1723 expect(vc.root().getOperand(4).ldType()).toEqual('non');
1724 });
1725
1726 it('should wrap on root', function () {
1727 var vc = vcClass.render(
1728 {
1729 "@type": 'koral:docGroup',
1730 'operation' : 'operation:and',
1731 'operands' : [
1732 {
1733 "@type": 'koral:doc',
1734 "key": 'pubDate',
1735 "match": 'match:eq',
1736 "value": '2014-12-05',
1737 "type": 'type:date'
1738 },
1739 {
1740 "@type" : 'koral:doc',
1741 'key' : 'foo',
1742 'value' : 'bar'
1743 }
1744 ]
1745 }
1746 );
1747
1748 // Wrap on root
1749 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1750 expect(vc.root().ldType()).toEqual('docGroup');
1751 expect(vc.root().operation()).toEqual('and');
1752 _orOn(vc.root());
1753 expect(vc.root().ldType()).toEqual('docGroup');
1754 expect(vc.root().operation()).toEqual('or');
1755
1756 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
1757 expect(vc.root().getOperand(0).operation()).toEqual('and');
1758 });
1759
1760 it('should add on root (case "and")', function () {
1761 var vc = vcClass.render(
1762 {
1763 "@type": 'koral:doc',
1764 "key": 'pubDate',
1765 "match": 'match:eq',
1766 "value": '2014-12-05',
1767 "type": 'type:date'
1768 }
1769 );
1770
1771 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
1772 expect(vc.root().ldType()).toEqual('doc');
1773 expect(vc.root().key()).toEqual('pubDate');
1774 expect(vc.root().value()).toEqual('2014-12-05');
1775
1776 // Wrap on root
1777 _andOn(vc.root());
1778 expect(vc.root().ldType()).toEqual('docGroup');
1779 expect(vc.root().operation()).toEqual('and');
1780 });
1781
1782 it('should add on root (case "or")', function () {
1783 var vc = vcClass.render(
1784 {
1785 "@type": 'koral:doc',
1786 "key": 'pubDate',
1787 "match": 'match:eq',
1788 "value": '2014-12-05',
1789 "type": 'type:date'
1790 }
1791 );
1792
1793 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
1794 expect(vc.root().key()).toEqual('pubDate');
1795 expect(vc.root().value()).toEqual('2014-12-05');
1796
1797 // Wrap on root
1798 _orOn(vc.root());
1799 expect(vc.root().ldType()).toEqual('docGroup');
1800 expect(vc.root().operation()).toEqual('or');
1801 });
1802
1803 it('should support multiple sub groups per group', function () {
1804 var vc = vcClass.render(
1805 {
1806 "@type": 'koral:docGroup',
1807 'operation' : 'operation:or',
1808 'operands' : [
1809 {
1810 "@type": 'koral:docGroup',
1811 'operation' : 'operation:and',
1812 'operands' : [
1813 {
1814 "@type": 'koral:doc',
1815 "key": 'title',
1816 "value": 't1',
1817 },
1818 {
1819 "@type" : 'koral:doc',
1820 'key' : 'title',
1821 'value' : 't2'
1822 }
1823 ]
1824 },
1825 {
1826 "@type": 'koral:docGroup',
1827 'operation' : 'operation:and',
1828 'operands' : [
1829 {
1830 "@type": 'koral:doc',
1831 "key": 'title',
1832 "value": 't3',
1833 },
1834 {
1835 "@type" : 'koral:doc',
1836 'key' : 'title',
1837 'value' : 't4'
1838 }
1839 ]
1840 }
1841 ]
1842 }
1843 );
1844 expect(vc.toQuery()).toEqual(
1845 '(title = "t1" & title = "t2") | ' +
1846 '(title = "t3" & title = "t4")'
1847 );
1848 expect(vc.root().operation()).toEqual('or');
1849 expect(vc.root().getOperand(0).toQuery()).toEqual('title = "t1" & title = "t2"');
1850 expect(vc.root().getOperand(1).toQuery()).toEqual('title = "t3" & title = "t4"');
1851
1852 _andOn(vc.root());
1853
1854 expect(vc.root().operation()).toEqual('and');
1855 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
1856 expect(vc.root().getOperand(1).ldType()).toEqual('non');
1857 });
1858 });
1859
1860 describe('KorAP.Rewrite', function () {
1861
1862 it('should be initializable', function () {
1863 var rewrite = rewriteClass.create({
1864 "@type" : "koral:rewrite",
1865 "operation" : "operation:modification",
1866 "src" : "querySerializer",
1867 "scope" : "tree"
Nils Diewald86dad5b2015-01-28 15:09:07 +00001868 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00001869 expect(rewrite.toString()).toEqual('Modification of "tree" by "querySerializer"');
1870 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00001871
Nils Diewald7c8ced22015-04-15 19:21:00 +00001872 it('should be deserialized by docs', function () {
1873 var doc = docClass.create(
1874 undefined,
1875 {
1876 "@type":"koral:doc",
1877 "key":"Titel",
1878 "value":"Baum",
1879 "match":"match:eq"
1880 });
1881
1882 expect(doc.element().classList.contains('doc')).toBeTruthy();
1883 expect(doc.element().classList.contains('rewritten')).toBe(false);
1884
1885 doc = docClass.create(
1886 undefined,
1887 {
1888 "@type":"koral:doc",
1889 "key":"Titel",
1890 "value":"Baum",
1891 "match":"match:eq",
1892 "rewrites" : [
1893 {
1894 "@type" : "koral:rewrite",
1895 "operation" : "operation:modification",
1896 "src" : "querySerializer",
1897 "scope" : "tree"
1898 }
1899 ]
1900 });
1901
1902 expect(doc.element().classList.contains('doc')).toBeTruthy();
1903 expect(doc.element().classList.contains('rewritten')).toBeTruthy();
1904 });
1905 /*
1906 it('should be deserialized by docGroups', function () {
1907 });
1908 */
Nils Diewald86dad5b2015-01-28 15:09:07 +00001909 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00001910 /*
1911 describe('KorAP.DocKey', function () {
1912 it('should be initializable', function () {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001913 var docKey = KorAP.DocKey.create();
1914 expect(docKey.toString()).toEqual('...');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001915 });
1916 });
1917 */
Nils Diewald2fe12e12015-03-06 16:47:06 +00001918});