blob: c08307dbc252f90fc61b6f5eb9ee6427b320d233 [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) {
Akron712733a2018-04-05 18:17:47 +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) {
Akrond141a362018-07-10 18:12:13 +020033 KorAP._and.apply(obj);
Nils Diewald7c8ced22015-04-15 19:21:00 +000034 };
Nils Diewald52f7eb12015-01-12 17:30:04 +000035
Nils Diewald7c8ced22015-04-15 19:21:00 +000036 function _orOn (obj) {
Akrond141a362018-07-10 18:12:13 +020037 KorAP._or.apply(obj);
Nils Diewald7c8ced22015-04-15 19:21:00 +000038 };
Nils Diewald52f7eb12015-01-12 17:30:04 +000039
Nils Diewald7c8ced22015-04-15 19:21:00 +000040 function _delOn (obj) {
Akrond141a362018-07-10 18:12:13 +020041 KorAP._delete.apply(obj);
Nils Diewald7c8ced22015-04-15 19:21:00 +000042 };
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 {
Akron712733a2018-04-05 18:17:47 +020049 "@type":"koral:docGroup",
50 "operation":"operation:and",
51 "operands":[
Nils Diewald7c8ced22015-04-15 19:21:00 +000052 {
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 {
Akron712733a2018-04-05 18:17:47 +020069 "@type":"koral:doc",
70 "key":"Titel",
71 "value":"Baum",
72 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +000073 },
74 {
Akron712733a2018-04-05 18:17:47 +020075 "@type":"koral:doc",
76 "key":"Veröffentlichungsort",
77 "value":"hihi",
78 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +000079 }
80 ]
81 }
Akron712733a2018-04-05 18:17:47 +020082 ]
Nils Diewaldf219eb82015-01-07 20:15:42 +000083 },
84 {
Akron712733a2018-04-05 18:17:47 +020085 "@type":"koral:doc",
86 "key":"Untertitel",
87 "value":"huhu",
88 "match":"match:contains"
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",
Akron31d89942018-04-06 16:44:51 +020098 "type" : "type:string",
Nils Diewald7c8ced22015-04-15 19:21:00 +000099 "@type" : "koral:doc"
100 });
101
102 // Create example factories
Akron712733a2018-04-05 18:17:47 +0200103 var textFactory = buildFactory(docClass, {
104 "key" : "author",
105 "value" : "Birkendale",
Akron31d89942018-04-06 16:44:51 +0200106 "type" : "type:string",
Akron712733a2018-04-05 18:17:47 +0200107 "match" : "match:contains",
108 "@type" : "koral:doc"
109 });
110
111 // Create example factories
Nils Diewald7c8ced22015-04-15 19:21:00 +0000112 var dateFactory = buildFactory(docClass, {
113 "key" : "pubDate",
114 "type" : "type:date",
115 "match" : "match:eq",
116 "value" : "2014-11-05",
117 "@type" : "koral:doc"
118 });
119
120 // Create example factories
121 var regexFactory = buildFactory(docClass, {
122 "key" : "title",
123 "type" : "type:regex",
124 "value" : "[^b]ee.+?",
125 "@type" : "koral:doc"
126 });
127
128 it('should be initializable', function () {
129 var doc = docClass.create();
130 expect(doc.matchop()).toEqual('eq');
131 expect(doc.key()).toBeUndefined();
132 expect(doc.value()).toBeUndefined();
133 expect(doc.type()).toEqual("string");
134 });
135
136 it('should be definable', function () {
137
138 // Empty doc
139 var doc = docClass.create();
140
141 // Set values
142 doc.key("title");
Akron8db5e3a2018-05-28 19:25:26 +0200143
Nils Diewald7c8ced22015-04-15 19:21:00 +0000144 doc.value("Der alte Mann");
145 expect(doc.matchop()).toEqual('eq');
146 expect(doc.key()).toEqual("title");
147 expect(doc.type()).toEqual("string");
148 expect(doc.value()).toEqual("Der alte Mann");
149 });
150
151
152 it('should deserialize JSON-LD string', function () {
153 var doc;
154
155 // String default
156 doc = stringFactory.create();
157 expect(doc.matchop()).toEqual('eq');
158 expect(doc.key()).toEqual("author");
159 expect(doc.type()).toEqual("string");
160 expect(doc.value()).toEqual("Max Birkendale");
161
162 // No valid string
163 doc = stringFactory.create({
Akron712733a2018-04-05 18:17:47 +0200164 value : undefined
Nils Diewald7c8ced22015-04-15 19:21:00 +0000165 });
166 expect(doc).toBeUndefined();
167
168 // No valid string
169 doc = stringFactory.create({
Akron712733a2018-04-05 18:17:47 +0200170 value : { "foo" : "bar" }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000171 });
172 expect(doc).toBeUndefined();
173
174 // Change match type
175 doc = stringFactory.create({
Akron712733a2018-04-05 18:17:47 +0200176 "match" : "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000177 });
178
179 expect(doc.matchop()).toEqual('ne');
180 expect(doc.key()).toEqual("author");
181 expect(doc.type()).toEqual("string");
182 expect(doc.value()).toEqual("Max Birkendale");
183
184 // Invalid match type
185 doc = stringFactory.create({
Akron712733a2018-04-05 18:17:47 +0200186 "match" : { "foo" : "bar" }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000187 });
188 expect(doc).toBeUndefined();
189 });
190
191 it('should deserialize JSON-LD regex', function () {
192 var doc = regexFactory.create();
193 expect(doc.key()).toEqual("title");
194 expect(doc.type()).toEqual("regex");
195 expect(doc.value()).toEqual("[^b]ee.+?");
196 expect(doc.matchop()).toEqual('eq');
197
198 // change matcher
199 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200200 match : "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000201 });
202 expect(doc.matchop()).toEqual('ne');
Akronea4e9052017-07-06 16:12:05 +0200203 expect(doc.rewrites()).toBeUndefined();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000204
205 // Invalid matcher
206 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200207 match : "match:chook"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000208 });
Akronea4e9052017-07-06 16:12:05 +0200209 expect(doc.matchop()).toEqual('eq');
210 expect(doc.rewrites()).toBeDefined();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000211
212 // Invalid regex
213 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200214 value : "[^b"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000215 });
216 expect(doc).toBeUndefined();
217 });
218
219 it('should deserialize JSON-LD date', function () {
220
221 // Normal date
222 doc = dateFactory.create({});
223
224 expect(doc.matchop()).toEqual('eq');
225 expect(doc.key()).toEqual("pubDate");
226 expect(doc.type()).toEqual("date");
227 expect(doc.value()).toEqual("2014-11-05");
228
229 // Short date 1
230 doc = dateFactory.create({
Akron712733a2018-04-05 18:17:47 +0200231 "value" : "2014-11"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000232 });
233
234 expect(doc.matchop()).toEqual('eq');
235 expect(doc.key()).toEqual("pubDate");
236 expect(doc.type()).toEqual("date");
237 expect(doc.value()).toEqual("2014-11");
238
239 // Short date 2
240 doc = dateFactory.create({
Akron712733a2018-04-05 18:17:47 +0200241 "value" : "2014"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000242 });
243
244 expect(doc.matchop()).toEqual('eq');
245 expect(doc.key()).toEqual("pubDate");
246 expect(doc.type()).toEqual("date");
247 expect(doc.value()).toEqual("2014");
248
249 // Invalid date!
250 doc = dateFactory.create({
Akron712733a2018-04-05 18:17:47 +0200251 "value" : "2014-11-050"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000252 });
253 expect(doc).toBeUndefined();
254
255 // Invalid matcher!
256 doc = dateFactory.create({
Akron31d89942018-04-06 16:44:51 +0200257 "match" : "match:contains",
Nils Diewald7c8ced22015-04-15 19:21:00 +0000258 });
Akronea4e9052017-07-06 16:12:05 +0200259 expect(doc).toBeDefined();
260 expect(doc.rewrites()).toBeDefined();
261 expect(doc.matchop()).toEqual('eq');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000262 });
263
264 it('should be serializale to JSON', function () {
265
266 // Empty doc
267 var doc = docClass.create();
268 expect(doc.toJson()).toEqual(jasmine.any(Object));
269
270 // Serialize string
271 doc = stringFactory.create();
272 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200273 "@type" : "koral:doc",
274 "type" : "type:string",
275 "key" : "author",
276 "value" : "Max Birkendale",
277 "match" : "match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000278 }));
279
280 // Serialize regex
281 doc = regexFactory.create();
282 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200283 "@type" : "koral:doc",
284 "type" : "type:regex",
285 "value" : "[^b]ee.+?",
286 "match" : "match:eq",
287 "key" : 'title'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000288 }));
289
290 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200291 match: "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000292 });
293 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200294 "@type" : "koral:doc",
295 "type" : "type:regex",
296 "value" : "[^b]ee.+?",
297 "match" : "match:ne",
298 "key" : 'title'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000299 }));
300
301 doc = dateFactory.create();
302 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200303 "@type" : "koral:doc",
304 "type" : "type:date",
305 "value" : "2014-11-05",
306 "match" : "match:eq",
307 "key" : 'pubDate'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000308 }));
309
310 doc = dateFactory.create({
Akron712733a2018-04-05 18:17:47 +0200311 value : "2014"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000312 });
313 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200314 "@type" : "koral:doc",
315 "type" : "type:date",
316 "value" : "2014",
317 "match" : "match:eq",
318 "key" : 'pubDate'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000319 }));
320 });
321
322
323 it('should be serializale to String', function () {
324 // Empty doc
325 var doc = docClass.create();
326 expect(doc.toQuery()).toEqual("");
327
328 // Serialize string
329 doc = stringFactory.create();
330 expect(doc.toQuery()).toEqual('author = "Max Birkendale"');
331
332 // Serialize string with quotes
333 doc = stringFactory.create({ "value" : 'Max "Der Coole" Birkendate'});
334 expect(doc.toQuery()).toEqual('author = "Max \\"Der Coole\\" Birkendate"');
335
336 // Serialize regex
337 doc = regexFactory.create();
338 expect(doc.toQuery()).toEqual('title = /[^b]ee.+?/');
339
340 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200341 match: "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000342 });
343 expect(doc.toQuery()).toEqual('title != /[^b]ee.+?/');
344
Akron8778f5d2017-06-30 21:25:55 +0200345 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200346 value: "WPD/AAA/00001"
Akron8778f5d2017-06-30 21:25:55 +0200347 });
348 expect(doc.toQuery()).toEqual('title = /WPD\\/AAA\\/00001/');
349
Nils Diewald7c8ced22015-04-15 19:21:00 +0000350 doc = dateFactory.create();
351 expect(doc.toQuery()).toEqual('pubDate in 2014-11-05');
352
353 doc = dateFactory.create({
Akron712733a2018-04-05 18:17:47 +0200354 value : "2014"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000355 });
356 expect(doc.toQuery()).toEqual('pubDate in 2014');
357 });
358 });
359
360
361 describe('KorAP.DocGroup', function () {
362 // Create example factories
363 var docFactory = buildFactory(
364 docClass,
Nils Diewaldf219eb82015-01-07 20:15:42 +0000365 {
Akron712733a2018-04-05 18:17:47 +0200366 "@type" : "koral:doc",
367 "match":"match:eq",
368 "key" : "author",
369 "value" : "Max Birkendale"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000370 }
371 );
372
373 var docGroupFactory = buildFactory(
374 docGroupClass, {
Akron712733a2018-04-05 18:17:47 +0200375 "@type" : "koral:docGroup",
376 "operation" : "operation:and",
377 "operands" : [
378 docFactory.create().toJson(),
379 docFactory.create({
380 "key" : "pubDate",
381 "type" : "type:date",
382 "value" : "2014-12-05"
383 }).toJson()
384 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000385 });
386
387 it('should be initializable', function () {
388 // Create empty group
389 var docGroup = docGroupClass.create();
390 expect(docGroup.operation()).toEqual('and');
391
392 // Create empty group
393 docGroup = docGroupClass.create();
394 docGroup.operation('or');
395 expect(docGroup.operation()).toEqual('or');
396 });
397
398 it('should be definable', function () {
399
400 // Empty group
401 var docGroup = docGroupClass.create();
402 expect(docGroup.operation()).toEqual('and');
403
404 // Set values
405 docGroup.operation("or");
406 expect(docGroup.operation()).toEqual('or');
407
408 // Set invalid values
409 docGroup.operation("hui");
410 expect(docGroup.operation()).toEqual('or');
411 });
412
413 it('should be deserializable', function () {
414 var docGroup = docGroupFactory.create();
415 expect(docGroup.operation()).toEqual("and");
416 expect(docGroup.operands().length).toEqual(2);
417
418 var op1 = docGroup.getOperand(0);
419 expect(op1.type()).toEqual("string");
420 expect(op1.key()).toEqual("author");
421 expect(op1.value()).toEqual("Max Birkendale");
422 expect(op1.matchop()).toEqual("eq");
423
424 var op2 = docGroup.getOperand(1);
425 expect(op2.type()).toEqual("date");
426 expect(op2.key()).toEqual("pubDate");
427 expect(op2.value()).toEqual("2014-12-05");
428 expect(op2.matchop()).toEqual("eq");
429
430 // Append empty group
431 var newGroup = docGroup.append(docGroupClass.create());
432 newGroup.operation('or');
433 newGroup.append(docFactory.create());
434 newGroup.append(docFactory.create({
Akron712733a2018-04-05 18:17:47 +0200435 "type" : "type:regex",
436 "key" : "title",
437 "value" : "^e.+?$",
438 "match" : "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000439 }));
440
441 expect(docGroup.operation()).toEqual("and");
442 expect(docGroup.operands().length).toEqual(3);
443
444 var op1 = docGroup.getOperand(0);
445 expect(op1.ldType()).toEqual("doc");
446 expect(op1.type()).toEqual("string");
447 expect(op1.key()).toEqual("author");
448 expect(op1.value()).toEqual("Max Birkendale");
449 expect(op1.matchop()).toEqual("eq");
450
451 var op2 = docGroup.getOperand(1);
452 expect(op2.ldType()).toEqual("doc");
453 expect(op2.type()).toEqual("date");
454 expect(op2.key()).toEqual("pubDate");
455 expect(op2.value()).toEqual("2014-12-05");
456 expect(op2.matchop()).toEqual("eq");
457
458 var op3 = docGroup.getOperand(2);
459 expect(op3.ldType()).toEqual("docGroup");
460 expect(op3.operation()).toEqual("or");
461
462 var op4 = op3.getOperand(0);
463 expect(op4.ldType()).toEqual("doc");
464 expect(op4.type()).toEqual("string");
465 expect(op4.key()).toEqual("author");
466 expect(op4.value()).toEqual("Max Birkendale");
467 expect(op4.matchop()).toEqual("eq");
468
469 var op5 = op3.getOperand(1);
470 expect(op5.ldType()).toEqual("doc");
471 expect(op5.type()).toEqual("regex");
472 expect(op5.key()).toEqual("title");
473 expect(op5.value()).toEqual("^e.+?$");
474 expect(op5.matchop()).toEqual("ne");
475 });
476
477 it('should be serializable to JSON', function () {
478 var docGroup = docGroupFactory.create();
479
480 expect(docGroup.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200481 "@type" : "koral:docGroup",
482 "operation" : "operation:and",
483 "operands" : [
484 {
485 "@type": 'koral:doc',
486 "key" : 'author',
487 "match": 'match:eq',
488 "value": 'Max Birkendale',
489 "type": 'type:string'
490 },
491 {
492 "@type": 'koral:doc',
493 "key": 'pubDate',
494 "match": 'match:eq',
495 "value": '2014-12-05',
496 "type": 'type:date'
497 }
498 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000499 }));
500 });
Nils Diewalde15b7a22015-01-09 21:50:21 +0000501
Nils Diewald7c8ced22015-04-15 19:21:00 +0000502 it('should be serializable to String', function () {
503 var docGroup = docGroupFactory.create();
504 expect(docGroup.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +0200505 'author = "Max Birkendale" & pubDate in 2014-12-05'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000506 );
Nils Diewald52f7eb12015-01-12 17:30:04 +0000507
Nils Diewald7c8ced22015-04-15 19:21:00 +0000508 docGroup = docGroupFactory.create({
Akron712733a2018-04-05 18:17:47 +0200509 "@type" : "koral:docGroup",
510 "operation" : "operation:or",
511 "operands" : [
512 {
513 "@type": 'koral:doc',
514 "key" : 'author',
515 "match": 'match:eq',
516 "value": 'Max Birkendale',
517 "type": 'type:string'
518 },
519 {
520 "@type" : "koral:docGroup",
521 "operation" : "operation:and",
522 "operands" : [
523 {
524 "@type": 'koral:doc',
525 "key": 'pubDate',
526 "match": 'match:geq',
527 "value": '2014-05-12',
528 "type": 'type:date'
529 },
530 {
531 "@type": 'koral:doc',
532 "key": 'pubDate',
533 "match": 'match:leq',
534 "value": '2014-12-05',
535 "type": 'type:date'
536 },
537 {
538 "@type": 'koral:doc',
539 "key": 'foo',
540 "match": 'match:ne',
541 "value": '[a]?bar',
542 "type": 'type:regex'
543 }
544 ]
545 }
546 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000547 });
548 expect(docGroup.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +0200549 'author = "Max Birkendale" | ' +
550 '(pubDate since 2014-05-12 & ' +
551 'pubDate until 2014-12-05 & foo != /[a]?bar/)'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000552 );
553 });
554 });
555
556 describe('KorAP.UnspecifiedDoc', function () {
557 it('should be initializable', function () {
558 var doc = unspecifiedClass.create();
559 var docElement = doc.element();
560 expect(docElement.getAttribute('class')).toEqual('doc unspecified');
561 expect(docElement.firstChild.firstChild.data).toEqual('⋯');
562 expect(docElement.lastChild.lastChild.data).toEqual('⋯');
563 expect(doc.toQuery()).toEqual('');
564
565 // Only removable
566 expect(docElement.lastChild.children.length).toEqual(0);
567 });
568
Akrond141a362018-07-10 18:12:13 +0200569 it('should be removable, when no root', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000570 var docGroup = docGroupClass.create();
571 docGroup.operation('or');
572 expect(docGroup.operation()).toEqual('or');
573
574 docGroup.append({
Akron712733a2018-04-05 18:17:47 +0200575 "@type": 'koral:doc',
576 "key": 'pubDate',
577 "match": 'match:eq',
578 "value": '2014-12-05',
579 "type": 'type:date'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000580 });
581
582 // Add unspecified object
583 docGroup.append();
584
Akrond141a362018-07-10 18:12:13 +0200585 var parent = document.createElement('div');
586 parent.appendChild(docGroup.element());
587
Nils Diewald7c8ced22015-04-15 19:21:00 +0000588 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
589 expect(docGroup.element().children[0].getAttribute('class')).toEqual('doc');
590
591 var unspec = docGroup.element().children[1];
592 expect(unspec.getAttribute('class')).toEqual('doc unspecified');
593
594 // Removable
595 expect(unspec.lastChild.children.length).toEqual(1);
596 expect(unspec.lastChild.children[0].getAttribute('class')).toEqual('delete');
597 });
598
Akrond141a362018-07-10 18:12:13 +0200599
Nils Diewald7c8ced22015-04-15 19:21:00 +0000600 it('should be replaceable by a doc', function () {
601 var doc = unspecifiedClass.create();
602 expect(doc.ldType()).toEqual("non");
603 // No parent, therefor not updateable
604 expect(doc.key("baum")).toBeNull();
605
606 var docGroup = docGroupClass.create();
607 docGroup.operation('or');
608 expect(docGroup.operation()).toEqual('or');
609
610 docGroup.append({
Akron712733a2018-04-05 18:17:47 +0200611 "@type": 'koral:doc',
612 "key": 'pubDate',
613 "match": 'match:eq',
614 "value": '2014-12-05',
615 "type": 'type:date'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000616 });
617
618 expect(docGroup.toQuery()).toEqual("pubDate in 2014-12-05");
619 docGroup.append();
620
621 expect(docGroup.getOperand(0).ldType()).toEqual("doc");
622 expect(docGroup.getOperand(1).ldType()).toEqual("non");
623
624 var op = docGroup.getOperand(1).element().lastChild;
Akron0b489ad2018-02-02 16:49:32 +0100625 expect(op.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000626 expect(op.children[0].getAttribute('class')).toEqual('delete');
627 expect(op.children.length).toEqual(1);
628
629 // Replace unspecified doc
630 expect(docGroup.getOperand(1).key("name")).not.toBeNull();
631 expect(docGroup.getOperand(1).ldType()).toEqual("doc");
632 expect(docGroup.getOperand(1).key()).toEqual("name");
Akron55a343b2018-04-06 19:57:36 +0200633 expect(docGroup.getOperand(1).value()).toBeUndefined();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000634
635 op = docGroup.getOperand(1).element().lastChild;
Akron0b489ad2018-02-02 16:49:32 +0100636 expect(op.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000637 expect(op.children[0].getAttribute('class')).toEqual('and');
638 expect(op.children[1].getAttribute('class')).toEqual('or');
639 expect(op.children[2].getAttribute('class')).toEqual('delete');
640 expect(op.children.length).toEqual(3);
641
642 docGroup.getOperand(1).value("Pachelbel");
643 expect(docGroup.getOperand(1).value()).toEqual("Pachelbel");
644 expect(docGroup.getOperand(1).type()).toEqual("string");
645 expect(docGroup.getOperand(1).matchop()).toEqual("eq");
646
647 // Specified!
648 expect(docGroup.toQuery()).toEqual('pubDate in 2014-12-05 | name = "Pachelbel"');
649 });
650
651 it('should be replaceable on root', function () {
Nils Diewald6283d692015-04-23 20:32:53 +0000652 var vc = vcClass.create();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000653 expect(vc.toQuery()).toEqual("");
654
655 expect(vc.root().ldType()).toEqual("non");
656
657 // No operators on root
658 op = vc.root().element().lastChild;
659 expect(op.lastChild.textContent).toEqual('⋯');
660
661 // Replace
662 expect(vc.root().key("baum")).not.toBeNull();
663 expect(vc.root().ldType()).toEqual("doc");
664
665 op = vc.root().element().lastChild;
Akron0b489ad2018-02-02 16:49:32 +0100666 expect(op.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000667 expect(op.children[0].getAttribute('class')).toEqual('and');
668 expect(op.children[1].getAttribute('class')).toEqual('or');
669 expect(op.children[2].getAttribute('class')).toEqual('delete');
670 expect(op.children.length).toEqual(3);
671 });
Akron55a343b2018-04-06 19:57:36 +0200672
673 it('should be clickable', function () {
674 var vc = vcClass.create([
675 ["pubDate", "date"]
676 ]);
677 expect(vc.toQuery()).toEqual("");
678 expect(vc.element().firstChild.textContent).toEqual("⋯");
679 vc.element().firstChild.firstChild.click();
680
681 // Click on pubDate
682 vc.element().firstChild.getElementsByTagName("LI")[0].click();
683
684 expect(vc.element().firstChild.firstChild.textContent).toEqual("pubDate");
685 expect(vc.element().firstChild.children[1].getAttribute("data-type")).toEqual("date");
686 });
Nils Diewald7c8ced22015-04-15 19:21:00 +0000687 });
688
689 describe('KorAP.Doc element', function () {
690 it('should be initializable', function () {
691 var docElement = docClass.create(undefined, {
Akron712733a2018-04-05 18:17:47 +0200692 "@type" : "koral:doc",
693 "key":"Titel",
694 "value":"Baum",
695 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000696 });
697 expect(docElement.key()).toEqual('Titel');
698 expect(docElement.matchop()).toEqual('eq');
699 expect(docElement.value()).toEqual('Baum');
700
701 var docE = docElement.element();
702 expect(docE.children[0].firstChild.data).toEqual('Titel');
703 expect(docE.children[1].firstChild.data).toEqual('eq');
704 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
705 expect(docE.children[2].firstChild.data).toEqual('Baum');
706 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
707
708 expect(docElement.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200709 "@type" : "koral:doc",
710 "key":"Titel",
711 "value":"Baum",
712 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000713 }));
714 });
715 });
716
717 describe('KorAP.DocGroup element', function () {
718 it('should be initializable', function () {
719
720 var docGroup = docGroupClass.create(undefined, {
Akron712733a2018-04-05 18:17:47 +0200721 "@type" : "koral:docGroup",
722 "operation" : "operation:and",
723 "operands" : [
724 {
725 "@type": 'koral:doc',
726 "key" : 'author',
727 "match": 'match:eq',
728 "value": 'Max Birkendale',
729 "type": 'type:string'
730 },
731 {
732 "@type": 'koral:doc',
733 "key": 'pubDate',
734 "match": 'match:eq',
735 "value": '2014-12-05',
736 "type": 'type:date'
737 }
738 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000739 });
740
741 expect(docGroup.operation()).toEqual('and');
742 var e = docGroup.element();
743 expect(e.getAttribute('class')).toEqual('docGroup');
744 expect(e.getAttribute('data-operation')).toEqual('and');
745
746 var first = e.children[0];
747 expect(first.getAttribute('class')).toEqual('doc');
748 expect(first.children[0].getAttribute('class')).toEqual('key');
749 expect(first.children[1].getAttribute('class')).toEqual('match');
750 expect(first.children[2].getAttribute('class')).toEqual('value');
751 expect(first.children[2].getAttribute('data-type')).toEqual('string');
752 expect(first.children[0].firstChild.data).toEqual('author');
753 expect(first.children[1].firstChild.data).toEqual('eq');
754 expect(first.children[2].firstChild.data).toEqual('Max Birkendale');
755
756 var second = e.children[1];
757 expect(second.getAttribute('class')).toEqual('doc');
758 expect(second.children[0].getAttribute('class')).toEqual('key');
759 expect(second.children[1].getAttribute('class')).toEqual('match');
760 expect(second.children[2].getAttribute('class')).toEqual('value');
761 expect(second.children[2].getAttribute('data-type')).toEqual('date');
762 expect(second.children[0].firstChild.data).toEqual('pubDate');
763 expect(second.children[1].firstChild.data).toEqual('eq');
764 expect(second.children[2].firstChild.data).toEqual('2014-12-05');
765 });
766
767 it('should be deserializable with nested groups', function () {
768 var docGroup = docGroupClass.create(undefined, {
Akron712733a2018-04-05 18:17:47 +0200769 "@type" : "koral:docGroup",
770 "operation" : "operation:or",
771 "operands" : [
772 {
773 "@type": 'koral:doc',
774 "key" : 'author',
775 "match": 'match:eq',
776 "value": 'Max Birkendale',
777 "type": 'type:string'
778 },
779 {
780 "@type" : "koral:docGroup",
781 "operation" : "operation:and",
782 "operands" : [
783 {
784 "@type": 'koral:doc',
785 "key": 'pubDate',
786 "match": 'match:geq',
787 "value": '2014-05-12',
788 "type": 'type:date'
789 },
790 {
791 "@type": 'koral:doc',
792 "key": 'pubDate',
793 "match": 'match:leq',
794 "value": '2014-12-05',
795 "type": 'type:date'
796 }
797 ]
798 }
799 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000800 });
801
802 expect(docGroup.operation()).toEqual('or');
803 var e = docGroup.element();
804 expect(e.getAttribute('class')).toEqual('docGroup');
805 expect(e.getAttribute('data-operation')).toEqual('or');
806
807 expect(e.children[0].getAttribute('class')).toEqual('doc');
808 var docop = e.children[0].lastChild;
Akron0b489ad2018-02-02 16:49:32 +0100809 expect(docop.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000810 expect(docop.children[0].getAttribute('class')).toEqual('and');
811 expect(docop.children[1].getAttribute('class')).toEqual('or');
812 expect(docop.children[2].getAttribute('class')).toEqual('delete');
813
814 expect(e.children[1].getAttribute('class')).toEqual('docGroup');
815 expect(e.children[1].getAttribute('data-operation')).toEqual('and');
816
817 // This and-operation can be "or"ed or "delete"d
818 var secop = e.children[1].children[2];
Akron0b489ad2018-02-02 16:49:32 +0100819 expect(secop.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000820 expect(secop.children[0].getAttribute('class')).toEqual('or');
821 expect(secop.children[1].getAttribute('class')).toEqual('delete');
822
823 // This or-operation can be "and"ed or "delete"d
Akron0b489ad2018-02-02 16:49:32 +0100824 expect(e.children[2].getAttribute('class')).toEqual('operators button-group');
825 expect(e.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000826 expect(e.lastChild.children[0].getAttribute('class')).toEqual('and');
827 expect(e.lastChild.children[1].getAttribute('class')).toEqual('delete');
828 });
829 });
830
Akrone4961b12017-05-10 21:04:46 +0200831 describe('KorAP.VirtualCorpus', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000832 var simpleGroupFactory = buildFactory(docGroupClass, {
833 "@type" : "koral:docGroup",
834 "operation" : "operation:and",
835 "operands" : [
Akron712733a2018-04-05 18:17:47 +0200836 {
837 "@type": 'koral:doc',
838 "key" : 'author',
839 "match": 'match:eq',
840 "value": 'Max Birkendale',
841 "type": 'type:string'
842 },
843 {
844 "@type": 'koral:doc',
845 "key": 'pubDate',
846 "match": 'match:eq',
847 "value": '2014-12-05',
848 "type": 'type:date'
849 }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000850 ]
851 });
852
853 var nestedGroupFactory = buildFactory(vcClass, {
854 "@type" : "koral:docGroup",
855 "operation" : "operation:or",
856 "operands" : [
Akron712733a2018-04-05 18:17:47 +0200857 {
858 "@type": 'koral:doc',
859 "key" : 'author',
860 "match": 'match:eq',
861 "value": 'Max Birkendale',
862 "type": 'type:string'
863 },
864 {
865 "@type" : "koral:docGroup",
866 "operation" : "operation:and",
867 "operands" : [
868 {
869 "@type": 'koral:doc',
870 "key": 'pubDate',
871 "match": 'match:geq',
872 "value": '2014-05-12',
873 "type": 'type:date'
874 },
875 {
876 "@type": 'koral:doc',
877 "key": 'pubDate',
878 "match": 'match:leq',
879 "value": '2014-12-05',
880 "type": 'type:date'
881 }
882 ]
883 }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000884 ]
885 });
886
887 var flatGroupFactory = buildFactory(vcClass, {
888 "@type" : "koral:docGroup",
889 "operation" : "operation:and",
890 "operands" : [
Akron712733a2018-04-05 18:17:47 +0200891 {
892 "@type": 'koral:doc',
893 "key": 'pubDate',
894 "match": 'match:geq',
895 "value": '2014-05-12',
896 "type": 'type:date'
897 },
898 {
899 "@type": 'koral:doc',
900 "key": 'pubDate',
901 "match": 'match:leq',
902 "value": '2014-12-05',
903 "type": 'type:date'
904 },
905 {
906 "@type": 'koral:doc',
907 "key": 'foo',
908 "match": 'match:eq',
909 "value": 'bar',
910 "type": 'type:string'
911 }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000912 ]
913 });
914
915 it('should be initializable', function () {
Nils Diewald6283d692015-04-23 20:32:53 +0000916 var vc = vcClass.create();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000917 expect(vc.element().getAttribute('class')).toEqual('vc');
918 expect(vc.root().element().getAttribute('class')).toEqual('doc unspecified');
919
920 // Not removable
921 expect(vc.root().element().lastChild.children.length).toEqual(0);
922 });
923
924 it('should be based on a doc', function () {
Nils Diewald6283d692015-04-23 20:32:53 +0000925 var vc = vcClass.create().fromJson({
Akron712733a2018-04-05 18:17:47 +0200926 "@type" : "koral:doc",
927 "key":"Titel",
928 "value":"Baum",
929 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000930 });
931
932 expect(vc.element().getAttribute('class')).toEqual('vc');
933 expect(vc.root().element().getAttribute('class')).toEqual('doc');
934 expect(vc.root().key()).toEqual('Titel');
935 expect(vc.root().value()).toEqual('Baum');
936 expect(vc.root().matchop()).toEqual('eq');
937
938 var docE = vc.root().element();
939 expect(docE.children[0].firstChild.data).toEqual('Titel');
940 expect(docE.children[1].firstChild.data).toEqual('eq');
941 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
942 expect(docE.children[2].firstChild.data).toEqual('Baum');
943 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
944 });
945
946 it('should be based on a docGroup', function () {
Nils Diewald6283d692015-04-23 20:32:53 +0000947 var vc = vcClass.create().fromJson(simpleGroupFactory.create().toJson());
Nils Diewald7c8ced22015-04-15 19:21:00 +0000948
949 expect(vc.element().getAttribute('class')).toEqual('vc');
950 expect(vc.root().element().getAttribute('class')).toEqual('docGroup');
951 expect(vc.root().operation()).toEqual('and');
952
953 var docGroup = vc.root();
954
955 var first = docGroup.getOperand(0);
956 expect(first.key()).toEqual('author');
957 expect(first.value()).toEqual('Max Birkendale');
958 expect(first.matchop()).toEqual('eq');
959
960 var second = docGroup.getOperand(1);
961 expect(second.key()).toEqual('pubDate');
962 expect(second.value()).toEqual('2014-12-05');
963 expect(second.matchop()).toEqual('eq');
964 });
965
966
967 it('should be based on a nested docGroup', function () {
968 var vc = nestedGroupFactory.create();
969
970 expect(vc.element().getAttribute('class')).toEqual('vc');
971 expect(vc.element().firstChild.getAttribute('class')).toEqual('docGroup');
972 expect(vc.element().firstChild.children[0].getAttribute('class')).toEqual('doc');
973 var dg = vc.element().firstChild.children[1];
974 expect(dg.getAttribute('class')).toEqual('docGroup');
975 expect(dg.children[0].getAttribute('class')).toEqual('doc');
976 expect(dg.children[1].getAttribute('class')).toEqual('doc');
Akron0b489ad2018-02-02 16:49:32 +0100977 expect(dg.children[2].getAttribute('class')).toEqual('operators button-group');
978 expect(vc.element().firstChild.children[2].getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000979 });
980
981 it('should be modifiable by deletion in flat docGroups', function () {
982 var vc = flatGroupFactory.create();
983 var docGroup = vc.root();
984
985 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
986
987 var doc = docGroup.getOperand(1);
988 expect(doc.key()).toEqual("pubDate");
989 expect(doc.value()).toEqual("2014-12-05");
990
991 // Remove operand 1
992 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
993 expect(doc._element).toEqual(undefined);
994
995 doc = docGroup.getOperand(1);
996 expect(doc.key()).toEqual("foo");
997
998 // Remove operand 1
999 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
1000 expect(doc._element).toEqual(undefined);
1001
1002 // Only one operand left ...
1003 expect(docGroup.getOperand(1)).toBeUndefined();
1004 // ... but there shouldn't be a group anymore at all!
1005 expect(docGroup.getOperand(0)).toBeUndefined();
1006
1007 var obj = vc.root();
1008 expect(obj.ldType()).toEqual("doc");
1009 expect(obj.key()).toEqual("pubDate");
1010 expect(obj.value()).toEqual("2014-05-12");
1011
1012 expect(obj.element().getAttribute('class')).toEqual('doc');
1013 });
1014
1015
1016 it('should be modifiable by deletion in nested docGroups (root case)', function () {
1017 var vc = nestedGroupFactory.create();
1018
1019 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001020 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05)'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001021 );
1022
1023 var docGroup = vc.root();
1024 expect(docGroup.ldType()).toEqual("docGroup");
1025 expect(docGroup.operation()).toEqual("or");
1026
1027 var doc = docGroup.getOperand(0);
1028 expect(doc.key()).toEqual("author");
1029 expect(doc.value()).toEqual("Max Birkendale");
1030
1031 docGroup = docGroup.getOperand(1);
1032 expect(docGroup.operation()).toEqual("and");
1033
1034 doc = docGroup.getOperand(0);
1035 expect(doc.key()).toEqual("pubDate");
1036 expect(doc.matchop()).toEqual("geq");
1037 expect(doc.value()).toEqual("2014-05-12");
1038 expect(doc.type()).toEqual("date");
1039
1040 doc = docGroup.getOperand(1);
1041 expect(doc.key()).toEqual("pubDate");
1042 expect(doc.matchop()).toEqual("leq");
1043 expect(doc.value()).toEqual("2014-12-05");
1044 expect(doc.type()).toEqual("date");
1045
1046 // Remove first operand so everything becomes root
1047 expect(
Akron712733a2018-04-05 18:17:47 +02001048 vc.root().delOperand(
1049 vc.root().getOperand(0)
1050 ).update().ldType()
Nils Diewald7c8ced22015-04-15 19:21:00 +00001051 ).toEqual("docGroup");
1052
1053 expect(vc.root().ldType()).toEqual("docGroup");
1054 expect(vc.root().operation()).toEqual("and");
1055 expect(vc.root().getOperand(0).ldType()).toEqual("doc");
1056
1057 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001058 'pubDate since 2014-05-12 & pubDate until 2014-12-05'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001059 );
1060 });
1061
1062 it('should be modifiable by deletion in nested docGroups (resolve group case)', function () {
1063 var vc = nestedGroupFactory.create();
1064
1065 // Get nested group
1066 var firstGroup = vc.root().getOperand(1);
1067 firstGroup.append(simpleGroupFactory.create({ "operation" : "operation:or" }));
1068
1069 // Structur is now:
1070 // or(doc, and(doc, doc, or(doc, doc)))
1071
1072 // Get nested or in and
1073 var orGroup = vc.root().getOperand(1).getOperand(2);
1074 expect(orGroup.ldType()).toEqual("docGroup");
1075 expect(orGroup.operation()).toEqual("or");
1076
1077 // Remove
1078 // Structur is now:
1079 // or(doc, and(doc, doc, doc)))
1080 expect(orGroup.delOperand(orGroup.getOperand(0)).update().operation()).toEqual("and");
1081 expect(vc.root().getOperand(1).operands().length).toEqual(3);
1082 });
1083
1084 it('should be modifiable by deletion in nested docGroups (identical group case)', function () {
1085 var vc = nestedGroupFactory.create();
1086
1087 // Get nested group
1088 var firstGroup = vc.root().getOperand(1);
1089 firstGroup.append(simpleGroupFactory.create({
Akron712733a2018-04-05 18:17:47 +02001090 "operation" : "operation:or"
Nils Diewald7c8ced22015-04-15 19:21:00 +00001091 }));
1092 var oldAuthor = firstGroup.getOperand(2).getOperand(0);
1093 oldAuthor.key("title");
1094 oldAuthor.value("Der Birnbaum");
1095
1096 // Structur is now:
1097 // or(doc, and(doc, doc, or(doc, doc)))
1098 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001099 'author = "Max Birkendale" | ' +
1100 '(pubDate since 2014-05-12 & ' +
1101 'pubDate until 2014-12-05 & ' +
1102 '(title = "Der Birnbaum" | ' +
1103 'pubDate in 2014-12-05))'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001104 );
1105
1106 var andGroup = vc.root().getOperand(1);
1107
1108 // Get leading docs in and
1109 var doc1 = andGroup.getOperand(0);
1110 expect(doc1.ldType()).toEqual("doc");
1111 expect(doc1.value()).toEqual("2014-05-12");
1112 var doc2 = andGroup.getOperand(1);
1113 expect(doc2.ldType()).toEqual("doc");
1114 expect(doc2.value()).toEqual("2014-12-05");
1115
1116 // Remove 2
1117 expect(
Akron712733a2018-04-05 18:17:47 +02001118 andGroup.delOperand(doc2).update().operation()
Nils Diewald7c8ced22015-04-15 19:21:00 +00001119 ).toEqual("and");
1120 // Structur is now:
1121 // or(doc, and(doc, or(doc, doc)))
1122
1123 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001124 'author = "Max Birkendale"' +
1125 ' | (pubDate since 2014-05-12 & ' +
1126 '(title = "Der Birnbaum" | pubDate in 2014-12-05))'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001127 );
1128
1129
1130 // Remove 1
1131 expect(andGroup.delOperand(doc1).update().operation()).toEqual("or");
1132 // Structur is now:
1133 // or(doc, doc, doc)
1134
1135 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001136 'author = "Max Birkendale" | title = "Der Birnbaum" | pubDate in 2014-12-05'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001137 );
1138 });
1139
1140 it('should be reducible to unspecification', function () {
1141 var vc = demoFactory.create();
1142
1143 expect(vc.toQuery()).toEqual(vc.root().toQuery());
1144 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001145 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & ' +
1146 '(Titel = "Baum" | Veröffentlichungsort = "hihi")) ' +
1147 '| Untertitel ~ "huhu"');
Akrond141a362018-07-10 18:12:13 +02001148 expect(vc.root().element().lastChild.children[0].innerText).toEqual('and');
1149 expect(vc.root().element().lastChild.children[1].innerText).toEqual('×');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001150 expect(vc.root().delOperand(vc.root().getOperand(0)).update()).not.toBeUndefined();
Akron712733a2018-04-05 18:17:47 +02001151 expect(vc.toQuery()).toEqual('Untertitel ~ "huhu"');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001152
1153 var lc = vc.root().element().lastChild;
Akrond141a362018-07-10 18:12:13 +02001154 expect(lc.children[0].innerText).toEqual('and');
1155 expect(lc.children[1].innerText).toEqual('or');
1156 expect(lc.children[2].innerText).toEqual('×');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001157
1158 // Clean everything
1159 vc.clean();
1160 expect(vc.toQuery()).toEqual('');
1161 });
1162
1163 it('should flatten on import', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001164 var vc = vcClass.create().fromJson({
Akron712733a2018-04-05 18:17:47 +02001165 "@type":"koral:docGroup",
1166 "operation":"operation:or",
1167 "operands":[
1168 {
1169 "@type":"koral:docGroup",
1170 "operation":"operation:or",
1171 "operands":[
Nils Diewald7c8ced22015-04-15 19:21:00 +00001172 {
Akron712733a2018-04-05 18:17:47 +02001173 "@type":"koral:doc",
1174 "key":"Titel",
1175 "value":"Baum",
1176 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +00001177 },
1178 {
Akron712733a2018-04-05 18:17:47 +02001179 "@type":"koral:doc",
1180 "key":"Veröffentlichungsort",
1181 "value":"hihi",
1182 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +00001183 },
1184 {
Akron712733a2018-04-05 18:17:47 +02001185 "@type":"koral:docGroup",
1186 "operation":"operation:or",
1187 "operands":[
1188 {
1189 "@type":"koral:doc",
1190 "key":"Titel",
1191 "value":"Baum",
1192 "match":"match:eq"
1193 },
1194 {
1195 "@type":"koral:doc",
1196 "key":"Veröffentlichungsort",
1197 "value":"hihi",
1198 "match":"match:eq"
1199 }
1200 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +00001201 }
Akron712733a2018-04-05 18:17:47 +02001202 ]
1203 },
1204 {
1205 "@type":"koral:doc",
1206 "key":"Untertitel",
1207 "value":"huhu",
1208 "match":"match:contains"
1209 }
1210 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +00001211 });
Nils Diewaldfda29d92015-01-22 17:28:01 +00001212
Nils Diewald7c8ced22015-04-15 19:21:00 +00001213 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001214 'Titel = "Baum" | Veröffentlichungsort = "hihi" | Untertitel ~ "huhu"'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001215 );
Nils Diewald86dad5b2015-01-28 15:09:07 +00001216 });
Akron1bdf5272018-07-24 18:51:30 +02001217
1218 it('should be deserializable from collection 1', function () {
1219 var kq = {
1220 "matches":["..."],
1221 "collection":{
1222 "@type": "koral:docGroup",
1223 "operation": "operation:or",
1224 "operands": [{
1225 "@type": "koral:docGroup",
1226 "operation": "operation:and",
1227 "operands": [
1228 {
1229 "@type": "koral:doc",
1230 "key": "title",
1231 "match": "match:eq",
1232 "value": "Der Birnbaum",
1233 "type": "type:string"
1234 },
1235 {
1236 "@type": "koral:doc",
1237 "key": "pubPlace",
1238 "match": "match:eq",
1239 "value": "Mannheim",
1240 "type": "type:string"
1241 },
1242 {
1243 "@type": "koral:docGroup",
1244 "operation": "operation:or",
1245 "operands": [
1246 {
1247 "@type": "koral:doc",
1248 "key": "subTitle",
1249 "match": "match:eq",
1250 "value": "Aufzucht und Pflege",
1251 "type": "type:string"
1252 },
1253 {
1254 "@type": "koral:doc",
1255 "key": "subTitle",
1256 "match": "match:eq",
1257 "value": "Gedichte",
1258 "type": "type:string"
1259 }
1260 ]
1261 }
1262 ]
1263 },{
1264 "@type": "koral:doc",
1265 "key": "pubDate",
1266 "match": "match:geq",
1267 "value": "2015-03-05",
1268 "type": "type:date",
1269 "rewrites" : [{
1270 "@type" : "koral:rewrite",
1271 "operation" : "operation:modification",
1272 "src" : "querySerializer",
1273 "scope" : "tree"
1274 }]
1275 }]
1276 }
1277 };
1278
1279 var vc = vcClass.create().fromJson(kq["collection"]);
1280 expect(vc.toQuery()).toEqual('(title = "Der Birnbaum" & pubPlace = "Mannheim" & (subTitle = "Aufzucht und Pflege" | subTitle = "Gedichte")) | pubDate since 2015-03-05');
1281 });
1282
1283 it('should be deserializable from collection 2', function () {
1284 var kq = {
1285 "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
1286 "meta": {},
1287 "query": {
1288 "@type": "koral:token",
1289 "wrap": {
1290 "@type": "koral:term",
1291 "match": "match:eq",
1292 "layer": "orth",
1293 "key": "Baum",
1294 "foundry": "opennlp",
1295 "rewrites": [
1296 {
1297 "@type": "koral:rewrite",
1298 "src": "Kustvakt",
1299 "operation": "operation:injection",
1300 "scope": "foundry"
1301 }
1302 ]
1303 },
1304 "idn": "Baum_2227",
1305 "rewrites": [
1306 {
1307 "@type": "koral:rewrite",
1308 "src": "Kustvakt",
1309 "operation": "operation:injection",
1310 "scope": "idn"
1311 }
1312 ]
1313 },
1314 "collection": {
1315 "@type": "koral:docGroup",
1316 "operation": "operation:and",
1317 "operands": [
1318 {
1319 "@type": "koral:doc",
1320 "match": "match:eq",
1321 "type": "type:regex",
1322 "value": "CC-BY.*",
1323 "key": "availability"
1324 },
1325 {
1326 "@type": "koral:doc",
1327 "match": "match:eq",
1328 "type":"type:text",
1329 "value": "Goethe",
1330 "key": "author"
1331 }
1332 ],
1333 "rewrites": [
1334 {
1335 "@type": "koral:rewrite",
1336 "src": "Kustvakt",
1337 "operation": "operation:insertion",
1338 "scope": "availability(FREE)"
1339 }
1340 ]
1341 },
1342 "matches": []
1343 };
1344
1345 var vc = vcClass.create().fromJson(kq["collection"]);
1346 expect(vc.toQuery()).toEqual('availability = /CC-BY.*/ & author = "Goethe"');
1347 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00001348 });
1349
Akron1bdf5272018-07-24 18:51:30 +02001350 it('shouldn\'t be deserializable from collection with unknown type', function () {
1351 var kq = {
1352 "@type" : "koral:doc",
1353 "match": "match:eq",
1354 "type":"type:failure",
1355 "value": "Goethe",
1356 "key": "author"
1357 };
1358
1359 expect(function () {
1360 vcClass.create().fromJson(kq)
1361 }).toThrow(new Error("Unknown value type: string"));
1362
1363 });
1364
1365
Nils Diewald7c8ced22015-04-15 19:21:00 +00001366 describe('KorAP.Operators', function () {
1367 it('should be initializable', function () {
1368 var op = operatorsClass.create(true, false, false);
1369 expect(op.and()).toBeTruthy();
1370 expect(op.or()).not.toBeTruthy();
1371 expect(op.del()).not.toBeTruthy();
1372
1373 op.and(false);
1374 expect(op.and()).not.toBeTruthy();
1375 expect(op.or()).not.toBeTruthy();
1376 expect(op.del()).not.toBeTruthy();
1377
1378 op.or(true);
1379 op.del(true);
1380 expect(op.and()).not.toBeTruthy();
1381 expect(op.or()).toBeTruthy();
1382 expect(op.del()).toBeTruthy();
1383
Akrond141a362018-07-10 18:12:13 +02001384 var e = op.update();
Akron0b489ad2018-02-02 16:49:32 +01001385 expect(e.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001386 expect(e.children[0].getAttribute('class')).toEqual('or');
Akrond141a362018-07-10 18:12:13 +02001387 expect(e.children[0].innerText).toEqual('or');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001388 expect(e.children[1].getAttribute('class')).toEqual('delete');
Akrond141a362018-07-10 18:12:13 +02001389 expect(e.children[1].innerText).toEqual('×');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001390
1391 op.and(true);
1392 op.del(false);
Akrond141a362018-07-10 18:12:13 +02001393 e = op.update();
Nils Diewald7c8ced22015-04-15 19:21:00 +00001394
Akron0b489ad2018-02-02 16:49:32 +01001395 expect(e.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001396 expect(e.children[0].getAttribute('class')).toEqual('and');
Akrond141a362018-07-10 18:12:13 +02001397 expect(e.children[0].innerText).toEqual('and');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001398 expect(e.children[1].getAttribute('class')).toEqual('or');
Akrond141a362018-07-10 18:12:13 +02001399 expect(e.children[1].innerText).toEqual('or');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001400 });
1401 });
1402
1403 describe('KorAP._delete (event)', function () {
1404 var complexVCFactory = buildFactory(vcClass,{
1405 "@type": 'koral:docGroup',
1406 'operation' : 'operation:and',
1407 'operands' : [
Akron712733a2018-04-05 18:17:47 +02001408 {
1409 "@type": 'koral:doc',
1410 "key": 'pubDate',
1411 "match": 'match:eq',
1412 "value": '2014-12-05',
1413 "type": 'type:date'
1414 },
1415 {
1416 "@type" : 'koral:docGroup',
1417 'operation' : 'operation:or',
1418 'operands' : [
1419 {
1420 '@type' : 'koral:doc',
1421 'key' : 'title',
1422 'value' : 'Hello World!'
1423 },
1424 {
1425 '@type' : 'koral:doc',
1426 'key' : 'foo',
1427 'value' : 'bar'
1428 }
1429 ]
1430 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001431 ]
1432 });
1433
1434 it('should clean on root docs', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001435 var vc = vcClass.create().fromJson({
Akron712733a2018-04-05 18:17:47 +02001436 "@type": 'koral:doc',
1437 "key": 'pubDate',
1438 "match": 'match:eq',
1439 "value": '2014-12-05',
1440 "type": 'type:date'
Nils Diewald86dad5b2015-01-28 15:09:07 +00001441 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00001442 expect(vc.root().toQuery()).toEqual('pubDate in 2014-12-05');
Akron0b489ad2018-02-02 16:49:32 +01001443 expect(vc.root().element().lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald86dad5b2015-01-28 15:09:07 +00001444
Nils Diewald7c8ced22015-04-15 19:21:00 +00001445 // Clean with delete from root
1446 expect(vc.root().element().lastChild.lastChild.getAttribute('class')).toEqual('delete');
1447 _delOn(vc.root());
1448 expect(vc.root().toQuery()).toEqual('');
1449 expect(vc.root().element().lastChild.lastChild.data).toEqual('⋯');
1450 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00001451
Nils Diewald7c8ced22015-04-15 19:21:00 +00001452 it('should remove on nested docs', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001453 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02001454 {
1455 "@type": 'koral:docGroup',
1456 'operation' : 'operation:and',
1457 'operands' : [
1458 {
1459 "@type": 'koral:doc',
1460 "key": 'pubDate',
1461 "match": 'match:eq',
1462 "value": '2014-12-05',
1463 "type": 'type:date'
1464 },
1465 {
1466 "@type" : 'koral:doc',
1467 'key' : 'foo',
1468 'value' : 'bar'
1469 }
1470 ]
1471 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001472 );
1473
1474 // Delete with direct element access
1475 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1476 _delOn(vc.root().getOperand(0));
1477
1478 expect(vc.toQuery()).toEqual('foo = "bar"');
1479 expect(vc.root().ldType()).toEqual('doc');
1480 });
1481
1482 it('should clean on doc groups', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001483 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02001484 {
1485 "@type": 'koral:docGroup',
1486 'operation' : 'operation:and',
1487 'operands' : [
1488 {
1489 "@type": 'koral:doc',
1490 "key": 'pubDate',
1491 "match": 'match:eq',
1492 "value": '2014-12-05',
1493 "type": 'type:date'
1494 },
1495 {
1496 "@type" : 'koral:doc',
1497 'key' : 'foo',
1498 'value' : 'bar'
1499 }
1500 ]
1501 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001502 );
1503
1504 // Cleanwith direct element access
1505 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1506 _delOn(vc.root());
1507 expect(vc.toQuery()).toEqual('');
1508 expect(vc.root().ldType()).toEqual('non');
1509 });
1510
1511 it('should remove on nested doc groups (case of ungrouping 1)', function () {
1512 var vc = complexVCFactory.create();
1513
1514 // Delete with direct element access
1515 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001516 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001517 );
1518
1519 // Remove hello world:
1520 _delOn(vc.root().getOperand(1).getOperand(0));
1521 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1522 expect(vc.root().ldType()).toEqual('docGroup');
1523 });
1524
1525 it('should remove on nested doc groups (case of ungrouping 2)', function () {
1526 var vc = complexVCFactory.create();
1527
1528 // Delete with direct element access
1529 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001530 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001531 );
1532
1533 // Remove bar
1534 _delOn(vc.root().getOperand(1).getOperand(1));
1535 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & title = "Hello World!"');
1536 expect(vc.root().ldType()).toEqual('docGroup');
1537 expect(vc.root().operation()).toEqual('and');
1538 });
1539
1540 it('should remove on nested doc groups (case of root changing)', function () {
1541 var vc = complexVCFactory.create();
1542
1543 // Delete with direct element access
1544 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001545 'pubDate in 2014-12-05 & ' +
1546 '(title = "Hello World!" | foo = "bar")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001547 );
1548
1549 // Remove bar
1550 _delOn(vc.root().getOperand(0));
1551 expect(vc.toQuery()).toEqual('title = "Hello World!" | foo = "bar"');
1552 expect(vc.root().ldType()).toEqual('docGroup');
1553 expect(vc.root().operation()).toEqual('or');
1554 });
1555
1556 it('should remove on nested doc groups (list flattening)', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001557 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02001558 {
1559 "@type": 'koral:docGroup',
1560 'operation' : 'operation:or',
1561 'operands' : [
1562 {
1563 "@type": 'koral:doc',
1564 "key": 'pubDate',
1565 "match": 'match:eq',
1566 "value": '2014-12-05',
1567 "type": 'type:date'
1568 },
1569 {
1570 "@type" : 'koral:doc',
1571 'key' : 'foo',
1572 'value' : 'bar'
1573 },
1574 {
1575 "@type": 'koral:docGroup',
1576 'operation' : 'operation:and',
1577 'operands' : [
1578 {
1579 "@type": 'koral:doc',
1580 "key": 'pubDate',
1581 "match": 'match:eq',
1582 "value": '2014-12-05',
1583 "type": 'type:date'
1584 },
1585 {
1586 "@type" : 'koral:docGroup',
1587 'operation' : 'operation:or',
1588 'operands' : [
1589 {
1590 '@type' : 'koral:doc',
1591 'key' : 'title',
1592 'value' : 'Hello World!'
1593 },
1594 {
1595 '@type' : 'koral:doc',
1596 'key' : 'yeah',
1597 'value' : 'juhu'
1598 }
1599 ]
1600 }
1601 ]
1602 }
1603 ]
1604 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001605 );
1606
1607 // Delete with direct element access
1608 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001609 'pubDate in 2014-12-05 | foo = "bar" | ' +
1610 '(pubDate in 2014-12-05 & ' +
1611 '(title = "Hello World!" | yeah = "juhu"))'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001612 );
1613
1614 expect(vc.root().ldType()).toEqual('docGroup');
1615 expect(vc.root().operation()).toEqual('or');
1616
1617 // Operands and operators
1618 expect(vc.element().firstChild.children.length).toEqual(4);
Akron0b489ad2018-02-02 16:49:32 +01001619 expect(vc.element().firstChild.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001620
1621 // Remove inner group and flatten
1622 _delOn(vc.root().getOperand(2).getOperand(0));
1623
1624 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001625 'pubDate in 2014-12-05 | foo = "bar" | title = "Hello World!" | yeah = "juhu"'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001626 );
1627 expect(vc.root().ldType()).toEqual('docGroup');
1628 expect(vc.root().operation()).toEqual('or');
1629
1630 // Operands and operators
1631 expect(vc.element().firstChild.children.length).toEqual(5);
Akron0b489ad2018-02-02 16:49:32 +01001632 expect(vc.element().firstChild.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001633 });
1634 });
1635
1636 describe('KorAP._add (event)', function () {
1637 var complexVCFactory = buildFactory(vcClass,{
1638 "@type": 'koral:docGroup',
1639 'operation' : 'operation:and',
1640 'operands' : [
Akron712733a2018-04-05 18:17:47 +02001641 {
1642 "@type": 'koral:doc',
1643 "key": 'pubDate',
1644 "match": 'match:eq',
1645 "value": '2014-12-05',
1646 "type": 'type:date'
1647 },
1648 {
1649 "@type" : 'koral:docGroup',
1650 'operation' : 'operation:or',
1651 'operands' : [
1652 {
1653 '@type' : 'koral:doc',
1654 'key' : 'title',
1655 'value' : 'Hello World!'
1656 },
1657 {
1658 '@type' : 'koral:doc',
1659 'key' : 'foo',
1660 'value' : 'bar'
1661 }
1662 ]
1663 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001664 ]
1665 });
1666
1667 it('should add new unspecified doc with "and"', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001668 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02001669 {
1670 "@type": 'koral:docGroup',
1671 'operation' : 'operation:and',
1672 'operands' : [
1673 {
1674 "@type": 'koral:doc',
1675 "key": 'pubDate',
1676 "match": 'match:eq',
1677 "value": '2014-12-05',
1678 "type": 'type:date'
1679 },
1680 {
1681 "@type" : 'koral:doc',
1682 'key' : 'foo',
1683 'value' : 'bar'
1684 }
1685 ]
1686 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001687 );
1688
1689 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1690
1691 var fc = vc.element().firstChild;
1692 expect(fc.getAttribute('data-operation')).toEqual('and');
1693 expect(fc.children.length).toEqual(3);
Akron0b489ad2018-02-02 16:49:32 +01001694 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001695 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1696 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1697
1698 // add with 'and' in the middle
1699 _andOn(vc.root().getOperand(0));
1700 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1701
1702 fc = vc.element().firstChild;
1703 expect(fc.getAttribute('data-operation')).toEqual('and');
1704 expect(fc.children.length).toEqual(4);
Akron0b489ad2018-02-02 16:49:32 +01001705 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001706
1707 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1708 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
1709 expect(fc.children[2].getAttribute('class')).toEqual('doc');
1710 });
1711
1712 it('should add new unspecified doc with "or"', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001713 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02001714 {
1715 "@type": 'koral:docGroup',
1716 'operation' : 'operation:and',
1717 'operands' : [
1718 {
1719 "@type": 'koral:doc',
1720 "key": 'pubDate',
1721 "match": 'match:eq',
1722 "value": '2014-12-05',
1723 "type": 'type:date'
1724 },
1725 {
1726 "@type" : 'koral:doc',
1727 'key' : 'foo',
1728 'value' : 'bar'
1729 }
1730 ]
1731 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001732 );
1733
1734 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1735
1736 var fc = vc.element().firstChild;
1737 expect(fc.children.length).toEqual(3);
Akron0b489ad2018-02-02 16:49:32 +01001738 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001739 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1740 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1741
1742 // add with 'or' in the middle
1743 _orOn(vc.root().getOperand(0));
1744 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1745 fc = vc.element().firstChild;
1746
1747 expect(fc.getAttribute('data-operation')).toEqual('and');
1748 expect(fc.children.length).toEqual(3);
1749 expect(fc.children[0].getAttribute('class')).toEqual('docGroup');
1750 expect(fc.children[0].getAttribute('data-operation')).toEqual('or');
1751 expect(fc.children[1].getAttribute('class')).toEqual('doc');
Akron0b489ad2018-02-02 16:49:32 +01001752 expect(fc.children[2].getAttribute('class')).toEqual('operators button-group');
1753 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001754
1755 fc = vc.element().firstChild.firstChild;
1756 expect(fc.children.length).toEqual(3);
1757 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1758 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
Akron0b489ad2018-02-02 16:49:32 +01001759 expect(fc.children[2].getAttribute('class')).toEqual('operators button-group');
1760 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001761 });
1762
1763 it('should add new unspecified doc with "and" before group', function () {
1764 var vc = demoFactory.create();
1765
1766 // Wrap with direct element access
1767 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001768 '(Titel = "Baum" & ' +
1769 'Veröffentlichungsort = "hihi" & ' +
1770 '(Titel = "Baum" | ' +
1771 'Veröffentlichungsort = "hihi")) | ' +
1772 'Untertitel ~ "huhu"'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001773 );
1774
1775 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
1776 expect(vc.root().getOperand(0).operation()).toEqual('and');
1777 expect(vc.root().getOperand(0).operands().length).toEqual(3);
1778
1779 // Add unspecified on the second doc
1780 var secDoc = vc.root().getOperand(0).getOperand(1);
1781 expect(secDoc.value()).toEqual('hihi');
1782
1783 // Add
1784 _andOn(secDoc);
1785
1786 var fo = vc.root().getOperand(0);
1787
1788 expect(fo.ldType()).toEqual('docGroup');
1789 expect(fo.operation()).toEqual('and');
1790 expect(fo.operands().length).toEqual(4);
1791
1792 expect(fo.getOperand(0).ldType()).toEqual('doc');
1793 expect(fo.getOperand(1).ldType()).toEqual('doc');
1794 expect(fo.getOperand(2).ldType()).toEqual('non');
1795 expect(fo.getOperand(3).ldType()).toEqual('docGroup');
1796 });
1797
1798
1799 it('should remove a doc with an unspecified doc in a nested group', function () {
1800 var vc = demoFactory.create();
1801
1802 // Wrap with direct element access
1803 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001804 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & (Titel = "Baum" | Veröffentlichungsort = "hihi")) | Untertitel ~ "huhu"'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001805 );
1806
1807 var fo = vc.root().getOperand(0).getOperand(0);
1808 expect(fo.key()).toEqual('Titel');
1809 expect(fo.value()).toEqual('Baum');
1810
1811 // Add unspecified on the root group
1812 _orOn(fo);
1813
1814 fo = vc.root().getOperand(0).getOperand(0);
1815
1816 expect(fo.operation()).toEqual('or');
1817 expect(fo.getOperand(0).ldType()).toEqual('doc');
1818 expect(fo.getOperand(1).ldType()).toEqual('non');
1819
1820 // Delete document
1821 _delOn(fo.getOperand(0));
1822
1823 // The operand is now non
1824 expect(vc.root().getOperand(0).getOperand(0).ldType()).toEqual('non');
1825 expect(vc.root().getOperand(0).getOperand(1).ldType()).toEqual('doc');
1826 expect(vc.root().getOperand(0).getOperand(2).ldType()).toEqual('docGroup');
1827 });
1828
1829
Akron712733a2018-04-05 18:17:47 +02001830 it('should remove an unspecified doc with a doc in a nested group', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +00001831 var vc = demoFactory.create();
1832
1833 // Wrap with direct element access
1834 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001835 '(Titel = "Baum" & ' +
1836 'Veröffentlichungsort = "hihi" & ' +
1837 '(Titel = "Baum" ' +
1838 '| Veröffentlichungsort = "hihi")) | ' +
1839 'Untertitel ~ "huhu"'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001840 );
1841
1842 var fo = vc.root().getOperand(0).getOperand(0);
1843 expect(fo.key()).toEqual('Titel');
1844 expect(fo.value()).toEqual('Baum');
1845
1846 // Add unspecified on the root group
1847 _orOn(fo);
1848
1849 fo = vc.root().getOperand(0).getOperand(0);
1850
1851 expect(fo.operation()).toEqual('or');
1852 expect(fo.getOperand(0).ldType()).toEqual('doc');
1853 expect(fo.getOperand(1).ldType()).toEqual('non');
1854
1855 // Delete unspecified doc
1856 _delOn(fo.getOperand(1));
1857
1858 // The operand is now non
1859 fo = vc.root().getOperand(0);
1860 expect(fo.getOperand(0).ldType()).toEqual('doc');
1861 expect(fo.getOperand(0).key()).toEqual('Titel');
1862 expect(fo.getOperand(0).value()).toEqual('Baum');
1863 expect(fo.getOperand(1).ldType()).toEqual('doc');
1864 expect(fo.getOperand(2).ldType()).toEqual('docGroup');
1865 });
1866
1867
1868 it('should add on parent group (case "and")', function () {
1869 var vc = complexVCFactory.create();
1870
1871 // Wrap with direct element access
1872 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001873 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001874 );
1875
1876 expect(vc.root().operands().length).toEqual(2);
1877
1878 // Add unspecified on the root group
1879 _andOn(vc.root().getOperand(1));
1880 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001881 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001882 );
1883
1884 expect(vc.root().ldType()).toEqual('docGroup');
1885 expect(vc.root().operands().length).toEqual(3);
1886 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
1887 expect(vc.root().getOperand(1).ldType()).toEqual('docGroup');
1888 expect(vc.root().getOperand(1).operation()).toEqual('or');
1889 expect(vc.root().getOperand(2).ldType()).toEqual('non');
1890
1891 // Add another unspecified on the root group
1892 _andOn(vc.root().getOperand(1));
1893
1894 expect(vc.root().operands().length).toEqual(4);
1895 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
1896 expect(vc.root().getOperand(1).ldType()).toEqual('docGroup');
1897 expect(vc.root().getOperand(2).ldType()).toEqual('non');
1898 expect(vc.root().getOperand(3).ldType()).toEqual('non');
1899
1900 // Add another unspecified after the first doc
1901 _andOn(vc.root().getOperand(0));
1902
1903 expect(vc.root().operands().length).toEqual(5);
1904 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
1905 expect(vc.root().getOperand(1).ldType()).toEqual('non');
1906 expect(vc.root().getOperand(2).ldType()).toEqual('docGroup');
1907 expect(vc.root().getOperand(3).ldType()).toEqual('non');
1908 expect(vc.root().getOperand(4).ldType()).toEqual('non');
1909 });
1910
1911 it('should wrap on root', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001912 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02001913 {
1914 "@type": 'koral:docGroup',
1915 'operation' : 'operation:and',
1916 'operands' : [
1917 {
1918 "@type": 'koral:doc',
1919 "key": 'pubDate',
1920 "match": 'match:eq',
1921 "value": '2014-12-05',
1922 "type": 'type:date'
1923 },
1924 {
1925 "@type" : 'koral:doc',
1926 'key' : 'foo',
1927 'value' : 'bar'
1928 }
1929 ]
1930 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001931 );
1932
1933 // Wrap on root
1934 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1935 expect(vc.root().ldType()).toEqual('docGroup');
1936 expect(vc.root().operation()).toEqual('and');
1937 _orOn(vc.root());
1938 expect(vc.root().ldType()).toEqual('docGroup');
1939 expect(vc.root().operation()).toEqual('or');
1940
1941 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
1942 expect(vc.root().getOperand(0).operation()).toEqual('and');
1943 });
1944
1945 it('should add on root (case "and")', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001946 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02001947 {
1948 "@type": 'koral:doc',
1949 "key": 'pubDate',
1950 "match": 'match:eq',
1951 "value": '2014-12-05',
1952 "type": 'type:date'
1953 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001954 );
1955
1956 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
1957 expect(vc.root().ldType()).toEqual('doc');
1958 expect(vc.root().key()).toEqual('pubDate');
1959 expect(vc.root().value()).toEqual('2014-12-05');
1960
1961 // Wrap on root
1962 _andOn(vc.root());
1963 expect(vc.root().ldType()).toEqual('docGroup');
1964 expect(vc.root().operation()).toEqual('and');
1965 });
1966
1967 it('should add on root (case "or")', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001968 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02001969 {
1970 "@type": 'koral:doc',
1971 "key": 'pubDate',
1972 "match": 'match:eq',
1973 "value": '2014-12-05',
1974 "type": 'type:date'
1975 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001976 );
1977
1978 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
1979 expect(vc.root().key()).toEqual('pubDate');
1980 expect(vc.root().value()).toEqual('2014-12-05');
1981
1982 // Wrap on root
1983 _orOn(vc.root());
1984 expect(vc.root().ldType()).toEqual('docGroup');
1985 expect(vc.root().operation()).toEqual('or');
1986 });
1987
1988 it('should support multiple sub groups per group', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001989 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02001990 {
1991 "@type": 'koral:docGroup',
1992 'operation' : 'operation:or',
1993 'operands' : [
1994 {
1995 "@type": 'koral:docGroup',
1996 'operation' : 'operation:and',
1997 'operands' : [
1998 {
1999 "@type": 'koral:doc',
2000 "key": 'title',
2001 "value": 't1',
2002 },
2003 {
2004 "@type" : 'koral:doc',
2005 'key' : 'title',
2006 'value' : 't2'
2007 }
2008 ]
2009 },
2010 {
2011 "@type": 'koral:docGroup',
2012 'operation' : 'operation:and',
2013 'operands' : [
2014 {
2015 "@type": 'koral:doc',
2016 "key": 'title',
2017 "value": 't3',
2018 },
2019 {
2020 "@type" : 'koral:doc',
2021 'key' : 'title',
2022 'value' : 't4'
2023 }
2024 ]
2025 }
2026 ]
2027 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002028 );
2029 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002030 '(title = "t1" & title = "t2") | ' +
2031 '(title = "t3" & title = "t4")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002032 );
2033 expect(vc.root().operation()).toEqual('or');
2034 expect(vc.root().getOperand(0).toQuery()).toEqual('title = "t1" & title = "t2"');
2035 expect(vc.root().getOperand(1).toQuery()).toEqual('title = "t3" & title = "t4"');
2036
2037 _andOn(vc.root());
2038
2039 expect(vc.root().operation()).toEqual('and');
2040 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
2041 expect(vc.root().getOperand(1).ldType()).toEqual('non');
2042 });
2043 });
2044
Akron5c829e92017-05-12 18:10:00 +02002045 // Check class method
2046 describe('KorAP.VC.checkRewrite', function () {
2047
2048 it('should check for simple rewrites', function () {
2049 expect(vcClass.checkRewrite(
2050 {
2051 "@type" : "koral:doc",
2052 "rewrites" : [{
Akron712733a2018-04-05 18:17:47 +02002053 "@type" : "koral:rewrite",
2054 "operation" : "operation:modification",
2055 "src" : "querySerializer",
2056 "scope" : "tree"
Akron5c829e92017-05-12 18:10:00 +02002057 }]
2058 }
2059 )).toBeTruthy();
Nils Diewald7c8ced22015-04-15 19:21:00 +00002060
Akron5c829e92017-05-12 18:10:00 +02002061 var nested = {
2062 "@type" : "koral:docGroup",
2063 "operands" : [
2064 {
2065 "@type" : "koral:doc"
2066 },
2067 {
2068 "@type" : "koral:docGroup",
2069 "operands" : [
2070 {
2071 "@type": "koral:doc"
2072 },
2073 {
2074 "@type": "koral:doc"
2075 }
2076 ]
2077 }
2078 ]
2079 };
2080
2081 expect(vcClass.checkRewrite(nested)).toBe(false);
2082
2083 nested["operands"][1]["operands"][1]["rewrites"] = [{
Akron712733a2018-04-05 18:17:47 +02002084 "@type" : "koral:rewrite",
2085 "operation" : "operation:modification",
2086 "src" : "querySerializer",
2087 "scope" : "tree"
Akron5c829e92017-05-12 18:10:00 +02002088 }];
2089
2090 expect(vcClass.checkRewrite(nested)).toBeTruthy();
2091 });
2092 });
Nils Diewald6283d692015-04-23 20:32:53 +00002093
2094 describe('KorAP.Rewrite', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +00002095 it('should be initializable', function () {
2096 var rewrite = rewriteClass.create({
Akron712733a2018-04-05 18:17:47 +02002097 "@type" : "koral:rewrite",
2098 "operation" : "operation:modification",
2099 "src" : "querySerializer",
2100 "scope" : "tree"
Nils Diewald86dad5b2015-01-28 15:09:07 +00002101 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00002102 expect(rewrite.toString()).toEqual('Modification of "tree" by "querySerializer"');
2103 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00002104
Nils Diewald7c8ced22015-04-15 19:21:00 +00002105 it('should be deserialized by docs', function () {
2106 var doc = docClass.create(
Akron712733a2018-04-05 18:17:47 +02002107 undefined,
2108 {
2109 "@type":"koral:doc",
2110 "key":"Titel",
2111 "value":"Baum",
2112 "match":"match:eq"
2113 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00002114
2115 expect(doc.element().classList.contains('doc')).toBeTruthy();
2116 expect(doc.element().classList.contains('rewritten')).toBe(false);
2117
2118 doc = docClass.create(
Akron712733a2018-04-05 18:17:47 +02002119 undefined,
2120 {
2121 "@type":"koral:doc",
2122 "key":"Titel",
2123 "value":"Baum",
2124 "match":"match:eq",
2125 "rewrites" : [
2126 {
2127 "@type" : "koral:rewrite",
2128 "operation" : "operation:modification",
2129 "src" : "querySerializer",
2130 "scope" : "tree"
2131 }
2132 ]
2133 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00002134
2135 expect(doc.element().classList.contains('doc')).toBeTruthy();
2136 expect(doc.element().classList.contains('rewritten')).toBeTruthy();
2137 });
Nils Diewald6283d692015-04-23 20:32:53 +00002138
Akron76c3dd62018-05-29 20:58:27 +02002139 it('should be described in a title attribute', function () {
2140
2141 doc = docClass.create(
2142 undefined,
2143 {
2144 "@type":"koral:doc",
2145 "key":"Titel",
2146 "value":"Baum",
2147 "match":"match:eq",
2148 "rewrites" : [
2149 {
2150 "@type" : "koral:rewrite",
2151 "operation" : "operation:modification",
2152 "src" : "querySerializer",
2153 "scope" : "tree"
2154 },
2155 {
2156 "@type" : "koral:rewrite",
2157 "operation" : "operation:injection",
2158 "src" : "me"
2159 }
2160 ]
2161 });
2162
2163 expect(doc.element().classList.contains('doc')).toBeTruthy();
2164 expect(doc.element().classList.contains('rewritten')).toBeTruthy();
2165 expect(doc.element().lastChild.getAttribute("title")).toEqual("querySerializer: tree (modification)\nme (injection)");
2166 });
2167
2168
Nils Diewald6283d692015-04-23 20:32:53 +00002169 xit('should be deserialized by docGroups', function () {
2170 var docGroup = docGroupClass.create(
Akron712733a2018-04-05 18:17:47 +02002171 undefined,
2172 {
2173 "@type" : "koral:docGroup",
2174 "operation" : "operation:or",
2175 "operands" : [
2176 {
2177 "@type" : "doc",
2178 "key" : "pubDate",
2179 "type" : "type:date",
2180 "value" : "2014-12-05"
2181 },
2182 {
2183 "@type" : "doc",
2184 "key" : "pubDate",
2185 "type" : "type:date",
2186 "value" : "2014-12-06"
2187 }
2188 ],
2189 "rewrites" : [
2190 {
2191 "@type" : "koral:rewrite",
2192 "operation" : "operation:modification",
2193 "src" : "querySerializer",
2194 "scope" : "tree"
2195 }
2196 ]
2197 }
Nils Diewald6283d692015-04-23 20:32:53 +00002198 );
2199
2200 expect(doc.element().classList.contains('docgroup')).toBeTruthy();
2201 expect(doc.element().classList.contains('rewritten')).toBe(false);
2202 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00002203 });
Nils Diewaldf0c4f112015-05-05 12:56:59 +00002204
2205 describe('KorAP.stringValue', function () {
2206 it('should be initializable', function () {
2207 var sv = stringValClass.create();
2208 expect(sv.regex()).toBe(false);
2209 expect(sv.value()).toBe('');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002210
2211 sv = stringValClass.create('Baum');
2212 expect(sv.regex()).toBe(false);
2213 expect(sv.value()).toBe('Baum');
2214
2215 sv = stringValClass.create('Baum', false);
2216 expect(sv.regex()).toBe(false);
2217 expect(sv.value()).toBe('Baum');
2218
2219 sv = stringValClass.create('Baum', true);
2220 expect(sv.regex()).toBe(true);
2221 expect(sv.value()).toBe('Baum');
2222 });
2223
2224 it('should be modifiable', function () {
2225 var sv = stringValClass.create();
2226 expect(sv.regex()).toBe(false);
2227 expect(sv.value()).toBe('');
2228
2229 expect(sv.value('Baum')).toBe('Baum');
2230 expect(sv.value()).toBe('Baum');
2231
2232 expect(sv.regex(true)).toBe(true);
2233 expect(sv.regex()).toBe(true);
2234 });
2235
2236 it('should have a toggleble regex value', function () {
2237 var sv = stringValClass.create();
Nils Diewald7991a3f2015-05-19 14:12:37 +00002238
2239 expect(sv.element().firstChild.value).toBe('');
2240 sv.element().firstChild.value = 'der'
2241 expect(sv.element().firstChild.value).toBe('der');
2242
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002243 expect(sv.regex()).toBe(false);
2244
2245 sv.toggleRegex();
Nils Diewald7991a3f2015-05-19 14:12:37 +00002246 expect(sv.element().firstChild.value).toBe('der');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002247 expect(sv.regex()).toBe(true);
Nils Diewald7991a3f2015-05-19 14:12:37 +00002248 sv.element().firstChild.value = 'derbe'
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002249
2250 sv.toggleRegex();
2251 expect(sv.regex()).toBe(false);
Nils Diewald7991a3f2015-05-19 14:12:37 +00002252 expect(sv.element().firstChild.value).toBe('derbe');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002253 });
2254
2255 it('should have an element', function () {
2256 var sv = stringValClass.create('der');
2257 expect(sv.element().nodeName).toBe('DIV');
2258 expect(sv.element().firstChild.nodeName).toBe('INPUT');
2259 expect(sv.element().firstChild.value).toBe('der');
2260 });
2261
2262 it('should have a classed element', function () {
2263 var sv = stringValClass.create();
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002264 expect(sv.element().classList.contains('regex')).toBe(false);
2265 expect(sv.regex()).toBe(false);
2266 sv.toggleRegex();
2267 expect(sv.element().classList.contains('regex')).toBe(true);
2268 });
2269
2270 it('should be storable', function () {
2271 var sv = stringValClass.create();
2272 var count = 1;
2273 sv.store = function (value, regex) {
Akron712733a2018-04-05 18:17:47 +02002274 expect(regex).toBe(true);
2275 expect(value).toBe('tree');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002276 };
2277 sv.regex(true);
2278 sv.value('tree');
2279 sv.element().lastChild.click();
Nils Diewaldf0c4f112015-05-05 12:56:59 +00002280 });
Akron712733a2018-04-05 18:17:47 +02002281
Nils Diewaldf0c4f112015-05-05 12:56:59 +00002282 });
Akrone4961b12017-05-10 21:04:46 +02002283
Akron712733a2018-04-05 18:17:47 +02002284 describe('KorAP.VC.Menu', function () {
Akrone65a88a2018-04-05 19:14:20 +02002285
2286 var vc;
2287
Akron712733a2018-04-05 18:17:47 +02002288 it('should be initializable', function () {
2289
Akrone65a88a2018-04-05 19:14:20 +02002290 vc = vcClass.create([
Akron712733a2018-04-05 18:17:47 +02002291 ['a', 'text'],
2292 ['b', 'string'],
2293 ['c', 'date']
2294 ]).fromJson();
2295 expect(vc.element().firstChild.classList.contains('unspecified')).toBeTruthy();
2296 expect(vc.element().firstChild.firstChild.tagName).toEqual('SPAN');
2297
2298 // Click on unspecified
2299 vc.element().firstChild.firstChild.click();
2300 expect(vc.element().firstChild.firstChild.tagName).toEqual('UL');
2301
2302 var list = vc.element().firstChild.firstChild;
2303 expect(list.getElementsByTagName("LI")[0].innerText).toEqual('a');
2304 expect(list.getElementsByTagName("LI")[1].innerText).toEqual('b');
2305 expect(list.getElementsByTagName("LI")[2].innerText).toEqual('c');
2306
2307 vc = vcClass.create([
2308 ['d', 'text'],
2309 ['e', 'string'],
2310 ['f', 'date']
2311 ]).fromJson();
2312 expect(vc.element().firstChild.classList.contains('unspecified')).toBeTruthy();
2313 expect(vc.element().firstChild.firstChild.tagName).toEqual('SPAN');
2314
2315 // Click on unspecified
2316 vc.element().firstChild.firstChild.click();
2317 expect(vc.element().firstChild.firstChild.tagName).toEqual('UL');
2318
2319 list = vc.element().firstChild.firstChild;
2320 expect(list.getElementsByTagName("LI")[0].innerText).toEqual('d');
2321 expect(list.getElementsByTagName("LI")[1].innerText).toEqual('e');
2322 expect(list.getElementsByTagName("LI")[2].innerText).toEqual('f');
Akron31d89942018-04-06 16:44:51 +02002323 // blur
2324 document.body.click();
Akron712733a2018-04-05 18:17:47 +02002325 });
Akrone65a88a2018-04-05 19:14:20 +02002326
2327 // Reinitialize to make tests stable
2328 vc = vcClass.create([
2329 ['d', 'text'],
2330 ['e', 'string'],
2331 ['f', 'date']
2332 ]).fromJson();
2333
2334 it('should be clickable on key', function () {
Akron31d89942018-04-06 16:44:51 +02002335 // Click on unspecified
2336 vc.element().firstChild.firstChild.click();
Akrone65a88a2018-04-05 19:14:20 +02002337 // Click on "d"
2338 vc.element().firstChild.firstChild.getElementsByTagName("LI")[0].click();
2339 expect(vc.element().firstChild.firstChild.tagName).toEqual('SPAN');
2340 expect(vc.element().firstChild.firstChild.innerText).toEqual('d');
2341 expect(vc.element().firstChild.children[1].innerText).toEqual('eq');
2342 expect(vc.element().firstChild.children[1].getAttribute('data-type')).toEqual('text');
Akron31d89942018-04-06 16:44:51 +02002343 // blur
2344 document.body.click();
Akrone65a88a2018-04-05 19:14:20 +02002345 });
2346
Akron31d89942018-04-06 16:44:51 +02002347 it('should be clickable on operation for text', function () {
2348 // Click on "d" (or unspecified)
2349 vc.element().firstChild.firstChild.click();
2350
2351 // Choose "d"
2352 vc.element().firstChild.firstChild.getElementsByTagName("LI")[0].click();
2353
2354 // Click on matchop
Akrone65a88a2018-04-05 19:14:20 +02002355 vc.element().firstChild.children[1].click();
Akron31d89942018-04-06 16:44:51 +02002356
Akrone65a88a2018-04-05 19:14:20 +02002357 expect(vc.element().firstChild.children[1].tagName).toEqual('UL');
Akron31d89942018-04-06 16:44:51 +02002358
Akrone65a88a2018-04-05 19:14:20 +02002359 var ul = vc.element().firstChild.children[1];
2360 expect(ul.getElementsByTagName('li')[0].innerText).toEqual("eq");
2361 expect(ul.getElementsByTagName('li')[1].innerText).toEqual("ne");
2362 expect(ul.getElementsByTagName('li')[2].innerText).toEqual("contains");
2363 expect(ul.getElementsByTagName('li')[3].innerText).toEqual("containsnot");
2364 expect(ul.getElementsByTagName('li')[4]).toBeUndefined();
Akron31d89942018-04-06 16:44:51 +02002365
2366 // Choose "contains"
2367 ul.getElementsByTagName('li')[2].click();
2368 expect(vc.element().firstChild.children[1].tagName).toEqual("SPAN");
2369 expect(vc.element().firstChild.children[1].innerText).toEqual("contains");
2370 // blur
2371 document.body.click();
Akrone65a88a2018-04-05 19:14:20 +02002372 })
Akron31d89942018-04-06 16:44:51 +02002373
2374 it('should be clickable on operation for string', function () {
2375 // Click on "d" (or unspecified)
2376 vc.element().firstChild.firstChild.click();
2377
2378 // Choose "e"
2379 vc.element().firstChild.firstChild.getElementsByTagName("LI")[1].click();
2380
2381 // As a consequence the matchoperator may no longer
2382 // be valid and needs to be re-evaluated
2383 var fc = vc.element().firstChild;
2384 expect(fc.firstChild.tagName).toEqual('SPAN');
2385 expect(fc.firstChild.innerText).toEqual('e');
2386 expect(fc.children[1].innerText).toEqual('eq');
2387 expect(fc.children[1].getAttribute('data-type')).toEqual('string');
2388
2389 vc.element().firstChild.children[1].click();
2390
2391 expect(vc.element().firstChild.children[1].tagName).toEqual('UL');
2392
2393 var ul = vc.element().firstChild.children[1];
2394 expect(ul.getElementsByTagName('li')[0].innerText).toEqual("eq");
2395 expect(ul.getElementsByTagName('li')[1].innerText).toEqual("ne");
2396 expect(ul.getElementsByTagName('li')[2]).toBeUndefined();
2397
2398 // Choose "ne"
2399 ul.getElementsByTagName('li')[1].click();
2400 expect(vc.element().firstChild.children[1].tagName).toEqual("SPAN");
2401 expect(vc.element().firstChild.children[1].innerText).toEqual("ne");
Akron8db5e3a2018-05-28 19:25:26 +02002402
2403 // Click on text
2404 expect(vc.element().firstChild.children[2].innerText).toEqual("⋯");
2405 vc.element().firstChild.children[2].click();
2406
2407 // Blur text element
2408 expect(vc.element().firstChild.children[2].firstChild.value).toEqual('');
2409
Akron31d89942018-04-06 16:44:51 +02002410 // blur
2411 document.body.click();
2412 });
2413
2414 it('should be clickable on operation for date', function () {
2415
2416 // Replay matchop check - so it's guaranteed that "ne" is chosen
2417 // Click on "e" (or unspecified)
2418 vc.element().firstChild.firstChild.click();
2419 // Rechoose "e"
2420 vc.element().firstChild.firstChild.getElementsByTagName("LI")[1].click();
2421 // Click on matchop
2422 vc.element().firstChild.children[1].click();
2423 // Choose "ne"
2424 vc.element().firstChild.children[1].getElementsByTagName('li')[1].click();
2425 expect(vc.element().firstChild.children[1].innerText).toEqual("ne");
2426
2427 // Click on "e"
2428 vc.element().firstChild.firstChild.click();
2429 // Choose "f"
2430 vc.element().firstChild.firstChild.getElementsByTagName("LI")[2].click();
2431
2432 // The matchoperator should still be "ne" as this is valid for dates as well (now)
2433 var fc = vc.element().firstChild;
2434 expect(fc.firstChild.tagName).toEqual('SPAN');
2435 expect(fc.firstChild.innerText).toEqual('f');
2436 expect(fc.children[1].innerText).toEqual('ne');
2437 // blur
2438 document.body.click();
2439 });
Akronddc98a72018-04-06 17:33:52 +02002440
2441
2442 // Check json deserialization
2443 it('should be initializable', function () {
2444 vc = vcClass.create([
2445 ['a', 'text'],
2446 ['b', 'string'],
2447 ['c', 'date']
2448 ]).fromJson({
2449 "@type" : "koral:doc",
2450 "key":"a",
2451 "value":"Baum"
2452 });
2453
2454 expect(vc.element().firstChild.children[1].getAttribute('data-type')).toEqual('text');
2455 });
Akron712733a2018-04-05 18:17:47 +02002456 });
2457
2458
Akrone4961b12017-05-10 21:04:46 +02002459 // Check prefix
2460 describe('KorAP.VC.Prefix', function () {
2461
2462 it('should be initializable', function () {
2463 var p = prefixClass.create();
2464 expect(p.element().classList.contains('pref')).toBeTruthy();
2465 expect(p.isSet()).not.toBeTruthy();
2466 });
2467
2468
2469 it('should be clickable', function () {
2470 var vc = vcClass.create([
Akron712733a2018-04-05 18:17:47 +02002471 ['a', null],
2472 ['b', null],
2473 ['c', null]
2474 ]).fromJson();
Akrone4961b12017-05-10 21:04:46 +02002475 expect(vc.element().firstChild.classList.contains('unspecified')).toBeTruthy();
2476
2477 // This should open up the menu
2478 vc.element().firstChild.firstChild.click();
2479 expect(vc.element().firstChild.firstChild.tagName).toEqual('UL');
2480
2481 KorAP._vcKeyMenu._prefix.clear();
2482 KorAP._vcKeyMenu._prefix.add('x');
2483
2484 var prefElement = vc.element().querySelector('span.pref');
2485 expect(prefElement.innerText).toEqual('x');
2486
2487 // This should add key 'x' to VC
2488 prefElement.click();
2489
2490 expect(vc.element().firstChild.classList.contains('doc')).toBeTruthy();
2491 expect(vc.element().firstChild.firstChild.className).toEqual('key');
2492 expect(vc.element().firstChild.firstChild.innerText).toEqual('x');
2493 });
2494 });
Nils Diewald2fe12e12015-03-06 16:47:06 +00002495});