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 | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 631 | it('should be based on a nested docGroup', function () { |
| 632 | var vc = KorAP.VirtualCollection.render({ |
| 633 | "@type" : "korap:docGroup", |
| 634 | "operation" : "operation:or", |
| 635 | "operands" : [ |
| 636 | { |
| 637 | "@type": 'korap:doc', |
| 638 | "key" : 'author', |
| 639 | "match": 'match:eq', |
| 640 | "value": 'Max Birkendale', |
| 641 | "type": 'type:string' |
| 642 | }, |
| 643 | { |
| 644 | "@type" : "korap:docGroup", |
| 645 | "operation" : "operation:and", |
| 646 | "operands" : [ |
| 647 | { |
| 648 | "@type": 'korap:doc', |
| 649 | "key": 'pubDate', |
| 650 | "match": 'match:geq', |
| 651 | "value": '2014-05-12', |
| 652 | "type": 'type:date' |
| 653 | }, |
| 654 | { |
| 655 | "@type": 'korap:doc', |
| 656 | "key": 'pubDate', |
| 657 | "match": 'match:leq', |
| 658 | "value": '2014-12-05', |
| 659 | "type": 'type:date' |
| 660 | } |
| 661 | ] |
| 662 | } |
| 663 | ] |
| 664 | }); |
| 665 | expect(vc.element().getAttribute('class')).toEqual('vc'); |
| 666 | expect(vc.element().firstChild.getAttribute('class')).toEqual('docGroup'); |
| 667 | expect(vc.element().firstChild.children[0].getAttribute('class')).toEqual('doc'); |
| 668 | var dg = vc.element().firstChild.children[1]; |
| 669 | expect(dg.getAttribute('class')).toEqual('docGroup'); |
| 670 | expect(dg.children[0].getAttribute('class')).toEqual('doc'); |
| 671 | expect(dg.children[1].getAttribute('class')).toEqual('doc'); |
| 672 | expect(dg.children[2].getAttribute('class')).toEqual('operators'); |
| 673 | expect(vc.element().firstChild.children[2].getAttribute('class')).toEqual('operators'); |
| 674 | }); |
Nils Diewald | 0b6c041 | 2014-12-19 03:55:57 +0000 | [diff] [blame] | 675 | }); |
| 676 | |
| 677 | describe('KorAP.Operators', function () { |
| 678 | it('should be initializable', function () { |
| 679 | var op = KorAP.Operators.create(true, false, false); |
| 680 | expect(op.and()).toBeTruthy(); |
| 681 | expect(op.or()).not.toBeTruthy(); |
| 682 | expect(op.del()).not.toBeTruthy(); |
| 683 | |
| 684 | op.and(false); |
| 685 | expect(op.and()).not.toBeTruthy(); |
| 686 | expect(op.or()).not.toBeTruthy(); |
| 687 | expect(op.del()).not.toBeTruthy(); |
| 688 | |
| 689 | op.or(true); |
| 690 | op.del(true); |
| 691 | expect(op.and()).not.toBeTruthy(); |
| 692 | expect(op.or()).toBeTruthy(); |
| 693 | expect(op.del()).toBeTruthy(); |
| 694 | |
| 695 | var e = op.element(); |
| 696 | expect(e.getAttribute('class')).toEqual('operators'); |
| 697 | expect(e.children[0].getAttribute('class')).toEqual('or'); |
| 698 | expect(e.children[0].firstChild.data).toEqual('or'); |
| 699 | expect(e.children[1].getAttribute('class')).toEqual('delete'); |
| 700 | expect(e.children[1].firstChild.data).toEqual('×'); |
| 701 | |
| 702 | op.and(true); |
| 703 | op.del(false); |
| 704 | op.update(); |
| 705 | |
| 706 | e = op.element(); |
| 707 | expect(e.getAttribute('class')).toEqual('operators'); |
| 708 | expect(e.children[0].getAttribute('class')).toEqual('and'); |
| 709 | expect(e.children[0].firstChild.data).toEqual('and'); |
| 710 | expect(e.children[1].getAttribute('class')).toEqual('or'); |
| 711 | expect(e.children[1].firstChild.data).toEqual('or'); |
| 712 | }); |
| 713 | }); |