blob: e7cc4fbf4cdb38c96b0886d686c53015b090c33b [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 */
Akrondd5c6d32018-08-17 14:12:58 +02004define([
5 'vc',
6 'vc/doc',
7 'vc/menu',
8 'vc/prefix',
9 'vc/docgroup',
10 'vc/docgroupref',
11 'vc/unspecified',
12 'vc/operators',
13 'vc/rewrite',
Akron68d28322018-08-27 15:02:42 +020014 'vc/stringval',
15 'vc/fragment'
Akrondd5c6d32018-08-17 14:12:58 +020016], function (vcClass,
17 docClass,
18 menuClass,
19 prefixClass,
20 docGroupClass,
21 docGroupRefClass,
22 unspecifiedClass,
23 operatorsClass,
24 rewriteClass,
Akron68d28322018-08-27 15:02:42 +020025 stringValClass,
26 fragmentClass) {
Nils Diewald6ac292b2015-01-15 21:33:21 +000027
Akrondd5c6d32018-08-17 14:12:58 +020028 KorAP._vcKeyMenu = undefined;
29
hebastaa0282be2018-12-05 16:58:00 +010030
Nils Diewald7c8ced22015-04-15 19:21:00 +000031 // Helper method for building factories
32 buildFactory = function (objClass, defaults) {
33 return {
34 create : function (overwrites) {
Akron712733a2018-04-05 18:17:47 +020035 var newObj = {};
36 for (var prop in defaults) {
37 newObj[prop] = defaults[prop];
38 };
39 for (var prop in overwrites) {
40 newObj[prop] = overwrites[prop];
41 };
42 return objClass.create().fromJson(newObj);
Nils Diewald7c8ced22015-04-15 19:21:00 +000043 }
Nils Diewald0b6c0412014-12-19 03:55:57 +000044 }
Nils Diewald7c8ced22015-04-15 19:21:00 +000045 };
Nils Diewald0b6c0412014-12-19 03:55:57 +000046
Nils Diewald7c8ced22015-04-15 19:21:00 +000047 function _andOn (obj) {
Akrond141a362018-07-10 18:12:13 +020048 KorAP._and.apply(obj);
Nils Diewald7c8ced22015-04-15 19:21:00 +000049 };
Nils Diewald52f7eb12015-01-12 17:30:04 +000050
Nils Diewald7c8ced22015-04-15 19:21:00 +000051 function _orOn (obj) {
Akrond141a362018-07-10 18:12:13 +020052 KorAP._or.apply(obj);
Nils Diewald7c8ced22015-04-15 19:21:00 +000053 };
Nils Diewald52f7eb12015-01-12 17:30:04 +000054
Nils Diewald7c8ced22015-04-15 19:21:00 +000055 function _delOn (obj) {
Akrond141a362018-07-10 18:12:13 +020056 KorAP._delete.apply(obj);
Nils Diewald7c8ced22015-04-15 19:21:00 +000057 };
Nils Diewald52f7eb12015-01-12 17:30:04 +000058
Nils Diewald7c8ced22015-04-15 19:21:00 +000059 var demoFactory = buildFactory(vcClass, {
60 "@type":"koral:docGroup",
61 "operation":"operation:or",
62 "operands":[
Nils Diewaldf219eb82015-01-07 20:15:42 +000063 {
Akron712733a2018-04-05 18:17:47 +020064 "@type":"koral:docGroup",
65 "operation":"operation:and",
66 "operands":[
Nils Diewald7c8ced22015-04-15 19:21:00 +000067 {
68 "@type":"koral:doc",
69 "key":"Titel",
70 "value":"Baum",
71 "match":"match:eq"
72 },
73 {
74 "@type":"koral:doc",
75 "key":"Veröffentlichungsort",
76 "value":"hihi",
77 "match":"match:eq"
78 },
79 {
80 "@type":"koral:docGroup",
81 "operation":"operation:or",
82 "operands":[
83 {
Akron712733a2018-04-05 18:17:47 +020084 "@type":"koral:doc",
85 "key":"Titel",
86 "value":"Baum",
87 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +000088 },
89 {
Akron712733a2018-04-05 18:17:47 +020090 "@type":"koral:doc",
91 "key":"Veröffentlichungsort",
92 "value":"hihi",
93 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +000094 }
95 ]
96 }
Akron712733a2018-04-05 18:17:47 +020097 ]
Nils Diewaldf219eb82015-01-07 20:15:42 +000098 },
99 {
Akron712733a2018-04-05 18:17:47 +0200100 "@type":"koral:doc",
101 "key":"Untertitel",
102 "value":"huhu",
103 "match":"match:contains"
Nils Diewaldf219eb82015-01-07 20:15:42 +0000104 }
105 ]
106 });
107
Nils Diewald7c8ced22015-04-15 19:21:00 +0000108 describe('KorAP.Doc', function () {
109 // Create example factories
110 var stringFactory = buildFactory(docClass, {
111 "key" : "author",
112 "value" : "Max Birkendale",
Akron31d89942018-04-06 16:44:51 +0200113 "type" : "type:string",
Nils Diewald7c8ced22015-04-15 19:21:00 +0000114 "@type" : "koral:doc"
115 });
116
117 // Create example factories
Akron712733a2018-04-05 18:17:47 +0200118 var textFactory = buildFactory(docClass, {
119 "key" : "author",
120 "value" : "Birkendale",
Akron31d89942018-04-06 16:44:51 +0200121 "type" : "type:string",
Akron712733a2018-04-05 18:17:47 +0200122 "match" : "match:contains",
123 "@type" : "koral:doc"
124 });
125
126 // Create example factories
Nils Diewald7c8ced22015-04-15 19:21:00 +0000127 var dateFactory = buildFactory(docClass, {
128 "key" : "pubDate",
129 "type" : "type:date",
130 "match" : "match:eq",
131 "value" : "2014-11-05",
132 "@type" : "koral:doc"
133 });
134
135 // Create example factories
136 var regexFactory = buildFactory(docClass, {
137 "key" : "title",
138 "type" : "type:regex",
139 "value" : "[^b]ee.+?",
140 "@type" : "koral:doc"
141 });
142
143 it('should be initializable', function () {
144 var doc = docClass.create();
145 expect(doc.matchop()).toEqual('eq');
146 expect(doc.key()).toBeUndefined();
147 expect(doc.value()).toBeUndefined();
148 expect(doc.type()).toEqual("string");
hebastaa0282be2018-12-05 16:58:00 +0100149 expect(doc.incomplete()).toBeTruthy();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000150 });
151
152 it('should be definable', function () {
153
154 // Empty doc
155 var doc = docClass.create();
156
157 // Set values
158 doc.key("title");
Akron8db5e3a2018-05-28 19:25:26 +0200159
Nils Diewald7c8ced22015-04-15 19:21:00 +0000160 doc.value("Der alte Mann");
161 expect(doc.matchop()).toEqual('eq');
162 expect(doc.key()).toEqual("title");
163 expect(doc.type()).toEqual("string");
164 expect(doc.value()).toEqual("Der alte Mann");
hebastaa0282be2018-12-05 16:58:00 +0100165 expect(doc.incomplete()).toBeFalsy();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000166 });
167
168
169 it('should deserialize JSON-LD string', function () {
170 var doc;
171
172 // String default
173 doc = stringFactory.create();
174 expect(doc.matchop()).toEqual('eq');
175 expect(doc.key()).toEqual("author");
176 expect(doc.type()).toEqual("string");
177 expect(doc.value()).toEqual("Max Birkendale");
hebastaa0282be2018-12-05 16:58:00 +0100178 expect(doc.incomplete()).toBeFalsy();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000179
180 // No valid string
181 doc = stringFactory.create({
Akron712733a2018-04-05 18:17:47 +0200182 value : undefined
Nils Diewald7c8ced22015-04-15 19:21:00 +0000183 });
184 expect(doc).toBeUndefined();
185
186 // No valid string
187 doc = stringFactory.create({
Akron712733a2018-04-05 18:17:47 +0200188 value : { "foo" : "bar" }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000189 });
190 expect(doc).toBeUndefined();
191
192 // Change match type
193 doc = stringFactory.create({
Akron712733a2018-04-05 18:17:47 +0200194 "match" : "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000195 });
196
197 expect(doc.matchop()).toEqual('ne');
198 expect(doc.key()).toEqual("author");
199 expect(doc.type()).toEqual("string");
200 expect(doc.value()).toEqual("Max Birkendale");
hebastaa0282be2018-12-05 16:58:00 +0100201 expect(doc.incomplete()).toBeFalsy();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000202
203 // Invalid match type
204 doc = stringFactory.create({
Akron712733a2018-04-05 18:17:47 +0200205 "match" : { "foo" : "bar" }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000206 });
207 expect(doc).toBeUndefined();
208 });
209
210 it('should deserialize JSON-LD regex', function () {
211 var doc = regexFactory.create();
212 expect(doc.key()).toEqual("title");
213 expect(doc.type()).toEqual("regex");
214 expect(doc.value()).toEqual("[^b]ee.+?");
215 expect(doc.matchop()).toEqual('eq');
216
217 // change matcher
218 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200219 match : "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000220 });
221 expect(doc.matchop()).toEqual('ne');
Akronea4e9052017-07-06 16:12:05 +0200222 expect(doc.rewrites()).toBeUndefined();
hebastaa0282be2018-12-05 16:58:00 +0100223 expect(doc.incomplete()).toBeFalsy();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000224
225 // Invalid matcher
226 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200227 match : "match:chook"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000228 });
Akronea4e9052017-07-06 16:12:05 +0200229 expect(doc.matchop()).toEqual('eq');
230 expect(doc.rewrites()).toBeDefined();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000231
232 // Invalid regex
233 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200234 value : "[^b"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000235 });
236 expect(doc).toBeUndefined();
237 });
238
239 it('should deserialize JSON-LD date', function () {
240
241 // Normal date
242 doc = dateFactory.create({});
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-11-05");
248
249 // Short date 1
250 doc = dateFactory.create({
Akron712733a2018-04-05 18:17:47 +0200251 "value" : "2014-11"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000252 });
253
254 expect(doc.matchop()).toEqual('eq');
255 expect(doc.key()).toEqual("pubDate");
256 expect(doc.type()).toEqual("date");
257 expect(doc.value()).toEqual("2014-11");
258
259 // Short date 2
260 doc = dateFactory.create({
Akron712733a2018-04-05 18:17:47 +0200261 "value" : "2014"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000262 });
263
264 expect(doc.matchop()).toEqual('eq');
265 expect(doc.key()).toEqual("pubDate");
266 expect(doc.type()).toEqual("date");
267 expect(doc.value()).toEqual("2014");
268
269 // Invalid date!
270 doc = dateFactory.create({
Akron712733a2018-04-05 18:17:47 +0200271 "value" : "2014-11-050"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000272 });
273 expect(doc).toBeUndefined();
274
275 // Invalid matcher!
276 doc = dateFactory.create({
Akron31d89942018-04-06 16:44:51 +0200277 "match" : "match:contains",
Nils Diewald7c8ced22015-04-15 19:21:00 +0000278 });
Akronea4e9052017-07-06 16:12:05 +0200279 expect(doc).toBeDefined();
280 expect(doc.rewrites()).toBeDefined();
281 expect(doc.matchop()).toEqual('eq');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000282 });
283
284 it('should be serializale to JSON', function () {
285
286 // Empty doc
287 var doc = docClass.create();
288 expect(doc.toJson()).toEqual(jasmine.any(Object));
289
290 // Serialize string
291 doc = stringFactory.create();
292 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200293 "@type" : "koral:doc",
294 "type" : "type:string",
295 "key" : "author",
296 "value" : "Max Birkendale",
297 "match" : "match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000298 }));
299
300 // Serialize regex
301 doc = regexFactory.create();
302 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200303 "@type" : "koral:doc",
304 "type" : "type:regex",
305 "value" : "[^b]ee.+?",
306 "match" : "match:eq",
307 "key" : 'title'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000308 }));
309
310 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200311 match: "match:ne"
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:regex",
316 "value" : "[^b]ee.+?",
317 "match" : "match:ne",
318 "key" : 'title'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000319 }));
320
321 doc = dateFactory.create();
322 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200323 "@type" : "koral:doc",
324 "type" : "type:date",
325 "value" : "2014-11-05",
326 "match" : "match:eq",
327 "key" : 'pubDate'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000328 }));
329
330 doc = dateFactory.create({
Akron712733a2018-04-05 18:17:47 +0200331 value : "2014"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000332 });
333 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200334 "@type" : "koral:doc",
335 "type" : "type:date",
336 "value" : "2014",
337 "match" : "match:eq",
338 "key" : 'pubDate'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000339 }));
340 });
341
342
343 it('should be serializale to String', function () {
344 // Empty doc
345 var doc = docClass.create();
Akronebc96662018-08-29 17:36:20 +0200346
Nils Diewald7c8ced22015-04-15 19:21:00 +0000347 expect(doc.toQuery()).toEqual("");
348
349 // Serialize string
350 doc = stringFactory.create();
351 expect(doc.toQuery()).toEqual('author = "Max Birkendale"');
352
hebastaa0282be2018-12-05 16:58:00 +0100353 // Check for incompletion
354 expect(doc.incomplete()).toBeFalsy();
355 doc.value("");
356 expect(doc.incomplete()).toBeTruthy();
357 expect(doc.toQuery()).toEqual('');
358
Nils Diewald7c8ced22015-04-15 19:21:00 +0000359 // Serialize string with quotes
360 doc = stringFactory.create({ "value" : 'Max "Der Coole" Birkendate'});
361 expect(doc.toQuery()).toEqual('author = "Max \\"Der Coole\\" Birkendate"');
362
363 // Serialize regex
364 doc = regexFactory.create();
365 expect(doc.toQuery()).toEqual('title = /[^b]ee.+?/');
366
367 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200368 match: "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000369 });
370 expect(doc.toQuery()).toEqual('title != /[^b]ee.+?/');
371
Akron8778f5d2017-06-30 21:25:55 +0200372 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200373 value: "WPD/AAA/00001"
Akron8778f5d2017-06-30 21:25:55 +0200374 });
375 expect(doc.toQuery()).toEqual('title = /WPD\\/AAA\\/00001/');
376
Nils Diewald7c8ced22015-04-15 19:21:00 +0000377 doc = dateFactory.create();
378 expect(doc.toQuery()).toEqual('pubDate in 2014-11-05');
379
380 doc = dateFactory.create({
Akron712733a2018-04-05 18:17:47 +0200381 value : "2014"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000382 });
383 expect(doc.toQuery()).toEqual('pubDate in 2014');
384 });
385 });
386
387
388 describe('KorAP.DocGroup', function () {
389 // Create example factories
390 var docFactory = buildFactory(
391 docClass,
Nils Diewaldf219eb82015-01-07 20:15:42 +0000392 {
Akron712733a2018-04-05 18:17:47 +0200393 "@type" : "koral:doc",
394 "match":"match:eq",
395 "key" : "author",
396 "value" : "Max Birkendale"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000397 }
398 );
399
400 var docGroupFactory = buildFactory(
401 docGroupClass, {
Akron712733a2018-04-05 18:17:47 +0200402 "@type" : "koral:docGroup",
403 "operation" : "operation:and",
404 "operands" : [
405 docFactory.create().toJson(),
406 docFactory.create({
407 "key" : "pubDate",
408 "type" : "type:date",
409 "value" : "2014-12-05"
410 }).toJson()
411 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000412 });
413
414 it('should be initializable', function () {
415 // Create empty group
416 var docGroup = docGroupClass.create();
417 expect(docGroup.operation()).toEqual('and');
418
419 // Create empty group
420 docGroup = docGroupClass.create();
421 docGroup.operation('or');
422 expect(docGroup.operation()).toEqual('or');
423 });
424
425 it('should be definable', function () {
426
427 // Empty group
428 var docGroup = docGroupClass.create();
429 expect(docGroup.operation()).toEqual('and');
430
431 // Set values
432 docGroup.operation("or");
433 expect(docGroup.operation()).toEqual('or');
434
435 // Set invalid values
436 docGroup.operation("hui");
437 expect(docGroup.operation()).toEqual('or');
438 });
439
440 it('should be deserializable', function () {
Akronadab5e52018-08-20 13:50:53 +0200441
442 // Reset list
443 KorAP._vcKeyMenu = menuClass.create([['referTo','ref']]);
444
Nils Diewald7c8ced22015-04-15 19:21:00 +0000445 var docGroup = docGroupFactory.create();
446 expect(docGroup.operation()).toEqual("and");
447 expect(docGroup.operands().length).toEqual(2);
448
449 var op1 = docGroup.getOperand(0);
450 expect(op1.type()).toEqual("string");
451 expect(op1.key()).toEqual("author");
452 expect(op1.value()).toEqual("Max Birkendale");
453 expect(op1.matchop()).toEqual("eq");
454
455 var op2 = docGroup.getOperand(1);
456 expect(op2.type()).toEqual("date");
457 expect(op2.key()).toEqual("pubDate");
458 expect(op2.value()).toEqual("2014-12-05");
459 expect(op2.matchop()).toEqual("eq");
460
461 // Append empty group
462 var newGroup = docGroup.append(docGroupClass.create());
463 newGroup.operation('or');
464 newGroup.append(docFactory.create());
465 newGroup.append(docFactory.create({
Akron712733a2018-04-05 18:17:47 +0200466 "type" : "type:regex",
467 "key" : "title",
468 "value" : "^e.+?$",
469 "match" : "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000470 }));
471
472 expect(docGroup.operation()).toEqual("and");
473 expect(docGroup.operands().length).toEqual(3);
474
475 var op1 = docGroup.getOperand(0);
476 expect(op1.ldType()).toEqual("doc");
477 expect(op1.type()).toEqual("string");
478 expect(op1.key()).toEqual("author");
479 expect(op1.value()).toEqual("Max Birkendale");
480 expect(op1.matchop()).toEqual("eq");
481
482 var op2 = docGroup.getOperand(1);
483 expect(op2.ldType()).toEqual("doc");
484 expect(op2.type()).toEqual("date");
485 expect(op2.key()).toEqual("pubDate");
486 expect(op2.value()).toEqual("2014-12-05");
487 expect(op2.matchop()).toEqual("eq");
488
489 var op3 = docGroup.getOperand(2);
490 expect(op3.ldType()).toEqual("docGroup");
491 expect(op3.operation()).toEqual("or");
Akronadab5e52018-08-20 13:50:53 +0200492
Nils Diewald7c8ced22015-04-15 19:21:00 +0000493 var op4 = op3.getOperand(0);
494 expect(op4.ldType()).toEqual("doc");
495 expect(op4.type()).toEqual("string");
496 expect(op4.key()).toEqual("author");
497 expect(op4.value()).toEqual("Max Birkendale");
498 expect(op4.matchop()).toEqual("eq");
499
500 var op5 = op3.getOperand(1);
501 expect(op5.ldType()).toEqual("doc");
502 expect(op5.type()).toEqual("regex");
503 expect(op5.key()).toEqual("title");
504 expect(op5.value()).toEqual("^e.+?$");
505 expect(op5.matchop()).toEqual("ne");
506 });
507
508 it('should be serializable to JSON', function () {
509 var docGroup = docGroupFactory.create();
510
511 expect(docGroup.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200512 "@type" : "koral:docGroup",
513 "operation" : "operation:and",
514 "operands" : [
515 {
516 "@type": 'koral:doc',
517 "key" : 'author',
518 "match": 'match:eq',
519 "value": 'Max Birkendale',
520 "type": 'type:string'
521 },
522 {
523 "@type": 'koral:doc',
524 "key": 'pubDate',
525 "match": 'match:eq',
526 "value": '2014-12-05',
527 "type": 'type:date'
528 }
529 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000530 }));
531 });
Nils Diewalde15b7a22015-01-09 21:50:21 +0000532
Nils Diewald7c8ced22015-04-15 19:21:00 +0000533 it('should be serializable to String', function () {
534 var docGroup = docGroupFactory.create();
535 expect(docGroup.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +0200536 'author = "Max Birkendale" & pubDate in 2014-12-05'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000537 );
Nils Diewald52f7eb12015-01-12 17:30:04 +0000538
Nils Diewald7c8ced22015-04-15 19:21:00 +0000539 docGroup = docGroupFactory.create({
Akron712733a2018-04-05 18:17:47 +0200540 "@type" : "koral:docGroup",
541 "operation" : "operation:or",
542 "operands" : [
543 {
544 "@type": 'koral:doc',
545 "key" : 'author',
546 "match": 'match:eq',
547 "value": 'Max Birkendale',
548 "type": 'type:string'
549 },
550 {
551 "@type" : "koral:docGroup",
552 "operation" : "operation:and",
553 "operands" : [
554 {
555 "@type": 'koral:doc',
556 "key": 'pubDate',
557 "match": 'match:geq',
558 "value": '2014-05-12',
559 "type": 'type:date'
560 },
561 {
562 "@type": 'koral:doc',
563 "key": 'pubDate',
564 "match": 'match:leq',
565 "value": '2014-12-05',
566 "type": 'type:date'
567 },
568 {
569 "@type": 'koral:doc',
570 "key": 'foo',
571 "match": 'match:ne',
572 "value": '[a]?bar',
573 "type": 'type:regex'
574 }
575 ]
576 }
577 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000578 });
579 expect(docGroup.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +0200580 'author = "Max Birkendale" | ' +
581 '(pubDate since 2014-05-12 & ' +
582 'pubDate until 2014-12-05 & foo != /[a]?bar/)'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000583 );
hebastaa0282be2018-12-05 16:58:00 +0100584
585
586 // Check for incompletion and only serialize complete operands
587 expect(docGroup.incomplete()).toBeFalsy();
588 var op1 = docGroup.getOperand(0);
589 op1.value("");
590 expect(docGroup.incomplete()).toBeFalsy();
591 expect(docGroup.toQuery()).toEqual(
592 '(pubDate since 2014-05-12 & ' +
593 'pubDate until 2014-12-05 & foo != /[a]?bar/)'
594 );
Nils Diewald7c8ced22015-04-15 19:21:00 +0000595 });
596 });
597
Akronb19803c2018-08-16 16:39:42 +0200598 describe('KorAP.DocGroupRef', function () {
599 // Create example factories
600 var docRefFactory = buildFactory(
601 docGroupRefClass,
602 {
603 "@type" : "koral:docGroupRef",
604 "ref" : "@max/myCorpus"
605 }
606 );
607
608 it('should be initializable', function () {
609 var vcRef = docGroupRefClass.create();
610 expect(vcRef.ref()).toBeUndefined();
611 });
612
613 it('should be definable', function () {
614 var vcRef = docGroupRefClass.create();
615 vcRef.ref("@peter/mycorpus");
616 expect(vcRef.ref()).toEqual("@peter/mycorpus");
617 vcRef.ref("@peter/mycorpus2");
618 expect(vcRef.ref()).toEqual("@peter/mycorpus2");
619 });
620
621 it('should deserialize JSON-LD string', function () {
622 var vcRef = docRefFactory.create();
623 expect(vcRef.ref()).toEqual("@max/myCorpus");
624 });
625
626 it('should serialize to JSON-LD', function () {
627 var vcRef = docRefFactory.create();
628 expect(vcRef.toJson()).toEqual(jasmine.objectContaining({
629 "@type" : "koral:docGroupRef",
630 "ref":"@max/myCorpus"
631 }));
632
633 vcRef.ref("@peter/myCorpus2");
634 expect(vcRef.toJson()).toEqual(jasmine.objectContaining({
635 "@type" : "koral:docGroupRef",
636 "ref":"@peter/myCorpus2"
637 }));
638 });
639
640 it('should serialize to a query', function () {
641 var vcRef = docRefFactory.create();
642 expect(vcRef.toQuery()).toEqual(
643 "referTo \"@max/myCorpus\""
644 );
645
646 vcRef.ref("@peter/myCorpus2");
647 expect(vcRef.toQuery()).toEqual(
648 "referTo \"@peter/myCorpus2\""
649 );
hebastaa0282be2018-12-05 16:58:00 +0100650
651 // Check for incompletion and only serialize complete operands
652 expect(vcRef.incomplete()).toBeFalsy();
653 vcRef.ref("");
654 expect(vcRef.incomplete()).toBeTruthy();
655 expect(vcRef.toQuery()).toEqual("");
Akronb19803c2018-08-16 16:39:42 +0200656 });
657 });
658
659
Nils Diewald7c8ced22015-04-15 19:21:00 +0000660 describe('KorAP.UnspecifiedDoc', function () {
661 it('should be initializable', function () {
662 var doc = unspecifiedClass.create();
663 var docElement = doc.element();
664 expect(docElement.getAttribute('class')).toEqual('doc unspecified');
Akronebc96662018-08-29 17:36:20 +0200665 expect(docElement.firstChild.firstChild.data).toEqual(KorAP.Locale.EMPTY);
666 expect(docElement.lastChild.lastChild.data).toEqual(KorAP.Locale.EMPTY);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000667 expect(doc.toQuery()).toEqual('');
hebastaa0282be2018-12-05 16:58:00 +0100668 expect(doc.incomplete()).toBeTruthy();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000669
670 // Only removable
671 expect(docElement.lastChild.children.length).toEqual(0);
672 });
673
Akrond141a362018-07-10 18:12:13 +0200674 it('should be removable, when no root', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000675 var docGroup = docGroupClass.create();
676 docGroup.operation('or');
677 expect(docGroup.operation()).toEqual('or');
678
679 docGroup.append({
Akron712733a2018-04-05 18:17:47 +0200680 "@type": 'koral:doc',
681 "key": 'pubDate',
682 "match": 'match:eq',
683 "value": '2014-12-05',
684 "type": 'type:date'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000685 });
686
687 // Add unspecified object
688 docGroup.append();
689
Akrond141a362018-07-10 18:12:13 +0200690 var parent = document.createElement('div');
691 parent.appendChild(docGroup.element());
692
Nils Diewald7c8ced22015-04-15 19:21:00 +0000693 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
694 expect(docGroup.element().children[0].getAttribute('class')).toEqual('doc');
695
696 var unspec = docGroup.element().children[1];
697 expect(unspec.getAttribute('class')).toEqual('doc unspecified');
698
Akronb19803c2018-08-16 16:39:42 +0200699 // Only unspec and delete
700 expect(unspec.children.length).toEqual(2);
701
Nils Diewald7c8ced22015-04-15 19:21:00 +0000702 // Removable
703 expect(unspec.lastChild.children.length).toEqual(1);
704 expect(unspec.lastChild.children[0].getAttribute('class')).toEqual('delete');
705 });
706
Akrond141a362018-07-10 18:12:13 +0200707
Nils Diewald7c8ced22015-04-15 19:21:00 +0000708 it('should be replaceable by a doc', function () {
709 var doc = unspecifiedClass.create();
710 expect(doc.ldType()).toEqual("non");
711 // No parent, therefor not updateable
712 expect(doc.key("baum")).toBeNull();
713
714 var docGroup = docGroupClass.create();
715 docGroup.operation('or');
716 expect(docGroup.operation()).toEqual('or');
717
718 docGroup.append({
Akron712733a2018-04-05 18:17:47 +0200719 "@type": 'koral:doc',
720 "key": 'pubDate',
721 "match": 'match:eq',
722 "value": '2014-12-05',
723 "type": 'type:date'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000724 });
725
726 expect(docGroup.toQuery()).toEqual("pubDate in 2014-12-05");
727 docGroup.append();
728
729 expect(docGroup.getOperand(0).ldType()).toEqual("doc");
730 expect(docGroup.getOperand(1).ldType()).toEqual("non");
731
732 var op = docGroup.getOperand(1).element().lastChild;
Akron0b489ad2018-02-02 16:49:32 +0100733 expect(op.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000734 expect(op.children[0].getAttribute('class')).toEqual('delete');
735 expect(op.children.length).toEqual(1);
736
737 // Replace unspecified doc
738 expect(docGroup.getOperand(1).key("name")).not.toBeNull();
739 expect(docGroup.getOperand(1).ldType()).toEqual("doc");
740 expect(docGroup.getOperand(1).key()).toEqual("name");
Akron55a343b2018-04-06 19:57:36 +0200741 expect(docGroup.getOperand(1).value()).toBeUndefined();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000742
Akronb19803c2018-08-16 16:39:42 +0200743 expect(docGroup.getOperand(1).element().children.length).toEqual(4);
744
Nils Diewald7c8ced22015-04-15 19:21:00 +0000745 op = docGroup.getOperand(1).element().lastChild;
Akron0b489ad2018-02-02 16:49:32 +0100746 expect(op.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000747 expect(op.children[0].getAttribute('class')).toEqual('and');
748 expect(op.children[1].getAttribute('class')).toEqual('or');
749 expect(op.children[2].getAttribute('class')).toEqual('delete');
Akronb19803c2018-08-16 16:39:42 +0200750
Nils Diewald7c8ced22015-04-15 19:21:00 +0000751 expect(op.children.length).toEqual(3);
752
753 docGroup.getOperand(1).value("Pachelbel");
754 expect(docGroup.getOperand(1).value()).toEqual("Pachelbel");
755 expect(docGroup.getOperand(1).type()).toEqual("string");
756 expect(docGroup.getOperand(1).matchop()).toEqual("eq");
757
758 // Specified!
759 expect(docGroup.toQuery()).toEqual('pubDate in 2014-12-05 | name = "Pachelbel"');
760 });
Akronb19803c2018-08-16 16:39:42 +0200761
Nils Diewald7c8ced22015-04-15 19:21:00 +0000762 it('should be replaceable on root', function () {
hebastaa0282be2018-12-05 16:58:00 +0100763
Nils Diewald6283d692015-04-23 20:32:53 +0000764 var vc = vcClass.create();
hebastaa0282be2018-12-05 16:58:00 +0100765 KorAP.vc = vc;
Nils Diewald7c8ced22015-04-15 19:21:00 +0000766 expect(vc.toQuery()).toEqual("");
767
768 expect(vc.root().ldType()).toEqual("non");
769
770 // No operators on root
771 op = vc.root().element().lastChild;
Akronebc96662018-08-29 17:36:20 +0200772 expect(op.lastChild.textContent).toEqual(KorAP.Locale.EMPTY);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000773
774 // Replace
775 expect(vc.root().key("baum")).not.toBeNull();
776 expect(vc.root().ldType()).toEqual("doc");
777
778 op = vc.root().element().lastChild;
Akron0b489ad2018-02-02 16:49:32 +0100779 expect(op.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000780 expect(op.children[0].getAttribute('class')).toEqual('and');
781 expect(op.children[1].getAttribute('class')).toEqual('or');
782 expect(op.children[2].getAttribute('class')).toEqual('delete');
783 expect(op.children.length).toEqual(3);
784 });
Akron55a343b2018-04-06 19:57:36 +0200785
786 it('should be clickable', function () {
787 var vc = vcClass.create([
788 ["pubDate", "date"]
789 ]);
hebastaa0282be2018-12-05 16:58:00 +0100790 KorAP.vc = vc;
791
Akron55a343b2018-04-06 19:57:36 +0200792 expect(vc.toQuery()).toEqual("");
Akronebc96662018-08-29 17:36:20 +0200793 expect(vc.builder().firstChild.textContent).toEqual(KorAP.Locale.EMPTY);
794 expect(vc.builder().firstChild.classList.contains('unspecified')).toEqual(true);
Akronadab5e52018-08-20 13:50:53 +0200795 vc.builder().firstChild.firstChild.click();
Akron55a343b2018-04-06 19:57:36 +0200796
797 // Click on pubDate
Akron3ad46942018-08-22 16:47:14 +0200798 vc.element().firstChild.getElementsByTagName("LI")[1].click();
Akronadab5e52018-08-20 13:50:53 +0200799 expect(vc.builder().firstChild.firstChild.textContent).toEqual("pubDate");
800 expect(vc.builder().firstChild.children[1].getAttribute("data-type")).toEqual("date");
Akron55a343b2018-04-06 19:57:36 +0200801 });
Nils Diewald7c8ced22015-04-15 19:21:00 +0000802 });
803
804 describe('KorAP.Doc element', function () {
805 it('should be initializable', function () {
806 var docElement = docClass.create(undefined, {
Akron712733a2018-04-05 18:17:47 +0200807 "@type" : "koral:doc",
808 "key":"Titel",
809 "value":"Baum",
810 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000811 });
812 expect(docElement.key()).toEqual('Titel');
813 expect(docElement.matchop()).toEqual('eq');
814 expect(docElement.value()).toEqual('Baum');
815
816 var docE = docElement.element();
817 expect(docE.children[0].firstChild.data).toEqual('Titel');
818 expect(docE.children[1].firstChild.data).toEqual('eq');
819 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
820 expect(docE.children[2].firstChild.data).toEqual('Baum');
821 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
822
823 expect(docElement.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200824 "@type" : "koral:doc",
825 "key":"Titel",
826 "value":"Baum",
827 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000828 }));
829 });
Akronb19803c2018-08-16 16:39:42 +0200830
831
832 it('should be replacable by unspecified', function () {
833 var vc = vcClass.create([
834 ["pubDate", "date"]
835 ]).fromJson({
836 "@type" : "koral:doc",
837 "key":"Titel",
838 "value":"Baum",
839 "match":"match:eq"
840 });
hebastaa0282be2018-12-05 16:58:00 +0100841 KorAP.vc = vc;
Akronb19803c2018-08-16 16:39:42 +0200842 expect(vc.toQuery()).toEqual("Titel = \"Baum\"");
843
Akronadab5e52018-08-20 13:50:53 +0200844 var vcE = vc.builder();
Akronb19803c2018-08-16 16:39:42 +0200845 expect(vcE.firstChild.children.length).toEqual(4);
846
847 // Click to delete
848 vcE.firstChild.lastChild.lastChild.click();
849
850 expect(vcE.firstChild.children.length).toEqual(1);
851
Akronebc96662018-08-29 17:36:20 +0200852 expect(vcE.firstChild.textContent).toEqual(KorAP.Locale.EMPTY);
853 expect(vcE.firstChild.classList.contains("unspecified")).toBeTruthy();
Akronb19803c2018-08-16 16:39:42 +0200854 vcE.firstChild.firstChild.click();
855
856 // Click on pubDate
Akron3ad46942018-08-22 16:47:14 +0200857 vcE.firstChild.getElementsByTagName("LI")[1].click();
Akronb19803c2018-08-16 16:39:42 +0200858
859 expect(vcE.firstChild.firstChild.textContent).toEqual("pubDate");
860 expect(vcE.firstChild.children[1].getAttribute("data-type")).toEqual("date");
861
862 expect(vcE.firstChild.children.length).toEqual(4);
863 });
864
865
Akron587e4d92018-08-31 12:44:26 +0200866 it('should be replaceable by docGroupRef with deletion', function () {
Akron3ad46942018-08-22 16:47:14 +0200867 var vc = vcClass.create().fromJson({
Akronb19803c2018-08-16 16:39:42 +0200868 "@type" : "koral:doc",
869 "key":"Titel",
870 "value":"Baum",
871 "match":"match:eq"
872 });
873
hebastaa0282be2018-12-05 16:58:00 +0100874 KorAP.vc = vc;
Akronb19803c2018-08-16 16:39:42 +0200875 expect(vc.toQuery()).toEqual("Titel = \"Baum\"");
876
Akronadab5e52018-08-20 13:50:53 +0200877 var vcE = vc.builder();
Akronb19803c2018-08-16 16:39:42 +0200878 expect(vcE.firstChild.children.length).toEqual(4);
879
880 // Click to delete
881 vcE.firstChild.lastChild.lastChild.click();
882
883 expect(vcE.firstChild.children.length).toEqual(1);
884
Akronebc96662018-08-29 17:36:20 +0200885 expect(vcE.firstChild.textContent).toEqual(KorAP.Locale.EMPTY);
886 expect(vcE.firstChild.classList.contains('unspecified')).toEqual(true);
Akronb19803c2018-08-16 16:39:42 +0200887 vcE.firstChild.firstChild.click();
888
Akron3ad46942018-08-22 16:47:14 +0200889 // Click on referTo
Akronb19803c2018-08-16 16:39:42 +0200890 vcE.firstChild.getElementsByTagName("LI")[0].click();
891
Akron3ad46942018-08-22 16:47:14 +0200892 expect(vcE.firstChild.firstChild.textContent).toEqual("referTo");
Akronb19803c2018-08-16 16:39:42 +0200893 expect(vcE.firstChild.children[1].getAttribute("data-type")).toEqual("string");
Akron587e4d92018-08-31 12:44:26 +0200894
Akronb19803c2018-08-16 16:39:42 +0200895 expect(vcE.firstChild.children.length).toEqual(3);
Akron62ac95b2018-08-30 18:08:25 +0200896 expect(vcE.firstChild.children[1].classList.contains("unspecified")).toBeTruthy();
Akronb19803c2018-08-16 16:39:42 +0200897 });
Akron587e4d92018-08-31 12:44:26 +0200898
899
900 it('should be replaceable by docGroupRef on root', function () {
901 var vc = vcClass.create().fromJson({
902 "@type" : "koral:doc",
903 "key":"Titel",
904 "value":"Baum",
905 "match":"match:eq"
906 });
907
hebastaa0282be2018-12-05 16:58:00 +0100908 KorAP.vc = vc;
Akron587e4d92018-08-31 12:44:26 +0200909 expect(vc.toQuery()).toEqual("Titel = \"Baum\"");
910
911 var vcE = vc.builder();
912 expect(vcE.firstChild.children.length).toEqual(4);
913
914 // Click on the key
915 vcE.firstChild.firstChild.click();
916
917 expect(vcE.firstChild.getElementsByTagName("LI")[0].textContent).toEqual("referTo");
918
919 // Click on referTo
920 vcE.firstChild.getElementsByTagName("LI")[0].click();
921
922 // Now it's a referTo element
923 expect(vcE.firstChild.firstChild.textContent).toEqual("referTo");
924 expect(vcE.firstChild.children.length).toEqual(3);
925 expect(vcE.firstChild.children[1].classList.contains("unspecified")).toBeTruthy();
926 });
927
928 it('should be replaceable by docGroupRef nested', function () {
929 var vc = vcClass.create().fromJson({
930 "@type" : "koral:docGroup",
931 "operation" : "operation:and",
932 "operands" : [
933 {
934 "@type": 'koral:doc',
935 "key" : 'author',
936 "match": 'match:eq',
937 "value": 'Max Birkendale',
938 "type": 'type:string'
939 },
940 {
941 "@type": 'koral:doc',
942 "key": 'pubDate',
943 "match": 'match:eq',
944 "value": '2014-12-05',
945 "type": 'type:date'
946 }
947 ]
948 });
949
hebastaa0282be2018-12-05 16:58:00 +0100950 KorAP.vc = vc;
Akron587e4d92018-08-31 12:44:26 +0200951 expect(vc.toQuery()).toEqual("author = \"Max Birkendale\" & pubDate in 2014-12-05");
952
953 var vcE = vc.builder();
954
955 // First doc
956 var doc = vcE.firstChild.firstChild;
957 expect(doc.children.length).toEqual(4);
958
959 // Click on the key
960 doc.firstChild.click();
961
962 expect(doc.getElementsByTagName("LI")[0].textContent).toEqual("referTo");
963
964 // Click on referTo
965 vcE.firstChild.getElementsByTagName("LI")[0].click();
966
967 // Now it's a referTo element
968 expect(vcE.firstChild.firstChild.firstChild.textContent).toEqual("referTo");
969 expect(vcE.firstChild.firstChild.children.length).toEqual(3);
970 expect(vcE.firstChild.firstChild.children[1].classList.contains("unspecified")).toBeTruthy();
971 });
Nils Diewald7c8ced22015-04-15 19:21:00 +0000972 });
973
974 describe('KorAP.DocGroup element', function () {
975 it('should be initializable', function () {
976
977 var docGroup = docGroupClass.create(undefined, {
Akron712733a2018-04-05 18:17:47 +0200978 "@type" : "koral:docGroup",
979 "operation" : "operation:and",
980 "operands" : [
981 {
982 "@type": 'koral:doc',
983 "key" : 'author',
984 "match": 'match:eq',
985 "value": 'Max Birkendale',
986 "type": 'type:string'
987 },
988 {
989 "@type": 'koral:doc',
990 "key": 'pubDate',
991 "match": 'match:eq',
992 "value": '2014-12-05',
993 "type": 'type:date'
994 }
995 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000996 });
997
998 expect(docGroup.operation()).toEqual('and');
999 var e = docGroup.element();
1000 expect(e.getAttribute('class')).toEqual('docGroup');
1001 expect(e.getAttribute('data-operation')).toEqual('and');
1002
1003 var first = e.children[0];
1004 expect(first.getAttribute('class')).toEqual('doc');
1005 expect(first.children[0].getAttribute('class')).toEqual('key');
1006 expect(first.children[1].getAttribute('class')).toEqual('match');
1007 expect(first.children[2].getAttribute('class')).toEqual('value');
1008 expect(first.children[2].getAttribute('data-type')).toEqual('string');
1009 expect(first.children[0].firstChild.data).toEqual('author');
1010 expect(first.children[1].firstChild.data).toEqual('eq');
1011 expect(first.children[2].firstChild.data).toEqual('Max Birkendale');
1012
1013 var second = e.children[1];
1014 expect(second.getAttribute('class')).toEqual('doc');
1015 expect(second.children[0].getAttribute('class')).toEqual('key');
1016 expect(second.children[1].getAttribute('class')).toEqual('match');
1017 expect(second.children[2].getAttribute('class')).toEqual('value');
1018 expect(second.children[2].getAttribute('data-type')).toEqual('date');
1019 expect(second.children[0].firstChild.data).toEqual('pubDate');
1020 expect(second.children[1].firstChild.data).toEqual('eq');
1021 expect(second.children[2].firstChild.data).toEqual('2014-12-05');
1022 });
1023
1024 it('should be deserializable with nested groups', function () {
1025 var docGroup = docGroupClass.create(undefined, {
Akron712733a2018-04-05 18:17:47 +02001026 "@type" : "koral:docGroup",
1027 "operation" : "operation:or",
1028 "operands" : [
1029 {
1030 "@type": 'koral:doc',
1031 "key" : 'author',
1032 "match": 'match:eq',
1033 "value": 'Max Birkendale',
1034 "type": 'type:string'
1035 },
1036 {
1037 "@type" : "koral:docGroup",
1038 "operation" : "operation:and",
1039 "operands" : [
1040 {
1041 "@type": 'koral:doc',
1042 "key": 'pubDate',
1043 "match": 'match:geq',
1044 "value": '2014-05-12',
1045 "type": 'type:date'
1046 },
1047 {
1048 "@type": 'koral:doc',
1049 "key": 'pubDate',
1050 "match": 'match:leq',
1051 "value": '2014-12-05',
1052 "type": 'type:date'
1053 }
1054 ]
1055 }
1056 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +00001057 });
1058
1059 expect(docGroup.operation()).toEqual('or');
1060 var e = docGroup.element();
1061 expect(e.getAttribute('class')).toEqual('docGroup');
1062 expect(e.getAttribute('data-operation')).toEqual('or');
1063
1064 expect(e.children[0].getAttribute('class')).toEqual('doc');
1065 var docop = e.children[0].lastChild;
Akron0b489ad2018-02-02 16:49:32 +01001066 expect(docop.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001067 expect(docop.children[0].getAttribute('class')).toEqual('and');
1068 expect(docop.children[1].getAttribute('class')).toEqual('or');
1069 expect(docop.children[2].getAttribute('class')).toEqual('delete');
1070
1071 expect(e.children[1].getAttribute('class')).toEqual('docGroup');
1072 expect(e.children[1].getAttribute('data-operation')).toEqual('and');
1073
1074 // This and-operation can be "or"ed or "delete"d
1075 var secop = e.children[1].children[2];
Akron0b489ad2018-02-02 16:49:32 +01001076 expect(secop.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001077 expect(secop.children[0].getAttribute('class')).toEqual('or');
1078 expect(secop.children[1].getAttribute('class')).toEqual('delete');
1079
1080 // This or-operation can be "and"ed or "delete"d
Akron0b489ad2018-02-02 16:49:32 +01001081 expect(e.children[2].getAttribute('class')).toEqual('operators button-group');
1082 expect(e.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001083 expect(e.lastChild.children[0].getAttribute('class')).toEqual('and');
1084 expect(e.lastChild.children[1].getAttribute('class')).toEqual('delete');
1085 });
1086 });
1087
Akronb19803c2018-08-16 16:39:42 +02001088 describe('KorAP.DocGroupRef element', function () {
1089 it('should be initializable', function () {
1090 var docGroupRef = docGroupRefClass.create(undefined, {
1091 "@type" : "koral:docGroupRef",
1092 "ref" : "@franz/myVC1"
1093 });
1094 expect(docGroupRef.ref()).toEqual("@franz/myVC1");
1095 var dgrE = docGroupRef.element();
1096
Akron3ad46942018-08-22 16:47:14 +02001097 expect(dgrE.children[0].firstChild.data).toEqual("referTo");
Akronb19803c2018-08-16 16:39:42 +02001098 expect(dgrE.children[0].tagName).toEqual("SPAN");
1099 expect(dgrE.children[0].classList.contains("key")).toBeTruthy();
1100 expect(dgrE.children[0].classList.contains("fixed")).toBeTruthy();
1101 expect(dgrE.children[1].firstChild.data).toEqual("@franz/myVC1");
1102 expect(dgrE.children[1].tagName).toEqual("SPAN");
1103 expect(dgrE.children[1].classList.contains("value")).toBeTruthy();
1104 expect(dgrE.children[1].getAttribute("data-type")).toEqual("string");
1105 });
1106
1107 it('should be modifiable on reference', function () {
1108 var docGroupRef = docGroupRefClass.create(undefined, {
1109 "@type" : "koral:docGroupRef",
1110 "ref" : "@franz/myVC1"
1111 });
1112 var dgrE = docGroupRef.element();
1113 expect(docGroupRef.toQuery()).toEqual("referTo \"@franz/myVC1\"");
1114 dgrE.children[1].click();
1115
1116 var input = dgrE.children[1].firstChild;
1117 expect(input.tagName).toEqual("INPUT");
1118 input.value = "Versuch";
1119
1120 var event = new KeyboardEvent("keypress", {
1121 "key" : "[Enter]",
1122 "keyCode" : 13
1123 });
1124
1125 input.dispatchEvent(event);
1126 expect(docGroupRef.toQuery()).toEqual("referTo \"Versuch\"");
1127 });
1128 });
1129
1130
Akrone4961b12017-05-10 21:04:46 +02001131 describe('KorAP.VirtualCorpus', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +00001132 var simpleGroupFactory = buildFactory(docGroupClass, {
1133 "@type" : "koral:docGroup",
1134 "operation" : "operation:and",
1135 "operands" : [
Akron712733a2018-04-05 18:17:47 +02001136 {
1137 "@type": 'koral:doc',
1138 "key" : 'author',
1139 "match": 'match:eq',
1140 "value": 'Max Birkendale',
1141 "type": 'type:string'
1142 },
1143 {
1144 "@type": 'koral:doc',
1145 "key": 'pubDate',
1146 "match": 'match:eq',
1147 "value": '2014-12-05',
1148 "type": 'type:date'
1149 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001150 ]
1151 });
1152
1153 var nestedGroupFactory = buildFactory(vcClass, {
1154 "@type" : "koral:docGroup",
1155 "operation" : "operation:or",
1156 "operands" : [
Akron712733a2018-04-05 18:17:47 +02001157 {
1158 "@type": 'koral:doc',
1159 "key" : 'author',
1160 "match": 'match:eq',
1161 "value": 'Max Birkendale',
1162 "type": 'type:string'
1163 },
1164 {
1165 "@type" : "koral:docGroup",
1166 "operation" : "operation:and",
1167 "operands" : [
1168 {
1169 "@type": 'koral:doc',
1170 "key": 'pubDate',
1171 "match": 'match:geq',
1172 "value": '2014-05-12',
1173 "type": 'type:date'
1174 },
1175 {
1176 "@type": 'koral:doc',
1177 "key": 'pubDate',
1178 "match": 'match:leq',
1179 "value": '2014-12-05',
1180 "type": 'type:date'
1181 }
1182 ]
1183 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001184 ]
1185 });
1186
1187 var flatGroupFactory = buildFactory(vcClass, {
1188 "@type" : "koral:docGroup",
1189 "operation" : "operation:and",
1190 "operands" : [
Akron712733a2018-04-05 18:17:47 +02001191 {
1192 "@type": 'koral:doc',
1193 "key": 'pubDate',
1194 "match": 'match:geq',
1195 "value": '2014-05-12',
1196 "type": 'type:date'
1197 },
1198 {
1199 "@type": 'koral:doc',
1200 "key": 'pubDate',
1201 "match": 'match:leq',
1202 "value": '2014-12-05',
1203 "type": 'type:date'
1204 },
1205 {
1206 "@type": 'koral:doc',
1207 "key": 'foo',
1208 "match": 'match:eq',
1209 "value": 'bar',
1210 "type": 'type:string'
1211 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001212 ]
1213 });
1214
1215 it('should be initializable', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001216 var vc = vcClass.create();
Nils Diewald7c8ced22015-04-15 19:21:00 +00001217 expect(vc.element().getAttribute('class')).toEqual('vc');
1218 expect(vc.root().element().getAttribute('class')).toEqual('doc unspecified');
1219
1220 // Not removable
1221 expect(vc.root().element().lastChild.children.length).toEqual(0);
1222 });
1223
1224 it('should be based on a doc', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001225 var vc = vcClass.create().fromJson({
Akron712733a2018-04-05 18:17:47 +02001226 "@type" : "koral:doc",
1227 "key":"Titel",
1228 "value":"Baum",
1229 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +00001230 });
1231
1232 expect(vc.element().getAttribute('class')).toEqual('vc');
1233 expect(vc.root().element().getAttribute('class')).toEqual('doc');
1234 expect(vc.root().key()).toEqual('Titel');
1235 expect(vc.root().value()).toEqual('Baum');
1236 expect(vc.root().matchop()).toEqual('eq');
1237
1238 var docE = vc.root().element();
1239 expect(docE.children[0].firstChild.data).toEqual('Titel');
1240 expect(docE.children[1].firstChild.data).toEqual('eq');
1241 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
1242 expect(docE.children[2].firstChild.data).toEqual('Baum');
1243 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
1244 });
1245
1246 it('should be based on a docGroup', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001247 var vc = vcClass.create().fromJson(simpleGroupFactory.create().toJson());
Nils Diewald7c8ced22015-04-15 19:21:00 +00001248
1249 expect(vc.element().getAttribute('class')).toEqual('vc');
1250 expect(vc.root().element().getAttribute('class')).toEqual('docGroup');
1251 expect(vc.root().operation()).toEqual('and');
1252
1253 var docGroup = vc.root();
1254
1255 var first = docGroup.getOperand(0);
1256 expect(first.key()).toEqual('author');
1257 expect(first.value()).toEqual('Max Birkendale');
1258 expect(first.matchop()).toEqual('eq');
1259
1260 var second = docGroup.getOperand(1);
1261 expect(second.key()).toEqual('pubDate');
1262 expect(second.value()).toEqual('2014-12-05');
1263 expect(second.matchop()).toEqual('eq');
1264 });
1265
Akronb19803c2018-08-16 16:39:42 +02001266 it('should be based on a docGroupRef', function () {
1267 var vc = vcClass.create().fromJson({
1268 "@type" : "koral:docGroupRef",
1269 "ref":"myCorpus"
1270 });
1271
Akronadab5e52018-08-20 13:50:53 +02001272 // iv class="doc groupref"><span class="key fixed">@referTo</span><span data-type="string" class="value">myCorpus</span>
1273 var vcE = vc.builder();
1274 expect(vcE.getAttribute('class')).toEqual('builder');
Akronb19803c2018-08-16 16:39:42 +02001275 expect(vcE.firstChild.tagName).toEqual('DIV');
1276 expect(vcE.firstChild.classList.contains('groupref')).toBeTruthy();
1277
1278 expect(vcE.firstChild.firstChild.tagName).toEqual('SPAN');
1279 expect(vcE.firstChild.firstChild.classList.contains('key')).toBeTruthy();
1280 expect(vcE.firstChild.firstChild.classList.contains('fixed')).toBeTruthy();
1281
Akron3ad46942018-08-22 16:47:14 +02001282 expect(vcE.firstChild.firstChild.textContent).toEqual("referTo");
Akronb19803c2018-08-16 16:39:42 +02001283
1284 expect(vcE.firstChild.children[1].tagName).toEqual('SPAN');
1285 expect(vcE.firstChild.children[1].classList.contains('value')).toBeTruthy();
1286 expect(vcE.firstChild.children[1].getAttribute('data-type')).toEqual('string');
1287 expect(vcE.firstChild.children[1].textContent).toEqual("myCorpus");
1288 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00001289
1290 it('should be based on a nested docGroup', function () {
1291 var vc = nestedGroupFactory.create();
1292
Akronadab5e52018-08-20 13:50:53 +02001293 expect(vc.builder().getAttribute('class')).toEqual('builder');
1294 expect(vc.builder().firstChild.getAttribute('class')).toEqual('docGroup');
1295 expect(vc.builder().firstChild.children[0].getAttribute('class')).toEqual('doc');
1296 var dg = vc.builder().firstChild.children[1];
Nils Diewald7c8ced22015-04-15 19:21:00 +00001297 expect(dg.getAttribute('class')).toEqual('docGroup');
1298 expect(dg.children[0].getAttribute('class')).toEqual('doc');
1299 expect(dg.children[1].getAttribute('class')).toEqual('doc');
Akron0b489ad2018-02-02 16:49:32 +01001300 expect(dg.children[2].getAttribute('class')).toEqual('operators button-group');
Akronadab5e52018-08-20 13:50:53 +02001301 expect(vc.builder().firstChild.children[2].getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001302 });
1303
Akronb19803c2018-08-16 16:39:42 +02001304 it('should be based on a nested docGroupRef', function () {
1305 var vc = vcClass.create().fromJson({
1306 "@type" : "koral:docGroup",
1307 "operation" : "operation:and",
1308 "operands" : [{
1309 "@type" : "koral:docGroupRef",
1310 "ref":"myCorpus"
1311 },{
1312 "@type" : "koral:doc",
1313 "key":"Titel",
1314 "value":"Baum",
1315 "match":"match:eq"
1316 }]
1317 });
1318
1319 expect(vc._root.ldType()).toEqual("docGroup");
1320
1321 expect(vc.toQuery()).toEqual('referTo "myCorpus" & Titel = "Baum"');
1322 });
1323
1324
Nils Diewald7c8ced22015-04-15 19:21:00 +00001325 it('should be modifiable by deletion in flat docGroups', function () {
1326 var vc = flatGroupFactory.create();
1327 var docGroup = vc.root();
1328
1329 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
1330
1331 var doc = docGroup.getOperand(1);
1332 expect(doc.key()).toEqual("pubDate");
1333 expect(doc.value()).toEqual("2014-12-05");
1334
1335 // Remove operand 1
1336 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
1337 expect(doc._element).toEqual(undefined);
1338
1339 doc = docGroup.getOperand(1);
1340 expect(doc.key()).toEqual("foo");
1341
1342 // Remove operand 1
1343 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
1344 expect(doc._element).toEqual(undefined);
1345
1346 // Only one operand left ...
1347 expect(docGroup.getOperand(1)).toBeUndefined();
1348 // ... but there shouldn't be a group anymore at all!
1349 expect(docGroup.getOperand(0)).toBeUndefined();
1350
1351 var obj = vc.root();
1352 expect(obj.ldType()).toEqual("doc");
1353 expect(obj.key()).toEqual("pubDate");
1354 expect(obj.value()).toEqual("2014-05-12");
1355
1356 expect(obj.element().getAttribute('class')).toEqual('doc');
1357 });
1358
1359
1360 it('should be modifiable by deletion in nested docGroups (root case)', function () {
1361 var vc = nestedGroupFactory.create();
1362
1363 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001364 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05)'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001365 );
1366
1367 var docGroup = vc.root();
1368 expect(docGroup.ldType()).toEqual("docGroup");
1369 expect(docGroup.operation()).toEqual("or");
1370
1371 var doc = docGroup.getOperand(0);
1372 expect(doc.key()).toEqual("author");
1373 expect(doc.value()).toEqual("Max Birkendale");
1374
1375 docGroup = docGroup.getOperand(1);
1376 expect(docGroup.operation()).toEqual("and");
1377
1378 doc = docGroup.getOperand(0);
1379 expect(doc.key()).toEqual("pubDate");
1380 expect(doc.matchop()).toEqual("geq");
1381 expect(doc.value()).toEqual("2014-05-12");
1382 expect(doc.type()).toEqual("date");
1383
1384 doc = docGroup.getOperand(1);
1385 expect(doc.key()).toEqual("pubDate");
1386 expect(doc.matchop()).toEqual("leq");
1387 expect(doc.value()).toEqual("2014-12-05");
1388 expect(doc.type()).toEqual("date");
1389
1390 // Remove first operand so everything becomes root
1391 expect(
Akron712733a2018-04-05 18:17:47 +02001392 vc.root().delOperand(
1393 vc.root().getOperand(0)
1394 ).update().ldType()
Nils Diewald7c8ced22015-04-15 19:21:00 +00001395 ).toEqual("docGroup");
1396
1397 expect(vc.root().ldType()).toEqual("docGroup");
1398 expect(vc.root().operation()).toEqual("and");
1399 expect(vc.root().getOperand(0).ldType()).toEqual("doc");
1400
1401 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001402 'pubDate since 2014-05-12 & pubDate until 2014-12-05'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001403 );
1404 });
1405
1406 it('should be modifiable by deletion in nested docGroups (resolve group case)', function () {
1407 var vc = nestedGroupFactory.create();
1408
1409 // Get nested group
1410 var firstGroup = vc.root().getOperand(1);
1411 firstGroup.append(simpleGroupFactory.create({ "operation" : "operation:or" }));
1412
1413 // Structur is now:
1414 // or(doc, and(doc, doc, or(doc, doc)))
1415
1416 // Get nested or in and
1417 var orGroup = vc.root().getOperand(1).getOperand(2);
1418 expect(orGroup.ldType()).toEqual("docGroup");
1419 expect(orGroup.operation()).toEqual("or");
1420
1421 // Remove
1422 // Structur is now:
1423 // or(doc, and(doc, doc, doc)))
1424 expect(orGroup.delOperand(orGroup.getOperand(0)).update().operation()).toEqual("and");
1425 expect(vc.root().getOperand(1).operands().length).toEqual(3);
1426 });
1427
1428 it('should be modifiable by deletion in nested docGroups (identical group case)', function () {
1429 var vc = nestedGroupFactory.create();
1430
1431 // Get nested group
1432 var firstGroup = vc.root().getOperand(1);
1433 firstGroup.append(simpleGroupFactory.create({
Akron712733a2018-04-05 18:17:47 +02001434 "operation" : "operation:or"
Nils Diewald7c8ced22015-04-15 19:21:00 +00001435 }));
1436 var oldAuthor = firstGroup.getOperand(2).getOperand(0);
1437 oldAuthor.key("title");
1438 oldAuthor.value("Der Birnbaum");
1439
1440 // Structur is now:
1441 // or(doc, and(doc, doc, or(doc, doc)))
1442 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001443 'author = "Max Birkendale" | ' +
1444 '(pubDate since 2014-05-12 & ' +
1445 'pubDate until 2014-12-05 & ' +
1446 '(title = "Der Birnbaum" | ' +
1447 'pubDate in 2014-12-05))'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001448 );
1449
1450 var andGroup = vc.root().getOperand(1);
1451
1452 // Get leading docs in and
1453 var doc1 = andGroup.getOperand(0);
1454 expect(doc1.ldType()).toEqual("doc");
1455 expect(doc1.value()).toEqual("2014-05-12");
1456 var doc2 = andGroup.getOperand(1);
1457 expect(doc2.ldType()).toEqual("doc");
1458 expect(doc2.value()).toEqual("2014-12-05");
1459
1460 // Remove 2
1461 expect(
Akron712733a2018-04-05 18:17:47 +02001462 andGroup.delOperand(doc2).update().operation()
Nils Diewald7c8ced22015-04-15 19:21:00 +00001463 ).toEqual("and");
1464 // Structur is now:
1465 // or(doc, and(doc, or(doc, doc)))
1466
1467 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001468 'author = "Max Birkendale"' +
1469 ' | (pubDate since 2014-05-12 & ' +
1470 '(title = "Der Birnbaum" | pubDate in 2014-12-05))'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001471 );
1472
1473
1474 // Remove 1
1475 expect(andGroup.delOperand(doc1).update().operation()).toEqual("or");
1476 // Structur is now:
1477 // or(doc, doc, doc)
1478
1479 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001480 'author = "Max Birkendale" | title = "Der Birnbaum" | pubDate in 2014-12-05'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001481 );
1482 });
1483
1484 it('should be reducible to unspecification', function () {
1485 var vc = demoFactory.create();
1486
1487 expect(vc.toQuery()).toEqual(vc.root().toQuery());
1488 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001489 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & ' +
1490 '(Titel = "Baum" | Veröffentlichungsort = "hihi")) ' +
1491 '| Untertitel ~ "huhu"');
Akrond141a362018-07-10 18:12:13 +02001492 expect(vc.root().element().lastChild.children[0].innerText).toEqual('and');
1493 expect(vc.root().element().lastChild.children[1].innerText).toEqual('×');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001494 expect(vc.root().delOperand(vc.root().getOperand(0)).update()).not.toBeUndefined();
Akron712733a2018-04-05 18:17:47 +02001495 expect(vc.toQuery()).toEqual('Untertitel ~ "huhu"');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001496
1497 var lc = vc.root().element().lastChild;
Akrond141a362018-07-10 18:12:13 +02001498 expect(lc.children[0].innerText).toEqual('and');
1499 expect(lc.children[1].innerText).toEqual('or');
1500 expect(lc.children[2].innerText).toEqual('×');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001501
1502 // Clean everything
1503 vc.clean();
1504 expect(vc.toQuery()).toEqual('');
1505 });
1506
1507 it('should flatten on import', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001508 var vc = vcClass.create().fromJson({
Akron712733a2018-04-05 18:17:47 +02001509 "@type":"koral:docGroup",
1510 "operation":"operation:or",
1511 "operands":[
1512 {
1513 "@type":"koral:docGroup",
1514 "operation":"operation:or",
1515 "operands":[
Nils Diewald7c8ced22015-04-15 19:21:00 +00001516 {
Akron712733a2018-04-05 18:17:47 +02001517 "@type":"koral:doc",
1518 "key":"Titel",
1519 "value":"Baum",
1520 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +00001521 },
1522 {
Akron712733a2018-04-05 18:17:47 +02001523 "@type":"koral:doc",
1524 "key":"Veröffentlichungsort",
1525 "value":"hihi",
1526 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +00001527 },
1528 {
Akron712733a2018-04-05 18:17:47 +02001529 "@type":"koral:docGroup",
1530 "operation":"operation:or",
1531 "operands":[
1532 {
1533 "@type":"koral:doc",
1534 "key":"Titel",
1535 "value":"Baum",
1536 "match":"match:eq"
1537 },
1538 {
1539 "@type":"koral:doc",
1540 "key":"Veröffentlichungsort",
1541 "value":"hihi",
1542 "match":"match:eq"
1543 }
1544 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +00001545 }
Akron712733a2018-04-05 18:17:47 +02001546 ]
1547 },
1548 {
1549 "@type":"koral:doc",
1550 "key":"Untertitel",
1551 "value":"huhu",
1552 "match":"match:contains"
1553 }
1554 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +00001555 });
Nils Diewaldfda29d92015-01-22 17:28:01 +00001556
Nils Diewald7c8ced22015-04-15 19:21:00 +00001557 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001558 'Titel = "Baum" | Veröffentlichungsort = "hihi" | Untertitel ~ "huhu"'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001559 );
Nils Diewald86dad5b2015-01-28 15:09:07 +00001560 });
Akron1bdf5272018-07-24 18:51:30 +02001561
1562 it('should be deserializable from collection 1', function () {
1563 var kq = {
1564 "matches":["..."],
1565 "collection":{
1566 "@type": "koral:docGroup",
1567 "operation": "operation:or",
1568 "operands": [{
1569 "@type": "koral:docGroup",
1570 "operation": "operation:and",
1571 "operands": [
1572 {
1573 "@type": "koral:doc",
1574 "key": "title",
1575 "match": "match:eq",
1576 "value": "Der Birnbaum",
1577 "type": "type:string"
1578 },
1579 {
1580 "@type": "koral:doc",
1581 "key": "pubPlace",
1582 "match": "match:eq",
1583 "value": "Mannheim",
1584 "type": "type:string"
1585 },
1586 {
1587 "@type": "koral:docGroup",
1588 "operation": "operation:or",
1589 "operands": [
1590 {
1591 "@type": "koral:doc",
1592 "key": "subTitle",
1593 "match": "match:eq",
1594 "value": "Aufzucht und Pflege",
1595 "type": "type:string"
1596 },
1597 {
1598 "@type": "koral:doc",
1599 "key": "subTitle",
1600 "match": "match:eq",
1601 "value": "Gedichte",
1602 "type": "type:string"
1603 }
1604 ]
1605 }
1606 ]
1607 },{
1608 "@type": "koral:doc",
1609 "key": "pubDate",
1610 "match": "match:geq",
1611 "value": "2015-03-05",
1612 "type": "type:date",
1613 "rewrites" : [{
1614 "@type" : "koral:rewrite",
1615 "operation" : "operation:modification",
1616 "src" : "querySerializer",
1617 "scope" : "tree"
1618 }]
1619 }]
1620 }
1621 };
1622
1623 var vc = vcClass.create().fromJson(kq["collection"]);
1624 expect(vc.toQuery()).toEqual('(title = "Der Birnbaum" & pubPlace = "Mannheim" & (subTitle = "Aufzucht und Pflege" | subTitle = "Gedichte")) | pubDate since 2015-03-05');
1625 });
1626
1627 it('should be deserializable from collection 2', function () {
1628 var kq = {
1629 "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
1630 "meta": {},
1631 "query": {
1632 "@type": "koral:token",
1633 "wrap": {
1634 "@type": "koral:term",
1635 "match": "match:eq",
1636 "layer": "orth",
1637 "key": "Baum",
1638 "foundry": "opennlp",
1639 "rewrites": [
1640 {
1641 "@type": "koral:rewrite",
1642 "src": "Kustvakt",
1643 "operation": "operation:injection",
1644 "scope": "foundry"
1645 }
1646 ]
1647 },
1648 "idn": "Baum_2227",
1649 "rewrites": [
1650 {
1651 "@type": "koral:rewrite",
1652 "src": "Kustvakt",
1653 "operation": "operation:injection",
1654 "scope": "idn"
1655 }
1656 ]
1657 },
1658 "collection": {
1659 "@type": "koral:docGroup",
1660 "operation": "operation:and",
1661 "operands": [
1662 {
1663 "@type": "koral:doc",
1664 "match": "match:eq",
1665 "type": "type:regex",
1666 "value": "CC-BY.*",
1667 "key": "availability"
1668 },
1669 {
1670 "@type": "koral:doc",
1671 "match": "match:eq",
1672 "type":"type:text",
1673 "value": "Goethe",
1674 "key": "author"
1675 }
1676 ],
1677 "rewrites": [
1678 {
1679 "@type": "koral:rewrite",
1680 "src": "Kustvakt",
1681 "operation": "operation:insertion",
1682 "scope": "availability(FREE)"
1683 }
1684 ]
1685 },
1686 "matches": []
1687 };
1688
1689 var vc = vcClass.create().fromJson(kq["collection"]);
1690 expect(vc.toQuery()).toEqual('availability = /CC-BY.*/ & author = "Goethe"');
1691 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00001692
Akron8a670162018-08-28 10:09:13 +02001693 it('shouldn\'t be deserializable from collection with unknown type', function () {
1694 var kq = {
1695 "@type" : "koral:doc",
1696 "match": "match:eq",
1697 "type":"type:failure",
1698 "value": "Goethe",
1699 "key": "author"
1700 };
Akron1bdf5272018-07-24 18:51:30 +02001701
Akron8a670162018-08-28 10:09:13 +02001702 expect(function () {
1703 vcClass.create().fromJson(kq)
1704 }).toThrow(new Error("Unknown value type: string"));
1705 });
1706
1707 it('should return a name', function () {
1708 var vc = vcClass.create();
1709 expect(vc.getName()).toEqual(KorAP.Locale.VC_allCorpora);
1710
1711 vc = vcClass.create().fromJson({"@type" : "koral:docGroupRef", "ref" : "DeReKo"});
1712 expect(vc.getName()).toEqual("DeReKo");
1713
1714 vc = vcClass.create().fromJson({"@type" : "koral:doc", "key" : "author", "value" : "Peter"});
1715 expect(vc.getName()).toEqual(KorAP.Locale.VC_oneCollection);
1716 });
Akrond2474aa2018-08-28 12:06:27 +02001717
1718 it('should check for rewrites', function () {
1719
1720 var vc = vcClass.create().fromJson({
1721 "@type" : "koral:doc",
1722 "key" : "author",
1723 "value" : "Goethe",
1724 "rewrites" : [{
1725 "@type" : "koral:rewrite",
1726 "operation" : "operation:modification",
1727 "src" : "querySerializer",
1728 "scope" : "value"
1729 }]
1730 });
1731 expect(vc.wasRewritten()).toBeTruthy();
1732
1733 var nested = {
1734 "@type" : "koral:docGroup",
1735 "operation" : "operation:or",
1736 "operands" : [
1737 {
1738 "@type" : "koral:doc",
1739 "key" : "author",
1740 "value" : "Goethe"
1741 },
1742 {
1743 "@type" : "koral:docGroup",
1744 "operation" : "operation:and",
1745 "operands" : [
1746 {
1747 "@type": "koral:doc",
1748 "key" : "author",
1749 "value" : "Schiller"
1750 },
1751 {
1752 "@type": "koral:doc",
1753 "key" : "author",
1754 "value" : "Fontane"
1755 }
1756 ]
1757 }
1758 ]
1759 };
1760 vc = vcClass.create().fromJson(nested);
1761 expect(vc.wasRewritten()).toBe(false);
1762
1763 nested["operands"][1]["operands"][1]["rewrites"] = [{
1764 "@type" : "koral:rewrite",
1765 "operation" : "operation:modification",
1766 "src" : "querySerializer",
1767 "scope" : "tree"
1768 }];
1769 vc = vcClass.create().fromJson(nested);
1770 expect(vc.wasRewritten()).toBeTruthy();
1771 });
Akron4feec9d2018-11-20 17:00:50 +01001772
1773 it('should add document at virtual corpus', function () {
1774 vc = vcClass.create();
1775 expect(vc.toQuery()).toEqual("");
1776
1777 let doc = docClass.create();
1778 doc.key("author");
1779 doc.type("string");
1780 doc.matchop("eq");
1781 doc.value("Goethe");
1782 expect(doc.toQuery()).toEqual('author = "Goethe"');
1783
1784 expect(vc.element().firstChild.children.length).toEqual(1);
1785 expect(vc.element().firstChild.firstChild.classList.contains("unspecified")).toBeTruthy();
1786
1787 vc.addRequired(doc);
1788 expect(vc.toQuery()).toEqual('author = "Goethe"');
1789
1790 expect(vc.element().firstChild.children.length).toEqual(1);
1791 expect(vc.element().firstChild.firstChild.classList.contains("doc")).toBeTruthy();
1792
1793 // Add as 'and' to doc
1794 let doc2 = docClass.create();
1795 doc2.key("author");
1796 doc2.type("string");
1797 doc2.matchop("eq");
1798 doc2.value("Schiller");
1799 vc.addRequired(doc2);
1800 expect(vc.toQuery()).toEqual('author = "Goethe" & author = "Schiller"');
1801
1802 expect(vc.element().firstChild.firstChild.classList.contains("docGroup")).toBeTruthy();
1803 expect(vc.element().firstChild.firstChild.children[0].classList.contains("doc")).toBeTruthy();
1804 expect(vc.element().firstChild.firstChild.children[1].classList.contains("doc")).toBeTruthy();
1805
1806
1807 // Add as 'and' to 'and'-docGroup
1808 let doc3 = docClass.create();
1809 doc3.key("author");
1810 doc3.type("string");
1811 doc3.matchop("eq");
1812 doc3.value("Fontane");
1813 vc.addRequired(doc3);
1814 expect(vc.toQuery()).toEqual('author = "Goethe" & author = "Schiller" & author = "Fontane"');
1815
1816 expect(vc.element().firstChild.firstChild.classList.contains("docGroup")).toBeTruthy();
1817 expect(vc.element().firstChild.firstChild.children[0].classList.contains("doc")).toBeTruthy();
1818 expect(vc.element().firstChild.firstChild.children[1].classList.contains("doc")).toBeTruthy();
1819 expect(vc.element().firstChild.firstChild.children[2].classList.contains("doc")).toBeTruthy();
1820
1821 // Add as 'and' to 'or'-docGroup
1822 vc = vcClass.create().fromJson({
1823 "@type" : 'koral:docGroup',
1824 'operation' : 'operation:or',
1825 'operands' : [
1826 {
1827 '@type' : 'koral:doc',
1828 'key' : 'title',
1829 'value' : 'Hello World!'
1830 },
1831 {
1832 '@type' : 'koral:doc',
1833 'key' : 'foo',
1834 'value' : 'bar'
1835 }
1836 ]
1837 });
1838 expect(vc.toQuery()).toEqual('title = "Hello World!" | foo = "bar"');
1839
1840 let doc4 = docClass.create();
1841 doc4.key("author");
1842 doc4.type("string");
1843 doc4.matchop("eq");
1844 doc4.value("Fontane");
1845 vc.addRequired(doc4);
1846 expect(vc.toQuery()).toEqual('(title = "Hello World!" | foo = "bar") & author = "Fontane"');
1847
1848 // Add as 'and' to 'or'-docGroup
1849 vc = vcClass.create().fromJson({
1850 "@type" : "koral:docGroupRef",
1851 "ref" : "@max/myCorpus"
1852 })
1853 let doc5 = docClass.create();
1854 doc5.key("author");
1855 doc5.type("string");
1856 doc5.matchop("eq");
1857 doc5.value("Goethe");
1858 expect(doc5.toQuery()).toEqual('author = "Goethe"');
1859 vc.addRequired(doc5);
1860 expect(vc.toQuery()).toEqual('referTo "@max/myCorpus" & author = "Goethe"');
1861 });
Akron1bdf5272018-07-24 18:51:30 +02001862 });
1863
1864
Nils Diewald7c8ced22015-04-15 19:21:00 +00001865 describe('KorAP.Operators', function () {
1866 it('should be initializable', function () {
1867 var op = operatorsClass.create(true, false, false);
1868 expect(op.and()).toBeTruthy();
1869 expect(op.or()).not.toBeTruthy();
1870 expect(op.del()).not.toBeTruthy();
1871
1872 op.and(false);
1873 expect(op.and()).not.toBeTruthy();
1874 expect(op.or()).not.toBeTruthy();
1875 expect(op.del()).not.toBeTruthy();
1876
1877 op.or(true);
1878 op.del(true);
1879 expect(op.and()).not.toBeTruthy();
1880 expect(op.or()).toBeTruthy();
1881 expect(op.del()).toBeTruthy();
1882
Akrond141a362018-07-10 18:12:13 +02001883 var e = op.update();
Akron0b489ad2018-02-02 16:49:32 +01001884 expect(e.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001885 expect(e.children[0].getAttribute('class')).toEqual('or');
Akrond141a362018-07-10 18:12:13 +02001886 expect(e.children[0].innerText).toEqual('or');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001887 expect(e.children[1].getAttribute('class')).toEqual('delete');
Akrond141a362018-07-10 18:12:13 +02001888 expect(e.children[1].innerText).toEqual('×');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001889
1890 op.and(true);
1891 op.del(false);
Akrond141a362018-07-10 18:12:13 +02001892 e = op.update();
Nils Diewald7c8ced22015-04-15 19:21:00 +00001893
Akron0b489ad2018-02-02 16:49:32 +01001894 expect(e.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001895 expect(e.children[0].getAttribute('class')).toEqual('and');
Akrond141a362018-07-10 18:12:13 +02001896 expect(e.children[0].innerText).toEqual('and');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001897 expect(e.children[1].getAttribute('class')).toEqual('or');
Akrond141a362018-07-10 18:12:13 +02001898 expect(e.children[1].innerText).toEqual('or');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001899 });
1900 });
1901
1902 describe('KorAP._delete (event)', function () {
1903 var complexVCFactory = buildFactory(vcClass,{
1904 "@type": 'koral:docGroup',
1905 'operation' : 'operation:and',
1906 'operands' : [
Akron712733a2018-04-05 18:17:47 +02001907 {
1908 "@type": 'koral:doc',
1909 "key": 'pubDate',
1910 "match": 'match:eq',
1911 "value": '2014-12-05',
1912 "type": 'type:date'
1913 },
1914 {
1915 "@type" : 'koral:docGroup',
1916 'operation' : 'operation:or',
1917 'operands' : [
1918 {
1919 '@type' : 'koral:doc',
1920 'key' : 'title',
1921 'value' : 'Hello World!'
1922 },
1923 {
1924 '@type' : 'koral:doc',
1925 'key' : 'foo',
1926 'value' : 'bar'
1927 }
1928 ]
1929 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001930 ]
1931 });
1932
1933 it('should clean on root docs', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001934 var vc = vcClass.create().fromJson({
Akron712733a2018-04-05 18:17:47 +02001935 "@type": 'koral:doc',
1936 "key": 'pubDate',
1937 "match": 'match:eq',
1938 "value": '2014-12-05',
1939 "type": 'type:date'
Nils Diewald86dad5b2015-01-28 15:09:07 +00001940 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00001941 expect(vc.root().toQuery()).toEqual('pubDate in 2014-12-05');
Akron0b489ad2018-02-02 16:49:32 +01001942 expect(vc.root().element().lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald86dad5b2015-01-28 15:09:07 +00001943
Nils Diewald7c8ced22015-04-15 19:21:00 +00001944 // Clean with delete from root
1945 expect(vc.root().element().lastChild.lastChild.getAttribute('class')).toEqual('delete');
1946 _delOn(vc.root());
1947 expect(vc.root().toQuery()).toEqual('');
Akronebc96662018-08-29 17:36:20 +02001948 expect(vc.root().element().lastChild.lastChild.data).toEqual(KorAP.Locale.EMPTY);
1949 expect(vc.root().element().classList.contains('unspecified')).toEqual(true);
Nils Diewald7c8ced22015-04-15 19:21:00 +00001950 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00001951
Nils Diewald7c8ced22015-04-15 19:21:00 +00001952 it('should remove on nested docs', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001953 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02001954 {
1955 "@type": 'koral:docGroup',
1956 'operation' : 'operation:and',
1957 'operands' : [
1958 {
1959 "@type": 'koral:doc',
1960 "key": 'pubDate',
1961 "match": 'match:eq',
1962 "value": '2014-12-05',
1963 "type": 'type:date'
1964 },
1965 {
1966 "@type" : 'koral:doc',
1967 'key' : 'foo',
1968 'value' : 'bar'
1969 }
1970 ]
1971 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001972 );
1973
1974 // Delete with direct element access
1975 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1976 _delOn(vc.root().getOperand(0));
1977
1978 expect(vc.toQuery()).toEqual('foo = "bar"');
1979 expect(vc.root().ldType()).toEqual('doc');
1980 });
1981
1982 it('should clean on doc groups', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001983 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02001984 {
1985 "@type": 'koral:docGroup',
1986 'operation' : 'operation:and',
1987 'operands' : [
1988 {
1989 "@type": 'koral:doc',
1990 "key": 'pubDate',
1991 "match": 'match:eq',
1992 "value": '2014-12-05',
1993 "type": 'type:date'
1994 },
1995 {
1996 "@type" : 'koral:doc',
1997 'key' : 'foo',
1998 'value' : 'bar'
1999 }
2000 ]
2001 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002002 );
2003
2004 // Cleanwith direct element access
2005 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
2006 _delOn(vc.root());
2007 expect(vc.toQuery()).toEqual('');
2008 expect(vc.root().ldType()).toEqual('non');
2009 });
2010
2011 it('should remove on nested doc groups (case of ungrouping 1)', function () {
2012 var vc = complexVCFactory.create();
2013
2014 // Delete with direct element access
2015 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002016 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002017 );
2018
2019 // Remove hello world:
2020 _delOn(vc.root().getOperand(1).getOperand(0));
2021 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
2022 expect(vc.root().ldType()).toEqual('docGroup');
2023 });
2024
2025 it('should remove on nested doc groups (case of ungrouping 2)', function () {
2026 var vc = complexVCFactory.create();
2027
2028 // Delete with direct element access
2029 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002030 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002031 );
2032
2033 // Remove bar
2034 _delOn(vc.root().getOperand(1).getOperand(1));
2035 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & title = "Hello World!"');
2036 expect(vc.root().ldType()).toEqual('docGroup');
2037 expect(vc.root().operation()).toEqual('and');
2038 });
2039
2040 it('should remove on nested doc groups (case of root changing)', function () {
2041 var vc = complexVCFactory.create();
2042
2043 // Delete with direct element access
2044 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002045 'pubDate in 2014-12-05 & ' +
2046 '(title = "Hello World!" | foo = "bar")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002047 );
2048
2049 // Remove bar
2050 _delOn(vc.root().getOperand(0));
2051 expect(vc.toQuery()).toEqual('title = "Hello World!" | foo = "bar"');
2052 expect(vc.root().ldType()).toEqual('docGroup');
2053 expect(vc.root().operation()).toEqual('or');
2054 });
2055
2056 it('should remove on nested doc groups (list flattening)', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00002057 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02002058 {
2059 "@type": 'koral:docGroup',
2060 'operation' : 'operation:or',
2061 'operands' : [
2062 {
2063 "@type": 'koral:doc',
2064 "key": 'pubDate',
2065 "match": 'match:eq',
2066 "value": '2014-12-05',
2067 "type": 'type:date'
2068 },
2069 {
2070 "@type" : 'koral:doc',
2071 'key' : 'foo',
2072 'value' : 'bar'
2073 },
2074 {
2075 "@type": 'koral:docGroup',
2076 'operation' : 'operation:and',
2077 'operands' : [
2078 {
2079 "@type": 'koral:doc',
2080 "key": 'pubDate',
2081 "match": 'match:eq',
2082 "value": '2014-12-05',
2083 "type": 'type:date'
2084 },
2085 {
2086 "@type" : 'koral:docGroup',
2087 'operation' : 'operation:or',
2088 'operands' : [
2089 {
2090 '@type' : 'koral:doc',
2091 'key' : 'title',
2092 'value' : 'Hello World!'
2093 },
2094 {
2095 '@type' : 'koral:doc',
2096 'key' : 'yeah',
2097 'value' : 'juhu'
2098 }
2099 ]
2100 }
2101 ]
2102 }
2103 ]
2104 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002105 );
2106
2107 // Delete with direct element access
2108 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002109 'pubDate in 2014-12-05 | foo = "bar" | ' +
2110 '(pubDate in 2014-12-05 & ' +
2111 '(title = "Hello World!" | yeah = "juhu"))'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002112 );
2113
2114 expect(vc.root().ldType()).toEqual('docGroup');
2115 expect(vc.root().operation()).toEqual('or');
2116
2117 // Operands and operators
Akronadab5e52018-08-20 13:50:53 +02002118 expect(vc.builder().firstChild.children.length).toEqual(4);
2119 expect(vc.builder().firstChild.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002120
2121 // Remove inner group and flatten
2122 _delOn(vc.root().getOperand(2).getOperand(0));
2123
2124 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002125 'pubDate in 2014-12-05 | foo = "bar" | title = "Hello World!" | yeah = "juhu"'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002126 );
2127 expect(vc.root().ldType()).toEqual('docGroup');
2128 expect(vc.root().operation()).toEqual('or');
2129
2130 // Operands and operators
Akronadab5e52018-08-20 13:50:53 +02002131 expect(vc.builder().firstChild.children.length).toEqual(5);
2132 expect(vc.builder().firstChild.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002133 });
2134 });
2135
2136 describe('KorAP._add (event)', function () {
2137 var complexVCFactory = buildFactory(vcClass,{
2138 "@type": 'koral:docGroup',
2139 'operation' : 'operation:and',
2140 'operands' : [
Akron712733a2018-04-05 18:17:47 +02002141 {
2142 "@type": 'koral:doc',
2143 "key": 'pubDate',
2144 "match": 'match:eq',
2145 "value": '2014-12-05',
2146 "type": 'type:date'
2147 },
2148 {
2149 "@type" : 'koral:docGroup',
2150 'operation' : 'operation:or',
2151 'operands' : [
2152 {
2153 '@type' : 'koral:doc',
2154 'key' : 'title',
2155 'value' : 'Hello World!'
2156 },
2157 {
2158 '@type' : 'koral:doc',
2159 'key' : 'foo',
2160 'value' : 'bar'
2161 }
2162 ]
2163 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002164 ]
2165 });
2166
2167 it('should add new unspecified doc with "and"', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00002168 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02002169 {
2170 "@type": 'koral:docGroup',
2171 'operation' : 'operation:and',
2172 'operands' : [
2173 {
2174 "@type": 'koral:doc',
2175 "key": 'pubDate',
2176 "match": 'match:eq',
2177 "value": '2014-12-05',
2178 "type": 'type:date'
2179 },
2180 {
2181 "@type" : 'koral:doc',
2182 'key' : 'foo',
2183 'value' : 'bar'
Akronb19803c2018-08-16 16:39:42 +02002184 },
2185 {
2186 "@type" : "koral:docGroupRef",
2187 "ref" : "myCorpus"
Akron712733a2018-04-05 18:17:47 +02002188 }
2189 ]
2190 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002191 );
2192
Akronb19803c2018-08-16 16:39:42 +02002193 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar" & referTo "myCorpus"');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002194
Akronadab5e52018-08-20 13:50:53 +02002195 var fc = vc.builder().firstChild;
Nils Diewald7c8ced22015-04-15 19:21:00 +00002196 expect(fc.getAttribute('data-operation')).toEqual('and');
Akronb19803c2018-08-16 16:39:42 +02002197 expect(fc.children.length).toEqual(4);
Akron0b489ad2018-02-02 16:49:32 +01002198 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002199 expect(fc.children[0].getAttribute('class')).toEqual('doc');
2200 expect(fc.children[1].getAttribute('class')).toEqual('doc');
Akronb19803c2018-08-16 16:39:42 +02002201 expect(fc.children[2].getAttribute('class')).toEqual('doc groupref');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002202
2203 // add with 'and' in the middle
2204 _andOn(vc.root().getOperand(0));
Akronb19803c2018-08-16 16:39:42 +02002205 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar" & referTo "myCorpus"');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002206
Akronadab5e52018-08-20 13:50:53 +02002207 fc = vc.builder().firstChild;
Nils Diewald7c8ced22015-04-15 19:21:00 +00002208 expect(fc.getAttribute('data-operation')).toEqual('and');
Akronb19803c2018-08-16 16:39:42 +02002209 expect(fc.children.length).toEqual(5);
Akron0b489ad2018-02-02 16:49:32 +01002210 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002211
2212 expect(fc.children[0].getAttribute('class')).toEqual('doc');
2213 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
2214 expect(fc.children[2].getAttribute('class')).toEqual('doc');
Akronb19803c2018-08-16 16:39:42 +02002215 expect(fc.children[3].getAttribute('class')).toEqual('doc groupref');
2216 expect(fc.children[4].classList.contains('button-group')).toBeTruthy();
2217 expect(fc.children.length).toEqual(5);
2218
2219 _andOn(vc.root().getOperand(3));
2220 expect(fc.children[0].getAttribute('class')).toEqual('doc');
2221 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
2222 expect(fc.children[2].getAttribute('class')).toEqual('doc');
2223 expect(fc.children[3].getAttribute('class')).toEqual('doc groupref');
2224 expect(fc.children[4].getAttribute('class')).toEqual('doc unspecified');
2225 expect(fc.children[5].classList.contains('button-group')).toBeTruthy();
2226 expect(fc.children.length).toEqual(6);
2227
Nils Diewald7c8ced22015-04-15 19:21:00 +00002228 });
2229
Akronb19803c2018-08-16 16:39:42 +02002230
Nils Diewald7c8ced22015-04-15 19:21:00 +00002231 it('should add new unspecified doc with "or"', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00002232 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02002233 {
2234 "@type": 'koral:docGroup',
2235 'operation' : 'operation:and',
2236 'operands' : [
2237 {
2238 "@type": 'koral:doc',
2239 "key": 'pubDate',
2240 "match": 'match:eq',
2241 "value": '2014-12-05',
2242 "type": 'type:date'
2243 },
2244 {
2245 "@type" : 'koral:doc',
2246 'key' : 'foo',
2247 'value' : 'bar'
Akronb19803c2018-08-16 16:39:42 +02002248 },
2249 {
2250 "@type" : "koral:docGroupRef",
2251 "ref" : "myCorpus"
Akron712733a2018-04-05 18:17:47 +02002252 }
2253 ]
2254 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002255 );
2256
Akronb19803c2018-08-16 16:39:42 +02002257 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar" & referTo "myCorpus"');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002258
Akronadab5e52018-08-20 13:50:53 +02002259 var fc = vc.builder().firstChild;
Akronb19803c2018-08-16 16:39:42 +02002260 expect(fc.children.length).toEqual(4);
Akron0b489ad2018-02-02 16:49:32 +01002261 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002262 expect(fc.children[0].getAttribute('class')).toEqual('doc');
2263 expect(fc.children[1].getAttribute('class')).toEqual('doc');
Akronb19803c2018-08-16 16:39:42 +02002264 expect(fc.children[2].getAttribute('class')).toEqual('doc groupref');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002265
2266 // add with 'or' in the middle
2267 _orOn(vc.root().getOperand(0));
Akronb19803c2018-08-16 16:39:42 +02002268 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar" & referTo "myCorpus"');
Akronadab5e52018-08-20 13:50:53 +02002269 fc = vc.builder().firstChild;
Nils Diewald7c8ced22015-04-15 19:21:00 +00002270
2271 expect(fc.getAttribute('data-operation')).toEqual('and');
Akronb19803c2018-08-16 16:39:42 +02002272 expect(fc.children.length).toEqual(4);
Nils Diewald7c8ced22015-04-15 19:21:00 +00002273 expect(fc.children[0].getAttribute('class')).toEqual('docGroup');
2274 expect(fc.children[0].getAttribute('data-operation')).toEqual('or');
2275 expect(fc.children[1].getAttribute('class')).toEqual('doc');
Akronb19803c2018-08-16 16:39:42 +02002276 expect(fc.children[2].getAttribute('class')).toEqual('doc groupref');
2277 expect(fc.children[3].getAttribute('class')).toEqual('operators button-group');
Akron0b489ad2018-02-02 16:49:32 +01002278 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002279
Akronadab5e52018-08-20 13:50:53 +02002280 fc = vc.builder().firstChild.firstChild;
Nils Diewald7c8ced22015-04-15 19:21:00 +00002281 expect(fc.children.length).toEqual(3);
2282 expect(fc.children[0].getAttribute('class')).toEqual('doc');
2283 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
Akron0b489ad2018-02-02 16:49:32 +01002284 expect(fc.children[2].getAttribute('class')).toEqual('operators button-group');
2285 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Akronb19803c2018-08-16 16:39:42 +02002286
2287 _orOn(vc.root().getOperand(2));
Akronadab5e52018-08-20 13:50:53 +02002288 fc = vc.builder().firstChild;
Akronb19803c2018-08-16 16:39:42 +02002289 expect(fc.children.length).toEqual(4);
2290
2291 expect(fc.children[0].getAttribute('class')).toEqual('docGroup');
2292 expect(fc.children[1].getAttribute('class')).toEqual('doc');
2293 expect(fc.children[2].getAttribute('class')).toEqual('docGroup');
2294 expect(fc.children[3].getAttribute('class')).toEqual('operators button-group');
2295
Akronadab5e52018-08-20 13:50:53 +02002296 fc = vc.builder().firstChild.children[2];
Akronb19803c2018-08-16 16:39:42 +02002297 expect(fc.children[0].getAttribute('class')).toEqual('doc groupref');
2298 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
2299 expect(fc.children[2].getAttribute('class')).toEqual('operators button-group');
2300 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
2301
Nils Diewald7c8ced22015-04-15 19:21:00 +00002302 });
2303
2304 it('should add new unspecified doc with "and" before group', function () {
2305 var vc = demoFactory.create();
2306
2307 // Wrap with direct element access
2308 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002309 '(Titel = "Baum" & ' +
2310 'Veröffentlichungsort = "hihi" & ' +
2311 '(Titel = "Baum" | ' +
2312 'Veröffentlichungsort = "hihi")) | ' +
2313 'Untertitel ~ "huhu"'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002314 );
2315
2316 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
2317 expect(vc.root().getOperand(0).operation()).toEqual('and');
2318 expect(vc.root().getOperand(0).operands().length).toEqual(3);
2319
2320 // Add unspecified on the second doc
2321 var secDoc = vc.root().getOperand(0).getOperand(1);
2322 expect(secDoc.value()).toEqual('hihi');
2323
2324 // Add
2325 _andOn(secDoc);
2326
2327 var fo = vc.root().getOperand(0);
2328
2329 expect(fo.ldType()).toEqual('docGroup');
2330 expect(fo.operation()).toEqual('and');
2331 expect(fo.operands().length).toEqual(4);
2332
2333 expect(fo.getOperand(0).ldType()).toEqual('doc');
2334 expect(fo.getOperand(1).ldType()).toEqual('doc');
2335 expect(fo.getOperand(2).ldType()).toEqual('non');
2336 expect(fo.getOperand(3).ldType()).toEqual('docGroup');
2337 });
2338
2339
2340 it('should remove a doc with an unspecified doc in a nested group', function () {
2341 var vc = demoFactory.create();
2342
2343 // Wrap with direct element access
2344 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002345 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & (Titel = "Baum" | Veröffentlichungsort = "hihi")) | Untertitel ~ "huhu"'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002346 );
2347
2348 var fo = vc.root().getOperand(0).getOperand(0);
2349 expect(fo.key()).toEqual('Titel');
2350 expect(fo.value()).toEqual('Baum');
2351
2352 // Add unspecified on the root group
2353 _orOn(fo);
2354
2355 fo = vc.root().getOperand(0).getOperand(0);
2356
2357 expect(fo.operation()).toEqual('or');
2358 expect(fo.getOperand(0).ldType()).toEqual('doc');
2359 expect(fo.getOperand(1).ldType()).toEqual('non');
2360
2361 // Delete document
2362 _delOn(fo.getOperand(0));
2363
2364 // The operand is now non
2365 expect(vc.root().getOperand(0).getOperand(0).ldType()).toEqual('non');
2366 expect(vc.root().getOperand(0).getOperand(1).ldType()).toEqual('doc');
2367 expect(vc.root().getOperand(0).getOperand(2).ldType()).toEqual('docGroup');
2368 });
2369
2370
Akron712733a2018-04-05 18:17:47 +02002371 it('should remove an unspecified doc with a doc in a nested group', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +00002372 var vc = demoFactory.create();
2373
2374 // Wrap with direct element access
2375 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002376 '(Titel = "Baum" & ' +
2377 'Veröffentlichungsort = "hihi" & ' +
2378 '(Titel = "Baum" ' +
2379 '| Veröffentlichungsort = "hihi")) | ' +
2380 'Untertitel ~ "huhu"'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002381 );
2382
2383 var fo = vc.root().getOperand(0).getOperand(0);
2384 expect(fo.key()).toEqual('Titel');
2385 expect(fo.value()).toEqual('Baum');
2386
2387 // Add unspecified on the root group
2388 _orOn(fo);
2389
2390 fo = vc.root().getOperand(0).getOperand(0);
2391
2392 expect(fo.operation()).toEqual('or');
2393 expect(fo.getOperand(0).ldType()).toEqual('doc');
2394 expect(fo.getOperand(1).ldType()).toEqual('non');
2395
2396 // Delete unspecified doc
2397 _delOn(fo.getOperand(1));
2398
2399 // The operand is now non
2400 fo = vc.root().getOperand(0);
2401 expect(fo.getOperand(0).ldType()).toEqual('doc');
2402 expect(fo.getOperand(0).key()).toEqual('Titel');
2403 expect(fo.getOperand(0).value()).toEqual('Baum');
2404 expect(fo.getOperand(1).ldType()).toEqual('doc');
2405 expect(fo.getOperand(2).ldType()).toEqual('docGroup');
2406 });
2407
2408
2409 it('should add on parent group (case "and")', function () {
2410 var vc = complexVCFactory.create();
2411
2412 // Wrap with direct element access
2413 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002414 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002415 );
2416
2417 expect(vc.root().operands().length).toEqual(2);
2418
2419 // Add unspecified on the root group
2420 _andOn(vc.root().getOperand(1));
2421 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002422 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002423 );
2424
2425 expect(vc.root().ldType()).toEqual('docGroup');
2426 expect(vc.root().operands().length).toEqual(3);
2427 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
2428 expect(vc.root().getOperand(1).ldType()).toEqual('docGroup');
2429 expect(vc.root().getOperand(1).operation()).toEqual('or');
2430 expect(vc.root().getOperand(2).ldType()).toEqual('non');
2431
2432 // Add another unspecified on the root group
2433 _andOn(vc.root().getOperand(1));
2434
2435 expect(vc.root().operands().length).toEqual(4);
2436 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
2437 expect(vc.root().getOperand(1).ldType()).toEqual('docGroup');
2438 expect(vc.root().getOperand(2).ldType()).toEqual('non');
2439 expect(vc.root().getOperand(3).ldType()).toEqual('non');
2440
2441 // Add another unspecified after the first doc
2442 _andOn(vc.root().getOperand(0));
2443
2444 expect(vc.root().operands().length).toEqual(5);
2445 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
2446 expect(vc.root().getOperand(1).ldType()).toEqual('non');
2447 expect(vc.root().getOperand(2).ldType()).toEqual('docGroup');
2448 expect(vc.root().getOperand(3).ldType()).toEqual('non');
2449 expect(vc.root().getOperand(4).ldType()).toEqual('non');
2450 });
2451
2452 it('should wrap on root', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00002453 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02002454 {
2455 "@type": 'koral:docGroup',
2456 'operation' : 'operation:and',
2457 'operands' : [
2458 {
2459 "@type": 'koral:doc',
2460 "key": 'pubDate',
2461 "match": 'match:eq',
2462 "value": '2014-12-05',
2463 "type": 'type:date'
2464 },
2465 {
2466 "@type" : 'koral:doc',
2467 'key' : 'foo',
2468 'value' : 'bar'
2469 }
2470 ]
2471 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002472 );
2473
2474 // Wrap on root
2475 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
2476 expect(vc.root().ldType()).toEqual('docGroup');
2477 expect(vc.root().operation()).toEqual('and');
2478 _orOn(vc.root());
2479 expect(vc.root().ldType()).toEqual('docGroup');
2480 expect(vc.root().operation()).toEqual('or');
2481
2482 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
2483 expect(vc.root().getOperand(0).operation()).toEqual('and');
2484 });
2485
2486 it('should add on root (case "and")', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00002487 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02002488 {
2489 "@type": 'koral:doc',
2490 "key": 'pubDate',
2491 "match": 'match:eq',
2492 "value": '2014-12-05',
2493 "type": 'type:date'
2494 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002495 );
2496
2497 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
2498 expect(vc.root().ldType()).toEqual('doc');
2499 expect(vc.root().key()).toEqual('pubDate');
2500 expect(vc.root().value()).toEqual('2014-12-05');
2501
2502 // Wrap on root
2503 _andOn(vc.root());
2504 expect(vc.root().ldType()).toEqual('docGroup');
2505 expect(vc.root().operation()).toEqual('and');
2506 });
2507
2508 it('should add on root (case "or")', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00002509 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02002510 {
2511 "@type": 'koral:doc',
2512 "key": 'pubDate',
2513 "match": 'match:eq',
2514 "value": '2014-12-05',
2515 "type": 'type:date'
2516 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002517 );
2518
2519 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
2520 expect(vc.root().key()).toEqual('pubDate');
2521 expect(vc.root().value()).toEqual('2014-12-05');
2522
2523 // Wrap on root
2524 _orOn(vc.root());
2525 expect(vc.root().ldType()).toEqual('docGroup');
2526 expect(vc.root().operation()).toEqual('or');
2527 });
2528
2529 it('should support multiple sub groups per group', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00002530 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02002531 {
2532 "@type": 'koral:docGroup',
2533 'operation' : 'operation:or',
2534 'operands' : [
2535 {
2536 "@type": 'koral:docGroup',
2537 'operation' : 'operation:and',
2538 'operands' : [
2539 {
2540 "@type": 'koral:doc',
2541 "key": 'title',
2542 "value": 't1',
2543 },
2544 {
2545 "@type" : 'koral:doc',
2546 'key' : 'title',
2547 'value' : 't2'
2548 }
2549 ]
2550 },
2551 {
2552 "@type": 'koral:docGroup',
2553 'operation' : 'operation:and',
2554 'operands' : [
2555 {
2556 "@type": 'koral:doc',
2557 "key": 'title',
2558 "value": 't3',
2559 },
2560 {
2561 "@type" : 'koral:doc',
2562 'key' : 'title',
2563 'value' : 't4'
2564 }
2565 ]
2566 }
2567 ]
2568 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002569 );
2570 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002571 '(title = "t1" & title = "t2") | ' +
2572 '(title = "t3" & title = "t4")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002573 );
2574 expect(vc.root().operation()).toEqual('or');
2575 expect(vc.root().getOperand(0).toQuery()).toEqual('title = "t1" & title = "t2"');
2576 expect(vc.root().getOperand(1).toQuery()).toEqual('title = "t3" & title = "t4"');
2577
2578 _andOn(vc.root());
2579
2580 expect(vc.root().operation()).toEqual('and');
2581 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
2582 expect(vc.root().getOperand(1).ldType()).toEqual('non');
2583 });
2584 });
2585
Nils Diewald6283d692015-04-23 20:32:53 +00002586
2587 describe('KorAP.Rewrite', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +00002588 it('should be initializable', function () {
2589 var rewrite = rewriteClass.create({
Akron712733a2018-04-05 18:17:47 +02002590 "@type" : "koral:rewrite",
2591 "operation" : "operation:modification",
2592 "src" : "querySerializer",
2593 "scope" : "tree"
Nils Diewald86dad5b2015-01-28 15:09:07 +00002594 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00002595 expect(rewrite.toString()).toEqual('Modification of "tree" by "querySerializer"');
2596 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00002597
Nils Diewald7c8ced22015-04-15 19:21:00 +00002598 it('should be deserialized by docs', function () {
2599 var doc = docClass.create(
Akron712733a2018-04-05 18:17:47 +02002600 undefined,
2601 {
2602 "@type":"koral:doc",
2603 "key":"Titel",
2604 "value":"Baum",
2605 "match":"match:eq"
2606 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00002607
2608 expect(doc.element().classList.contains('doc')).toBeTruthy();
2609 expect(doc.element().classList.contains('rewritten')).toBe(false);
2610
2611 doc = docClass.create(
Akron712733a2018-04-05 18:17:47 +02002612 undefined,
2613 {
2614 "@type":"koral:doc",
2615 "key":"Titel",
2616 "value":"Baum",
2617 "match":"match:eq",
2618 "rewrites" : [
2619 {
2620 "@type" : "koral:rewrite",
2621 "operation" : "operation:modification",
2622 "src" : "querySerializer",
2623 "scope" : "tree"
2624 }
2625 ]
2626 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00002627
2628 expect(doc.element().classList.contains('doc')).toBeTruthy();
2629 expect(doc.element().classList.contains('rewritten')).toBeTruthy();
2630 });
Nils Diewald6283d692015-04-23 20:32:53 +00002631
Akron76c3dd62018-05-29 20:58:27 +02002632 it('should be described in a title attribute', function () {
2633
2634 doc = docClass.create(
2635 undefined,
2636 {
2637 "@type":"koral:doc",
2638 "key":"Titel",
2639 "value":"Baum",
2640 "match":"match:eq",
2641 "rewrites" : [
2642 {
2643 "@type" : "koral:rewrite",
2644 "operation" : "operation:modification",
2645 "src" : "querySerializer",
2646 "scope" : "tree"
2647 },
2648 {
2649 "@type" : "koral:rewrite",
2650 "operation" : "operation:injection",
2651 "src" : "me"
2652 }
2653 ]
2654 });
2655
2656 expect(doc.element().classList.contains('doc')).toBeTruthy();
2657 expect(doc.element().classList.contains('rewritten')).toBeTruthy();
2658 expect(doc.element().lastChild.getAttribute("title")).toEqual("querySerializer: tree (modification)\nme (injection)");
2659 });
2660
2661
Nils Diewald6283d692015-04-23 20:32:53 +00002662 xit('should be deserialized by docGroups', function () {
2663 var docGroup = docGroupClass.create(
Akron712733a2018-04-05 18:17:47 +02002664 undefined,
2665 {
2666 "@type" : "koral:docGroup",
2667 "operation" : "operation:or",
2668 "operands" : [
2669 {
2670 "@type" : "doc",
2671 "key" : "pubDate",
2672 "type" : "type:date",
2673 "value" : "2014-12-05"
2674 },
2675 {
2676 "@type" : "doc",
2677 "key" : "pubDate",
2678 "type" : "type:date",
2679 "value" : "2014-12-06"
2680 }
2681 ],
2682 "rewrites" : [
2683 {
2684 "@type" : "koral:rewrite",
2685 "operation" : "operation:modification",
2686 "src" : "querySerializer",
2687 "scope" : "tree"
2688 }
2689 ]
2690 }
Nils Diewald6283d692015-04-23 20:32:53 +00002691 );
2692
2693 expect(doc.element().classList.contains('docgroup')).toBeTruthy();
2694 expect(doc.element().classList.contains('rewritten')).toBe(false);
2695 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00002696 });
Nils Diewaldf0c4f112015-05-05 12:56:59 +00002697
2698 describe('KorAP.stringValue', function () {
2699 it('should be initializable', function () {
2700 var sv = stringValClass.create();
2701 expect(sv.regex()).toBe(false);
2702 expect(sv.value()).toBe('');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002703
2704 sv = stringValClass.create('Baum');
2705 expect(sv.regex()).toBe(false);
2706 expect(sv.value()).toBe('Baum');
2707
2708 sv = stringValClass.create('Baum', false);
2709 expect(sv.regex()).toBe(false);
2710 expect(sv.value()).toBe('Baum');
2711
2712 sv = stringValClass.create('Baum', true);
2713 expect(sv.regex()).toBe(true);
2714 expect(sv.value()).toBe('Baum');
2715 });
2716
2717 it('should be modifiable', function () {
2718 var sv = stringValClass.create();
2719 expect(sv.regex()).toBe(false);
2720 expect(sv.value()).toBe('');
2721
2722 expect(sv.value('Baum')).toBe('Baum');
2723 expect(sv.value()).toBe('Baum');
2724
2725 expect(sv.regex(true)).toBe(true);
2726 expect(sv.regex()).toBe(true);
2727 });
2728
2729 it('should have a toggleble regex value', function () {
2730 var sv = stringValClass.create();
Nils Diewald7991a3f2015-05-19 14:12:37 +00002731
2732 expect(sv.element().firstChild.value).toBe('');
2733 sv.element().firstChild.value = 'der'
2734 expect(sv.element().firstChild.value).toBe('der');
2735
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002736 expect(sv.regex()).toBe(false);
2737
2738 sv.toggleRegex();
Nils Diewald7991a3f2015-05-19 14:12:37 +00002739 expect(sv.element().firstChild.value).toBe('der');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002740 expect(sv.regex()).toBe(true);
Nils Diewald7991a3f2015-05-19 14:12:37 +00002741 sv.element().firstChild.value = 'derbe'
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002742
2743 sv.toggleRegex();
2744 expect(sv.regex()).toBe(false);
Nils Diewald7991a3f2015-05-19 14:12:37 +00002745 expect(sv.element().firstChild.value).toBe('derbe');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002746 });
2747
2748 it('should have an element', function () {
2749 var sv = stringValClass.create('der');
2750 expect(sv.element().nodeName).toBe('DIV');
2751 expect(sv.element().firstChild.nodeName).toBe('INPUT');
2752 expect(sv.element().firstChild.value).toBe('der');
2753 });
2754
2755 it('should have a classed element', function () {
2756 var sv = stringValClass.create();
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002757 expect(sv.element().classList.contains('regex')).toBe(false);
2758 expect(sv.regex()).toBe(false);
2759 sv.toggleRegex();
2760 expect(sv.element().classList.contains('regex')).toBe(true);
2761 });
2762
2763 it('should be storable', function () {
2764 var sv = stringValClass.create();
2765 var count = 1;
2766 sv.store = function (value, regex) {
Akron712733a2018-04-05 18:17:47 +02002767 expect(regex).toBe(true);
2768 expect(value).toBe('tree');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002769 };
2770 sv.regex(true);
2771 sv.value('tree');
2772 sv.element().lastChild.click();
Nils Diewaldf0c4f112015-05-05 12:56:59 +00002773 });
Akron712733a2018-04-05 18:17:47 +02002774
Akronb19803c2018-08-16 16:39:42 +02002775 it('should have a disableoption for regex', function () {
2776 var sv = stringValClass.create(undefined, undefined, true);
2777 var svE = sv.element();
2778 expect(svE.children.length).toEqual(2);
2779
2780 sv = stringValClass.create(undefined, undefined, false);
2781 svE = sv.element();
2782 expect(svE.children.length).toEqual(1);
2783 });
2784
Nils Diewaldf0c4f112015-05-05 12:56:59 +00002785 });
Akrone4961b12017-05-10 21:04:46 +02002786
Akron712733a2018-04-05 18:17:47 +02002787 describe('KorAP.VC.Menu', function () {
Akrone65a88a2018-04-05 19:14:20 +02002788
2789 var vc;
2790
Akron712733a2018-04-05 18:17:47 +02002791 it('should be initializable', function () {
2792
Akrone65a88a2018-04-05 19:14:20 +02002793 vc = vcClass.create([
Akron712733a2018-04-05 18:17:47 +02002794 ['a', 'text'],
2795 ['b', 'string'],
2796 ['c', 'date']
2797 ]).fromJson();
Akronadab5e52018-08-20 13:50:53 +02002798 expect(vc.builder().firstChild.classList.contains('unspecified')).toBeTruthy();
2799 expect(vc.builder().firstChild.firstChild.tagName).toEqual('SPAN');
Akron712733a2018-04-05 18:17:47 +02002800
2801 // Click on unspecified
Akronadab5e52018-08-20 13:50:53 +02002802 vc.builder().firstChild.firstChild.click();
2803 expect(vc.builder().firstChild.firstChild.tagName).toEqual('UL');
Akron712733a2018-04-05 18:17:47 +02002804
Akronadab5e52018-08-20 13:50:53 +02002805 var list = vc.builder().firstChild.firstChild;
Akron3ad46942018-08-22 16:47:14 +02002806 expect(list.getElementsByTagName("LI")[0].innerText).toEqual('referTo');
2807 expect(list.getElementsByTagName("LI")[1].innerText).toEqual('a');
2808 expect(list.getElementsByTagName("LI")[2].innerText).toEqual('b');
2809 expect(list.getElementsByTagName("LI")[3].innerText).toEqual('c');
Akron712733a2018-04-05 18:17:47 +02002810
2811 vc = vcClass.create([
2812 ['d', 'text'],
2813 ['e', 'string'],
2814 ['f', 'date']
2815 ]).fromJson();
Akronadab5e52018-08-20 13:50:53 +02002816 expect(vc.builder().firstChild.classList.contains('unspecified')).toBeTruthy();
2817 expect(vc.builder().firstChild.firstChild.tagName).toEqual('SPAN');
Akron712733a2018-04-05 18:17:47 +02002818
2819 // Click on unspecified
Akronadab5e52018-08-20 13:50:53 +02002820 vc.builder().firstChild.firstChild.click();
2821 expect(vc.builder().firstChild.firstChild.tagName).toEqual('UL');
Akron712733a2018-04-05 18:17:47 +02002822
Akronadab5e52018-08-20 13:50:53 +02002823 list = vc.builder().firstChild.firstChild;
Akron3ad46942018-08-22 16:47:14 +02002824 expect(list.getElementsByTagName("LI")[0].innerText).toEqual('referTo');
2825 expect(list.getElementsByTagName("LI")[1].innerText).toEqual('d');
2826 expect(list.getElementsByTagName("LI")[2].innerText).toEqual('e');
2827 expect(list.getElementsByTagName("LI")[3].innerText).toEqual('f');
Akron31d89942018-04-06 16:44:51 +02002828 // blur
2829 document.body.click();
Akron712733a2018-04-05 18:17:47 +02002830 });
Akrone65a88a2018-04-05 19:14:20 +02002831
2832 // Reinitialize to make tests stable
2833 vc = vcClass.create([
2834 ['d', 'text'],
2835 ['e', 'string'],
2836 ['f', 'date']
2837 ]).fromJson();
2838
2839 it('should be clickable on key', function () {
Akron31d89942018-04-06 16:44:51 +02002840 // Click on unspecified
Akronadab5e52018-08-20 13:50:53 +02002841 vc.builder().firstChild.firstChild.click();
Akrone65a88a2018-04-05 19:14:20 +02002842 // Click on "d"
Akronadab5e52018-08-20 13:50:53 +02002843 vc.builder().firstChild.firstChild.getElementsByTagName("LI")[1].click();
2844 expect(vc.builder().firstChild.firstChild.tagName).toEqual('SPAN');
2845 expect(vc.builder().firstChild.firstChild.innerText).toEqual('d');
2846 expect(vc.builder().firstChild.children[1].innerText).toEqual('eq');
2847 expect(vc.builder().firstChild.children[1].getAttribute('data-type')).toEqual('text');
Akron31d89942018-04-06 16:44:51 +02002848 // blur
2849 document.body.click();
Akrone65a88a2018-04-05 19:14:20 +02002850 });
2851
Akron31d89942018-04-06 16:44:51 +02002852 it('should be clickable on operation for text', function () {
2853 // Click on "d" (or unspecified)
Akronadab5e52018-08-20 13:50:53 +02002854 vc.builder().firstChild.firstChild.click();
Akron31d89942018-04-06 16:44:51 +02002855
2856 // Choose "d"
Akronadab5e52018-08-20 13:50:53 +02002857 vc.builder().firstChild.firstChild.getElementsByTagName("LI")[1].click();
Akron31d89942018-04-06 16:44:51 +02002858
2859 // Click on matchop
Akronadab5e52018-08-20 13:50:53 +02002860 vc.builder().firstChild.children[1].click();
Akron31d89942018-04-06 16:44:51 +02002861
Akronadab5e52018-08-20 13:50:53 +02002862 expect(vc.builder().firstChild.children[1].tagName).toEqual('UL');
Akron31d89942018-04-06 16:44:51 +02002863
Akronadab5e52018-08-20 13:50:53 +02002864 var ul = vc.builder().firstChild.children[1];
Akrone65a88a2018-04-05 19:14:20 +02002865 expect(ul.getElementsByTagName('li')[0].innerText).toEqual("eq");
2866 expect(ul.getElementsByTagName('li')[1].innerText).toEqual("ne");
2867 expect(ul.getElementsByTagName('li')[2].innerText).toEqual("contains");
2868 expect(ul.getElementsByTagName('li')[3].innerText).toEqual("containsnot");
2869 expect(ul.getElementsByTagName('li')[4]).toBeUndefined();
Akron31d89942018-04-06 16:44:51 +02002870
2871 // Choose "contains"
2872 ul.getElementsByTagName('li')[2].click();
Akronadab5e52018-08-20 13:50:53 +02002873 expect(vc.builder().firstChild.children[1].tagName).toEqual("SPAN");
2874 expect(vc.builder().firstChild.children[1].innerText).toEqual("contains");
Akron31d89942018-04-06 16:44:51 +02002875 // blur
2876 document.body.click();
Akrone65a88a2018-04-05 19:14:20 +02002877 })
Akron31d89942018-04-06 16:44:51 +02002878
2879 it('should be clickable on operation for string', function () {
2880 // Click on "d" (or unspecified)
Akronadab5e52018-08-20 13:50:53 +02002881 vc.builder().firstChild.firstChild.click();
Akron31d89942018-04-06 16:44:51 +02002882
2883 // Choose "e"
Akronadab5e52018-08-20 13:50:53 +02002884 vc.builder().firstChild.firstChild.getElementsByTagName("LI")[2].click();
2885
Akron31d89942018-04-06 16:44:51 +02002886 // As a consequence the matchoperator may no longer
2887 // be valid and needs to be re-evaluated
Akronadab5e52018-08-20 13:50:53 +02002888 var fc = vc.builder().firstChild;
Akron31d89942018-04-06 16:44:51 +02002889 expect(fc.firstChild.tagName).toEqual('SPAN');
2890 expect(fc.firstChild.innerText).toEqual('e');
2891 expect(fc.children[1].innerText).toEqual('eq');
2892 expect(fc.children[1].getAttribute('data-type')).toEqual('string');
2893
Akronadab5e52018-08-20 13:50:53 +02002894 vc.builder().firstChild.children[1].click();
Akron31d89942018-04-06 16:44:51 +02002895
Akronadab5e52018-08-20 13:50:53 +02002896 expect(vc.builder().firstChild.children[1].tagName).toEqual('UL');
Akron31d89942018-04-06 16:44:51 +02002897
Akronadab5e52018-08-20 13:50:53 +02002898 var ul = vc.builder().firstChild.children[1];
Akron31d89942018-04-06 16:44:51 +02002899 expect(ul.getElementsByTagName('li')[0].innerText).toEqual("eq");
2900 expect(ul.getElementsByTagName('li')[1].innerText).toEqual("ne");
2901 expect(ul.getElementsByTagName('li')[2]).toBeUndefined();
2902
2903 // Choose "ne"
2904 ul.getElementsByTagName('li')[1].click();
Akronadab5e52018-08-20 13:50:53 +02002905 expect(vc.builder().firstChild.children[1].tagName).toEqual("SPAN");
2906 expect(vc.builder().firstChild.children[1].innerText).toEqual("ne");
Akron8db5e3a2018-05-28 19:25:26 +02002907
2908 // Click on text
Akronebc96662018-08-29 17:36:20 +02002909 expect(vc.builder().firstChild.children[2].innerText).toEqual(KorAP.Locale.EMPTY);
2910 expect(vc.builder().firstChild.children[2].classList.contains('unspecified')).toEqual(true);
Akronadab5e52018-08-20 13:50:53 +02002911 vc.builder().firstChild.children[2].click();
Akron8db5e3a2018-05-28 19:25:26 +02002912
2913 // Blur text element
Akronadab5e52018-08-20 13:50:53 +02002914 expect(vc.builder().firstChild.children[2].firstChild.value).toEqual('');
Akron8db5e3a2018-05-28 19:25:26 +02002915
Akron31d89942018-04-06 16:44:51 +02002916 // blur
2917 document.body.click();
2918 });
2919
2920 it('should be clickable on operation for date', function () {
2921
2922 // Replay matchop check - so it's guaranteed that "ne" is chosen
2923 // Click on "e" (or unspecified)
Akronadab5e52018-08-20 13:50:53 +02002924 vc.builder().firstChild.firstChild.click();
Akron31d89942018-04-06 16:44:51 +02002925 // Rechoose "e"
Akronadab5e52018-08-20 13:50:53 +02002926 vc.builder().firstChild.firstChild.getElementsByTagName("LI")[1].click();
Akron31d89942018-04-06 16:44:51 +02002927 // Click on matchop
Akronadab5e52018-08-20 13:50:53 +02002928 vc.builder().firstChild.children[1].click();
Akron31d89942018-04-06 16:44:51 +02002929 // Choose "ne"
Akronadab5e52018-08-20 13:50:53 +02002930 vc.builder().firstChild.children[1].getElementsByTagName('li')[1].click();
2931 expect(vc.builder().firstChild.children[1].innerText).toEqual("ne");
Akron31d89942018-04-06 16:44:51 +02002932
2933 // Click on "e"
Akronadab5e52018-08-20 13:50:53 +02002934 vc.builder().firstChild.firstChild.click();
Akron31d89942018-04-06 16:44:51 +02002935 // Choose "f"
Akronadab5e52018-08-20 13:50:53 +02002936 vc.builder().firstChild.firstChild.getElementsByTagName("LI")[3].click();
Akron31d89942018-04-06 16:44:51 +02002937
2938 // The matchoperator should still be "ne" as this is valid for dates as well (now)
Akronadab5e52018-08-20 13:50:53 +02002939 var fc = vc.builder().firstChild;
Akron31d89942018-04-06 16:44:51 +02002940 expect(fc.firstChild.tagName).toEqual('SPAN');
2941 expect(fc.firstChild.innerText).toEqual('f');
2942 expect(fc.children[1].innerText).toEqual('ne');
2943 // blur
2944 document.body.click();
2945 });
Akronddc98a72018-04-06 17:33:52 +02002946
2947
2948 // Check json deserialization
2949 it('should be initializable', function () {
2950 vc = vcClass.create([
2951 ['a', 'text'],
2952 ['b', 'string'],
2953 ['c', 'date']
2954 ]).fromJson({
2955 "@type" : "koral:doc",
2956 "key":"a",
2957 "value":"Baum"
2958 });
2959
Akronadab5e52018-08-20 13:50:53 +02002960 expect(vc.builder().firstChild.children[1].getAttribute('data-type')).toEqual('text');
Akronddc98a72018-04-06 17:33:52 +02002961 });
Akron712733a2018-04-05 18:17:47 +02002962 });
2963
2964
Akrone4961b12017-05-10 21:04:46 +02002965 // Check prefix
2966 describe('KorAP.VC.Prefix', function () {
2967
2968 it('should be initializable', function () {
2969 var p = prefixClass.create();
2970 expect(p.element().classList.contains('pref')).toBeTruthy();
2971 expect(p.isSet()).not.toBeTruthy();
2972 });
2973
2974
2975 it('should be clickable', function () {
hebastaa0282be2018-12-05 16:58:00 +01002976 KorAP.vc = vcClass.create([
Akron712733a2018-04-05 18:17:47 +02002977 ['a', null],
2978 ['b', null],
2979 ['c', null]
2980 ]).fromJson();
hebastaa0282be2018-12-05 16:58:00 +01002981
2982 //vc = KorAP.vc;
2983
2984 expect(KorAP.vc.builder().firstChild.classList.contains('unspecified')).toBeTruthy();
Akrone4961b12017-05-10 21:04:46 +02002985
2986 // This should open up the menu
hebastaa0282be2018-12-05 16:58:00 +01002987 KorAP.vc.builder().firstChild.firstChild.click();
2988 expect(KorAP.vc.builder().firstChild.firstChild.tagName).toEqual('UL');
Akrone4961b12017-05-10 21:04:46 +02002989
2990 KorAP._vcKeyMenu._prefix.clear();
2991 KorAP._vcKeyMenu._prefix.add('x');
2992
hebastaa0282be2018-12-05 16:58:00 +01002993 var prefElement = KorAP.vc.builder().querySelector('span.pref');
Akrone4961b12017-05-10 21:04:46 +02002994 expect(prefElement.innerText).toEqual('x');
2995
2996 // This should add key 'x' to VC
2997 prefElement.click();
2998
hebastaa0282be2018-12-05 16:58:00 +01002999 expect(KorAP.vc.builder().firstChild.classList.contains('doc')).toBeTruthy();
3000 expect(KorAP.vc.builder().firstChild.firstChild.className).toEqual('key');
3001 expect(KorAP.vc.builder().firstChild.firstChild.innerText).toEqual('x');
Akrone4961b12017-05-10 21:04:46 +02003002 });
3003 });
Akron68d28322018-08-27 15:02:42 +02003004
Akron889ec292018-11-19 17:56:01 +01003005 // Check fragment handling for corpusByMatch helper
Akron68d28322018-08-27 15:02:42 +02003006 describe('KorAP.VC.Fragment', function () {
3007 it('should be initializable', function () {
3008 var f = fragmentClass.create();
3009 var e = f.element();
3010 expect(e.classList.contains('vc')).toBeTruthy();
3011 expect(e.classList.contains('fragment')).toBeTruthy();
Akrond45a1702018-11-19 18:15:17 +01003012 expect(e.firstChild.children.length).toEqual(0);
Akron68d28322018-08-27 15:02:42 +02003013 });
3014
3015 it('should be expansable', function () {
3016 var f = fragmentClass.create();
3017 f.add("author", "Peter");
3018
Akrond45a1702018-11-19 18:15:17 +01003019 var root = f.element().lastChild.firstChild;
Akron68d28322018-08-27 15:02:42 +02003020
3021 expect(root.classList.contains("doc")).toBeTruthy();
3022 expect(root.children[0].tagName).toEqual("SPAN");
3023 expect(root.children[0].textContent).toEqual("author");
3024 expect(root.children[1].tagName).toEqual("SPAN");
3025 expect(root.children[1].textContent).toEqual("eq");
3026 expect(root.children[2].tagName).toEqual("SPAN");
3027 expect(root.children[2].textContent).toEqual("Peter");
3028
3029 f.add("title", "Example");
3030
Akrond45a1702018-11-19 18:15:17 +01003031 root = f.element().lastChild.firstChild;
Akron68d28322018-08-27 15:02:42 +02003032
3033 expect(root.classList.contains("docGroup")).toBeTruthy();
3034
3035 var doc = root.children[0];
3036
3037 expect(doc.children[0].tagName).toEqual("SPAN");
3038 expect(doc.children[0].textContent).toEqual("author");
3039 expect(doc.children[1].tagName).toEqual("SPAN");
3040 expect(doc.children[1].textContent).toEqual("eq");
3041 expect(doc.children[2].tagName).toEqual("SPAN");
3042 expect(doc.children[2].textContent).toEqual("Peter");
3043
3044 doc = root.children[1];
3045
3046 expect(doc.children[0].tagName).toEqual("SPAN");
3047 expect(doc.children[0].textContent).toEqual("title");
3048 expect(doc.children[1].tagName).toEqual("SPAN");
3049 expect(doc.children[1].textContent).toEqual("eq");
3050 expect(doc.children[2].tagName).toEqual("SPAN");
3051 expect(doc.children[2].textContent).toEqual("Example");
3052 });
3053
3054
3055 it('should be reducible', function () {
3056 var f = fragmentClass.create();
Akrond45a1702018-11-19 18:15:17 +01003057 expect(f.element().lastChild.children.length).toEqual(0);
Akron889ec292018-11-19 17:56:01 +01003058
Akron68d28322018-08-27 15:02:42 +02003059 f.add("author", "Peter");
3060 f.add("title", "Example");
3061
Akrond45a1702018-11-19 18:15:17 +01003062 expect(f.element().lastChild.children.length).toEqual(1);
Akron889ec292018-11-19 17:56:01 +01003063
Akrond45a1702018-11-19 18:15:17 +01003064 var root = f.element().lastChild.firstChild;
Akron889ec292018-11-19 17:56:01 +01003065
Akron68d28322018-08-27 15:02:42 +02003066 expect(root.classList.contains("docGroup")).toBeTruthy();
3067
3068 expect(root.children.length).toEqual(2);
3069
3070 f.remove("author","Peter");
3071
Akrond45a1702018-11-19 18:15:17 +01003072 root = f.element().lastChild.firstChild;
Akron68d28322018-08-27 15:02:42 +02003073 expect(root.classList.contains("doc")).toBeTruthy();
3074
3075 expect(root.children[0].tagName).toEqual("SPAN");
3076 expect(root.children[0].textContent).toEqual("title");
3077 expect(root.children[1].tagName).toEqual("SPAN");
3078 expect(root.children[1].textContent).toEqual("eq");
3079 expect(root.children[2].tagName).toEqual("SPAN");
3080 expect(root.children[2].textContent).toEqual("Example");
Akron889ec292018-11-19 17:56:01 +01003081
3082 f.remove("title","Example");
3083
Akrond45a1702018-11-19 18:15:17 +01003084 expect(f.element().lastChild.children.length).toEqual(0);
Akron68d28322018-08-27 15:02:42 +02003085 });
3086 });
Nils Diewald2fe12e12015-03-06 16:47:06 +00003087});