hebasta | 5e4d754 | 2018-06-19 16:57:36 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Test suite for corpus statistic |
| 3 | * |
| 4 | * @author Helge Stallkamp |
Akron | 18a99c0 | 2018-08-20 12:55:14 +0200 | [diff] [blame] | 5 | * @author Nils Diewald |
hebasta | 5e4d754 | 2018-06-19 16:57:36 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | |
hebasta | 6c8f09d | 2018-07-24 13:21:36 +0200 | [diff] [blame] | 9 | define(['vc', 'vc/statistic', 'view/corpstatv'], function(vcClass, statClass, corpStatVClass){ |
hebasta | 47d03a4 | 2018-06-25 10:31:58 +0200 | [diff] [blame] | 10 | |
| 11 | var json = { |
Akron | 18a99c0 | 2018-08-20 12:55:14 +0200 | [diff] [blame] | 12 | "@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 | }; |
hebasta | 5e4d754 | 2018-06-19 16:57:36 +0200 | [diff] [blame] | 68 | |
hebasta | 47d03a4 | 2018-06-25 10:31:58 +0200 | [diff] [blame] | 69 | var preDefinedStat={ |
Akron | 18a99c0 | 2018-08-20 12:55:14 +0200 | [diff] [blame] | 70 | "documents":12, |
| 71 | "tokens":2323, |
| 72 | "sentences":343434, |
| 73 | "paragraphs":45454545 |
hebasta | 47d03a4 | 2018-06-25 10:31:58 +0200 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | KorAP.API.getCorpStat = function(collQu, cb){ |
| 77 | return cb(preDefinedStat); |
Akron | 18a99c0 | 2018-08-20 12:55:14 +0200 | [diff] [blame] | 78 | }; |
| 79 | |
hebasta | 4c92eec | 2018-06-29 10:11:15 +0200 | [diff] [blame] | 80 | |
hebasta | 47d03a4 | 2018-06-25 10:31:58 +0200 | [diff] [blame] | 81 | describe('KorAP.CorpusStat', function(){ |
hebasta | 47d03a4 | 2018-06-25 10:31:58 +0200 | [diff] [blame] | 82 | |
hebasta | 5e4d754 | 2018-06-19 16:57:36 +0200 | [diff] [blame] | 83 | it('should be initiable', function(){ |
Akron | 18a99c0 | 2018-08-20 12:55:14 +0200 | [diff] [blame] | 84 | var stat = statClass.create(preDefinedStat); |
hebasta | 5e4d754 | 2018-06-19 16:57:36 +0200 | [diff] [blame] | 85 | expect( function() { statClass.create() }).toThrow(new Error("Missing parameter")); |
| 86 | }); |
| 87 | |
| 88 | |
Akron | 18a99c0 | 2018-08-20 12:55:14 +0200 | [diff] [blame] | 89 | it('should be parsed in a statistic view and displayed as HTML Description List', function(){ |
| 90 | var stat = statClass.create(preDefinedStat); |
| 91 | var descL = stat.element(); |
hebasta | 5e4d754 | 2018-06-19 16:57:36 +0200 | [diff] [blame] | 92 | expect(descL.tagName).toEqual('DL'); |
| 93 | expect(descL.children[0].tagName).toEqual('DIV'); |
| 94 | expect(descL.children[0].children[0].tagName).toEqual('DT'); |
| 95 | expect(descL.children[0].children[0].attributes[0].name).toEqual('title'); |
| 96 | expect(descL.children[0].children[1].tagName).toEqual('DD'); |
Akron | 18a99c0 | 2018-08-20 12:55:14 +0200 | [diff] [blame] | 97 | |
hebasta | 5e4d754 | 2018-06-19 16:57:36 +0200 | [diff] [blame] | 98 | expect(descL.children[0].children[0].firstChild.nodeValue).toEqual('documents'); |
| 99 | expect(descL.children[0].children[1].firstChild.nodeValue).toEqual('12'); |
| 100 | expect(descL.children[0].children[0].attributes[0].value).toEqual('documents'); |
Akron | 2f97912 | 2018-07-25 17:00:23 +0200 | [diff] [blame] | 101 | |
| 102 | expect(descL.children[1].children[0].firstChild.nodeValue).toEqual('tokens'); |
| 103 | expect(descL.children[1].children[1].firstChild.nodeValue).toEqual(new Number(2323).toLocaleString()); |
| 104 | expect(descL.children[1].children[0].attributes[0].value).toEqual('tokens'); |
| 105 | }); |
hebasta | 5e4d754 | 2018-06-19 16:57:36 +0200 | [diff] [blame] | 106 | |
hebasta | 47d03a4 | 2018-06-25 10:31:58 +0200 | [diff] [blame] | 107 | |
hebasta | 6c8f09d | 2018-07-24 13:21:36 +0200 | [diff] [blame] | 108 | it('should display corpus statistic after creating a corpus statistic view', function(){ |
Akron | 18a99c0 | 2018-08-20 12:55:14 +0200 | [diff] [blame] | 109 | var vc = vcClass.create([ |
| 110 | ['title', 'string'], |
| 111 | ['subTitle', 'string'], |
| 112 | ['pubDate', 'date'], |
| 113 | ['author', 'text'] |
| 114 | ]).fromJson(json); |
| 115 | |
| 116 | statView = corpStatVClass.create(vc); |
| 117 | //corpStatVClass.show(vc); |
| 118 | |
hebasta | 47d03a4 | 2018-06-25 10:31:58 +0200 | [diff] [blame] | 119 | var testDiv = document.createElement('div'); |
hebasta | 6c8f09d | 2018-07-24 13:21:36 +0200 | [diff] [blame] | 120 | testDiv.appendChild(statView.show()); |
| 121 | //statClass.showCorpStat(testDiv, vc); |
hebasta | 47d03a4 | 2018-06-25 10:31:58 +0200 | [diff] [blame] | 122 | |
| 123 | expect(testDiv.children[0].tagName).toEqual('DIV'); |
| 124 | expect(testDiv.children[0].getAttribute("class")).toEqual('stattable'); |
| 125 | expect(testDiv.children[0].children[0].tagName).toEqual('DL'); |
| 126 | expect(testDiv.children[0].children[0].children[0].children[0].firstChild.nodeValue).toEqual('documents'); |
| 127 | expect(testDiv.children[0].children[0].children[0].children[1].firstChild.nodeValue).toEqual('12'); |
hebasta | 47d03a4 | 2018-06-25 10:31:58 +0200 | [diff] [blame] | 128 | }); |
Akron | 18a99c0 | 2018-08-20 12:55:14 +0200 | [diff] [blame] | 129 | |
| 130 | it('should display the statistics button in a panel', function () { |
| 131 | var vc = vcClass.create([ |
| 132 | ['title', 'string'], |
| 133 | ['subTitle', 'string'], |
| 134 | ['pubDate', 'date'], |
| 135 | ['author', 'text'] |
| 136 | ]).fromJson(json); |
| 137 | |
| 138 | var show = document.createElement('div'); |
| 139 | |
| 140 | show.appendChild(vc.element()); |
| 141 | |
| 142 | var panel = show.firstChild.lastChild.firstChild; |
| 143 | expect(panel.classList.contains('panel')).toBeTruthy(); |
| 144 | expect(panel.classList.contains('vcinfo')).toBeTruthy(); |
| 145 | expect(panel.lastChild.classList.contains('button-group')).toBeTruthy(); |
| 146 | expect(panel.lastChild.classList.contains('vcinfo')).toBeTruthy(); |
| 147 | expect(panel.lastChild.children[0].tagName).toEqual('SPAN'); |
| 148 | expect(panel.lastChild.children[0].textContent).toEqual('Statistics'); |
| 149 | }); |
| 150 | |
| 151 | |
| 152 | it('should display the statistics button in a panel after VC modification', function () { |
| 153 | |
| 154 | var vc = vcClass.create([ |
| 155 | ['title', 'string'], |
| 156 | ['subTitle', 'string'], |
| 157 | ['pubDate', 'date'], |
| 158 | ['author', 'text'] |
| 159 | ]).fromJson(json); |
| 160 | |
| 161 | var show = document.createElement('div'); |
| 162 | |
| 163 | show.appendChild(vc.element()); |
| 164 | |
| 165 | expect(show.querySelector(".statistic").tagName).toEqual("SPAN"); |
| 166 | |
Akron | adab5e5 | 2018-08-20 13:50:53 +0200 | [diff] [blame] | 167 | var and = vc.builder().lastChild.firstChild; |
Akron | 18a99c0 | 2018-08-20 12:55:14 +0200 | [diff] [blame] | 168 | |
| 169 | // Click on and() in VC |
| 170 | and.click(); |
| 171 | |
| 172 | // Check that statistics is there |
| 173 | expect(show.querySelector(".statistic").tagName).toEqual("SPAN"); |
| 174 | }); |
| 175 | |
| 176 | |
| 177 | it('should display and hide corpus statistics view in the panel', function () { |
| 178 | |
| 179 | var vc = vcClass.create([ |
| 180 | ['title', 'string'], |
| 181 | ['subTitle', 'string'], |
| 182 | ['pubDate', 'date'], |
| 183 | ['author', 'text'] |
| 184 | ]).fromJson(json); |
| 185 | |
| 186 | var show = document.createElement('div'); |
| 187 | |
| 188 | show.appendChild(vc.element()); |
| 189 | |
| 190 | var panel = show.firstChild.lastChild.firstChild; |
| 191 | |
| 192 | // Show statistics |
| 193 | panel.lastChild.children[0].click(); |
| 194 | |
| 195 | // Statistics view |
| 196 | var viewE = panel.firstChild.firstChild; |
| 197 | |
| 198 | expect(viewE.tagName).toEqual("DIV"); |
| 199 | expect(viewE.classList.contains("view")).toBeTruthy(); |
| 200 | expect(viewE.classList.contains("vcstatistic")).toBeTruthy(); |
| 201 | |
| 202 | expect(viewE.firstChild.classList.contains("stattable")).toBeTruthy(); |
| 203 | expect(viewE.firstChild.firstChild.tagName).toEqual("DL"); |
| 204 | |
| 205 | // List of statistic values |
| 206 | var dl = viewE.firstChild.firstChild.firstChild; |
| 207 | expect(dl.firstChild.tagName).toEqual("DT"); |
| 208 | expect(dl.firstChild.textContent).toEqual("documents"); |
| 209 | expect(dl.lastChild.tagName).toEqual("DD"); |
| 210 | expect(dl.lastChild.textContent).toEqual("12"); |
| 211 | |
| 212 | expect(viewE.children.length).toEqual(2); |
| 213 | |
| 214 | expect(viewE.lastChild.classList.contains("button-group")).toBeTruthy(); |
| 215 | expect(viewE.lastChild.children.length).toEqual(1); |
| 216 | expect(viewE.lastChild.firstChild.tagName).toEqual("SPAN"); |
| 217 | expect(viewE.lastChild.firstChild.textContent).toEqual("Close"); |
| 218 | |
| 219 | // Close view |
| 220 | viewE.lastChild.firstChild.firstChild.click(); |
| 221 | |
| 222 | // Is gone |
| 223 | expect(panel.firstChild.children.length).toEqual(0); |
| 224 | |
| 225 | // Show statistics |
| 226 | panel.lastChild.children[0].click(); |
| 227 | |
| 228 | // Is there |
| 229 | expect(panel.firstChild.children.length).toEqual(1); |
| 230 | |
| 231 | // Only show once |
| 232 | panel.lastChild.children[0].click(); |
| 233 | expect(panel.firstChild.children.length).toEqual(1); |
| 234 | }); |
| 235 | }); |
hebasta | 5e4d754 | 2018-06-19 16:57:36 +0200 | [diff] [blame] | 236 | }); |