blob: d5be4d82961d29a015435f1edd716d0499d79086 [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
hebasta8cab44b2019-12-16 13:50:32 +0100427 it ('should refresh a disabled corpus statistic if statistic button is clicked', function(){
428
429 KorAP.vc = generateCorpusDocGr();
430
431 //Show corpus statistic
432 let show = document.createElement('div');
433 show.appendChild(KorAP.vc.element());
434 let panel = show.firstChild.lastChild.firstChild;
435 let statbut = panel.lastChild.children[0];
436 statbut.click();
437 let view = panel.firstChild.firstChild;
438
439 //corpus statistic is active
440 expect(checkStatActive(view, show)).toBe(true);
441
442 //change vc, a line in vc builder is deleted
443 KorAP._delete.apply(KorAP.vc.root().getOperand(0));
444 expect(checkStatDisabled(view,show)).toBe(true);
445
446 //click at statistic button
447 statbut.click();
448 expect(checkStatActive(view,show)).toBe(true);
449 });
hebastaa0282be2018-12-05 16:58:00 +0100450
451 it('should disable corpus statistic if entries in vc builder are deleted', function(){
452 KorAP.vc = generateCorpusDocGr();
453
454 // create corpus builder and corpus statistic;
455 let show = document.createElement('div');
456 show.appendChild(KorAP.vc.element());
457 let panel = show.firstChild.lastChild.firstChild;
458 panel.lastChild.children[0].click();
459 let view = panel.firstChild.firstChild;
460
461 expect(checkStatActive(view, show)).toBe(true);
462
463 //delete foo=bar
464 KorAP._delete.apply(KorAP.vc.root().getOperand(1));
465 expect(checkStatDisabled(view, show)).toBe(true);
466
467 //refresh corpus statistic
468 let rlbutton = show.getElementsByClassName("refresh").item(0);
469 rlbutton.click();
470 expect(checkStatActive(view,show)).toBe(true);
hebasta3f4be922018-12-11 10:41:46 +0100471
hebastaa0282be2018-12-05 16:58:00 +0100472 KorAP._delete.apply(KorAP.vc.root());
hebasta3f4be922018-12-11 10:41:46 +0100473 view = panel.firstChild.firstChild;
474 expect(checkStatDisabled(view, show)).toBe(true);
hebastade595b42019-02-05 13:53:10 +0100475
476 KorAP.vc = generateBCorpusDocGr();
477 // create corpus builder and corpus statistic;
478 show = document.createElement('div');
479 show.appendChild(KorAP.vc.element());
480 panel = show.firstChild.lastChild.firstChild;
481 panel.lastChild.children[0].click();
482 view = panel.firstChild.firstChild;
483
484 expect(checkStatActive(view, show)).toBe(true);
485 KorAP._delete.apply(KorAP.vc.root().getOperand(1));
486 view = panel.firstChild.firstChild;
487 expect(checkStatDisabled(view, show)).toBe(true);
488
hebastaa0282be2018-12-05 16:58:00 +0100489 });
490
491
492 it('should disable corpus statistic if key, matchoperator or value is changed', function(){
493 /*
494 * Doc change of key, match operator and value
495 */
496 KorAP.vc= generateCorpusDoc();
497 // show vc builder and open corpus statistic
498 let show = document.createElement('div');
499 show.appendChild(KorAP.vc.element());
500 let panel = show.firstChild.lastChild.firstChild;
501 panel.lastChild.children[0].click();
502 let view = panel.firstChild.firstChild;
503 expect(checkStatActive(view, show)).toBe(true);
504
505 KorAP.vc.root().matchop("ne").update();
506 expect(checkStatDisabled(view, show)).toBe(true);
507
508 let rlbutton = show.getElementsByClassName("refresh").item(0);
509 rlbutton.click();
510
511 view = panel.firstChild.firstChild;
512 expect(checkStatActive(view, show)).toBe(true);
513 KorAP.vc.root().value("Hello tester").update();
514 expect(checkStatDisabled(view, show)).toBe(true);
515
516 //refresh corpus statistic
517 rlbutton = show.getElementsByClassName("refresh").item(0);
518 rlbutton.click();
519 view = panel.firstChild.firstChild;
520 expect(checkStatActive(view, show)).toBe(true);
521
522 KorAP.vc.root().key("author").update();
523 expect(checkStatDisabled(view, show)).toBe(true);
524
525
526 /*
527 * DocGroupRef change of value...
528 */
529 KorAP.vc = generateCorpusRef();
530 show = document.createElement('div');
531 show.appendChild(KorAP.vc.element());
532 panel = show.firstChild.lastChild.firstChild;
533 panel.lastChild.children[0].click();
534 view = panel.firstChild.firstChild;
535 expect(checkStatActive(view, show)).toBe(true);
536
537 KorAP.vc.root().ref("@anton/secondCorpus").update();
538 expect(checkStatDisabled(view, show)).toBe(true);
539 });
540
541
542 it('should not disable corpus statistic if docgroup definition is incomplete', function(){
543
544 KorAP.vc = generateCorpusDocGr();
545
546 //Show corpus statistic
547 let show = document.createElement('div');
548 show.appendChild(KorAP.vc.element());
549 let panel = show.firstChild.lastChild.firstChild;
550 panel.lastChild.children[0].click();
551 let view = panel.firstChild.firstChild;
552
553 expect(checkStatActive(view, show)).toBe(true);
554
555 KorAP._and.apply(KorAP.vc.root());
556
557 let andbuild = show.getElementsByClassName("builder");
558 expect(andbuild[0].firstChild.classList.contains('docGroup')).toBeTruthy();
559 expect(andbuild[0].firstChild.getAttribute("data-operation")).toEqual("and");
560 expect(checkStatActive(view, show)).toBe(true);
561 });
562
563
564 it('should not disable corpus statistic if doc/docref definition is incomplete', function(){
565
566 /*
567 * DOC incomplete
568 */
569 KorAP.vc = vcClass.create().fromJson();
570 expect(KorAP.vc.builder().firstChild.classList.contains('unspecified')).toBeTruthy();
571
572 // show vc builder and open corpus statistic
573 let show = document.createElement('div');
574 show.appendChild(KorAP.vc.element());
575 let panel = show.firstChild.lastChild.firstChild;
576 panel.lastChild.children[0].click();
577 let view = panel.firstChild.firstChild;
578
579 // corpus statistic should be shown and be up-to-date, reload button is not shown
580 expect(checkStatActive(view, show)).toBe(true);
581
582 // open the menu
583 KorAP.vc.builder().firstChild.firstChild.click();
584 KorAP._vcKeyMenu._prefix.add("author");
585 let prefElement = KorAP.vc.builder().querySelector('span.pref');
586 // add key 'author' to VC
587 prefElement.click();
588
589 expect(checkStatActive(view, show)).toBe(true);
590
591
592 /*
593 * DOCREF incomplete
594 */
595 KorAP.vc = vcClass.create().fromJson();
596 expect(KorAP.vc.builder().firstChild.classList.contains('unspecified')).toBeTruthy();
597
598 // show vc builder and open corpus statistic
599 show = document.createElement('div');
600 show.appendChild(KorAP.vc.element());
601 panel = show.firstChild.lastChild.firstChild;
602 panel.lastChild.children[0].click();
603 view = panel.firstChild.firstChild;
604 expect(checkStatActive(view, show)).toBe(true);
605
606 KorAP.vc.builder().firstChild.firstChild.click();
607 KorAP._vcKeyMenu._prefix.add("referTo");
608 prefElement = KorAP.vc.builder().querySelector('span.pref');
609 prefElement.click();
610 expect(checkStatActive(view, show)).toBe(true);
611 });
612 });
hebasta5e4d7542018-06-19 16:57:36 +0200613});