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 | }; |
| 12 | return objClass.create().fromJson(newObj); |
| 13 | } |
| 14 | } |
| 15 | }; |
| 16 | |
| 17 | describe('KorAP.Doc', function () { |
| 18 | |
| 19 | // Create example factories |
| 20 | var stringFactory = buildFactory(KorAP.Doc, { |
| 21 | "key" : "author", |
| 22 | "value" : "Max Birkendale", |
| 23 | "@type" : "korap:doc" |
| 24 | }); |
| 25 | |
| 26 | // Create example factories |
| 27 | var dateFactory = buildFactory(KorAP.Doc, { |
| 28 | "key" : "pubDate", |
| 29 | "type" : "type:date", |
| 30 | "match" : "match:eq", |
| 31 | "value" : "2014-11-05", |
| 32 | "@type" : "korap:doc" |
| 33 | }); |
| 34 | |
| 35 | // Create example factories |
| 36 | var regexFactory = buildFactory(KorAP.Doc, { |
| 37 | "key" : "title", |
| 38 | "type" : "type:regex", |
| 39 | "value" : "[^b]ee.+?", |
| 40 | "@type" : "korap:doc" |
| 41 | }); |
| 42 | |
| 43 | it('should be initializable', function () { |
| 44 | var doc = KorAP.Doc.create(); |
| 45 | expect(doc.matchop()).toEqual('eq'); |
| 46 | expect(doc.key()).toBeUndefined(); |
| 47 | expect(doc.value()).toBeUndefined(); |
| 48 | expect(doc.type()).toEqual("string"); |
| 49 | }); |
| 50 | |
| 51 | it('should be definable', function () { |
| 52 | |
| 53 | // Empty doc |
| 54 | var doc = KorAP.Doc.create(); |
| 55 | |
| 56 | // Set values |
| 57 | doc.key("title"); |
| 58 | doc.value("Der alte Mann"); |
| 59 | expect(doc.matchop()).toEqual('eq'); |
| 60 | expect(doc.key()).toEqual("title"); |
| 61 | expect(doc.type()).toEqual("string"); |
| 62 | expect(doc.value()).toEqual("Der alte Mann"); |
| 63 | }); |
| 64 | |
| 65 | |
| 66 | it('should deserialize JSON-LD string', function () { |
| 67 | var doc; |
| 68 | |
| 69 | // String default |
| 70 | doc = stringFactory.create(); |
| 71 | expect(doc.matchop()).toEqual('eq'); |
| 72 | expect(doc.key()).toEqual("author"); |
| 73 | expect(doc.type()).toEqual("string"); |
| 74 | expect(doc.value()).toEqual("Max Birkendale"); |
| 75 | |
| 76 | // No valid string |
| 77 | doc = stringFactory.create({ |
| 78 | value : undefined |
| 79 | }); |
| 80 | expect(doc).toBeUndefined(); |
| 81 | |
| 82 | // No valid string |
| 83 | doc = stringFactory.create({ |
| 84 | value : { "foo" : "bar" } |
| 85 | }); |
| 86 | expect(doc).toBeUndefined(); |
| 87 | |
| 88 | // Change match type |
| 89 | doc = stringFactory.create({ |
| 90 | "match" : "match:ne" |
| 91 | }); |
| 92 | |
| 93 | expect(doc.matchop()).toEqual('ne'); |
| 94 | expect(doc.key()).toEqual("author"); |
| 95 | expect(doc.type()).toEqual("string"); |
| 96 | expect(doc.value()).toEqual("Max Birkendale"); |
| 97 | |
| 98 | |
| 99 | // Invalid match type |
| 100 | doc = stringFactory.create({ |
| 101 | "match" : { "foo" : "bar" } |
| 102 | }); |
| 103 | expect(doc).toBeUndefined(); |
| 104 | }); |
| 105 | |
| 106 | it('should deserialize JSON-LD regex', function () { |
| 107 | var doc = regexFactory.create(); |
| 108 | expect(doc.key()).toEqual("title"); |
| 109 | expect(doc.type()).toEqual("regex"); |
| 110 | expect(doc.value()).toEqual("[^b]ee.+?"); |
| 111 | expect(doc.matchop()).toEqual('eq'); |
| 112 | |
| 113 | // change matcher |
| 114 | doc = regexFactory.create({ |
| 115 | match : "match:ne" |
| 116 | }); |
| 117 | expect(doc.matchop()).toEqual('ne'); |
| 118 | |
| 119 | // Invalid matcher |
| 120 | doc = regexFactory.create({ |
| 121 | match : "match:chook" |
| 122 | }); |
| 123 | expect(doc).toBeUndefined(); |
| 124 | |
| 125 | // Invalid regex |
| 126 | doc = regexFactory.create({ |
| 127 | value : "[^b" |
| 128 | }); |
| 129 | expect(doc).toBeUndefined(); |
| 130 | }); |
| 131 | |
| 132 | it('should deserialize JSON-LD date', function () { |
| 133 | |
| 134 | // Normal date |
| 135 | doc = dateFactory.create({}); |
| 136 | |
| 137 | expect(doc.matchop()).toEqual('eq'); |
| 138 | expect(doc.key()).toEqual("pubDate"); |
| 139 | expect(doc.type()).toEqual("date"); |
| 140 | expect(doc.value()).toEqual("2014-11-05"); |
| 141 | |
| 142 | // Short date 1 |
| 143 | doc = dateFactory.create({ |
| 144 | "value" : "2014-11" |
| 145 | }); |
| 146 | |
| 147 | expect(doc.matchop()).toEqual('eq'); |
| 148 | expect(doc.key()).toEqual("pubDate"); |
| 149 | expect(doc.type()).toEqual("date"); |
| 150 | expect(doc.value()).toEqual("2014-11"); |
| 151 | |
| 152 | // Short date 2 |
| 153 | doc = dateFactory.create({ |
| 154 | "value" : "2014" |
| 155 | }); |
| 156 | |
| 157 | expect(doc.matchop()).toEqual('eq'); |
| 158 | expect(doc.key()).toEqual("pubDate"); |
| 159 | expect(doc.type()).toEqual("date"); |
| 160 | expect(doc.value()).toEqual("2014"); |
| 161 | |
| 162 | // Invalid date! |
| 163 | doc = dateFactory.create({ |
| 164 | "value" : "2014-11-050" |
| 165 | }); |
| 166 | expect(doc).toBeUndefined(); |
| 167 | |
| 168 | // Invalid matcher! |
| 169 | doc = dateFactory.create({ |
| 170 | "match" : "match:ne", |
| 171 | }); |
| 172 | expect(doc).toBeUndefined(); |
| 173 | }); |
| 174 | |
| 175 | it('should be serializale', function () { |
| 176 | |
| 177 | // Empty doc |
| 178 | var doc = KorAP.Doc.create(); |
| 179 | expect(doc.toJson()).toEqual(jasmine.any(Object)); |
| 180 | |
| 181 | // Serialize string |
| 182 | doc = stringFactory.create(); |
| 183 | expect(doc.toJson()).toEqual(jasmine.objectContaining({ |
| 184 | "@type" : "korap:doc", |
| 185 | "type" : "type:string", |
| 186 | "key" : "author", |
| 187 | "value" : "Max Birkendale", |
| 188 | "match" : "match:eq" |
| 189 | })); |
| 190 | |
| 191 | // Serialize regex |
| 192 | doc = regexFactory.create(); |
| 193 | expect(doc.toJson()).toEqual(jasmine.objectContaining({ |
| 194 | "@type" : "korap:doc", |
| 195 | "type" : "type:regex", |
| 196 | "value" : "[^b]ee.+?", |
| 197 | "match" : "match:eq", |
| 198 | "key" : 'title' |
| 199 | })); |
| 200 | |
| 201 | doc = regexFactory.create({ |
| 202 | match: "match:ne" |
| 203 | }); |
| 204 | expect(doc.toJson()).toEqual(jasmine.objectContaining({ |
| 205 | "@type" : "korap:doc", |
| 206 | "type" : "type:regex", |
| 207 | "value" : "[^b]ee.+?", |
| 208 | "match" : "match:ne", |
| 209 | "key" : 'title' |
| 210 | })); |
| 211 | |
| 212 | doc = dateFactory.create(); |
| 213 | expect(doc.toJson()).toEqual(jasmine.objectContaining({ |
| 214 | "@type" : "korap:doc", |
| 215 | "type" : "type:date", |
| 216 | "value" : "2014-11-05", |
| 217 | "match" : "match:eq", |
| 218 | "key" : 'pubDate' |
| 219 | })); |
| 220 | |
| 221 | doc = dateFactory.create({ |
| 222 | value : "2014" |
| 223 | }); |
| 224 | expect(doc.toJson()).toEqual(jasmine.objectContaining({ |
| 225 | "@type" : "korap:doc", |
| 226 | "type" : "type:date", |
| 227 | "value" : "2014", |
| 228 | "match" : "match:eq", |
| 229 | "key" : 'pubDate' |
| 230 | })); |
| 231 | }); |
| 232 | }); |
| 233 | |
| 234 | |
| 235 | describe('KorAP.DocGroup', function () { |
| 236 | // Create example factories |
| 237 | var docFactory = buildFactory( |
| 238 | KorAP.Doc, |
| 239 | { |
| 240 | "@type" : "korap:doc", |
| 241 | "match":"match:eq", |
| 242 | "key" : "author", |
| 243 | "value" : "Max Birkendale" |
| 244 | } |
| 245 | ); |
| 246 | |
| 247 | var docGroupFactory = buildFactory( |
| 248 | KorAP.DocGroup, { |
| 249 | "@type" : "korap:docGroup", |
| 250 | "operation" : "operation:and", |
| 251 | "operands" : [ |
| 252 | docFactory.create().toJson(), |
| 253 | docFactory.create({ |
| 254 | "key" : "pubDate", |
| 255 | "type" : "type:date", |
| 256 | "value" : "2014-12-05" |
| 257 | }).toJson() |
| 258 | ] |
| 259 | }); |
| 260 | |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 261 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 262 | it('should be initializable', function () { |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 263 | // Create empty group |
| 264 | var docGroup = KorAP.DocGroup.create(); |
| 265 | expect(docGroup.operation()).toEqual('and'); |
| 266 | |
| 267 | // Create empty group |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 268 | docGroup = KorAP.DocGroup.create(); |
| 269 | docGroup.operation('or'); |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 270 | expect(docGroup.operation()).toEqual('or'); |
| 271 | }); |
| 272 | |
| 273 | it('should be definable', function () { |
| 274 | |
| 275 | // Empty group |
| 276 | var docGroup = KorAP.DocGroup.create(); |
| 277 | expect(docGroup.operation()).toEqual('and'); |
| 278 | |
| 279 | // Set values |
| 280 | docGroup.operation("or"); |
| 281 | expect(docGroup.operation()).toEqual('or'); |
| 282 | |
| 283 | // Set invalid values |
| 284 | docGroup.operation("hui"); |
| 285 | expect(docGroup.operation()).toEqual('or'); |
| 286 | }); |
| 287 | |
| 288 | it('should be deserializable', function () { |
| 289 | var docGroup = docGroupFactory.create(); |
| 290 | expect(docGroup.operation()).toEqual("and"); |
| 291 | expect(docGroup.operands().length).toEqual(2); |
| 292 | |
| 293 | var op1 = docGroup.getOperand(0); |
| 294 | expect(op1.type()).toEqual("string"); |
| 295 | expect(op1.key()).toEqual("author"); |
| 296 | expect(op1.value()).toEqual("Max Birkendale"); |
| 297 | expect(op1.matchop()).toEqual("eq"); |
| 298 | |
| 299 | var op2 = docGroup.getOperand(1); |
| 300 | expect(op2.type()).toEqual("date"); |
| 301 | expect(op2.key()).toEqual("pubDate"); |
| 302 | expect(op2.value()).toEqual("2014-12-05"); |
| 303 | expect(op2.matchop()).toEqual("eq"); |
| 304 | |
| 305 | // Create empty group |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 306 | var newGroup = docGroup.append(KorAP.DocGroup.create()); |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 307 | newGroup.operation('or'); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 308 | newGroup.append(docFactory.create()); |
| 309 | newGroup.append(docFactory.create({ |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 310 | "type" : "type:regex", |
| 311 | "key" : "title", |
| 312 | "value" : "^e.+?$", |
| 313 | "match" : "match:ne" |
| 314 | })); |
| 315 | |
| 316 | expect(docGroup.operation()).toEqual("and"); |
| 317 | expect(docGroup.operands().length).toEqual(3); |
| 318 | |
| 319 | var op1 = docGroup.getOperand(0); |
| 320 | expect(op1.ldType()).toEqual("doc"); |
| 321 | expect(op1.type()).toEqual("string"); |
| 322 | expect(op1.key()).toEqual("author"); |
| 323 | expect(op1.value()).toEqual("Max Birkendale"); |
| 324 | expect(op1.matchop()).toEqual("eq"); |
| 325 | |
| 326 | var op2 = docGroup.getOperand(1); |
| 327 | expect(op2.ldType()).toEqual("doc"); |
| 328 | expect(op2.type()).toEqual("date"); |
| 329 | expect(op2.key()).toEqual("pubDate"); |
| 330 | expect(op2.value()).toEqual("2014-12-05"); |
| 331 | expect(op2.matchop()).toEqual("eq"); |
| 332 | |
| 333 | var op3 = docGroup.getOperand(2); |
| 334 | expect(op3.ldType()).toEqual("docGroup"); |
| 335 | expect(op3.operation()).toEqual("or"); |
| 336 | |
| 337 | var op4 = op3.getOperand(0); |
| 338 | expect(op4.ldType()).toEqual("doc"); |
| 339 | expect(op4.type()).toEqual("string"); |
| 340 | expect(op4.key()).toEqual("author"); |
| 341 | expect(op4.value()).toEqual("Max Birkendale"); |
| 342 | expect(op4.matchop()).toEqual("eq"); |
| 343 | |
| 344 | var op5 = op3.getOperand(1); |
| 345 | expect(op5.ldType()).toEqual("doc"); |
| 346 | expect(op5.type()).toEqual("regex"); |
| 347 | expect(op5.key()).toEqual("title"); |
| 348 | expect(op5.value()).toEqual("^e.+?$"); |
| 349 | expect(op5.matchop()).toEqual("ne"); |
| 350 | }); |
| 351 | |
| 352 | it('should be serializable', function () { |
| 353 | var docGroup = docGroupFactory.create(); |
| 354 | |
| 355 | expect(docGroup.toJson()).toEqual(jasmine.objectContaining({ |
| 356 | "@type" : "korap:docGroup", |
| 357 | "operation" : "operation:and", |
| 358 | "operands" : [ |
| 359 | { |
| 360 | "@type": 'korap:doc', |
| 361 | "key" : 'author', |
| 362 | "match": 'match:eq', |
| 363 | "value": 'Max Birkendale', |
| 364 | "type": 'type:string' |
| 365 | }, |
| 366 | { |
| 367 | "@type": 'korap:doc', |
| 368 | "key": 'pubDate', |
| 369 | "match": 'match:eq', |
| 370 | "value": '2014-12-05', |
| 371 | "type": 'type:date' |
| 372 | } |
| 373 | ] |
| 374 | })); |
| 375 | }); |
| 376 | }); |
| 377 | |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 378 | describe('KorAP.UnspecifiedDoc', function () { |
| 379 | it('should be initializable', function () { |
| 380 | var docElement = KorAP.UnspecifiedDoc.create().element(); |
| 381 | expect(docElement.getAttribute('class')).toEqual('unspecified'); |
| 382 | expect(docElement.firstChild.firstChild.data).toEqual('⋯'); |
| 383 | expect(docElement.lastChild.getAttribute('class')).toEqual('operators'); |
| 384 | |
| 385 | // Not removable |
| 386 | expect(docElement.lastChild.children.length).toEqual(0); |
| 387 | }); |
| 388 | |
| 389 | it('should be removable, when no root', function () { |
| 390 | var docGroup = KorAP.DocGroup.create(); |
| 391 | docGroup.operation('or'); |
| 392 | expect(docGroup.operation()).toEqual('or'); |
| 393 | |
| 394 | docGroup.append({ |
| 395 | "@type": 'korap:doc', |
| 396 | "key": 'pubDate', |
| 397 | "match": 'match:eq', |
| 398 | "value": '2014-12-05', |
| 399 | "type": 'type:date' |
| 400 | }); |
| 401 | |
| 402 | expect(docGroup.element().getAttribute('class')).toEqual('docGroup'); |
| 403 | expect(docGroup.element().children[0].getAttribute('class')).toEqual('doc'); |
| 404 | |
| 405 | // Add unspecified object |
| 406 | docGroup.append(); |
| 407 | var unspec = docGroup.element().children[1]; |
| 408 | expect(unspec.getAttribute('class')).toEqual('unspecified'); |
| 409 | |
| 410 | // Removable |
| 411 | expect(unspec.lastChild.children.length).toEqual(1); |
| 412 | expect(unspec.lastChild.children[0].getAttribute('class')).toEqual('delete'); |
| 413 | }); |
| 414 | }); |
| 415 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 416 | describe('KorAP.Doc element', function () { |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 417 | it('should be initializable', function () { |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 418 | var docElement = KorAP.Doc.create(undefined, { |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 419 | "@type" : "korap:doc", |
| 420 | "key":"Titel", |
| 421 | "value":"Baum", |
| 422 | "match":"match:eq" |
| 423 | }); |
| 424 | expect(docElement.key()).toEqual('Titel'); |
| 425 | expect(docElement.matchop()).toEqual('eq'); |
| 426 | expect(docElement.value()).toEqual('Baum'); |
| 427 | |
| 428 | var docE = docElement.element(); |
| 429 | expect(docE.children[0].firstChild.data).toEqual('Titel'); |
| 430 | expect(docE.children[1].firstChild.data).toEqual('eq'); |
| 431 | expect(docE.children[1].getAttribute('data-type')).toEqual('string'); |
| 432 | expect(docE.children[2].firstChild.data).toEqual('Baum'); |
| 433 | expect(docE.children[2].getAttribute('data-type')).toEqual('string'); |
| 434 | |
| 435 | expect(docElement.toJson()).toEqual(jasmine.objectContaining({ |
| 436 | "@type" : "korap:doc", |
| 437 | "key":"Titel", |
| 438 | "value":"Baum", |
| 439 | "match":"match:eq" |
| 440 | })); |
| 441 | }); |
| 442 | }); |
| 443 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 444 | describe('KorAP.DocGroup element', function () { |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 445 | it('should be initializable', function () { |
| 446 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 447 | var docGroup = KorAP.DocGroup.create(undefined, { |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 448 | "@type" : "korap:docGroup", |
| 449 | "operation" : "operation:and", |
| 450 | "operands" : [ |
| 451 | { |
| 452 | "@type": 'korap:doc', |
| 453 | "key" : 'author', |
| 454 | "match": 'match:eq', |
| 455 | "value": 'Max Birkendale', |
| 456 | "type": 'type:string' |
| 457 | }, |
| 458 | { |
| 459 | "@type": 'korap:doc', |
| 460 | "key": 'pubDate', |
| 461 | "match": 'match:eq', |
| 462 | "value": '2014-12-05', |
| 463 | "type": 'type:date' |
| 464 | } |
| 465 | ] |
| 466 | }); |
| 467 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 468 | expect(docGroup.operation()).toEqual('and'); |
| 469 | var e = docGroup.element(); |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 470 | expect(e.getAttribute('class')).toEqual('docGroup'); |
| 471 | expect(e.getAttribute('data-operation')).toEqual('and'); |
| 472 | |
| 473 | var first = e.children[0]; |
| 474 | expect(first.getAttribute('class')).toEqual('doc'); |
| 475 | expect(first.children[0].getAttribute('class')).toEqual('key'); |
| 476 | expect(first.children[1].getAttribute('class')).toEqual('match'); |
| 477 | expect(first.children[2].getAttribute('class')).toEqual('value'); |
| 478 | expect(first.children[2].getAttribute('data-type')).toEqual('string'); |
| 479 | expect(first.children[0].firstChild.data).toEqual('author'); |
| 480 | expect(first.children[1].firstChild.data).toEqual('eq'); |
| 481 | expect(first.children[2].firstChild.data).toEqual('Max Birkendale'); |
| 482 | |
| 483 | var second = e.children[1]; |
| 484 | expect(second.getAttribute('class')).toEqual('doc'); |
| 485 | expect(second.children[0].getAttribute('class')).toEqual('key'); |
| 486 | expect(second.children[1].getAttribute('class')).toEqual('match'); |
| 487 | expect(second.children[2].getAttribute('class')).toEqual('value'); |
| 488 | expect(second.children[2].getAttribute('data-type')).toEqual('date'); |
| 489 | expect(second.children[0].firstChild.data).toEqual('pubDate'); |
| 490 | expect(second.children[1].firstChild.data).toEqual('eq'); |
| 491 | expect(second.children[2].firstChild.data).toEqual('2014-12-05'); |
| 492 | |
| 493 | }); |
| 494 | |
| 495 | it('should be deserializable with nested groups', function () { |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 496 | var docGroup = KorAP.DocGroup.create(undefined, { |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 497 | "@type" : "korap:docGroup", |
| 498 | "operation" : "operation:or", |
| 499 | "operands" : [ |
| 500 | { |
| 501 | "@type": 'korap:doc', |
| 502 | "key" : 'author', |
| 503 | "match": 'match:eq', |
| 504 | "value": 'Max Birkendale', |
| 505 | "type": 'type:string' |
| 506 | }, |
| 507 | { |
| 508 | "@type" : "korap:docGroup", |
| 509 | "operation" : "operation:and", |
| 510 | "operands" : [ |
| 511 | { |
| 512 | "@type": 'korap:doc', |
| 513 | "key": 'pubDate', |
| 514 | "match": 'match:geq', |
| 515 | "value": '2014-05-12', |
| 516 | "type": 'type:date' |
| 517 | }, |
| 518 | { |
| 519 | "@type": 'korap:doc', |
| 520 | "key": 'pubDate', |
| 521 | "match": 'match:leq', |
| 522 | "value": '2014-12-05', |
| 523 | "type": 'type:date' |
| 524 | } |
| 525 | ] |
| 526 | } |
| 527 | ] |
| 528 | }); |
| 529 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 530 | expect(docGroup.operation()).toEqual('or'); |
| 531 | var e = docGroup.element(); |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 532 | expect(e.getAttribute('class')).toEqual('docGroup'); |
| 533 | expect(e.getAttribute('data-operation')).toEqual('or'); |
| 534 | |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 535 | expect(e.children[0].getAttribute('class')).toEqual('doc'); |
| 536 | var docop = e.children[0].lastChild; |
| 537 | expect(docop.getAttribute('class')).toEqual('operators'); |
| 538 | expect(docop.children[0].getAttribute('class')).toEqual('and'); |
| 539 | expect(docop.children[1].getAttribute('class')).toEqual('or'); |
| 540 | expect(docop.children[2].getAttribute('class')).toEqual('delete'); |
| 541 | |
| 542 | expect(e.children[1].getAttribute('class')).toEqual('docGroup'); |
| 543 | expect(e.children[1].getAttribute('data-operation')).toEqual('and'); |
| 544 | |
| 545 | // This and-operation can be "or"ed or "delete"d |
| 546 | var secop = e.children[1].children[2]; |
| 547 | expect(secop.getAttribute('class')).toEqual('operators'); |
| 548 | expect(secop.children[0].getAttribute('class')).toEqual('or'); |
| 549 | expect(secop.children[1].getAttribute('class')).toEqual('delete'); |
| 550 | |
| 551 | // This or-operation can be "and"ed or "delete"d |
| 552 | expect(e.children[2].getAttribute('class')).toEqual('operators'); |
| 553 | expect(e.lastChild.getAttribute('class')).toEqual('operators'); |
| 554 | expect(e.lastChild.children[0].getAttribute('class')).toEqual('and'); |
| 555 | expect(e.lastChild.children[1].getAttribute('class')).toEqual('delete'); |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 556 | |
| 557 | }); |
| 558 | }); |
| 559 | |
| 560 | describe('KorAP.VirtualCollection', function () { |
| 561 | it('should be initializable', function () { |
| 562 | var vc = KorAP.VirtualCollection.render(); |
| 563 | expect(vc.element().getAttribute('class')).toEqual('vc'); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 564 | expect(vc.root().element().getAttribute('class')).toEqual('unspecified'); |
| 565 | |
| 566 | // Not removable |
| 567 | expect(vc.root().element().lastChild.children.length).toEqual(0); |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 568 | }); |
| 569 | |
| 570 | it('should be based on a doc', function () { |
| 571 | var vc = KorAP.VirtualCollection.render({ |
| 572 | "@type" : "korap:doc", |
| 573 | "key":"Titel", |
| 574 | "value":"Baum", |
| 575 | "match":"match:eq" |
| 576 | }); |
| 577 | |
| 578 | expect(vc.element().getAttribute('class')).toEqual('vc'); |
| 579 | expect(vc.root().element().getAttribute('class')).toEqual('doc'); |
| 580 | expect(vc.root().key()).toEqual('Titel'); |
| 581 | expect(vc.root().value()).toEqual('Baum'); |
| 582 | expect(vc.root().matchop()).toEqual('eq'); |
| 583 | |
| 584 | var docE = vc.root().element(); |
| 585 | expect(docE.children[0].firstChild.data).toEqual('Titel'); |
| 586 | expect(docE.children[1].firstChild.data).toEqual('eq'); |
| 587 | expect(docE.children[1].getAttribute('data-type')).toEqual('string'); |
| 588 | expect(docE.children[2].firstChild.data).toEqual('Baum'); |
| 589 | expect(docE.children[2].getAttribute('data-type')).toEqual('string'); |
| 590 | }); |
| 591 | |
| 592 | it('should be based on a docGroup', function () { |
| 593 | var vc = KorAP.VirtualCollection.render({ |
| 594 | "@type" : "korap:docGroup", |
| 595 | "operation" : "operation:and", |
| 596 | "operands" : [ |
| 597 | { |
| 598 | "@type": 'korap:doc', |
| 599 | "key" : 'author', |
| 600 | "match": 'match:eq', |
| 601 | "value": 'Max Birkendale', |
| 602 | "type": 'type:string' |
| 603 | }, |
| 604 | { |
| 605 | "@type": 'korap:doc', |
| 606 | "key": 'pubDate', |
| 607 | "match": 'match:eq', |
| 608 | "value": '2014-12-05', |
| 609 | "type": 'type:date' |
| 610 | } |
| 611 | ] |
| 612 | }); |
| 613 | |
| 614 | expect(vc.element().getAttribute('class')).toEqual('vc'); |
| 615 | expect(vc.root().element().getAttribute('class')).toEqual('docGroup'); |
| 616 | expect(vc.root().operation()).toEqual('and'); |
| 617 | |
| 618 | var docGroup = vc.root(); |
| 619 | |
| 620 | var first = docGroup.getOperand(0); |
| 621 | expect(first.key()).toEqual('author'); |
| 622 | expect(first.value()).toEqual('Max Birkendale'); |
| 623 | expect(first.matchop()).toEqual('eq'); |
| 624 | |
| 625 | var second = docGroup.getOperand(1); |
| 626 | expect(second.key()).toEqual('pubDate'); |
| 627 | expect(second.value()).toEqual('2014-12-05'); |
| 628 | expect(second.matchop()).toEqual('eq'); |
| 629 | }); |
| 630 | |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame^] | 631 | |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 632 | it('should be based on a nested docGroup', function () { |
| 633 | var vc = KorAP.VirtualCollection.render({ |
| 634 | "@type" : "korap:docGroup", |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame^] | 635 | "operation" : "operation:or", |
| 636 | "operands" : [ |
| 637 | { |
| 638 | "@type": 'korap:doc', |
| 639 | "key" : 'author', |
| 640 | "match": 'match:eq', |
| 641 | "value": 'Max Birkendale', |
| 642 | "type": 'type:string' |
| 643 | }, |
| 644 | { |
| 645 | "@type" : "korap:docGroup", |
| 646 | "operation" : "operation:and", |
| 647 | "operands" : [ |
| 648 | { |
| 649 | "@type": 'korap:doc', |
| 650 | "key": 'pubDate', |
| 651 | "match": 'match:geq', |
| 652 | "value": '2014-05-12', |
| 653 | "type": 'type:date' |
| 654 | }, |
| 655 | { |
| 656 | "@type": 'korap:doc', |
| 657 | "key": 'pubDate', |
| 658 | "match": 'match:leq', |
| 659 | "value": '2014-12-05', |
| 660 | "type": 'type:date' |
| 661 | } |
| 662 | ] |
| 663 | } |
| 664 | ] |
| 665 | }); |
| 666 | expect(vc.element().getAttribute('class')).toEqual('vc'); |
| 667 | expect(vc.element().firstChild.getAttribute('class')).toEqual('docGroup'); |
| 668 | expect(vc.element().firstChild.children[0].getAttribute('class')).toEqual('doc'); |
| 669 | var dg = vc.element().firstChild.children[1]; |
| 670 | expect(dg.getAttribute('class')).toEqual('docGroup'); |
| 671 | expect(dg.children[0].getAttribute('class')).toEqual('doc'); |
| 672 | expect(dg.children[1].getAttribute('class')).toEqual('doc'); |
| 673 | expect(dg.children[2].getAttribute('class')).toEqual('operators'); |
| 674 | expect(vc.element().firstChild.children[2].getAttribute('class')).toEqual('operators'); |
| 675 | }); |
| 676 | |
| 677 | it('should be modifiable by deletion', function () { |
| 678 | var vc = KorAP.VirtualCollection.render({ |
| 679 | "@type" : "korap:docGroup", |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 680 | "operation" : "operation:and", |
| 681 | "operands" : [ |
| 682 | { |
| 683 | "@type": 'korap:doc', |
| 684 | "key": 'pubDate', |
| 685 | "match": 'match:geq', |
| 686 | "value": '2014-05-12', |
| 687 | "type": 'type:date' |
| 688 | }, |
| 689 | { |
| 690 | "@type": 'korap:doc', |
| 691 | "key": 'pubDate', |
| 692 | "match": 'match:leq', |
| 693 | "value": '2014-12-05', |
| 694 | "type": 'type:date' |
| 695 | }, |
| 696 | { |
| 697 | "@type": 'korap:doc', |
| 698 | "key": 'foo', |
| 699 | "match": 'match:eq', |
| 700 | "value": 'bar', |
| 701 | "type": 'type:string' |
| 702 | } |
| 703 | ] |
| 704 | }); |
| 705 | |
| 706 | var docGroup = vc.root(); |
| 707 | var doc = docGroup.getOperand(1); |
| 708 | expect(doc.key()).toEqual("pubDate"); |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame^] | 709 | |
| 710 | // Remove operand 1 |
| 711 | expect(docGroup.delOperand(doc)).not.toBeUndefined(); |
| 712 | expect(doc._element).toEqual(undefined); |
| 713 | |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 714 | doc = docGroup.getOperand(1); |
| 715 | expect(doc.key()).toEqual("foo"); |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 716 | |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame^] | 717 | // Remove operand 1 |
| 718 | expect(docGroup.delOperand(doc)).not.toBeUndefined(); |
| 719 | expect(doc._element).toEqual(undefined); |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 720 | |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame^] | 721 | // Only one operand - but there shouldn't be a group anymore! |
| 722 | expect(docGroup.getOperand(1)).toBeUndefined(); |
| 723 | }); |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 724 | }); |
| 725 | |
| 726 | describe('KorAP.Operators', function () { |
| 727 | it('should be initializable', function () { |
| 728 | var op = KorAP.Operators.create(true, false, false); |
| 729 | expect(op.and()).toBeTruthy(); |
| 730 | expect(op.or()).not.toBeTruthy(); |
| 731 | expect(op.del()).not.toBeTruthy(); |
| 732 | |
| 733 | op.and(false); |
| 734 | expect(op.and()).not.toBeTruthy(); |
| 735 | expect(op.or()).not.toBeTruthy(); |
| 736 | expect(op.del()).not.toBeTruthy(); |
| 737 | |
| 738 | op.or(true); |
| 739 | op.del(true); |
| 740 | expect(op.and()).not.toBeTruthy(); |
| 741 | expect(op.or()).toBeTruthy(); |
| 742 | expect(op.del()).toBeTruthy(); |
| 743 | |
| 744 | var e = op.element(); |
| 745 | expect(e.getAttribute('class')).toEqual('operators'); |
| 746 | expect(e.children[0].getAttribute('class')).toEqual('or'); |
| 747 | expect(e.children[0].firstChild.data).toEqual('or'); |
| 748 | expect(e.children[1].getAttribute('class')).toEqual('delete'); |
| 749 | expect(e.children[1].firstChild.data).toEqual('×'); |
| 750 | |
| 751 | op.and(true); |
| 752 | op.del(false); |
| 753 | op.update(); |
| 754 | |
| 755 | e = op.element(); |
| 756 | expect(e.getAttribute('class')).toEqual('operators'); |
| 757 | expect(e.children[0].getAttribute('class')).toEqual('and'); |
| 758 | expect(e.children[0].firstChild.data).toEqual('and'); |
| 759 | expect(e.children[1].getAttribute('class')).toEqual('or'); |
| 760 | expect(e.children[1].firstChild.data).toEqual('or'); |
| 761 | }); |
| 762 | }); |