Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 1 | // Helper method for building factories |
| 2 | buildFactory = function (objClass, defaults) { |
| 3 | return { |
| 4 | create : function (overwrites) { |
| 5 | var newObj = {}; |
| 6 | for (var prop in defaults) { |
| 7 | newObj[prop] = defaults[prop]; |
| 8 | }; |
| 9 | for (var prop in overwrites) { |
| 10 | newObj[prop] = overwrites[prop]; |
| 11 | }; |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 12 | if (objClass === KorAP.VirtualCollection) |
| 13 | return objClass.render(newObj); |
| 14 | else |
| 15 | return objClass.create().fromJson(newObj); |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 16 | } |
| 17 | } |
| 18 | }; |
| 19 | |
| 20 | describe('KorAP.Doc', function () { |
| 21 | |
| 22 | // Create example factories |
| 23 | var stringFactory = buildFactory(KorAP.Doc, { |
| 24 | "key" : "author", |
| 25 | "value" : "Max Birkendale", |
| 26 | "@type" : "korap:doc" |
| 27 | }); |
| 28 | |
| 29 | // Create example factories |
| 30 | var dateFactory = buildFactory(KorAP.Doc, { |
| 31 | "key" : "pubDate", |
| 32 | "type" : "type:date", |
| 33 | "match" : "match:eq", |
| 34 | "value" : "2014-11-05", |
| 35 | "@type" : "korap:doc" |
| 36 | }); |
| 37 | |
| 38 | // Create example factories |
| 39 | var regexFactory = buildFactory(KorAP.Doc, { |
| 40 | "key" : "title", |
| 41 | "type" : "type:regex", |
| 42 | "value" : "[^b]ee.+?", |
| 43 | "@type" : "korap:doc" |
| 44 | }); |
| 45 | |
| 46 | it('should be initializable', function () { |
| 47 | var doc = KorAP.Doc.create(); |
| 48 | expect(doc.matchop()).toEqual('eq'); |
| 49 | expect(doc.key()).toBeUndefined(); |
| 50 | expect(doc.value()).toBeUndefined(); |
| 51 | expect(doc.type()).toEqual("string"); |
| 52 | }); |
| 53 | |
| 54 | it('should be definable', function () { |
| 55 | |
| 56 | // Empty doc |
| 57 | var doc = KorAP.Doc.create(); |
| 58 | |
| 59 | // Set values |
| 60 | doc.key("title"); |
| 61 | doc.value("Der alte Mann"); |
| 62 | expect(doc.matchop()).toEqual('eq'); |
| 63 | expect(doc.key()).toEqual("title"); |
| 64 | expect(doc.type()).toEqual("string"); |
| 65 | expect(doc.value()).toEqual("Der alte Mann"); |
| 66 | }); |
| 67 | |
| 68 | |
| 69 | it('should deserialize JSON-LD string', function () { |
| 70 | var doc; |
| 71 | |
| 72 | // String default |
| 73 | doc = stringFactory.create(); |
| 74 | expect(doc.matchop()).toEqual('eq'); |
| 75 | expect(doc.key()).toEqual("author"); |
| 76 | expect(doc.type()).toEqual("string"); |
| 77 | expect(doc.value()).toEqual("Max Birkendale"); |
| 78 | |
| 79 | // No valid string |
| 80 | doc = stringFactory.create({ |
| 81 | value : undefined |
| 82 | }); |
| 83 | expect(doc).toBeUndefined(); |
| 84 | |
| 85 | // No valid string |
| 86 | doc = stringFactory.create({ |
| 87 | value : { "foo" : "bar" } |
| 88 | }); |
| 89 | expect(doc).toBeUndefined(); |
| 90 | |
| 91 | // Change match type |
| 92 | doc = stringFactory.create({ |
| 93 | "match" : "match:ne" |
| 94 | }); |
| 95 | |
| 96 | expect(doc.matchop()).toEqual('ne'); |
| 97 | expect(doc.key()).toEqual("author"); |
| 98 | expect(doc.type()).toEqual("string"); |
| 99 | expect(doc.value()).toEqual("Max Birkendale"); |
| 100 | |
| 101 | |
| 102 | // Invalid match type |
| 103 | doc = stringFactory.create({ |
| 104 | "match" : { "foo" : "bar" } |
| 105 | }); |
| 106 | expect(doc).toBeUndefined(); |
| 107 | }); |
| 108 | |
| 109 | it('should deserialize JSON-LD regex', function () { |
| 110 | var doc = regexFactory.create(); |
| 111 | expect(doc.key()).toEqual("title"); |
| 112 | expect(doc.type()).toEqual("regex"); |
| 113 | expect(doc.value()).toEqual("[^b]ee.+?"); |
| 114 | expect(doc.matchop()).toEqual('eq'); |
| 115 | |
| 116 | // change matcher |
| 117 | doc = regexFactory.create({ |
| 118 | match : "match:ne" |
| 119 | }); |
| 120 | expect(doc.matchop()).toEqual('ne'); |
| 121 | |
| 122 | // Invalid matcher |
| 123 | doc = regexFactory.create({ |
| 124 | match : "match:chook" |
| 125 | }); |
| 126 | expect(doc).toBeUndefined(); |
| 127 | |
| 128 | // Invalid regex |
| 129 | doc = regexFactory.create({ |
| 130 | value : "[^b" |
| 131 | }); |
| 132 | expect(doc).toBeUndefined(); |
| 133 | }); |
| 134 | |
| 135 | it('should deserialize JSON-LD date', function () { |
| 136 | |
| 137 | // Normal date |
| 138 | doc = dateFactory.create({}); |
| 139 | |
| 140 | expect(doc.matchop()).toEqual('eq'); |
| 141 | expect(doc.key()).toEqual("pubDate"); |
| 142 | expect(doc.type()).toEqual("date"); |
| 143 | expect(doc.value()).toEqual("2014-11-05"); |
| 144 | |
| 145 | // Short date 1 |
| 146 | doc = dateFactory.create({ |
| 147 | "value" : "2014-11" |
| 148 | }); |
| 149 | |
| 150 | expect(doc.matchop()).toEqual('eq'); |
| 151 | expect(doc.key()).toEqual("pubDate"); |
| 152 | expect(doc.type()).toEqual("date"); |
| 153 | expect(doc.value()).toEqual("2014-11"); |
| 154 | |
| 155 | // Short date 2 |
| 156 | doc = dateFactory.create({ |
| 157 | "value" : "2014" |
| 158 | }); |
| 159 | |
| 160 | expect(doc.matchop()).toEqual('eq'); |
| 161 | expect(doc.key()).toEqual("pubDate"); |
| 162 | expect(doc.type()).toEqual("date"); |
| 163 | expect(doc.value()).toEqual("2014"); |
| 164 | |
| 165 | // Invalid date! |
| 166 | doc = dateFactory.create({ |
| 167 | "value" : "2014-11-050" |
| 168 | }); |
| 169 | expect(doc).toBeUndefined(); |
| 170 | |
| 171 | // Invalid matcher! |
| 172 | doc = dateFactory.create({ |
| 173 | "match" : "match:ne", |
| 174 | }); |
| 175 | expect(doc).toBeUndefined(); |
| 176 | }); |
| 177 | |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 178 | it('should be serializale to JSON', function () { |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 179 | |
| 180 | // Empty doc |
| 181 | var doc = KorAP.Doc.create(); |
| 182 | expect(doc.toJson()).toEqual(jasmine.any(Object)); |
| 183 | |
| 184 | // Serialize string |
| 185 | doc = stringFactory.create(); |
| 186 | expect(doc.toJson()).toEqual(jasmine.objectContaining({ |
| 187 | "@type" : "korap:doc", |
| 188 | "type" : "type:string", |
| 189 | "key" : "author", |
| 190 | "value" : "Max Birkendale", |
| 191 | "match" : "match:eq" |
| 192 | })); |
| 193 | |
| 194 | // Serialize regex |
| 195 | doc = regexFactory.create(); |
| 196 | expect(doc.toJson()).toEqual(jasmine.objectContaining({ |
| 197 | "@type" : "korap:doc", |
| 198 | "type" : "type:regex", |
| 199 | "value" : "[^b]ee.+?", |
| 200 | "match" : "match:eq", |
| 201 | "key" : 'title' |
| 202 | })); |
| 203 | |
| 204 | doc = regexFactory.create({ |
| 205 | match: "match:ne" |
| 206 | }); |
| 207 | expect(doc.toJson()).toEqual(jasmine.objectContaining({ |
| 208 | "@type" : "korap:doc", |
| 209 | "type" : "type:regex", |
| 210 | "value" : "[^b]ee.+?", |
| 211 | "match" : "match:ne", |
| 212 | "key" : 'title' |
| 213 | })); |
| 214 | |
| 215 | doc = dateFactory.create(); |
| 216 | expect(doc.toJson()).toEqual(jasmine.objectContaining({ |
| 217 | "@type" : "korap:doc", |
| 218 | "type" : "type:date", |
| 219 | "value" : "2014-11-05", |
| 220 | "match" : "match:eq", |
| 221 | "key" : 'pubDate' |
| 222 | })); |
| 223 | |
| 224 | doc = dateFactory.create({ |
| 225 | value : "2014" |
| 226 | }); |
| 227 | expect(doc.toJson()).toEqual(jasmine.objectContaining({ |
| 228 | "@type" : "korap:doc", |
| 229 | "type" : "type:date", |
| 230 | "value" : "2014", |
| 231 | "match" : "match:eq", |
| 232 | "key" : 'pubDate' |
| 233 | })); |
| 234 | }); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 235 | |
| 236 | it('should be serializale to String', function () { |
| 237 | |
| 238 | // Empty doc |
| 239 | var doc = KorAP.Doc.create(); |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 240 | expect(doc.toQuery()).toEqual(""); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 241 | |
| 242 | // Serialize string |
| 243 | doc = stringFactory.create(); |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 244 | expect(doc.toQuery()).toEqual('author = "Max Birkendale"'); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 245 | |
| 246 | // Serialize string with quotes |
| 247 | doc = stringFactory.create({ "value" : 'Max "Der Coole" Birkendate'}); |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 248 | expect(doc.toQuery()).toEqual('author = "Max \\"Der Coole\\" Birkendate"'); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 249 | |
| 250 | // Serialize regex |
| 251 | doc = regexFactory.create(); |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 252 | expect(doc.toQuery()).toEqual('title = /[^b]ee.+?/'); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 253 | |
| 254 | doc = regexFactory.create({ |
| 255 | match: "match:ne" |
| 256 | }); |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 257 | expect(doc.toQuery()).toEqual('title != /[^b]ee.+?/'); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 258 | |
| 259 | doc = dateFactory.create(); |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 260 | expect(doc.toQuery()).toEqual('pubDate in 2014-11-05'); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 261 | |
| 262 | doc = dateFactory.create({ |
| 263 | value : "2014" |
| 264 | }); |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 265 | expect(doc.toQuery()).toEqual('pubDate in 2014'); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 266 | }); |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 267 | }); |
| 268 | |
| 269 | |
| 270 | describe('KorAP.DocGroup', function () { |
| 271 | // Create example factories |
| 272 | var docFactory = buildFactory( |
| 273 | KorAP.Doc, |
| 274 | { |
| 275 | "@type" : "korap:doc", |
| 276 | "match":"match:eq", |
| 277 | "key" : "author", |
| 278 | "value" : "Max Birkendale" |
| 279 | } |
| 280 | ); |
| 281 | |
| 282 | var docGroupFactory = buildFactory( |
| 283 | KorAP.DocGroup, { |
| 284 | "@type" : "korap:docGroup", |
| 285 | "operation" : "operation:and", |
| 286 | "operands" : [ |
| 287 | docFactory.create().toJson(), |
| 288 | docFactory.create({ |
| 289 | "key" : "pubDate", |
| 290 | "type" : "type:date", |
| 291 | "value" : "2014-12-05" |
| 292 | }).toJson() |
| 293 | ] |
| 294 | }); |
| 295 | |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 296 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 297 | it('should be initializable', function () { |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 298 | // Create empty group |
| 299 | var docGroup = KorAP.DocGroup.create(); |
| 300 | expect(docGroup.operation()).toEqual('and'); |
| 301 | |
| 302 | // Create empty group |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 303 | docGroup = KorAP.DocGroup.create(); |
| 304 | docGroup.operation('or'); |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 305 | expect(docGroup.operation()).toEqual('or'); |
| 306 | }); |
| 307 | |
| 308 | it('should be definable', function () { |
| 309 | |
| 310 | // Empty group |
| 311 | var docGroup = KorAP.DocGroup.create(); |
| 312 | expect(docGroup.operation()).toEqual('and'); |
| 313 | |
| 314 | // Set values |
| 315 | docGroup.operation("or"); |
| 316 | expect(docGroup.operation()).toEqual('or'); |
| 317 | |
| 318 | // Set invalid values |
| 319 | docGroup.operation("hui"); |
| 320 | expect(docGroup.operation()).toEqual('or'); |
| 321 | }); |
| 322 | |
| 323 | it('should be deserializable', function () { |
| 324 | var docGroup = docGroupFactory.create(); |
| 325 | expect(docGroup.operation()).toEqual("and"); |
| 326 | expect(docGroup.operands().length).toEqual(2); |
| 327 | |
| 328 | var op1 = docGroup.getOperand(0); |
| 329 | expect(op1.type()).toEqual("string"); |
| 330 | expect(op1.key()).toEqual("author"); |
| 331 | expect(op1.value()).toEqual("Max Birkendale"); |
| 332 | expect(op1.matchop()).toEqual("eq"); |
| 333 | |
| 334 | var op2 = docGroup.getOperand(1); |
| 335 | expect(op2.type()).toEqual("date"); |
| 336 | expect(op2.key()).toEqual("pubDate"); |
| 337 | expect(op2.value()).toEqual("2014-12-05"); |
| 338 | expect(op2.matchop()).toEqual("eq"); |
| 339 | |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 340 | // Append empty group |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 341 | var newGroup = docGroup.append(KorAP.DocGroup.create()); |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 342 | newGroup.operation('or'); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 343 | newGroup.append(docFactory.create()); |
| 344 | newGroup.append(docFactory.create({ |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 345 | "type" : "type:regex", |
| 346 | "key" : "title", |
| 347 | "value" : "^e.+?$", |
| 348 | "match" : "match:ne" |
| 349 | })); |
| 350 | |
| 351 | expect(docGroup.operation()).toEqual("and"); |
| 352 | expect(docGroup.operands().length).toEqual(3); |
| 353 | |
| 354 | var op1 = docGroup.getOperand(0); |
| 355 | expect(op1.ldType()).toEqual("doc"); |
| 356 | expect(op1.type()).toEqual("string"); |
| 357 | expect(op1.key()).toEqual("author"); |
| 358 | expect(op1.value()).toEqual("Max Birkendale"); |
| 359 | expect(op1.matchop()).toEqual("eq"); |
| 360 | |
| 361 | var op2 = docGroup.getOperand(1); |
| 362 | expect(op2.ldType()).toEqual("doc"); |
| 363 | expect(op2.type()).toEqual("date"); |
| 364 | expect(op2.key()).toEqual("pubDate"); |
| 365 | expect(op2.value()).toEqual("2014-12-05"); |
| 366 | expect(op2.matchop()).toEqual("eq"); |
| 367 | |
| 368 | var op3 = docGroup.getOperand(2); |
| 369 | expect(op3.ldType()).toEqual("docGroup"); |
| 370 | expect(op3.operation()).toEqual("or"); |
| 371 | |
| 372 | var op4 = op3.getOperand(0); |
| 373 | expect(op4.ldType()).toEqual("doc"); |
| 374 | expect(op4.type()).toEqual("string"); |
| 375 | expect(op4.key()).toEqual("author"); |
| 376 | expect(op4.value()).toEqual("Max Birkendale"); |
| 377 | expect(op4.matchop()).toEqual("eq"); |
| 378 | |
| 379 | var op5 = op3.getOperand(1); |
| 380 | expect(op5.ldType()).toEqual("doc"); |
| 381 | expect(op5.type()).toEqual("regex"); |
| 382 | expect(op5.key()).toEqual("title"); |
| 383 | expect(op5.value()).toEqual("^e.+?$"); |
| 384 | expect(op5.matchop()).toEqual("ne"); |
| 385 | }); |
| 386 | |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 387 | it('should be serializable to JSON', function () { |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 388 | var docGroup = docGroupFactory.create(); |
| 389 | |
| 390 | expect(docGroup.toJson()).toEqual(jasmine.objectContaining({ |
| 391 | "@type" : "korap:docGroup", |
| 392 | "operation" : "operation:and", |
| 393 | "operands" : [ |
| 394 | { |
| 395 | "@type": 'korap:doc', |
| 396 | "key" : 'author', |
| 397 | "match": 'match:eq', |
| 398 | "value": 'Max Birkendale', |
| 399 | "type": 'type:string' |
| 400 | }, |
| 401 | { |
| 402 | "@type": 'korap:doc', |
| 403 | "key": 'pubDate', |
| 404 | "match": 'match:eq', |
| 405 | "value": '2014-12-05', |
| 406 | "type": 'type:date' |
| 407 | } |
| 408 | ] |
| 409 | })); |
| 410 | }); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 411 | |
| 412 | it('should be serializable to String', function () { |
| 413 | var docGroup = docGroupFactory.create(); |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 414 | expect(docGroup.toQuery()).toEqual('author = "Max Birkendale" & pubDate in 2014-12-05'); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 415 | |
| 416 | docGroup = docGroupFactory.create({ |
| 417 | "@type" : "korap:docGroup", |
| 418 | "operation" : "operation:or", |
| 419 | "operands" : [ |
| 420 | { |
| 421 | "@type": 'korap:doc', |
| 422 | "key" : 'author', |
| 423 | "match": 'match:eq', |
| 424 | "value": 'Max Birkendale', |
| 425 | "type": 'type:string' |
| 426 | }, |
| 427 | { |
| 428 | "@type" : "korap:docGroup", |
| 429 | "operation" : "operation:and", |
| 430 | "operands" : [ |
| 431 | { |
| 432 | "@type": 'korap:doc', |
| 433 | "key": 'pubDate', |
| 434 | "match": 'match:geq', |
| 435 | "value": '2014-05-12', |
| 436 | "type": 'type:date' |
| 437 | }, |
| 438 | { |
| 439 | "@type": 'korap:doc', |
| 440 | "key": 'pubDate', |
| 441 | "match": 'match:leq', |
| 442 | "value": '2014-12-05', |
| 443 | "type": 'type:date' |
| 444 | }, |
| 445 | { |
| 446 | "@type": 'korap:doc', |
| 447 | "key": 'foo', |
| 448 | "match": 'match:ne', |
| 449 | "value": '[a]?bar', |
| 450 | "type": 'type:regex' |
| 451 | } |
| 452 | ] |
| 453 | } |
| 454 | ] |
| 455 | }); |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 456 | expect(docGroup.toQuery()).toEqual( |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 457 | 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05 & foo != /[a]?bar/)' |
| 458 | ); |
| 459 | }); |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 460 | }); |
| 461 | |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 462 | describe('KorAP.UnspecifiedDoc', function () { |
| 463 | it('should be initializable', function () { |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 464 | var doc = KorAP.UnspecifiedDoc.create(); |
| 465 | var docElement = doc.element(); |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 466 | expect(docElement.getAttribute('class')).toEqual('doc unspecified'); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 467 | expect(docElement.firstChild.firstChild.data).toEqual('⋯'); |
| 468 | expect(docElement.lastChild.getAttribute('class')).toEqual('operators'); |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 469 | expect(doc.toQuery()).toEqual('⋯'); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 470 | |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 471 | // Only removable |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 472 | expect(docElement.lastChild.children.length).toEqual(0); |
| 473 | }); |
| 474 | |
| 475 | it('should be removable, when no root', function () { |
| 476 | var docGroup = KorAP.DocGroup.create(); |
| 477 | docGroup.operation('or'); |
| 478 | expect(docGroup.operation()).toEqual('or'); |
| 479 | |
| 480 | docGroup.append({ |
| 481 | "@type": 'korap:doc', |
| 482 | "key": 'pubDate', |
| 483 | "match": 'match:eq', |
| 484 | "value": '2014-12-05', |
| 485 | "type": 'type:date' |
| 486 | }); |
| 487 | |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 488 | // Add unspecified object |
| 489 | docGroup.append(); |
| 490 | |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 491 | expect(docGroup.element().getAttribute('class')).toEqual('docGroup'); |
| 492 | expect(docGroup.element().children[0].getAttribute('class')).toEqual('doc'); |
| 493 | |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 494 | var unspec = docGroup.element().children[1]; |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 495 | expect(unspec.getAttribute('class')).toEqual('doc unspecified'); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 496 | |
| 497 | // Removable |
| 498 | expect(unspec.lastChild.children.length).toEqual(1); |
| 499 | expect(unspec.lastChild.children[0].getAttribute('class')).toEqual('delete'); |
| 500 | }); |
| 501 | }); |
| 502 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 503 | describe('KorAP.Doc element', function () { |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 504 | it('should be initializable', function () { |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 505 | var docElement = KorAP.Doc.create(undefined, { |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 506 | "@type" : "korap:doc", |
| 507 | "key":"Titel", |
| 508 | "value":"Baum", |
| 509 | "match":"match:eq" |
| 510 | }); |
| 511 | expect(docElement.key()).toEqual('Titel'); |
| 512 | expect(docElement.matchop()).toEqual('eq'); |
| 513 | expect(docElement.value()).toEqual('Baum'); |
| 514 | |
| 515 | var docE = docElement.element(); |
| 516 | expect(docE.children[0].firstChild.data).toEqual('Titel'); |
| 517 | expect(docE.children[1].firstChild.data).toEqual('eq'); |
| 518 | expect(docE.children[1].getAttribute('data-type')).toEqual('string'); |
| 519 | expect(docE.children[2].firstChild.data).toEqual('Baum'); |
| 520 | expect(docE.children[2].getAttribute('data-type')).toEqual('string'); |
| 521 | |
| 522 | expect(docElement.toJson()).toEqual(jasmine.objectContaining({ |
| 523 | "@type" : "korap:doc", |
| 524 | "key":"Titel", |
| 525 | "value":"Baum", |
| 526 | "match":"match:eq" |
| 527 | })); |
| 528 | }); |
| 529 | }); |
| 530 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 531 | describe('KorAP.DocGroup element', function () { |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 532 | it('should be initializable', function () { |
| 533 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 534 | var docGroup = KorAP.DocGroup.create(undefined, { |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 535 | "@type" : "korap:docGroup", |
| 536 | "operation" : "operation:and", |
| 537 | "operands" : [ |
| 538 | { |
| 539 | "@type": 'korap:doc', |
| 540 | "key" : 'author', |
| 541 | "match": 'match:eq', |
| 542 | "value": 'Max Birkendale', |
| 543 | "type": 'type:string' |
| 544 | }, |
| 545 | { |
| 546 | "@type": 'korap:doc', |
| 547 | "key": 'pubDate', |
| 548 | "match": 'match:eq', |
| 549 | "value": '2014-12-05', |
| 550 | "type": 'type:date' |
| 551 | } |
| 552 | ] |
| 553 | }); |
| 554 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 555 | expect(docGroup.operation()).toEqual('and'); |
| 556 | var e = docGroup.element(); |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 557 | expect(e.getAttribute('class')).toEqual('docGroup'); |
| 558 | expect(e.getAttribute('data-operation')).toEqual('and'); |
| 559 | |
| 560 | var first = e.children[0]; |
| 561 | expect(first.getAttribute('class')).toEqual('doc'); |
| 562 | expect(first.children[0].getAttribute('class')).toEqual('key'); |
| 563 | expect(first.children[1].getAttribute('class')).toEqual('match'); |
| 564 | expect(first.children[2].getAttribute('class')).toEqual('value'); |
| 565 | expect(first.children[2].getAttribute('data-type')).toEqual('string'); |
| 566 | expect(first.children[0].firstChild.data).toEqual('author'); |
| 567 | expect(first.children[1].firstChild.data).toEqual('eq'); |
| 568 | expect(first.children[2].firstChild.data).toEqual('Max Birkendale'); |
| 569 | |
| 570 | var second = e.children[1]; |
| 571 | expect(second.getAttribute('class')).toEqual('doc'); |
| 572 | expect(second.children[0].getAttribute('class')).toEqual('key'); |
| 573 | expect(second.children[1].getAttribute('class')).toEqual('match'); |
| 574 | expect(second.children[2].getAttribute('class')).toEqual('value'); |
| 575 | expect(second.children[2].getAttribute('data-type')).toEqual('date'); |
| 576 | expect(second.children[0].firstChild.data).toEqual('pubDate'); |
| 577 | expect(second.children[1].firstChild.data).toEqual('eq'); |
| 578 | expect(second.children[2].firstChild.data).toEqual('2014-12-05'); |
| 579 | |
| 580 | }); |
| 581 | |
| 582 | it('should be deserializable with nested groups', function () { |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 583 | var docGroup = KorAP.DocGroup.create(undefined, { |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 584 | "@type" : "korap:docGroup", |
| 585 | "operation" : "operation:or", |
| 586 | "operands" : [ |
| 587 | { |
| 588 | "@type": 'korap:doc', |
| 589 | "key" : 'author', |
| 590 | "match": 'match:eq', |
| 591 | "value": 'Max Birkendale', |
| 592 | "type": 'type:string' |
| 593 | }, |
| 594 | { |
| 595 | "@type" : "korap:docGroup", |
| 596 | "operation" : "operation:and", |
| 597 | "operands" : [ |
| 598 | { |
| 599 | "@type": 'korap:doc', |
| 600 | "key": 'pubDate', |
| 601 | "match": 'match:geq', |
| 602 | "value": '2014-05-12', |
| 603 | "type": 'type:date' |
| 604 | }, |
| 605 | { |
| 606 | "@type": 'korap:doc', |
| 607 | "key": 'pubDate', |
| 608 | "match": 'match:leq', |
| 609 | "value": '2014-12-05', |
| 610 | "type": 'type:date' |
| 611 | } |
| 612 | ] |
| 613 | } |
| 614 | ] |
| 615 | }); |
| 616 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 617 | expect(docGroup.operation()).toEqual('or'); |
| 618 | var e = docGroup.element(); |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 619 | expect(e.getAttribute('class')).toEqual('docGroup'); |
| 620 | expect(e.getAttribute('data-operation')).toEqual('or'); |
| 621 | |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 622 | expect(e.children[0].getAttribute('class')).toEqual('doc'); |
| 623 | var docop = e.children[0].lastChild; |
| 624 | expect(docop.getAttribute('class')).toEqual('operators'); |
| 625 | expect(docop.children[0].getAttribute('class')).toEqual('and'); |
| 626 | expect(docop.children[1].getAttribute('class')).toEqual('or'); |
| 627 | expect(docop.children[2].getAttribute('class')).toEqual('delete'); |
| 628 | |
| 629 | expect(e.children[1].getAttribute('class')).toEqual('docGroup'); |
| 630 | expect(e.children[1].getAttribute('data-operation')).toEqual('and'); |
| 631 | |
| 632 | // This and-operation can be "or"ed or "delete"d |
| 633 | var secop = e.children[1].children[2]; |
| 634 | expect(secop.getAttribute('class')).toEqual('operators'); |
| 635 | expect(secop.children[0].getAttribute('class')).toEqual('or'); |
| 636 | expect(secop.children[1].getAttribute('class')).toEqual('delete'); |
| 637 | |
| 638 | // This or-operation can be "and"ed or "delete"d |
| 639 | expect(e.children[2].getAttribute('class')).toEqual('operators'); |
| 640 | expect(e.lastChild.getAttribute('class')).toEqual('operators'); |
| 641 | expect(e.lastChild.children[0].getAttribute('class')).toEqual('and'); |
| 642 | expect(e.lastChild.children[1].getAttribute('class')).toEqual('delete'); |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 643 | |
| 644 | }); |
| 645 | }); |
| 646 | |
| 647 | describe('KorAP.VirtualCollection', function () { |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 648 | |
| 649 | var simpleGroupFactory = buildFactory(KorAP.DocGroup, { |
| 650 | "@type" : "korap:docGroup", |
| 651 | "operation" : "operation:and", |
| 652 | "operands" : [ |
| 653 | { |
| 654 | "@type": 'korap:doc', |
| 655 | "key" : 'author', |
| 656 | "match": 'match:eq', |
| 657 | "value": 'Max Birkendale', |
| 658 | "type": 'type:string' |
| 659 | }, |
| 660 | { |
| 661 | "@type": 'korap:doc', |
| 662 | "key": 'pubDate', |
| 663 | "match": 'match:eq', |
| 664 | "value": '2014-12-05', |
| 665 | "type": 'type:date' |
| 666 | } |
| 667 | ] |
| 668 | }); |
| 669 | |
| 670 | var nestedGroupFactory = buildFactory(KorAP.VirtualCollection, { |
| 671 | "@type" : "korap:docGroup", |
| 672 | "operation" : "operation:or", |
| 673 | "operands" : [ |
| 674 | { |
| 675 | "@type": 'korap:doc', |
| 676 | "key" : 'author', |
| 677 | "match": 'match:eq', |
| 678 | "value": 'Max Birkendale', |
| 679 | "type": 'type:string' |
| 680 | }, |
| 681 | { |
| 682 | "@type" : "korap:docGroup", |
| 683 | "operation" : "operation:and", |
| 684 | "operands" : [ |
| 685 | { |
| 686 | "@type": 'korap:doc', |
| 687 | "key": 'pubDate', |
| 688 | "match": 'match:geq', |
| 689 | "value": '2014-05-12', |
| 690 | "type": 'type:date' |
| 691 | }, |
| 692 | { |
| 693 | "@type": 'korap:doc', |
| 694 | "key": 'pubDate', |
| 695 | "match": 'match:leq', |
| 696 | "value": '2014-12-05', |
| 697 | "type": 'type:date' |
| 698 | } |
| 699 | ] |
| 700 | } |
| 701 | ] |
| 702 | }); |
| 703 | |
| 704 | var flatGroupFactory = buildFactory(KorAP.VirtualCollection, { |
| 705 | "@type" : "korap:docGroup", |
| 706 | "operation" : "operation:and", |
| 707 | "operands" : [ |
| 708 | { |
| 709 | "@type": 'korap:doc', |
| 710 | "key": 'pubDate', |
| 711 | "match": 'match:geq', |
| 712 | "value": '2014-05-12', |
| 713 | "type": 'type:date' |
| 714 | }, |
| 715 | { |
| 716 | "@type": 'korap:doc', |
| 717 | "key": 'pubDate', |
| 718 | "match": 'match:leq', |
| 719 | "value": '2014-12-05', |
| 720 | "type": 'type:date' |
| 721 | }, |
| 722 | { |
| 723 | "@type": 'korap:doc', |
| 724 | "key": 'foo', |
| 725 | "match": 'match:eq', |
| 726 | "value": 'bar', |
| 727 | "type": 'type:string' |
| 728 | } |
| 729 | ] |
| 730 | }); |
Nils Diewald | 4019bd2 | 2015-01-08 19:57:50 +0000 | [diff] [blame] | 731 | |
| 732 | var demoFactory = buildFactory(KorAP.VirtualCollection, { |
| 733 | "@type":"korap:docGroup", |
| 734 | "operation":"operation:or", |
| 735 | "operands":[ |
| 736 | { |
| 737 | "@type":"korap:docGroup", |
| 738 | "operation":"operation:and", |
| 739 | "operands":[ |
| 740 | { |
| 741 | "@type":"korap:doc", |
| 742 | "key":"Titel", |
| 743 | "value":"Baum", |
| 744 | "match":"match:eq" |
| 745 | }, |
| 746 | { |
| 747 | "@type":"korap:doc", |
| 748 | "key":"Veröffentlichungsort", |
| 749 | "value":"hihi", |
| 750 | "match":"match:eq" |
| 751 | }, |
| 752 | { |
| 753 | "@type":"korap:docGroup", |
| 754 | "operation":"operation:or", |
| 755 | "operands":[ |
| 756 | { |
| 757 | "@type":"korap:doc", |
| 758 | "key":"Titel", |
| 759 | "value":"Baum", |
| 760 | "match":"match:eq" |
| 761 | }, |
| 762 | { |
| 763 | "@type":"korap:doc", |
| 764 | "key":"Veröffentlichungsort", |
| 765 | "value":"hihi", |
| 766 | "match":"match:eq" |
| 767 | } |
| 768 | ] |
| 769 | } |
| 770 | ] |
| 771 | }, |
| 772 | { |
| 773 | "@type":"korap:doc", |
| 774 | "key":"Untertitel", |
| 775 | "value":"huhu", |
| 776 | "match":"match:eq" |
| 777 | } |
| 778 | ] |
| 779 | }); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 780 | |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 781 | it('should be initializable', function () { |
| 782 | var vc = KorAP.VirtualCollection.render(); |
| 783 | expect(vc.element().getAttribute('class')).toEqual('vc'); |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 784 | expect(vc.root().element().getAttribute('class')).toEqual('doc unspecified'); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 785 | |
| 786 | // Not removable |
| 787 | expect(vc.root().element().lastChild.children.length).toEqual(0); |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 788 | }); |
| 789 | |
| 790 | it('should be based on a doc', function () { |
| 791 | var vc = KorAP.VirtualCollection.render({ |
| 792 | "@type" : "korap:doc", |
| 793 | "key":"Titel", |
| 794 | "value":"Baum", |
| 795 | "match":"match:eq" |
| 796 | }); |
| 797 | |
| 798 | expect(vc.element().getAttribute('class')).toEqual('vc'); |
| 799 | expect(vc.root().element().getAttribute('class')).toEqual('doc'); |
| 800 | expect(vc.root().key()).toEqual('Titel'); |
| 801 | expect(vc.root().value()).toEqual('Baum'); |
| 802 | expect(vc.root().matchop()).toEqual('eq'); |
| 803 | |
| 804 | var docE = vc.root().element(); |
| 805 | expect(docE.children[0].firstChild.data).toEqual('Titel'); |
| 806 | expect(docE.children[1].firstChild.data).toEqual('eq'); |
| 807 | expect(docE.children[1].getAttribute('data-type')).toEqual('string'); |
| 808 | expect(docE.children[2].firstChild.data).toEqual('Baum'); |
| 809 | expect(docE.children[2].getAttribute('data-type')).toEqual('string'); |
| 810 | }); |
| 811 | |
| 812 | it('should be based on a docGroup', function () { |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 813 | var vc = KorAP.VirtualCollection.render(simpleGroupFactory.create().toJson()); |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 814 | |
| 815 | expect(vc.element().getAttribute('class')).toEqual('vc'); |
| 816 | expect(vc.root().element().getAttribute('class')).toEqual('docGroup'); |
| 817 | expect(vc.root().operation()).toEqual('and'); |
| 818 | |
| 819 | var docGroup = vc.root(); |
| 820 | |
| 821 | var first = docGroup.getOperand(0); |
| 822 | expect(first.key()).toEqual('author'); |
| 823 | expect(first.value()).toEqual('Max Birkendale'); |
| 824 | expect(first.matchop()).toEqual('eq'); |
| 825 | |
| 826 | var second = docGroup.getOperand(1); |
| 827 | expect(second.key()).toEqual('pubDate'); |
| 828 | expect(second.value()).toEqual('2014-12-05'); |
| 829 | expect(second.matchop()).toEqual('eq'); |
| 830 | }); |
| 831 | |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 832 | |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 833 | it('should be based on a nested docGroup', function () { |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 834 | var vc = nestedGroupFactory.create(); |
| 835 | |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 836 | expect(vc.element().getAttribute('class')).toEqual('vc'); |
| 837 | expect(vc.element().firstChild.getAttribute('class')).toEqual('docGroup'); |
| 838 | expect(vc.element().firstChild.children[0].getAttribute('class')).toEqual('doc'); |
| 839 | var dg = vc.element().firstChild.children[1]; |
| 840 | expect(dg.getAttribute('class')).toEqual('docGroup'); |
| 841 | expect(dg.children[0].getAttribute('class')).toEqual('doc'); |
| 842 | expect(dg.children[1].getAttribute('class')).toEqual('doc'); |
| 843 | expect(dg.children[2].getAttribute('class')).toEqual('operators'); |
| 844 | expect(vc.element().firstChild.children[2].getAttribute('class')).toEqual('operators'); |
| 845 | }); |
| 846 | |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 847 | it('should be modifiable by deletion in flat docGroups', function () { |
| 848 | var vc = flatGroupFactory.create(); |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 849 | var docGroup = vc.root(); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 850 | |
| 851 | expect(docGroup.element().getAttribute('class')).toEqual('docGroup'); |
| 852 | |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 853 | var doc = docGroup.getOperand(1); |
| 854 | expect(doc.key()).toEqual("pubDate"); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 855 | expect(doc.value()).toEqual("2014-12-05"); |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 856 | |
| 857 | // Remove operand 1 |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 858 | expect(docGroup.delOperand(doc).update()).not.toBeUndefined(); |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 859 | expect(doc._element).toEqual(undefined); |
| 860 | |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 861 | doc = docGroup.getOperand(1); |
| 862 | expect(doc.key()).toEqual("foo"); |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 863 | |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 864 | // Remove operand 1 |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 865 | expect(docGroup.delOperand(doc).update()).not.toBeUndefined(); |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 866 | expect(doc._element).toEqual(undefined); |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 867 | |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 868 | // Only one operand left ... |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 869 | expect(docGroup.getOperand(1)).toBeUndefined(); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 870 | // ... but there shouldn't be a group anymore at all! |
| 871 | expect(docGroup.getOperand(0)).toBeUndefined(); |
| 872 | |
| 873 | var obj = vc.root(); |
| 874 | expect(obj.ldType()).toEqual("doc"); |
| 875 | expect(obj.key()).toEqual("pubDate"); |
| 876 | expect(obj.value()).toEqual("2014-05-12"); |
| 877 | |
| 878 | expect(obj.element().getAttribute('class')).toEqual('doc'); |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 879 | }); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 880 | |
| 881 | |
| 882 | it('should be modifiable by deletion in nested docGroups (root case)', function () { |
| 883 | var vc = nestedGroupFactory.create(); |
| 884 | |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 885 | expect(vc.toQuery()).toEqual( |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 886 | 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05)' |
| 887 | ); |
| 888 | |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 889 | var docGroup = vc.root(); |
| 890 | expect(docGroup.ldType()).toEqual("docGroup"); |
| 891 | expect(docGroup.operation()).toEqual("or"); |
| 892 | |
| 893 | var doc = docGroup.getOperand(0); |
| 894 | expect(doc.key()).toEqual("author"); |
| 895 | expect(doc.value()).toEqual("Max Birkendale"); |
| 896 | |
| 897 | docGroup = docGroup.getOperand(1); |
| 898 | expect(docGroup.operation()).toEqual("and"); |
| 899 | |
| 900 | doc = docGroup.getOperand(0); |
| 901 | expect(doc.key()).toEqual("pubDate"); |
| 902 | expect(doc.matchop()).toEqual("geq"); |
| 903 | expect(doc.value()).toEqual("2014-05-12"); |
| 904 | expect(doc.type()).toEqual("date"); |
| 905 | |
| 906 | doc = docGroup.getOperand(1); |
| 907 | expect(doc.key()).toEqual("pubDate"); |
| 908 | expect(doc.matchop()).toEqual("leq"); |
| 909 | expect(doc.value()).toEqual("2014-12-05"); |
| 910 | expect(doc.type()).toEqual("date"); |
| 911 | |
| 912 | // Remove first operand so everything becomes root |
| 913 | expect( |
| 914 | vc.root().delOperand( |
| 915 | vc.root().getOperand(0) |
| 916 | ).update().ldType() |
| 917 | ).toEqual("docGroup"); |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 918 | |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 919 | expect(vc.root().ldType()).toEqual("docGroup"); |
| 920 | expect(vc.root().operation()).toEqual("and"); |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 921 | expect(vc.root().getOperand(0).ldType()).toEqual("doc"); |
| 922 | |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 923 | expect(vc.toQuery()).toEqual( |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 924 | 'pubDate since 2014-05-12 & pubDate until 2014-12-05' |
| 925 | ); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 926 | }); |
| 927 | |
| 928 | it('should be modifiable by deletion in nested docGroups (resolve group case)', function () { |
| 929 | var vc = nestedGroupFactory.create(); |
| 930 | |
| 931 | // Get nested group |
| 932 | var firstGroup = vc.root().getOperand(1); |
| 933 | firstGroup.append(simpleGroupFactory.create({ "operation" : "operation:or" })); |
| 934 | |
| 935 | // Structur is now: |
| 936 | // or(doc, and(doc, doc, or(doc, doc))) |
| 937 | |
| 938 | // Get nested or in and |
| 939 | var orGroup = vc.root().getOperand(1).getOperand(2); |
| 940 | expect(orGroup.ldType()).toEqual("docGroup"); |
| 941 | expect(orGroup.operation()).toEqual("or"); |
| 942 | |
| 943 | // Remove |
| 944 | // Structur is now: |
| 945 | // or(doc, and(doc, doc, doc))) |
| 946 | expect(orGroup.delOperand(orGroup.getOperand(0)).update().operation()).toEqual("and"); |
| 947 | expect(vc.root().getOperand(1).operands().length).toEqual(3); |
| 948 | }); |
| 949 | |
| 950 | it('should be modifiable by deletion in nested docGroups (identical group case)', function () { |
| 951 | var vc = nestedGroupFactory.create(); |
| 952 | |
| 953 | // Get nested group |
| 954 | var firstGroup = vc.root().getOperand(1); |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 955 | firstGroup.append(simpleGroupFactory.create({ |
| 956 | "operation" : "operation:or" |
| 957 | })); |
| 958 | var oldAuthor = firstGroup.getOperand(2).getOperand(0); |
| 959 | oldAuthor.key("title"); |
| 960 | oldAuthor.value("Der Birnbaum"); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 961 | |
| 962 | // Structur is now: |
| 963 | // or(doc, and(doc, doc, or(doc, doc))) |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 964 | expect(vc.toQuery()).toEqual( |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 965 | 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05 & (title = "Der Birnbaum" | pubDate in 2014-12-05))' |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 966 | ); |
| 967 | |
| 968 | var andGroup = vc.root().getOperand(1); |
| 969 | |
| 970 | // Get leading docs in and |
| 971 | var doc1 = andGroup.getOperand(0); |
| 972 | expect(doc1.ldType()).toEqual("doc"); |
| 973 | expect(doc1.value()).toEqual("2014-05-12"); |
| 974 | var doc2 = andGroup.getOperand(1); |
| 975 | expect(doc2.ldType()).toEqual("doc"); |
| 976 | expect(doc2.value()).toEqual("2014-12-05"); |
| 977 | |
| 978 | // Remove 2 |
| 979 | expect(andGroup.delOperand(doc2).update().operation()).toEqual("and"); |
| 980 | // Structur is now: |
| 981 | // or(doc, and(doc, or(doc, doc))) |
| 982 | |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 983 | expect(vc.toQuery()).toEqual( |
| 984 | 'author = "Max Birkendale"' + |
| 985 | ' | (pubDate since 2014-05-12 & ' + |
| 986 | '(title = "Der Birnbaum" | pubDate in 2014-12-05))' |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 987 | ); |
| 988 | |
| 989 | |
| 990 | // Remove 1 |
| 991 | expect(andGroup.delOperand(doc1).update().operation()).toEqual("or"); |
| 992 | // Structur is now: |
| 993 | // or(doc, doc, doc) |
| 994 | |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 995 | expect(vc.toQuery()).toEqual( |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 996 | 'author = "Max Birkendale" | title = "Der Birnbaum" | pubDate in 2014-12-05' |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 997 | ); |
| 998 | }); |
Nils Diewald | 4019bd2 | 2015-01-08 19:57:50 +0000 | [diff] [blame] | 999 | |
| 1000 | it('should be reducible to unspecification', function () { |
| 1001 | var vc = demoFactory.create(); |
| 1002 | |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 1003 | expect(vc.toQuery()).toEqual(vc.root().toQuery()); |
Nils Diewald | 4019bd2 | 2015-01-08 19:57:50 +0000 | [diff] [blame] | 1004 | |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 1005 | expect(vc.toQuery()).toEqual( |
| 1006 | '(Titel = "Baum" & Veröffentlichungsort = "hihi" & ' + |
| 1007 | '(Titel = "Baum" | Veröffentlichungsort = "hihi")) ' + |
| 1008 | '| Untertitel = "huhu"'); |
Nils Diewald | 4019bd2 | 2015-01-08 19:57:50 +0000 | [diff] [blame] | 1009 | |
| 1010 | expect(vc.root().element().lastChild.children[0].firstChild.nodeValue).toEqual('and'); |
| 1011 | expect(vc.root().element().lastChild.children[1].firstChild.nodeValue).toEqual('×'); |
Nils Diewald | 4019bd2 | 2015-01-08 19:57:50 +0000 | [diff] [blame] | 1012 | expect(vc.root().delOperand(vc.root().getOperand(0)).update()).not.toBeUndefined(); |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 1013 | expect(vc.toQuery()).toEqual('Untertitel = "huhu"'); |
Nils Diewald | 4019bd2 | 2015-01-08 19:57:50 +0000 | [diff] [blame] | 1014 | |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 1015 | var lc = vc.root().element().lastChild; |
| 1016 | expect(lc.children[0].firstChild.nodeValue).toEqual('and'); |
| 1017 | expect(lc.children[1].firstChild.nodeValue).toEqual('or'); |
| 1018 | expect(lc.children[2].firstChild.nodeValue).toEqual('×'); |
Nils Diewald | 4019bd2 | 2015-01-08 19:57:50 +0000 | [diff] [blame] | 1019 | |
| 1020 | // Clean everything |
| 1021 | vc.clean(); |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame^] | 1022 | expect(vc.toQuery()).toEqual('⋯'); |
Nils Diewald | 4019bd2 | 2015-01-08 19:57:50 +0000 | [diff] [blame] | 1023 | }); |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 1024 | }); |
| 1025 | |
| 1026 | describe('KorAP.Operators', function () { |
| 1027 | it('should be initializable', function () { |
| 1028 | var op = KorAP.Operators.create(true, false, false); |
| 1029 | expect(op.and()).toBeTruthy(); |
| 1030 | expect(op.or()).not.toBeTruthy(); |
| 1031 | expect(op.del()).not.toBeTruthy(); |
| 1032 | |
| 1033 | op.and(false); |
| 1034 | expect(op.and()).not.toBeTruthy(); |
| 1035 | expect(op.or()).not.toBeTruthy(); |
| 1036 | expect(op.del()).not.toBeTruthy(); |
| 1037 | |
| 1038 | op.or(true); |
| 1039 | op.del(true); |
| 1040 | expect(op.and()).not.toBeTruthy(); |
| 1041 | expect(op.or()).toBeTruthy(); |
| 1042 | expect(op.del()).toBeTruthy(); |
| 1043 | |
| 1044 | var e = op.element(); |
| 1045 | expect(e.getAttribute('class')).toEqual('operators'); |
| 1046 | expect(e.children[0].getAttribute('class')).toEqual('or'); |
| 1047 | expect(e.children[0].firstChild.data).toEqual('or'); |
| 1048 | expect(e.children[1].getAttribute('class')).toEqual('delete'); |
| 1049 | expect(e.children[1].firstChild.data).toEqual('×'); |
| 1050 | |
| 1051 | op.and(true); |
| 1052 | op.del(false); |
| 1053 | op.update(); |
| 1054 | |
| 1055 | e = op.element(); |
| 1056 | expect(e.getAttribute('class')).toEqual('operators'); |
| 1057 | expect(e.children[0].getAttribute('class')).toEqual('and'); |
| 1058 | expect(e.children[0].firstChild.data).toEqual('and'); |
| 1059 | expect(e.children[1].getAttribute('class')).toEqual('or'); |
| 1060 | expect(e.children[1].firstChild.data).toEqual('or'); |
| 1061 | }); |
| 1062 | }); |