blob: 24b24a712299df24517bbc469b1b031886d39c96 [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');
Akrone4961b12017-05-10 21:04:46 +02008 var menuClass = require('vc/menu');
9 var prefixClass = require('vc/prefix');
Nils Diewald7c8ced22015-04-15 19:21:00 +000010 var docGroupClass = require('vc/docgroup');
11 var unspecifiedClass = require('vc/unspecified');
12 var operatorsClass = require('vc/operators');
13 var rewriteClass = require('vc/rewrite');
Nils Diewaldf0c4f112015-05-05 12:56:59 +000014 var stringValClass = require('vc/stringval');
Nils Diewald6ac292b2015-01-15 21:33:21 +000015
Nils Diewald7c8ced22015-04-15 19:21:00 +000016 // Helper method for building factories
17 buildFactory = function (objClass, defaults) {
18 return {
19 create : function (overwrites) {
Akrone4961b12017-05-10 21:04:46 +020020 var newObj = {};
21 for (var prop in defaults) {
22 newObj[prop] = defaults[prop];
23 };
24 for (var prop in overwrites) {
25 newObj[prop] = overwrites[prop];
26 };
27 return objClass.create().fromJson(newObj);
Nils Diewald7c8ced22015-04-15 19:21:00 +000028 }
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({
Akrone4961b12017-05-10 21:04:46 +0200153 value : undefined
Nils Diewald7c8ced22015-04-15 19:21:00 +0000154 });
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({
Akron8778f5d2017-06-30 21:25:55 +0200326 match: "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000327 });
328 expect(doc.toQuery()).toEqual('title != /[^b]ee.+?/');
329
Akron8778f5d2017-06-30 21:25:55 +0200330 doc = regexFactory.create({
331 value: "WPD/AAA/00001"
332 });
333 expect(doc.toQuery()).toEqual('title = /WPD\\/AAA\\/00001/');
334
Nils Diewald7c8ced22015-04-15 19:21:00 +0000335 doc = dateFactory.create();
336 expect(doc.toQuery()).toEqual('pubDate in 2014-11-05');
337
338 doc = dateFactory.create({
Akron8778f5d2017-06-30 21:25:55 +0200339 value : "2014"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000340 });
341 expect(doc.toQuery()).toEqual('pubDate in 2014');
342 });
343 });
344
345
346 describe('KorAP.DocGroup', function () {
347 // Create example factories
348 var docFactory = buildFactory(
349 docClass,
Nils Diewaldf219eb82015-01-07 20:15:42 +0000350 {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000351 "@type" : "koral:doc",
352 "match":"match:eq",
353 "key" : "author",
354 "value" : "Max Birkendale"
355 }
356 );
357
358 var docGroupFactory = buildFactory(
359 docGroupClass, {
360 "@type" : "koral:docGroup",
361 "operation" : "operation:and",
362 "operands" : [
363 docFactory.create().toJson(),
364 docFactory.create({
365 "key" : "pubDate",
366 "type" : "type:date",
367 "value" : "2014-12-05"
368 }).toJson()
369 ]
370 });
371
372 it('should be initializable', function () {
373 // Create empty group
374 var docGroup = docGroupClass.create();
375 expect(docGroup.operation()).toEqual('and');
376
377 // Create empty group
378 docGroup = docGroupClass.create();
379 docGroup.operation('or');
380 expect(docGroup.operation()).toEqual('or');
381 });
382
383 it('should be definable', function () {
384
385 // Empty group
386 var docGroup = docGroupClass.create();
387 expect(docGroup.operation()).toEqual('and');
388
389 // Set values
390 docGroup.operation("or");
391 expect(docGroup.operation()).toEqual('or');
392
393 // Set invalid values
394 docGroup.operation("hui");
395 expect(docGroup.operation()).toEqual('or');
396 });
397
398 it('should be deserializable', function () {
399 var docGroup = docGroupFactory.create();
400 expect(docGroup.operation()).toEqual("and");
401 expect(docGroup.operands().length).toEqual(2);
402
403 var op1 = docGroup.getOperand(0);
404 expect(op1.type()).toEqual("string");
405 expect(op1.key()).toEqual("author");
406 expect(op1.value()).toEqual("Max Birkendale");
407 expect(op1.matchop()).toEqual("eq");
408
409 var op2 = docGroup.getOperand(1);
410 expect(op2.type()).toEqual("date");
411 expect(op2.key()).toEqual("pubDate");
412 expect(op2.value()).toEqual("2014-12-05");
413 expect(op2.matchop()).toEqual("eq");
414
415 // Append empty group
416 var newGroup = docGroup.append(docGroupClass.create());
417 newGroup.operation('or');
418 newGroup.append(docFactory.create());
419 newGroup.append(docFactory.create({
420 "type" : "type:regex",
421 "key" : "title",
422 "value" : "^e.+?$",
423 "match" : "match:ne"
424 }));
425
426 expect(docGroup.operation()).toEqual("and");
427 expect(docGroup.operands().length).toEqual(3);
428
429 var op1 = docGroup.getOperand(0);
430 expect(op1.ldType()).toEqual("doc");
431 expect(op1.type()).toEqual("string");
432 expect(op1.key()).toEqual("author");
433 expect(op1.value()).toEqual("Max Birkendale");
434 expect(op1.matchop()).toEqual("eq");
435
436 var op2 = docGroup.getOperand(1);
437 expect(op2.ldType()).toEqual("doc");
438 expect(op2.type()).toEqual("date");
439 expect(op2.key()).toEqual("pubDate");
440 expect(op2.value()).toEqual("2014-12-05");
441 expect(op2.matchop()).toEqual("eq");
442
443 var op3 = docGroup.getOperand(2);
444 expect(op3.ldType()).toEqual("docGroup");
445 expect(op3.operation()).toEqual("or");
446
447 var op4 = op3.getOperand(0);
448 expect(op4.ldType()).toEqual("doc");
449 expect(op4.type()).toEqual("string");
450 expect(op4.key()).toEqual("author");
451 expect(op4.value()).toEqual("Max Birkendale");
452 expect(op4.matchop()).toEqual("eq");
453
454 var op5 = op3.getOperand(1);
455 expect(op5.ldType()).toEqual("doc");
456 expect(op5.type()).toEqual("regex");
457 expect(op5.key()).toEqual("title");
458 expect(op5.value()).toEqual("^e.+?$");
459 expect(op5.matchop()).toEqual("ne");
460 });
461
462 it('should be serializable to JSON', function () {
463 var docGroup = docGroupFactory.create();
464
465 expect(docGroup.toJson()).toEqual(jasmine.objectContaining({
Nils Diewald2fe12e12015-03-06 16:47:06 +0000466 "@type" : "koral:docGroup",
Nils Diewaldf219eb82015-01-07 20:15:42 +0000467 "operation" : "operation:and",
468 "operands" : [
469 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000470 "@type": 'koral:doc',
Nils Diewald7c8ced22015-04-15 19:21:00 +0000471 "key" : 'author',
472 "match": 'match:eq',
473 "value": 'Max Birkendale',
474 "type": 'type:string'
Nils Diewaldf219eb82015-01-07 20:15:42 +0000475 },
476 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000477 "@type": 'koral:doc',
Nils Diewaldf219eb82015-01-07 20:15:42 +0000478 "key": 'pubDate',
Nils Diewalde15b7a22015-01-09 21:50:21 +0000479 "match": 'match:eq',
480 "value": '2014-12-05',
481 "type": 'type:date'
Nils Diewalde15b7a22015-01-09 21:50:21 +0000482 }
483 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000484 }));
485 });
Nils Diewalde15b7a22015-01-09 21:50:21 +0000486
Nils Diewald7c8ced22015-04-15 19:21:00 +0000487 it('should be serializable to String', function () {
488 var docGroup = docGroupFactory.create();
489 expect(docGroup.toQuery()).toEqual(
490 'author = "Max Birkendale" & pubDate in 2014-12-05'
491 );
Nils Diewald52f7eb12015-01-12 17:30:04 +0000492
Nils Diewald7c8ced22015-04-15 19:21:00 +0000493 docGroup = docGroupFactory.create({
494 "@type" : "koral:docGroup",
495 "operation" : "operation:or",
496 "operands" : [
Nils Diewalde15b7a22015-01-09 21:50:21 +0000497 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000498 "@type": 'koral:doc',
Nils Diewald7c8ced22015-04-15 19:21:00 +0000499 "key" : 'author',
Nils Diewalde15b7a22015-01-09 21:50:21 +0000500 "match": 'match:eq',
Nils Diewald7c8ced22015-04-15 19:21:00 +0000501 "value": 'Max Birkendale',
502 "type": 'type:string'
Nils Diewalde15b7a22015-01-09 21:50:21 +0000503 },
504 {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000505 "@type" : "koral:docGroup",
506 "operation" : "operation:and",
507 "operands" : [
Nils Diewalde15b7a22015-01-09 21:50:21 +0000508 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000509 "@type": 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +0000510 "key": 'pubDate',
Nils Diewald7c8ced22015-04-15 19:21:00 +0000511 "match": 'match:geq',
512 "value": '2014-05-12',
513 "type": 'type:date'
514 },
515 {
516 "@type": 'koral:doc',
517 "key": 'pubDate',
518 "match": 'match:leq',
Nils Diewalde15b7a22015-01-09 21:50:21 +0000519 "value": '2014-12-05',
520 "type": 'type:date'
521 },
522 {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000523 "@type": 'koral:doc',
524 "key": 'foo',
525 "match": 'match:ne',
526 "value": '[a]?bar',
527 "type": 'type:regex'
528 }
529 ]
530 }
531 ]
532 });
533 expect(docGroup.toQuery()).toEqual(
534 'author = "Max Birkendale" | ' +
535 '(pubDate since 2014-05-12 & ' +
536 'pubDate until 2014-12-05 & foo != /[a]?bar/)'
537 );
538 });
539 });
540
541 describe('KorAP.UnspecifiedDoc', function () {
542 it('should be initializable', function () {
543 var doc = unspecifiedClass.create();
544 var docElement = doc.element();
545 expect(docElement.getAttribute('class')).toEqual('doc unspecified');
546 expect(docElement.firstChild.firstChild.data).toEqual('⋯');
547 expect(docElement.lastChild.lastChild.data).toEqual('⋯');
548 expect(doc.toQuery()).toEqual('');
549
550 // Only removable
551 expect(docElement.lastChild.children.length).toEqual(0);
552 });
553
554 it('should be removable, when no root', function () {
555 var docGroup = docGroupClass.create();
556 docGroup.operation('or');
557 expect(docGroup.operation()).toEqual('or');
558
559 docGroup.append({
560 "@type": 'koral:doc',
561 "key": 'pubDate',
562 "match": 'match:eq',
563 "value": '2014-12-05',
564 "type": 'type:date'
565 });
566
567 // Add unspecified object
568 docGroup.append();
569
570 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
571 expect(docGroup.element().children[0].getAttribute('class')).toEqual('doc');
572
573 var unspec = docGroup.element().children[1];
574 expect(unspec.getAttribute('class')).toEqual('doc unspecified');
575
576 // Removable
577 expect(unspec.lastChild.children.length).toEqual(1);
578 expect(unspec.lastChild.children[0].getAttribute('class')).toEqual('delete');
579 });
580
581 it('should be replaceable by a doc', function () {
582 var doc = unspecifiedClass.create();
583 expect(doc.ldType()).toEqual("non");
584 // No parent, therefor not updateable
585 expect(doc.key("baum")).toBeNull();
586
587 var docGroup = docGroupClass.create();
588 docGroup.operation('or');
589 expect(docGroup.operation()).toEqual('or');
590
591 docGroup.append({
592 "@type": 'koral:doc',
593 "key": 'pubDate',
594 "match": 'match:eq',
595 "value": '2014-12-05',
596 "type": 'type:date'
597 });
598
599 expect(docGroup.toQuery()).toEqual("pubDate in 2014-12-05");
600 docGroup.append();
601
602 expect(docGroup.getOperand(0).ldType()).toEqual("doc");
603 expect(docGroup.getOperand(1).ldType()).toEqual("non");
604
605 var op = docGroup.getOperand(1).element().lastChild;
606 expect(op.getAttribute('class')).toEqual('operators');
607 expect(op.children[0].getAttribute('class')).toEqual('delete');
608 expect(op.children.length).toEqual(1);
609
610 // Replace unspecified doc
611 expect(docGroup.getOperand(1).key("name")).not.toBeNull();
612 expect(docGroup.getOperand(1).ldType()).toEqual("doc");
613 expect(docGroup.getOperand(1).key()).toEqual("name");
614 expect(docGroup.getOperand(1).value()).toEqual("");
615
616 op = docGroup.getOperand(1).element().lastChild;
617 expect(op.getAttribute('class')).toEqual('operators');
618 expect(op.children[0].getAttribute('class')).toEqual('and');
619 expect(op.children[1].getAttribute('class')).toEqual('or');
620 expect(op.children[2].getAttribute('class')).toEqual('delete');
621 expect(op.children.length).toEqual(3);
622
623 docGroup.getOperand(1).value("Pachelbel");
624 expect(docGroup.getOperand(1).value()).toEqual("Pachelbel");
625 expect(docGroup.getOperand(1).type()).toEqual("string");
626 expect(docGroup.getOperand(1).matchop()).toEqual("eq");
627
628 // Specified!
629 expect(docGroup.toQuery()).toEqual('pubDate in 2014-12-05 | name = "Pachelbel"');
630 });
631
632 it('should be replaceable on root', function () {
Nils Diewald6283d692015-04-23 20:32:53 +0000633 var vc = vcClass.create();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000634 expect(vc.toQuery()).toEqual("");
635
636 expect(vc.root().ldType()).toEqual("non");
637
638 // No operators on root
639 op = vc.root().element().lastChild;
640 expect(op.lastChild.textContent).toEqual('⋯');
641
642 // Replace
643 expect(vc.root().key("baum")).not.toBeNull();
644 expect(vc.root().ldType()).toEqual("doc");
645
646 op = vc.root().element().lastChild;
647 expect(op.getAttribute('class')).toEqual('operators');
648 expect(op.children[0].getAttribute('class')).toEqual('and');
649 expect(op.children[1].getAttribute('class')).toEqual('or');
650 expect(op.children[2].getAttribute('class')).toEqual('delete');
651 expect(op.children.length).toEqual(3);
652 });
653 });
654
655 describe('KorAP.Doc element', function () {
656 it('should be initializable', function () {
657 var docElement = docClass.create(undefined, {
658 "@type" : "koral:doc",
659 "key":"Titel",
660 "value":"Baum",
661 "match":"match:eq"
662 });
663 expect(docElement.key()).toEqual('Titel');
664 expect(docElement.matchop()).toEqual('eq');
665 expect(docElement.value()).toEqual('Baum');
666
667 var docE = docElement.element();
668 expect(docE.children[0].firstChild.data).toEqual('Titel');
669 expect(docE.children[1].firstChild.data).toEqual('eq');
670 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
671 expect(docE.children[2].firstChild.data).toEqual('Baum');
672 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
673
674 expect(docElement.toJson()).toEqual(jasmine.objectContaining({
675 "@type" : "koral:doc",
676 "key":"Titel",
677 "value":"Baum",
678 "match":"match:eq"
679 }));
680 });
681 });
682
683 describe('KorAP.DocGroup element', function () {
684 it('should be initializable', function () {
685
686 var docGroup = docGroupClass.create(undefined, {
687 "@type" : "koral:docGroup",
688 "operation" : "operation:and",
689 "operands" : [
690 {
691 "@type": 'koral:doc',
692 "key" : 'author',
693 "match": 'match:eq',
694 "value": 'Max Birkendale',
695 "type": 'type:string'
696 },
697 {
698 "@type": 'koral:doc',
699 "key": 'pubDate',
700 "match": 'match:eq',
701 "value": '2014-12-05',
702 "type": 'type:date'
703 }
704 ]
705 });
706
707 expect(docGroup.operation()).toEqual('and');
708 var e = docGroup.element();
709 expect(e.getAttribute('class')).toEqual('docGroup');
710 expect(e.getAttribute('data-operation')).toEqual('and');
711
712 var first = e.children[0];
713 expect(first.getAttribute('class')).toEqual('doc');
714 expect(first.children[0].getAttribute('class')).toEqual('key');
715 expect(first.children[1].getAttribute('class')).toEqual('match');
716 expect(first.children[2].getAttribute('class')).toEqual('value');
717 expect(first.children[2].getAttribute('data-type')).toEqual('string');
718 expect(first.children[0].firstChild.data).toEqual('author');
719 expect(first.children[1].firstChild.data).toEqual('eq');
720 expect(first.children[2].firstChild.data).toEqual('Max Birkendale');
721
722 var second = e.children[1];
723 expect(second.getAttribute('class')).toEqual('doc');
724 expect(second.children[0].getAttribute('class')).toEqual('key');
725 expect(second.children[1].getAttribute('class')).toEqual('match');
726 expect(second.children[2].getAttribute('class')).toEqual('value');
727 expect(second.children[2].getAttribute('data-type')).toEqual('date');
728 expect(second.children[0].firstChild.data).toEqual('pubDate');
729 expect(second.children[1].firstChild.data).toEqual('eq');
730 expect(second.children[2].firstChild.data).toEqual('2014-12-05');
731 });
732
733 it('should be deserializable with nested groups', function () {
734 var docGroup = docGroupClass.create(undefined, {
735 "@type" : "koral:docGroup",
736 "operation" : "operation:or",
737 "operands" : [
738 {
739 "@type": 'koral:doc',
740 "key" : 'author',
741 "match": 'match:eq',
742 "value": 'Max Birkendale',
743 "type": 'type:string'
744 },
745 {
746 "@type" : "koral:docGroup",
747 "operation" : "operation:and",
748 "operands" : [
749 {
750 "@type": 'koral:doc',
751 "key": 'pubDate',
752 "match": 'match:geq',
753 "value": '2014-05-12',
754 "type": 'type:date'
755 },
756 {
757 "@type": 'koral:doc',
758 "key": 'pubDate',
759 "match": 'match:leq',
760 "value": '2014-12-05',
761 "type": 'type:date'
762 }
763 ]
764 }
765 ]
766 });
767
768 expect(docGroup.operation()).toEqual('or');
769 var e = docGroup.element();
770 expect(e.getAttribute('class')).toEqual('docGroup');
771 expect(e.getAttribute('data-operation')).toEqual('or');
772
773 expect(e.children[0].getAttribute('class')).toEqual('doc');
774 var docop = e.children[0].lastChild;
775 expect(docop.getAttribute('class')).toEqual('operators');
776 expect(docop.children[0].getAttribute('class')).toEqual('and');
777 expect(docop.children[1].getAttribute('class')).toEqual('or');
778 expect(docop.children[2].getAttribute('class')).toEqual('delete');
779
780 expect(e.children[1].getAttribute('class')).toEqual('docGroup');
781 expect(e.children[1].getAttribute('data-operation')).toEqual('and');
782
783 // This and-operation can be "or"ed or "delete"d
784 var secop = e.children[1].children[2];
785 expect(secop.getAttribute('class')).toEqual('operators');
786 expect(secop.children[0].getAttribute('class')).toEqual('or');
787 expect(secop.children[1].getAttribute('class')).toEqual('delete');
788
789 // This or-operation can be "and"ed or "delete"d
790 expect(e.children[2].getAttribute('class')).toEqual('operators');
791 expect(e.lastChild.getAttribute('class')).toEqual('operators');
792 expect(e.lastChild.children[0].getAttribute('class')).toEqual('and');
793 expect(e.lastChild.children[1].getAttribute('class')).toEqual('delete');
794 });
795 });
796
Akrone4961b12017-05-10 21:04:46 +0200797 describe('KorAP.VirtualCorpus', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000798 var simpleGroupFactory = buildFactory(docGroupClass, {
799 "@type" : "koral:docGroup",
800 "operation" : "operation:and",
801 "operands" : [
802 {
803 "@type": 'koral:doc',
804 "key" : 'author',
805 "match": 'match:eq',
806 "value": 'Max Birkendale',
807 "type": 'type:string'
808 },
809 {
810 "@type": 'koral:doc',
811 "key": 'pubDate',
812 "match": 'match:eq',
813 "value": '2014-12-05',
814 "type": 'type:date'
815 }
816 ]
817 });
818
819 var nestedGroupFactory = buildFactory(vcClass, {
820 "@type" : "koral:docGroup",
821 "operation" : "operation:or",
822 "operands" : [
823 {
824 "@type": 'koral:doc',
825 "key" : 'author',
826 "match": 'match:eq',
827 "value": 'Max Birkendale',
828 "type": 'type:string'
829 },
830 {
831 "@type" : "koral:docGroup",
832 "operation" : "operation:and",
833 "operands" : [
834 {
835 "@type": 'koral:doc',
836 "key": 'pubDate',
837 "match": 'match:geq',
838 "value": '2014-05-12',
839 "type": 'type:date'
840 },
841 {
842 "@type": 'koral:doc',
843 "key": 'pubDate',
844 "match": 'match:leq',
845 "value": '2014-12-05',
846 "type": 'type:date'
847 }
848 ]
849 }
850 ]
851 });
852
853 var flatGroupFactory = buildFactory(vcClass, {
854 "@type" : "koral:docGroup",
855 "operation" : "operation:and",
856 "operands" : [
857 {
858 "@type": 'koral:doc',
859 "key": 'pubDate',
860 "match": 'match:geq',
861 "value": '2014-05-12',
862 "type": 'type:date'
863 },
864 {
865 "@type": 'koral:doc',
866 "key": 'pubDate',
867 "match": 'match:leq',
868 "value": '2014-12-05',
869 "type": 'type:date'
870 },
871 {
872 "@type": 'koral:doc',
873 "key": 'foo',
874 "match": 'match:eq',
875 "value": 'bar',
876 "type": 'type:string'
877 }
878 ]
879 });
880
881 it('should be initializable', function () {
Nils Diewald6283d692015-04-23 20:32:53 +0000882 var vc = vcClass.create();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000883 expect(vc.element().getAttribute('class')).toEqual('vc');
884 expect(vc.root().element().getAttribute('class')).toEqual('doc unspecified');
885
886 // Not removable
887 expect(vc.root().element().lastChild.children.length).toEqual(0);
888 });
889
890 it('should be based on a doc', function () {
Nils Diewald6283d692015-04-23 20:32:53 +0000891 var vc = vcClass.create().fromJson({
Nils Diewald7c8ced22015-04-15 19:21:00 +0000892 "@type" : "koral:doc",
893 "key":"Titel",
894 "value":"Baum",
895 "match":"match:eq"
896 });
897
898 expect(vc.element().getAttribute('class')).toEqual('vc');
899 expect(vc.root().element().getAttribute('class')).toEqual('doc');
900 expect(vc.root().key()).toEqual('Titel');
901 expect(vc.root().value()).toEqual('Baum');
902 expect(vc.root().matchop()).toEqual('eq');
903
904 var docE = vc.root().element();
905 expect(docE.children[0].firstChild.data).toEqual('Titel');
906 expect(docE.children[1].firstChild.data).toEqual('eq');
907 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
908 expect(docE.children[2].firstChild.data).toEqual('Baum');
909 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
910 });
911
912 it('should be based on a docGroup', function () {
Nils Diewald6283d692015-04-23 20:32:53 +0000913 var vc = vcClass.create().fromJson(simpleGroupFactory.create().toJson());
Nils Diewald7c8ced22015-04-15 19:21:00 +0000914
915 expect(vc.element().getAttribute('class')).toEqual('vc');
916 expect(vc.root().element().getAttribute('class')).toEqual('docGroup');
917 expect(vc.root().operation()).toEqual('and');
918
919 var docGroup = vc.root();
920
921 var first = docGroup.getOperand(0);
922 expect(first.key()).toEqual('author');
923 expect(first.value()).toEqual('Max Birkendale');
924 expect(first.matchop()).toEqual('eq');
925
926 var second = docGroup.getOperand(1);
927 expect(second.key()).toEqual('pubDate');
928 expect(second.value()).toEqual('2014-12-05');
929 expect(second.matchop()).toEqual('eq');
930 });
931
932
933 it('should be based on a nested docGroup', function () {
934 var vc = nestedGroupFactory.create();
935
936 expect(vc.element().getAttribute('class')).toEqual('vc');
937 expect(vc.element().firstChild.getAttribute('class')).toEqual('docGroup');
938 expect(vc.element().firstChild.children[0].getAttribute('class')).toEqual('doc');
939 var dg = vc.element().firstChild.children[1];
940 expect(dg.getAttribute('class')).toEqual('docGroup');
941 expect(dg.children[0].getAttribute('class')).toEqual('doc');
942 expect(dg.children[1].getAttribute('class')).toEqual('doc');
943 expect(dg.children[2].getAttribute('class')).toEqual('operators');
944 expect(vc.element().firstChild.children[2].getAttribute('class')).toEqual('operators');
945 });
946
947 it('should be modifiable by deletion in flat docGroups', function () {
948 var vc = flatGroupFactory.create();
949 var docGroup = vc.root();
950
951 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
952
953 var doc = docGroup.getOperand(1);
954 expect(doc.key()).toEqual("pubDate");
955 expect(doc.value()).toEqual("2014-12-05");
956
957 // Remove operand 1
958 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
959 expect(doc._element).toEqual(undefined);
960
961 doc = docGroup.getOperand(1);
962 expect(doc.key()).toEqual("foo");
963
964 // Remove operand 1
965 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
966 expect(doc._element).toEqual(undefined);
967
968 // Only one operand left ...
969 expect(docGroup.getOperand(1)).toBeUndefined();
970 // ... but there shouldn't be a group anymore at all!
971 expect(docGroup.getOperand(0)).toBeUndefined();
972
973 var obj = vc.root();
974 expect(obj.ldType()).toEqual("doc");
975 expect(obj.key()).toEqual("pubDate");
976 expect(obj.value()).toEqual("2014-05-12");
977
978 expect(obj.element().getAttribute('class')).toEqual('doc');
979 });
980
981
982 it('should be modifiable by deletion in nested docGroups (root case)', function () {
983 var vc = nestedGroupFactory.create();
984
985 expect(vc.toQuery()).toEqual(
986 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05)'
987 );
988
989 var docGroup = vc.root();
990 expect(docGroup.ldType()).toEqual("docGroup");
991 expect(docGroup.operation()).toEqual("or");
992
993 var doc = docGroup.getOperand(0);
994 expect(doc.key()).toEqual("author");
995 expect(doc.value()).toEqual("Max Birkendale");
996
997 docGroup = docGroup.getOperand(1);
998 expect(docGroup.operation()).toEqual("and");
999
1000 doc = docGroup.getOperand(0);
1001 expect(doc.key()).toEqual("pubDate");
1002 expect(doc.matchop()).toEqual("geq");
1003 expect(doc.value()).toEqual("2014-05-12");
1004 expect(doc.type()).toEqual("date");
1005
1006 doc = docGroup.getOperand(1);
1007 expect(doc.key()).toEqual("pubDate");
1008 expect(doc.matchop()).toEqual("leq");
1009 expect(doc.value()).toEqual("2014-12-05");
1010 expect(doc.type()).toEqual("date");
1011
1012 // Remove first operand so everything becomes root
1013 expect(
1014 vc.root().delOperand(
1015 vc.root().getOperand(0)
1016 ).update().ldType()
1017 ).toEqual("docGroup");
1018
1019 expect(vc.root().ldType()).toEqual("docGroup");
1020 expect(vc.root().operation()).toEqual("and");
1021 expect(vc.root().getOperand(0).ldType()).toEqual("doc");
1022
1023 expect(vc.toQuery()).toEqual(
1024 'pubDate since 2014-05-12 & pubDate until 2014-12-05'
1025 );
1026 });
1027
1028 it('should be modifiable by deletion in nested docGroups (resolve group case)', function () {
1029 var vc = nestedGroupFactory.create();
1030
1031 // Get nested group
1032 var firstGroup = vc.root().getOperand(1);
1033 firstGroup.append(simpleGroupFactory.create({ "operation" : "operation:or" }));
1034
1035 // Structur is now:
1036 // or(doc, and(doc, doc, or(doc, doc)))
1037
1038 // Get nested or in and
1039 var orGroup = vc.root().getOperand(1).getOperand(2);
1040 expect(orGroup.ldType()).toEqual("docGroup");
1041 expect(orGroup.operation()).toEqual("or");
1042
1043 // Remove
1044 // Structur is now:
1045 // or(doc, and(doc, doc, doc)))
1046 expect(orGroup.delOperand(orGroup.getOperand(0)).update().operation()).toEqual("and");
1047 expect(vc.root().getOperand(1).operands().length).toEqual(3);
1048 });
1049
1050 it('should be modifiable by deletion in nested docGroups (identical group case)', function () {
1051 var vc = nestedGroupFactory.create();
1052
1053 // Get nested group
1054 var firstGroup = vc.root().getOperand(1);
1055 firstGroup.append(simpleGroupFactory.create({
1056 "operation" : "operation:or"
1057 }));
1058 var oldAuthor = firstGroup.getOperand(2).getOperand(0);
1059 oldAuthor.key("title");
1060 oldAuthor.value("Der Birnbaum");
1061
1062 // Structur is now:
1063 // or(doc, and(doc, doc, or(doc, doc)))
1064 expect(vc.toQuery()).toEqual(
1065 'author = "Max Birkendale" | ' +
1066 '(pubDate since 2014-05-12 & ' +
1067 'pubDate until 2014-12-05 & ' +
1068 '(title = "Der Birnbaum" | ' +
1069 'pubDate in 2014-12-05))'
1070 );
1071
1072 var andGroup = vc.root().getOperand(1);
1073
1074 // Get leading docs in and
1075 var doc1 = andGroup.getOperand(0);
1076 expect(doc1.ldType()).toEqual("doc");
1077 expect(doc1.value()).toEqual("2014-05-12");
1078 var doc2 = andGroup.getOperand(1);
1079 expect(doc2.ldType()).toEqual("doc");
1080 expect(doc2.value()).toEqual("2014-12-05");
1081
1082 // Remove 2
1083 expect(
1084 andGroup.delOperand(doc2).update().operation()
1085 ).toEqual("and");
1086 // Structur is now:
1087 // or(doc, and(doc, or(doc, doc)))
1088
1089 expect(vc.toQuery()).toEqual(
1090 'author = "Max Birkendale"' +
1091 ' | (pubDate since 2014-05-12 & ' +
1092 '(title = "Der Birnbaum" | pubDate in 2014-12-05))'
1093 );
1094
1095
1096 // Remove 1
1097 expect(andGroup.delOperand(doc1).update().operation()).toEqual("or");
1098 // Structur is now:
1099 // or(doc, doc, doc)
1100
1101 expect(vc.toQuery()).toEqual(
1102 'author = "Max Birkendale" | title = "Der Birnbaum" | pubDate in 2014-12-05'
1103 );
1104 });
1105
1106 it('should be reducible to unspecification', function () {
1107 var vc = demoFactory.create();
1108
1109 expect(vc.toQuery()).toEqual(vc.root().toQuery());
1110 expect(vc.toQuery()).toEqual(
1111 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & ' +
1112 '(Titel = "Baum" | Veröffentlichungsort = "hihi")) ' +
1113 '| Untertitel = "huhu"');
1114 expect(vc.root().element().lastChild.children[0].firstChild.nodeValue).toEqual('and');
1115 expect(vc.root().element().lastChild.children[1].firstChild.nodeValue).toEqual('×');
1116 expect(vc.root().delOperand(vc.root().getOperand(0)).update()).not.toBeUndefined();
1117 expect(vc.toQuery()).toEqual('Untertitel = "huhu"');
1118
1119 var lc = vc.root().element().lastChild;
1120 expect(lc.children[0].firstChild.nodeValue).toEqual('and');
1121 expect(lc.children[1].firstChild.nodeValue).toEqual('or');
1122 expect(lc.children[2].firstChild.nodeValue).toEqual('×');
1123
1124 // Clean everything
1125 vc.clean();
1126 expect(vc.toQuery()).toEqual('');
1127 });
1128
1129 it('should flatten on import', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001130 var vc = vcClass.create().fromJson({
Nils Diewald7c8ced22015-04-15 19:21:00 +00001131 "@type":"koral:docGroup",
1132 "operation":"operation:or",
1133 "operands":[
1134 {
1135 "@type":"koral:docGroup",
1136 "operation":"operation:or",
1137 "operands":[
1138 {
1139 "@type":"koral:doc",
1140 "key":"Titel",
1141 "value":"Baum",
1142 "match":"match:eq"
1143 },
1144 {
1145 "@type":"koral:doc",
1146 "key":"Veröffentlichungsort",
1147 "value":"hihi",
1148 "match":"match:eq"
1149 },
1150 {
1151 "@type":"koral:docGroup",
1152 "operation":"operation:or",
1153 "operands":[
Nils Diewalde15b7a22015-01-09 21:50:21 +00001154 {
Nils Diewald7c8ced22015-04-15 19:21:00 +00001155 "@type":"koral:doc",
1156 "key":"Titel",
1157 "value":"Baum",
1158 "match":"match:eq"
Nils Diewalde15b7a22015-01-09 21:50:21 +00001159 },
1160 {
Nils Diewald7c8ced22015-04-15 19:21:00 +00001161 "@type":"koral:doc",
1162 "key":"Veröffentlichungsort",
1163 "value":"hihi",
1164 "match":"match:eq"
Nils Diewalde15b7a22015-01-09 21:50:21 +00001165 }
1166 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +00001167 }
Nils Diewald6ac292b2015-01-15 21:33:21 +00001168 ]
1169 },
1170 {
Nils Diewald7c8ced22015-04-15 19:21:00 +00001171 "@type":"koral:doc",
1172 "key":"Untertitel",
1173 "value":"huhu",
1174 "match":"match:eq"
Nils Diewald6ac292b2015-01-15 21:33:21 +00001175 }
1176 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +00001177 });
Nils Diewaldfda29d92015-01-22 17:28:01 +00001178
Nils Diewald7c8ced22015-04-15 19:21:00 +00001179 expect(vc.toQuery()).toEqual(
1180 'Titel = "Baum" | Veröffentlichungsort = "hihi" | Untertitel = "huhu"'
1181 );
Nils Diewald86dad5b2015-01-28 15:09:07 +00001182 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00001183 });
1184
Nils Diewald7c8ced22015-04-15 19:21:00 +00001185 describe('KorAP.Operators', function () {
1186 it('should be initializable', function () {
1187 var op = operatorsClass.create(true, false, false);
1188 expect(op.and()).toBeTruthy();
1189 expect(op.or()).not.toBeTruthy();
1190 expect(op.del()).not.toBeTruthy();
1191
1192 op.and(false);
1193 expect(op.and()).not.toBeTruthy();
1194 expect(op.or()).not.toBeTruthy();
1195 expect(op.del()).not.toBeTruthy();
1196
1197 op.or(true);
1198 op.del(true);
1199 expect(op.and()).not.toBeTruthy();
1200 expect(op.or()).toBeTruthy();
1201 expect(op.del()).toBeTruthy();
1202
1203 var e = op.element();
1204 expect(e.getAttribute('class')).toEqual('operators');
1205 expect(e.children[0].getAttribute('class')).toEqual('or');
1206 expect(e.children[0].firstChild.data).toEqual('or');
1207 expect(e.children[1].getAttribute('class')).toEqual('delete');
1208 expect(e.children[1].firstChild.data).toEqual('×');
1209
1210 op.and(true);
1211 op.del(false);
1212 op.update();
1213
1214 e = op.element();
1215 expect(e.getAttribute('class')).toEqual('operators');
1216 expect(e.children[0].getAttribute('class')).toEqual('and');
1217 expect(e.children[0].firstChild.data).toEqual('and');
1218 expect(e.children[1].getAttribute('class')).toEqual('or');
1219 expect(e.children[1].firstChild.data).toEqual('or');
1220 });
1221 });
1222
1223 describe('KorAP._delete (event)', function () {
1224 var complexVCFactory = buildFactory(vcClass,{
1225 "@type": 'koral:docGroup',
1226 'operation' : 'operation:and',
1227 'operands' : [
1228 {
1229 "@type": 'koral:doc',
1230 "key": 'pubDate',
1231 "match": 'match:eq',
1232 "value": '2014-12-05',
1233 "type": 'type:date'
1234 },
1235 {
1236 "@type" : 'koral:docGroup',
1237 'operation' : 'operation:or',
1238 'operands' : [
1239 {
1240 '@type' : 'koral:doc',
1241 'key' : 'title',
1242 'value' : 'Hello World!'
1243 },
1244 {
1245 '@type' : 'koral:doc',
1246 'key' : 'foo',
1247 'value' : 'bar'
1248 }
1249 ]
1250 }
1251 ]
1252 });
1253
1254 it('should clean on root docs', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001255 var vc = vcClass.create().fromJson({
Nils Diewald7c8ced22015-04-15 19:21:00 +00001256 "@type": 'koral:doc',
1257 "key": 'pubDate',
1258 "match": 'match:eq',
1259 "value": '2014-12-05',
1260 "type": 'type:date'
Nils Diewald86dad5b2015-01-28 15:09:07 +00001261 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00001262 expect(vc.root().toQuery()).toEqual('pubDate in 2014-12-05');
1263 expect(vc.root().element().lastChild.getAttribute('class')).toEqual('operators');
Nils Diewald86dad5b2015-01-28 15:09:07 +00001264
Nils Diewald7c8ced22015-04-15 19:21:00 +00001265 // Clean with delete from root
1266 expect(vc.root().element().lastChild.lastChild.getAttribute('class')).toEqual('delete');
1267 _delOn(vc.root());
1268 expect(vc.root().toQuery()).toEqual('');
1269 expect(vc.root().element().lastChild.lastChild.data).toEqual('⋯');
1270 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00001271
Nils Diewald7c8ced22015-04-15 19:21:00 +00001272 it('should remove on nested docs', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001273 var vc = vcClass.create().fromJson(
Nils Diewald7c8ced22015-04-15 19:21:00 +00001274 {
1275 "@type": 'koral:docGroup',
1276 'operation' : 'operation:and',
1277 'operands' : [
1278 {
1279 "@type": 'koral:doc',
1280 "key": 'pubDate',
1281 "match": 'match:eq',
1282 "value": '2014-12-05',
1283 "type": 'type:date'
1284 },
1285 {
1286 "@type" : 'koral:doc',
1287 'key' : 'foo',
1288 'value' : 'bar'
1289 }
1290 ]
1291 }
1292 );
1293
1294 // Delete with direct element access
1295 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1296 _delOn(vc.root().getOperand(0));
1297
1298 expect(vc.toQuery()).toEqual('foo = "bar"');
1299 expect(vc.root().ldType()).toEqual('doc');
1300 });
1301
1302 it('should clean on doc groups', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001303 var vc = vcClass.create().fromJson(
Nils Diewald7c8ced22015-04-15 19:21:00 +00001304 {
1305 "@type": 'koral:docGroup',
1306 'operation' : 'operation:and',
1307 'operands' : [
1308 {
1309 "@type": 'koral:doc',
1310 "key": 'pubDate',
1311 "match": 'match:eq',
1312 "value": '2014-12-05',
1313 "type": 'type:date'
1314 },
1315 {
1316 "@type" : 'koral:doc',
1317 'key' : 'foo',
1318 'value' : 'bar'
1319 }
1320 ]
1321 }
1322 );
1323
1324 // Cleanwith direct element access
1325 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1326 _delOn(vc.root());
1327 expect(vc.toQuery()).toEqual('');
1328 expect(vc.root().ldType()).toEqual('non');
1329 });
1330
1331 it('should remove on nested doc groups (case of ungrouping 1)', function () {
1332 var vc = complexVCFactory.create();
1333
1334 // Delete with direct element access
1335 expect(vc.toQuery()).toEqual(
1336 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1337 );
1338
1339 // Remove hello world:
1340 _delOn(vc.root().getOperand(1).getOperand(0));
1341 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1342 expect(vc.root().ldType()).toEqual('docGroup');
1343 });
1344
1345 it('should remove on nested doc groups (case of ungrouping 2)', function () {
1346 var vc = complexVCFactory.create();
1347
1348 // Delete with direct element access
1349 expect(vc.toQuery()).toEqual(
1350 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1351 );
1352
1353 // Remove bar
1354 _delOn(vc.root().getOperand(1).getOperand(1));
1355 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & title = "Hello World!"');
1356 expect(vc.root().ldType()).toEqual('docGroup');
1357 expect(vc.root().operation()).toEqual('and');
1358 });
1359
1360 it('should remove on nested doc groups (case of root changing)', function () {
1361 var vc = complexVCFactory.create();
1362
1363 // Delete with direct element access
1364 expect(vc.toQuery()).toEqual(
1365 'pubDate in 2014-12-05 & ' +
1366 '(title = "Hello World!" | foo = "bar")'
1367 );
1368
1369 // Remove bar
1370 _delOn(vc.root().getOperand(0));
1371 expect(vc.toQuery()).toEqual('title = "Hello World!" | foo = "bar"');
1372 expect(vc.root().ldType()).toEqual('docGroup');
1373 expect(vc.root().operation()).toEqual('or');
1374 });
1375
1376 it('should remove on nested doc groups (list flattening)', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001377 var vc = vcClass.create().fromJson(
Nils Diewald7c8ced22015-04-15 19:21:00 +00001378 {
1379 "@type": 'koral:docGroup',
1380 'operation' : 'operation:or',
1381 'operands' : [
1382 {
1383 "@type": 'koral:doc',
1384 "key": 'pubDate',
1385 "match": 'match:eq',
1386 "value": '2014-12-05',
1387 "type": 'type:date'
1388 },
1389 {
1390 "@type" : 'koral:doc',
1391 'key' : 'foo',
1392 'value' : 'bar'
1393 },
1394 {
1395 "@type": 'koral:docGroup',
1396 'operation' : 'operation:and',
1397 'operands' : [
1398 {
1399 "@type": 'koral:doc',
1400 "key": 'pubDate',
1401 "match": 'match:eq',
1402 "value": '2014-12-05',
1403 "type": 'type:date'
1404 },
1405 {
1406 "@type" : 'koral:docGroup',
1407 'operation' : 'operation:or',
1408 'operands' : [
1409 {
1410 '@type' : 'koral:doc',
1411 'key' : 'title',
1412 'value' : 'Hello World!'
1413 },
1414 {
1415 '@type' : 'koral:doc',
1416 'key' : 'yeah',
1417 'value' : 'juhu'
1418 }
1419 ]
1420 }
1421 ]
1422 }
1423 ]
1424 }
1425 );
1426
1427 // Delete with direct element access
1428 expect(vc.toQuery()).toEqual(
1429 'pubDate in 2014-12-05 | foo = "bar" | ' +
1430 '(pubDate in 2014-12-05 & ' +
1431 '(title = "Hello World!" | yeah = "juhu"))'
1432 );
1433
1434 expect(vc.root().ldType()).toEqual('docGroup');
1435 expect(vc.root().operation()).toEqual('or');
1436
1437 // Operands and operators
1438 expect(vc.element().firstChild.children.length).toEqual(4);
1439 expect(vc.element().firstChild.lastChild.getAttribute('class')).toEqual('operators');
1440
1441 // Remove inner group and flatten
1442 _delOn(vc.root().getOperand(2).getOperand(0));
1443
1444 expect(vc.toQuery()).toEqual(
1445 'pubDate in 2014-12-05 | foo = "bar" | title = "Hello World!" | yeah = "juhu"'
1446 );
1447 expect(vc.root().ldType()).toEqual('docGroup');
1448 expect(vc.root().operation()).toEqual('or');
1449
1450 // Operands and operators
1451 expect(vc.element().firstChild.children.length).toEqual(5);
1452 expect(vc.element().firstChild.lastChild.getAttribute('class')).toEqual('operators');
1453 });
1454 });
1455
1456 describe('KorAP._add (event)', function () {
1457 var complexVCFactory = buildFactory(vcClass,{
1458 "@type": 'koral:docGroup',
1459 'operation' : 'operation:and',
1460 'operands' : [
1461 {
1462 "@type": 'koral:doc',
1463 "key": 'pubDate',
1464 "match": 'match:eq',
1465 "value": '2014-12-05',
1466 "type": 'type:date'
1467 },
1468 {
1469 "@type" : 'koral:docGroup',
1470 'operation' : 'operation:or',
1471 'operands' : [
1472 {
1473 '@type' : 'koral:doc',
1474 'key' : 'title',
1475 'value' : 'Hello World!'
1476 },
1477 {
1478 '@type' : 'koral:doc',
1479 'key' : 'foo',
1480 'value' : 'bar'
1481 }
1482 ]
1483 }
1484 ]
1485 });
1486
1487 it('should add new unspecified doc with "and"', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001488 var vc = vcClass.create().fromJson(
Nils Diewald7c8ced22015-04-15 19:21:00 +00001489 {
1490 "@type": 'koral:docGroup',
1491 'operation' : 'operation:and',
1492 'operands' : [
1493 {
1494 "@type": 'koral:doc',
1495 "key": 'pubDate',
1496 "match": 'match:eq',
1497 "value": '2014-12-05',
1498 "type": 'type:date'
1499 },
1500 {
1501 "@type" : 'koral:doc',
1502 'key' : 'foo',
1503 'value' : 'bar'
1504 }
1505 ]
1506 }
1507 );
1508
1509 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1510
1511 var fc = vc.element().firstChild;
1512 expect(fc.getAttribute('data-operation')).toEqual('and');
1513 expect(fc.children.length).toEqual(3);
1514 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1515 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1516 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1517
1518 // add with 'and' in the middle
1519 _andOn(vc.root().getOperand(0));
1520 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1521
1522 fc = vc.element().firstChild;
1523 expect(fc.getAttribute('data-operation')).toEqual('and');
1524 expect(fc.children.length).toEqual(4);
1525 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1526
1527 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1528 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
1529 expect(fc.children[2].getAttribute('class')).toEqual('doc');
1530 });
1531
1532 it('should add new unspecified doc with "or"', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001533 var vc = vcClass.create().fromJson(
Nils Diewald7c8ced22015-04-15 19:21:00 +00001534 {
1535 "@type": 'koral:docGroup',
1536 'operation' : 'operation:and',
1537 'operands' : [
1538 {
1539 "@type": 'koral:doc',
1540 "key": 'pubDate',
1541 "match": 'match:eq',
1542 "value": '2014-12-05',
1543 "type": 'type:date'
1544 },
1545 {
1546 "@type" : 'koral:doc',
1547 'key' : 'foo',
1548 'value' : 'bar'
1549 }
1550 ]
1551 }
1552 );
1553
1554 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1555
1556 var fc = vc.element().firstChild;
1557 expect(fc.children.length).toEqual(3);
1558 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1559 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1560 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1561
1562 // add with 'or' in the middle
1563 _orOn(vc.root().getOperand(0));
1564 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1565 fc = vc.element().firstChild;
1566
1567 expect(fc.getAttribute('data-operation')).toEqual('and');
1568 expect(fc.children.length).toEqual(3);
1569 expect(fc.children[0].getAttribute('class')).toEqual('docGroup');
1570 expect(fc.children[0].getAttribute('data-operation')).toEqual('or');
1571 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1572 expect(fc.children[2].getAttribute('class')).toEqual('operators');
1573 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1574
1575 fc = vc.element().firstChild.firstChild;
1576 expect(fc.children.length).toEqual(3);
1577 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1578 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
1579 expect(fc.children[2].getAttribute('class')).toEqual('operators');
1580 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1581 });
1582
1583 it('should add new unspecified doc with "and" before group', function () {
1584 var vc = demoFactory.create();
1585
1586 // Wrap with direct element access
1587 expect(vc.toQuery()).toEqual(
1588 '(Titel = "Baum" & ' +
1589 'Veröffentlichungsort = "hihi" & ' +
1590 '(Titel = "Baum" | ' +
1591 'Veröffentlichungsort = "hihi")) | ' +
1592 'Untertitel = "huhu"'
1593 );
1594
1595 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
1596 expect(vc.root().getOperand(0).operation()).toEqual('and');
1597 expect(vc.root().getOperand(0).operands().length).toEqual(3);
1598
1599 // Add unspecified on the second doc
1600 var secDoc = vc.root().getOperand(0).getOperand(1);
1601 expect(secDoc.value()).toEqual('hihi');
1602
1603 // Add
1604 _andOn(secDoc);
1605
1606 var fo = vc.root().getOperand(0);
1607
1608 expect(fo.ldType()).toEqual('docGroup');
1609 expect(fo.operation()).toEqual('and');
1610 expect(fo.operands().length).toEqual(4);
1611
1612 expect(fo.getOperand(0).ldType()).toEqual('doc');
1613 expect(fo.getOperand(1).ldType()).toEqual('doc');
1614 expect(fo.getOperand(2).ldType()).toEqual('non');
1615 expect(fo.getOperand(3).ldType()).toEqual('docGroup');
1616 });
1617
1618
1619 it('should remove a doc with an unspecified doc in a nested group', function () {
1620 var vc = demoFactory.create();
1621
1622 // Wrap with direct element access
1623 expect(vc.toQuery()).toEqual(
1624 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & (Titel = "Baum" | Veröffentlichungsort = "hihi")) | Untertitel = "huhu"'
1625 );
1626
1627 var fo = vc.root().getOperand(0).getOperand(0);
1628 expect(fo.key()).toEqual('Titel');
1629 expect(fo.value()).toEqual('Baum');
1630
1631 // Add unspecified on the root group
1632 _orOn(fo);
1633
1634 fo = vc.root().getOperand(0).getOperand(0);
1635
1636 expect(fo.operation()).toEqual('or');
1637 expect(fo.getOperand(0).ldType()).toEqual('doc');
1638 expect(fo.getOperand(1).ldType()).toEqual('non');
1639
1640 // Delete document
1641 _delOn(fo.getOperand(0));
1642
1643 // The operand is now non
1644 expect(vc.root().getOperand(0).getOperand(0).ldType()).toEqual('non');
1645 expect(vc.root().getOperand(0).getOperand(1).ldType()).toEqual('doc');
1646 expect(vc.root().getOperand(0).getOperand(2).ldType()).toEqual('docGroup');
1647 });
1648
1649
1650 it('should remove an unspecified doc with an doc in a nested group', function () {
1651 var vc = demoFactory.create();
1652
1653 // Wrap with direct element access
1654 expect(vc.toQuery()).toEqual(
1655 '(Titel = "Baum" & ' +
1656 'Veröffentlichungsort = "hihi" & ' +
1657 '(Titel = "Baum" ' +
1658 '| Veröffentlichungsort = "hihi")) | ' +
1659 'Untertitel = "huhu"'
1660 );
1661
1662 var fo = vc.root().getOperand(0).getOperand(0);
1663 expect(fo.key()).toEqual('Titel');
1664 expect(fo.value()).toEqual('Baum');
1665
1666 // Add unspecified on the root group
1667 _orOn(fo);
1668
1669 fo = vc.root().getOperand(0).getOperand(0);
1670
1671 expect(fo.operation()).toEqual('or');
1672 expect(fo.getOperand(0).ldType()).toEqual('doc');
1673 expect(fo.getOperand(1).ldType()).toEqual('non');
1674
1675 // Delete unspecified doc
1676 _delOn(fo.getOperand(1));
1677
1678 // The operand is now non
1679 fo = vc.root().getOperand(0);
1680 expect(fo.getOperand(0).ldType()).toEqual('doc');
1681 expect(fo.getOperand(0).key()).toEqual('Titel');
1682 expect(fo.getOperand(0).value()).toEqual('Baum');
1683 expect(fo.getOperand(1).ldType()).toEqual('doc');
1684 expect(fo.getOperand(2).ldType()).toEqual('docGroup');
1685 });
1686
1687
1688 it('should add on parent group (case "and")', function () {
1689 var vc = complexVCFactory.create();
1690
1691 // Wrap with direct element access
1692 expect(vc.toQuery()).toEqual(
1693 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1694 );
1695
1696 expect(vc.root().operands().length).toEqual(2);
1697
1698 // Add unspecified on the root group
1699 _andOn(vc.root().getOperand(1));
1700 expect(vc.toQuery()).toEqual(
1701 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1702 );
1703
1704 expect(vc.root().ldType()).toEqual('docGroup');
1705 expect(vc.root().operands().length).toEqual(3);
1706 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
1707 expect(vc.root().getOperand(1).ldType()).toEqual('docGroup');
1708 expect(vc.root().getOperand(1).operation()).toEqual('or');
1709 expect(vc.root().getOperand(2).ldType()).toEqual('non');
1710
1711 // Add another unspecified on the root group
1712 _andOn(vc.root().getOperand(1));
1713
1714 expect(vc.root().operands().length).toEqual(4);
1715 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
1716 expect(vc.root().getOperand(1).ldType()).toEqual('docGroup');
1717 expect(vc.root().getOperand(2).ldType()).toEqual('non');
1718 expect(vc.root().getOperand(3).ldType()).toEqual('non');
1719
1720 // Add another unspecified after the first doc
1721 _andOn(vc.root().getOperand(0));
1722
1723 expect(vc.root().operands().length).toEqual(5);
1724 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
1725 expect(vc.root().getOperand(1).ldType()).toEqual('non');
1726 expect(vc.root().getOperand(2).ldType()).toEqual('docGroup');
1727 expect(vc.root().getOperand(3).ldType()).toEqual('non');
1728 expect(vc.root().getOperand(4).ldType()).toEqual('non');
1729 });
1730
1731 it('should wrap on root', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001732 var vc = vcClass.create().fromJson(
Nils Diewald7c8ced22015-04-15 19:21:00 +00001733 {
1734 "@type": 'koral:docGroup',
1735 'operation' : 'operation:and',
1736 'operands' : [
1737 {
1738 "@type": 'koral:doc',
1739 "key": 'pubDate',
1740 "match": 'match:eq',
1741 "value": '2014-12-05',
1742 "type": 'type:date'
1743 },
1744 {
1745 "@type" : 'koral:doc',
1746 'key' : 'foo',
1747 'value' : 'bar'
1748 }
1749 ]
1750 }
1751 );
1752
1753 // Wrap on root
1754 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1755 expect(vc.root().ldType()).toEqual('docGroup');
1756 expect(vc.root().operation()).toEqual('and');
1757 _orOn(vc.root());
1758 expect(vc.root().ldType()).toEqual('docGroup');
1759 expect(vc.root().operation()).toEqual('or');
1760
1761 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
1762 expect(vc.root().getOperand(0).operation()).toEqual('and');
1763 });
1764
1765 it('should add on root (case "and")', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001766 var vc = vcClass.create().fromJson(
Nils Diewald7c8ced22015-04-15 19:21:00 +00001767 {
1768 "@type": 'koral:doc',
1769 "key": 'pubDate',
1770 "match": 'match:eq',
1771 "value": '2014-12-05',
1772 "type": 'type:date'
1773 }
1774 );
1775
1776 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
1777 expect(vc.root().ldType()).toEqual('doc');
1778 expect(vc.root().key()).toEqual('pubDate');
1779 expect(vc.root().value()).toEqual('2014-12-05');
1780
1781 // Wrap on root
1782 _andOn(vc.root());
1783 expect(vc.root().ldType()).toEqual('docGroup');
1784 expect(vc.root().operation()).toEqual('and');
1785 });
1786
1787 it('should add on root (case "or")', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001788 var vc = vcClass.create().fromJson(
Nils Diewald7c8ced22015-04-15 19:21:00 +00001789 {
1790 "@type": 'koral:doc',
1791 "key": 'pubDate',
1792 "match": 'match:eq',
1793 "value": '2014-12-05',
1794 "type": 'type:date'
1795 }
1796 );
1797
1798 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
1799 expect(vc.root().key()).toEqual('pubDate');
1800 expect(vc.root().value()).toEqual('2014-12-05');
1801
1802 // Wrap on root
1803 _orOn(vc.root());
1804 expect(vc.root().ldType()).toEqual('docGroup');
1805 expect(vc.root().operation()).toEqual('or');
1806 });
1807
1808 it('should support multiple sub groups per group', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001809 var vc = vcClass.create().fromJson(
Nils Diewald7c8ced22015-04-15 19:21:00 +00001810 {
1811 "@type": 'koral:docGroup',
1812 'operation' : 'operation:or',
1813 'operands' : [
1814 {
1815 "@type": 'koral:docGroup',
1816 'operation' : 'operation:and',
1817 'operands' : [
1818 {
1819 "@type": 'koral:doc',
1820 "key": 'title',
1821 "value": 't1',
1822 },
1823 {
1824 "@type" : 'koral:doc',
1825 'key' : 'title',
1826 'value' : 't2'
1827 }
1828 ]
1829 },
1830 {
1831 "@type": 'koral:docGroup',
1832 'operation' : 'operation:and',
1833 'operands' : [
1834 {
1835 "@type": 'koral:doc',
1836 "key": 'title',
1837 "value": 't3',
1838 },
1839 {
1840 "@type" : 'koral:doc',
1841 'key' : 'title',
1842 'value' : 't4'
1843 }
1844 ]
1845 }
1846 ]
1847 }
1848 );
1849 expect(vc.toQuery()).toEqual(
1850 '(title = "t1" & title = "t2") | ' +
1851 '(title = "t3" & title = "t4")'
1852 );
1853 expect(vc.root().operation()).toEqual('or');
1854 expect(vc.root().getOperand(0).toQuery()).toEqual('title = "t1" & title = "t2"');
1855 expect(vc.root().getOperand(1).toQuery()).toEqual('title = "t3" & title = "t4"');
1856
1857 _andOn(vc.root());
1858
1859 expect(vc.root().operation()).toEqual('and');
1860 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
1861 expect(vc.root().getOperand(1).ldType()).toEqual('non');
1862 });
1863 });
1864
Akron5c829e92017-05-12 18:10:00 +02001865 // Check class method
1866 describe('KorAP.VC.checkRewrite', function () {
1867
1868 it('should check for simple rewrites', function () {
1869 expect(vcClass.checkRewrite(
1870 {
1871 "@type" : "koral:doc",
1872 "rewrites" : [{
1873 "@type" : "koral:rewrite",
1874 "operation" : "operation:modification",
1875 "src" : "querySerializer",
1876 "scope" : "tree"
1877 }]
1878 }
1879 )).toBeTruthy();
Nils Diewald7c8ced22015-04-15 19:21:00 +00001880
Akron5c829e92017-05-12 18:10:00 +02001881 var nested = {
1882 "@type" : "koral:docGroup",
1883 "operands" : [
1884 {
1885 "@type" : "koral:doc"
1886 },
1887 {
1888 "@type" : "koral:docGroup",
1889 "operands" : [
1890 {
1891 "@type": "koral:doc"
1892 },
1893 {
1894 "@type": "koral:doc"
1895 }
1896 ]
1897 }
1898 ]
1899 };
1900
1901 expect(vcClass.checkRewrite(nested)).toBe(false);
1902
1903 nested["operands"][1]["operands"][1]["rewrites"] = [{
1904 "@type" : "koral:rewrite",
1905 "operation" : "operation:modification",
1906 "src" : "querySerializer",
1907 "scope" : "tree"
1908 }];
1909
1910 expect(vcClass.checkRewrite(nested)).toBeTruthy();
1911 });
1912 });
Nils Diewald6283d692015-04-23 20:32:53 +00001913
1914 describe('KorAP.Rewrite', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +00001915 it('should be initializable', function () {
1916 var rewrite = rewriteClass.create({
1917 "@type" : "koral:rewrite",
1918 "operation" : "operation:modification",
1919 "src" : "querySerializer",
1920 "scope" : "tree"
Nils Diewald86dad5b2015-01-28 15:09:07 +00001921 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00001922 expect(rewrite.toString()).toEqual('Modification of "tree" by "querySerializer"');
1923 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00001924
Nils Diewald7c8ced22015-04-15 19:21:00 +00001925 it('should be deserialized by docs', function () {
1926 var doc = docClass.create(
1927 undefined,
1928 {
1929 "@type":"koral:doc",
1930 "key":"Titel",
1931 "value":"Baum",
1932 "match":"match:eq"
1933 });
1934
1935 expect(doc.element().classList.contains('doc')).toBeTruthy();
1936 expect(doc.element().classList.contains('rewritten')).toBe(false);
1937
1938 doc = docClass.create(
1939 undefined,
1940 {
1941 "@type":"koral:doc",
1942 "key":"Titel",
1943 "value":"Baum",
1944 "match":"match:eq",
1945 "rewrites" : [
1946 {
1947 "@type" : "koral:rewrite",
1948 "operation" : "operation:modification",
1949 "src" : "querySerializer",
1950 "scope" : "tree"
1951 }
1952 ]
1953 });
1954
1955 expect(doc.element().classList.contains('doc')).toBeTruthy();
1956 expect(doc.element().classList.contains('rewritten')).toBeTruthy();
1957 });
Nils Diewald6283d692015-04-23 20:32:53 +00001958
1959 xit('should be deserialized by docGroups', function () {
1960 var docGroup = docGroupClass.create(
1961 undefined,
1962 {
1963 "@type" : "koral:docGroup",
1964 "operation" : "operation:or",
1965 "operands" : [
1966 {
1967 "@type" : "doc",
1968 "key" : "pubDate",
1969 "type" : "type:date",
1970 "value" : "2014-12-05"
1971 },
1972 {
1973 "@type" : "doc",
1974 "key" : "pubDate",
1975 "type" : "type:date",
1976 "value" : "2014-12-06"
1977 }
1978 ],
1979 "rewrites" : [
1980 {
1981 "@type" : "koral:rewrite",
1982 "operation" : "operation:modification",
1983 "src" : "querySerializer",
1984 "scope" : "tree"
1985 }
1986 ]
1987 }
1988 );
1989
1990 expect(doc.element().classList.contains('docgroup')).toBeTruthy();
1991 expect(doc.element().classList.contains('rewritten')).toBe(false);
1992 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00001993 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00001994 /*
1995 describe('KorAP.DocKey', function () {
1996 it('should be initializable', function () {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001997 var docKey = KorAP.DocKey.create();
1998 expect(docKey.toString()).toEqual('...');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001999 });
2000 });
Nils Diewaldf0c4f112015-05-05 12:56:59 +00002001
2002 expect(
2003 function() { menuItemClass.create([]) }
2004 ).toThrow(new Error("Missing parameters"));
2005
2006
Nils Diewald7c8ced22015-04-15 19:21:00 +00002007 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +00002008
2009 describe('KorAP.stringValue', function () {
2010 it('should be initializable', function () {
2011 var sv = stringValClass.create();
2012 expect(sv.regex()).toBe(false);
2013 expect(sv.value()).toBe('');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002014
2015 sv = stringValClass.create('Baum');
2016 expect(sv.regex()).toBe(false);
2017 expect(sv.value()).toBe('Baum');
2018
2019 sv = stringValClass.create('Baum', false);
2020 expect(sv.regex()).toBe(false);
2021 expect(sv.value()).toBe('Baum');
2022
2023 sv = stringValClass.create('Baum', true);
2024 expect(sv.regex()).toBe(true);
2025 expect(sv.value()).toBe('Baum');
2026 });
2027
2028 it('should be modifiable', function () {
2029 var sv = stringValClass.create();
2030 expect(sv.regex()).toBe(false);
2031 expect(sv.value()).toBe('');
2032
2033 expect(sv.value('Baum')).toBe('Baum');
2034 expect(sv.value()).toBe('Baum');
2035
2036 expect(sv.regex(true)).toBe(true);
2037 expect(sv.regex()).toBe(true);
2038 });
2039
2040 it('should have a toggleble regex value', function () {
2041 var sv = stringValClass.create();
Nils Diewald7991a3f2015-05-19 14:12:37 +00002042
2043 expect(sv.element().firstChild.value).toBe('');
2044 sv.element().firstChild.value = 'der'
2045 expect(sv.element().firstChild.value).toBe('der');
2046
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002047 expect(sv.regex()).toBe(false);
2048
2049 sv.toggleRegex();
Nils Diewald7991a3f2015-05-19 14:12:37 +00002050 expect(sv.element().firstChild.value).toBe('der');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002051 expect(sv.regex()).toBe(true);
Nils Diewald7991a3f2015-05-19 14:12:37 +00002052 sv.element().firstChild.value = 'derbe'
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002053
2054 sv.toggleRegex();
2055 expect(sv.regex()).toBe(false);
Nils Diewald7991a3f2015-05-19 14:12:37 +00002056 expect(sv.element().firstChild.value).toBe('derbe');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002057 });
2058
2059 it('should have an element', function () {
2060 var sv = stringValClass.create('der');
2061 expect(sv.element().nodeName).toBe('DIV');
2062 expect(sv.element().firstChild.nodeName).toBe('INPUT');
2063 expect(sv.element().firstChild.value).toBe('der');
2064 });
2065
2066 it('should have a classed element', function () {
2067 var sv = stringValClass.create();
2068 console.log(sv.element());
2069 expect(sv.element().classList.contains('regex')).toBe(false);
2070 expect(sv.regex()).toBe(false);
2071 sv.toggleRegex();
2072 expect(sv.element().classList.contains('regex')).toBe(true);
2073 });
2074
2075 it('should be storable', function () {
2076 var sv = stringValClass.create();
2077 var count = 1;
2078 sv.store = function (value, regex) {
Akrone4961b12017-05-10 21:04:46 +02002079 expect(regex).toBe(true);
2080 expect(value).toBe('tree');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002081 };
2082 sv.regex(true);
2083 sv.value('tree');
2084 sv.element().lastChild.click();
Nils Diewaldf0c4f112015-05-05 12:56:59 +00002085 });
2086 });
Akrone4961b12017-05-10 21:04:46 +02002087
2088 // Check prefix
2089 describe('KorAP.VC.Prefix', function () {
2090
2091 it('should be initializable', function () {
2092 var p = prefixClass.create();
2093 expect(p.element().classList.contains('pref')).toBeTruthy();
2094 expect(p.isSet()).not.toBeTruthy();
2095 });
2096
2097
2098 it('should be clickable', function () {
2099 var vc = vcClass.create([
2100 ['a', null],
2101 ['b', null],
2102 ['c', null]
2103 ]).fromJson();
2104 expect(vc.element().firstChild.classList.contains('unspecified')).toBeTruthy();
2105
2106 // This should open up the menu
2107 vc.element().firstChild.firstChild.click();
2108 expect(vc.element().firstChild.firstChild.tagName).toEqual('UL');
2109
2110 KorAP._vcKeyMenu._prefix.clear();
2111 KorAP._vcKeyMenu._prefix.add('x');
2112
2113 var prefElement = vc.element().querySelector('span.pref');
2114 expect(prefElement.innerText).toEqual('x');
2115
2116 // This should add key 'x' to VC
2117 prefElement.click();
2118
2119 expect(vc.element().firstChild.classList.contains('doc')).toBeTruthy();
2120 expect(vc.element().firstChild.firstChild.className).toEqual('key');
2121 expect(vc.element().firstChild.firstChild.innerText).toEqual('x');
2122 });
2123 });
Nils Diewald2fe12e12015-03-06 16:47:06 +00002124});