blob: f657ee9165a5c92cf3ca0d824753a9dcac1a35c7 [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
Nils Diewald6ac292b2015-01-15 21:33:21 +000030
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");
149 });
150
151 it('should be definable', function () {
152
153 // Empty doc
154 var doc = docClass.create();
155
156 // Set values
157 doc.key("title");
Akron8db5e3a2018-05-28 19:25:26 +0200158
Nils Diewald7c8ced22015-04-15 19:21:00 +0000159 doc.value("Der alte Mann");
160 expect(doc.matchop()).toEqual('eq');
161 expect(doc.key()).toEqual("title");
162 expect(doc.type()).toEqual("string");
163 expect(doc.value()).toEqual("Der alte Mann");
164 });
165
166
167 it('should deserialize JSON-LD string', function () {
168 var doc;
169
170 // String default
171 doc = stringFactory.create();
172 expect(doc.matchop()).toEqual('eq');
173 expect(doc.key()).toEqual("author");
174 expect(doc.type()).toEqual("string");
175 expect(doc.value()).toEqual("Max Birkendale");
176
177 // No valid string
178 doc = stringFactory.create({
Akron712733a2018-04-05 18:17:47 +0200179 value : undefined
Nils Diewald7c8ced22015-04-15 19:21:00 +0000180 });
181 expect(doc).toBeUndefined();
182
183 // No valid string
184 doc = stringFactory.create({
Akron712733a2018-04-05 18:17:47 +0200185 value : { "foo" : "bar" }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000186 });
187 expect(doc).toBeUndefined();
188
189 // Change match type
190 doc = stringFactory.create({
Akron712733a2018-04-05 18:17:47 +0200191 "match" : "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000192 });
193
194 expect(doc.matchop()).toEqual('ne');
195 expect(doc.key()).toEqual("author");
196 expect(doc.type()).toEqual("string");
197 expect(doc.value()).toEqual("Max Birkendale");
198
199 // Invalid match type
200 doc = stringFactory.create({
Akron712733a2018-04-05 18:17:47 +0200201 "match" : { "foo" : "bar" }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000202 });
203 expect(doc).toBeUndefined();
204 });
205
206 it('should deserialize JSON-LD regex', function () {
207 var doc = regexFactory.create();
208 expect(doc.key()).toEqual("title");
209 expect(doc.type()).toEqual("regex");
210 expect(doc.value()).toEqual("[^b]ee.+?");
211 expect(doc.matchop()).toEqual('eq');
212
213 // change matcher
214 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200215 match : "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000216 });
217 expect(doc.matchop()).toEqual('ne');
Akronea4e9052017-07-06 16:12:05 +0200218 expect(doc.rewrites()).toBeUndefined();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000219
220 // Invalid matcher
221 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200222 match : "match:chook"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000223 });
Akronea4e9052017-07-06 16:12:05 +0200224 expect(doc.matchop()).toEqual('eq');
225 expect(doc.rewrites()).toBeDefined();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000226
227 // Invalid regex
228 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200229 value : "[^b"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000230 });
231 expect(doc).toBeUndefined();
232 });
233
234 it('should deserialize JSON-LD date', function () {
235
236 // Normal date
237 doc = dateFactory.create({});
238
239 expect(doc.matchop()).toEqual('eq');
240 expect(doc.key()).toEqual("pubDate");
241 expect(doc.type()).toEqual("date");
242 expect(doc.value()).toEqual("2014-11-05");
243
244 // Short date 1
245 doc = dateFactory.create({
Akron712733a2018-04-05 18:17:47 +0200246 "value" : "2014-11"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000247 });
248
249 expect(doc.matchop()).toEqual('eq');
250 expect(doc.key()).toEqual("pubDate");
251 expect(doc.type()).toEqual("date");
252 expect(doc.value()).toEqual("2014-11");
253
254 // Short date 2
255 doc = dateFactory.create({
Akron712733a2018-04-05 18:17:47 +0200256 "value" : "2014"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000257 });
258
259 expect(doc.matchop()).toEqual('eq');
260 expect(doc.key()).toEqual("pubDate");
261 expect(doc.type()).toEqual("date");
262 expect(doc.value()).toEqual("2014");
263
264 // Invalid date!
265 doc = dateFactory.create({
Akron712733a2018-04-05 18:17:47 +0200266 "value" : "2014-11-050"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000267 });
268 expect(doc).toBeUndefined();
269
270 // Invalid matcher!
271 doc = dateFactory.create({
Akron31d89942018-04-06 16:44:51 +0200272 "match" : "match:contains",
Nils Diewald7c8ced22015-04-15 19:21:00 +0000273 });
Akronea4e9052017-07-06 16:12:05 +0200274 expect(doc).toBeDefined();
275 expect(doc.rewrites()).toBeDefined();
276 expect(doc.matchop()).toEqual('eq');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000277 });
278
279 it('should be serializale to JSON', function () {
280
281 // Empty doc
282 var doc = docClass.create();
283 expect(doc.toJson()).toEqual(jasmine.any(Object));
284
285 // Serialize string
286 doc = stringFactory.create();
287 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200288 "@type" : "koral:doc",
289 "type" : "type:string",
290 "key" : "author",
291 "value" : "Max Birkendale",
292 "match" : "match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000293 }));
294
295 // Serialize regex
296 doc = regexFactory.create();
297 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200298 "@type" : "koral:doc",
299 "type" : "type:regex",
300 "value" : "[^b]ee.+?",
301 "match" : "match:eq",
302 "key" : 'title'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000303 }));
304
305 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200306 match: "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000307 });
308 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200309 "@type" : "koral:doc",
310 "type" : "type:regex",
311 "value" : "[^b]ee.+?",
312 "match" : "match:ne",
313 "key" : 'title'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000314 }));
315
316 doc = dateFactory.create();
317 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200318 "@type" : "koral:doc",
319 "type" : "type:date",
320 "value" : "2014-11-05",
321 "match" : "match:eq",
322 "key" : 'pubDate'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000323 }));
324
325 doc = dateFactory.create({
Akron712733a2018-04-05 18:17:47 +0200326 value : "2014"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000327 });
328 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200329 "@type" : "koral:doc",
330 "type" : "type:date",
331 "value" : "2014",
332 "match" : "match:eq",
333 "key" : 'pubDate'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000334 }));
335 });
336
337
338 it('should be serializale to String', function () {
339 // Empty doc
340 var doc = docClass.create();
Akronebc96662018-08-29 17:36:20 +0200341
Nils Diewald7c8ced22015-04-15 19:21:00 +0000342 expect(doc.toQuery()).toEqual("");
343
344 // Serialize string
345 doc = stringFactory.create();
346 expect(doc.toQuery()).toEqual('author = "Max Birkendale"');
347
348 // Serialize string with quotes
349 doc = stringFactory.create({ "value" : 'Max "Der Coole" Birkendate'});
350 expect(doc.toQuery()).toEqual('author = "Max \\"Der Coole\\" Birkendate"');
351
352 // Serialize regex
353 doc = regexFactory.create();
354 expect(doc.toQuery()).toEqual('title = /[^b]ee.+?/');
355
356 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200357 match: "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000358 });
359 expect(doc.toQuery()).toEqual('title != /[^b]ee.+?/');
360
Akron8778f5d2017-06-30 21:25:55 +0200361 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200362 value: "WPD/AAA/00001"
Akron8778f5d2017-06-30 21:25:55 +0200363 });
364 expect(doc.toQuery()).toEqual('title = /WPD\\/AAA\\/00001/');
365
Nils Diewald7c8ced22015-04-15 19:21:00 +0000366 doc = dateFactory.create();
367 expect(doc.toQuery()).toEqual('pubDate in 2014-11-05');
368
369 doc = dateFactory.create({
Akron712733a2018-04-05 18:17:47 +0200370 value : "2014"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000371 });
372 expect(doc.toQuery()).toEqual('pubDate in 2014');
373 });
374 });
375
376
377 describe('KorAP.DocGroup', function () {
378 // Create example factories
379 var docFactory = buildFactory(
380 docClass,
Nils Diewaldf219eb82015-01-07 20:15:42 +0000381 {
Akron712733a2018-04-05 18:17:47 +0200382 "@type" : "koral:doc",
383 "match":"match:eq",
384 "key" : "author",
385 "value" : "Max Birkendale"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000386 }
387 );
388
389 var docGroupFactory = buildFactory(
390 docGroupClass, {
Akron712733a2018-04-05 18:17:47 +0200391 "@type" : "koral:docGroup",
392 "operation" : "operation:and",
393 "operands" : [
394 docFactory.create().toJson(),
395 docFactory.create({
396 "key" : "pubDate",
397 "type" : "type:date",
398 "value" : "2014-12-05"
399 }).toJson()
400 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000401 });
402
403 it('should be initializable', function () {
404 // Create empty group
405 var docGroup = docGroupClass.create();
406 expect(docGroup.operation()).toEqual('and');
407
408 // Create empty group
409 docGroup = docGroupClass.create();
410 docGroup.operation('or');
411 expect(docGroup.operation()).toEqual('or');
412 });
413
414 it('should be definable', function () {
415
416 // Empty group
417 var docGroup = docGroupClass.create();
418 expect(docGroup.operation()).toEqual('and');
419
420 // Set values
421 docGroup.operation("or");
422 expect(docGroup.operation()).toEqual('or');
423
424 // Set invalid values
425 docGroup.operation("hui");
426 expect(docGroup.operation()).toEqual('or');
427 });
428
429 it('should be deserializable', function () {
Akronadab5e52018-08-20 13:50:53 +0200430
431 // Reset list
432 KorAP._vcKeyMenu = menuClass.create([['referTo','ref']]);
433
Nils Diewald7c8ced22015-04-15 19:21:00 +0000434 var docGroup = docGroupFactory.create();
435 expect(docGroup.operation()).toEqual("and");
436 expect(docGroup.operands().length).toEqual(2);
437
438 var op1 = docGroup.getOperand(0);
439 expect(op1.type()).toEqual("string");
440 expect(op1.key()).toEqual("author");
441 expect(op1.value()).toEqual("Max Birkendale");
442 expect(op1.matchop()).toEqual("eq");
443
444 var op2 = docGroup.getOperand(1);
445 expect(op2.type()).toEqual("date");
446 expect(op2.key()).toEqual("pubDate");
447 expect(op2.value()).toEqual("2014-12-05");
448 expect(op2.matchop()).toEqual("eq");
449
450 // Append empty group
451 var newGroup = docGroup.append(docGroupClass.create());
452 newGroup.operation('or');
453 newGroup.append(docFactory.create());
454 newGroup.append(docFactory.create({
Akron712733a2018-04-05 18:17:47 +0200455 "type" : "type:regex",
456 "key" : "title",
457 "value" : "^e.+?$",
458 "match" : "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000459 }));
460
461 expect(docGroup.operation()).toEqual("and");
462 expect(docGroup.operands().length).toEqual(3);
463
464 var op1 = docGroup.getOperand(0);
465 expect(op1.ldType()).toEqual("doc");
466 expect(op1.type()).toEqual("string");
467 expect(op1.key()).toEqual("author");
468 expect(op1.value()).toEqual("Max Birkendale");
469 expect(op1.matchop()).toEqual("eq");
470
471 var op2 = docGroup.getOperand(1);
472 expect(op2.ldType()).toEqual("doc");
473 expect(op2.type()).toEqual("date");
474 expect(op2.key()).toEqual("pubDate");
475 expect(op2.value()).toEqual("2014-12-05");
476 expect(op2.matchop()).toEqual("eq");
477
478 var op3 = docGroup.getOperand(2);
479 expect(op3.ldType()).toEqual("docGroup");
480 expect(op3.operation()).toEqual("or");
Akronadab5e52018-08-20 13:50:53 +0200481
Nils Diewald7c8ced22015-04-15 19:21:00 +0000482 var op4 = op3.getOperand(0);
483 expect(op4.ldType()).toEqual("doc");
484 expect(op4.type()).toEqual("string");
485 expect(op4.key()).toEqual("author");
486 expect(op4.value()).toEqual("Max Birkendale");
487 expect(op4.matchop()).toEqual("eq");
488
489 var op5 = op3.getOperand(1);
490 expect(op5.ldType()).toEqual("doc");
491 expect(op5.type()).toEqual("regex");
492 expect(op5.key()).toEqual("title");
493 expect(op5.value()).toEqual("^e.+?$");
494 expect(op5.matchop()).toEqual("ne");
495 });
496
497 it('should be serializable to JSON', function () {
498 var docGroup = docGroupFactory.create();
499
500 expect(docGroup.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200501 "@type" : "koral:docGroup",
502 "operation" : "operation:and",
503 "operands" : [
504 {
505 "@type": 'koral:doc',
506 "key" : 'author',
507 "match": 'match:eq',
508 "value": 'Max Birkendale',
509 "type": 'type:string'
510 },
511 {
512 "@type": 'koral:doc',
513 "key": 'pubDate',
514 "match": 'match:eq',
515 "value": '2014-12-05',
516 "type": 'type:date'
517 }
518 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000519 }));
520 });
Nils Diewalde15b7a22015-01-09 21:50:21 +0000521
Nils Diewald7c8ced22015-04-15 19:21:00 +0000522 it('should be serializable to String', function () {
523 var docGroup = docGroupFactory.create();
524 expect(docGroup.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +0200525 'author = "Max Birkendale" & pubDate in 2014-12-05'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000526 );
Nils Diewald52f7eb12015-01-12 17:30:04 +0000527
Nils Diewald7c8ced22015-04-15 19:21:00 +0000528 docGroup = docGroupFactory.create({
Akron712733a2018-04-05 18:17:47 +0200529 "@type" : "koral:docGroup",
530 "operation" : "operation:or",
531 "operands" : [
532 {
533 "@type": 'koral:doc',
534 "key" : 'author',
535 "match": 'match:eq',
536 "value": 'Max Birkendale',
537 "type": 'type:string'
538 },
539 {
540 "@type" : "koral:docGroup",
541 "operation" : "operation:and",
542 "operands" : [
543 {
544 "@type": 'koral:doc',
545 "key": 'pubDate',
546 "match": 'match:geq',
547 "value": '2014-05-12',
548 "type": 'type:date'
549 },
550 {
551 "@type": 'koral:doc',
552 "key": 'pubDate',
553 "match": 'match:leq',
554 "value": '2014-12-05',
555 "type": 'type:date'
556 },
557 {
558 "@type": 'koral:doc',
559 "key": 'foo',
560 "match": 'match:ne',
561 "value": '[a]?bar',
562 "type": 'type:regex'
563 }
564 ]
565 }
566 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000567 });
568 expect(docGroup.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +0200569 'author = "Max Birkendale" | ' +
570 '(pubDate since 2014-05-12 & ' +
571 'pubDate until 2014-12-05 & foo != /[a]?bar/)'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000572 );
573 });
574 });
575
Akronb19803c2018-08-16 16:39:42 +0200576 describe('KorAP.DocGroupRef', function () {
577 // Create example factories
578 var docRefFactory = buildFactory(
579 docGroupRefClass,
580 {
581 "@type" : "koral:docGroupRef",
582 "ref" : "@max/myCorpus"
583 }
584 );
585
586 it('should be initializable', function () {
587 var vcRef = docGroupRefClass.create();
588 expect(vcRef.ref()).toBeUndefined();
589 });
590
591 it('should be definable', function () {
592 var vcRef = docGroupRefClass.create();
593 vcRef.ref("@peter/mycorpus");
594 expect(vcRef.ref()).toEqual("@peter/mycorpus");
595 vcRef.ref("@peter/mycorpus2");
596 expect(vcRef.ref()).toEqual("@peter/mycorpus2");
597 });
598
599 it('should deserialize JSON-LD string', function () {
600 var vcRef = docRefFactory.create();
601 expect(vcRef.ref()).toEqual("@max/myCorpus");
602 });
603
604 it('should serialize to JSON-LD', function () {
605 var vcRef = docRefFactory.create();
606 expect(vcRef.toJson()).toEqual(jasmine.objectContaining({
607 "@type" : "koral:docGroupRef",
608 "ref":"@max/myCorpus"
609 }));
610
611 vcRef.ref("@peter/myCorpus2");
612 expect(vcRef.toJson()).toEqual(jasmine.objectContaining({
613 "@type" : "koral:docGroupRef",
614 "ref":"@peter/myCorpus2"
615 }));
616 });
617
618 it('should serialize to a query', function () {
619 var vcRef = docRefFactory.create();
620 expect(vcRef.toQuery()).toEqual(
621 "referTo \"@max/myCorpus\""
622 );
623
624 vcRef.ref("@peter/myCorpus2");
625 expect(vcRef.toQuery()).toEqual(
626 "referTo \"@peter/myCorpus2\""
627 );
628 });
629 });
630
631
Nils Diewald7c8ced22015-04-15 19:21:00 +0000632 describe('KorAP.UnspecifiedDoc', function () {
633 it('should be initializable', function () {
634 var doc = unspecifiedClass.create();
635 var docElement = doc.element();
636 expect(docElement.getAttribute('class')).toEqual('doc unspecified');
Akronebc96662018-08-29 17:36:20 +0200637 expect(docElement.firstChild.firstChild.data).toEqual(KorAP.Locale.EMPTY);
638 expect(docElement.lastChild.lastChild.data).toEqual(KorAP.Locale.EMPTY);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000639 expect(doc.toQuery()).toEqual('');
640
641 // Only removable
642 expect(docElement.lastChild.children.length).toEqual(0);
643 });
644
Akrond141a362018-07-10 18:12:13 +0200645 it('should be removable, when no root', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000646 var docGroup = docGroupClass.create();
647 docGroup.operation('or');
648 expect(docGroup.operation()).toEqual('or');
649
650 docGroup.append({
Akron712733a2018-04-05 18:17:47 +0200651 "@type": 'koral:doc',
652 "key": 'pubDate',
653 "match": 'match:eq',
654 "value": '2014-12-05',
655 "type": 'type:date'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000656 });
657
658 // Add unspecified object
659 docGroup.append();
660
Akrond141a362018-07-10 18:12:13 +0200661 var parent = document.createElement('div');
662 parent.appendChild(docGroup.element());
663
Nils Diewald7c8ced22015-04-15 19:21:00 +0000664 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
665 expect(docGroup.element().children[0].getAttribute('class')).toEqual('doc');
666
667 var unspec = docGroup.element().children[1];
668 expect(unspec.getAttribute('class')).toEqual('doc unspecified');
669
Akronb19803c2018-08-16 16:39:42 +0200670 // Only unspec and delete
671 expect(unspec.children.length).toEqual(2);
672
Nils Diewald7c8ced22015-04-15 19:21:00 +0000673 // Removable
674 expect(unspec.lastChild.children.length).toEqual(1);
675 expect(unspec.lastChild.children[0].getAttribute('class')).toEqual('delete');
676 });
677
Akrond141a362018-07-10 18:12:13 +0200678
Nils Diewald7c8ced22015-04-15 19:21:00 +0000679 it('should be replaceable by a doc', function () {
680 var doc = unspecifiedClass.create();
681 expect(doc.ldType()).toEqual("non");
682 // No parent, therefor not updateable
683 expect(doc.key("baum")).toBeNull();
684
685 var docGroup = docGroupClass.create();
686 docGroup.operation('or');
687 expect(docGroup.operation()).toEqual('or');
688
689 docGroup.append({
Akron712733a2018-04-05 18:17:47 +0200690 "@type": 'koral:doc',
691 "key": 'pubDate',
692 "match": 'match:eq',
693 "value": '2014-12-05',
694 "type": 'type:date'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000695 });
696
697 expect(docGroup.toQuery()).toEqual("pubDate in 2014-12-05");
698 docGroup.append();
699
700 expect(docGroup.getOperand(0).ldType()).toEqual("doc");
701 expect(docGroup.getOperand(1).ldType()).toEqual("non");
702
703 var op = docGroup.getOperand(1).element().lastChild;
Akron0b489ad2018-02-02 16:49:32 +0100704 expect(op.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000705 expect(op.children[0].getAttribute('class')).toEqual('delete');
706 expect(op.children.length).toEqual(1);
707
708 // Replace unspecified doc
709 expect(docGroup.getOperand(1).key("name")).not.toBeNull();
710 expect(docGroup.getOperand(1).ldType()).toEqual("doc");
711 expect(docGroup.getOperand(1).key()).toEqual("name");
Akron55a343b2018-04-06 19:57:36 +0200712 expect(docGroup.getOperand(1).value()).toBeUndefined();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000713
Akronb19803c2018-08-16 16:39:42 +0200714 expect(docGroup.getOperand(1).element().children.length).toEqual(4);
715
Nils Diewald7c8ced22015-04-15 19:21:00 +0000716 op = docGroup.getOperand(1).element().lastChild;
Akron0b489ad2018-02-02 16:49:32 +0100717 expect(op.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000718 expect(op.children[0].getAttribute('class')).toEqual('and');
719 expect(op.children[1].getAttribute('class')).toEqual('or');
720 expect(op.children[2].getAttribute('class')).toEqual('delete');
Akronb19803c2018-08-16 16:39:42 +0200721
Nils Diewald7c8ced22015-04-15 19:21:00 +0000722 expect(op.children.length).toEqual(3);
723
724 docGroup.getOperand(1).value("Pachelbel");
725 expect(docGroup.getOperand(1).value()).toEqual("Pachelbel");
726 expect(docGroup.getOperand(1).type()).toEqual("string");
727 expect(docGroup.getOperand(1).matchop()).toEqual("eq");
728
729 // Specified!
730 expect(docGroup.toQuery()).toEqual('pubDate in 2014-12-05 | name = "Pachelbel"');
731 });
Akronb19803c2018-08-16 16:39:42 +0200732
Nils Diewald7c8ced22015-04-15 19:21:00 +0000733 it('should be replaceable on root', function () {
Nils Diewald6283d692015-04-23 20:32:53 +0000734 var vc = vcClass.create();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000735 expect(vc.toQuery()).toEqual("");
736
737 expect(vc.root().ldType()).toEqual("non");
738
739 // No operators on root
740 op = vc.root().element().lastChild;
Akronebc96662018-08-29 17:36:20 +0200741 expect(op.lastChild.textContent).toEqual(KorAP.Locale.EMPTY);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000742
743 // Replace
744 expect(vc.root().key("baum")).not.toBeNull();
745 expect(vc.root().ldType()).toEqual("doc");
746
747 op = vc.root().element().lastChild;
Akron0b489ad2018-02-02 16:49:32 +0100748 expect(op.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000749 expect(op.children[0].getAttribute('class')).toEqual('and');
750 expect(op.children[1].getAttribute('class')).toEqual('or');
751 expect(op.children[2].getAttribute('class')).toEqual('delete');
752 expect(op.children.length).toEqual(3);
753 });
Akron55a343b2018-04-06 19:57:36 +0200754
755 it('should be clickable', function () {
756 var vc = vcClass.create([
757 ["pubDate", "date"]
758 ]);
759 expect(vc.toQuery()).toEqual("");
Akronebc96662018-08-29 17:36:20 +0200760 expect(vc.builder().firstChild.textContent).toEqual(KorAP.Locale.EMPTY);
761 expect(vc.builder().firstChild.classList.contains('unspecified')).toEqual(true);
Akronadab5e52018-08-20 13:50:53 +0200762 vc.builder().firstChild.firstChild.click();
Akron55a343b2018-04-06 19:57:36 +0200763
764 // Click on pubDate
Akron3ad46942018-08-22 16:47:14 +0200765 vc.element().firstChild.getElementsByTagName("LI")[1].click();
Akronadab5e52018-08-20 13:50:53 +0200766 expect(vc.builder().firstChild.firstChild.textContent).toEqual("pubDate");
767 expect(vc.builder().firstChild.children[1].getAttribute("data-type")).toEqual("date");
Akron55a343b2018-04-06 19:57:36 +0200768 });
Nils Diewald7c8ced22015-04-15 19:21:00 +0000769 });
770
771 describe('KorAP.Doc element', function () {
772 it('should be initializable', function () {
773 var docElement = docClass.create(undefined, {
Akron712733a2018-04-05 18:17:47 +0200774 "@type" : "koral:doc",
775 "key":"Titel",
776 "value":"Baum",
777 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000778 });
779 expect(docElement.key()).toEqual('Titel');
780 expect(docElement.matchop()).toEqual('eq');
781 expect(docElement.value()).toEqual('Baum');
782
783 var docE = docElement.element();
784 expect(docE.children[0].firstChild.data).toEqual('Titel');
785 expect(docE.children[1].firstChild.data).toEqual('eq');
786 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
787 expect(docE.children[2].firstChild.data).toEqual('Baum');
788 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
789
790 expect(docElement.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200791 "@type" : "koral:doc",
792 "key":"Titel",
793 "value":"Baum",
794 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000795 }));
796 });
Akronb19803c2018-08-16 16:39:42 +0200797
798
799 it('should be replacable by unspecified', function () {
800 var vc = vcClass.create([
801 ["pubDate", "date"]
802 ]).fromJson({
803 "@type" : "koral:doc",
804 "key":"Titel",
805 "value":"Baum",
806 "match":"match:eq"
807 });
808 expect(vc.toQuery()).toEqual("Titel = \"Baum\"");
809
Akronadab5e52018-08-20 13:50:53 +0200810 var vcE = vc.builder();
Akronb19803c2018-08-16 16:39:42 +0200811 expect(vcE.firstChild.children.length).toEqual(4);
812
813 // Click to delete
814 vcE.firstChild.lastChild.lastChild.click();
815
816 expect(vcE.firstChild.children.length).toEqual(1);
817
Akronebc96662018-08-29 17:36:20 +0200818 expect(vcE.firstChild.textContent).toEqual(KorAP.Locale.EMPTY);
819 expect(vcE.firstChild.classList.contains("unspecified")).toBeTruthy();
Akronb19803c2018-08-16 16:39:42 +0200820 vcE.firstChild.firstChild.click();
821
822 // Click on pubDate
Akron3ad46942018-08-22 16:47:14 +0200823 vcE.firstChild.getElementsByTagName("LI")[1].click();
Akronb19803c2018-08-16 16:39:42 +0200824
825 expect(vcE.firstChild.firstChild.textContent).toEqual("pubDate");
826 expect(vcE.firstChild.children[1].getAttribute("data-type")).toEqual("date");
827
828 expect(vcE.firstChild.children.length).toEqual(4);
829 });
830
831
Akron587e4d92018-08-31 12:44:26 +0200832 it('should be replaceable by docGroupRef with deletion', function () {
Akron3ad46942018-08-22 16:47:14 +0200833 var vc = vcClass.create().fromJson({
Akronb19803c2018-08-16 16:39:42 +0200834 "@type" : "koral:doc",
835 "key":"Titel",
836 "value":"Baum",
837 "match":"match:eq"
838 });
839
840 expect(vc.toQuery()).toEqual("Titel = \"Baum\"");
841
Akronadab5e52018-08-20 13:50:53 +0200842 var vcE = vc.builder();
Akronb19803c2018-08-16 16:39:42 +0200843 expect(vcE.firstChild.children.length).toEqual(4);
844
845 // Click to delete
846 vcE.firstChild.lastChild.lastChild.click();
847
848 expect(vcE.firstChild.children.length).toEqual(1);
849
Akronebc96662018-08-29 17:36:20 +0200850 expect(vcE.firstChild.textContent).toEqual(KorAP.Locale.EMPTY);
851 expect(vcE.firstChild.classList.contains('unspecified')).toEqual(true);
Akronb19803c2018-08-16 16:39:42 +0200852 vcE.firstChild.firstChild.click();
853
Akron3ad46942018-08-22 16:47:14 +0200854 // Click on referTo
Akronb19803c2018-08-16 16:39:42 +0200855 vcE.firstChild.getElementsByTagName("LI")[0].click();
856
Akron3ad46942018-08-22 16:47:14 +0200857 expect(vcE.firstChild.firstChild.textContent).toEqual("referTo");
Akronb19803c2018-08-16 16:39:42 +0200858 expect(vcE.firstChild.children[1].getAttribute("data-type")).toEqual("string");
Akron587e4d92018-08-31 12:44:26 +0200859
Akronb19803c2018-08-16 16:39:42 +0200860 expect(vcE.firstChild.children.length).toEqual(3);
Akron62ac95b2018-08-30 18:08:25 +0200861 expect(vcE.firstChild.children[1].classList.contains("unspecified")).toBeTruthy();
Akronb19803c2018-08-16 16:39:42 +0200862 });
Akron587e4d92018-08-31 12:44:26 +0200863
864
865 it('should be replaceable by docGroupRef on root', function () {
866 var vc = vcClass.create().fromJson({
867 "@type" : "koral:doc",
868 "key":"Titel",
869 "value":"Baum",
870 "match":"match:eq"
871 });
872
873 expect(vc.toQuery()).toEqual("Titel = \"Baum\"");
874
875 var vcE = vc.builder();
876 expect(vcE.firstChild.children.length).toEqual(4);
877
878 // Click on the key
879 vcE.firstChild.firstChild.click();
880
881 expect(vcE.firstChild.getElementsByTagName("LI")[0].textContent).toEqual("referTo");
882
883 // Click on referTo
884 vcE.firstChild.getElementsByTagName("LI")[0].click();
885
886 // Now it's a referTo element
887 expect(vcE.firstChild.firstChild.textContent).toEqual("referTo");
888 expect(vcE.firstChild.children.length).toEqual(3);
889 expect(vcE.firstChild.children[1].classList.contains("unspecified")).toBeTruthy();
890 });
891
892 it('should be replaceable by docGroupRef nested', function () {
893 var vc = vcClass.create().fromJson({
894 "@type" : "koral:docGroup",
895 "operation" : "operation:and",
896 "operands" : [
897 {
898 "@type": 'koral:doc',
899 "key" : 'author',
900 "match": 'match:eq',
901 "value": 'Max Birkendale',
902 "type": 'type:string'
903 },
904 {
905 "@type": 'koral:doc',
906 "key": 'pubDate',
907 "match": 'match:eq',
908 "value": '2014-12-05',
909 "type": 'type:date'
910 }
911 ]
912 });
913
914 expect(vc.toQuery()).toEqual("author = \"Max Birkendale\" & pubDate in 2014-12-05");
915
916 var vcE = vc.builder();
917
918 // First doc
919 var doc = vcE.firstChild.firstChild;
920 expect(doc.children.length).toEqual(4);
921
922 // Click on the key
923 doc.firstChild.click();
924
925 expect(doc.getElementsByTagName("LI")[0].textContent).toEqual("referTo");
926
927 // Click on referTo
928 vcE.firstChild.getElementsByTagName("LI")[0].click();
929
930 // Now it's a referTo element
931 expect(vcE.firstChild.firstChild.firstChild.textContent).toEqual("referTo");
932 expect(vcE.firstChild.firstChild.children.length).toEqual(3);
933 expect(vcE.firstChild.firstChild.children[1].classList.contains("unspecified")).toBeTruthy();
934 });
Nils Diewald7c8ced22015-04-15 19:21:00 +0000935 });
936
937 describe('KorAP.DocGroup element', function () {
938 it('should be initializable', function () {
939
940 var docGroup = docGroupClass.create(undefined, {
Akron712733a2018-04-05 18:17:47 +0200941 "@type" : "koral:docGroup",
942 "operation" : "operation:and",
943 "operands" : [
944 {
945 "@type": 'koral:doc',
946 "key" : 'author',
947 "match": 'match:eq',
948 "value": 'Max Birkendale',
949 "type": 'type:string'
950 },
951 {
952 "@type": 'koral:doc',
953 "key": 'pubDate',
954 "match": 'match:eq',
955 "value": '2014-12-05',
956 "type": 'type:date'
957 }
958 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000959 });
960
961 expect(docGroup.operation()).toEqual('and');
962 var e = docGroup.element();
963 expect(e.getAttribute('class')).toEqual('docGroup');
964 expect(e.getAttribute('data-operation')).toEqual('and');
965
966 var first = e.children[0];
967 expect(first.getAttribute('class')).toEqual('doc');
968 expect(first.children[0].getAttribute('class')).toEqual('key');
969 expect(first.children[1].getAttribute('class')).toEqual('match');
970 expect(first.children[2].getAttribute('class')).toEqual('value');
971 expect(first.children[2].getAttribute('data-type')).toEqual('string');
972 expect(first.children[0].firstChild.data).toEqual('author');
973 expect(first.children[1].firstChild.data).toEqual('eq');
974 expect(first.children[2].firstChild.data).toEqual('Max Birkendale');
975
976 var second = e.children[1];
977 expect(second.getAttribute('class')).toEqual('doc');
978 expect(second.children[0].getAttribute('class')).toEqual('key');
979 expect(second.children[1].getAttribute('class')).toEqual('match');
980 expect(second.children[2].getAttribute('class')).toEqual('value');
981 expect(second.children[2].getAttribute('data-type')).toEqual('date');
982 expect(second.children[0].firstChild.data).toEqual('pubDate');
983 expect(second.children[1].firstChild.data).toEqual('eq');
984 expect(second.children[2].firstChild.data).toEqual('2014-12-05');
985 });
986
987 it('should be deserializable with nested groups', function () {
988 var docGroup = docGroupClass.create(undefined, {
Akron712733a2018-04-05 18:17:47 +0200989 "@type" : "koral:docGroup",
990 "operation" : "operation:or",
991 "operands" : [
992 {
993 "@type": 'koral:doc',
994 "key" : 'author',
995 "match": 'match:eq',
996 "value": 'Max Birkendale',
997 "type": 'type:string'
998 },
999 {
1000 "@type" : "koral:docGroup",
1001 "operation" : "operation:and",
1002 "operands" : [
1003 {
1004 "@type": 'koral:doc',
1005 "key": 'pubDate',
1006 "match": 'match:geq',
1007 "value": '2014-05-12',
1008 "type": 'type:date'
1009 },
1010 {
1011 "@type": 'koral:doc',
1012 "key": 'pubDate',
1013 "match": 'match:leq',
1014 "value": '2014-12-05',
1015 "type": 'type:date'
1016 }
1017 ]
1018 }
1019 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +00001020 });
1021
1022 expect(docGroup.operation()).toEqual('or');
1023 var e = docGroup.element();
1024 expect(e.getAttribute('class')).toEqual('docGroup');
1025 expect(e.getAttribute('data-operation')).toEqual('or');
1026
1027 expect(e.children[0].getAttribute('class')).toEqual('doc');
1028 var docop = e.children[0].lastChild;
Akron0b489ad2018-02-02 16:49:32 +01001029 expect(docop.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001030 expect(docop.children[0].getAttribute('class')).toEqual('and');
1031 expect(docop.children[1].getAttribute('class')).toEqual('or');
1032 expect(docop.children[2].getAttribute('class')).toEqual('delete');
1033
1034 expect(e.children[1].getAttribute('class')).toEqual('docGroup');
1035 expect(e.children[1].getAttribute('data-operation')).toEqual('and');
1036
1037 // This and-operation can be "or"ed or "delete"d
1038 var secop = e.children[1].children[2];
Akron0b489ad2018-02-02 16:49:32 +01001039 expect(secop.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001040 expect(secop.children[0].getAttribute('class')).toEqual('or');
1041 expect(secop.children[1].getAttribute('class')).toEqual('delete');
1042
1043 // This or-operation can be "and"ed or "delete"d
Akron0b489ad2018-02-02 16:49:32 +01001044 expect(e.children[2].getAttribute('class')).toEqual('operators button-group');
1045 expect(e.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001046 expect(e.lastChild.children[0].getAttribute('class')).toEqual('and');
1047 expect(e.lastChild.children[1].getAttribute('class')).toEqual('delete');
1048 });
1049 });
1050
Akronb19803c2018-08-16 16:39:42 +02001051 describe('KorAP.DocGroupRef element', function () {
1052 it('should be initializable', function () {
1053 var docGroupRef = docGroupRefClass.create(undefined, {
1054 "@type" : "koral:docGroupRef",
1055 "ref" : "@franz/myVC1"
1056 });
1057 expect(docGroupRef.ref()).toEqual("@franz/myVC1");
1058 var dgrE = docGroupRef.element();
1059
Akron3ad46942018-08-22 16:47:14 +02001060 expect(dgrE.children[0].firstChild.data).toEqual("referTo");
Akronb19803c2018-08-16 16:39:42 +02001061 expect(dgrE.children[0].tagName).toEqual("SPAN");
1062 expect(dgrE.children[0].classList.contains("key")).toBeTruthy();
1063 expect(dgrE.children[0].classList.contains("fixed")).toBeTruthy();
1064 expect(dgrE.children[1].firstChild.data).toEqual("@franz/myVC1");
1065 expect(dgrE.children[1].tagName).toEqual("SPAN");
1066 expect(dgrE.children[1].classList.contains("value")).toBeTruthy();
1067 expect(dgrE.children[1].getAttribute("data-type")).toEqual("string");
1068 });
1069
1070 it('should be modifiable on reference', function () {
1071 var docGroupRef = docGroupRefClass.create(undefined, {
1072 "@type" : "koral:docGroupRef",
1073 "ref" : "@franz/myVC1"
1074 });
1075 var dgrE = docGroupRef.element();
1076 expect(docGroupRef.toQuery()).toEqual("referTo \"@franz/myVC1\"");
1077 dgrE.children[1].click();
1078
1079 var input = dgrE.children[1].firstChild;
1080 expect(input.tagName).toEqual("INPUT");
1081 input.value = "Versuch";
1082
1083 var event = new KeyboardEvent("keypress", {
1084 "key" : "[Enter]",
1085 "keyCode" : 13
1086 });
1087
1088 input.dispatchEvent(event);
1089 expect(docGroupRef.toQuery()).toEqual("referTo \"Versuch\"");
1090 });
1091 });
1092
1093
Akrone4961b12017-05-10 21:04:46 +02001094 describe('KorAP.VirtualCorpus', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +00001095 var simpleGroupFactory = buildFactory(docGroupClass, {
1096 "@type" : "koral:docGroup",
1097 "operation" : "operation:and",
1098 "operands" : [
Akron712733a2018-04-05 18:17:47 +02001099 {
1100 "@type": 'koral:doc',
1101 "key" : 'author',
1102 "match": 'match:eq',
1103 "value": 'Max Birkendale',
1104 "type": 'type:string'
1105 },
1106 {
1107 "@type": 'koral:doc',
1108 "key": 'pubDate',
1109 "match": 'match:eq',
1110 "value": '2014-12-05',
1111 "type": 'type:date'
1112 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001113 ]
1114 });
1115
1116 var nestedGroupFactory = buildFactory(vcClass, {
1117 "@type" : "koral:docGroup",
1118 "operation" : "operation:or",
1119 "operands" : [
Akron712733a2018-04-05 18:17:47 +02001120 {
1121 "@type": 'koral:doc',
1122 "key" : 'author',
1123 "match": 'match:eq',
1124 "value": 'Max Birkendale',
1125 "type": 'type:string'
1126 },
1127 {
1128 "@type" : "koral:docGroup",
1129 "operation" : "operation:and",
1130 "operands" : [
1131 {
1132 "@type": 'koral:doc',
1133 "key": 'pubDate',
1134 "match": 'match:geq',
1135 "value": '2014-05-12',
1136 "type": 'type:date'
1137 },
1138 {
1139 "@type": 'koral:doc',
1140 "key": 'pubDate',
1141 "match": 'match:leq',
1142 "value": '2014-12-05',
1143 "type": 'type:date'
1144 }
1145 ]
1146 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001147 ]
1148 });
1149
1150 var flatGroupFactory = buildFactory(vcClass, {
1151 "@type" : "koral:docGroup",
1152 "operation" : "operation:and",
1153 "operands" : [
Akron712733a2018-04-05 18:17:47 +02001154 {
1155 "@type": 'koral:doc',
1156 "key": 'pubDate',
1157 "match": 'match:geq',
1158 "value": '2014-05-12',
1159 "type": 'type:date'
1160 },
1161 {
1162 "@type": 'koral:doc',
1163 "key": 'pubDate',
1164 "match": 'match:leq',
1165 "value": '2014-12-05',
1166 "type": 'type:date'
1167 },
1168 {
1169 "@type": 'koral:doc',
1170 "key": 'foo',
1171 "match": 'match:eq',
1172 "value": 'bar',
1173 "type": 'type:string'
1174 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001175 ]
1176 });
1177
1178 it('should be initializable', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001179 var vc = vcClass.create();
Nils Diewald7c8ced22015-04-15 19:21:00 +00001180 expect(vc.element().getAttribute('class')).toEqual('vc');
1181 expect(vc.root().element().getAttribute('class')).toEqual('doc unspecified');
1182
1183 // Not removable
1184 expect(vc.root().element().lastChild.children.length).toEqual(0);
1185 });
1186
1187 it('should be based on a doc', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001188 var vc = vcClass.create().fromJson({
Akron712733a2018-04-05 18:17:47 +02001189 "@type" : "koral:doc",
1190 "key":"Titel",
1191 "value":"Baum",
1192 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +00001193 });
1194
1195 expect(vc.element().getAttribute('class')).toEqual('vc');
1196 expect(vc.root().element().getAttribute('class')).toEqual('doc');
1197 expect(vc.root().key()).toEqual('Titel');
1198 expect(vc.root().value()).toEqual('Baum');
1199 expect(vc.root().matchop()).toEqual('eq');
1200
1201 var docE = vc.root().element();
1202 expect(docE.children[0].firstChild.data).toEqual('Titel');
1203 expect(docE.children[1].firstChild.data).toEqual('eq');
1204 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
1205 expect(docE.children[2].firstChild.data).toEqual('Baum');
1206 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
1207 });
1208
1209 it('should be based on a docGroup', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001210 var vc = vcClass.create().fromJson(simpleGroupFactory.create().toJson());
Nils Diewald7c8ced22015-04-15 19:21:00 +00001211
1212 expect(vc.element().getAttribute('class')).toEqual('vc');
1213 expect(vc.root().element().getAttribute('class')).toEqual('docGroup');
1214 expect(vc.root().operation()).toEqual('and');
1215
1216 var docGroup = vc.root();
1217
1218 var first = docGroup.getOperand(0);
1219 expect(first.key()).toEqual('author');
1220 expect(first.value()).toEqual('Max Birkendale');
1221 expect(first.matchop()).toEqual('eq');
1222
1223 var second = docGroup.getOperand(1);
1224 expect(second.key()).toEqual('pubDate');
1225 expect(second.value()).toEqual('2014-12-05');
1226 expect(second.matchop()).toEqual('eq');
1227 });
1228
Akronb19803c2018-08-16 16:39:42 +02001229 it('should be based on a docGroupRef', function () {
1230 var vc = vcClass.create().fromJson({
1231 "@type" : "koral:docGroupRef",
1232 "ref":"myCorpus"
1233 });
1234
Akronadab5e52018-08-20 13:50:53 +02001235 // iv class="doc groupref"><span class="key fixed">@referTo</span><span data-type="string" class="value">myCorpus</span>
1236 var vcE = vc.builder();
1237 expect(vcE.getAttribute('class')).toEqual('builder');
Akronb19803c2018-08-16 16:39:42 +02001238 expect(vcE.firstChild.tagName).toEqual('DIV');
1239 expect(vcE.firstChild.classList.contains('groupref')).toBeTruthy();
1240
1241 expect(vcE.firstChild.firstChild.tagName).toEqual('SPAN');
1242 expect(vcE.firstChild.firstChild.classList.contains('key')).toBeTruthy();
1243 expect(vcE.firstChild.firstChild.classList.contains('fixed')).toBeTruthy();
1244
Akron3ad46942018-08-22 16:47:14 +02001245 expect(vcE.firstChild.firstChild.textContent).toEqual("referTo");
Akronb19803c2018-08-16 16:39:42 +02001246
1247 expect(vcE.firstChild.children[1].tagName).toEqual('SPAN');
1248 expect(vcE.firstChild.children[1].classList.contains('value')).toBeTruthy();
1249 expect(vcE.firstChild.children[1].getAttribute('data-type')).toEqual('string');
1250 expect(vcE.firstChild.children[1].textContent).toEqual("myCorpus");
1251 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00001252
1253 it('should be based on a nested docGroup', function () {
1254 var vc = nestedGroupFactory.create();
1255
Akronadab5e52018-08-20 13:50:53 +02001256 expect(vc.builder().getAttribute('class')).toEqual('builder');
1257 expect(vc.builder().firstChild.getAttribute('class')).toEqual('docGroup');
1258 expect(vc.builder().firstChild.children[0].getAttribute('class')).toEqual('doc');
1259 var dg = vc.builder().firstChild.children[1];
Nils Diewald7c8ced22015-04-15 19:21:00 +00001260 expect(dg.getAttribute('class')).toEqual('docGroup');
1261 expect(dg.children[0].getAttribute('class')).toEqual('doc');
1262 expect(dg.children[1].getAttribute('class')).toEqual('doc');
Akron0b489ad2018-02-02 16:49:32 +01001263 expect(dg.children[2].getAttribute('class')).toEqual('operators button-group');
Akronadab5e52018-08-20 13:50:53 +02001264 expect(vc.builder().firstChild.children[2].getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001265 });
1266
Akronb19803c2018-08-16 16:39:42 +02001267 it('should be based on a nested docGroupRef', function () {
1268 var vc = vcClass.create().fromJson({
1269 "@type" : "koral:docGroup",
1270 "operation" : "operation:and",
1271 "operands" : [{
1272 "@type" : "koral:docGroupRef",
1273 "ref":"myCorpus"
1274 },{
1275 "@type" : "koral:doc",
1276 "key":"Titel",
1277 "value":"Baum",
1278 "match":"match:eq"
1279 }]
1280 });
1281
1282 expect(vc._root.ldType()).toEqual("docGroup");
1283
1284 expect(vc.toQuery()).toEqual('referTo "myCorpus" & Titel = "Baum"');
1285 });
1286
1287
Nils Diewald7c8ced22015-04-15 19:21:00 +00001288 it('should be modifiable by deletion in flat docGroups', function () {
1289 var vc = flatGroupFactory.create();
1290 var docGroup = vc.root();
1291
1292 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
1293
1294 var doc = docGroup.getOperand(1);
1295 expect(doc.key()).toEqual("pubDate");
1296 expect(doc.value()).toEqual("2014-12-05");
1297
1298 // Remove operand 1
1299 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
1300 expect(doc._element).toEqual(undefined);
1301
1302 doc = docGroup.getOperand(1);
1303 expect(doc.key()).toEqual("foo");
1304
1305 // Remove operand 1
1306 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
1307 expect(doc._element).toEqual(undefined);
1308
1309 // Only one operand left ...
1310 expect(docGroup.getOperand(1)).toBeUndefined();
1311 // ... but there shouldn't be a group anymore at all!
1312 expect(docGroup.getOperand(0)).toBeUndefined();
1313
1314 var obj = vc.root();
1315 expect(obj.ldType()).toEqual("doc");
1316 expect(obj.key()).toEqual("pubDate");
1317 expect(obj.value()).toEqual("2014-05-12");
1318
1319 expect(obj.element().getAttribute('class')).toEqual('doc');
1320 });
1321
1322
1323 it('should be modifiable by deletion in nested docGroups (root case)', function () {
1324 var vc = nestedGroupFactory.create();
1325
1326 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001327 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05)'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001328 );
1329
1330 var docGroup = vc.root();
1331 expect(docGroup.ldType()).toEqual("docGroup");
1332 expect(docGroup.operation()).toEqual("or");
1333
1334 var doc = docGroup.getOperand(0);
1335 expect(doc.key()).toEqual("author");
1336 expect(doc.value()).toEqual("Max Birkendale");
1337
1338 docGroup = docGroup.getOperand(1);
1339 expect(docGroup.operation()).toEqual("and");
1340
1341 doc = docGroup.getOperand(0);
1342 expect(doc.key()).toEqual("pubDate");
1343 expect(doc.matchop()).toEqual("geq");
1344 expect(doc.value()).toEqual("2014-05-12");
1345 expect(doc.type()).toEqual("date");
1346
1347 doc = docGroup.getOperand(1);
1348 expect(doc.key()).toEqual("pubDate");
1349 expect(doc.matchop()).toEqual("leq");
1350 expect(doc.value()).toEqual("2014-12-05");
1351 expect(doc.type()).toEqual("date");
1352
1353 // Remove first operand so everything becomes root
1354 expect(
Akron712733a2018-04-05 18:17:47 +02001355 vc.root().delOperand(
1356 vc.root().getOperand(0)
1357 ).update().ldType()
Nils Diewald7c8ced22015-04-15 19:21:00 +00001358 ).toEqual("docGroup");
1359
1360 expect(vc.root().ldType()).toEqual("docGroup");
1361 expect(vc.root().operation()).toEqual("and");
1362 expect(vc.root().getOperand(0).ldType()).toEqual("doc");
1363
1364 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001365 'pubDate since 2014-05-12 & pubDate until 2014-12-05'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001366 );
1367 });
1368
1369 it('should be modifiable by deletion in nested docGroups (resolve group case)', function () {
1370 var vc = nestedGroupFactory.create();
1371
1372 // Get nested group
1373 var firstGroup = vc.root().getOperand(1);
1374 firstGroup.append(simpleGroupFactory.create({ "operation" : "operation:or" }));
1375
1376 // Structur is now:
1377 // or(doc, and(doc, doc, or(doc, doc)))
1378
1379 // Get nested or in and
1380 var orGroup = vc.root().getOperand(1).getOperand(2);
1381 expect(orGroup.ldType()).toEqual("docGroup");
1382 expect(orGroup.operation()).toEqual("or");
1383
1384 // Remove
1385 // Structur is now:
1386 // or(doc, and(doc, doc, doc)))
1387 expect(orGroup.delOperand(orGroup.getOperand(0)).update().operation()).toEqual("and");
1388 expect(vc.root().getOperand(1).operands().length).toEqual(3);
1389 });
1390
1391 it('should be modifiable by deletion in nested docGroups (identical group case)', function () {
1392 var vc = nestedGroupFactory.create();
1393
1394 // Get nested group
1395 var firstGroup = vc.root().getOperand(1);
1396 firstGroup.append(simpleGroupFactory.create({
Akron712733a2018-04-05 18:17:47 +02001397 "operation" : "operation:or"
Nils Diewald7c8ced22015-04-15 19:21:00 +00001398 }));
1399 var oldAuthor = firstGroup.getOperand(2).getOperand(0);
1400 oldAuthor.key("title");
1401 oldAuthor.value("Der Birnbaum");
1402
1403 // Structur is now:
1404 // or(doc, and(doc, doc, or(doc, doc)))
1405 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001406 'author = "Max Birkendale" | ' +
1407 '(pubDate since 2014-05-12 & ' +
1408 'pubDate until 2014-12-05 & ' +
1409 '(title = "Der Birnbaum" | ' +
1410 'pubDate in 2014-12-05))'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001411 );
1412
1413 var andGroup = vc.root().getOperand(1);
1414
1415 // Get leading docs in and
1416 var doc1 = andGroup.getOperand(0);
1417 expect(doc1.ldType()).toEqual("doc");
1418 expect(doc1.value()).toEqual("2014-05-12");
1419 var doc2 = andGroup.getOperand(1);
1420 expect(doc2.ldType()).toEqual("doc");
1421 expect(doc2.value()).toEqual("2014-12-05");
1422
1423 // Remove 2
1424 expect(
Akron712733a2018-04-05 18:17:47 +02001425 andGroup.delOperand(doc2).update().operation()
Nils Diewald7c8ced22015-04-15 19:21:00 +00001426 ).toEqual("and");
1427 // Structur is now:
1428 // or(doc, and(doc, or(doc, doc)))
1429
1430 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001431 'author = "Max Birkendale"' +
1432 ' | (pubDate since 2014-05-12 & ' +
1433 '(title = "Der Birnbaum" | pubDate in 2014-12-05))'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001434 );
1435
1436
1437 // Remove 1
1438 expect(andGroup.delOperand(doc1).update().operation()).toEqual("or");
1439 // Structur is now:
1440 // or(doc, doc, doc)
1441
1442 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001443 'author = "Max Birkendale" | title = "Der Birnbaum" | pubDate in 2014-12-05'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001444 );
1445 });
1446
1447 it('should be reducible to unspecification', function () {
1448 var vc = demoFactory.create();
1449
1450 expect(vc.toQuery()).toEqual(vc.root().toQuery());
1451 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001452 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & ' +
1453 '(Titel = "Baum" | Veröffentlichungsort = "hihi")) ' +
1454 '| Untertitel ~ "huhu"');
Akrond141a362018-07-10 18:12:13 +02001455 expect(vc.root().element().lastChild.children[0].innerText).toEqual('and');
1456 expect(vc.root().element().lastChild.children[1].innerText).toEqual('×');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001457 expect(vc.root().delOperand(vc.root().getOperand(0)).update()).not.toBeUndefined();
Akron712733a2018-04-05 18:17:47 +02001458 expect(vc.toQuery()).toEqual('Untertitel ~ "huhu"');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001459
1460 var lc = vc.root().element().lastChild;
Akrond141a362018-07-10 18:12:13 +02001461 expect(lc.children[0].innerText).toEqual('and');
1462 expect(lc.children[1].innerText).toEqual('or');
1463 expect(lc.children[2].innerText).toEqual('×');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001464
1465 // Clean everything
1466 vc.clean();
1467 expect(vc.toQuery()).toEqual('');
1468 });
1469
1470 it('should flatten on import', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001471 var vc = vcClass.create().fromJson({
Akron712733a2018-04-05 18:17:47 +02001472 "@type":"koral:docGroup",
1473 "operation":"operation:or",
1474 "operands":[
1475 {
1476 "@type":"koral:docGroup",
1477 "operation":"operation:or",
1478 "operands":[
Nils Diewald7c8ced22015-04-15 19:21:00 +00001479 {
Akron712733a2018-04-05 18:17:47 +02001480 "@type":"koral:doc",
1481 "key":"Titel",
1482 "value":"Baum",
1483 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +00001484 },
1485 {
Akron712733a2018-04-05 18:17:47 +02001486 "@type":"koral:doc",
1487 "key":"Veröffentlichungsort",
1488 "value":"hihi",
1489 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +00001490 },
1491 {
Akron712733a2018-04-05 18:17:47 +02001492 "@type":"koral:docGroup",
1493 "operation":"operation:or",
1494 "operands":[
1495 {
1496 "@type":"koral:doc",
1497 "key":"Titel",
1498 "value":"Baum",
1499 "match":"match:eq"
1500 },
1501 {
1502 "@type":"koral:doc",
1503 "key":"Veröffentlichungsort",
1504 "value":"hihi",
1505 "match":"match:eq"
1506 }
1507 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +00001508 }
Akron712733a2018-04-05 18:17:47 +02001509 ]
1510 },
1511 {
1512 "@type":"koral:doc",
1513 "key":"Untertitel",
1514 "value":"huhu",
1515 "match":"match:contains"
1516 }
1517 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +00001518 });
Nils Diewaldfda29d92015-01-22 17:28:01 +00001519
Nils Diewald7c8ced22015-04-15 19:21:00 +00001520 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001521 'Titel = "Baum" | Veröffentlichungsort = "hihi" | Untertitel ~ "huhu"'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001522 );
Nils Diewald86dad5b2015-01-28 15:09:07 +00001523 });
Akron1bdf5272018-07-24 18:51:30 +02001524
1525 it('should be deserializable from collection 1', function () {
1526 var kq = {
1527 "matches":["..."],
1528 "collection":{
1529 "@type": "koral:docGroup",
1530 "operation": "operation:or",
1531 "operands": [{
1532 "@type": "koral:docGroup",
1533 "operation": "operation:and",
1534 "operands": [
1535 {
1536 "@type": "koral:doc",
1537 "key": "title",
1538 "match": "match:eq",
1539 "value": "Der Birnbaum",
1540 "type": "type:string"
1541 },
1542 {
1543 "@type": "koral:doc",
1544 "key": "pubPlace",
1545 "match": "match:eq",
1546 "value": "Mannheim",
1547 "type": "type:string"
1548 },
1549 {
1550 "@type": "koral:docGroup",
1551 "operation": "operation:or",
1552 "operands": [
1553 {
1554 "@type": "koral:doc",
1555 "key": "subTitle",
1556 "match": "match:eq",
1557 "value": "Aufzucht und Pflege",
1558 "type": "type:string"
1559 },
1560 {
1561 "@type": "koral:doc",
1562 "key": "subTitle",
1563 "match": "match:eq",
1564 "value": "Gedichte",
1565 "type": "type:string"
1566 }
1567 ]
1568 }
1569 ]
1570 },{
1571 "@type": "koral:doc",
1572 "key": "pubDate",
1573 "match": "match:geq",
1574 "value": "2015-03-05",
1575 "type": "type:date",
1576 "rewrites" : [{
1577 "@type" : "koral:rewrite",
1578 "operation" : "operation:modification",
1579 "src" : "querySerializer",
1580 "scope" : "tree"
1581 }]
1582 }]
1583 }
1584 };
1585
1586 var vc = vcClass.create().fromJson(kq["collection"]);
1587 expect(vc.toQuery()).toEqual('(title = "Der Birnbaum" & pubPlace = "Mannheim" & (subTitle = "Aufzucht und Pflege" | subTitle = "Gedichte")) | pubDate since 2015-03-05');
1588 });
1589
1590 it('should be deserializable from collection 2', function () {
1591 var kq = {
1592 "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
1593 "meta": {},
1594 "query": {
1595 "@type": "koral:token",
1596 "wrap": {
1597 "@type": "koral:term",
1598 "match": "match:eq",
1599 "layer": "orth",
1600 "key": "Baum",
1601 "foundry": "opennlp",
1602 "rewrites": [
1603 {
1604 "@type": "koral:rewrite",
1605 "src": "Kustvakt",
1606 "operation": "operation:injection",
1607 "scope": "foundry"
1608 }
1609 ]
1610 },
1611 "idn": "Baum_2227",
1612 "rewrites": [
1613 {
1614 "@type": "koral:rewrite",
1615 "src": "Kustvakt",
1616 "operation": "operation:injection",
1617 "scope": "idn"
1618 }
1619 ]
1620 },
1621 "collection": {
1622 "@type": "koral:docGroup",
1623 "operation": "operation:and",
1624 "operands": [
1625 {
1626 "@type": "koral:doc",
1627 "match": "match:eq",
1628 "type": "type:regex",
1629 "value": "CC-BY.*",
1630 "key": "availability"
1631 },
1632 {
1633 "@type": "koral:doc",
1634 "match": "match:eq",
1635 "type":"type:text",
1636 "value": "Goethe",
1637 "key": "author"
1638 }
1639 ],
1640 "rewrites": [
1641 {
1642 "@type": "koral:rewrite",
1643 "src": "Kustvakt",
1644 "operation": "operation:insertion",
1645 "scope": "availability(FREE)"
1646 }
1647 ]
1648 },
1649 "matches": []
1650 };
1651
1652 var vc = vcClass.create().fromJson(kq["collection"]);
1653 expect(vc.toQuery()).toEqual('availability = /CC-BY.*/ & author = "Goethe"');
1654 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00001655
Akron8a670162018-08-28 10:09:13 +02001656 it('shouldn\'t be deserializable from collection with unknown type', function () {
1657 var kq = {
1658 "@type" : "koral:doc",
1659 "match": "match:eq",
1660 "type":"type:failure",
1661 "value": "Goethe",
1662 "key": "author"
1663 };
Akron1bdf5272018-07-24 18:51:30 +02001664
Akron8a670162018-08-28 10:09:13 +02001665 expect(function () {
1666 vcClass.create().fromJson(kq)
1667 }).toThrow(new Error("Unknown value type: string"));
1668 });
1669
1670 it('should return a name', function () {
1671 var vc = vcClass.create();
1672 expect(vc.getName()).toEqual(KorAP.Locale.VC_allCorpora);
1673
1674 vc = vcClass.create().fromJson({"@type" : "koral:docGroupRef", "ref" : "DeReKo"});
1675 expect(vc.getName()).toEqual("DeReKo");
1676
1677 vc = vcClass.create().fromJson({"@type" : "koral:doc", "key" : "author", "value" : "Peter"});
1678 expect(vc.getName()).toEqual(KorAP.Locale.VC_oneCollection);
1679 });
Akrond2474aa2018-08-28 12:06:27 +02001680
1681 it('should check for rewrites', function () {
1682
1683 var vc = vcClass.create().fromJson({
1684 "@type" : "koral:doc",
1685 "key" : "author",
1686 "value" : "Goethe",
1687 "rewrites" : [{
1688 "@type" : "koral:rewrite",
1689 "operation" : "operation:modification",
1690 "src" : "querySerializer",
1691 "scope" : "value"
1692 }]
1693 });
1694 expect(vc.wasRewritten()).toBeTruthy();
1695
1696 var nested = {
1697 "@type" : "koral:docGroup",
1698 "operation" : "operation:or",
1699 "operands" : [
1700 {
1701 "@type" : "koral:doc",
1702 "key" : "author",
1703 "value" : "Goethe"
1704 },
1705 {
1706 "@type" : "koral:docGroup",
1707 "operation" : "operation:and",
1708 "operands" : [
1709 {
1710 "@type": "koral:doc",
1711 "key" : "author",
1712 "value" : "Schiller"
1713 },
1714 {
1715 "@type": "koral:doc",
1716 "key" : "author",
1717 "value" : "Fontane"
1718 }
1719 ]
1720 }
1721 ]
1722 };
1723 vc = vcClass.create().fromJson(nested);
1724 expect(vc.wasRewritten()).toBe(false);
1725
1726 nested["operands"][1]["operands"][1]["rewrites"] = [{
1727 "@type" : "koral:rewrite",
1728 "operation" : "operation:modification",
1729 "src" : "querySerializer",
1730 "scope" : "tree"
1731 }];
1732 vc = vcClass.create().fromJson(nested);
1733 expect(vc.wasRewritten()).toBeTruthy();
1734 });
Akron4feec9d2018-11-20 17:00:50 +01001735
1736 it('should add document at virtual corpus', function () {
1737 vc = vcClass.create();
1738 expect(vc.toQuery()).toEqual("");
1739
1740 let doc = docClass.create();
1741 doc.key("author");
1742 doc.type("string");
1743 doc.matchop("eq");
1744 doc.value("Goethe");
1745 expect(doc.toQuery()).toEqual('author = "Goethe"');
1746
1747 expect(vc.element().firstChild.children.length).toEqual(1);
1748 expect(vc.element().firstChild.firstChild.classList.contains("unspecified")).toBeTruthy();
1749
1750 vc.addRequired(doc);
1751 expect(vc.toQuery()).toEqual('author = "Goethe"');
1752
1753 expect(vc.element().firstChild.children.length).toEqual(1);
1754 expect(vc.element().firstChild.firstChild.classList.contains("doc")).toBeTruthy();
1755
1756 // Add as 'and' to doc
1757 let doc2 = docClass.create();
1758 doc2.key("author");
1759 doc2.type("string");
1760 doc2.matchop("eq");
1761 doc2.value("Schiller");
1762 vc.addRequired(doc2);
1763 expect(vc.toQuery()).toEqual('author = "Goethe" & author = "Schiller"');
1764
1765 expect(vc.element().firstChild.firstChild.classList.contains("docGroup")).toBeTruthy();
1766 expect(vc.element().firstChild.firstChild.children[0].classList.contains("doc")).toBeTruthy();
1767 expect(vc.element().firstChild.firstChild.children[1].classList.contains("doc")).toBeTruthy();
1768
1769
1770 // Add as 'and' to 'and'-docGroup
1771 let doc3 = docClass.create();
1772 doc3.key("author");
1773 doc3.type("string");
1774 doc3.matchop("eq");
1775 doc3.value("Fontane");
1776 vc.addRequired(doc3);
1777 expect(vc.toQuery()).toEqual('author = "Goethe" & author = "Schiller" & author = "Fontane"');
1778
1779 expect(vc.element().firstChild.firstChild.classList.contains("docGroup")).toBeTruthy();
1780 expect(vc.element().firstChild.firstChild.children[0].classList.contains("doc")).toBeTruthy();
1781 expect(vc.element().firstChild.firstChild.children[1].classList.contains("doc")).toBeTruthy();
1782 expect(vc.element().firstChild.firstChild.children[2].classList.contains("doc")).toBeTruthy();
1783
1784 // Add as 'and' to 'or'-docGroup
1785 vc = vcClass.create().fromJson({
1786 "@type" : 'koral:docGroup',
1787 'operation' : 'operation:or',
1788 'operands' : [
1789 {
1790 '@type' : 'koral:doc',
1791 'key' : 'title',
1792 'value' : 'Hello World!'
1793 },
1794 {
1795 '@type' : 'koral:doc',
1796 'key' : 'foo',
1797 'value' : 'bar'
1798 }
1799 ]
1800 });
1801 expect(vc.toQuery()).toEqual('title = "Hello World!" | foo = "bar"');
1802
1803 let doc4 = docClass.create();
1804 doc4.key("author");
1805 doc4.type("string");
1806 doc4.matchop("eq");
1807 doc4.value("Fontane");
1808 vc.addRequired(doc4);
1809 expect(vc.toQuery()).toEqual('(title = "Hello World!" | foo = "bar") & author = "Fontane"');
1810
1811 // Add as 'and' to 'or'-docGroup
1812 vc = vcClass.create().fromJson({
1813 "@type" : "koral:docGroupRef",
1814 "ref" : "@max/myCorpus"
1815 })
1816 let doc5 = docClass.create();
1817 doc5.key("author");
1818 doc5.type("string");
1819 doc5.matchop("eq");
1820 doc5.value("Goethe");
1821 expect(doc5.toQuery()).toEqual('author = "Goethe"');
1822 vc.addRequired(doc5);
1823 expect(vc.toQuery()).toEqual('referTo "@max/myCorpus" & author = "Goethe"');
1824 });
Akron1bdf5272018-07-24 18:51:30 +02001825 });
1826
1827
Nils Diewald7c8ced22015-04-15 19:21:00 +00001828 describe('KorAP.Operators', function () {
1829 it('should be initializable', function () {
1830 var op = operatorsClass.create(true, false, false);
1831 expect(op.and()).toBeTruthy();
1832 expect(op.or()).not.toBeTruthy();
1833 expect(op.del()).not.toBeTruthy();
1834
1835 op.and(false);
1836 expect(op.and()).not.toBeTruthy();
1837 expect(op.or()).not.toBeTruthy();
1838 expect(op.del()).not.toBeTruthy();
1839
1840 op.or(true);
1841 op.del(true);
1842 expect(op.and()).not.toBeTruthy();
1843 expect(op.or()).toBeTruthy();
1844 expect(op.del()).toBeTruthy();
1845
Akrond141a362018-07-10 18:12:13 +02001846 var e = op.update();
Akron0b489ad2018-02-02 16:49:32 +01001847 expect(e.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001848 expect(e.children[0].getAttribute('class')).toEqual('or');
Akrond141a362018-07-10 18:12:13 +02001849 expect(e.children[0].innerText).toEqual('or');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001850 expect(e.children[1].getAttribute('class')).toEqual('delete');
Akrond141a362018-07-10 18:12:13 +02001851 expect(e.children[1].innerText).toEqual('×');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001852
1853 op.and(true);
1854 op.del(false);
Akrond141a362018-07-10 18:12:13 +02001855 e = op.update();
Nils Diewald7c8ced22015-04-15 19:21:00 +00001856
Akron0b489ad2018-02-02 16:49:32 +01001857 expect(e.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001858 expect(e.children[0].getAttribute('class')).toEqual('and');
Akrond141a362018-07-10 18:12:13 +02001859 expect(e.children[0].innerText).toEqual('and');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001860 expect(e.children[1].getAttribute('class')).toEqual('or');
Akrond141a362018-07-10 18:12:13 +02001861 expect(e.children[1].innerText).toEqual('or');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001862 });
1863 });
1864
1865 describe('KorAP._delete (event)', function () {
1866 var complexVCFactory = buildFactory(vcClass,{
1867 "@type": 'koral:docGroup',
1868 'operation' : 'operation:and',
1869 'operands' : [
Akron712733a2018-04-05 18:17:47 +02001870 {
1871 "@type": 'koral:doc',
1872 "key": 'pubDate',
1873 "match": 'match:eq',
1874 "value": '2014-12-05',
1875 "type": 'type:date'
1876 },
1877 {
1878 "@type" : 'koral:docGroup',
1879 'operation' : 'operation:or',
1880 'operands' : [
1881 {
1882 '@type' : 'koral:doc',
1883 'key' : 'title',
1884 'value' : 'Hello World!'
1885 },
1886 {
1887 '@type' : 'koral:doc',
1888 'key' : 'foo',
1889 'value' : 'bar'
1890 }
1891 ]
1892 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001893 ]
1894 });
1895
1896 it('should clean on root docs', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001897 var vc = vcClass.create().fromJson({
Akron712733a2018-04-05 18:17:47 +02001898 "@type": 'koral:doc',
1899 "key": 'pubDate',
1900 "match": 'match:eq',
1901 "value": '2014-12-05',
1902 "type": 'type:date'
Nils Diewald86dad5b2015-01-28 15:09:07 +00001903 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00001904 expect(vc.root().toQuery()).toEqual('pubDate in 2014-12-05');
Akron0b489ad2018-02-02 16:49:32 +01001905 expect(vc.root().element().lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald86dad5b2015-01-28 15:09:07 +00001906
Nils Diewald7c8ced22015-04-15 19:21:00 +00001907 // Clean with delete from root
1908 expect(vc.root().element().lastChild.lastChild.getAttribute('class')).toEqual('delete');
1909 _delOn(vc.root());
1910 expect(vc.root().toQuery()).toEqual('');
Akronebc96662018-08-29 17:36:20 +02001911 expect(vc.root().element().lastChild.lastChild.data).toEqual(KorAP.Locale.EMPTY);
1912 expect(vc.root().element().classList.contains('unspecified')).toEqual(true);
Nils Diewald7c8ced22015-04-15 19:21:00 +00001913 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00001914
Nils Diewald7c8ced22015-04-15 19:21:00 +00001915 it('should remove on nested docs', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001916 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02001917 {
1918 "@type": 'koral:docGroup',
1919 'operation' : 'operation:and',
1920 'operands' : [
1921 {
1922 "@type": 'koral:doc',
1923 "key": 'pubDate',
1924 "match": 'match:eq',
1925 "value": '2014-12-05',
1926 "type": 'type:date'
1927 },
1928 {
1929 "@type" : 'koral:doc',
1930 'key' : 'foo',
1931 'value' : 'bar'
1932 }
1933 ]
1934 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001935 );
1936
1937 // Delete with direct element access
1938 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1939 _delOn(vc.root().getOperand(0));
1940
1941 expect(vc.toQuery()).toEqual('foo = "bar"');
1942 expect(vc.root().ldType()).toEqual('doc');
1943 });
1944
1945 it('should clean on doc groups', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001946 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02001947 {
1948 "@type": 'koral:docGroup',
1949 'operation' : 'operation:and',
1950 'operands' : [
1951 {
1952 "@type": 'koral:doc',
1953 "key": 'pubDate',
1954 "match": 'match:eq',
1955 "value": '2014-12-05',
1956 "type": 'type:date'
1957 },
1958 {
1959 "@type" : 'koral:doc',
1960 'key' : 'foo',
1961 'value' : 'bar'
1962 }
1963 ]
1964 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001965 );
1966
1967 // Cleanwith direct element access
1968 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1969 _delOn(vc.root());
1970 expect(vc.toQuery()).toEqual('');
1971 expect(vc.root().ldType()).toEqual('non');
1972 });
1973
1974 it('should remove on nested doc groups (case of ungrouping 1)', function () {
1975 var vc = complexVCFactory.create();
1976
1977 // Delete with direct element access
1978 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001979 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001980 );
1981
1982 // Remove hello world:
1983 _delOn(vc.root().getOperand(1).getOperand(0));
1984 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1985 expect(vc.root().ldType()).toEqual('docGroup');
1986 });
1987
1988 it('should remove on nested doc groups (case of ungrouping 2)', function () {
1989 var vc = complexVCFactory.create();
1990
1991 // Delete with direct element access
1992 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001993 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001994 );
1995
1996 // Remove bar
1997 _delOn(vc.root().getOperand(1).getOperand(1));
1998 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & title = "Hello World!"');
1999 expect(vc.root().ldType()).toEqual('docGroup');
2000 expect(vc.root().operation()).toEqual('and');
2001 });
2002
2003 it('should remove on nested doc groups (case of root changing)', function () {
2004 var vc = complexVCFactory.create();
2005
2006 // Delete with direct element access
2007 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002008 'pubDate in 2014-12-05 & ' +
2009 '(title = "Hello World!" | foo = "bar")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002010 );
2011
2012 // Remove bar
2013 _delOn(vc.root().getOperand(0));
2014 expect(vc.toQuery()).toEqual('title = "Hello World!" | foo = "bar"');
2015 expect(vc.root().ldType()).toEqual('docGroup');
2016 expect(vc.root().operation()).toEqual('or');
2017 });
2018
2019 it('should remove on nested doc groups (list flattening)', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00002020 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02002021 {
2022 "@type": 'koral:docGroup',
2023 'operation' : 'operation:or',
2024 'operands' : [
2025 {
2026 "@type": 'koral:doc',
2027 "key": 'pubDate',
2028 "match": 'match:eq',
2029 "value": '2014-12-05',
2030 "type": 'type:date'
2031 },
2032 {
2033 "@type" : 'koral:doc',
2034 'key' : 'foo',
2035 'value' : 'bar'
2036 },
2037 {
2038 "@type": 'koral:docGroup',
2039 'operation' : 'operation:and',
2040 'operands' : [
2041 {
2042 "@type": 'koral:doc',
2043 "key": 'pubDate',
2044 "match": 'match:eq',
2045 "value": '2014-12-05',
2046 "type": 'type:date'
2047 },
2048 {
2049 "@type" : 'koral:docGroup',
2050 'operation' : 'operation:or',
2051 'operands' : [
2052 {
2053 '@type' : 'koral:doc',
2054 'key' : 'title',
2055 'value' : 'Hello World!'
2056 },
2057 {
2058 '@type' : 'koral:doc',
2059 'key' : 'yeah',
2060 'value' : 'juhu'
2061 }
2062 ]
2063 }
2064 ]
2065 }
2066 ]
2067 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002068 );
2069
2070 // Delete with direct element access
2071 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002072 'pubDate in 2014-12-05 | foo = "bar" | ' +
2073 '(pubDate in 2014-12-05 & ' +
2074 '(title = "Hello World!" | yeah = "juhu"))'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002075 );
2076
2077 expect(vc.root().ldType()).toEqual('docGroup');
2078 expect(vc.root().operation()).toEqual('or');
2079
2080 // Operands and operators
Akronadab5e52018-08-20 13:50:53 +02002081 expect(vc.builder().firstChild.children.length).toEqual(4);
2082 expect(vc.builder().firstChild.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002083
2084 // Remove inner group and flatten
2085 _delOn(vc.root().getOperand(2).getOperand(0));
2086
2087 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002088 'pubDate in 2014-12-05 | foo = "bar" | title = "Hello World!" | yeah = "juhu"'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002089 );
2090 expect(vc.root().ldType()).toEqual('docGroup');
2091 expect(vc.root().operation()).toEqual('or');
2092
2093 // Operands and operators
Akronadab5e52018-08-20 13:50:53 +02002094 expect(vc.builder().firstChild.children.length).toEqual(5);
2095 expect(vc.builder().firstChild.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002096 });
2097 });
2098
2099 describe('KorAP._add (event)', function () {
2100 var complexVCFactory = buildFactory(vcClass,{
2101 "@type": 'koral:docGroup',
2102 'operation' : 'operation:and',
2103 'operands' : [
Akron712733a2018-04-05 18:17:47 +02002104 {
2105 "@type": 'koral:doc',
2106 "key": 'pubDate',
2107 "match": 'match:eq',
2108 "value": '2014-12-05',
2109 "type": 'type:date'
2110 },
2111 {
2112 "@type" : 'koral:docGroup',
2113 'operation' : 'operation:or',
2114 'operands' : [
2115 {
2116 '@type' : 'koral:doc',
2117 'key' : 'title',
2118 'value' : 'Hello World!'
2119 },
2120 {
2121 '@type' : 'koral:doc',
2122 'key' : 'foo',
2123 'value' : 'bar'
2124 }
2125 ]
2126 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002127 ]
2128 });
2129
2130 it('should add new unspecified doc with "and"', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00002131 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02002132 {
2133 "@type": 'koral:docGroup',
2134 'operation' : 'operation:and',
2135 'operands' : [
2136 {
2137 "@type": 'koral:doc',
2138 "key": 'pubDate',
2139 "match": 'match:eq',
2140 "value": '2014-12-05',
2141 "type": 'type:date'
2142 },
2143 {
2144 "@type" : 'koral:doc',
2145 'key' : 'foo',
2146 'value' : 'bar'
Akronb19803c2018-08-16 16:39:42 +02002147 },
2148 {
2149 "@type" : "koral:docGroupRef",
2150 "ref" : "myCorpus"
Akron712733a2018-04-05 18:17:47 +02002151 }
2152 ]
2153 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002154 );
2155
Akronb19803c2018-08-16 16:39:42 +02002156 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar" & referTo "myCorpus"');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002157
Akronadab5e52018-08-20 13:50:53 +02002158 var fc = vc.builder().firstChild;
Nils Diewald7c8ced22015-04-15 19:21:00 +00002159 expect(fc.getAttribute('data-operation')).toEqual('and');
Akronb19803c2018-08-16 16:39:42 +02002160 expect(fc.children.length).toEqual(4);
Akron0b489ad2018-02-02 16:49:32 +01002161 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002162 expect(fc.children[0].getAttribute('class')).toEqual('doc');
2163 expect(fc.children[1].getAttribute('class')).toEqual('doc');
Akronb19803c2018-08-16 16:39:42 +02002164 expect(fc.children[2].getAttribute('class')).toEqual('doc groupref');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002165
2166 // add with 'and' in the middle
2167 _andOn(vc.root().getOperand(0));
Akronb19803c2018-08-16 16:39:42 +02002168 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar" & referTo "myCorpus"');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002169
Akronadab5e52018-08-20 13:50:53 +02002170 fc = vc.builder().firstChild;
Nils Diewald7c8ced22015-04-15 19:21:00 +00002171 expect(fc.getAttribute('data-operation')).toEqual('and');
Akronb19803c2018-08-16 16:39:42 +02002172 expect(fc.children.length).toEqual(5);
Akron0b489ad2018-02-02 16:49:32 +01002173 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002174
2175 expect(fc.children[0].getAttribute('class')).toEqual('doc');
2176 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
2177 expect(fc.children[2].getAttribute('class')).toEqual('doc');
Akronb19803c2018-08-16 16:39:42 +02002178 expect(fc.children[3].getAttribute('class')).toEqual('doc groupref');
2179 expect(fc.children[4].classList.contains('button-group')).toBeTruthy();
2180 expect(fc.children.length).toEqual(5);
2181
2182 _andOn(vc.root().getOperand(3));
2183 expect(fc.children[0].getAttribute('class')).toEqual('doc');
2184 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
2185 expect(fc.children[2].getAttribute('class')).toEqual('doc');
2186 expect(fc.children[3].getAttribute('class')).toEqual('doc groupref');
2187 expect(fc.children[4].getAttribute('class')).toEqual('doc unspecified');
2188 expect(fc.children[5].classList.contains('button-group')).toBeTruthy();
2189 expect(fc.children.length).toEqual(6);
2190
Nils Diewald7c8ced22015-04-15 19:21:00 +00002191 });
2192
Akronb19803c2018-08-16 16:39:42 +02002193
Nils Diewald7c8ced22015-04-15 19:21:00 +00002194 it('should add new unspecified doc with "or"', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00002195 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02002196 {
2197 "@type": 'koral:docGroup',
2198 'operation' : 'operation:and',
2199 'operands' : [
2200 {
2201 "@type": 'koral:doc',
2202 "key": 'pubDate',
2203 "match": 'match:eq',
2204 "value": '2014-12-05',
2205 "type": 'type:date'
2206 },
2207 {
2208 "@type" : 'koral:doc',
2209 'key' : 'foo',
2210 'value' : 'bar'
Akronb19803c2018-08-16 16:39:42 +02002211 },
2212 {
2213 "@type" : "koral:docGroupRef",
2214 "ref" : "myCorpus"
Akron712733a2018-04-05 18:17:47 +02002215 }
2216 ]
2217 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002218 );
2219
Akronb19803c2018-08-16 16:39:42 +02002220 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar" & referTo "myCorpus"');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002221
Akronadab5e52018-08-20 13:50:53 +02002222 var fc = vc.builder().firstChild;
Akronb19803c2018-08-16 16:39:42 +02002223 expect(fc.children.length).toEqual(4);
Akron0b489ad2018-02-02 16:49:32 +01002224 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002225 expect(fc.children[0].getAttribute('class')).toEqual('doc');
2226 expect(fc.children[1].getAttribute('class')).toEqual('doc');
Akronb19803c2018-08-16 16:39:42 +02002227 expect(fc.children[2].getAttribute('class')).toEqual('doc groupref');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002228
2229 // add with 'or' in the middle
2230 _orOn(vc.root().getOperand(0));
Akronb19803c2018-08-16 16:39:42 +02002231 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar" & referTo "myCorpus"');
Akronadab5e52018-08-20 13:50:53 +02002232 fc = vc.builder().firstChild;
Nils Diewald7c8ced22015-04-15 19:21:00 +00002233
2234 expect(fc.getAttribute('data-operation')).toEqual('and');
Akronb19803c2018-08-16 16:39:42 +02002235 expect(fc.children.length).toEqual(4);
Nils Diewald7c8ced22015-04-15 19:21:00 +00002236 expect(fc.children[0].getAttribute('class')).toEqual('docGroup');
2237 expect(fc.children[0].getAttribute('data-operation')).toEqual('or');
2238 expect(fc.children[1].getAttribute('class')).toEqual('doc');
Akronb19803c2018-08-16 16:39:42 +02002239 expect(fc.children[2].getAttribute('class')).toEqual('doc groupref');
2240 expect(fc.children[3].getAttribute('class')).toEqual('operators button-group');
Akron0b489ad2018-02-02 16:49:32 +01002241 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00002242
Akronadab5e52018-08-20 13:50:53 +02002243 fc = vc.builder().firstChild.firstChild;
Nils Diewald7c8ced22015-04-15 19:21:00 +00002244 expect(fc.children.length).toEqual(3);
2245 expect(fc.children[0].getAttribute('class')).toEqual('doc');
2246 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
Akron0b489ad2018-02-02 16:49:32 +01002247 expect(fc.children[2].getAttribute('class')).toEqual('operators button-group');
2248 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Akronb19803c2018-08-16 16:39:42 +02002249
2250 _orOn(vc.root().getOperand(2));
Akronadab5e52018-08-20 13:50:53 +02002251 fc = vc.builder().firstChild;
Akronb19803c2018-08-16 16:39:42 +02002252 expect(fc.children.length).toEqual(4);
2253
2254 expect(fc.children[0].getAttribute('class')).toEqual('docGroup');
2255 expect(fc.children[1].getAttribute('class')).toEqual('doc');
2256 expect(fc.children[2].getAttribute('class')).toEqual('docGroup');
2257 expect(fc.children[3].getAttribute('class')).toEqual('operators button-group');
2258
Akronadab5e52018-08-20 13:50:53 +02002259 fc = vc.builder().firstChild.children[2];
Akronb19803c2018-08-16 16:39:42 +02002260 expect(fc.children[0].getAttribute('class')).toEqual('doc groupref');
2261 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
2262 expect(fc.children[2].getAttribute('class')).toEqual('operators button-group');
2263 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
2264
Nils Diewald7c8ced22015-04-15 19:21:00 +00002265 });
2266
2267 it('should add new unspecified doc with "and" before group', function () {
2268 var vc = demoFactory.create();
2269
2270 // Wrap with direct element access
2271 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002272 '(Titel = "Baum" & ' +
2273 'Veröffentlichungsort = "hihi" & ' +
2274 '(Titel = "Baum" | ' +
2275 'Veröffentlichungsort = "hihi")) | ' +
2276 'Untertitel ~ "huhu"'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002277 );
2278
2279 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
2280 expect(vc.root().getOperand(0).operation()).toEqual('and');
2281 expect(vc.root().getOperand(0).operands().length).toEqual(3);
2282
2283 // Add unspecified on the second doc
2284 var secDoc = vc.root().getOperand(0).getOperand(1);
2285 expect(secDoc.value()).toEqual('hihi');
2286
2287 // Add
2288 _andOn(secDoc);
2289
2290 var fo = vc.root().getOperand(0);
2291
2292 expect(fo.ldType()).toEqual('docGroup');
2293 expect(fo.operation()).toEqual('and');
2294 expect(fo.operands().length).toEqual(4);
2295
2296 expect(fo.getOperand(0).ldType()).toEqual('doc');
2297 expect(fo.getOperand(1).ldType()).toEqual('doc');
2298 expect(fo.getOperand(2).ldType()).toEqual('non');
2299 expect(fo.getOperand(3).ldType()).toEqual('docGroup');
2300 });
2301
2302
2303 it('should remove a doc with an unspecified doc in a nested group', function () {
2304 var vc = demoFactory.create();
2305
2306 // Wrap with direct element access
2307 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002308 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & (Titel = "Baum" | Veröffentlichungsort = "hihi")) | Untertitel ~ "huhu"'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002309 );
2310
2311 var fo = vc.root().getOperand(0).getOperand(0);
2312 expect(fo.key()).toEqual('Titel');
2313 expect(fo.value()).toEqual('Baum');
2314
2315 // Add unspecified on the root group
2316 _orOn(fo);
2317
2318 fo = vc.root().getOperand(0).getOperand(0);
2319
2320 expect(fo.operation()).toEqual('or');
2321 expect(fo.getOperand(0).ldType()).toEqual('doc');
2322 expect(fo.getOperand(1).ldType()).toEqual('non');
2323
2324 // Delete document
2325 _delOn(fo.getOperand(0));
2326
2327 // The operand is now non
2328 expect(vc.root().getOperand(0).getOperand(0).ldType()).toEqual('non');
2329 expect(vc.root().getOperand(0).getOperand(1).ldType()).toEqual('doc');
2330 expect(vc.root().getOperand(0).getOperand(2).ldType()).toEqual('docGroup');
2331 });
2332
2333
Akron712733a2018-04-05 18:17:47 +02002334 it('should remove an unspecified doc with a doc in a nested group', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +00002335 var vc = demoFactory.create();
2336
2337 // Wrap with direct element access
2338 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002339 '(Titel = "Baum" & ' +
2340 'Veröffentlichungsort = "hihi" & ' +
2341 '(Titel = "Baum" ' +
2342 '| Veröffentlichungsort = "hihi")) | ' +
2343 'Untertitel ~ "huhu"'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002344 );
2345
2346 var fo = vc.root().getOperand(0).getOperand(0);
2347 expect(fo.key()).toEqual('Titel');
2348 expect(fo.value()).toEqual('Baum');
2349
2350 // Add unspecified on the root group
2351 _orOn(fo);
2352
2353 fo = vc.root().getOperand(0).getOperand(0);
2354
2355 expect(fo.operation()).toEqual('or');
2356 expect(fo.getOperand(0).ldType()).toEqual('doc');
2357 expect(fo.getOperand(1).ldType()).toEqual('non');
2358
2359 // Delete unspecified doc
2360 _delOn(fo.getOperand(1));
2361
2362 // The operand is now non
2363 fo = vc.root().getOperand(0);
2364 expect(fo.getOperand(0).ldType()).toEqual('doc');
2365 expect(fo.getOperand(0).key()).toEqual('Titel');
2366 expect(fo.getOperand(0).value()).toEqual('Baum');
2367 expect(fo.getOperand(1).ldType()).toEqual('doc');
2368 expect(fo.getOperand(2).ldType()).toEqual('docGroup');
2369 });
2370
2371
2372 it('should add on parent group (case "and")', function () {
2373 var vc = complexVCFactory.create();
2374
2375 // Wrap with direct element access
2376 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002377 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002378 );
2379
2380 expect(vc.root().operands().length).toEqual(2);
2381
2382 // Add unspecified on the root group
2383 _andOn(vc.root().getOperand(1));
2384 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002385 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002386 );
2387
2388 expect(vc.root().ldType()).toEqual('docGroup');
2389 expect(vc.root().operands().length).toEqual(3);
2390 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
2391 expect(vc.root().getOperand(1).ldType()).toEqual('docGroup');
2392 expect(vc.root().getOperand(1).operation()).toEqual('or');
2393 expect(vc.root().getOperand(2).ldType()).toEqual('non');
2394
2395 // Add another unspecified on the root group
2396 _andOn(vc.root().getOperand(1));
2397
2398 expect(vc.root().operands().length).toEqual(4);
2399 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
2400 expect(vc.root().getOperand(1).ldType()).toEqual('docGroup');
2401 expect(vc.root().getOperand(2).ldType()).toEqual('non');
2402 expect(vc.root().getOperand(3).ldType()).toEqual('non');
2403
2404 // Add another unspecified after the first doc
2405 _andOn(vc.root().getOperand(0));
2406
2407 expect(vc.root().operands().length).toEqual(5);
2408 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
2409 expect(vc.root().getOperand(1).ldType()).toEqual('non');
2410 expect(vc.root().getOperand(2).ldType()).toEqual('docGroup');
2411 expect(vc.root().getOperand(3).ldType()).toEqual('non');
2412 expect(vc.root().getOperand(4).ldType()).toEqual('non');
2413 });
2414
2415 it('should wrap on root', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00002416 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02002417 {
2418 "@type": 'koral:docGroup',
2419 'operation' : 'operation:and',
2420 'operands' : [
2421 {
2422 "@type": 'koral:doc',
2423 "key": 'pubDate',
2424 "match": 'match:eq',
2425 "value": '2014-12-05',
2426 "type": 'type:date'
2427 },
2428 {
2429 "@type" : 'koral:doc',
2430 'key' : 'foo',
2431 'value' : 'bar'
2432 }
2433 ]
2434 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002435 );
2436
2437 // Wrap on root
2438 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
2439 expect(vc.root().ldType()).toEqual('docGroup');
2440 expect(vc.root().operation()).toEqual('and');
2441 _orOn(vc.root());
2442 expect(vc.root().ldType()).toEqual('docGroup');
2443 expect(vc.root().operation()).toEqual('or');
2444
2445 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
2446 expect(vc.root().getOperand(0).operation()).toEqual('and');
2447 });
2448
2449 it('should add on root (case "and")', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00002450 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02002451 {
2452 "@type": 'koral:doc',
2453 "key": 'pubDate',
2454 "match": 'match:eq',
2455 "value": '2014-12-05',
2456 "type": 'type:date'
2457 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002458 );
2459
2460 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
2461 expect(vc.root().ldType()).toEqual('doc');
2462 expect(vc.root().key()).toEqual('pubDate');
2463 expect(vc.root().value()).toEqual('2014-12-05');
2464
2465 // Wrap on root
2466 _andOn(vc.root());
2467 expect(vc.root().ldType()).toEqual('docGroup');
2468 expect(vc.root().operation()).toEqual('and');
2469 });
2470
2471 it('should add on root (case "or")', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00002472 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02002473 {
2474 "@type": 'koral:doc',
2475 "key": 'pubDate',
2476 "match": 'match:eq',
2477 "value": '2014-12-05',
2478 "type": 'type:date'
2479 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002480 );
2481
2482 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
2483 expect(vc.root().key()).toEqual('pubDate');
2484 expect(vc.root().value()).toEqual('2014-12-05');
2485
2486 // Wrap on root
2487 _orOn(vc.root());
2488 expect(vc.root().ldType()).toEqual('docGroup');
2489 expect(vc.root().operation()).toEqual('or');
2490 });
2491
2492 it('should support multiple sub groups per group', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00002493 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02002494 {
2495 "@type": 'koral:docGroup',
2496 'operation' : 'operation:or',
2497 'operands' : [
2498 {
2499 "@type": 'koral:docGroup',
2500 'operation' : 'operation:and',
2501 'operands' : [
2502 {
2503 "@type": 'koral:doc',
2504 "key": 'title',
2505 "value": 't1',
2506 },
2507 {
2508 "@type" : 'koral:doc',
2509 'key' : 'title',
2510 'value' : 't2'
2511 }
2512 ]
2513 },
2514 {
2515 "@type": 'koral:docGroup',
2516 'operation' : 'operation:and',
2517 'operands' : [
2518 {
2519 "@type": 'koral:doc',
2520 "key": 'title',
2521 "value": 't3',
2522 },
2523 {
2524 "@type" : 'koral:doc',
2525 'key' : 'title',
2526 'value' : 't4'
2527 }
2528 ]
2529 }
2530 ]
2531 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002532 );
2533 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002534 '(title = "t1" & title = "t2") | ' +
2535 '(title = "t3" & title = "t4")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002536 );
2537 expect(vc.root().operation()).toEqual('or');
2538 expect(vc.root().getOperand(0).toQuery()).toEqual('title = "t1" & title = "t2"');
2539 expect(vc.root().getOperand(1).toQuery()).toEqual('title = "t3" & title = "t4"');
2540
2541 _andOn(vc.root());
2542
2543 expect(vc.root().operation()).toEqual('and');
2544 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
2545 expect(vc.root().getOperand(1).ldType()).toEqual('non');
2546 });
2547 });
2548
Nils Diewald6283d692015-04-23 20:32:53 +00002549
2550 describe('KorAP.Rewrite', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +00002551 it('should be initializable', function () {
2552 var rewrite = rewriteClass.create({
Akron712733a2018-04-05 18:17:47 +02002553 "@type" : "koral:rewrite",
2554 "operation" : "operation:modification",
2555 "src" : "querySerializer",
2556 "scope" : "tree"
Nils Diewald86dad5b2015-01-28 15:09:07 +00002557 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00002558 expect(rewrite.toString()).toEqual('Modification of "tree" by "querySerializer"');
2559 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00002560
Nils Diewald7c8ced22015-04-15 19:21:00 +00002561 it('should be deserialized by docs', function () {
2562 var doc = docClass.create(
Akron712733a2018-04-05 18:17:47 +02002563 undefined,
2564 {
2565 "@type":"koral:doc",
2566 "key":"Titel",
2567 "value":"Baum",
2568 "match":"match:eq"
2569 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00002570
2571 expect(doc.element().classList.contains('doc')).toBeTruthy();
2572 expect(doc.element().classList.contains('rewritten')).toBe(false);
2573
2574 doc = docClass.create(
Akron712733a2018-04-05 18:17:47 +02002575 undefined,
2576 {
2577 "@type":"koral:doc",
2578 "key":"Titel",
2579 "value":"Baum",
2580 "match":"match:eq",
2581 "rewrites" : [
2582 {
2583 "@type" : "koral:rewrite",
2584 "operation" : "operation:modification",
2585 "src" : "querySerializer",
2586 "scope" : "tree"
2587 }
2588 ]
2589 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00002590
2591 expect(doc.element().classList.contains('doc')).toBeTruthy();
2592 expect(doc.element().classList.contains('rewritten')).toBeTruthy();
2593 });
Nils Diewald6283d692015-04-23 20:32:53 +00002594
Akron76c3dd62018-05-29 20:58:27 +02002595 it('should be described in a title attribute', function () {
2596
2597 doc = docClass.create(
2598 undefined,
2599 {
2600 "@type":"koral:doc",
2601 "key":"Titel",
2602 "value":"Baum",
2603 "match":"match:eq",
2604 "rewrites" : [
2605 {
2606 "@type" : "koral:rewrite",
2607 "operation" : "operation:modification",
2608 "src" : "querySerializer",
2609 "scope" : "tree"
2610 },
2611 {
2612 "@type" : "koral:rewrite",
2613 "operation" : "operation:injection",
2614 "src" : "me"
2615 }
2616 ]
2617 });
2618
2619 expect(doc.element().classList.contains('doc')).toBeTruthy();
2620 expect(doc.element().classList.contains('rewritten')).toBeTruthy();
2621 expect(doc.element().lastChild.getAttribute("title")).toEqual("querySerializer: tree (modification)\nme (injection)");
2622 });
2623
2624
Nils Diewald6283d692015-04-23 20:32:53 +00002625 xit('should be deserialized by docGroups', function () {
2626 var docGroup = docGroupClass.create(
Akron712733a2018-04-05 18:17:47 +02002627 undefined,
2628 {
2629 "@type" : "koral:docGroup",
2630 "operation" : "operation:or",
2631 "operands" : [
2632 {
2633 "@type" : "doc",
2634 "key" : "pubDate",
2635 "type" : "type:date",
2636 "value" : "2014-12-05"
2637 },
2638 {
2639 "@type" : "doc",
2640 "key" : "pubDate",
2641 "type" : "type:date",
2642 "value" : "2014-12-06"
2643 }
2644 ],
2645 "rewrites" : [
2646 {
2647 "@type" : "koral:rewrite",
2648 "operation" : "operation:modification",
2649 "src" : "querySerializer",
2650 "scope" : "tree"
2651 }
2652 ]
2653 }
Nils Diewald6283d692015-04-23 20:32:53 +00002654 );
2655
2656 expect(doc.element().classList.contains('docgroup')).toBeTruthy();
2657 expect(doc.element().classList.contains('rewritten')).toBe(false);
2658 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00002659 });
Nils Diewaldf0c4f112015-05-05 12:56:59 +00002660
2661 describe('KorAP.stringValue', function () {
2662 it('should be initializable', function () {
2663 var sv = stringValClass.create();
2664 expect(sv.regex()).toBe(false);
2665 expect(sv.value()).toBe('');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002666
2667 sv = stringValClass.create('Baum');
2668 expect(sv.regex()).toBe(false);
2669 expect(sv.value()).toBe('Baum');
2670
2671 sv = stringValClass.create('Baum', false);
2672 expect(sv.regex()).toBe(false);
2673 expect(sv.value()).toBe('Baum');
2674
2675 sv = stringValClass.create('Baum', true);
2676 expect(sv.regex()).toBe(true);
2677 expect(sv.value()).toBe('Baum');
2678 });
2679
2680 it('should be modifiable', function () {
2681 var sv = stringValClass.create();
2682 expect(sv.regex()).toBe(false);
2683 expect(sv.value()).toBe('');
2684
2685 expect(sv.value('Baum')).toBe('Baum');
2686 expect(sv.value()).toBe('Baum');
2687
2688 expect(sv.regex(true)).toBe(true);
2689 expect(sv.regex()).toBe(true);
2690 });
2691
2692 it('should have a toggleble regex value', function () {
2693 var sv = stringValClass.create();
Nils Diewald7991a3f2015-05-19 14:12:37 +00002694
2695 expect(sv.element().firstChild.value).toBe('');
2696 sv.element().firstChild.value = 'der'
2697 expect(sv.element().firstChild.value).toBe('der');
2698
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002699 expect(sv.regex()).toBe(false);
2700
2701 sv.toggleRegex();
Nils Diewald7991a3f2015-05-19 14:12:37 +00002702 expect(sv.element().firstChild.value).toBe('der');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002703 expect(sv.regex()).toBe(true);
Nils Diewald7991a3f2015-05-19 14:12:37 +00002704 sv.element().firstChild.value = 'derbe'
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002705
2706 sv.toggleRegex();
2707 expect(sv.regex()).toBe(false);
Nils Diewald7991a3f2015-05-19 14:12:37 +00002708 expect(sv.element().firstChild.value).toBe('derbe');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002709 });
2710
2711 it('should have an element', function () {
2712 var sv = stringValClass.create('der');
2713 expect(sv.element().nodeName).toBe('DIV');
2714 expect(sv.element().firstChild.nodeName).toBe('INPUT');
2715 expect(sv.element().firstChild.value).toBe('der');
2716 });
2717
2718 it('should have a classed element', function () {
2719 var sv = stringValClass.create();
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002720 expect(sv.element().classList.contains('regex')).toBe(false);
2721 expect(sv.regex()).toBe(false);
2722 sv.toggleRegex();
2723 expect(sv.element().classList.contains('regex')).toBe(true);
2724 });
2725
2726 it('should be storable', function () {
2727 var sv = stringValClass.create();
2728 var count = 1;
2729 sv.store = function (value, regex) {
Akron712733a2018-04-05 18:17:47 +02002730 expect(regex).toBe(true);
2731 expect(value).toBe('tree');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002732 };
2733 sv.regex(true);
2734 sv.value('tree');
2735 sv.element().lastChild.click();
Nils Diewaldf0c4f112015-05-05 12:56:59 +00002736 });
Akron712733a2018-04-05 18:17:47 +02002737
Akronb19803c2018-08-16 16:39:42 +02002738 it('should have a disableoption for regex', function () {
2739 var sv = stringValClass.create(undefined, undefined, true);
2740 var svE = sv.element();
2741 expect(svE.children.length).toEqual(2);
2742
2743 sv = stringValClass.create(undefined, undefined, false);
2744 svE = sv.element();
2745 expect(svE.children.length).toEqual(1);
2746 });
2747
Nils Diewaldf0c4f112015-05-05 12:56:59 +00002748 });
Akrone4961b12017-05-10 21:04:46 +02002749
Akron712733a2018-04-05 18:17:47 +02002750 describe('KorAP.VC.Menu', function () {
Akrone65a88a2018-04-05 19:14:20 +02002751
2752 var vc;
2753
Akron712733a2018-04-05 18:17:47 +02002754 it('should be initializable', function () {
2755
Akrone65a88a2018-04-05 19:14:20 +02002756 vc = vcClass.create([
Akron712733a2018-04-05 18:17:47 +02002757 ['a', 'text'],
2758 ['b', 'string'],
2759 ['c', 'date']
2760 ]).fromJson();
Akronadab5e52018-08-20 13:50:53 +02002761 expect(vc.builder().firstChild.classList.contains('unspecified')).toBeTruthy();
2762 expect(vc.builder().firstChild.firstChild.tagName).toEqual('SPAN');
Akron712733a2018-04-05 18:17:47 +02002763
2764 // Click on unspecified
Akronadab5e52018-08-20 13:50:53 +02002765 vc.builder().firstChild.firstChild.click();
2766 expect(vc.builder().firstChild.firstChild.tagName).toEqual('UL');
Akron712733a2018-04-05 18:17:47 +02002767
Akronadab5e52018-08-20 13:50:53 +02002768 var list = vc.builder().firstChild.firstChild;
Akron3ad46942018-08-22 16:47:14 +02002769 expect(list.getElementsByTagName("LI")[0].innerText).toEqual('referTo');
2770 expect(list.getElementsByTagName("LI")[1].innerText).toEqual('a');
2771 expect(list.getElementsByTagName("LI")[2].innerText).toEqual('b');
2772 expect(list.getElementsByTagName("LI")[3].innerText).toEqual('c');
Akron712733a2018-04-05 18:17:47 +02002773
2774 vc = vcClass.create([
2775 ['d', 'text'],
2776 ['e', 'string'],
2777 ['f', 'date']
2778 ]).fromJson();
Akronadab5e52018-08-20 13:50:53 +02002779 expect(vc.builder().firstChild.classList.contains('unspecified')).toBeTruthy();
2780 expect(vc.builder().firstChild.firstChild.tagName).toEqual('SPAN');
Akron712733a2018-04-05 18:17:47 +02002781
2782 // Click on unspecified
Akronadab5e52018-08-20 13:50:53 +02002783 vc.builder().firstChild.firstChild.click();
2784 expect(vc.builder().firstChild.firstChild.tagName).toEqual('UL');
Akron712733a2018-04-05 18:17:47 +02002785
Akronadab5e52018-08-20 13:50:53 +02002786 list = vc.builder().firstChild.firstChild;
Akron3ad46942018-08-22 16:47:14 +02002787 expect(list.getElementsByTagName("LI")[0].innerText).toEqual('referTo');
2788 expect(list.getElementsByTagName("LI")[1].innerText).toEqual('d');
2789 expect(list.getElementsByTagName("LI")[2].innerText).toEqual('e');
2790 expect(list.getElementsByTagName("LI")[3].innerText).toEqual('f');
Akron31d89942018-04-06 16:44:51 +02002791 // blur
2792 document.body.click();
Akron712733a2018-04-05 18:17:47 +02002793 });
Akrone65a88a2018-04-05 19:14:20 +02002794
2795 // Reinitialize to make tests stable
2796 vc = vcClass.create([
2797 ['d', 'text'],
2798 ['e', 'string'],
2799 ['f', 'date']
2800 ]).fromJson();
2801
2802 it('should be clickable on key', function () {
Akron31d89942018-04-06 16:44:51 +02002803 // Click on unspecified
Akronadab5e52018-08-20 13:50:53 +02002804 vc.builder().firstChild.firstChild.click();
Akrone65a88a2018-04-05 19:14:20 +02002805 // Click on "d"
Akronadab5e52018-08-20 13:50:53 +02002806 vc.builder().firstChild.firstChild.getElementsByTagName("LI")[1].click();
2807 expect(vc.builder().firstChild.firstChild.tagName).toEqual('SPAN');
2808 expect(vc.builder().firstChild.firstChild.innerText).toEqual('d');
2809 expect(vc.builder().firstChild.children[1].innerText).toEqual('eq');
2810 expect(vc.builder().firstChild.children[1].getAttribute('data-type')).toEqual('text');
Akron31d89942018-04-06 16:44:51 +02002811 // blur
2812 document.body.click();
Akrone65a88a2018-04-05 19:14:20 +02002813 });
2814
Akron31d89942018-04-06 16:44:51 +02002815 it('should be clickable on operation for text', function () {
2816 // Click on "d" (or unspecified)
Akronadab5e52018-08-20 13:50:53 +02002817 vc.builder().firstChild.firstChild.click();
Akron31d89942018-04-06 16:44:51 +02002818
2819 // Choose "d"
Akronadab5e52018-08-20 13:50:53 +02002820 vc.builder().firstChild.firstChild.getElementsByTagName("LI")[1].click();
Akron31d89942018-04-06 16:44:51 +02002821
2822 // Click on matchop
Akronadab5e52018-08-20 13:50:53 +02002823 vc.builder().firstChild.children[1].click();
Akron31d89942018-04-06 16:44:51 +02002824
Akronadab5e52018-08-20 13:50:53 +02002825 expect(vc.builder().firstChild.children[1].tagName).toEqual('UL');
Akron31d89942018-04-06 16:44:51 +02002826
Akronadab5e52018-08-20 13:50:53 +02002827 var ul = vc.builder().firstChild.children[1];
Akrone65a88a2018-04-05 19:14:20 +02002828 expect(ul.getElementsByTagName('li')[0].innerText).toEqual("eq");
2829 expect(ul.getElementsByTagName('li')[1].innerText).toEqual("ne");
2830 expect(ul.getElementsByTagName('li')[2].innerText).toEqual("contains");
2831 expect(ul.getElementsByTagName('li')[3].innerText).toEqual("containsnot");
2832 expect(ul.getElementsByTagName('li')[4]).toBeUndefined();
Akron31d89942018-04-06 16:44:51 +02002833
2834 // Choose "contains"
2835 ul.getElementsByTagName('li')[2].click();
Akronadab5e52018-08-20 13:50:53 +02002836 expect(vc.builder().firstChild.children[1].tagName).toEqual("SPAN");
2837 expect(vc.builder().firstChild.children[1].innerText).toEqual("contains");
Akron31d89942018-04-06 16:44:51 +02002838 // blur
2839 document.body.click();
Akrone65a88a2018-04-05 19:14:20 +02002840 })
Akron31d89942018-04-06 16:44:51 +02002841
2842 it('should be clickable on operation for string', function () {
2843 // Click on "d" (or unspecified)
Akronadab5e52018-08-20 13:50:53 +02002844 vc.builder().firstChild.firstChild.click();
Akron31d89942018-04-06 16:44:51 +02002845
2846 // Choose "e"
Akronadab5e52018-08-20 13:50:53 +02002847 vc.builder().firstChild.firstChild.getElementsByTagName("LI")[2].click();
2848
Akron31d89942018-04-06 16:44:51 +02002849 // As a consequence the matchoperator may no longer
2850 // be valid and needs to be re-evaluated
Akronadab5e52018-08-20 13:50:53 +02002851 var fc = vc.builder().firstChild;
Akron31d89942018-04-06 16:44:51 +02002852 expect(fc.firstChild.tagName).toEqual('SPAN');
2853 expect(fc.firstChild.innerText).toEqual('e');
2854 expect(fc.children[1].innerText).toEqual('eq');
2855 expect(fc.children[1].getAttribute('data-type')).toEqual('string');
2856
Akronadab5e52018-08-20 13:50:53 +02002857 vc.builder().firstChild.children[1].click();
Akron31d89942018-04-06 16:44:51 +02002858
Akronadab5e52018-08-20 13:50:53 +02002859 expect(vc.builder().firstChild.children[1].tagName).toEqual('UL');
Akron31d89942018-04-06 16:44:51 +02002860
Akronadab5e52018-08-20 13:50:53 +02002861 var ul = vc.builder().firstChild.children[1];
Akron31d89942018-04-06 16:44:51 +02002862 expect(ul.getElementsByTagName('li')[0].innerText).toEqual("eq");
2863 expect(ul.getElementsByTagName('li')[1].innerText).toEqual("ne");
2864 expect(ul.getElementsByTagName('li')[2]).toBeUndefined();
2865
2866 // Choose "ne"
2867 ul.getElementsByTagName('li')[1].click();
Akronadab5e52018-08-20 13:50:53 +02002868 expect(vc.builder().firstChild.children[1].tagName).toEqual("SPAN");
2869 expect(vc.builder().firstChild.children[1].innerText).toEqual("ne");
Akron8db5e3a2018-05-28 19:25:26 +02002870
2871 // Click on text
Akronebc96662018-08-29 17:36:20 +02002872 expect(vc.builder().firstChild.children[2].innerText).toEqual(KorAP.Locale.EMPTY);
2873 expect(vc.builder().firstChild.children[2].classList.contains('unspecified')).toEqual(true);
Akronadab5e52018-08-20 13:50:53 +02002874 vc.builder().firstChild.children[2].click();
Akron8db5e3a2018-05-28 19:25:26 +02002875
2876 // Blur text element
Akronadab5e52018-08-20 13:50:53 +02002877 expect(vc.builder().firstChild.children[2].firstChild.value).toEqual('');
Akron8db5e3a2018-05-28 19:25:26 +02002878
Akron31d89942018-04-06 16:44:51 +02002879 // blur
2880 document.body.click();
2881 });
2882
2883 it('should be clickable on operation for date', function () {
2884
2885 // Replay matchop check - so it's guaranteed that "ne" is chosen
2886 // Click on "e" (or unspecified)
Akronadab5e52018-08-20 13:50:53 +02002887 vc.builder().firstChild.firstChild.click();
Akron31d89942018-04-06 16:44:51 +02002888 // Rechoose "e"
Akronadab5e52018-08-20 13:50:53 +02002889 vc.builder().firstChild.firstChild.getElementsByTagName("LI")[1].click();
Akron31d89942018-04-06 16:44:51 +02002890 // Click on matchop
Akronadab5e52018-08-20 13:50:53 +02002891 vc.builder().firstChild.children[1].click();
Akron31d89942018-04-06 16:44:51 +02002892 // Choose "ne"
Akronadab5e52018-08-20 13:50:53 +02002893 vc.builder().firstChild.children[1].getElementsByTagName('li')[1].click();
2894 expect(vc.builder().firstChild.children[1].innerText).toEqual("ne");
Akron31d89942018-04-06 16:44:51 +02002895
2896 // Click on "e"
Akronadab5e52018-08-20 13:50:53 +02002897 vc.builder().firstChild.firstChild.click();
Akron31d89942018-04-06 16:44:51 +02002898 // Choose "f"
Akronadab5e52018-08-20 13:50:53 +02002899 vc.builder().firstChild.firstChild.getElementsByTagName("LI")[3].click();
Akron31d89942018-04-06 16:44:51 +02002900
2901 // The matchoperator should still be "ne" as this is valid for dates as well (now)
Akronadab5e52018-08-20 13:50:53 +02002902 var fc = vc.builder().firstChild;
Akron31d89942018-04-06 16:44:51 +02002903 expect(fc.firstChild.tagName).toEqual('SPAN');
2904 expect(fc.firstChild.innerText).toEqual('f');
2905 expect(fc.children[1].innerText).toEqual('ne');
2906 // blur
2907 document.body.click();
2908 });
Akronddc98a72018-04-06 17:33:52 +02002909
2910
2911 // Check json deserialization
2912 it('should be initializable', function () {
2913 vc = vcClass.create([
2914 ['a', 'text'],
2915 ['b', 'string'],
2916 ['c', 'date']
2917 ]).fromJson({
2918 "@type" : "koral:doc",
2919 "key":"a",
2920 "value":"Baum"
2921 });
2922
Akronadab5e52018-08-20 13:50:53 +02002923 expect(vc.builder().firstChild.children[1].getAttribute('data-type')).toEqual('text');
Akronddc98a72018-04-06 17:33:52 +02002924 });
Akron712733a2018-04-05 18:17:47 +02002925 });
2926
2927
Akrone4961b12017-05-10 21:04:46 +02002928 // Check prefix
2929 describe('KorAP.VC.Prefix', function () {
2930
2931 it('should be initializable', function () {
2932 var p = prefixClass.create();
2933 expect(p.element().classList.contains('pref')).toBeTruthy();
2934 expect(p.isSet()).not.toBeTruthy();
2935 });
2936
2937
2938 it('should be clickable', function () {
2939 var vc = vcClass.create([
Akron712733a2018-04-05 18:17:47 +02002940 ['a', null],
2941 ['b', null],
2942 ['c', null]
2943 ]).fromJson();
Akronadab5e52018-08-20 13:50:53 +02002944 expect(vc.builder().firstChild.classList.contains('unspecified')).toBeTruthy();
Akrone4961b12017-05-10 21:04:46 +02002945
2946 // This should open up the menu
Akronadab5e52018-08-20 13:50:53 +02002947 vc.builder().firstChild.firstChild.click();
2948 expect(vc.builder().firstChild.firstChild.tagName).toEqual('UL');
Akrone4961b12017-05-10 21:04:46 +02002949
2950 KorAP._vcKeyMenu._prefix.clear();
2951 KorAP._vcKeyMenu._prefix.add('x');
2952
Akronadab5e52018-08-20 13:50:53 +02002953 var prefElement = vc.builder().querySelector('span.pref');
Akrone4961b12017-05-10 21:04:46 +02002954 expect(prefElement.innerText).toEqual('x');
2955
2956 // This should add key 'x' to VC
2957 prefElement.click();
2958
Akronadab5e52018-08-20 13:50:53 +02002959 expect(vc.builder().firstChild.classList.contains('doc')).toBeTruthy();
2960 expect(vc.builder().firstChild.firstChild.className).toEqual('key');
2961 expect(vc.builder().firstChild.firstChild.innerText).toEqual('x');
Akrone4961b12017-05-10 21:04:46 +02002962 });
2963 });
Akron68d28322018-08-27 15:02:42 +02002964
Akron889ec292018-11-19 17:56:01 +01002965 // Check fragment handling for corpusByMatch helper
Akron68d28322018-08-27 15:02:42 +02002966 describe('KorAP.VC.Fragment', function () {
2967 it('should be initializable', function () {
2968 var f = fragmentClass.create();
2969 var e = f.element();
2970 expect(e.classList.contains('vc')).toBeTruthy();
2971 expect(e.classList.contains('fragment')).toBeTruthy();
Akrond45a1702018-11-19 18:15:17 +01002972 expect(e.firstChild.children.length).toEqual(0);
Akron68d28322018-08-27 15:02:42 +02002973 });
2974
2975 it('should be expansable', function () {
2976 var f = fragmentClass.create();
2977 f.add("author", "Peter");
2978
Akrond45a1702018-11-19 18:15:17 +01002979 var root = f.element().lastChild.firstChild;
Akron68d28322018-08-27 15:02:42 +02002980
2981 expect(root.classList.contains("doc")).toBeTruthy();
2982 expect(root.children[0].tagName).toEqual("SPAN");
2983 expect(root.children[0].textContent).toEqual("author");
2984 expect(root.children[1].tagName).toEqual("SPAN");
2985 expect(root.children[1].textContent).toEqual("eq");
2986 expect(root.children[2].tagName).toEqual("SPAN");
2987 expect(root.children[2].textContent).toEqual("Peter");
2988
2989 f.add("title", "Example");
2990
Akrond45a1702018-11-19 18:15:17 +01002991 root = f.element().lastChild.firstChild;
Akron68d28322018-08-27 15:02:42 +02002992
2993 expect(root.classList.contains("docGroup")).toBeTruthy();
2994
2995 var doc = root.children[0];
2996
2997 expect(doc.children[0].tagName).toEqual("SPAN");
2998 expect(doc.children[0].textContent).toEqual("author");
2999 expect(doc.children[1].tagName).toEqual("SPAN");
3000 expect(doc.children[1].textContent).toEqual("eq");
3001 expect(doc.children[2].tagName).toEqual("SPAN");
3002 expect(doc.children[2].textContent).toEqual("Peter");
3003
3004 doc = root.children[1];
3005
3006 expect(doc.children[0].tagName).toEqual("SPAN");
3007 expect(doc.children[0].textContent).toEqual("title");
3008 expect(doc.children[1].tagName).toEqual("SPAN");
3009 expect(doc.children[1].textContent).toEqual("eq");
3010 expect(doc.children[2].tagName).toEqual("SPAN");
3011 expect(doc.children[2].textContent).toEqual("Example");
3012 });
3013
3014
3015 it('should be reducible', function () {
3016 var f = fragmentClass.create();
Akrond45a1702018-11-19 18:15:17 +01003017 expect(f.element().lastChild.children.length).toEqual(0);
Akron889ec292018-11-19 17:56:01 +01003018
Akron68d28322018-08-27 15:02:42 +02003019 f.add("author", "Peter");
3020 f.add("title", "Example");
3021
Akrond45a1702018-11-19 18:15:17 +01003022 expect(f.element().lastChild.children.length).toEqual(1);
Akron889ec292018-11-19 17:56:01 +01003023
Akrond45a1702018-11-19 18:15:17 +01003024 var root = f.element().lastChild.firstChild;
Akron889ec292018-11-19 17:56:01 +01003025
Akron68d28322018-08-27 15:02:42 +02003026 expect(root.classList.contains("docGroup")).toBeTruthy();
3027
3028 expect(root.children.length).toEqual(2);
3029
3030 f.remove("author","Peter");
3031
Akrond45a1702018-11-19 18:15:17 +01003032 root = f.element().lastChild.firstChild;
Akron68d28322018-08-27 15:02:42 +02003033 expect(root.classList.contains("doc")).toBeTruthy();
3034
3035 expect(root.children[0].tagName).toEqual("SPAN");
3036 expect(root.children[0].textContent).toEqual("title");
3037 expect(root.children[1].tagName).toEqual("SPAN");
3038 expect(root.children[1].textContent).toEqual("eq");
3039 expect(root.children[2].tagName).toEqual("SPAN");
3040 expect(root.children[2].textContent).toEqual("Example");
Akron889ec292018-11-19 17:56:01 +01003041
3042 f.remove("title","Example");
3043
Akrond45a1702018-11-19 18:15:17 +01003044 expect(f.element().lastChild.children.length).toEqual(0);
Akron68d28322018-08-27 15:02:42 +02003045 });
3046 });
Nils Diewald2fe12e12015-03-06 16:47:06 +00003047});