blob: e9ec76b56ee79f115fa5d1e2a3a5d88eac759c24 [file] [log] [blame]
Nils Diewald6ac292b2015-01-15 21:33:21 +00001/*
Nils Diewald7c8ced22015-04-15 19:21:00 +00002 * Todo: In demoSpec: Create "and" on the last element of the top "or"-Group
3 */
4define(['vc'], function () {
Nils Diewald6ac292b2015-01-15 21:33:21 +00005
Nils Diewald7c8ced22015-04-15 19:21:00 +00006 var vcClass = require('vc');
7 var docClass = require('vc/doc');
Akrone4961b12017-05-10 21:04:46 +02008 var menuClass = require('vc/menu');
9 var prefixClass = require('vc/prefix');
Nils Diewald7c8ced22015-04-15 19:21:00 +000010 var docGroupClass = require('vc/docgroup');
Akronb19803c2018-08-16 16:39:42 +020011 var docGroupRefClass = require('vc/docgroupref');
Nils Diewald7c8ced22015-04-15 19:21:00 +000012 var unspecifiedClass = require('vc/unspecified');
13 var operatorsClass = require('vc/operators');
14 var rewriteClass = require('vc/rewrite');
Nils Diewaldf0c4f112015-05-05 12:56:59 +000015 var stringValClass = require('vc/stringval');
Nils Diewald6ac292b2015-01-15 21:33:21 +000016
Nils Diewald7c8ced22015-04-15 19:21:00 +000017 // Helper method for building factories
18 buildFactory = function (objClass, defaults) {
19 return {
20 create : function (overwrites) {
Akron712733a2018-04-05 18:17:47 +020021 var newObj = {};
22 for (var prop in defaults) {
23 newObj[prop] = defaults[prop];
24 };
25 for (var prop in overwrites) {
26 newObj[prop] = overwrites[prop];
27 };
28 return objClass.create().fromJson(newObj);
Nils Diewald7c8ced22015-04-15 19:21:00 +000029 }
Nils Diewald0b6c0412014-12-19 03:55:57 +000030 }
Nils Diewald7c8ced22015-04-15 19:21:00 +000031 };
Nils Diewald0b6c0412014-12-19 03:55:57 +000032
Nils Diewald7c8ced22015-04-15 19:21:00 +000033 function _andOn (obj) {
Akrond141a362018-07-10 18:12:13 +020034 KorAP._and.apply(obj);
Nils Diewald7c8ced22015-04-15 19:21:00 +000035 };
Nils Diewald52f7eb12015-01-12 17:30:04 +000036
Nils Diewald7c8ced22015-04-15 19:21:00 +000037 function _orOn (obj) {
Akrond141a362018-07-10 18:12:13 +020038 KorAP._or.apply(obj);
Nils Diewald7c8ced22015-04-15 19:21:00 +000039 };
Nils Diewald52f7eb12015-01-12 17:30:04 +000040
Nils Diewald7c8ced22015-04-15 19:21:00 +000041 function _delOn (obj) {
Akrond141a362018-07-10 18:12:13 +020042 KorAP._delete.apply(obj);
Nils Diewald7c8ced22015-04-15 19:21:00 +000043 };
Nils Diewald52f7eb12015-01-12 17:30:04 +000044
Nils Diewald7c8ced22015-04-15 19:21:00 +000045 var demoFactory = buildFactory(vcClass, {
46 "@type":"koral:docGroup",
47 "operation":"operation:or",
48 "operands":[
Nils Diewaldf219eb82015-01-07 20:15:42 +000049 {
Akron712733a2018-04-05 18:17:47 +020050 "@type":"koral:docGroup",
51 "operation":"operation:and",
52 "operands":[
Nils Diewald7c8ced22015-04-15 19:21:00 +000053 {
54 "@type":"koral:doc",
55 "key":"Titel",
56 "value":"Baum",
57 "match":"match:eq"
58 },
59 {
60 "@type":"koral:doc",
61 "key":"Veröffentlichungsort",
62 "value":"hihi",
63 "match":"match:eq"
64 },
65 {
66 "@type":"koral:docGroup",
67 "operation":"operation:or",
68 "operands":[
69 {
Akron712733a2018-04-05 18:17:47 +020070 "@type":"koral:doc",
71 "key":"Titel",
72 "value":"Baum",
73 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +000074 },
75 {
Akron712733a2018-04-05 18:17:47 +020076 "@type":"koral:doc",
77 "key":"Veröffentlichungsort",
78 "value":"hihi",
79 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +000080 }
81 ]
82 }
Akron712733a2018-04-05 18:17:47 +020083 ]
Nils Diewaldf219eb82015-01-07 20:15:42 +000084 },
85 {
Akron712733a2018-04-05 18:17:47 +020086 "@type":"koral:doc",
87 "key":"Untertitel",
88 "value":"huhu",
89 "match":"match:contains"
Nils Diewaldf219eb82015-01-07 20:15:42 +000090 }
91 ]
92 });
93
Nils Diewald7c8ced22015-04-15 19:21:00 +000094 describe('KorAP.Doc', function () {
95 // Create example factories
96 var stringFactory = buildFactory(docClass, {
97 "key" : "author",
98 "value" : "Max Birkendale",
Akron31d89942018-04-06 16:44:51 +020099 "type" : "type:string",
Nils Diewald7c8ced22015-04-15 19:21:00 +0000100 "@type" : "koral:doc"
101 });
102
103 // Create example factories
Akron712733a2018-04-05 18:17:47 +0200104 var textFactory = buildFactory(docClass, {
105 "key" : "author",
106 "value" : "Birkendale",
Akron31d89942018-04-06 16:44:51 +0200107 "type" : "type:string",
Akron712733a2018-04-05 18:17:47 +0200108 "match" : "match:contains",
109 "@type" : "koral:doc"
110 });
111
112 // Create example factories
Nils Diewald7c8ced22015-04-15 19:21:00 +0000113 var dateFactory = buildFactory(docClass, {
114 "key" : "pubDate",
115 "type" : "type:date",
116 "match" : "match:eq",
117 "value" : "2014-11-05",
118 "@type" : "koral:doc"
119 });
120
121 // Create example factories
122 var regexFactory = buildFactory(docClass, {
123 "key" : "title",
124 "type" : "type:regex",
125 "value" : "[^b]ee.+?",
126 "@type" : "koral:doc"
127 });
128
129 it('should be initializable', function () {
130 var doc = docClass.create();
131 expect(doc.matchop()).toEqual('eq');
132 expect(doc.key()).toBeUndefined();
133 expect(doc.value()).toBeUndefined();
134 expect(doc.type()).toEqual("string");
135 });
136
137 it('should be definable', function () {
138
139 // Empty doc
140 var doc = docClass.create();
141
142 // Set values
143 doc.key("title");
Akron8db5e3a2018-05-28 19:25:26 +0200144
Nils Diewald7c8ced22015-04-15 19:21:00 +0000145 doc.value("Der alte Mann");
146 expect(doc.matchop()).toEqual('eq');
147 expect(doc.key()).toEqual("title");
148 expect(doc.type()).toEqual("string");
149 expect(doc.value()).toEqual("Der alte Mann");
150 });
151
152
153 it('should deserialize JSON-LD string', function () {
154 var doc;
155
156 // String default
157 doc = stringFactory.create();
158 expect(doc.matchop()).toEqual('eq');
159 expect(doc.key()).toEqual("author");
160 expect(doc.type()).toEqual("string");
161 expect(doc.value()).toEqual("Max Birkendale");
162
163 // No valid string
164 doc = stringFactory.create({
Akron712733a2018-04-05 18:17:47 +0200165 value : undefined
Nils Diewald7c8ced22015-04-15 19:21:00 +0000166 });
167 expect(doc).toBeUndefined();
168
169 // No valid string
170 doc = stringFactory.create({
Akron712733a2018-04-05 18:17:47 +0200171 value : { "foo" : "bar" }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000172 });
173 expect(doc).toBeUndefined();
174
175 // Change match type
176 doc = stringFactory.create({
Akron712733a2018-04-05 18:17:47 +0200177 "match" : "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000178 });
179
180 expect(doc.matchop()).toEqual('ne');
181 expect(doc.key()).toEqual("author");
182 expect(doc.type()).toEqual("string");
183 expect(doc.value()).toEqual("Max Birkendale");
184
185 // Invalid match type
186 doc = stringFactory.create({
Akron712733a2018-04-05 18:17:47 +0200187 "match" : { "foo" : "bar" }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000188 });
189 expect(doc).toBeUndefined();
190 });
191
192 it('should deserialize JSON-LD regex', function () {
193 var doc = regexFactory.create();
194 expect(doc.key()).toEqual("title");
195 expect(doc.type()).toEqual("regex");
196 expect(doc.value()).toEqual("[^b]ee.+?");
197 expect(doc.matchop()).toEqual('eq');
198
199 // change matcher
200 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200201 match : "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000202 });
203 expect(doc.matchop()).toEqual('ne');
Akronea4e9052017-07-06 16:12:05 +0200204 expect(doc.rewrites()).toBeUndefined();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000205
206 // Invalid matcher
207 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200208 match : "match:chook"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000209 });
Akronea4e9052017-07-06 16:12:05 +0200210 expect(doc.matchop()).toEqual('eq');
211 expect(doc.rewrites()).toBeDefined();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000212
213 // Invalid regex
214 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200215 value : "[^b"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000216 });
217 expect(doc).toBeUndefined();
218 });
219
220 it('should deserialize JSON-LD date', function () {
221
222 // Normal date
223 doc = dateFactory.create({});
224
225 expect(doc.matchop()).toEqual('eq');
226 expect(doc.key()).toEqual("pubDate");
227 expect(doc.type()).toEqual("date");
228 expect(doc.value()).toEqual("2014-11-05");
229
230 // Short date 1
231 doc = dateFactory.create({
Akron712733a2018-04-05 18:17:47 +0200232 "value" : "2014-11"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000233 });
234
235 expect(doc.matchop()).toEqual('eq');
236 expect(doc.key()).toEqual("pubDate");
237 expect(doc.type()).toEqual("date");
238 expect(doc.value()).toEqual("2014-11");
239
240 // Short date 2
241 doc = dateFactory.create({
Akron712733a2018-04-05 18:17:47 +0200242 "value" : "2014"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000243 });
244
245 expect(doc.matchop()).toEqual('eq');
246 expect(doc.key()).toEqual("pubDate");
247 expect(doc.type()).toEqual("date");
248 expect(doc.value()).toEqual("2014");
249
250 // Invalid date!
251 doc = dateFactory.create({
Akron712733a2018-04-05 18:17:47 +0200252 "value" : "2014-11-050"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000253 });
254 expect(doc).toBeUndefined();
255
256 // Invalid matcher!
257 doc = dateFactory.create({
Akron31d89942018-04-06 16:44:51 +0200258 "match" : "match:contains",
Nils Diewald7c8ced22015-04-15 19:21:00 +0000259 });
Akronea4e9052017-07-06 16:12:05 +0200260 expect(doc).toBeDefined();
261 expect(doc.rewrites()).toBeDefined();
262 expect(doc.matchop()).toEqual('eq');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000263 });
264
265 it('should be serializale to JSON', function () {
266
267 // Empty doc
268 var doc = docClass.create();
269 expect(doc.toJson()).toEqual(jasmine.any(Object));
270
271 // Serialize string
272 doc = stringFactory.create();
273 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200274 "@type" : "koral:doc",
275 "type" : "type:string",
276 "key" : "author",
277 "value" : "Max Birkendale",
278 "match" : "match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000279 }));
280
281 // Serialize regex
282 doc = regexFactory.create();
283 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200284 "@type" : "koral:doc",
285 "type" : "type:regex",
286 "value" : "[^b]ee.+?",
287 "match" : "match:eq",
288 "key" : 'title'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000289 }));
290
291 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200292 match: "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000293 });
294 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200295 "@type" : "koral:doc",
296 "type" : "type:regex",
297 "value" : "[^b]ee.+?",
298 "match" : "match:ne",
299 "key" : 'title'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000300 }));
301
302 doc = dateFactory.create();
303 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200304 "@type" : "koral:doc",
305 "type" : "type:date",
306 "value" : "2014-11-05",
307 "match" : "match:eq",
308 "key" : 'pubDate'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000309 }));
310
311 doc = dateFactory.create({
Akron712733a2018-04-05 18:17:47 +0200312 value : "2014"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000313 });
314 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200315 "@type" : "koral:doc",
316 "type" : "type:date",
317 "value" : "2014",
318 "match" : "match:eq",
319 "key" : 'pubDate'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000320 }));
321 });
322
323
324 it('should be serializale to String', function () {
325 // Empty doc
326 var doc = docClass.create();
327 expect(doc.toQuery()).toEqual("");
328
329 // Serialize string
330 doc = stringFactory.create();
331 expect(doc.toQuery()).toEqual('author = "Max Birkendale"');
332
333 // Serialize string with quotes
334 doc = stringFactory.create({ "value" : 'Max "Der Coole" Birkendate'});
335 expect(doc.toQuery()).toEqual('author = "Max \\"Der Coole\\" Birkendate"');
336
337 // Serialize regex
338 doc = regexFactory.create();
339 expect(doc.toQuery()).toEqual('title = /[^b]ee.+?/');
340
341 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200342 match: "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000343 });
344 expect(doc.toQuery()).toEqual('title != /[^b]ee.+?/');
345
Akron8778f5d2017-06-30 21:25:55 +0200346 doc = regexFactory.create({
Akron712733a2018-04-05 18:17:47 +0200347 value: "WPD/AAA/00001"
Akron8778f5d2017-06-30 21:25:55 +0200348 });
349 expect(doc.toQuery()).toEqual('title = /WPD\\/AAA\\/00001/');
350
Nils Diewald7c8ced22015-04-15 19:21:00 +0000351 doc = dateFactory.create();
352 expect(doc.toQuery()).toEqual('pubDate in 2014-11-05');
353
354 doc = dateFactory.create({
Akron712733a2018-04-05 18:17:47 +0200355 value : "2014"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000356 });
357 expect(doc.toQuery()).toEqual('pubDate in 2014');
358 });
359 });
360
361
362 describe('KorAP.DocGroup', function () {
363 // Create example factories
364 var docFactory = buildFactory(
365 docClass,
Nils Diewaldf219eb82015-01-07 20:15:42 +0000366 {
Akron712733a2018-04-05 18:17:47 +0200367 "@type" : "koral:doc",
368 "match":"match:eq",
369 "key" : "author",
370 "value" : "Max Birkendale"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000371 }
372 );
373
374 var docGroupFactory = buildFactory(
375 docGroupClass, {
Akron712733a2018-04-05 18:17:47 +0200376 "@type" : "koral:docGroup",
377 "operation" : "operation:and",
378 "operands" : [
379 docFactory.create().toJson(),
380 docFactory.create({
381 "key" : "pubDate",
382 "type" : "type:date",
383 "value" : "2014-12-05"
384 }).toJson()
385 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000386 });
387
388 it('should be initializable', function () {
389 // Create empty group
390 var docGroup = docGroupClass.create();
391 expect(docGroup.operation()).toEqual('and');
392
393 // Create empty group
394 docGroup = docGroupClass.create();
395 docGroup.operation('or');
396 expect(docGroup.operation()).toEqual('or');
397 });
398
399 it('should be definable', function () {
400
401 // Empty group
402 var docGroup = docGroupClass.create();
403 expect(docGroup.operation()).toEqual('and');
404
405 // Set values
406 docGroup.operation("or");
407 expect(docGroup.operation()).toEqual('or');
408
409 // Set invalid values
410 docGroup.operation("hui");
411 expect(docGroup.operation()).toEqual('or');
412 });
413
414 it('should be deserializable', function () {
415 var docGroup = docGroupFactory.create();
416 expect(docGroup.operation()).toEqual("and");
417 expect(docGroup.operands().length).toEqual(2);
418
419 var op1 = docGroup.getOperand(0);
420 expect(op1.type()).toEqual("string");
421 expect(op1.key()).toEqual("author");
422 expect(op1.value()).toEqual("Max Birkendale");
423 expect(op1.matchop()).toEqual("eq");
424
425 var op2 = docGroup.getOperand(1);
426 expect(op2.type()).toEqual("date");
427 expect(op2.key()).toEqual("pubDate");
428 expect(op2.value()).toEqual("2014-12-05");
429 expect(op2.matchop()).toEqual("eq");
430
431 // Append empty group
432 var newGroup = docGroup.append(docGroupClass.create());
433 newGroup.operation('or');
434 newGroup.append(docFactory.create());
435 newGroup.append(docFactory.create({
Akron712733a2018-04-05 18:17:47 +0200436 "type" : "type:regex",
437 "key" : "title",
438 "value" : "^e.+?$",
439 "match" : "match:ne"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000440 }));
441
442 expect(docGroup.operation()).toEqual("and");
443 expect(docGroup.operands().length).toEqual(3);
444
445 var op1 = docGroup.getOperand(0);
446 expect(op1.ldType()).toEqual("doc");
447 expect(op1.type()).toEqual("string");
448 expect(op1.key()).toEqual("author");
449 expect(op1.value()).toEqual("Max Birkendale");
450 expect(op1.matchop()).toEqual("eq");
451
452 var op2 = docGroup.getOperand(1);
453 expect(op2.ldType()).toEqual("doc");
454 expect(op2.type()).toEqual("date");
455 expect(op2.key()).toEqual("pubDate");
456 expect(op2.value()).toEqual("2014-12-05");
457 expect(op2.matchop()).toEqual("eq");
458
459 var op3 = docGroup.getOperand(2);
460 expect(op3.ldType()).toEqual("docGroup");
461 expect(op3.operation()).toEqual("or");
462
463 var op4 = op3.getOperand(0);
464 expect(op4.ldType()).toEqual("doc");
465 expect(op4.type()).toEqual("string");
466 expect(op4.key()).toEqual("author");
467 expect(op4.value()).toEqual("Max Birkendale");
468 expect(op4.matchop()).toEqual("eq");
469
470 var op5 = op3.getOperand(1);
471 expect(op5.ldType()).toEqual("doc");
472 expect(op5.type()).toEqual("regex");
473 expect(op5.key()).toEqual("title");
474 expect(op5.value()).toEqual("^e.+?$");
475 expect(op5.matchop()).toEqual("ne");
476 });
477
478 it('should be serializable to JSON', function () {
479 var docGroup = docGroupFactory.create();
480
481 expect(docGroup.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200482 "@type" : "koral:docGroup",
483 "operation" : "operation:and",
484 "operands" : [
485 {
486 "@type": 'koral:doc',
487 "key" : 'author',
488 "match": 'match:eq',
489 "value": 'Max Birkendale',
490 "type": 'type:string'
491 },
492 {
493 "@type": 'koral:doc',
494 "key": 'pubDate',
495 "match": 'match:eq',
496 "value": '2014-12-05',
497 "type": 'type:date'
498 }
499 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000500 }));
501 });
Nils Diewalde15b7a22015-01-09 21:50:21 +0000502
Nils Diewald7c8ced22015-04-15 19:21:00 +0000503 it('should be serializable to String', function () {
504 var docGroup = docGroupFactory.create();
505 expect(docGroup.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +0200506 'author = "Max Birkendale" & pubDate in 2014-12-05'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000507 );
Nils Diewald52f7eb12015-01-12 17:30:04 +0000508
Nils Diewald7c8ced22015-04-15 19:21:00 +0000509 docGroup = docGroupFactory.create({
Akron712733a2018-04-05 18:17:47 +0200510 "@type" : "koral:docGroup",
511 "operation" : "operation:or",
512 "operands" : [
513 {
514 "@type": 'koral:doc',
515 "key" : 'author',
516 "match": 'match:eq',
517 "value": 'Max Birkendale',
518 "type": 'type:string'
519 },
520 {
521 "@type" : "koral:docGroup",
522 "operation" : "operation:and",
523 "operands" : [
524 {
525 "@type": 'koral:doc',
526 "key": 'pubDate',
527 "match": 'match:geq',
528 "value": '2014-05-12',
529 "type": 'type:date'
530 },
531 {
532 "@type": 'koral:doc',
533 "key": 'pubDate',
534 "match": 'match:leq',
535 "value": '2014-12-05',
536 "type": 'type:date'
537 },
538 {
539 "@type": 'koral:doc',
540 "key": 'foo',
541 "match": 'match:ne',
542 "value": '[a]?bar',
543 "type": 'type:regex'
544 }
545 ]
546 }
547 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000548 });
549 expect(docGroup.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +0200550 'author = "Max Birkendale" | ' +
551 '(pubDate since 2014-05-12 & ' +
552 'pubDate until 2014-12-05 & foo != /[a]?bar/)'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000553 );
554 });
555 });
556
Akronb19803c2018-08-16 16:39:42 +0200557 describe('KorAP.DocGroupRef', function () {
558 // Create example factories
559 var docRefFactory = buildFactory(
560 docGroupRefClass,
561 {
562 "@type" : "koral:docGroupRef",
563 "ref" : "@max/myCorpus"
564 }
565 );
566
567 it('should be initializable', function () {
568 var vcRef = docGroupRefClass.create();
569 expect(vcRef.ref()).toBeUndefined();
570 });
571
572 it('should be definable', function () {
573 var vcRef = docGroupRefClass.create();
574 vcRef.ref("@peter/mycorpus");
575 expect(vcRef.ref()).toEqual("@peter/mycorpus");
576 vcRef.ref("@peter/mycorpus2");
577 expect(vcRef.ref()).toEqual("@peter/mycorpus2");
578 });
579
580 it('should deserialize JSON-LD string', function () {
581 var vcRef = docRefFactory.create();
582 expect(vcRef.ref()).toEqual("@max/myCorpus");
583 });
584
585 it('should serialize to JSON-LD', function () {
586 var vcRef = docRefFactory.create();
587 expect(vcRef.toJson()).toEqual(jasmine.objectContaining({
588 "@type" : "koral:docGroupRef",
589 "ref":"@max/myCorpus"
590 }));
591
592 vcRef.ref("@peter/myCorpus2");
593 expect(vcRef.toJson()).toEqual(jasmine.objectContaining({
594 "@type" : "koral:docGroupRef",
595 "ref":"@peter/myCorpus2"
596 }));
597 });
598
599 it('should serialize to a query', function () {
600 var vcRef = docRefFactory.create();
601 expect(vcRef.toQuery()).toEqual(
602 "referTo \"@max/myCorpus\""
603 );
604
605 vcRef.ref("@peter/myCorpus2");
606 expect(vcRef.toQuery()).toEqual(
607 "referTo \"@peter/myCorpus2\""
608 );
609 });
610 });
611
612
Nils Diewald7c8ced22015-04-15 19:21:00 +0000613 describe('KorAP.UnspecifiedDoc', function () {
614 it('should be initializable', function () {
615 var doc = unspecifiedClass.create();
616 var docElement = doc.element();
617 expect(docElement.getAttribute('class')).toEqual('doc unspecified');
618 expect(docElement.firstChild.firstChild.data).toEqual('⋯');
619 expect(docElement.lastChild.lastChild.data).toEqual('⋯');
620 expect(doc.toQuery()).toEqual('');
621
622 // Only removable
623 expect(docElement.lastChild.children.length).toEqual(0);
624 });
625
Akrond141a362018-07-10 18:12:13 +0200626 it('should be removable, when no root', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000627 var docGroup = docGroupClass.create();
628 docGroup.operation('or');
629 expect(docGroup.operation()).toEqual('or');
630
631 docGroup.append({
Akron712733a2018-04-05 18:17:47 +0200632 "@type": 'koral:doc',
633 "key": 'pubDate',
634 "match": 'match:eq',
635 "value": '2014-12-05',
636 "type": 'type:date'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000637 });
638
639 // Add unspecified object
640 docGroup.append();
641
Akrond141a362018-07-10 18:12:13 +0200642 var parent = document.createElement('div');
643 parent.appendChild(docGroup.element());
644
Nils Diewald7c8ced22015-04-15 19:21:00 +0000645 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
646 expect(docGroup.element().children[0].getAttribute('class')).toEqual('doc');
647
648 var unspec = docGroup.element().children[1];
649 expect(unspec.getAttribute('class')).toEqual('doc unspecified');
650
Akronb19803c2018-08-16 16:39:42 +0200651 // Only unspec and delete
652 expect(unspec.children.length).toEqual(2);
653
Nils Diewald7c8ced22015-04-15 19:21:00 +0000654 // Removable
655 expect(unspec.lastChild.children.length).toEqual(1);
656 expect(unspec.lastChild.children[0].getAttribute('class')).toEqual('delete');
657 });
658
Akrond141a362018-07-10 18:12:13 +0200659
Nils Diewald7c8ced22015-04-15 19:21:00 +0000660 it('should be replaceable by a doc', function () {
661 var doc = unspecifiedClass.create();
662 expect(doc.ldType()).toEqual("non");
663 // No parent, therefor not updateable
664 expect(doc.key("baum")).toBeNull();
665
666 var docGroup = docGroupClass.create();
667 docGroup.operation('or');
668 expect(docGroup.operation()).toEqual('or');
669
670 docGroup.append({
Akron712733a2018-04-05 18:17:47 +0200671 "@type": 'koral:doc',
672 "key": 'pubDate',
673 "match": 'match:eq',
674 "value": '2014-12-05',
675 "type": 'type:date'
Nils Diewald7c8ced22015-04-15 19:21:00 +0000676 });
677
678 expect(docGroup.toQuery()).toEqual("pubDate in 2014-12-05");
679 docGroup.append();
680
681 expect(docGroup.getOperand(0).ldType()).toEqual("doc");
682 expect(docGroup.getOperand(1).ldType()).toEqual("non");
683
684 var op = docGroup.getOperand(1).element().lastChild;
Akron0b489ad2018-02-02 16:49:32 +0100685 expect(op.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000686 expect(op.children[0].getAttribute('class')).toEqual('delete');
687 expect(op.children.length).toEqual(1);
688
689 // Replace unspecified doc
690 expect(docGroup.getOperand(1).key("name")).not.toBeNull();
691 expect(docGroup.getOperand(1).ldType()).toEqual("doc");
692 expect(docGroup.getOperand(1).key()).toEqual("name");
Akron55a343b2018-04-06 19:57:36 +0200693 expect(docGroup.getOperand(1).value()).toBeUndefined();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000694
Akronb19803c2018-08-16 16:39:42 +0200695 expect(docGroup.getOperand(1).element().children.length).toEqual(4);
696
Nils Diewald7c8ced22015-04-15 19:21:00 +0000697 op = docGroup.getOperand(1).element().lastChild;
Akron0b489ad2018-02-02 16:49:32 +0100698 expect(op.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000699 expect(op.children[0].getAttribute('class')).toEqual('and');
700 expect(op.children[1].getAttribute('class')).toEqual('or');
701 expect(op.children[2].getAttribute('class')).toEqual('delete');
Akronb19803c2018-08-16 16:39:42 +0200702
Nils Diewald7c8ced22015-04-15 19:21:00 +0000703 expect(op.children.length).toEqual(3);
704
705 docGroup.getOperand(1).value("Pachelbel");
706 expect(docGroup.getOperand(1).value()).toEqual("Pachelbel");
707 expect(docGroup.getOperand(1).type()).toEqual("string");
708 expect(docGroup.getOperand(1).matchop()).toEqual("eq");
709
710 // Specified!
711 expect(docGroup.toQuery()).toEqual('pubDate in 2014-12-05 | name = "Pachelbel"');
712 });
Akronb19803c2018-08-16 16:39:42 +0200713
Nils Diewald7c8ced22015-04-15 19:21:00 +0000714 it('should be replaceable on root', function () {
Nils Diewald6283d692015-04-23 20:32:53 +0000715 var vc = vcClass.create();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000716 expect(vc.toQuery()).toEqual("");
717
718 expect(vc.root().ldType()).toEqual("non");
719
720 // No operators on root
721 op = vc.root().element().lastChild;
722 expect(op.lastChild.textContent).toEqual('⋯');
723
724 // Replace
725 expect(vc.root().key("baum")).not.toBeNull();
726 expect(vc.root().ldType()).toEqual("doc");
727
728 op = vc.root().element().lastChild;
Akron0b489ad2018-02-02 16:49:32 +0100729 expect(op.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000730 expect(op.children[0].getAttribute('class')).toEqual('and');
731 expect(op.children[1].getAttribute('class')).toEqual('or');
732 expect(op.children[2].getAttribute('class')).toEqual('delete');
733 expect(op.children.length).toEqual(3);
734 });
Akron55a343b2018-04-06 19:57:36 +0200735
736 it('should be clickable', function () {
737 var vc = vcClass.create([
738 ["pubDate", "date"]
739 ]);
740 expect(vc.toQuery()).toEqual("");
741 expect(vc.element().firstChild.textContent).toEqual("⋯");
742 vc.element().firstChild.firstChild.click();
743
744 // Click on pubDate
745 vc.element().firstChild.getElementsByTagName("LI")[0].click();
746
747 expect(vc.element().firstChild.firstChild.textContent).toEqual("pubDate");
748 expect(vc.element().firstChild.children[1].getAttribute("data-type")).toEqual("date");
749 });
Nils Diewald7c8ced22015-04-15 19:21:00 +0000750 });
751
752 describe('KorAP.Doc element', function () {
753 it('should be initializable', function () {
754 var docElement = docClass.create(undefined, {
Akron712733a2018-04-05 18:17:47 +0200755 "@type" : "koral:doc",
756 "key":"Titel",
757 "value":"Baum",
758 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000759 });
760 expect(docElement.key()).toEqual('Titel');
761 expect(docElement.matchop()).toEqual('eq');
762 expect(docElement.value()).toEqual('Baum');
763
764 var docE = docElement.element();
765 expect(docE.children[0].firstChild.data).toEqual('Titel');
766 expect(docE.children[1].firstChild.data).toEqual('eq');
767 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
768 expect(docE.children[2].firstChild.data).toEqual('Baum');
769 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
770
771 expect(docElement.toJson()).toEqual(jasmine.objectContaining({
Akron712733a2018-04-05 18:17:47 +0200772 "@type" : "koral:doc",
773 "key":"Titel",
774 "value":"Baum",
775 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +0000776 }));
777 });
Akronb19803c2018-08-16 16:39:42 +0200778
779
780 it('should be replacable by unspecified', function () {
781 var vc = vcClass.create([
782 ["pubDate", "date"]
783 ]).fromJson({
784 "@type" : "koral:doc",
785 "key":"Titel",
786 "value":"Baum",
787 "match":"match:eq"
788 });
789 expect(vc.toQuery()).toEqual("Titel = \"Baum\"");
790
791 var vcE = vc.element();
792 expect(vcE.firstChild.children.length).toEqual(4);
793
794 // Click to delete
795 vcE.firstChild.lastChild.lastChild.click();
796
797 expect(vcE.firstChild.children.length).toEqual(1);
798
799 expect(vcE.firstChild.textContent).toEqual("⋯");
800 vcE.firstChild.firstChild.click();
801
802 // Click on pubDate
803 vcE.firstChild.getElementsByTagName("LI")[0].click();
804
805 expect(vcE.firstChild.firstChild.textContent).toEqual("pubDate");
806 expect(vcE.firstChild.children[1].getAttribute("data-type")).toEqual("date");
807
808 expect(vcE.firstChild.children.length).toEqual(4);
809 });
810
811
812 it('should be replaceable by a docGroupRef', function () {
813 var vc = vcClass.create([
814 ["@referTo", "ref"]
815 ]).fromJson({
816 "@type" : "koral:doc",
817 "key":"Titel",
818 "value":"Baum",
819 "match":"match:eq"
820 });
821
822 expect(vc.toQuery()).toEqual("Titel = \"Baum\"");
823
824 var vcE = vc.element();
825 expect(vcE.firstChild.children.length).toEqual(4);
826
827 // Click to delete
828 vcE.firstChild.lastChild.lastChild.click();
829
830 expect(vcE.firstChild.children.length).toEqual(1);
831
832 expect(vcE.firstChild.textContent).toEqual("⋯");
833 vcE.firstChild.firstChild.click();
834
835 // Click on @referTo
836 vcE.firstChild.getElementsByTagName("LI")[0].click();
837
838 expect(vcE.firstChild.firstChild.textContent).toEqual("@referTo");
839 expect(vcE.firstChild.children[1].getAttribute("data-type")).toEqual("string");
840 expect(vcE.firstChild.children.length).toEqual(3);
841 });
Nils Diewald7c8ced22015-04-15 19:21:00 +0000842 });
843
844 describe('KorAP.DocGroup element', function () {
845 it('should be initializable', function () {
846
847 var docGroup = docGroupClass.create(undefined, {
Akron712733a2018-04-05 18:17:47 +0200848 "@type" : "koral:docGroup",
849 "operation" : "operation:and",
850 "operands" : [
851 {
852 "@type": 'koral:doc',
853 "key" : 'author',
854 "match": 'match:eq',
855 "value": 'Max Birkendale',
856 "type": 'type:string'
857 },
858 {
859 "@type": 'koral:doc',
860 "key": 'pubDate',
861 "match": 'match:eq',
862 "value": '2014-12-05',
863 "type": 'type:date'
864 }
865 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000866 });
867
868 expect(docGroup.operation()).toEqual('and');
869 var e = docGroup.element();
870 expect(e.getAttribute('class')).toEqual('docGroup');
871 expect(e.getAttribute('data-operation')).toEqual('and');
872
873 var first = e.children[0];
874 expect(first.getAttribute('class')).toEqual('doc');
875 expect(first.children[0].getAttribute('class')).toEqual('key');
876 expect(first.children[1].getAttribute('class')).toEqual('match');
877 expect(first.children[2].getAttribute('class')).toEqual('value');
878 expect(first.children[2].getAttribute('data-type')).toEqual('string');
879 expect(first.children[0].firstChild.data).toEqual('author');
880 expect(first.children[1].firstChild.data).toEqual('eq');
881 expect(first.children[2].firstChild.data).toEqual('Max Birkendale');
882
883 var second = e.children[1];
884 expect(second.getAttribute('class')).toEqual('doc');
885 expect(second.children[0].getAttribute('class')).toEqual('key');
886 expect(second.children[1].getAttribute('class')).toEqual('match');
887 expect(second.children[2].getAttribute('class')).toEqual('value');
888 expect(second.children[2].getAttribute('data-type')).toEqual('date');
889 expect(second.children[0].firstChild.data).toEqual('pubDate');
890 expect(second.children[1].firstChild.data).toEqual('eq');
891 expect(second.children[2].firstChild.data).toEqual('2014-12-05');
892 });
893
894 it('should be deserializable with nested groups', function () {
895 var docGroup = docGroupClass.create(undefined, {
Akron712733a2018-04-05 18:17:47 +0200896 "@type" : "koral:docGroup",
897 "operation" : "operation:or",
898 "operands" : [
899 {
900 "@type": 'koral:doc',
901 "key" : 'author',
902 "match": 'match:eq',
903 "value": 'Max Birkendale',
904 "type": 'type:string'
905 },
906 {
907 "@type" : "koral:docGroup",
908 "operation" : "operation:and",
909 "operands" : [
910 {
911 "@type": 'koral:doc',
912 "key": 'pubDate',
913 "match": 'match:geq',
914 "value": '2014-05-12',
915 "type": 'type:date'
916 },
917 {
918 "@type": 'koral:doc',
919 "key": 'pubDate',
920 "match": 'match:leq',
921 "value": '2014-12-05',
922 "type": 'type:date'
923 }
924 ]
925 }
926 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000927 });
928
929 expect(docGroup.operation()).toEqual('or');
930 var e = docGroup.element();
931 expect(e.getAttribute('class')).toEqual('docGroup');
932 expect(e.getAttribute('data-operation')).toEqual('or');
933
934 expect(e.children[0].getAttribute('class')).toEqual('doc');
935 var docop = e.children[0].lastChild;
Akron0b489ad2018-02-02 16:49:32 +0100936 expect(docop.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000937 expect(docop.children[0].getAttribute('class')).toEqual('and');
938 expect(docop.children[1].getAttribute('class')).toEqual('or');
939 expect(docop.children[2].getAttribute('class')).toEqual('delete');
940
941 expect(e.children[1].getAttribute('class')).toEqual('docGroup');
942 expect(e.children[1].getAttribute('data-operation')).toEqual('and');
943
944 // This and-operation can be "or"ed or "delete"d
945 var secop = e.children[1].children[2];
Akron0b489ad2018-02-02 16:49:32 +0100946 expect(secop.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000947 expect(secop.children[0].getAttribute('class')).toEqual('or');
948 expect(secop.children[1].getAttribute('class')).toEqual('delete');
949
950 // This or-operation can be "and"ed or "delete"d
Akron0b489ad2018-02-02 16:49:32 +0100951 expect(e.children[2].getAttribute('class')).toEqual('operators button-group');
952 expect(e.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +0000953 expect(e.lastChild.children[0].getAttribute('class')).toEqual('and');
954 expect(e.lastChild.children[1].getAttribute('class')).toEqual('delete');
955 });
956 });
957
Akronb19803c2018-08-16 16:39:42 +0200958 describe('KorAP.DocGroupRef element', function () {
959 it('should be initializable', function () {
960 var docGroupRef = docGroupRefClass.create(undefined, {
961 "@type" : "koral:docGroupRef",
962 "ref" : "@franz/myVC1"
963 });
964 expect(docGroupRef.ref()).toEqual("@franz/myVC1");
965 var dgrE = docGroupRef.element();
966
967 expect(dgrE.children[0].firstChild.data).toEqual("@referTo");
968 expect(dgrE.children[0].tagName).toEqual("SPAN");
969 expect(dgrE.children[0].classList.contains("key")).toBeTruthy();
970 expect(dgrE.children[0].classList.contains("fixed")).toBeTruthy();
971 expect(dgrE.children[1].firstChild.data).toEqual("@franz/myVC1");
972 expect(dgrE.children[1].tagName).toEqual("SPAN");
973 expect(dgrE.children[1].classList.contains("value")).toBeTruthy();
974 expect(dgrE.children[1].getAttribute("data-type")).toEqual("string");
975 });
976
977 it('should be modifiable on reference', function () {
978 var docGroupRef = docGroupRefClass.create(undefined, {
979 "@type" : "koral:docGroupRef",
980 "ref" : "@franz/myVC1"
981 });
982 var dgrE = docGroupRef.element();
983 expect(docGroupRef.toQuery()).toEqual("referTo \"@franz/myVC1\"");
984 dgrE.children[1].click();
985
986 var input = dgrE.children[1].firstChild;
987 expect(input.tagName).toEqual("INPUT");
988 input.value = "Versuch";
989
990 var event = new KeyboardEvent("keypress", {
991 "key" : "[Enter]",
992 "keyCode" : 13
993 });
994
995 input.dispatchEvent(event);
996 expect(docGroupRef.toQuery()).toEqual("referTo \"Versuch\"");
997 });
998 });
999
1000
Akrone4961b12017-05-10 21:04:46 +02001001 describe('KorAP.VirtualCorpus', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +00001002 var simpleGroupFactory = buildFactory(docGroupClass, {
1003 "@type" : "koral:docGroup",
1004 "operation" : "operation:and",
1005 "operands" : [
Akron712733a2018-04-05 18:17:47 +02001006 {
1007 "@type": 'koral:doc',
1008 "key" : 'author',
1009 "match": 'match:eq',
1010 "value": 'Max Birkendale',
1011 "type": 'type:string'
1012 },
1013 {
1014 "@type": 'koral:doc',
1015 "key": 'pubDate',
1016 "match": 'match:eq',
1017 "value": '2014-12-05',
1018 "type": 'type:date'
1019 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001020 ]
1021 });
1022
1023 var nestedGroupFactory = buildFactory(vcClass, {
1024 "@type" : "koral:docGroup",
1025 "operation" : "operation:or",
1026 "operands" : [
Akron712733a2018-04-05 18:17:47 +02001027 {
1028 "@type": 'koral:doc',
1029 "key" : 'author',
1030 "match": 'match:eq',
1031 "value": 'Max Birkendale',
1032 "type": 'type:string'
1033 },
1034 {
1035 "@type" : "koral:docGroup",
1036 "operation" : "operation:and",
1037 "operands" : [
1038 {
1039 "@type": 'koral:doc',
1040 "key": 'pubDate',
1041 "match": 'match:geq',
1042 "value": '2014-05-12',
1043 "type": 'type:date'
1044 },
1045 {
1046 "@type": 'koral:doc',
1047 "key": 'pubDate',
1048 "match": 'match:leq',
1049 "value": '2014-12-05',
1050 "type": 'type:date'
1051 }
1052 ]
1053 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001054 ]
1055 });
1056
1057 var flatGroupFactory = buildFactory(vcClass, {
1058 "@type" : "koral:docGroup",
1059 "operation" : "operation:and",
1060 "operands" : [
Akron712733a2018-04-05 18:17:47 +02001061 {
1062 "@type": 'koral:doc',
1063 "key": 'pubDate',
1064 "match": 'match:geq',
1065 "value": '2014-05-12',
1066 "type": 'type:date'
1067 },
1068 {
1069 "@type": 'koral:doc',
1070 "key": 'pubDate',
1071 "match": 'match:leq',
1072 "value": '2014-12-05',
1073 "type": 'type:date'
1074 },
1075 {
1076 "@type": 'koral:doc',
1077 "key": 'foo',
1078 "match": 'match:eq',
1079 "value": 'bar',
1080 "type": 'type:string'
1081 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001082 ]
1083 });
1084
1085 it('should be initializable', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001086 var vc = vcClass.create();
Nils Diewald7c8ced22015-04-15 19:21:00 +00001087 expect(vc.element().getAttribute('class')).toEqual('vc');
1088 expect(vc.root().element().getAttribute('class')).toEqual('doc unspecified');
1089
1090 // Not removable
1091 expect(vc.root().element().lastChild.children.length).toEqual(0);
1092 });
1093
1094 it('should be based on a doc', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001095 var vc = vcClass.create().fromJson({
Akron712733a2018-04-05 18:17:47 +02001096 "@type" : "koral:doc",
1097 "key":"Titel",
1098 "value":"Baum",
1099 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +00001100 });
1101
1102 expect(vc.element().getAttribute('class')).toEqual('vc');
1103 expect(vc.root().element().getAttribute('class')).toEqual('doc');
1104 expect(vc.root().key()).toEqual('Titel');
1105 expect(vc.root().value()).toEqual('Baum');
1106 expect(vc.root().matchop()).toEqual('eq');
1107
1108 var docE = vc.root().element();
1109 expect(docE.children[0].firstChild.data).toEqual('Titel');
1110 expect(docE.children[1].firstChild.data).toEqual('eq');
1111 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
1112 expect(docE.children[2].firstChild.data).toEqual('Baum');
1113 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
1114 });
1115
1116 it('should be based on a docGroup', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001117 var vc = vcClass.create().fromJson(simpleGroupFactory.create().toJson());
Nils Diewald7c8ced22015-04-15 19:21:00 +00001118
1119 expect(vc.element().getAttribute('class')).toEqual('vc');
1120 expect(vc.root().element().getAttribute('class')).toEqual('docGroup');
1121 expect(vc.root().operation()).toEqual('and');
1122
1123 var docGroup = vc.root();
1124
1125 var first = docGroup.getOperand(0);
1126 expect(first.key()).toEqual('author');
1127 expect(first.value()).toEqual('Max Birkendale');
1128 expect(first.matchop()).toEqual('eq');
1129
1130 var second = docGroup.getOperand(1);
1131 expect(second.key()).toEqual('pubDate');
1132 expect(second.value()).toEqual('2014-12-05');
1133 expect(second.matchop()).toEqual('eq');
1134 });
1135
Akronb19803c2018-08-16 16:39:42 +02001136 it('should be based on a docGroupRef', function () {
1137 var vc = vcClass.create().fromJson({
1138 "@type" : "koral:docGroupRef",
1139 "ref":"myCorpus"
1140 });
1141
1142 // iv class="doc groupref"><span class="key fixed">@referTo</span><span data-type="string" class="value">myCorpus</span>
1143 var vcE = vc.element();
1144 expect(vcE.getAttribute('class')).toEqual('vc');
1145 expect(vcE.firstChild.tagName).toEqual('DIV');
1146 expect(vcE.firstChild.classList.contains('groupref')).toBeTruthy();
1147
1148 expect(vcE.firstChild.firstChild.tagName).toEqual('SPAN');
1149 expect(vcE.firstChild.firstChild.classList.contains('key')).toBeTruthy();
1150 expect(vcE.firstChild.firstChild.classList.contains('fixed')).toBeTruthy();
1151
1152 expect(vcE.firstChild.firstChild.textContent).toEqual("@referTo");
1153
1154 expect(vcE.firstChild.children[1].tagName).toEqual('SPAN');
1155 expect(vcE.firstChild.children[1].classList.contains('value')).toBeTruthy();
1156 expect(vcE.firstChild.children[1].getAttribute('data-type')).toEqual('string');
1157 expect(vcE.firstChild.children[1].textContent).toEqual("myCorpus");
1158 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00001159
1160 it('should be based on a nested docGroup', function () {
1161 var vc = nestedGroupFactory.create();
1162
1163 expect(vc.element().getAttribute('class')).toEqual('vc');
1164 expect(vc.element().firstChild.getAttribute('class')).toEqual('docGroup');
1165 expect(vc.element().firstChild.children[0].getAttribute('class')).toEqual('doc');
1166 var dg = vc.element().firstChild.children[1];
1167 expect(dg.getAttribute('class')).toEqual('docGroup');
1168 expect(dg.children[0].getAttribute('class')).toEqual('doc');
1169 expect(dg.children[1].getAttribute('class')).toEqual('doc');
Akron0b489ad2018-02-02 16:49:32 +01001170 expect(dg.children[2].getAttribute('class')).toEqual('operators button-group');
1171 expect(vc.element().firstChild.children[2].getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001172 });
1173
Akronb19803c2018-08-16 16:39:42 +02001174 it('should be based on a nested docGroupRef', function () {
1175 var vc = vcClass.create().fromJson({
1176 "@type" : "koral:docGroup",
1177 "operation" : "operation:and",
1178 "operands" : [{
1179 "@type" : "koral:docGroupRef",
1180 "ref":"myCorpus"
1181 },{
1182 "@type" : "koral:doc",
1183 "key":"Titel",
1184 "value":"Baum",
1185 "match":"match:eq"
1186 }]
1187 });
1188
1189 expect(vc._root.ldType()).toEqual("docGroup");
1190
1191 expect(vc.toQuery()).toEqual('referTo "myCorpus" & Titel = "Baum"');
1192 });
1193
1194
Nils Diewald7c8ced22015-04-15 19:21:00 +00001195 it('should be modifiable by deletion in flat docGroups', function () {
1196 var vc = flatGroupFactory.create();
1197 var docGroup = vc.root();
1198
1199 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
1200
1201 var doc = docGroup.getOperand(1);
1202 expect(doc.key()).toEqual("pubDate");
1203 expect(doc.value()).toEqual("2014-12-05");
1204
1205 // Remove operand 1
1206 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
1207 expect(doc._element).toEqual(undefined);
1208
1209 doc = docGroup.getOperand(1);
1210 expect(doc.key()).toEqual("foo");
1211
1212 // Remove operand 1
1213 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
1214 expect(doc._element).toEqual(undefined);
1215
1216 // Only one operand left ...
1217 expect(docGroup.getOperand(1)).toBeUndefined();
1218 // ... but there shouldn't be a group anymore at all!
1219 expect(docGroup.getOperand(0)).toBeUndefined();
1220
1221 var obj = vc.root();
1222 expect(obj.ldType()).toEqual("doc");
1223 expect(obj.key()).toEqual("pubDate");
1224 expect(obj.value()).toEqual("2014-05-12");
1225
1226 expect(obj.element().getAttribute('class')).toEqual('doc');
1227 });
1228
1229
1230 it('should be modifiable by deletion in nested docGroups (root case)', function () {
1231 var vc = nestedGroupFactory.create();
1232
1233 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001234 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05)'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001235 );
1236
1237 var docGroup = vc.root();
1238 expect(docGroup.ldType()).toEqual("docGroup");
1239 expect(docGroup.operation()).toEqual("or");
1240
1241 var doc = docGroup.getOperand(0);
1242 expect(doc.key()).toEqual("author");
1243 expect(doc.value()).toEqual("Max Birkendale");
1244
1245 docGroup = docGroup.getOperand(1);
1246 expect(docGroup.operation()).toEqual("and");
1247
1248 doc = docGroup.getOperand(0);
1249 expect(doc.key()).toEqual("pubDate");
1250 expect(doc.matchop()).toEqual("geq");
1251 expect(doc.value()).toEqual("2014-05-12");
1252 expect(doc.type()).toEqual("date");
1253
1254 doc = docGroup.getOperand(1);
1255 expect(doc.key()).toEqual("pubDate");
1256 expect(doc.matchop()).toEqual("leq");
1257 expect(doc.value()).toEqual("2014-12-05");
1258 expect(doc.type()).toEqual("date");
1259
1260 // Remove first operand so everything becomes root
1261 expect(
Akron712733a2018-04-05 18:17:47 +02001262 vc.root().delOperand(
1263 vc.root().getOperand(0)
1264 ).update().ldType()
Nils Diewald7c8ced22015-04-15 19:21:00 +00001265 ).toEqual("docGroup");
1266
1267 expect(vc.root().ldType()).toEqual("docGroup");
1268 expect(vc.root().operation()).toEqual("and");
1269 expect(vc.root().getOperand(0).ldType()).toEqual("doc");
1270
1271 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001272 'pubDate since 2014-05-12 & pubDate until 2014-12-05'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001273 );
1274 });
1275
1276 it('should be modifiable by deletion in nested docGroups (resolve group case)', function () {
1277 var vc = nestedGroupFactory.create();
1278
1279 // Get nested group
1280 var firstGroup = vc.root().getOperand(1);
1281 firstGroup.append(simpleGroupFactory.create({ "operation" : "operation:or" }));
1282
1283 // Structur is now:
1284 // or(doc, and(doc, doc, or(doc, doc)))
1285
1286 // Get nested or in and
1287 var orGroup = vc.root().getOperand(1).getOperand(2);
1288 expect(orGroup.ldType()).toEqual("docGroup");
1289 expect(orGroup.operation()).toEqual("or");
1290
1291 // Remove
1292 // Structur is now:
1293 // or(doc, and(doc, doc, doc)))
1294 expect(orGroup.delOperand(orGroup.getOperand(0)).update().operation()).toEqual("and");
1295 expect(vc.root().getOperand(1).operands().length).toEqual(3);
1296 });
1297
1298 it('should be modifiable by deletion in nested docGroups (identical group case)', function () {
1299 var vc = nestedGroupFactory.create();
1300
1301 // Get nested group
1302 var firstGroup = vc.root().getOperand(1);
1303 firstGroup.append(simpleGroupFactory.create({
Akron712733a2018-04-05 18:17:47 +02001304 "operation" : "operation:or"
Nils Diewald7c8ced22015-04-15 19:21:00 +00001305 }));
1306 var oldAuthor = firstGroup.getOperand(2).getOperand(0);
1307 oldAuthor.key("title");
1308 oldAuthor.value("Der Birnbaum");
1309
1310 // Structur is now:
1311 // or(doc, and(doc, doc, or(doc, doc)))
1312 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001313 'author = "Max Birkendale" | ' +
1314 '(pubDate since 2014-05-12 & ' +
1315 'pubDate until 2014-12-05 & ' +
1316 '(title = "Der Birnbaum" | ' +
1317 'pubDate in 2014-12-05))'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001318 );
1319
1320 var andGroup = vc.root().getOperand(1);
1321
1322 // Get leading docs in and
1323 var doc1 = andGroup.getOperand(0);
1324 expect(doc1.ldType()).toEqual("doc");
1325 expect(doc1.value()).toEqual("2014-05-12");
1326 var doc2 = andGroup.getOperand(1);
1327 expect(doc2.ldType()).toEqual("doc");
1328 expect(doc2.value()).toEqual("2014-12-05");
1329
1330 // Remove 2
1331 expect(
Akron712733a2018-04-05 18:17:47 +02001332 andGroup.delOperand(doc2).update().operation()
Nils Diewald7c8ced22015-04-15 19:21:00 +00001333 ).toEqual("and");
1334 // Structur is now:
1335 // or(doc, and(doc, or(doc, doc)))
1336
1337 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001338 'author = "Max Birkendale"' +
1339 ' | (pubDate since 2014-05-12 & ' +
1340 '(title = "Der Birnbaum" | pubDate in 2014-12-05))'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001341 );
1342
1343
1344 // Remove 1
1345 expect(andGroup.delOperand(doc1).update().operation()).toEqual("or");
1346 // Structur is now:
1347 // or(doc, doc, doc)
1348
1349 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001350 'author = "Max Birkendale" | title = "Der Birnbaum" | pubDate in 2014-12-05'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001351 );
1352 });
1353
1354 it('should be reducible to unspecification', function () {
1355 var vc = demoFactory.create();
1356
1357 expect(vc.toQuery()).toEqual(vc.root().toQuery());
1358 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001359 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & ' +
1360 '(Titel = "Baum" | Veröffentlichungsort = "hihi")) ' +
1361 '| Untertitel ~ "huhu"');
Akrond141a362018-07-10 18:12:13 +02001362 expect(vc.root().element().lastChild.children[0].innerText).toEqual('and');
1363 expect(vc.root().element().lastChild.children[1].innerText).toEqual('×');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001364 expect(vc.root().delOperand(vc.root().getOperand(0)).update()).not.toBeUndefined();
Akron712733a2018-04-05 18:17:47 +02001365 expect(vc.toQuery()).toEqual('Untertitel ~ "huhu"');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001366
1367 var lc = vc.root().element().lastChild;
Akrond141a362018-07-10 18:12:13 +02001368 expect(lc.children[0].innerText).toEqual('and');
1369 expect(lc.children[1].innerText).toEqual('or');
1370 expect(lc.children[2].innerText).toEqual('×');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001371
1372 // Clean everything
1373 vc.clean();
1374 expect(vc.toQuery()).toEqual('');
1375 });
1376
1377 it('should flatten on import', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001378 var vc = vcClass.create().fromJson({
Akron712733a2018-04-05 18:17:47 +02001379 "@type":"koral:docGroup",
1380 "operation":"operation:or",
1381 "operands":[
1382 {
1383 "@type":"koral:docGroup",
1384 "operation":"operation:or",
1385 "operands":[
Nils Diewald7c8ced22015-04-15 19:21:00 +00001386 {
Akron712733a2018-04-05 18:17:47 +02001387 "@type":"koral:doc",
1388 "key":"Titel",
1389 "value":"Baum",
1390 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +00001391 },
1392 {
Akron712733a2018-04-05 18:17:47 +02001393 "@type":"koral:doc",
1394 "key":"Veröffentlichungsort",
1395 "value":"hihi",
1396 "match":"match:eq"
Nils Diewald7c8ced22015-04-15 19:21:00 +00001397 },
1398 {
Akron712733a2018-04-05 18:17:47 +02001399 "@type":"koral:docGroup",
1400 "operation":"operation:or",
1401 "operands":[
1402 {
1403 "@type":"koral:doc",
1404 "key":"Titel",
1405 "value":"Baum",
1406 "match":"match:eq"
1407 },
1408 {
1409 "@type":"koral:doc",
1410 "key":"Veröffentlichungsort",
1411 "value":"hihi",
1412 "match":"match:eq"
1413 }
1414 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +00001415 }
Akron712733a2018-04-05 18:17:47 +02001416 ]
1417 },
1418 {
1419 "@type":"koral:doc",
1420 "key":"Untertitel",
1421 "value":"huhu",
1422 "match":"match:contains"
1423 }
1424 ]
Nils Diewald7c8ced22015-04-15 19:21:00 +00001425 });
Nils Diewaldfda29d92015-01-22 17:28:01 +00001426
Nils Diewald7c8ced22015-04-15 19:21:00 +00001427 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001428 'Titel = "Baum" | Veröffentlichungsort = "hihi" | Untertitel ~ "huhu"'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001429 );
Nils Diewald86dad5b2015-01-28 15:09:07 +00001430 });
Akron1bdf5272018-07-24 18:51:30 +02001431
1432 it('should be deserializable from collection 1', function () {
1433 var kq = {
1434 "matches":["..."],
1435 "collection":{
1436 "@type": "koral:docGroup",
1437 "operation": "operation:or",
1438 "operands": [{
1439 "@type": "koral:docGroup",
1440 "operation": "operation:and",
1441 "operands": [
1442 {
1443 "@type": "koral:doc",
1444 "key": "title",
1445 "match": "match:eq",
1446 "value": "Der Birnbaum",
1447 "type": "type:string"
1448 },
1449 {
1450 "@type": "koral:doc",
1451 "key": "pubPlace",
1452 "match": "match:eq",
1453 "value": "Mannheim",
1454 "type": "type:string"
1455 },
1456 {
1457 "@type": "koral:docGroup",
1458 "operation": "operation:or",
1459 "operands": [
1460 {
1461 "@type": "koral:doc",
1462 "key": "subTitle",
1463 "match": "match:eq",
1464 "value": "Aufzucht und Pflege",
1465 "type": "type:string"
1466 },
1467 {
1468 "@type": "koral:doc",
1469 "key": "subTitle",
1470 "match": "match:eq",
1471 "value": "Gedichte",
1472 "type": "type:string"
1473 }
1474 ]
1475 }
1476 ]
1477 },{
1478 "@type": "koral:doc",
1479 "key": "pubDate",
1480 "match": "match:geq",
1481 "value": "2015-03-05",
1482 "type": "type:date",
1483 "rewrites" : [{
1484 "@type" : "koral:rewrite",
1485 "operation" : "operation:modification",
1486 "src" : "querySerializer",
1487 "scope" : "tree"
1488 }]
1489 }]
1490 }
1491 };
1492
1493 var vc = vcClass.create().fromJson(kq["collection"]);
1494 expect(vc.toQuery()).toEqual('(title = "Der Birnbaum" & pubPlace = "Mannheim" & (subTitle = "Aufzucht und Pflege" | subTitle = "Gedichte")) | pubDate since 2015-03-05');
1495 });
1496
1497 it('should be deserializable from collection 2', function () {
1498 var kq = {
1499 "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
1500 "meta": {},
1501 "query": {
1502 "@type": "koral:token",
1503 "wrap": {
1504 "@type": "koral:term",
1505 "match": "match:eq",
1506 "layer": "orth",
1507 "key": "Baum",
1508 "foundry": "opennlp",
1509 "rewrites": [
1510 {
1511 "@type": "koral:rewrite",
1512 "src": "Kustvakt",
1513 "operation": "operation:injection",
1514 "scope": "foundry"
1515 }
1516 ]
1517 },
1518 "idn": "Baum_2227",
1519 "rewrites": [
1520 {
1521 "@type": "koral:rewrite",
1522 "src": "Kustvakt",
1523 "operation": "operation:injection",
1524 "scope": "idn"
1525 }
1526 ]
1527 },
1528 "collection": {
1529 "@type": "koral:docGroup",
1530 "operation": "operation:and",
1531 "operands": [
1532 {
1533 "@type": "koral:doc",
1534 "match": "match:eq",
1535 "type": "type:regex",
1536 "value": "CC-BY.*",
1537 "key": "availability"
1538 },
1539 {
1540 "@type": "koral:doc",
1541 "match": "match:eq",
1542 "type":"type:text",
1543 "value": "Goethe",
1544 "key": "author"
1545 }
1546 ],
1547 "rewrites": [
1548 {
1549 "@type": "koral:rewrite",
1550 "src": "Kustvakt",
1551 "operation": "operation:insertion",
1552 "scope": "availability(FREE)"
1553 }
1554 ]
1555 },
1556 "matches": []
1557 };
1558
1559 var vc = vcClass.create().fromJson(kq["collection"]);
1560 expect(vc.toQuery()).toEqual('availability = /CC-BY.*/ & author = "Goethe"');
1561 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00001562 });
1563
Akron1bdf5272018-07-24 18:51:30 +02001564 it('shouldn\'t be deserializable from collection with unknown type', function () {
1565 var kq = {
1566 "@type" : "koral:doc",
1567 "match": "match:eq",
1568 "type":"type:failure",
1569 "value": "Goethe",
1570 "key": "author"
1571 };
1572
1573 expect(function () {
1574 vcClass.create().fromJson(kq)
1575 }).toThrow(new Error("Unknown value type: string"));
Akron1bdf5272018-07-24 18:51:30 +02001576 });
1577
1578
Nils Diewald7c8ced22015-04-15 19:21:00 +00001579 describe('KorAP.Operators', function () {
1580 it('should be initializable', function () {
1581 var op = operatorsClass.create(true, false, false);
1582 expect(op.and()).toBeTruthy();
1583 expect(op.or()).not.toBeTruthy();
1584 expect(op.del()).not.toBeTruthy();
1585
1586 op.and(false);
1587 expect(op.and()).not.toBeTruthy();
1588 expect(op.or()).not.toBeTruthy();
1589 expect(op.del()).not.toBeTruthy();
1590
1591 op.or(true);
1592 op.del(true);
1593 expect(op.and()).not.toBeTruthy();
1594 expect(op.or()).toBeTruthy();
1595 expect(op.del()).toBeTruthy();
1596
Akrond141a362018-07-10 18:12:13 +02001597 var e = op.update();
Akron0b489ad2018-02-02 16:49:32 +01001598 expect(e.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001599 expect(e.children[0].getAttribute('class')).toEqual('or');
Akrond141a362018-07-10 18:12:13 +02001600 expect(e.children[0].innerText).toEqual('or');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001601 expect(e.children[1].getAttribute('class')).toEqual('delete');
Akrond141a362018-07-10 18:12:13 +02001602 expect(e.children[1].innerText).toEqual('×');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001603
1604 op.and(true);
1605 op.del(false);
Akrond141a362018-07-10 18:12:13 +02001606 e = op.update();
Nils Diewald7c8ced22015-04-15 19:21:00 +00001607
Akron0b489ad2018-02-02 16:49:32 +01001608 expect(e.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001609 expect(e.children[0].getAttribute('class')).toEqual('and');
Akrond141a362018-07-10 18:12:13 +02001610 expect(e.children[0].innerText).toEqual('and');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001611 expect(e.children[1].getAttribute('class')).toEqual('or');
Akrond141a362018-07-10 18:12:13 +02001612 expect(e.children[1].innerText).toEqual('or');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001613 });
1614 });
1615
1616 describe('KorAP._delete (event)', function () {
1617 var complexVCFactory = buildFactory(vcClass,{
1618 "@type": 'koral:docGroup',
1619 'operation' : 'operation:and',
1620 'operands' : [
Akron712733a2018-04-05 18:17:47 +02001621 {
1622 "@type": 'koral:doc',
1623 "key": 'pubDate',
1624 "match": 'match:eq',
1625 "value": '2014-12-05',
1626 "type": 'type:date'
1627 },
1628 {
1629 "@type" : 'koral:docGroup',
1630 'operation' : 'operation:or',
1631 'operands' : [
1632 {
1633 '@type' : 'koral:doc',
1634 'key' : 'title',
1635 'value' : 'Hello World!'
1636 },
1637 {
1638 '@type' : 'koral:doc',
1639 'key' : 'foo',
1640 'value' : 'bar'
1641 }
1642 ]
1643 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001644 ]
1645 });
1646
1647 it('should clean on root docs', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001648 var vc = vcClass.create().fromJson({
Akron712733a2018-04-05 18:17:47 +02001649 "@type": 'koral:doc',
1650 "key": 'pubDate',
1651 "match": 'match:eq',
1652 "value": '2014-12-05',
1653 "type": 'type:date'
Nils Diewald86dad5b2015-01-28 15:09:07 +00001654 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00001655 expect(vc.root().toQuery()).toEqual('pubDate in 2014-12-05');
Akron0b489ad2018-02-02 16:49:32 +01001656 expect(vc.root().element().lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald86dad5b2015-01-28 15:09:07 +00001657
Nils Diewald7c8ced22015-04-15 19:21:00 +00001658 // Clean with delete from root
1659 expect(vc.root().element().lastChild.lastChild.getAttribute('class')).toEqual('delete');
1660 _delOn(vc.root());
1661 expect(vc.root().toQuery()).toEqual('');
1662 expect(vc.root().element().lastChild.lastChild.data).toEqual('⋯');
1663 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00001664
Nils Diewald7c8ced22015-04-15 19:21:00 +00001665 it('should remove on nested docs', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001666 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02001667 {
1668 "@type": 'koral:docGroup',
1669 'operation' : 'operation:and',
1670 'operands' : [
1671 {
1672 "@type": 'koral:doc',
1673 "key": 'pubDate',
1674 "match": 'match:eq',
1675 "value": '2014-12-05',
1676 "type": 'type:date'
1677 },
1678 {
1679 "@type" : 'koral:doc',
1680 'key' : 'foo',
1681 'value' : 'bar'
1682 }
1683 ]
1684 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001685 );
1686
1687 // Delete with direct element access
1688 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1689 _delOn(vc.root().getOperand(0));
1690
1691 expect(vc.toQuery()).toEqual('foo = "bar"');
1692 expect(vc.root().ldType()).toEqual('doc');
1693 });
1694
1695 it('should clean on doc groups', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001696 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02001697 {
1698 "@type": 'koral:docGroup',
1699 'operation' : 'operation:and',
1700 'operands' : [
1701 {
1702 "@type": 'koral:doc',
1703 "key": 'pubDate',
1704 "match": 'match:eq',
1705 "value": '2014-12-05',
1706 "type": 'type:date'
1707 },
1708 {
1709 "@type" : 'koral:doc',
1710 'key' : 'foo',
1711 'value' : 'bar'
1712 }
1713 ]
1714 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001715 );
1716
1717 // Cleanwith direct element access
1718 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1719 _delOn(vc.root());
1720 expect(vc.toQuery()).toEqual('');
1721 expect(vc.root().ldType()).toEqual('non');
1722 });
1723
1724 it('should remove on nested doc groups (case of ungrouping 1)', function () {
1725 var vc = complexVCFactory.create();
1726
1727 // Delete with direct element access
1728 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001729 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001730 );
1731
1732 // Remove hello world:
1733 _delOn(vc.root().getOperand(1).getOperand(0));
1734 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1735 expect(vc.root().ldType()).toEqual('docGroup');
1736 });
1737
1738 it('should remove on nested doc groups (case of ungrouping 2)', function () {
1739 var vc = complexVCFactory.create();
1740
1741 // Delete with direct element access
1742 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001743 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001744 );
1745
1746 // Remove bar
1747 _delOn(vc.root().getOperand(1).getOperand(1));
1748 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & title = "Hello World!"');
1749 expect(vc.root().ldType()).toEqual('docGroup');
1750 expect(vc.root().operation()).toEqual('and');
1751 });
1752
1753 it('should remove on nested doc groups (case of root changing)', function () {
1754 var vc = complexVCFactory.create();
1755
1756 // Delete with direct element access
1757 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001758 'pubDate in 2014-12-05 & ' +
1759 '(title = "Hello World!" | foo = "bar")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001760 );
1761
1762 // Remove bar
1763 _delOn(vc.root().getOperand(0));
1764 expect(vc.toQuery()).toEqual('title = "Hello World!" | foo = "bar"');
1765 expect(vc.root().ldType()).toEqual('docGroup');
1766 expect(vc.root().operation()).toEqual('or');
1767 });
1768
1769 it('should remove on nested doc groups (list flattening)', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001770 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02001771 {
1772 "@type": 'koral:docGroup',
1773 'operation' : 'operation:or',
1774 'operands' : [
1775 {
1776 "@type": 'koral:doc',
1777 "key": 'pubDate',
1778 "match": 'match:eq',
1779 "value": '2014-12-05',
1780 "type": 'type:date'
1781 },
1782 {
1783 "@type" : 'koral:doc',
1784 'key' : 'foo',
1785 'value' : 'bar'
1786 },
1787 {
1788 "@type": 'koral:docGroup',
1789 'operation' : 'operation:and',
1790 'operands' : [
1791 {
1792 "@type": 'koral:doc',
1793 "key": 'pubDate',
1794 "match": 'match:eq',
1795 "value": '2014-12-05',
1796 "type": 'type:date'
1797 },
1798 {
1799 "@type" : 'koral:docGroup',
1800 'operation' : 'operation:or',
1801 'operands' : [
1802 {
1803 '@type' : 'koral:doc',
1804 'key' : 'title',
1805 'value' : 'Hello World!'
1806 },
1807 {
1808 '@type' : 'koral:doc',
1809 'key' : 'yeah',
1810 'value' : 'juhu'
1811 }
1812 ]
1813 }
1814 ]
1815 }
1816 ]
1817 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001818 );
1819
1820 // Delete with direct element access
1821 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001822 'pubDate in 2014-12-05 | foo = "bar" | ' +
1823 '(pubDate in 2014-12-05 & ' +
1824 '(title = "Hello World!" | yeah = "juhu"))'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001825 );
1826
1827 expect(vc.root().ldType()).toEqual('docGroup');
1828 expect(vc.root().operation()).toEqual('or');
1829
1830 // Operands and operators
1831 expect(vc.element().firstChild.children.length).toEqual(4);
Akron0b489ad2018-02-02 16:49:32 +01001832 expect(vc.element().firstChild.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001833
1834 // Remove inner group and flatten
1835 _delOn(vc.root().getOperand(2).getOperand(0));
1836
1837 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02001838 'pubDate in 2014-12-05 | foo = "bar" | title = "Hello World!" | yeah = "juhu"'
Nils Diewald7c8ced22015-04-15 19:21:00 +00001839 );
1840 expect(vc.root().ldType()).toEqual('docGroup');
1841 expect(vc.root().operation()).toEqual('or');
1842
1843 // Operands and operators
1844 expect(vc.element().firstChild.children.length).toEqual(5);
Akron0b489ad2018-02-02 16:49:32 +01001845 expect(vc.element().firstChild.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001846 });
1847 });
1848
1849 describe('KorAP._add (event)', function () {
1850 var complexVCFactory = buildFactory(vcClass,{
1851 "@type": 'koral:docGroup',
1852 'operation' : 'operation:and',
1853 'operands' : [
Akron712733a2018-04-05 18:17:47 +02001854 {
1855 "@type": 'koral:doc',
1856 "key": 'pubDate',
1857 "match": 'match:eq',
1858 "value": '2014-12-05',
1859 "type": 'type:date'
1860 },
1861 {
1862 "@type" : 'koral:docGroup',
1863 'operation' : 'operation:or',
1864 'operands' : [
1865 {
1866 '@type' : 'koral:doc',
1867 'key' : 'title',
1868 'value' : 'Hello World!'
1869 },
1870 {
1871 '@type' : 'koral:doc',
1872 'key' : 'foo',
1873 'value' : 'bar'
1874 }
1875 ]
1876 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001877 ]
1878 });
1879
1880 it('should add new unspecified doc with "and"', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001881 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02001882 {
1883 "@type": 'koral:docGroup',
1884 'operation' : 'operation:and',
1885 'operands' : [
1886 {
1887 "@type": 'koral:doc',
1888 "key": 'pubDate',
1889 "match": 'match:eq',
1890 "value": '2014-12-05',
1891 "type": 'type:date'
1892 },
1893 {
1894 "@type" : 'koral:doc',
1895 'key' : 'foo',
1896 'value' : 'bar'
Akronb19803c2018-08-16 16:39:42 +02001897 },
1898 {
1899 "@type" : "koral:docGroupRef",
1900 "ref" : "myCorpus"
Akron712733a2018-04-05 18:17:47 +02001901 }
1902 ]
1903 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001904 );
1905
Akronb19803c2018-08-16 16:39:42 +02001906 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar" & referTo "myCorpus"');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001907
1908 var fc = vc.element().firstChild;
1909 expect(fc.getAttribute('data-operation')).toEqual('and');
Akronb19803c2018-08-16 16:39:42 +02001910 expect(fc.children.length).toEqual(4);
Akron0b489ad2018-02-02 16:49:32 +01001911 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001912 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1913 expect(fc.children[1].getAttribute('class')).toEqual('doc');
Akronb19803c2018-08-16 16:39:42 +02001914 expect(fc.children[2].getAttribute('class')).toEqual('doc groupref');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001915
1916 // add with 'and' in the middle
1917 _andOn(vc.root().getOperand(0));
Akronb19803c2018-08-16 16:39:42 +02001918 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar" & referTo "myCorpus"');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001919
1920 fc = vc.element().firstChild;
1921 expect(fc.getAttribute('data-operation')).toEqual('and');
Akronb19803c2018-08-16 16:39:42 +02001922 expect(fc.children.length).toEqual(5);
Akron0b489ad2018-02-02 16:49:32 +01001923 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001924
1925 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1926 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
1927 expect(fc.children[2].getAttribute('class')).toEqual('doc');
Akronb19803c2018-08-16 16:39:42 +02001928 expect(fc.children[3].getAttribute('class')).toEqual('doc groupref');
1929 expect(fc.children[4].classList.contains('button-group')).toBeTruthy();
1930 expect(fc.children.length).toEqual(5);
1931
1932 _andOn(vc.root().getOperand(3));
1933 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1934 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
1935 expect(fc.children[2].getAttribute('class')).toEqual('doc');
1936 expect(fc.children[3].getAttribute('class')).toEqual('doc groupref');
1937 expect(fc.children[4].getAttribute('class')).toEqual('doc unspecified');
1938 expect(fc.children[5].classList.contains('button-group')).toBeTruthy();
1939 expect(fc.children.length).toEqual(6);
1940
Nils Diewald7c8ced22015-04-15 19:21:00 +00001941 });
1942
Akronb19803c2018-08-16 16:39:42 +02001943
Nils Diewald7c8ced22015-04-15 19:21:00 +00001944 it('should add new unspecified doc with "or"', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00001945 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02001946 {
1947 "@type": 'koral:docGroup',
1948 'operation' : 'operation:and',
1949 'operands' : [
1950 {
1951 "@type": 'koral:doc',
1952 "key": 'pubDate',
1953 "match": 'match:eq',
1954 "value": '2014-12-05',
1955 "type": 'type:date'
1956 },
1957 {
1958 "@type" : 'koral:doc',
1959 'key' : 'foo',
1960 'value' : 'bar'
Akronb19803c2018-08-16 16:39:42 +02001961 },
1962 {
1963 "@type" : "koral:docGroupRef",
1964 "ref" : "myCorpus"
Akron712733a2018-04-05 18:17:47 +02001965 }
1966 ]
1967 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00001968 );
1969
Akronb19803c2018-08-16 16:39:42 +02001970 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar" & referTo "myCorpus"');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001971
1972 var fc = vc.element().firstChild;
Akronb19803c2018-08-16 16:39:42 +02001973 expect(fc.children.length).toEqual(4);
Akron0b489ad2018-02-02 16:49:32 +01001974 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001975 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1976 expect(fc.children[1].getAttribute('class')).toEqual('doc');
Akronb19803c2018-08-16 16:39:42 +02001977 expect(fc.children[2].getAttribute('class')).toEqual('doc groupref');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001978
1979 // add with 'or' in the middle
1980 _orOn(vc.root().getOperand(0));
Akronb19803c2018-08-16 16:39:42 +02001981 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar" & referTo "myCorpus"');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001982 fc = vc.element().firstChild;
1983
1984 expect(fc.getAttribute('data-operation')).toEqual('and');
Akronb19803c2018-08-16 16:39:42 +02001985 expect(fc.children.length).toEqual(4);
Nils Diewald7c8ced22015-04-15 19:21:00 +00001986 expect(fc.children[0].getAttribute('class')).toEqual('docGroup');
1987 expect(fc.children[0].getAttribute('data-operation')).toEqual('or');
1988 expect(fc.children[1].getAttribute('class')).toEqual('doc');
Akronb19803c2018-08-16 16:39:42 +02001989 expect(fc.children[2].getAttribute('class')).toEqual('doc groupref');
1990 expect(fc.children[3].getAttribute('class')).toEqual('operators button-group');
Akron0b489ad2018-02-02 16:49:32 +01001991 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Nils Diewald7c8ced22015-04-15 19:21:00 +00001992
1993 fc = vc.element().firstChild.firstChild;
1994 expect(fc.children.length).toEqual(3);
1995 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1996 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
Akron0b489ad2018-02-02 16:49:32 +01001997 expect(fc.children[2].getAttribute('class')).toEqual('operators button-group');
1998 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
Akronb19803c2018-08-16 16:39:42 +02001999
2000 _orOn(vc.root().getOperand(2));
2001 fc = vc.element().firstChild;
2002 expect(fc.children.length).toEqual(4);
2003
2004 expect(fc.children[0].getAttribute('class')).toEqual('docGroup');
2005 expect(fc.children[1].getAttribute('class')).toEqual('doc');
2006 expect(fc.children[2].getAttribute('class')).toEqual('docGroup');
2007 expect(fc.children[3].getAttribute('class')).toEqual('operators button-group');
2008
2009 fc = vc.element().firstChild.children[2];
2010 expect(fc.children[0].getAttribute('class')).toEqual('doc groupref');
2011 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
2012 expect(fc.children[2].getAttribute('class')).toEqual('operators button-group');
2013 expect(fc.lastChild.getAttribute('class')).toEqual('operators button-group');
2014
Nils Diewald7c8ced22015-04-15 19:21:00 +00002015 });
2016
2017 it('should add new unspecified doc with "and" before group', function () {
2018 var vc = demoFactory.create();
2019
2020 // Wrap with direct element access
2021 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002022 '(Titel = "Baum" & ' +
2023 'Veröffentlichungsort = "hihi" & ' +
2024 '(Titel = "Baum" | ' +
2025 'Veröffentlichungsort = "hihi")) | ' +
2026 'Untertitel ~ "huhu"'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002027 );
2028
2029 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
2030 expect(vc.root().getOperand(0).operation()).toEqual('and');
2031 expect(vc.root().getOperand(0).operands().length).toEqual(3);
2032
2033 // Add unspecified on the second doc
2034 var secDoc = vc.root().getOperand(0).getOperand(1);
2035 expect(secDoc.value()).toEqual('hihi');
2036
2037 // Add
2038 _andOn(secDoc);
2039
2040 var fo = vc.root().getOperand(0);
2041
2042 expect(fo.ldType()).toEqual('docGroup');
2043 expect(fo.operation()).toEqual('and');
2044 expect(fo.operands().length).toEqual(4);
2045
2046 expect(fo.getOperand(0).ldType()).toEqual('doc');
2047 expect(fo.getOperand(1).ldType()).toEqual('doc');
2048 expect(fo.getOperand(2).ldType()).toEqual('non');
2049 expect(fo.getOperand(3).ldType()).toEqual('docGroup');
2050 });
2051
2052
2053 it('should remove a doc with an unspecified doc in a nested group', function () {
2054 var vc = demoFactory.create();
2055
2056 // Wrap with direct element access
2057 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002058 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & (Titel = "Baum" | Veröffentlichungsort = "hihi")) | Untertitel ~ "huhu"'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002059 );
2060
2061 var fo = vc.root().getOperand(0).getOperand(0);
2062 expect(fo.key()).toEqual('Titel');
2063 expect(fo.value()).toEqual('Baum');
2064
2065 // Add unspecified on the root group
2066 _orOn(fo);
2067
2068 fo = vc.root().getOperand(0).getOperand(0);
2069
2070 expect(fo.operation()).toEqual('or');
2071 expect(fo.getOperand(0).ldType()).toEqual('doc');
2072 expect(fo.getOperand(1).ldType()).toEqual('non');
2073
2074 // Delete document
2075 _delOn(fo.getOperand(0));
2076
2077 // The operand is now non
2078 expect(vc.root().getOperand(0).getOperand(0).ldType()).toEqual('non');
2079 expect(vc.root().getOperand(0).getOperand(1).ldType()).toEqual('doc');
2080 expect(vc.root().getOperand(0).getOperand(2).ldType()).toEqual('docGroup');
2081 });
2082
2083
Akron712733a2018-04-05 18:17:47 +02002084 it('should remove an unspecified doc with a doc in a nested group', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +00002085 var vc = demoFactory.create();
2086
2087 // Wrap with direct element access
2088 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002089 '(Titel = "Baum" & ' +
2090 'Veröffentlichungsort = "hihi" & ' +
2091 '(Titel = "Baum" ' +
2092 '| Veröffentlichungsort = "hihi")) | ' +
2093 'Untertitel ~ "huhu"'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002094 );
2095
2096 var fo = vc.root().getOperand(0).getOperand(0);
2097 expect(fo.key()).toEqual('Titel');
2098 expect(fo.value()).toEqual('Baum');
2099
2100 // Add unspecified on the root group
2101 _orOn(fo);
2102
2103 fo = vc.root().getOperand(0).getOperand(0);
2104
2105 expect(fo.operation()).toEqual('or');
2106 expect(fo.getOperand(0).ldType()).toEqual('doc');
2107 expect(fo.getOperand(1).ldType()).toEqual('non');
2108
2109 // Delete unspecified doc
2110 _delOn(fo.getOperand(1));
2111
2112 // The operand is now non
2113 fo = vc.root().getOperand(0);
2114 expect(fo.getOperand(0).ldType()).toEqual('doc');
2115 expect(fo.getOperand(0).key()).toEqual('Titel');
2116 expect(fo.getOperand(0).value()).toEqual('Baum');
2117 expect(fo.getOperand(1).ldType()).toEqual('doc');
2118 expect(fo.getOperand(2).ldType()).toEqual('docGroup');
2119 });
2120
2121
2122 it('should add on parent group (case "and")', function () {
2123 var vc = complexVCFactory.create();
2124
2125 // Wrap with direct element access
2126 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002127 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002128 );
2129
2130 expect(vc.root().operands().length).toEqual(2);
2131
2132 // Add unspecified on the root group
2133 _andOn(vc.root().getOperand(1));
2134 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002135 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002136 );
2137
2138 expect(vc.root().ldType()).toEqual('docGroup');
2139 expect(vc.root().operands().length).toEqual(3);
2140 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
2141 expect(vc.root().getOperand(1).ldType()).toEqual('docGroup');
2142 expect(vc.root().getOperand(1).operation()).toEqual('or');
2143 expect(vc.root().getOperand(2).ldType()).toEqual('non');
2144
2145 // Add another unspecified on the root group
2146 _andOn(vc.root().getOperand(1));
2147
2148 expect(vc.root().operands().length).toEqual(4);
2149 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
2150 expect(vc.root().getOperand(1).ldType()).toEqual('docGroup');
2151 expect(vc.root().getOperand(2).ldType()).toEqual('non');
2152 expect(vc.root().getOperand(3).ldType()).toEqual('non');
2153
2154 // Add another unspecified after the first doc
2155 _andOn(vc.root().getOperand(0));
2156
2157 expect(vc.root().operands().length).toEqual(5);
2158 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
2159 expect(vc.root().getOperand(1).ldType()).toEqual('non');
2160 expect(vc.root().getOperand(2).ldType()).toEqual('docGroup');
2161 expect(vc.root().getOperand(3).ldType()).toEqual('non');
2162 expect(vc.root().getOperand(4).ldType()).toEqual('non');
2163 });
2164
2165 it('should wrap on root', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00002166 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02002167 {
2168 "@type": 'koral:docGroup',
2169 'operation' : 'operation:and',
2170 'operands' : [
2171 {
2172 "@type": 'koral:doc',
2173 "key": 'pubDate',
2174 "match": 'match:eq',
2175 "value": '2014-12-05',
2176 "type": 'type:date'
2177 },
2178 {
2179 "@type" : 'koral:doc',
2180 'key' : 'foo',
2181 'value' : 'bar'
2182 }
2183 ]
2184 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002185 );
2186
2187 // Wrap on root
2188 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
2189 expect(vc.root().ldType()).toEqual('docGroup');
2190 expect(vc.root().operation()).toEqual('and');
2191 _orOn(vc.root());
2192 expect(vc.root().ldType()).toEqual('docGroup');
2193 expect(vc.root().operation()).toEqual('or');
2194
2195 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
2196 expect(vc.root().getOperand(0).operation()).toEqual('and');
2197 });
2198
2199 it('should add on root (case "and")', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00002200 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02002201 {
2202 "@type": 'koral:doc',
2203 "key": 'pubDate',
2204 "match": 'match:eq',
2205 "value": '2014-12-05',
2206 "type": 'type:date'
2207 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002208 );
2209
2210 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
2211 expect(vc.root().ldType()).toEqual('doc');
2212 expect(vc.root().key()).toEqual('pubDate');
2213 expect(vc.root().value()).toEqual('2014-12-05');
2214
2215 // Wrap on root
2216 _andOn(vc.root());
2217 expect(vc.root().ldType()).toEqual('docGroup');
2218 expect(vc.root().operation()).toEqual('and');
2219 });
2220
2221 it('should add on root (case "or")', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00002222 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02002223 {
2224 "@type": 'koral:doc',
2225 "key": 'pubDate',
2226 "match": 'match:eq',
2227 "value": '2014-12-05',
2228 "type": 'type:date'
2229 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002230 );
2231
2232 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
2233 expect(vc.root().key()).toEqual('pubDate');
2234 expect(vc.root().value()).toEqual('2014-12-05');
2235
2236 // Wrap on root
2237 _orOn(vc.root());
2238 expect(vc.root().ldType()).toEqual('docGroup');
2239 expect(vc.root().operation()).toEqual('or');
2240 });
2241
2242 it('should support multiple sub groups per group', function () {
Nils Diewald6283d692015-04-23 20:32:53 +00002243 var vc = vcClass.create().fromJson(
Akron712733a2018-04-05 18:17:47 +02002244 {
2245 "@type": 'koral:docGroup',
2246 'operation' : 'operation:or',
2247 'operands' : [
2248 {
2249 "@type": 'koral:docGroup',
2250 'operation' : 'operation:and',
2251 'operands' : [
2252 {
2253 "@type": 'koral:doc',
2254 "key": 'title',
2255 "value": 't1',
2256 },
2257 {
2258 "@type" : 'koral:doc',
2259 'key' : 'title',
2260 'value' : 't2'
2261 }
2262 ]
2263 },
2264 {
2265 "@type": 'koral:docGroup',
2266 'operation' : 'operation:and',
2267 'operands' : [
2268 {
2269 "@type": 'koral:doc',
2270 "key": 'title',
2271 "value": 't3',
2272 },
2273 {
2274 "@type" : 'koral:doc',
2275 'key' : 'title',
2276 'value' : 't4'
2277 }
2278 ]
2279 }
2280 ]
2281 }
Nils Diewald7c8ced22015-04-15 19:21:00 +00002282 );
2283 expect(vc.toQuery()).toEqual(
Akron712733a2018-04-05 18:17:47 +02002284 '(title = "t1" & title = "t2") | ' +
2285 '(title = "t3" & title = "t4")'
Nils Diewald7c8ced22015-04-15 19:21:00 +00002286 );
2287 expect(vc.root().operation()).toEqual('or');
2288 expect(vc.root().getOperand(0).toQuery()).toEqual('title = "t1" & title = "t2"');
2289 expect(vc.root().getOperand(1).toQuery()).toEqual('title = "t3" & title = "t4"');
2290
2291 _andOn(vc.root());
2292
2293 expect(vc.root().operation()).toEqual('and');
2294 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
2295 expect(vc.root().getOperand(1).ldType()).toEqual('non');
2296 });
2297 });
2298
Akron5c829e92017-05-12 18:10:00 +02002299 // Check class method
2300 describe('KorAP.VC.checkRewrite', function () {
2301
2302 it('should check for simple rewrites', function () {
2303 expect(vcClass.checkRewrite(
2304 {
2305 "@type" : "koral:doc",
2306 "rewrites" : [{
Akron712733a2018-04-05 18:17:47 +02002307 "@type" : "koral:rewrite",
2308 "operation" : "operation:modification",
2309 "src" : "querySerializer",
2310 "scope" : "tree"
Akron5c829e92017-05-12 18:10:00 +02002311 }]
2312 }
2313 )).toBeTruthy();
Nils Diewald7c8ced22015-04-15 19:21:00 +00002314
Akron5c829e92017-05-12 18:10:00 +02002315 var nested = {
2316 "@type" : "koral:docGroup",
2317 "operands" : [
2318 {
2319 "@type" : "koral:doc"
2320 },
2321 {
2322 "@type" : "koral:docGroup",
2323 "operands" : [
2324 {
2325 "@type": "koral:doc"
2326 },
2327 {
2328 "@type": "koral:doc"
2329 }
2330 ]
2331 }
2332 ]
2333 };
2334
2335 expect(vcClass.checkRewrite(nested)).toBe(false);
2336
2337 nested["operands"][1]["operands"][1]["rewrites"] = [{
Akron712733a2018-04-05 18:17:47 +02002338 "@type" : "koral:rewrite",
2339 "operation" : "operation:modification",
2340 "src" : "querySerializer",
2341 "scope" : "tree"
Akron5c829e92017-05-12 18:10:00 +02002342 }];
2343
2344 expect(vcClass.checkRewrite(nested)).toBeTruthy();
2345 });
2346 });
Nils Diewald6283d692015-04-23 20:32:53 +00002347
2348 describe('KorAP.Rewrite', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +00002349 it('should be initializable', function () {
2350 var rewrite = rewriteClass.create({
Akron712733a2018-04-05 18:17:47 +02002351 "@type" : "koral:rewrite",
2352 "operation" : "operation:modification",
2353 "src" : "querySerializer",
2354 "scope" : "tree"
Nils Diewald86dad5b2015-01-28 15:09:07 +00002355 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00002356 expect(rewrite.toString()).toEqual('Modification of "tree" by "querySerializer"');
2357 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00002358
Nils Diewald7c8ced22015-04-15 19:21:00 +00002359 it('should be deserialized by docs', function () {
2360 var doc = docClass.create(
Akron712733a2018-04-05 18:17:47 +02002361 undefined,
2362 {
2363 "@type":"koral:doc",
2364 "key":"Titel",
2365 "value":"Baum",
2366 "match":"match:eq"
2367 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00002368
2369 expect(doc.element().classList.contains('doc')).toBeTruthy();
2370 expect(doc.element().classList.contains('rewritten')).toBe(false);
2371
2372 doc = docClass.create(
Akron712733a2018-04-05 18:17:47 +02002373 undefined,
2374 {
2375 "@type":"koral:doc",
2376 "key":"Titel",
2377 "value":"Baum",
2378 "match":"match:eq",
2379 "rewrites" : [
2380 {
2381 "@type" : "koral:rewrite",
2382 "operation" : "operation:modification",
2383 "src" : "querySerializer",
2384 "scope" : "tree"
2385 }
2386 ]
2387 });
Nils Diewald7c8ced22015-04-15 19:21:00 +00002388
2389 expect(doc.element().classList.contains('doc')).toBeTruthy();
2390 expect(doc.element().classList.contains('rewritten')).toBeTruthy();
2391 });
Nils Diewald6283d692015-04-23 20:32:53 +00002392
Akron76c3dd62018-05-29 20:58:27 +02002393 it('should be described in a title attribute', function () {
2394
2395 doc = docClass.create(
2396 undefined,
2397 {
2398 "@type":"koral:doc",
2399 "key":"Titel",
2400 "value":"Baum",
2401 "match":"match:eq",
2402 "rewrites" : [
2403 {
2404 "@type" : "koral:rewrite",
2405 "operation" : "operation:modification",
2406 "src" : "querySerializer",
2407 "scope" : "tree"
2408 },
2409 {
2410 "@type" : "koral:rewrite",
2411 "operation" : "operation:injection",
2412 "src" : "me"
2413 }
2414 ]
2415 });
2416
2417 expect(doc.element().classList.contains('doc')).toBeTruthy();
2418 expect(doc.element().classList.contains('rewritten')).toBeTruthy();
2419 expect(doc.element().lastChild.getAttribute("title")).toEqual("querySerializer: tree (modification)\nme (injection)");
2420 });
2421
2422
Nils Diewald6283d692015-04-23 20:32:53 +00002423 xit('should be deserialized by docGroups', function () {
2424 var docGroup = docGroupClass.create(
Akron712733a2018-04-05 18:17:47 +02002425 undefined,
2426 {
2427 "@type" : "koral:docGroup",
2428 "operation" : "operation:or",
2429 "operands" : [
2430 {
2431 "@type" : "doc",
2432 "key" : "pubDate",
2433 "type" : "type:date",
2434 "value" : "2014-12-05"
2435 },
2436 {
2437 "@type" : "doc",
2438 "key" : "pubDate",
2439 "type" : "type:date",
2440 "value" : "2014-12-06"
2441 }
2442 ],
2443 "rewrites" : [
2444 {
2445 "@type" : "koral:rewrite",
2446 "operation" : "operation:modification",
2447 "src" : "querySerializer",
2448 "scope" : "tree"
2449 }
2450 ]
2451 }
Nils Diewald6283d692015-04-23 20:32:53 +00002452 );
2453
2454 expect(doc.element().classList.contains('docgroup')).toBeTruthy();
2455 expect(doc.element().classList.contains('rewritten')).toBe(false);
2456 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00002457 });
Nils Diewaldf0c4f112015-05-05 12:56:59 +00002458
2459 describe('KorAP.stringValue', function () {
2460 it('should be initializable', function () {
2461 var sv = stringValClass.create();
2462 expect(sv.regex()).toBe(false);
2463 expect(sv.value()).toBe('');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002464
2465 sv = stringValClass.create('Baum');
2466 expect(sv.regex()).toBe(false);
2467 expect(sv.value()).toBe('Baum');
2468
2469 sv = stringValClass.create('Baum', false);
2470 expect(sv.regex()).toBe(false);
2471 expect(sv.value()).toBe('Baum');
2472
2473 sv = stringValClass.create('Baum', true);
2474 expect(sv.regex()).toBe(true);
2475 expect(sv.value()).toBe('Baum');
2476 });
2477
2478 it('should be modifiable', function () {
2479 var sv = stringValClass.create();
2480 expect(sv.regex()).toBe(false);
2481 expect(sv.value()).toBe('');
2482
2483 expect(sv.value('Baum')).toBe('Baum');
2484 expect(sv.value()).toBe('Baum');
2485
2486 expect(sv.regex(true)).toBe(true);
2487 expect(sv.regex()).toBe(true);
2488 });
2489
2490 it('should have a toggleble regex value', function () {
2491 var sv = stringValClass.create();
Nils Diewald7991a3f2015-05-19 14:12:37 +00002492
2493 expect(sv.element().firstChild.value).toBe('');
2494 sv.element().firstChild.value = 'der'
2495 expect(sv.element().firstChild.value).toBe('der');
2496
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002497 expect(sv.regex()).toBe(false);
2498
2499 sv.toggleRegex();
Nils Diewald7991a3f2015-05-19 14:12:37 +00002500 expect(sv.element().firstChild.value).toBe('der');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002501 expect(sv.regex()).toBe(true);
Nils Diewald7991a3f2015-05-19 14:12:37 +00002502 sv.element().firstChild.value = 'derbe'
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002503
2504 sv.toggleRegex();
2505 expect(sv.regex()).toBe(false);
Nils Diewald7991a3f2015-05-19 14:12:37 +00002506 expect(sv.element().firstChild.value).toBe('derbe');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002507 });
2508
2509 it('should have an element', function () {
2510 var sv = stringValClass.create('der');
2511 expect(sv.element().nodeName).toBe('DIV');
2512 expect(sv.element().firstChild.nodeName).toBe('INPUT');
2513 expect(sv.element().firstChild.value).toBe('der');
2514 });
2515
2516 it('should have a classed element', function () {
2517 var sv = stringValClass.create();
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002518 expect(sv.element().classList.contains('regex')).toBe(false);
2519 expect(sv.regex()).toBe(false);
2520 sv.toggleRegex();
2521 expect(sv.element().classList.contains('regex')).toBe(true);
2522 });
2523
2524 it('should be storable', function () {
2525 var sv = stringValClass.create();
2526 var count = 1;
2527 sv.store = function (value, regex) {
Akron712733a2018-04-05 18:17:47 +02002528 expect(regex).toBe(true);
2529 expect(value).toBe('tree');
Nils Diewaldc4c4b832015-05-05 16:00:08 +00002530 };
2531 sv.regex(true);
2532 sv.value('tree');
2533 sv.element().lastChild.click();
Nils Diewaldf0c4f112015-05-05 12:56:59 +00002534 });
Akron712733a2018-04-05 18:17:47 +02002535
Akronb19803c2018-08-16 16:39:42 +02002536 it('should have a disableoption for regex', function () {
2537 var sv = stringValClass.create(undefined, undefined, true);
2538 var svE = sv.element();
2539 expect(svE.children.length).toEqual(2);
2540
2541 sv = stringValClass.create(undefined, undefined, false);
2542 svE = sv.element();
2543 expect(svE.children.length).toEqual(1);
2544 });
2545
Nils Diewaldf0c4f112015-05-05 12:56:59 +00002546 });
Akrone4961b12017-05-10 21:04:46 +02002547
Akron712733a2018-04-05 18:17:47 +02002548 describe('KorAP.VC.Menu', function () {
Akrone65a88a2018-04-05 19:14:20 +02002549
2550 var vc;
2551
Akron712733a2018-04-05 18:17:47 +02002552 it('should be initializable', function () {
2553
Akrone65a88a2018-04-05 19:14:20 +02002554 vc = vcClass.create([
Akron712733a2018-04-05 18:17:47 +02002555 ['a', 'text'],
2556 ['b', 'string'],
2557 ['c', 'date']
2558 ]).fromJson();
2559 expect(vc.element().firstChild.classList.contains('unspecified')).toBeTruthy();
2560 expect(vc.element().firstChild.firstChild.tagName).toEqual('SPAN');
2561
2562 // Click on unspecified
2563 vc.element().firstChild.firstChild.click();
2564 expect(vc.element().firstChild.firstChild.tagName).toEqual('UL');
2565
2566 var list = vc.element().firstChild.firstChild;
2567 expect(list.getElementsByTagName("LI")[0].innerText).toEqual('a');
2568 expect(list.getElementsByTagName("LI")[1].innerText).toEqual('b');
2569 expect(list.getElementsByTagName("LI")[2].innerText).toEqual('c');
2570
2571 vc = vcClass.create([
2572 ['d', 'text'],
2573 ['e', 'string'],
2574 ['f', 'date']
2575 ]).fromJson();
2576 expect(vc.element().firstChild.classList.contains('unspecified')).toBeTruthy();
2577 expect(vc.element().firstChild.firstChild.tagName).toEqual('SPAN');
2578
2579 // Click on unspecified
2580 vc.element().firstChild.firstChild.click();
2581 expect(vc.element().firstChild.firstChild.tagName).toEqual('UL');
2582
2583 list = vc.element().firstChild.firstChild;
2584 expect(list.getElementsByTagName("LI")[0].innerText).toEqual('d');
2585 expect(list.getElementsByTagName("LI")[1].innerText).toEqual('e');
2586 expect(list.getElementsByTagName("LI")[2].innerText).toEqual('f');
Akron31d89942018-04-06 16:44:51 +02002587 // blur
2588 document.body.click();
Akron712733a2018-04-05 18:17:47 +02002589 });
Akrone65a88a2018-04-05 19:14:20 +02002590
2591 // Reinitialize to make tests stable
2592 vc = vcClass.create([
2593 ['d', 'text'],
2594 ['e', 'string'],
2595 ['f', 'date']
2596 ]).fromJson();
2597
2598 it('should be clickable on key', function () {
Akron31d89942018-04-06 16:44:51 +02002599 // Click on unspecified
2600 vc.element().firstChild.firstChild.click();
Akrone65a88a2018-04-05 19:14:20 +02002601 // Click on "d"
2602 vc.element().firstChild.firstChild.getElementsByTagName("LI")[0].click();
2603 expect(vc.element().firstChild.firstChild.tagName).toEqual('SPAN');
2604 expect(vc.element().firstChild.firstChild.innerText).toEqual('d');
2605 expect(vc.element().firstChild.children[1].innerText).toEqual('eq');
2606 expect(vc.element().firstChild.children[1].getAttribute('data-type')).toEqual('text');
Akron31d89942018-04-06 16:44:51 +02002607 // blur
2608 document.body.click();
Akrone65a88a2018-04-05 19:14:20 +02002609 });
2610
Akron31d89942018-04-06 16:44:51 +02002611 it('should be clickable on operation for text', function () {
2612 // Click on "d" (or unspecified)
2613 vc.element().firstChild.firstChild.click();
2614
2615 // Choose "d"
2616 vc.element().firstChild.firstChild.getElementsByTagName("LI")[0].click();
2617
2618 // Click on matchop
Akrone65a88a2018-04-05 19:14:20 +02002619 vc.element().firstChild.children[1].click();
Akron31d89942018-04-06 16:44:51 +02002620
Akrone65a88a2018-04-05 19:14:20 +02002621 expect(vc.element().firstChild.children[1].tagName).toEqual('UL');
Akron31d89942018-04-06 16:44:51 +02002622
Akrone65a88a2018-04-05 19:14:20 +02002623 var ul = vc.element().firstChild.children[1];
2624 expect(ul.getElementsByTagName('li')[0].innerText).toEqual("eq");
2625 expect(ul.getElementsByTagName('li')[1].innerText).toEqual("ne");
2626 expect(ul.getElementsByTagName('li')[2].innerText).toEqual("contains");
2627 expect(ul.getElementsByTagName('li')[3].innerText).toEqual("containsnot");
2628 expect(ul.getElementsByTagName('li')[4]).toBeUndefined();
Akron31d89942018-04-06 16:44:51 +02002629
2630 // Choose "contains"
2631 ul.getElementsByTagName('li')[2].click();
2632 expect(vc.element().firstChild.children[1].tagName).toEqual("SPAN");
2633 expect(vc.element().firstChild.children[1].innerText).toEqual("contains");
2634 // blur
2635 document.body.click();
Akrone65a88a2018-04-05 19:14:20 +02002636 })
Akron31d89942018-04-06 16:44:51 +02002637
2638 it('should be clickable on operation for string', function () {
2639 // Click on "d" (or unspecified)
2640 vc.element().firstChild.firstChild.click();
2641
2642 // Choose "e"
2643 vc.element().firstChild.firstChild.getElementsByTagName("LI")[1].click();
2644
2645 // As a consequence the matchoperator may no longer
2646 // be valid and needs to be re-evaluated
2647 var fc = vc.element().firstChild;
2648 expect(fc.firstChild.tagName).toEqual('SPAN');
2649 expect(fc.firstChild.innerText).toEqual('e');
2650 expect(fc.children[1].innerText).toEqual('eq');
2651 expect(fc.children[1].getAttribute('data-type')).toEqual('string');
2652
2653 vc.element().firstChild.children[1].click();
2654
2655 expect(vc.element().firstChild.children[1].tagName).toEqual('UL');
2656
2657 var ul = vc.element().firstChild.children[1];
2658 expect(ul.getElementsByTagName('li')[0].innerText).toEqual("eq");
2659 expect(ul.getElementsByTagName('li')[1].innerText).toEqual("ne");
2660 expect(ul.getElementsByTagName('li')[2]).toBeUndefined();
2661
2662 // Choose "ne"
2663 ul.getElementsByTagName('li')[1].click();
2664 expect(vc.element().firstChild.children[1].tagName).toEqual("SPAN");
2665 expect(vc.element().firstChild.children[1].innerText).toEqual("ne");
Akron8db5e3a2018-05-28 19:25:26 +02002666
2667 // Click on text
2668 expect(vc.element().firstChild.children[2].innerText).toEqual("⋯");
2669 vc.element().firstChild.children[2].click();
2670
2671 // Blur text element
2672 expect(vc.element().firstChild.children[2].firstChild.value).toEqual('');
2673
Akron31d89942018-04-06 16:44:51 +02002674 // blur
2675 document.body.click();
2676 });
2677
2678 it('should be clickable on operation for date', function () {
2679
2680 // Replay matchop check - so it's guaranteed that "ne" is chosen
2681 // Click on "e" (or unspecified)
2682 vc.element().firstChild.firstChild.click();
2683 // Rechoose "e"
2684 vc.element().firstChild.firstChild.getElementsByTagName("LI")[1].click();
2685 // Click on matchop
2686 vc.element().firstChild.children[1].click();
2687 // Choose "ne"
2688 vc.element().firstChild.children[1].getElementsByTagName('li')[1].click();
2689 expect(vc.element().firstChild.children[1].innerText).toEqual("ne");
2690
2691 // Click on "e"
2692 vc.element().firstChild.firstChild.click();
2693 // Choose "f"
2694 vc.element().firstChild.firstChild.getElementsByTagName("LI")[2].click();
2695
2696 // The matchoperator should still be "ne" as this is valid for dates as well (now)
2697 var fc = vc.element().firstChild;
2698 expect(fc.firstChild.tagName).toEqual('SPAN');
2699 expect(fc.firstChild.innerText).toEqual('f');
2700 expect(fc.children[1].innerText).toEqual('ne');
2701 // blur
2702 document.body.click();
2703 });
Akronddc98a72018-04-06 17:33:52 +02002704
2705
2706 // Check json deserialization
2707 it('should be initializable', function () {
2708 vc = vcClass.create([
2709 ['a', 'text'],
2710 ['b', 'string'],
2711 ['c', 'date']
2712 ]).fromJson({
2713 "@type" : "koral:doc",
2714 "key":"a",
2715 "value":"Baum"
2716 });
2717
2718 expect(vc.element().firstChild.children[1].getAttribute('data-type')).toEqual('text');
2719 });
Akron712733a2018-04-05 18:17:47 +02002720 });
2721
2722
Akrone4961b12017-05-10 21:04:46 +02002723 // Check prefix
2724 describe('KorAP.VC.Prefix', function () {
2725
2726 it('should be initializable', function () {
2727 var p = prefixClass.create();
2728 expect(p.element().classList.contains('pref')).toBeTruthy();
2729 expect(p.isSet()).not.toBeTruthy();
2730 });
2731
2732
2733 it('should be clickable', function () {
2734 var vc = vcClass.create([
Akron712733a2018-04-05 18:17:47 +02002735 ['a', null],
2736 ['b', null],
2737 ['c', null]
2738 ]).fromJson();
Akrone4961b12017-05-10 21:04:46 +02002739 expect(vc.element().firstChild.classList.contains('unspecified')).toBeTruthy();
2740
2741 // This should open up the menu
2742 vc.element().firstChild.firstChild.click();
2743 expect(vc.element().firstChild.firstChild.tagName).toEqual('UL');
2744
2745 KorAP._vcKeyMenu._prefix.clear();
2746 KorAP._vcKeyMenu._prefix.add('x');
2747
2748 var prefElement = vc.element().querySelector('span.pref');
2749 expect(prefElement.innerText).toEqual('x');
2750
2751 // This should add key 'x' to VC
2752 prefElement.click();
2753
2754 expect(vc.element().firstChild.classList.contains('doc')).toBeTruthy();
2755 expect(vc.element().firstChild.firstChild.className).toEqual('key');
2756 expect(vc.element().firstChild.firstChild.innerText).toEqual('x');
2757 });
2758 });
Nils Diewald2fe12e12015-03-06 16:47:06 +00002759});