blob: fbb41a1cceba1c8264b47e6e8a586e1373739e9b [file] [log] [blame]
hebasta5e4d7542018-06-19 16:57:36 +02001/**
2 * Test suite for corpus statistic
3 *
4 * @author Helge Stallkamp
Akron18a99c02018-08-20 12:55:14 +02005 * @author Nils Diewald
hebasta5e4d7542018-06-19 16:57:36 +02006 */
7
8
Akronaa613222019-11-19 13:57:12 +01009define(['vc', 'vc/statistic', 'view/vc/corpstatv'], function(vcClass, statClass, corpStatVClass){
hebastaa0282be2018-12-05 16:58:00 +010010
hebasta47d03a42018-06-25 10:31:58 +020011 var json = {
Akron18a99c02018-08-20 12:55:14 +020012 "@type":"koral:docGroup",
13 "operation":"operation:or",
14 "operands":[
15 {
16 "@type":"koral:docGroup",
17 "operation":"operation:and",
18 "operands":[
19 {
20 "@type":"koral:doc",
21 "key":"title",
22 "value":"Der Birnbaum",
23 "match":"match:eq"
24 },
25 {
26 "@type":"koral:doc",
27 "key":"pubPlace",
28 "value":"Mannheim",
29 "type" : "type:regex",
30 "match":"match:contains"
31 },
32 {
33 "@type":"koral:docGroup",
34 "operation":"operation:or",
35 "operands":[
36 {
37 "@type":"koral:doc",
38 "key":"subTitle",
39 "value":"Aufzucht und Pflege",
40 "match":"match:eq"
41 },
42 {
43 "@type":"koral:doc",
44 "key":"subTitle",
45 "value":"Gedichte",
46 "match":"match:eq",
47 "rewrites" : [
48 {
49 "@type": "koral:rewrite",
50 "src" : "policy",
51 "operation" : "operation:injection",
52 }
53 ]
54 }
55 ]
56 }
57 ]
58 },
59 {
60 "@type":"koral:doc",
61 "key":"pubDate",
62 "type":"type:date",
63 "value":"2015-03-05",
64 "match":"match:geq"
65 }
66 ]
67 };
hebasta5e4d7542018-06-19 16:57:36 +020068
hebasta47d03a42018-06-25 10:31:58 +020069 var preDefinedStat={
Akron18a99c02018-08-20 12:55:14 +020070 "documents":12,
71 "tokens":2323,
72 "sentences":343434,
73 "paragraphs":45454545
hebasta47d03a42018-06-25 10:31:58 +020074 };
75
Akron751e9e42019-03-13 09:54:55 +010076 var preDefinedStat2={
77 "documents":20216975,
78 "tokens": "5991667065",
79 "sentences":403923016,
80 "paragraphs":129385487
81 };
82
83
hebasta47d03a42018-06-25 10:31:58 +020084 KorAP.API.getCorpStat = function(collQu, cb){
85 return cb(preDefinedStat);
Akron18a99c02018-08-20 12:55:14 +020086 };
87
hebastaa0282be2018-12-05 16:58:00 +010088
89 generateCorpusDocGr = function(){
90 let vc = vcClass.create().fromJson({
91 "@type" : 'koral:docGroup',
92 'operation' : 'operation:or',
93 'operands' : [
94 {
95 '@type' : 'koral:doc',
96 'key' : 'title',
97 'match': 'match:eq',
98 'value' : 'Hello World!'
99 },
100 {
101 '@type' : 'koral:doc',
102 'match': 'match:eq',
103 'key' : 'foo',
104 'value' : 'bar'
105 }
106 ]
107 });
108 return vc;
109 }
110
hebastade595b42019-02-05 13:53:10 +0100111 /**
112 * Generate corpus doc group with more entries
113 */
114 generateBCorpusDocGr = function(){
115 let vc = vcClass.create().fromJson({
116 "@type": "koral:docGroup",
117 "operation": "operation:or",
118 "operands": [{
119 "@type" : 'koral:docGroup',
120 'operation' : 'operation:or',
121 'operands' : [
122 {
123 '@type' : 'koral:doc',
124 'key' : 'title',
125 'match': 'match:eq',
126 'value' : 'Hello World!'
127 },
128 {
129 '@type' : 'koral:doc',
130 'match': 'match:eq',
131 'key' : 'foo',
132 'value' : 'bar'
133 }
134 ]
135 },
136 {
137 '@type' : 'koral:doc',
138 'match': 'match:eq',
139 'key' : 'author',
140 'value' : 'Goethe'
141 }
142 ]
143 });
144
145 return vc;
146 }
147
hebastaa0282be2018-12-05 16:58:00 +0100148 generateCorpusDoc = function(){
149 let vc= vcClass.create().fromJson({
150 '@type' : 'koral:doc',
151 'key' : 'title',
152 'match': 'match:eq',
153 'value' : 'Hello World!',
154 'type' : 'type:string'
155 });
156 return vc;
157 };
158
159
160 /**
161 * Generate vc with docgroupref
162 */
163 generateCorpusRef = function(){
164 let vc = vcClass.create().fromJson({
165 "@type" : "koral:docGroupRef",
166 "ref" : "@max/myCorpus"
167 });
168 return vc;
169 };
170
171
172 /**
173 * Checks is corpus statistic is active
174 */
175 checkStatActive = function(view, div){
176 // corpus statistic exists, it is active and reloadStatButton is shown
177 if ((view.firstChild.classList.contains("stattable") === true)
hebasta48842cf2018-12-11 12:57:38 +0100178 && (view.firstChild.classList.contains("stdisabled") === false)
hebastaa0282be2018-12-05 16:58:00 +0100179 && (div.getElementsByClassName("reloadStatB").length == 0) ) {
180 return true;
181 }
182 return false;
183 };
184
185
186 /**
187 * Checks if corpus statistic is disabled
188 */
189 checkStatDisabled = function(view, div){
190 if ((view.firstChild.classList.contains("stattable") === true)
hebasta48842cf2018-12-11 12:57:38 +0100191 && (view.firstChild.classList.contains("stdisabled") === true)
hebastaa0282be2018-12-05 16:58:00 +0100192 && (div.getElementsByClassName("reloadStatB").length === 1) ) {
193 return true;
194 }
195 return false;
196 };
197
198
199 describe('KorAP.CorpusStat', function(){
hebasta47d03a42018-06-25 10:31:58 +0200200
hebasta5e4d7542018-06-19 16:57:36 +0200201 it('should be initiable', function(){
Akron18a99c02018-08-20 12:55:14 +0200202 var stat = statClass.create(preDefinedStat);
hebasta5e4d7542018-06-19 16:57:36 +0200203 expect( function() { statClass.create() }).toThrow(new Error("Missing parameter"));
204 });
205
206
Akron18a99c02018-08-20 12:55:14 +0200207 it('should be parsed in a statistic view and displayed as HTML Description List', function(){
208 var stat = statClass.create(preDefinedStat);
hebastaa0282be2018-12-05 16:58:00 +0100209 var descL = stat.element();
hebasta5e4d7542018-06-19 16:57:36 +0200210 expect(descL.tagName).toEqual('DL');
211 expect(descL.children[0].tagName).toEqual('DIV');
212 expect(descL.children[0].children[0].tagName).toEqual('DT');
213 expect(descL.children[0].children[0].attributes[0].name).toEqual('title');
214 expect(descL.children[0].children[1].tagName).toEqual('DD');
Akron18a99c02018-08-20 12:55:14 +0200215
hebasta5e4d7542018-06-19 16:57:36 +0200216 expect(descL.children[0].children[0].firstChild.nodeValue).toEqual('documents');
217 expect(descL.children[0].children[1].firstChild.nodeValue).toEqual('12');
218 expect(descL.children[0].children[0].attributes[0].value).toEqual('documents');
Akron2f979122018-07-25 17:00:23 +0200219
220 expect(descL.children[1].children[0].firstChild.nodeValue).toEqual('tokens');
221 expect(descL.children[1].children[1].firstChild.nodeValue).toEqual(new Number(2323).toLocaleString());
222 expect(descL.children[1].children[0].attributes[0].value).toEqual('tokens');
223 });
Akron751e9e42019-03-13 09:54:55 +0100224
225
226 it('should be parsed in a statistic view and displayed as HTML Description List (2)', function(){
227 var stat = statClass.create(preDefinedStat2);
228 var descL = stat.element();
229 expect(descL.tagName).toEqual('DL');
230 expect(descL.children[0].tagName).toEqual('DIV');
231 expect(descL.children[0].children[0].tagName).toEqual('DT');
232 expect(descL.children[0].children[0].attributes[0].name).toEqual('title');
233 expect(descL.children[0].children[1].tagName).toEqual('DD');
234
235 expect(descL.children[0].children[0].firstChild.nodeValue).toEqual('documents');
236 expect(descL.children[0].children[1].firstChild.nodeValue).toEqual(new Number(20216975).toLocaleString());
237 expect(descL.children[0].children[0].attributes[0].value).toEqual('documents');
238
239 expect(descL.children[1].children[0].firstChild.nodeValue).toEqual('tokens');
240 expect(descL.children[1].children[1].firstChild.nodeValue).toEqual(new Number(5991667065).toLocaleString());
241 expect(descL.children[1].children[0].attributes[0].value).toEqual('tokens');
242
243 expect(descL.children[2].children[0].firstChild.nodeValue).toEqual('sentences');
244 expect(descL.children[2].children[1].firstChild.nodeValue).toEqual(new Number(403923016).toLocaleString());
245 expect(descL.children[2].children[0].attributes[0].value).toEqual('sentences');
246
247 expect(descL.children[3].children[0].firstChild.nodeValue).toEqual('paragraphs');
248 expect(descL.children[3].children[1].firstChild.nodeValue).toEqual(new Number(129385487).toLocaleString());
249 expect(descL.children[3].children[0].attributes[0].value).toEqual('paragraphs');
250 });
hebasta5e4d7542018-06-19 16:57:36 +0200251
hebasta47d03a42018-06-25 10:31:58 +0200252
hebasta6c8f09d2018-07-24 13:21:36 +0200253 it('should display corpus statistic after creating a corpus statistic view', function(){
Akron18a99c02018-08-20 12:55:14 +0200254 var vc = vcClass.create([
255 ['title', 'string'],
256 ['subTitle', 'string'],
257 ['pubDate', 'date'],
258 ['author', 'text']
259 ]).fromJson(json);
260
hebastaa0282be2018-12-05 16:58:00 +0100261 KorAP.vc = vc;
262
Akron18a99c02018-08-20 12:55:14 +0200263 statView = corpStatVClass.create(vc);
hebastaa0282be2018-12-05 16:58:00 +0100264 // corpStatVClass.show(vc);
Akron18a99c02018-08-20 12:55:14 +0200265
hebasta47d03a42018-06-25 10:31:58 +0200266 var testDiv = document.createElement('div');
hebasta6c8f09d2018-07-24 13:21:36 +0200267 testDiv.appendChild(statView.show());
hebastaa0282be2018-12-05 16:58:00 +0100268 // statClass.showCorpStat(testDiv, vc);
hebasta47d03a42018-06-25 10:31:58 +0200269
270 expect(testDiv.children[0].tagName).toEqual('DIV');
271 expect(testDiv.children[0].getAttribute("class")).toEqual('stattable');
272 expect(testDiv.children[0].children[0].tagName).toEqual('DL');
273 expect(testDiv.children[0].children[0].children[0].children[0].firstChild.nodeValue).toEqual('documents');
274 expect(testDiv.children[0].children[0].children[0].children[1].firstChild.nodeValue).toEqual('12');
hebasta47d03a42018-06-25 10:31:58 +0200275 });
Akron18a99c02018-08-20 12:55:14 +0200276
277 it('should display the statistics button in a panel', function () {
278 var vc = vcClass.create([
279 ['title', 'string'],
280 ['subTitle', 'string'],
281 ['pubDate', 'date'],
282 ['author', 'text']
283 ]).fromJson(json);
284
285 var show = document.createElement('div');
286
287 show.appendChild(vc.element());
288
289 var panel = show.firstChild.lastChild.firstChild;
290 expect(panel.classList.contains('panel')).toBeTruthy();
291 expect(panel.classList.contains('vcinfo')).toBeTruthy();
292 expect(panel.lastChild.classList.contains('button-group')).toBeTruthy();
293 expect(panel.lastChild.classList.contains('vcinfo')).toBeTruthy();
294 expect(panel.lastChild.children[0].tagName).toEqual('SPAN');
295 expect(panel.lastChild.children[0].textContent).toEqual('Statistics');
296 });
297
298
299 it('should display the statistics button in a panel after VC modification', function () {
300
301 var vc = vcClass.create([
302 ['title', 'string'],
303 ['subTitle', 'string'],
304 ['pubDate', 'date'],
305 ['author', 'text']
306 ]).fromJson(json);
307
308 var show = document.createElement('div');
309
310 show.appendChild(vc.element());
311
312 expect(show.querySelector(".statistic").tagName).toEqual("SPAN");
313
Akronadab5e52018-08-20 13:50:53 +0200314 var and = vc.builder().lastChild.firstChild;
Akron18a99c02018-08-20 12:55:14 +0200315
316 // Click on and() in VC
317 and.click();
318
319 // Check that statistics is there
320 expect(show.querySelector(".statistic").tagName).toEqual("SPAN");
321 });
322
323
324 it('should display and hide corpus statistics view in the panel', function () {
325
326 var vc = vcClass.create([
327 ['title', 'string'],
328 ['subTitle', 'string'],
329 ['pubDate', 'date'],
330 ['author', 'text']
331 ]).fromJson(json);
332
333 var show = document.createElement('div');
334
335 show.appendChild(vc.element());
336
337 var panel = show.firstChild.lastChild.firstChild;
338
339 // Show statistics
340 panel.lastChild.children[0].click();
341
342 // Statistics view
343 var viewE = panel.firstChild.firstChild;
344
345 expect(viewE.tagName).toEqual("DIV");
346 expect(viewE.classList.contains("view")).toBeTruthy();
347 expect(viewE.classList.contains("vcstatistic")).toBeTruthy();
348
349 expect(viewE.firstChild.classList.contains("stattable")).toBeTruthy();
350 expect(viewE.firstChild.firstChild.tagName).toEqual("DL");
351
352 // List of statistic values
353 var dl = viewE.firstChild.firstChild.firstChild;
354 expect(dl.firstChild.tagName).toEqual("DT");
355 expect(dl.firstChild.textContent).toEqual("documents");
356 expect(dl.lastChild.tagName).toEqual("DD");
357 expect(dl.lastChild.textContent).toEqual("12");
358
359 expect(viewE.children.length).toEqual(2);
360
361 expect(viewE.lastChild.classList.contains("button-group")).toBeTruthy();
362 expect(viewE.lastChild.children.length).toEqual(1);
363 expect(viewE.lastChild.firstChild.tagName).toEqual("SPAN");
364 expect(viewE.lastChild.firstChild.textContent).toEqual("Close");
365
366 // Close view
367 viewE.lastChild.firstChild.firstChild.click();
368
369 // Is gone
370 expect(panel.firstChild.children.length).toEqual(0);
371
372 // Show statistics
373 panel.lastChild.children[0].click();
374
375 // Is there
376 expect(panel.firstChild.children.length).toEqual(1);
377
378 // Only show once
379 panel.lastChild.children[0].click();
380 expect(panel.firstChild.children.length).toEqual(1);
381 });
hebastaa0282be2018-12-05 16:58:00 +0100382
383
384 });
385
386
387
388 /**
389 * Test disabling and reload of corpus statistic if vc is changed
390 * in vc builder through user-interaction
391 */
392 describe ('KorAP.CorpusStat.Disable', function(){
hebastade595b42019-02-05 13:53:10 +0100393
hebastade595b42019-02-05 13:53:10 +0100394
hebastaa0282be2018-12-05 16:58:00 +0100395 /**
396 * If the user defines a new vc, the statistic should be disabled,
397 * because it is out-of-date.
398 *
399 * The user can choose to display an up-to-date corpus statistic. Here it is tested
400 * if corpus statistic is disabled after a valid change of corpus statistic and if the corpus statistic is updatable.
401 */
402 it ('should disable the corpus statistic if corpus definition is changed and display a functional reload button', function(){
403
404 KorAP.vc = generateCorpusDocGr();
405
406 //Show corpus statistic
407 let show = document.createElement('div');
408 show.appendChild(KorAP.vc.element());
409 let panel = show.firstChild.lastChild.firstChild;
410 panel.lastChild.children[0].click();
411 let view = panel.firstChild.firstChild;
412
413 //corpus statistic is active
414 expect(checkStatActive(view, show)).toBe(true);
415
416 //change vc, a line in vc builder is deleted
417 KorAP._delete.apply(KorAP.vc.root().getOperand(0));
418 expect(checkStatDisabled(view,show)).toBe(true);
419
420 //click at reload button
421 let rlbutton = show.getElementsByClassName("refresh").item(0);
422 rlbutton.click();
423
424 expect(checkStatActive(view,show)).toBe(true);
425 });
426
427
428 it('should disable corpus statistic if entries in vc builder are deleted', function(){
429 KorAP.vc = generateCorpusDocGr();
430
431 // create corpus builder and corpus statistic;
432 let show = document.createElement('div');
433 show.appendChild(KorAP.vc.element());
434 let panel = show.firstChild.lastChild.firstChild;
435 panel.lastChild.children[0].click();
436 let view = panel.firstChild.firstChild;
437
438 expect(checkStatActive(view, show)).toBe(true);
439
440 //delete foo=bar
441 KorAP._delete.apply(KorAP.vc.root().getOperand(1));
442 expect(checkStatDisabled(view, show)).toBe(true);
443
444 //refresh corpus statistic
445 let rlbutton = show.getElementsByClassName("refresh").item(0);
446 rlbutton.click();
447 expect(checkStatActive(view,show)).toBe(true);
hebasta3f4be922018-12-11 10:41:46 +0100448
hebastaa0282be2018-12-05 16:58:00 +0100449 KorAP._delete.apply(KorAP.vc.root());
hebasta3f4be922018-12-11 10:41:46 +0100450 view = panel.firstChild.firstChild;
451 expect(checkStatDisabled(view, show)).toBe(true);
hebastade595b42019-02-05 13:53:10 +0100452
453 KorAP.vc = generateBCorpusDocGr();
454 // create corpus builder and corpus statistic;
455 show = document.createElement('div');
456 show.appendChild(KorAP.vc.element());
457 panel = show.firstChild.lastChild.firstChild;
458 panel.lastChild.children[0].click();
459 view = panel.firstChild.firstChild;
460
461 expect(checkStatActive(view, show)).toBe(true);
462 KorAP._delete.apply(KorAP.vc.root().getOperand(1));
463 view = panel.firstChild.firstChild;
464 expect(checkStatDisabled(view, show)).toBe(true);
465
hebastaa0282be2018-12-05 16:58:00 +0100466 });
467
468
469 it('should disable corpus statistic if key, matchoperator or value is changed', function(){
470 /*
471 * Doc change of key, match operator and value
472 */
473 KorAP.vc= generateCorpusDoc();
474 // show vc builder and open corpus statistic
475 let show = document.createElement('div');
476 show.appendChild(KorAP.vc.element());
477 let panel = show.firstChild.lastChild.firstChild;
478 panel.lastChild.children[0].click();
479 let view = panel.firstChild.firstChild;
480 expect(checkStatActive(view, show)).toBe(true);
481
482 KorAP.vc.root().matchop("ne").update();
483 expect(checkStatDisabled(view, show)).toBe(true);
484
485 let rlbutton = show.getElementsByClassName("refresh").item(0);
486 rlbutton.click();
487
488 view = panel.firstChild.firstChild;
489 expect(checkStatActive(view, show)).toBe(true);
490 KorAP.vc.root().value("Hello tester").update();
491 expect(checkStatDisabled(view, show)).toBe(true);
492
493 //refresh corpus statistic
494 rlbutton = show.getElementsByClassName("refresh").item(0);
495 rlbutton.click();
496 view = panel.firstChild.firstChild;
497 expect(checkStatActive(view, show)).toBe(true);
498
499 KorAP.vc.root().key("author").update();
500 expect(checkStatDisabled(view, show)).toBe(true);
501
502
503 /*
504 * DocGroupRef change of value...
505 */
506 KorAP.vc = generateCorpusRef();
507 show = document.createElement('div');
508 show.appendChild(KorAP.vc.element());
509 panel = show.firstChild.lastChild.firstChild;
510 panel.lastChild.children[0].click();
511 view = panel.firstChild.firstChild;
512 expect(checkStatActive(view, show)).toBe(true);
513
514 KorAP.vc.root().ref("@anton/secondCorpus").update();
515 expect(checkStatDisabled(view, show)).toBe(true);
516 });
517
518
519 it('should not disable corpus statistic if docgroup definition is incomplete', function(){
520
521 KorAP.vc = generateCorpusDocGr();
522
523 //Show corpus statistic
524 let show = document.createElement('div');
525 show.appendChild(KorAP.vc.element());
526 let panel = show.firstChild.lastChild.firstChild;
527 panel.lastChild.children[0].click();
528 let view = panel.firstChild.firstChild;
529
530 expect(checkStatActive(view, show)).toBe(true);
531
532 KorAP._and.apply(KorAP.vc.root());
533
534 let andbuild = show.getElementsByClassName("builder");
535 expect(andbuild[0].firstChild.classList.contains('docGroup')).toBeTruthy();
536 expect(andbuild[0].firstChild.getAttribute("data-operation")).toEqual("and");
537 expect(checkStatActive(view, show)).toBe(true);
538 });
539
540
541 it('should not disable corpus statistic if doc/docref definition is incomplete', function(){
542
543 /*
544 * DOC incomplete
545 */
546 KorAP.vc = vcClass.create().fromJson();
547 expect(KorAP.vc.builder().firstChild.classList.contains('unspecified')).toBeTruthy();
548
549 // show vc builder and open corpus statistic
550 let show = document.createElement('div');
551 show.appendChild(KorAP.vc.element());
552 let panel = show.firstChild.lastChild.firstChild;
553 panel.lastChild.children[0].click();
554 let view = panel.firstChild.firstChild;
555
556 // corpus statistic should be shown and be up-to-date, reload button is not shown
557 expect(checkStatActive(view, show)).toBe(true);
558
559 // open the menu
560 KorAP.vc.builder().firstChild.firstChild.click();
561 KorAP._vcKeyMenu._prefix.add("author");
562 let prefElement = KorAP.vc.builder().querySelector('span.pref');
563 // add key 'author' to VC
564 prefElement.click();
565
566 expect(checkStatActive(view, show)).toBe(true);
567
568
569 /*
570 * DOCREF incomplete
571 */
572 KorAP.vc = vcClass.create().fromJson();
573 expect(KorAP.vc.builder().firstChild.classList.contains('unspecified')).toBeTruthy();
574
575 // show vc builder and open corpus statistic
576 show = document.createElement('div');
577 show.appendChild(KorAP.vc.element());
578 panel = show.firstChild.lastChild.firstChild;
579 panel.lastChild.children[0].click();
580 view = panel.firstChild.firstChild;
581 expect(checkStatActive(view, show)).toBe(true);
582
583 KorAP.vc.builder().firstChild.firstChild.click();
584 KorAP._vcKeyMenu._prefix.add("referTo");
585 prefElement = KorAP.vc.builder().querySelector('span.pref');
586 prefElement.click();
587 expect(checkStatActive(view, show)).toBe(true);
588 });
589 });
hebasta5e4d7542018-06-19 16:57:36 +0200590});