blob: 89bd8978fb2dafd63e979c112b565301db621948 [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({
Akronea4e9052017-07-06 16:12:05 +0200159 value : { "foo" : "bar" }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000160 });
161 expect(doc).toBeUndefined();
162
163 // Change match type
164 doc = stringFactory.create({
Akronea4e9052017-07-06 16:12:05 +0200165 "match" : "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000166 });
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({
Akronea4e9052017-07-06 16:12:05 +0200175 "match" : { "foo" : "bar" }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000176 });
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({
Akronea4e9052017-07-06 16:12:05 +0200189 match : "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000190 });
191 expect(doc.matchop()).toEqual('ne');
Akronea4e9052017-07-06 16:12:05 +0200192 expect(doc.rewrites()).toBeUndefined();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000193
194 // Invalid matcher
195 doc = regexFactory.create({
Akronea4e9052017-07-06 16:12:05 +0200196 match : "match:chook"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000197 });
Akronea4e9052017-07-06 16:12:05 +0200198 expect(doc.matchop()).toEqual('eq');
199 expect(doc.rewrites()).toBeDefined();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000200
201 // Invalid regex
202 doc = regexFactory.create({
Akronea4e9052017-07-06 16:12:05 +0200203 value : "[^b"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000204 });
205 expect(doc).toBeUndefined();
206 });
207
208 it('should deserialize JSON-LD date', function () {
209
210 // Normal date
211 doc = dateFactory.create({});
212
213 expect(doc.matchop()).toEqual('eq');
214 expect(doc.key()).toEqual("pubDate");
215 expect(doc.type()).toEqual("date");
216 expect(doc.value()).toEqual("2014-11-05");
217
218 // Short date 1
219 doc = dateFactory.create({
Akronea4e9052017-07-06 16:12:05 +0200220 "value" : "2014-11"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000221 });
222
223 expect(doc.matchop()).toEqual('eq');
224 expect(doc.key()).toEqual("pubDate");
225 expect(doc.type()).toEqual("date");
226 expect(doc.value()).toEqual("2014-11");
227
228 // Short date 2
229 doc = dateFactory.create({
Akronea4e9052017-07-06 16:12:05 +0200230 "value" : "2014"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000231 });
232
233 expect(doc.matchop()).toEqual('eq');
234 expect(doc.key()).toEqual("pubDate");
235 expect(doc.type()).toEqual("date");
236 expect(doc.value()).toEqual("2014");
237
238 // Invalid date!
239 doc = dateFactory.create({
Akronea4e9052017-07-06 16:12:05 +0200240 "value" : "2014-11-050"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000241 });
242 expect(doc).toBeUndefined();
243
244 // Invalid matcher!
245 doc = dateFactory.create({
Akronea4e9052017-07-06 16:12:05 +0200246 "match" : "match:ne",
Nils Diewald7c8ced22015-04-15 19:21:00 +0000247 });
Akronea4e9052017-07-06 16:12:05 +0200248 expect(doc).toBeDefined();
249 expect(doc.rewrites()).toBeDefined();
250 expect(doc.matchop()).toEqual('eq');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000251 });
252
253 it('should be serializale to JSON', function () {
254
255 // Empty doc
256 var doc = docClass.create();
257 expect(doc.toJson()).toEqual(jasmine.any(Object));
258
259 // Serialize string
260 doc = stringFactory.create();
261 expect(doc.toJson()).toEqual(jasmine.objectContaining({
262 "@type" : "koral:doc",
263 "type" : "type:string",
264 "key" : "author",
265 "value" : "Max Birkendale",
266 "match" : "match:eq"
267 }));
268
269 // Serialize regex
270 doc = regexFactory.create();
271 expect(doc.toJson()).toEqual(jasmine.objectContaining({
272 "@type" : "koral:doc",
273 "type" : "type:regex",
274 "value" : "[^b]ee.+?",
275 "match" : "match:eq",
276 "key" : 'title'
277 }));
278
279 doc = regexFactory.create({
280 match: "match:ne"
281 });
282 expect(doc.toJson()).toEqual(jasmine.objectContaining({
283 "@type" : "koral:doc",
284 "type" : "type:regex",
285 "value" : "[^b]ee.+?",
286 "match" : "match:ne",
287 "key" : 'title'
288 }));
289
290 doc = dateFactory.create();
291 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Akronea4e9052017-07-06 16:12:05 +0200292 "@type" : "koral:doc",
293 "type" : "type:date",
294 "value" : "2014-11-05",
295 "match" : "match:eq",
296 "key" : 'pubDate'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000297 }));
298
299 doc = dateFactory.create({
300 value : "2014"
301 });
302 expect(doc.toJson()).toEqual(jasmine.objectContaining({
303 "@type" : "koral:doc",
304 "type" : "type:date",
305 "value" : "2014",
306 "match" : "match:eq",
307 "key" : 'pubDate'
308 }));
309 });
310
311
312 it('should be serializale to String', function () {
313 // Empty doc
314 var doc = docClass.create();
315 expect(doc.toQuery()).toEqual("");
316
317 // Serialize string
318 doc = stringFactory.create();
319 expect(doc.toQuery()).toEqual('author = "Max Birkendale"');
320
321 // Serialize string with quotes
322 doc = stringFactory.create({ "value" : 'Max "Der Coole" Birkendate'});
323 expect(doc.toQuery()).toEqual('author = "Max \\"Der Coole\\" Birkendate"');
324
325 // Serialize regex
326 doc = regexFactory.create();
327 expect(doc.toQuery()).toEqual('title = /[^b]ee.+?/');
328
329 doc = regexFactory.create({
Akron8778f5d2017-06-30 21:25:55 +0200330 match: "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000331 });
332 expect(doc.toQuery()).toEqual('title != /[^b]ee.+?/');
333
Akron8778f5d2017-06-30 21:25:55 +0200334 doc = regexFactory.create({
335 value: "WPD/AAA/00001"
336 });
337 expect(doc.toQuery()).toEqual('title = /WPD\\/AAA\\/00001/');
338
Nils Diewald7c8ced22015-04-15 19:21:00 +0000339 doc = dateFactory.create();
340 expect(doc.toQuery()).toEqual('pubDate in 2014-11-05');
341
342 doc = dateFactory.create({
Akron8778f5d2017-06-30 21:25:55 +0200343 value : "2014"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000344 });
345 expect(doc.toQuery()).toEqual('pubDate in 2014');
346 });
347 });
348
349
350 describe('KorAP.DocGroup', function () {
351 // Create example factories
352 var docFactory = buildFactory(
353 docClass,
Nils Diewaldf219eb82015-01-07 20:15:42 +0000354 {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000355 "@type" : "koral:doc",
356 "match":"match:eq",
357 "key" : "author",
358 "value" : "Max Birkendale"
359 }
360 );
361
362 var docGroupFactory = buildFactory(
363 docGroupClass, {
364 "@type" : "koral:docGroup",
365 "operation" : "operation:and",
366 "operands" : [
367 docFactory.create().toJson(),
368 docFactory.create({
369 "key" : "pubDate",
370 "type" : "type:date",
371 "value" : "2014-12-05"
372 }).toJson()
373 ]
374 });
375
376 it('should be initializable', function () {
377 // Create empty group
378 var docGroup = docGroupClass.create();
379 expect(docGroup.operation()).toEqual('and');
380
381 // Create empty group
382 docGroup = docGroupClass.create();
383 docGroup.operation('or');
384 expect(docGroup.operation()).toEqual('or');
385 });
386
387 it('should be definable', function () {
388
389 // Empty group
390 var docGroup = docGroupClass.create();
391 expect(docGroup.operation()).toEqual('and');
392
393 // Set values
394 docGroup.operation("or");
395 expect(docGroup.operation()).toEqual('or');
396
397 // Set invalid values
398 docGroup.operation("hui");
399 expect(docGroup.operation()).toEqual('or');
400 });
401
402 it('should be deserializable', function () {
403 var docGroup = docGroupFactory.create();
404 expect(docGroup.operation()).toEqual("and");
405 expect(docGroup.operands().length).toEqual(2);
406
407 var op1 = docGroup.getOperand(0);
408 expect(op1.type()).toEqual("string");
409 expect(op1.key()).toEqual("author");
410 expect(op1.value()).toEqual("Max Birkendale");
411 expect(op1.matchop()).toEqual("eq");
412
413 var op2 = docGroup.getOperand(1);
414 expect(op2.type()).toEqual("date");
415 expect(op2.key()).toEqual("pubDate");
416 expect(op2.value()).toEqual("2014-12-05");
417 expect(op2.matchop()).toEqual("eq");
418
419 // Append empty group
420 var newGroup = docGroup.append(docGroupClass.create());
421 newGroup.operation('or');
422 newGroup.append(docFactory.create());
423 newGroup.append(docFactory.create({
424 "type" : "type:regex",
425 "key" : "title",
426 "value" : "^e.+?$",
427 "match" : "match:ne"
428 }));
429
430 expect(docGroup.operation()).toEqual("and");
431 expect(docGroup.operands().length).toEqual(3);
432
433 var op1 = docGroup.getOperand(0);
434 expect(op1.ldType()).toEqual("doc");
435 expect(op1.type()).toEqual("string");
436 expect(op1.key()).toEqual("author");
437 expect(op1.value()).toEqual("Max Birkendale");
438 expect(op1.matchop()).toEqual("eq");
439
440 var op2 = docGroup.getOperand(1);
441 expect(op2.ldType()).toEqual("doc");
442 expect(op2.type()).toEqual("date");
443 expect(op2.key()).toEqual("pubDate");
444 expect(op2.value()).toEqual("2014-12-05");
445 expect(op2.matchop()).toEqual("eq");
446
447 var op3 = docGroup.getOperand(2);
448 expect(op3.ldType()).toEqual("docGroup");
449 expect(op3.operation()).toEqual("or");
450
451 var op4 = op3.getOperand(0);
452 expect(op4.ldType()).toEqual("doc");
453 expect(op4.type()).toEqual("string");
454 expect(op4.key()).toEqual("author");
455 expect(op4.value()).toEqual("Max Birkendale");
456 expect(op4.matchop()).toEqual("eq");
457
458 var op5 = op3.getOperand(1);
459 expect(op5.ldType()).toEqual("doc");
460 expect(op5.type()).toEqual("regex");
461 expect(op5.key()).toEqual("title");
462 expect(op5.value()).toEqual("^e.+?$");
463 expect(op5.matchop()).toEqual("ne");
464 });
465
466 it('should be serializable to JSON', function () {
467 var docGroup = docGroupFactory.create();
468
469 expect(docGroup.toJson()).toEqual(jasmine.objectContaining({
Nils Diewald2fe12e12015-03-06 16:47:06 +0000470 "@type" : "koral:docGroup",
Nils Diewaldf219eb82015-01-07 20:15:42 +0000471 "operation" : "operation:and",
472 "operands" : [
473 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000474 "@type": 'koral:doc',
Nils Diewald7c8ced22015-04-15 19:21:00 +0000475 "key" : 'author',
476 "match": 'match:eq',
477 "value": 'Max Birkendale',
478 "type": 'type:string'
Nils Diewaldf219eb82015-01-07 20:15:42 +0000479 },
480 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000481 "@type": 'koral:doc',
Nils Diewaldf219eb82015-01-07 20:15:42 +0000482 "key": 'pubDate',
Nils Diewalde15b7a22015-01-09 21:50:21 +0000483 "match": 'match:eq',
484 "value": '2014-12-05',
485 "type": 'type:date'
Nils Diewalde15b7a22015-01-09 21:50:21 +0000486 }
487 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000488 }));
489 });
Nils Diewalde15b7a22015-01-09 21:50:21 +0000490
Nils Diewald7c8ced22015-04-15 19:21:00 +0000491 it('should be serializable to String', function () {
492 var docGroup = docGroupFactory.create();
493 expect(docGroup.toQuery()).toEqual(
494 'author = "Max Birkendale" & pubDate in 2014-12-05'
495 );
Nils Diewald52f7eb12015-01-12 17:30:04 +0000496
Nils Diewald7c8ced22015-04-15 19:21:00 +0000497 docGroup = docGroupFactory.create({
498 "@type" : "koral:docGroup",
499 "operation" : "operation:or",
500 "operands" : [
Nils Diewalde15b7a22015-01-09 21:50:21 +0000501 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000502 "@type": 'koral:doc',
Nils Diewald7c8ced22015-04-15 19:21:00 +0000503 "key" : 'author',
Nils Diewalde15b7a22015-01-09 21:50:21 +0000504 "match": 'match:eq',
Nils Diewald7c8ced22015-04-15 19:21:00 +0000505 "value": 'Max Birkendale',
506 "type": 'type:string'
Nils Diewalde15b7a22015-01-09 21:50:21 +0000507 },
508 {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000509 "@type" : "koral:docGroup",
510 "operation" : "operation:and",
511 "operands" : [
Nils Diewalde15b7a22015-01-09 21:50:21 +0000512 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000513 "@type": 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +0000514 "key": 'pubDate',
Nils Diewald7c8ced22015-04-15 19:21:00 +0000515 "match": 'match:geq',
516 "value": '2014-05-12',
517 "type": 'type:date'
518 },
519 {
520 "@type": 'koral:doc',
521 "key": 'pubDate',
522 "match": 'match:leq',
Nils Diewalde15b7a22015-01-09 21:50:21 +0000523 "value": '2014-12-05',
524 "type": 'type:date'
525 },
526 {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000527 "@type": 'koral:doc',
528 "key": 'foo',
529 "match": 'match:ne',
530 "value": '[a]?bar',
531 "type": 'type:regex'
532 }
533 ]
534 }
535 ]
536 });
537 expect(docGroup.toQuery()).toEqual(
538 'author = "Max Birkendale" | ' +
539 '(pubDate since 2014-05-12 & ' +
540 'pubDate until 2014-12-05 & foo != /[a]?bar/)'
541 );
542 });
543 });
544
545 describe('KorAP.UnspecifiedDoc', function () {
546 it('should be initializable', function () {
547 var doc = unspecifiedClass.create();
548 var docElement = doc.element();
549 expect(docElement.getAttribute('class')).toEqual('doc unspecified');
550 expect(docElement.firstChild.firstChild.data).toEqual('⋯');
551 expect(docElement.lastChild.lastChild.data).toEqual('⋯');
552 expect(doc.toQuery()).toEqual('');
553
554 // Only removable
555 expect(docElement.lastChild.children.length).toEqual(0);
556 });
557
558 it('should be removable, when no root', function () {
559 var docGroup = docGroupClass.create();
560 docGroup.operation('or');
561 expect(docGroup.operation()).toEqual('or');
562
563 docGroup.append({
564 "@type": 'koral:doc',
565 "key": 'pubDate',
566 "match": 'match:eq',
567 "value": '2014-12-05',
568 "type": 'type:date'
569 });
570
571 // Add unspecified object
572 docGroup.append();
573
574 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
575 expect(docGroup.element().children[0].getAttribute('class')).toEqual('doc');
576
577 var unspec = docGroup.element().children[1];
578 expect(unspec.getAttribute('class')).toEqual('doc unspecified');
579
580 // Removable
581 expect(unspec.lastChild.children.length).toEqual(1);
582 expect(unspec.lastChild.children[0].getAttribute('class')).toEqual('delete');
583 });
584
585 it('should be replaceable by a doc', function () {
586 var doc = unspecifiedClass.create();
587 expect(doc.ldType()).toEqual("non");
588 // No parent, therefor not updateable
589 expect(doc.key("baum")).toBeNull();
590
591 var docGroup = docGroupClass.create();
592 docGroup.operation('or');
593 expect(docGroup.operation()).toEqual('or');
594
595 docGroup.append({
596 "@type": 'koral:doc',
597 "key": 'pubDate',
598 "match": 'match:eq',
599 "value": '2014-12-05',
600 "type": 'type:date'
601 });
602
603 expect(docGroup.toQuery()).toEqual("pubDate in 2014-12-05");
604 docGroup.append();
605
606 expect(docGroup.getOperand(0).ldType()).toEqual("doc");
607 expect(docGroup.getOperand(1).ldType()).toEqual("non");
608
609 var op = docGroup.getOperand(1).element().lastChild;
Akron0b489ad2018-02-02 16:49:32 +0100610 expect(op.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000611 expect(op.children[0].getAttribute('class')).toEqual('delete');
612 expect(op.children.length).toEqual(1);
613
614 // Replace unspecified doc
615 expect(docGroup.getOperand(1).key("name")).not.toBeNull();
616 expect(docGroup.getOperand(1).ldType()).toEqual("doc");
617 expect(docGroup.getOperand(1).key()).toEqual("name");
618 expect(docGroup.getOperand(1).value()).toEqual("");
619
620 op = docGroup.getOperand(1).element().lastChild;
Akron0b489ad2018-02-02 16:49:32 +0100621 expect(op.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000622 expect(op.children[0].getAttribute('class')).toEqual('and');
623 expect(op.children[1].getAttribute('class')).toEqual('or');
624 expect(op.children[2].getAttribute('class')).toEqual('delete');
625 expect(op.children.length).toEqual(3);
626
627 docGroup.getOperand(1).value("Pachelbel");
628 expect(docGroup.getOperand(1).value()).toEqual("Pachelbel");
629 expect(docGroup.getOperand(1).type()).toEqual("string");
630 expect(docGroup.getOperand(1).matchop()).toEqual("eq");
631
632 // Specified!
633 expect(docGroup.toQuery()).toEqual('pubDate in 2014-12-05 | name = "Pachelbel"');
634 });
635
636 it('should be replaceable on root', function () {
Nils Diewald6283d692015-04-23 20:32:53 +0000637 var vc = vcClass.create();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000638 expect(vc.toQuery()).toEqual("");
639
640 expect(vc.root().ldType()).toEqual("non");
641
642 // No operators on root
643 op = vc.root().element().lastChild;
644 expect(op.lastChild.textContent).toEqual('⋯');
645
646 // Replace
647 expect(vc.root().key("baum")).not.toBeNull();
648 expect(vc.root().ldType()).toEqual("doc");
649
650 op = vc.root().element().lastChild;
Akron0b489ad2018-02-02 16:49:32 +0100651 expect(op.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000652 expect(op.children[0].getAttribute('class')).toEqual('and');
653 expect(op.children[1].getAttribute('class')).toEqual('or');
654 expect(op.children[2].getAttribute('class')).toEqual('delete');
655 expect(op.children.length).toEqual(3);
656 });
657 });
658
659 describe('KorAP.Doc element', function () {
660 it('should be initializable', function () {
661 var docElement = docClass.create(undefined, {
662 "@type" : "koral:doc",
663 "key":"Titel",
664 "value":"Baum",
665 "match":"match:eq"
666 });
667 expect(docElement.key()).toEqual('Titel');
668 expect(docElement.matchop()).toEqual('eq');
669 expect(docElement.value()).toEqual('Baum');
670
671 var docE = docElement.element();
672 expect(docE.children[0].firstChild.data).toEqual('Titel');
673 expect(docE.children[1].firstChild.data).toEqual('eq');
674 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
675 expect(docE.children[2].firstChild.data).toEqual('Baum');
676 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
677
678 expect(docElement.toJson()).toEqual(jasmine.objectContaining({
679 "@type" : "koral:doc",
680 "key":"Titel",
681 "value":"Baum",
682 "match":"match:eq"
683 }));
684 });
685 });
686
687 describe('KorAP.DocGroup element', function () {
688 it('should be initializable', function () {
689
690 var docGroup = docGroupClass.create(undefined, {
691 "@type" : "koral:docGroup",
692 "operation" : "operation:and",
693 "operands" : [
694 {
695 "@type": 'koral:doc',
696 "key" : 'author',
697 "match": 'match:eq',
698 "value": 'Max Birkendale',
699 "type": 'type:string'
700 },
701 {
702 "@type": 'koral:doc',
703 "key": 'pubDate',
704 "match": 'match:eq',
705 "value": '2014-12-05',
706 "type": 'type:date'
707 }
708 ]
709 });
710
711 expect(docGroup.operation()).toEqual('and');
712 var e = docGroup.element();
713 expect(e.getAttribute('class')).toEqual('docGroup');
714 expect(e.getAttribute('data-operation')).toEqual('and');
715
716 var first = e.children[0];
717 expect(first.getAttribute('class')).toEqual('doc');
718 expect(first.children[0].getAttribute('class')).toEqual('key');
719 expect(first.children[1].getAttribute('class')).toEqual('match');
720 expect(first.children[2].getAttribute('class')).toEqual('value');
721 expect(first.children[2].getAttribute('data-type')).toEqual('string');
722 expect(first.children[0].firstChild.data).toEqual('author');
723 expect(first.children[1].firstChild.data).toEqual('eq');
724 expect(first.children[2].firstChild.data).toEqual('Max Birkendale');
725
726 var second = e.children[1];
727 expect(second.getAttribute('class')).toEqual('doc');
728 expect(second.children[0].getAttribute('class')).toEqual('key');
729 expect(second.children[1].getAttribute('class')).toEqual('match');
730 expect(second.children[2].getAttribute('class')).toEqual('value');
731 expect(second.children[2].getAttribute('data-type')).toEqual('date');
732 expect(second.children[0].firstChild.data).toEqual('pubDate');
733 expect(second.children[1].firstChild.data).toEqual('eq');
734 expect(second.children[2].firstChild.data).toEqual('2014-12-05');
735 });
736
737 it('should be deserializable with nested groups', function () {
738 var docGroup = docGroupClass.create(undefined, {
739 "@type" : "koral:docGroup",
740 "operation" : "operation:or",
741 "operands" : [
742 {
743 "@type": 'koral:doc',
744 "key" : 'author',
745 "match": 'match:eq',
746 "value": 'Max Birkendale',
747 "type": 'type:string'
748 },
749 {
750 "@type" : "koral:docGroup",
751 "operation" : "operation:and",
752 "operands" : [
753 {
754 "@type": 'koral:doc',
755 "key": 'pubDate',
756 "match": 'match:geq',
757 "value": '2014-05-12',
758 "type": 'type:date'
759 },
760 {
761 "@type": 'koral:doc',
762 "key": 'pubDate',
763 "match": 'match:leq',
764 "value": '2014-12-05',
765 "type": 'type:date'
766 }
767 ]
768 }
769 ]
770 });
771
772 expect(docGroup.operation()).toEqual('or');
773 var e = docGroup.element();
774 expect(e.getAttribute('class')).toEqual('docGroup');
775 expect(e.getAttribute('data-operation')).toEqual('or');
776
777 expect(e.children[0].getAttribute('class')).toEqual('doc');
778 var docop = e.children[0].lastChild;
Akron0b489ad2018-02-02 16:49:32 +0100779 expect(docop.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000780 expect(docop.children[0].getAttribute('class')).toEqual('and');
781 expect(docop.children[1].getAttribute('class')).toEqual('or');
782 expect(docop.children[2].getAttribute('class')).toEqual('delete');
783
784 expect(e.children[1].getAttribute('class')).toEqual('docGroup');
785 expect(e.children[1].getAttribute('data-operation')).toEqual('and');
786
787 // This and-operation can be "or"ed or "delete"d
788 var secop = e.children[1].children[2];
Akron0b489ad2018-02-02 16:49:32 +0100789 expect(secop.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000790 expect(secop.children[0].getAttribute('class')).toEqual('or');
791 expect(secop.children[1].getAttribute('class')).toEqual('delete');
792
793 // This or-operation can be "and"ed or "delete"d
Akron0b489ad2018-02-02 16:49:32 +0100794 expect(e.children[2].getAttribute('class')).toEqual('operators button-group');
795 expect(e.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000796 expect(e.lastChild.children[0].getAttribute('class')).toEqual('and');
797 expect(e.lastChild.children[1].getAttribute('class')).toEqual('delete');
798 });
799 });
800
Akrone4961b12017-05-10 21:04:46 +0200801 describe('KorAP.VirtualCorpus', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000802 var simpleGroupFactory = buildFactory(docGroupClass, {
803 "@type" : "koral:docGroup",
804 "operation" : "operation:and",
805 "operands" : [
806 {
807 "@type": 'koral:doc',
808 "key" : 'author',
809 "match": 'match:eq',
810 "value": 'Max Birkendale',
811 "type": 'type:string'
812 },
813 {
814 "@type": 'koral:doc',
815 "key": 'pubDate',
816 "match": 'match:eq',
817 "value": '2014-12-05',
818 "type": 'type:date'
819 }
820 ]
821 });
822
823 var nestedGroupFactory = buildFactory(vcClass, {
824 "@type" : "koral:docGroup",
825 "operation" : "operation:or",
826 "operands" : [
827 {
828 "@type": 'koral:doc',
829 "key" : 'author',
830 "match": 'match:eq',
831 "value": 'Max Birkendale',
832 "type": 'type:string'
833 },
834 {
835 "@type" : "koral:docGroup",
836 "operation" : "operation:and",
837 "operands" : [
838 {
839 "@type": 'koral:doc',
840 "key": 'pubDate',
841 "match": 'match:geq',
842 "value": '2014-05-12',
843 "type": 'type:date'
844 },
845 {
846 "@type": 'koral:doc',
847 "key": 'pubDate',
848 "match": 'match:leq',
849 "value": '2014-12-05',
850 "type": 'type:date'
851 }
852 ]
853 }
854 ]
855 });
856
857 var flatGroupFactory = buildFactory(vcClass, {
858 "@type" : "koral:docGroup",
859 "operation" : "operation:and",
860 "operands" : [
861 {
862 "@type": 'koral:doc',
863 "key": 'pubDate',
864 "match": 'match:geq',
865 "value": '2014-05-12',
866 "type": 'type:date'
867 },
868 {
869 "@type": 'koral:doc',
870 "key": 'pubDate',
871 "match": 'match:leq',
872 "value": '2014-12-05',
873 "type": 'type:date'
874 },
875 {
876 "@type": 'koral:doc',
877 "key": 'foo',
878 "match": 'match:eq',
879 "value": 'bar',
880 "type": 'type:string'
881 }
882 ]
883 });
884
885 it('should be initializable', function () {
Nils Diewald6283d692015-04-23 20:32:53 +0000886 var vc = vcClass.create();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000887 expect(vc.element().getAttribute('class')).toEqual('vc');
888 expect(vc.root().element().getAttribute('class')).toEqual('doc unspecified');
889
890 // Not removable
891 expect(vc.root().element().lastChild.children.length).toEqual(0);
892 });
893
894 it('should be based on a doc', function () {
Nils Diewald6283d692015-04-23 20:32:53 +0000895 var vc = vcClass.create().fromJson({
Nils Diewald7c8ced22015-04-15 19:21:00 +0000896 "@type" : "koral:doc",
897 "key":"Titel",
898 "value":"Baum",
899 "match":"match:eq"
900 });
901
902 expect(vc.element().getAttribute('class')).toEqual('vc');
903 expect(vc.root().element().getAttribute('class')).toEqual('doc');
904 expect(vc.root().key()).toEqual('Titel');
905 expect(vc.root().value()).toEqual('Baum');
906 expect(vc.root().matchop()).toEqual('eq');
907
908 var docE = vc.root().element();
909 expect(docE.children[0].firstChild.data).toEqual('Titel');
910 expect(docE.children[1].firstChild.data).toEqual('eq');
911 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
912 expect(docE.children[2].firstChild.data).toEqual('Baum');
913 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
914 });
915
916 it('should be based on a docGroup', function () {
Nils Diewald6283d692015-04-23 20:32:53 +0000917 var vc = vcClass.create().fromJson(simpleGroupFactory.create().toJson());
Nils Diewald7c8ced22015-04-15 19:21:00 +0000918
919 expect(vc.element().getAttribute('class')).toEqual('vc');
920 expect(vc.root().element().getAttribute('class')).toEqual('docGroup');
921 expect(vc.root().operation()).toEqual('and');
922
923 var docGroup = vc.root();
924
925 var first = docGroup.getOperand(0);
926 expect(first.key()).toEqual('author');
927 expect(first.value()).toEqual('Max Birkendale');
928 expect(first.matchop()).toEqual('eq');
929
930 var second = docGroup.getOperand(1);
931 expect(second.key()).toEqual('pubDate');
932 expect(second.value()).toEqual('2014-12-05');
933 expect(second.matchop()).toEqual('eq');
934 });
935
936
937 it('should be based on a nested docGroup', function () {
938 var vc = nestedGroupFactory.create();
939
940 expect(vc.element().getAttribute('class')).toEqual('vc');
941 expect(vc.element().firstChild.getAttribute('class')).toEqual('docGroup');
942 expect(vc.element().firstChild.children[0].getAttribute('class')).toEqual('doc');
943 var dg = vc.element().firstChild.children[1];
944 expect(dg.getAttribute('class')).toEqual('docGroup');
945 expect(dg.children[0].getAttribute('class')).toEqual('doc');
946 expect(dg.children[1].getAttribute('class')).toEqual('doc');
Akron0b489ad2018-02-02 16:49:32 +0100947 expect(dg.children[2].getAttribute('class')).toEqual('operators button-group');
948 expect(vc.element().firstChild.children[2].getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000949 });
950
951 it('should be modifiable by deletion in flat docGroups', function () {
952 var vc = flatGroupFactory.create();
953 var docGroup = vc.root();
954
955 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
956
957 var doc = docGroup.getOperand(1);
958 expect(doc.key()).toEqual("pubDate");
959 expect(doc.value()).toEqual("2014-12-05");
960
961 // Remove operand 1
962 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
963 expect(doc._element).toEqual(undefined);
964
965 doc = docGroup.getOperand(1);
966 expect(doc.key()).toEqual("foo");
967
968 // Remove operand 1
969 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
970 expect(doc._element).toEqual(undefined);
971
972 // Only one operand left ...
973 expect(docGroup.getOperand(1)).toBeUndefined();
974 // ... but there shouldn't be a group anymore at all!
975 expect(docGroup.getOperand(0)).toBeUndefined();
976
977 var obj = vc.root();
978 expect(obj.ldType()).toEqual("doc");
979 expect(obj.key()).toEqual("pubDate");
980 expect(obj.value()).toEqual("2014-05-12");
981
982 expect(obj.element().getAttribute('class')).toEqual('doc');
983 });
984
985
986 it('should be modifiable by deletion in nested docGroups (root case)', function () {
987 var vc = nestedGroupFactory.create();
988
989 expect(vc.toQuery()).toEqual(
990 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05)'
991 );
992
993 var docGroup = vc.root();
994 expect(docGroup.ldType()).toEqual("docGroup");
995 expect(docGroup.operation()).toEqual("or");
996
997 var doc = docGroup.getOperand(0);
998 expect(doc.key()).toEqual("author");
999 expect(doc.value()).toEqual("Max Birkendale");
1000
1001 docGroup = docGroup.getOperand(1);
1002 expect(docGroup.operation()).toEqual("and");
1003
1004 doc = docGroup.getOperand(0);
1005 expect(doc.key()).toEqual("pubDate");
1006 expect(doc.matchop()).toEqual("geq");
1007 expect(doc.value()).toEqual("2014-05-12");
1008 expect(doc.type()).toEqual("date");
1009
1010 doc = docGroup.getOperand(1);
1011 expect(doc.key()).toEqual("pubDate");
1012 expect(doc.matchop()).toEqual("leq");
1013 expect(doc.value()).toEqual("2014-12-05");
1014 expect(doc.type()).toEqual("date");
1015
1016 // Remove first operand so everything becomes root
1017 expect(
1018 vc.root().delOperand(
1019 vc.root().getOperand(0)
1020 ).update().ldType()
1021 ).toEqual("docGroup");
1022
1023 expect(vc.root().ldType()).toEqual("docGroup");
1024 expect(vc.root().operation()).toEqual("and");
1025 expect(vc.root().getOperand(0).ldType()).toEqual("doc");
1026
1027 expect(vc.toQuery()).toEqual(
1028 'pubDate since 2014-05-12 & pubDate until 2014-12-05'
1029 );
1030 });
1031
1032 it('should be modifiable by deletion in nested docGroups (resolve group case)', function () {
1033 var vc = nestedGroupFactory.create();
1034
1035 // Get nested group
1036 var firstGroup = vc.root().getOperand(1);
1037 firstGroup.append(simpleGroupFactory.create({ "operation" : "operation:or" }));
1038
1039 // Structur is now:
1040 // or(doc, and(doc, doc, or(doc, doc)))
1041
1042 // Get nested or in and
1043 var orGroup = vc.root().getOperand(1).getOperand(2);
1044 expect(orGroup.ldType()).toEqual("docGroup");
1045 expect(orGroup.operation()).toEqual("or");
1046
1047 // Remove
1048 // Structur is now:
1049 // or(doc, and(doc, doc, doc)))
1050 expect(orGroup.delOperand(orGroup.getOperand(0)).update().operation()).toEqual("and");
1051 expect(vc.root().getOperand(1).operands().length).toEqual(3);
1052 });
1053
1054 it('should be modifiable by deletion in nested docGroups (identical group case)', function () {
1055 var vc = nestedGroupFactory.create();
1056
1057 // Get nested group
1058 var firstGroup = vc.root().getOperand(1);
1059 firstGroup.append(simpleGroupFactory.create({
1060 "operation" : "operation:or"
1061 }));
1062 var oldAuthor = firstGroup.getOperand(2).getOperand(0);
1063 oldAuthor.key("title");
1064 oldAuthor.value("Der Birnbaum");
1065
1066 // Structur is now:
1067 // or(doc, and(doc, doc, or(doc, doc)))
1068 expect(vc.toQuery()).toEqual(
1069 'author = "Max Birkendale" | ' +
1070 '(pubDate since 2014-05-12 & ' +
1071 'pubDate until 2014-12-05 & ' +
1072 '(title = "Der Birnbaum" | ' +
1073 'pubDate in 2014-12-05))'
1074 );
1075
1076 var andGroup = vc.root().getOperand(1);
1077
1078 // Get leading docs in and
1079 var doc1 = andGroup.getOperand(0);
1080 expect(doc1.ldType()).toEqual("doc");
1081 expect(doc1.value()).toEqual("2014-05-12");
1082 var doc2 = andGroup.getOperand(1);
1083 expect(doc2.ldType()).toEqual("doc");
1084 expect(doc2.value()).toEqual("2014-12-05");
1085
1086 // Remove 2
1087 expect(
1088 andGroup.delOperand(doc2).update().operation()
1089 ).toEqual("and");
1090 // Structur is now:
1091 // or(doc, and(doc, or(doc, doc)))
1092
1093 expect(vc.toQuery()).toEqual(
1094 'author = "Max Birkendale"' +
1095 ' | (pubDate since 2014-05-12 & ' +
1096 '(title = "Der Birnbaum" | pubDate in 2014-12-05))'
1097 );
1098
1099
1100 // Remove 1
1101 expect(andGroup.delOperand(doc1).update().operation()).toEqual("or");
1102 // Structur is now:
1103 // or(doc, doc, doc)
1104
1105 expect(vc.toQuery()).toEqual(
1106 'author = "Max Birkendale" | title = "Der Birnbaum" | pubDate in 2014-12-05'
1107 );
1108 });
1109
1110 it('should be reducible to unspecification', function () {
1111 var vc = demoFactory.create();
1112
1113 expect(vc.toQuery()).toEqual(vc.root().toQuery());
1114 expect(vc.toQuery()).toEqual(
1115 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & ' +
1116 '(Titel = "Baum" | Veröffentlichungsort = "hihi")) ' +
1117 '| Untertitel = "huhu"');
1118 expect(vc.root().element().lastChild.children[0].firstChild.nodeValue).toEqual('and');
1119 expect(vc.root().element().lastChild.children[1].firstChild.nodeValue).toEqual('×');
1120 expect(vc.root().delOperand(vc.root().getOperand(0)).update()).not.toBeUndefined();
1121 expect(vc.toQuery()).toEqual('Untertitel = "huhu"');
1122
1123 var lc = vc.root().element().lastChild;
1124 expect(lc.children[0].firstChild.nodeValue).toEqual('and');
1125 expect(lc.children[1].firstChild.nodeValue).toEqual('or');
1126 expect(lc.children[2].firstChild.nodeValue).toEqual('×');
1127
1128 // Clean everything
1129 vc.clean();
1130 expect(vc.toQuery()).toEqual('');
1131 });
1132
1133 it('should flatten on import', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001134 var vc = vcClass.create().fromJson({
Nils Diewald7c8ced22015-04-15 19:21:00 +00001135 "@type":"koral:docGroup",
1136 "operation":"operation:or",
1137 "operands":[
1138 {
1139 "@type":"koral:docGroup",
1140 "operation":"operation:or",
1141 "operands":[
1142 {
1143 "@type":"koral:doc",
1144 "key":"Titel",
1145 "value":"Baum",
1146 "match":"match:eq"
1147 },
1148 {
1149 "@type":"koral:doc",
1150 "key":"Veröffentlichungsort",
1151 "value":"hihi",
1152 "match":"match:eq"
1153 },
1154 {
1155 "@type":"koral:docGroup",
1156 "operation":"operation:or",
1157 "operands":[
Nils Diewalde15b7a22015-01-09 21:50:21 +00001158 {
Nils Diewald7c8ced22015-04-15 19:21:00 +00001159 "@type":"koral:doc",
1160 "key":"Titel",
1161 "value":"Baum",
1162 "match":"match:eq"
Nils Diewalde15b7a22015-01-09 21:50:21 +00001163 },
1164 {
Nils Diewald7c8ced22015-04-15 19:21:00 +00001165 "@type":"koral:doc",
1166 "key":"Veröffentlichungsort",
1167 "value":"hihi",
1168 "match":"match:eq"
Nils Diewalde15b7a22015-01-09 21:50:21 +00001169 }
1170 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +00001171 }
Nils Diewald6ac292b2015-01-15 21:33:21 +00001172 ]
1173 },
1174 {
Nils Diewald7c8ced22015-04-15 19:21:00 +00001175 "@type":"koral:doc",
1176 "key":"Untertitel",
1177 "value":"huhu",
1178 "match":"match:eq"
Nils Diewald6ac292b2015-01-15 21:33:21 +00001179 }
1180 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +00001181 });
Nils Diewaldfda29d92015-01-22 17:28:01 +00001182
Nils Diewald7c8ced22015-04-15 19:21:00 +00001183 expect(vc.toQuery()).toEqual(
1184 'Titel = "Baum" | Veröffentlichungsort = "hihi" | Untertitel = "huhu"'
1185 );
Nils Diewald86dad5b2015-01-28 15:09:07 +00001186 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00001187 });
1188
Nils Diewald7c8ced22015-04-15 19:21:00 +00001189 describe('KorAP.Operators', function () {
1190 it('should be initializable', function () {
1191 var op = operatorsClass.create(true, false, false);
1192 expect(op.and()).toBeTruthy();
1193 expect(op.or()).not.toBeTruthy();
1194 expect(op.del()).not.toBeTruthy();
1195
1196 op.and(false);
1197 expect(op.and()).not.toBeTruthy();
1198 expect(op.or()).not.toBeTruthy();
1199 expect(op.del()).not.toBeTruthy();
1200
1201 op.or(true);
1202 op.del(true);
1203 expect(op.and()).not.toBeTruthy();
1204 expect(op.or()).toBeTruthy();
1205 expect(op.del()).toBeTruthy();
1206
1207 var e = op.element();
Akron0b489ad2018-02-02 16:49:32 +01001208 expect(e.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001209 expect(e.children[0].getAttribute('class')).toEqual('or');
1210 expect(e.children[0].firstChild.data).toEqual('or');
1211 expect(e.children[1].getAttribute('class')).toEqual('delete');
1212 expect(e.children[1].firstChild.data).toEqual('×');
1213
1214 op.and(true);
1215 op.del(false);
1216 op.update();
1217
1218 e = op.element();
Akron0b489ad2018-02-02 16:49:32 +01001219 expect(e.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001220 expect(e.children[0].getAttribute('class')).toEqual('and');
1221 expect(e.children[0].firstChild.data).toEqual('and');
1222 expect(e.children[1].getAttribute('class')).toEqual('or');
1223 expect(e.children[1].firstChild.data).toEqual('or');
1224 });
1225 });
1226
1227 describe('KorAP._delete (event)', function () {
1228 var complexVCFactory = buildFactory(vcClass,{
1229 "@type": 'koral:docGroup',
1230 'operation' : 'operation:and',
1231 'operands' : [
1232 {
1233 "@type": 'koral:doc',
1234 "key": 'pubDate',
1235 "match": 'match:eq',
1236 "value": '2014-12-05',
1237 "type": 'type:date'
1238 },
1239 {
1240 "@type" : 'koral:docGroup',
1241 'operation' : 'operation:or',
1242 'operands' : [
1243 {
1244 '@type' : 'koral:doc',
1245 'key' : 'title',
1246 'value' : 'Hello World!'
1247 },
1248 {
1249 '@type' : 'koral:doc',
1250 'key' : 'foo',
1251 'value' : 'bar'
1252 }
1253 ]
1254 }
1255 ]
1256 });
1257
1258 it('should clean on root docs', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001259 var vc = vcClass.create().fromJson({
Nils Diewald7c8ced22015-04-15 19:21:00 +00001260 "@type": 'koral:doc',
1261 "key": 'pubDate',
1262 "match": 'match:eq',
1263 "value": '2014-12-05',
1264 "type": 'type:date'
Nils Diewald86dad5b2015-01-28 15:09:07 +00001265 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00001266 expect(vc.root().toQuery()).toEqual('pubDate in 2014-12-05');
Akron0b489ad2018-02-02 16:49:32 +01001267 expect(vc.root().element().lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald86dad5b2015-01-28 15:09:07 +00001268
Nils Diewald7c8ced22015-04-15 19:21:00 +00001269 // Clean with delete from root
1270 expect(vc.root().element().lastChild.lastChild.getAttribute('class')).toEqual('delete');
1271 _delOn(vc.root());
1272 expect(vc.root().toQuery()).toEqual('');
1273 expect(vc.root().element().lastChild.lastChild.data).toEqual('⋯');
1274 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00001275
Nils Diewald7c8ced22015-04-15 19:21:00 +00001276 it('should remove on nested docs', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001277 var vc = vcClass.create().fromJson(
Nils Diewald7c8ced22015-04-15 19:21:00 +00001278 {
1279 "@type": 'koral:docGroup',
1280 'operation' : 'operation:and',
1281 'operands' : [
1282 {
1283 "@type": 'koral:doc',
1284 "key": 'pubDate',
1285 "match": 'match:eq',
1286 "value": '2014-12-05',
1287 "type": 'type:date'
1288 },
1289 {
1290 "@type" : 'koral:doc',
1291 'key' : 'foo',
1292 'value' : 'bar'
1293 }
1294 ]
1295 }
1296 );
1297
1298 // Delete with direct element access
1299 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1300 _delOn(vc.root().getOperand(0));
1301
1302 expect(vc.toQuery()).toEqual('foo = "bar"');
1303 expect(vc.root().ldType()).toEqual('doc');
1304 });
1305
1306 it('should clean on doc groups', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001307 var vc = vcClass.create().fromJson(
Nils Diewald7c8ced22015-04-15 19:21:00 +00001308 {
1309 "@type": 'koral:docGroup',
1310 'operation' : 'operation:and',
1311 'operands' : [
1312 {
1313 "@type": 'koral:doc',
1314 "key": 'pubDate',
1315 "match": 'match:eq',
1316 "value": '2014-12-05',
1317 "type": 'type:date'
1318 },
1319 {
1320 "@type" : 'koral:doc',
1321 'key' : 'foo',
1322 'value' : 'bar'
1323 }
1324 ]
1325 }
1326 );
1327
1328 // Cleanwith direct element access
1329 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1330 _delOn(vc.root());
1331 expect(vc.toQuery()).toEqual('');
1332 expect(vc.root().ldType()).toEqual('non');
1333 });
1334
1335 it('should remove on nested doc groups (case of ungrouping 1)', function () {
1336 var vc = complexVCFactory.create();
1337
1338 // Delete with direct element access
1339 expect(vc.toQuery()).toEqual(
1340 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1341 );
1342
1343 // Remove hello world:
1344 _delOn(vc.root().getOperand(1).getOperand(0));
1345 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1346 expect(vc.root().ldType()).toEqual('docGroup');
1347 });
1348
1349 it('should remove on nested doc groups (case of ungrouping 2)', function () {
1350 var vc = complexVCFactory.create();
1351
1352 // Delete with direct element access
1353 expect(vc.toQuery()).toEqual(
1354 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1355 );
1356
1357 // Remove bar
1358 _delOn(vc.root().getOperand(1).getOperand(1));
1359 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & title = "Hello World!"');
1360 expect(vc.root().ldType()).toEqual('docGroup');
1361 expect(vc.root().operation()).toEqual('and');
1362 });
1363
1364 it('should remove on nested doc groups (case of root changing)', function () {
1365 var vc = complexVCFactory.create();
1366
1367 // Delete with direct element access
1368 expect(vc.toQuery()).toEqual(
1369 'pubDate in 2014-12-05 & ' +
1370 '(title = "Hello World!" | foo = "bar")'
1371 );
1372
1373 // Remove bar
1374 _delOn(vc.root().getOperand(0));
1375 expect(vc.toQuery()).toEqual('title = "Hello World!" | foo = "bar"');
1376 expect(vc.root().ldType()).toEqual('docGroup');
1377 expect(vc.root().operation()).toEqual('or');
1378 });
1379
1380 it('should remove on nested doc groups (list flattening)', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001381 var vc = vcClass.create().fromJson(
Nils Diewald7c8ced22015-04-15 19:21:00 +00001382 {
1383 "@type": 'koral:docGroup',
1384 'operation' : 'operation:or',
1385 'operands' : [
1386 {
1387 "@type": 'koral:doc',
1388 "key": 'pubDate',
1389 "match": 'match:eq',
1390 "value": '2014-12-05',
1391 "type": 'type:date'
1392 },
1393 {
1394 "@type" : 'koral:doc',
1395 'key' : 'foo',
1396 'value' : 'bar'
1397 },
1398 {
1399 "@type": 'koral:docGroup',
1400 'operation' : 'operation:and',
1401 'operands' : [
1402 {
1403 "@type": 'koral:doc',
1404 "key": 'pubDate',
1405 "match": 'match:eq',
1406 "value": '2014-12-05',
1407 "type": 'type:date'
1408 },
1409 {
1410 "@type" : 'koral:docGroup',
1411 'operation' : 'operation:or',
1412 'operands' : [
1413 {
1414 '@type' : 'koral:doc',
1415 'key' : 'title',
1416 'value' : 'Hello World!'
1417 },
1418 {
1419 '@type' : 'koral:doc',
1420 'key' : 'yeah',
1421 'value' : 'juhu'
1422 }
1423 ]
1424 }
1425 ]
1426 }
1427 ]
1428 }
1429 );
1430
1431 // Delete with direct element access
1432 expect(vc.toQuery()).toEqual(
1433 'pubDate in 2014-12-05 | foo = "bar" | ' +
1434 '(pubDate in 2014-12-05 & ' +
1435 '(title = "Hello World!" | yeah = "juhu"))'
1436 );
1437
1438 expect(vc.root().ldType()).toEqual('docGroup');
1439 expect(vc.root().operation()).toEqual('or');
1440
1441 // Operands and operators
1442 expect(vc.element().firstChild.children.length).toEqual(4);
Akron0b489ad2018-02-02 16:49:32 +01001443 expect(vc.element().firstChild.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001444
1445 // Remove inner group and flatten
1446 _delOn(vc.root().getOperand(2).getOperand(0));
1447
1448 expect(vc.toQuery()).toEqual(
1449 'pubDate in 2014-12-05 | foo = "bar" | title = "Hello World!" | yeah = "juhu"'
1450 );
1451 expect(vc.root().ldType()).toEqual('docGroup');
1452 expect(vc.root().operation()).toEqual('or');
1453
1454 // Operands and operators
1455 expect(vc.element().firstChild.children.length).toEqual(5);
Akron0b489ad2018-02-02 16:49:32 +01001456 expect(vc.element().firstChild.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001457 });
1458 });
1459
1460 describe('KorAP._add (event)', function () {
1461 var complexVCFactory = buildFactory(vcClass,{
1462 "@type": 'koral:docGroup',
1463 'operation' : 'operation:and',
1464 'operands' : [
1465 {
1466 "@type": 'koral:doc',
1467 "key": 'pubDate',
1468 "match": 'match:eq',
1469 "value": '2014-12-05',
1470 "type": 'type:date'
1471 },
1472 {
1473 "@type" : 'koral:docGroup',
1474 'operation' : 'operation:or',
1475 'operands' : [
1476 {
1477 '@type' : 'koral:doc',
1478 'key' : 'title',
1479 'value' : 'Hello World!'
1480 },
1481 {
1482 '@type' : 'koral:doc',
1483 'key' : 'foo',
1484 'value' : 'bar'
1485 }
1486 ]
1487 }
1488 ]
1489 });
1490
1491 it('should add new unspecified doc with "and"', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001492 var vc = vcClass.create().fromJson(
Nils Diewald7c8ced22015-04-15 19:21:00 +00001493 {
1494 "@type": 'koral:docGroup',
1495 'operation' : 'operation:and',
1496 'operands' : [
1497 {
1498 "@type": 'koral:doc',
1499 "key": 'pubDate',
1500 "match": 'match:eq',
1501 "value": '2014-12-05',
1502 "type": 'type:date'
1503 },
1504 {
1505 "@type" : 'koral:doc',
1506 'key' : 'foo',
1507 'value' : 'bar'
1508 }
1509 ]
1510 }
1511 );
1512
1513 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1514
1515 var fc = vc.element().firstChild;
1516 expect(fc.getAttribute('data-operation')).toEqual('and');
1517 expect(fc.children.length).toEqual(3);
Akron0b489ad2018-02-02 16:49:32 +01001518 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001519 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1520 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1521
1522 // add with 'and' in the middle
1523 _andOn(vc.root().getOperand(0));
1524 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1525
1526 fc = vc.element().firstChild;
1527 expect(fc.getAttribute('data-operation')).toEqual('and');
1528 expect(fc.children.length).toEqual(4);
Akron0b489ad2018-02-02 16:49:32 +01001529 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001530
1531 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1532 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
1533 expect(fc.children[2].getAttribute('class')).toEqual('doc');
1534 });
1535
1536 it('should add new unspecified doc with "or"', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001537 var vc = vcClass.create().fromJson(
Nils Diewald7c8ced22015-04-15 19:21:00 +00001538 {
1539 "@type": 'koral:docGroup',
1540 'operation' : 'operation:and',
1541 'operands' : [
1542 {
1543 "@type": 'koral:doc',
1544 "key": 'pubDate',
1545 "match": 'match:eq',
1546 "value": '2014-12-05',
1547 "type": 'type:date'
1548 },
1549 {
1550 "@type" : 'koral:doc',
1551 'key' : 'foo',
1552 'value' : 'bar'
1553 }
1554 ]
1555 }
1556 );
1557
1558 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1559
1560 var fc = vc.element().firstChild;
1561 expect(fc.children.length).toEqual(3);
Akron0b489ad2018-02-02 16:49:32 +01001562 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001563 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1564 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1565
1566 // add with 'or' in the middle
1567 _orOn(vc.root().getOperand(0));
1568 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1569 fc = vc.element().firstChild;
1570
1571 expect(fc.getAttribute('data-operation')).toEqual('and');
1572 expect(fc.children.length).toEqual(3);
1573 expect(fc.children[0].getAttribute('class')).toEqual('docGroup');
1574 expect(fc.children[0].getAttribute('data-operation')).toEqual('or');
1575 expect(fc.children[1].getAttribute('class')).toEqual('doc');
Akron0b489ad2018-02-02 16:49:32 +01001576 expect(fc.children[2].getAttribute('class')).toEqual('operators button-group');
1577 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001578
1579 fc = vc.element().firstChild.firstChild;
1580 expect(fc.children.length).toEqual(3);
1581 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1582 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
Akron0b489ad2018-02-02 16:49:32 +01001583 expect(fc.children[2].getAttribute('class')).toEqual('operators button-group');
1584 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001585 });
1586
1587 it('should add new unspecified doc with "and" before group', function () {
1588 var vc = demoFactory.create();
1589
1590 // Wrap with direct element access
1591 expect(vc.toQuery()).toEqual(
1592 '(Titel = "Baum" & ' +
1593 'Veröffentlichungsort = "hihi" & ' +
1594 '(Titel = "Baum" | ' +
1595 'Veröffentlichungsort = "hihi")) | ' +
1596 'Untertitel = "huhu"'
1597 );
1598
1599 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
1600 expect(vc.root().getOperand(0).operation()).toEqual('and');
1601 expect(vc.root().getOperand(0).operands().length).toEqual(3);
1602
1603 // Add unspecified on the second doc
1604 var secDoc = vc.root().getOperand(0).getOperand(1);
1605 expect(secDoc.value()).toEqual('hihi');
1606
1607 // Add
1608 _andOn(secDoc);
1609
1610 var fo = vc.root().getOperand(0);
1611
1612 expect(fo.ldType()).toEqual('docGroup');
1613 expect(fo.operation()).toEqual('and');
1614 expect(fo.operands().length).toEqual(4);
1615
1616 expect(fo.getOperand(0).ldType()).toEqual('doc');
1617 expect(fo.getOperand(1).ldType()).toEqual('doc');
1618 expect(fo.getOperand(2).ldType()).toEqual('non');
1619 expect(fo.getOperand(3).ldType()).toEqual('docGroup');
1620 });
1621
1622
1623 it('should remove a doc with an unspecified doc in a nested group', function () {
1624 var vc = demoFactory.create();
1625
1626 // Wrap with direct element access
1627 expect(vc.toQuery()).toEqual(
1628 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & (Titel = "Baum" | Veröffentlichungsort = "hihi")) | Untertitel = "huhu"'
1629 );
1630
1631 var fo = vc.root().getOperand(0).getOperand(0);
1632 expect(fo.key()).toEqual('Titel');
1633 expect(fo.value()).toEqual('Baum');
1634
1635 // Add unspecified on the root group
1636 _orOn(fo);
1637
1638 fo = vc.root().getOperand(0).getOperand(0);
1639
1640 expect(fo.operation()).toEqual('or');
1641 expect(fo.getOperand(0).ldType()).toEqual('doc');
1642 expect(fo.getOperand(1).ldType()).toEqual('non');
1643
1644 // Delete document
1645 _delOn(fo.getOperand(0));
1646
1647 // The operand is now non
1648 expect(vc.root().getOperand(0).getOperand(0).ldType()).toEqual('non');
1649 expect(vc.root().getOperand(0).getOperand(1).ldType()).toEqual('doc');
1650 expect(vc.root().getOperand(0).getOperand(2).ldType()).toEqual('docGroup');
1651 });
1652
1653
1654 it('should remove an unspecified doc with an doc in a nested group', function () {
1655 var vc = demoFactory.create();
1656
1657 // Wrap with direct element access
1658 expect(vc.toQuery()).toEqual(
1659 '(Titel = "Baum" & ' +
1660 'Veröffentlichungsort = "hihi" & ' +
1661 '(Titel = "Baum" ' +
1662 '| Veröffentlichungsort = "hihi")) | ' +
1663 'Untertitel = "huhu"'
1664 );
1665
1666 var fo = vc.root().getOperand(0).getOperand(0);
1667 expect(fo.key()).toEqual('Titel');
1668 expect(fo.value()).toEqual('Baum');
1669
1670 // Add unspecified on the root group
1671 _orOn(fo);
1672
1673 fo = vc.root().getOperand(0).getOperand(0);
1674
1675 expect(fo.operation()).toEqual('or');
1676 expect(fo.getOperand(0).ldType()).toEqual('doc');
1677 expect(fo.getOperand(1).ldType()).toEqual('non');
1678
1679 // Delete unspecified doc
1680 _delOn(fo.getOperand(1));
1681
1682 // The operand is now non
1683 fo = vc.root().getOperand(0);
1684 expect(fo.getOperand(0).ldType()).toEqual('doc');
1685 expect(fo.getOperand(0).key()).toEqual('Titel');
1686 expect(fo.getOperand(0).value()).toEqual('Baum');
1687 expect(fo.getOperand(1).ldType()).toEqual('doc');
1688 expect(fo.getOperand(2).ldType()).toEqual('docGroup');
1689 });
1690
1691
1692 it('should add on parent group (case "and")', function () {
1693 var vc = complexVCFactory.create();
1694
1695 // Wrap with direct element access
1696 expect(vc.toQuery()).toEqual(
1697 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1698 );
1699
1700 expect(vc.root().operands().length).toEqual(2);
1701
1702 // Add unspecified on the root group
1703 _andOn(vc.root().getOperand(1));
1704 expect(vc.toQuery()).toEqual(
1705 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1706 );
1707
1708 expect(vc.root().ldType()).toEqual('docGroup');
1709 expect(vc.root().operands().length).toEqual(3);
1710 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
1711 expect(vc.root().getOperand(1).ldType()).toEqual('docGroup');
1712 expect(vc.root().getOperand(1).operation()).toEqual('or');
1713 expect(vc.root().getOperand(2).ldType()).toEqual('non');
1714
1715 // Add another unspecified on the root group
1716 _andOn(vc.root().getOperand(1));
1717
1718 expect(vc.root().operands().length).toEqual(4);
1719 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
1720 expect(vc.root().getOperand(1).ldType()).toEqual('docGroup');
1721 expect(vc.root().getOperand(2).ldType()).toEqual('non');
1722 expect(vc.root().getOperand(3).ldType()).toEqual('non');
1723
1724 // Add another unspecified after the first doc
1725 _andOn(vc.root().getOperand(0));
1726
1727 expect(vc.root().operands().length).toEqual(5);
1728 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
1729 expect(vc.root().getOperand(1).ldType()).toEqual('non');
1730 expect(vc.root().getOperand(2).ldType()).toEqual('docGroup');
1731 expect(vc.root().getOperand(3).ldType()).toEqual('non');
1732 expect(vc.root().getOperand(4).ldType()).toEqual('non');
1733 });
1734
1735 it('should wrap on root', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001736 var vc = vcClass.create().fromJson(
Nils Diewald7c8ced22015-04-15 19:21:00 +00001737 {
1738 "@type": 'koral:docGroup',
1739 'operation' : 'operation:and',
1740 'operands' : [
1741 {
1742 "@type": 'koral:doc',
1743 "key": 'pubDate',
1744 "match": 'match:eq',
1745 "value": '2014-12-05',
1746 "type": 'type:date'
1747 },
1748 {
1749 "@type" : 'koral:doc',
1750 'key' : 'foo',
1751 'value' : 'bar'
1752 }
1753 ]
1754 }
1755 );
1756
1757 // Wrap on root
1758 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1759 expect(vc.root().ldType()).toEqual('docGroup');
1760 expect(vc.root().operation()).toEqual('and');
1761 _orOn(vc.root());
1762 expect(vc.root().ldType()).toEqual('docGroup');
1763 expect(vc.root().operation()).toEqual('or');
1764
1765 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
1766 expect(vc.root().getOperand(0).operation()).toEqual('and');
1767 });
1768
1769 it('should add on root (case "and")', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001770 var vc = vcClass.create().fromJson(
Nils Diewald7c8ced22015-04-15 19:21:00 +00001771 {
1772 "@type": 'koral:doc',
1773 "key": 'pubDate',
1774 "match": 'match:eq',
1775 "value": '2014-12-05',
1776 "type": 'type:date'
1777 }
1778 );
1779
1780 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
1781 expect(vc.root().ldType()).toEqual('doc');
1782 expect(vc.root().key()).toEqual('pubDate');
1783 expect(vc.root().value()).toEqual('2014-12-05');
1784
1785 // Wrap on root
1786 _andOn(vc.root());
1787 expect(vc.root().ldType()).toEqual('docGroup');
1788 expect(vc.root().operation()).toEqual('and');
1789 });
1790
1791 it('should add on root (case "or")', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001792 var vc = vcClass.create().fromJson(
Nils Diewald7c8ced22015-04-15 19:21:00 +00001793 {
1794 "@type": 'koral:doc',
1795 "key": 'pubDate',
1796 "match": 'match:eq',
1797 "value": '2014-12-05',
1798 "type": 'type:date'
1799 }
1800 );
1801
1802 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
1803 expect(vc.root().key()).toEqual('pubDate');
1804 expect(vc.root().value()).toEqual('2014-12-05');
1805
1806 // Wrap on root
1807 _orOn(vc.root());
1808 expect(vc.root().ldType()).toEqual('docGroup');
1809 expect(vc.root().operation()).toEqual('or');
1810 });
1811
1812 it('should support multiple sub groups per group', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001813 var vc = vcClass.create().fromJson(
Nils Diewald7c8ced22015-04-15 19:21:00 +00001814 {
1815 "@type": 'koral:docGroup',
1816 'operation' : 'operation:or',
1817 'operands' : [
1818 {
1819 "@type": 'koral:docGroup',
1820 'operation' : 'operation:and',
1821 'operands' : [
1822 {
1823 "@type": 'koral:doc',
1824 "key": 'title',
1825 "value": 't1',
1826 },
1827 {
1828 "@type" : 'koral:doc',
1829 'key' : 'title',
1830 'value' : 't2'
1831 }
1832 ]
1833 },
1834 {
1835 "@type": 'koral:docGroup',
1836 'operation' : 'operation:and',
1837 'operands' : [
1838 {
1839 "@type": 'koral:doc',
1840 "key": 'title',
1841 "value": 't3',
1842 },
1843 {
1844 "@type" : 'koral:doc',
1845 'key' : 'title',
1846 'value' : 't4'
1847 }
1848 ]
1849 }
1850 ]
1851 }
1852 );
1853 expect(vc.toQuery()).toEqual(
1854 '(title = "t1" & title = "t2") | ' +
1855 '(title = "t3" & title = "t4")'
1856 );
1857 expect(vc.root().operation()).toEqual('or');
1858 expect(vc.root().getOperand(0).toQuery()).toEqual('title = "t1" & title = "t2"');
1859 expect(vc.root().getOperand(1).toQuery()).toEqual('title = "t3" & title = "t4"');
1860
1861 _andOn(vc.root());
1862
1863 expect(vc.root().operation()).toEqual('and');
1864 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
1865 expect(vc.root().getOperand(1).ldType()).toEqual('non');
1866 });
1867 });
1868
Akron5c829e92017-05-12 18:10:00 +02001869 // Check class method
1870 describe('KorAP.VC.checkRewrite', function () {
1871
1872 it('should check for simple rewrites', function () {
1873 expect(vcClass.checkRewrite(
1874 {
1875 "@type" : "koral:doc",
1876 "rewrites" : [{
1877 "@type" : "koral:rewrite",
1878 "operation" : "operation:modification",
1879 "src" : "querySerializer",
1880 "scope" : "tree"
1881 }]
1882 }
1883 )).toBeTruthy();
Nils Diewald7c8ced22015-04-15 19:21:00 +00001884
Akron5c829e92017-05-12 18:10:00 +02001885 var nested = {
1886 "@type" : "koral:docGroup",
1887 "operands" : [
1888 {
1889 "@type" : "koral:doc"
1890 },
1891 {
1892 "@type" : "koral:docGroup",
1893 "operands" : [
1894 {
1895 "@type": "koral:doc"
1896 },
1897 {
1898 "@type": "koral:doc"
1899 }
1900 ]
1901 }
1902 ]
1903 };
1904
1905 expect(vcClass.checkRewrite(nested)).toBe(false);
1906
1907 nested["operands"][1]["operands"][1]["rewrites"] = [{
1908 "@type" : "koral:rewrite",
1909 "operation" : "operation:modification",
1910 "src" : "querySerializer",
1911 "scope" : "tree"
1912 }];
1913
1914 expect(vcClass.checkRewrite(nested)).toBeTruthy();
1915 });
1916 });
Nils Diewald6283d692015-04-23 20:32:53 +00001917
1918 describe('KorAP.Rewrite', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +00001919 it('should be initializable', function () {
1920 var rewrite = rewriteClass.create({
1921 "@type" : "koral:rewrite",
1922 "operation" : "operation:modification",
1923 "src" : "querySerializer",
1924 "scope" : "tree"
Nils Diewald86dad5b2015-01-28 15:09:07 +00001925 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00001926 expect(rewrite.toString()).toEqual('Modification of "tree" by "querySerializer"');
1927 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00001928
Nils Diewald7c8ced22015-04-15 19:21:00 +00001929 it('should be deserialized by docs', function () {
1930 var doc = docClass.create(
1931 undefined,
1932 {
1933 "@type":"koral:doc",
1934 "key":"Titel",
1935 "value":"Baum",
1936 "match":"match:eq"
1937 });
1938
1939 expect(doc.element().classList.contains('doc')).toBeTruthy();
1940 expect(doc.element().classList.contains('rewritten')).toBe(false);
1941
1942 doc = docClass.create(
1943 undefined,
1944 {
1945 "@type":"koral:doc",
1946 "key":"Titel",
1947 "value":"Baum",
1948 "match":"match:eq",
1949 "rewrites" : [
1950 {
1951 "@type" : "koral:rewrite",
1952 "operation" : "operation:modification",
1953 "src" : "querySerializer",
1954 "scope" : "tree"
1955 }
1956 ]
1957 });
1958
1959 expect(doc.element().classList.contains('doc')).toBeTruthy();
1960 expect(doc.element().classList.contains('rewritten')).toBeTruthy();
1961 });
Nils Diewald6283d692015-04-23 20:32:53 +00001962
1963 xit('should be deserialized by docGroups', function () {
1964 var docGroup = docGroupClass.create(
1965 undefined,
1966 {
1967 "@type" : "koral:docGroup",
1968 "operation" : "operation:or",
1969 "operands" : [
1970 {
1971 "@type" : "doc",
1972 "key" : "pubDate",
1973 "type" : "type:date",
1974 "value" : "2014-12-05"
1975 },
1976 {
1977 "@type" : "doc",
1978 "key" : "pubDate",
1979 "type" : "type:date",
1980 "value" : "2014-12-06"
1981 }
1982 ],
1983 "rewrites" : [
1984 {
1985 "@type" : "koral:rewrite",
1986 "operation" : "operation:modification",
1987 "src" : "querySerializer",
1988 "scope" : "tree"
1989 }
1990 ]
1991 }
1992 );
1993
1994 expect(doc.element().classList.contains('docgroup')).toBeTruthy();
1995 expect(doc.element().classList.contains('rewritten')).toBe(false);
1996 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00001997 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00001998 /*
1999 describe('KorAP.DocKey', function () {
2000 it('should be initializable', function () {
Nils Diewald2fe12e12015-03-06 16:47:06 +00002001 var docKey = KorAP.DocKey.create();
2002 expect(docKey.toString()).toEqual('...');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002003 });
2004 });
Nils Diewaldf0c4f112015-05-05 12:56:59 +00002005
2006 expect(
2007 function() { menuItemClass.create([]) }
2008 ).toThrow(new Error("Missing parameters"));
2009
2010
Nils Diewald7c8ced22015-04-15 19:21:00 +00002011 */
Nils Diewaldf0c4f112015-05-05 12:56:59 +00002012
2013 describe('KorAP.stringValue', function () {
2014 it('should be initializable', function () {
2015 var sv = stringValClass.create();
2016 expect(sv.regex()).toBe(false);
2017 expect(sv.value()).toBe('');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002018
2019 sv = stringValClass.create('Baum');
2020 expect(sv.regex()).toBe(false);
2021 expect(sv.value()).toBe('Baum');
2022
2023 sv = stringValClass.create('Baum', false);
2024 expect(sv.regex()).toBe(false);
2025 expect(sv.value()).toBe('Baum');
2026
2027 sv = stringValClass.create('Baum', true);
2028 expect(sv.regex()).toBe(true);
2029 expect(sv.value()).toBe('Baum');
2030 });
2031
2032 it('should be modifiable', function () {
2033 var sv = stringValClass.create();
2034 expect(sv.regex()).toBe(false);
2035 expect(sv.value()).toBe('');
2036
2037 expect(sv.value('Baum')).toBe('Baum');
2038 expect(sv.value()).toBe('Baum');
2039
2040 expect(sv.regex(true)).toBe(true);
2041 expect(sv.regex()).toBe(true);
2042 });
2043
2044 it('should have a toggleble regex value', function () {
2045 var sv = stringValClass.create();
Nils Diewald7991a3f2015-05-19 14:12:37 +00002046
2047 expect(sv.element().firstChild.value).toBe('');
2048 sv.element().firstChild.value = 'der'
2049 expect(sv.element().firstChild.value).toBe('der');
2050
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002051 expect(sv.regex()).toBe(false);
2052
2053 sv.toggleRegex();
Nils Diewald7991a3f2015-05-19 14:12:37 +00002054 expect(sv.element().firstChild.value).toBe('der');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002055 expect(sv.regex()).toBe(true);
Nils Diewald7991a3f2015-05-19 14:12:37 +00002056 sv.element().firstChild.value = 'derbe'
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002057
2058 sv.toggleRegex();
2059 expect(sv.regex()).toBe(false);
Nils Diewald7991a3f2015-05-19 14:12:37 +00002060 expect(sv.element().firstChild.value).toBe('derbe');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002061 });
2062
2063 it('should have an element', function () {
2064 var sv = stringValClass.create('der');
2065 expect(sv.element().nodeName).toBe('DIV');
2066 expect(sv.element().firstChild.nodeName).toBe('INPUT');
2067 expect(sv.element().firstChild.value).toBe('der');
2068 });
2069
2070 it('should have a classed element', function () {
2071 var sv = stringValClass.create();
2072 console.log(sv.element());
2073 expect(sv.element().classList.contains('regex')).toBe(false);
2074 expect(sv.regex()).toBe(false);
2075 sv.toggleRegex();
2076 expect(sv.element().classList.contains('regex')).toBe(true);
2077 });
2078
2079 it('should be storable', function () {
2080 var sv = stringValClass.create();
2081 var count = 1;
2082 sv.store = function (value, regex) {
Akrone4961b12017-05-10 21:04:46 +02002083 expect(regex).toBe(true);
2084 expect(value).toBe('tree');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002085 };
2086 sv.regex(true);
2087 sv.value('tree');
2088 sv.element().lastChild.click();
Nils Diewaldf0c4f112015-05-05 12:56:59 +00002089 });
2090 });
Akrone4961b12017-05-10 21:04:46 +02002091
2092 // Check prefix
2093 describe('KorAP.VC.Prefix', function () {
2094
2095 it('should be initializable', function () {
2096 var p = prefixClass.create();
2097 expect(p.element().classList.contains('pref')).toBeTruthy();
2098 expect(p.isSet()).not.toBeTruthy();
2099 });
2100
2101
2102 it('should be clickable', function () {
2103 var vc = vcClass.create([
2104 ['a', null],
2105 ['b', null],
2106 ['c', null]
2107 ]).fromJson();
2108 expect(vc.element().firstChild.classList.contains('unspecified')).toBeTruthy();
2109
2110 // This should open up the menu
2111 vc.element().firstChild.firstChild.click();
2112 expect(vc.element().firstChild.firstChild.tagName).toEqual('UL');
2113
2114 KorAP._vcKeyMenu._prefix.clear();
2115 KorAP._vcKeyMenu._prefix.add('x');
2116
2117 var prefElement = vc.element().querySelector('span.pref');
2118 expect(prefElement.innerText).toEqual('x');
2119
2120 // This should add key 'x' to VC
2121 prefElement.click();
2122
2123 expect(vc.element().firstChild.classList.contains('doc')).toBeTruthy();
2124 expect(vc.element().firstChild.firstChild.className).toEqual('key');
2125 expect(vc.element().firstChild.firstChild.innerText).toEqual('x');
2126 });
2127 });
Nils Diewald2fe12e12015-03-06 16:47:06 +00002128});