Code coverage report for spec/generic/test.spec.js

Statements: 90.5% (362 / 400)      Branches: 62.5% (5 / 8)      Functions: 63.01% (46 / 73)      Lines: 90.45% (360 / 398)      Ignored: none     

All files » spec/generic/ » test.spec.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 12101 1 1     1 1       1 2 1         1 17 17   17           17           17               1 1 1 1 1           1           1           1               1     1     1         1     1             1             1             1             1                 1             1               1 1     1 1 1   1   1     1               1   1 1         1     1 1       1             1   1 1   1   1     1   1             1   1 1 1       1 1 1   1                   1                     1           1               1                     1     1 1 1     1       1 1               1 1 1   1             1             1                 1             1             1             1 1   1 1   1 1       1 1 1   1                   1                   1                       1                   1                   1                   1 1   1 1   1 1   1 1   1 1   1 1       1 1 1 1   1         1         1         1         1         1         1         1           1 1           1 1   1 1   1 1   1 1     1             1 1 1 1 1 1 1 1   1 1   1 1   1 1   1 1   1 1     1 1       1 1   1 1     1 1       1 1   1 1   1 1   1 1       1 1 1   1 1   1 1 1 1 1   1     1 1     1 1   1     1 1       1 1 1       1     1     1     1     1     1     1     1         1         1   1         1     1         1   1         1     1         1     1         1   1         1     1         1   1         1       1 1   1         2       1     1       1 1 1   1         1         1         1         1         1         1         1             1                 1                 1                         1                 1                 1                     1   1                       1                   1       1 1 1   1         1         1             1             1               1                   1                       1       1 1 1   1         1         1         1           1     1     1       1 1     1     1 1         1 1 1 1           1     1 1 1                           1 1 1 2   1 2     1       1       1       1         1       1 1 1             1     1 1         1     1           1   1           1             1     1                 1 1   1     1 4         1           1 1 1   1         1     1     1         1 1   1       1     1           1 1 1   1     1   1     1           1 1 1         1     1     1 1 1     1 1           1 1 1 1 1     1     1 1 1   1 1       1     1 1         1   1     1 1       1 1 1 1 1   1 1 1            
Eif (typeof (window) === 'undefined') {
  var loki = require('../../src/lokijs.js');
  var suite = require('../helpers/assert-helpers.js').suite;
}
 
describe('loki', function () {
  var db,
    users,
    jonas;
 
  function docCompare(a, b) {
    if (a.$loki < b.$loki) return -1;
    Eif (a.$loki > b.$loki) return 1;
 
    return 0;
  }
 
  beforeEach(function () {
    db = new loki('test.json');
    users = db.addCollection('user');
 
    users.insert({
      name: 'dave',
      age: 25,
      lang: 'English'
    });
 
    users.insert({
      name: 'joe',
      age: 39,
      lang: 'Italian'
    });
 
    jonas = users.insert({
      name: 'jonas',
      age: 30,
      lang: 'Swedish'
    });
  })
 
 
  describe('core methods', function () {
    it('works', function () {
      var tdb = new loki('regextests');
      var tcu = tdb.addCollection('user');
      tcu.insert({
        name: 'abcd',
        age: 25,
        lang: 'English'
      });
 
      tcu.insert({
        name: 'AbCd',
        age: 39,
        lang: 'Italian'
      });
 
      tcu.insert({
        name: 'acdb',
        age: 30,
        lang: 'Swedish'
      });
 
      tcu.insert({
        name: 'aBcD',
        age: 30,
        lang: 'Swedish'
      });
 
 
      // findOne()
      var j = users.findOne({
        'name': 'jonas'
      });
      expect(j.name).toEqual('jonas');
 
      // find()
      var result = users.find({
        'age': {
          '$gt': 29
        }
      });
      expect(result.length).toEqual(2);
 
      // $regex test with raw regex
      expect(users.find({
        'name': {
          '$regex': /o/
        }
      }).length).toEqual(2);
 
      // case insensitive regex with array of ["pattern", "options"]
      expect(tcu.find({
        'name': {
          '$regex': ["abcd", "i"]
        }
      }).length).toEqual(3);
 
      // regex with single encoded string pattern (no options)
      expect(tcu.find({
        'name': {
          '$regex': "cd"
        }
      }).length).toEqual(2);
 
      // $contains
      expect(users.find({
        'name': {
          '$contains': "jo"
        }
      }).length).toEqual(2);
 
      // $contains using array element
      expect(users.find({
        'name': {
          '$contains': ["jo"]
        }
      }).length).toEqual(2);
 
 
 
      // $contains any with one value
      expect(users.find({
        'name': {
          '$containsAny': 'nas'
        }
      }).length).toEqual(1);
 
      // $contains any with multiple values
      expect(users.find({
        'name': {
          '$containsAny': ['nas', 'dave']
        }
      }).length).toEqual(2);
 
 
      // insert() : try inserting existing document (should fail), try adding doc with legacy id column
      var collectionLength = users.data.length;
      var objDave = users.findOne({
        'name': 'dave'
      });
      var wasAdded = true;
      try {
        users.insert(objDave);
      } catch (err) {
        wasAdded = false;
      }
      expect(wasAdded).toEqual(false);
 
      // our collections are not strongly typed so lets invent some object that has its 'own' id column
      var legacyObject = {
        id: 999,
        first: 'aaa',
        last: 'bbb',
        city: 'pasadena',
        state: 'ca'
      }
 
      wasAdded = true;
 
      try {
        users.insert(legacyObject);
      } catch (err) {
        wasAdded = false;
      }
 
      expect(wasAdded).toEqual(true);
 
      // remove object so later queries access valid properties on all objects
      Eif (wasAdded) {
        users.remove(legacyObject); // the object itself should have been modified
      }
 
      // update()
      legacyObject = {
        id: 998,
        first: 'aaa',
        last: 'bbb',
        city: 'pasadena',
        state: 'ca'
      }
      var wasUpdated = true;
 
      try {
        users.update(legacyObject);
      } catch (err) {
        wasUpdated = false;
      }
      expect(wasUpdated).toEqual(false);
 
      // remove() - add some bogus object to remove
      var userCount1 = users.data.length;
 
      testObject = {
        first: 'aaa',
        last: 'bbb',
        city: 'pasadena',
        state: 'ca'
      }
 
      users.insert(testObject);
 
      expect(userCount1 + 1).toEqual(users.data.length);
      users.remove(testObject);
      expect(userCount1).toEqual(users.data.length);
    })
  });
 
  describe('dot notation', function () {
    it('works', function () {
      var dnc = db.addCollection('dncoll');
 
      dnc.insert({
        first: 'aaa',
        last: 'bbb',
        addr: {
          street: '111 anystreet',
          state: 'AS',
          zip: 12345
        }
      });
 
      dnc.insert({
        first: 'ddd',
        last: 'eee',
        addr: {
          street: '222 anystreet',
          state: 'FF',
          zip: 32345
        }
      });
 
      // make sure it can handle case where top level property doesn't exist
      dnc.insert({
        first: 'mmm',
        last: 'nnn'
      });
 
      // make sure it can handle case where subscan property doesn't exist
      dnc.insert({
        first: 'ooo',
        last: 'ppp',
        addr: {
          state: 'YY'
        }
      });
 
      dnc.insert({
        first: 'jjj',
        last: 'kkk',
        addr: {
          street: '777 anystreet',
          state: 'WW',
          zip: 12345
        }
      });
 
      // test dot notation using regular find (with multiple results)
      var firstResult = dnc.find({
        "addr.zip": 12345
      });
      expect(firstResult.length).toEqual(2);
      expect(firstResult[0].addr.zip).toEqual(12345);
      expect(firstResult[1].addr.zip).toEqual(12345);
 
      // test not notation using findOne
      var secObj = dnc.findOne({
        "addr.state": 'FF'
      });
 
      expect(secObj !== null).toBeTruthy();
      expect(secObj.addr.zip).toEqual(32345);
 
    });
 
  });
 
  // We only support dot notation involving array when
  // the leaf property is the array.  This verifies that functionality
  describe('dot notation across leaf array', function() {
    it('works', function () {
      var dna = db.addCollection('dnacoll');
 
      dna.insert({
        id: 1,
        children: [{
          someProperty: 11
        }]
      });
 
      dna.insert({
        id: 2,
        children: [{
          someProperty: 22
        }]
      });
 
      dna.insert({
        id: 3,
        children: [{
          someProperty: 33
        }, {
          someProperty: 22
        }]
      });
 
      dna.insert({
        id: 4,
        children: [{
          someProperty: 11
        }]
      });
 
      dna.insert({
        id: 5,
        children: [{
          missing: null
        }]
      });
 
      dna.insert({
        id: 6,
        children: [{
          someProperty: null
        }]
      });
 
      var results = dna.find({'children.someProperty': 33 });
      expect(results.length).toEqual(1);
 
      results = dna.find({'children.someProperty': 11 });
      expect(results.length).toEqual(2);
 
      results = dna.find({'children.someProperty': 22});
      expect(results.length).toEqual(2);
    });
  });
 
  describe('dot notation across child array', function() {
    it('works', function () {
      var dna = db.addCollection('dnacoll');
 
      dna.insert({
        id: 1,
        children: [{
          id: 11,
          someArray: [{
            someProperty: 111
          }]
        }]
      });
 
      dna.insert({
        id: 2,
        children: [{
          id: 22,
          someArray: [{
            someProperty: 222
          }]
        }]
      });
 
      dna.insert({
        id: 3,
        children: [{
          id: 33,
          someArray: [{
            someProperty: 333
          }, {
            someProperty: 222
          }]
        }]
      });
 
      dna.insert({
        id: 4,
        children: [{
          id: 44,
          someArray: [{
            someProperty: 111
          }]
        }]
      });
 
      dna.insert({
        id: 5,
        children: [{
          id: 55,
          someArray: [{
            missing: null
          }]
        }]
      });
 
      dna.insert({
        id: 6,
        children: [{
          id: 66,
          someArray: [{
            someProperty: null
          }]
        }]
      });
 
      var results = dna.find({'children.someArray.someProperty': 333});
      expect(results.length).toEqual(1);
 
      results = dna.find({'children.someArray.someProperty': 111});
      expect(results.length).toEqual(2);
 
      results = dna.find({'children.someArray.someProperty': 222});
      expect(results.length).toEqual(2);
 
      results = dna.find({'$and': [{'id': 3}, {'children.someArray.someProperty': 222}]});
      expect(results.length).toEqual(1);
 
      results = dna.find({'$and': [{'id': 1}, {'children.someArray.someProperty': 222}]});
      expect(results.length).toEqual(0);
 
      results = dna.find({'$or': [{'id': 1}, {'children.someArray.someProperty': 222}]});
      expect(results.length).toEqual(3);
    });
  });
 
  describe('calculateRange', function () {
    it('works', function () {
      var eic = db.addCollection('eic');
      eic.ensureIndex('testid');
 
      eic.insert({
        'testid': 1,
        'testString': 'hhh',
        'testFloat': 5.2
      }); //0
      eic.insert({
        'testid': 1,
        'testString': 'aaa',
        'testFloat': 6.2
      }); //1
      eic.insert({
        'testid': 5,
        'testString': 'zzz',
        'testFloat': 7.2
      }); //2
      eic.insert({
        'testid': 6,
        'testString': 'ggg',
        'testFloat': 1.2
      }); //3
      eic.insert({
        'testid': 9,
        'testString': 'www',
        'testFloat': 8.2
      }); //4
      eic.insert({
        'testid': 11,
        'testString': 'yyy',
        'testFloat': 4.2
      }); //5
      eic.insert({
        'testid': 22,
        'testString': 'yyz',
        'testFloat': 9.2
      }); //6
      eic.insert({
        'testid': 23,
        'testString': 'm',
        'testFloat': 2.2
      }); //7
 
      var rset = eic.chain();
      rset.find({
        'testid': 1
      }); // force index to be built
 
      // ranges are order of sequence in index not data array positions
 
      var range = rset.calculateRange('$eq', 'testid', 22);
      expect(range).toEqual([6, 6]);
 
      range = rset.calculateRange('$eq', 'testid', 1);
      expect(range).toEqual([0, 1]);
 
      range = rset.calculateRange('$eq', 'testid', 7);
      expect(range).toEqual([0, -1]);
 
      range = rset.calculateRange('$gte', 'testid', 23);
      expect(range).toEqual([7, 7]);
 
      // reference this new record for future evaluations
      eic.insert({
        'testid': 23,
        'testString': 'bbb',
        'testFloat': 1.9
      });
 
      // test when all records are in range
      range = rset.calculateRange('$lt', 'testid', 25);
      expect(range).toEqual([0, 8]);
      range = rset.calculateRange('$lte', 'testid', 25);
      expect(range).toEqual([0, 8]);
      range = rset.calculateRange('$gt', 'testid', 0);
      expect(range).toEqual([0, 8]);
      range = rset.calculateRange('$gte', 'testid', 0);
      expect(range).toEqual([0, 8]);
 
      range = rset.calculateRange('$gte', 'testid', 23);
      expect(range).toEqual([7, 8]);
 
      range = rset.calculateRange('$gte', 'testid', 24);
      expect(range).toEqual([0, -1]);
 
      range = rset.calculateRange('$lte', 'testid', 5);
      expect(range).toEqual([0, 2]);
 
      range = rset.calculateRange('$lte', 'testid', 1);
      expect(range).toEqual([0, 1]);
 
      range = rset.calculateRange('$lte', 'testid', -1);
      expect(range).toEqual([0, -1]);
 
      // add another index on string property
      eic.ensureIndex('testString');
      rset.find({
        'testString': 'asdf'
      }); // force index to be built
 
      range = rset.calculateRange('$lte', 'testString', 'ggg');
      expect(range).toEqual([0, 2]); // includes record added in middle
 
      range = rset.calculateRange('$gte', 'testString', 'm');
      expect(range).toEqual([4, 8]); // offset by 1 because of record in middle
 
      // add some float range evaluations
      eic.ensureIndex('testFloat');
      rset.find({
        'testFloat': '1.1'
      }); // force index to be built
 
      range = rset.calculateRange('$lte', 'testFloat', 1.2);
      expect(range).toEqual([0, 0]);
 
      range = rset.calculateRange('$eq', 'testFloat', 1.111);
      expect(range).toEqual([0, -1]);
 
      range = rset.calculateRange('$eq', 'testFloat', 8.2);
      expect(range).toEqual([7, 7]); // 8th pos
 
      range = rset.calculateRange('$gte', 'testFloat', 1.0);
      expect(range).toEqual([0, 8]); // 8th pos
    })
  });
 
  describe('indexLifecycle', function () {
    it('works', function () {
      var ilc = db.addCollection('ilc');
 
      var hasIdx = ilc.binaryIndices.hasOwnProperty('testid');
      expect(hasIdx).toEqual(false);
 
      ilc.ensureIndex('testid');
      hasIdx = ilc.binaryIndices.hasOwnProperty('testid');
      expect(hasIdx).toEqual(true);
      expect(ilc.binaryIndices.testid.dirty).toEqual(false);
      expect(ilc.binaryIndices.testid.values).toEqual([]);
 
      ilc.insert({
        'testid': 5
      });
      expect(ilc.binaryIndices.testid.dirty).toEqual(true);
      ilc.insert({
        'testid': 8
      });
      expect(ilc.binaryIndices.testid.values).toEqual([]);
      expect(ilc.binaryIndices.testid.dirty).toEqual(true);
 
      ilc.find({
        'testid': 8
      }); // should force index build
      expect(ilc.binaryIndices.testid.dirty).toEqual(false);
      expect(ilc.binaryIndices.testid.values.length).toEqual(2);
    })
  })
 
  describe('indexes', function () {
    it('works', function () {
      var itc = db.addCollection('test', {
        indices: ['testid']
      });
 
      itc.insert({
        'testid': 1
      });
      itc.insert({
        'testid': 2
      });
      itc.insert({
        'testid': 5
      });
      itc.insert({
        'testid': 5
      });
      itc.insert({
        'testid': 9
      });
      itc.insert({
        'testid': 11
      });
      itc.insert({
        'testid': 22
      });
      itc.insert({
        'testid': 22
      });
 
      // lte
      var results = itc.find({
        'testid': {
          '$lte': 1
        }
      });
      expect(results.length).toEqual(1);
 
      results = itc.find({
        'testid': {
          '$lte': 22
        }
      });
      expect(results.length).toEqual(8);
 
      // lt
      results = itc.find({
        'testid': {
          '$lt': 1
        }
      });
      expect(results.length).toEqual(0);
 
      results = itc.find({
        'testid': {
          '$lt': 22
        }
      });
      expect(results.length).toEqual(6);
 
      // eq
      results = itc.find({
        'testid': {
          '$eq': 22
        }
      });
      expect(results.length).toEqual(2);
 
      // gt
      results = itc.find({
        'testid': {
          '$gt': 22
        }
      });
      expect(results.length).toEqual(0);
 
      results = itc.find({
        'testid': {
          '$gt': 5
        }
      });
      expect(results.length).toEqual(4);
 
      // gte
      results = itc.find({
        'testid': {
          '$gte': 5
        }
      });
      expect(results.length).toEqual(6);
 
      results = itc.find({
        'testid': {
          '$gte': 10
        }
      });
      expect(results.length).toEqual(3);
    });
  });
 
  describe('resultSet', function () {
    it('works', function () {
      // Resultset find
      expect(users.chain().find({
        'age': {
          '$gte': 30
        }
      }).where(function (obj) {
        return obj.lang === 'Swedish';
      }).data().length).toEqual(1);
 
      // Resultset offset
      expect(users.chain().offset(1).data().length).toEqual(users.data.length - 1);
 
      // Resultset limit
      expect(users.chain().limit(2).data().length).toEqual(2);
    });
  });
 
  describe('andOrOps', function () {
    it('works', function () {
      var eic = db.addCollection('eic');
 
      eic.insert({
        'testid': 1,
        'testString': 'hhh',
        'testFloat': 5.2
      }); //0
      eic.insert({
        'testid': 1,
        'testString': 'bbb',
        'testFloat': 6.2
      }); //1
      eic.insert({
        'testid': 5,
        'testString': 'zzz',
        'testFloat': 7.2
      }); //2
      eic.insert({
        'testid': 6,
        'testString': 'ggg',
        'testFloat': 1.2
      }); //3
      eic.insert({
        'testid': 9,
        'testString': 'www',
        'testFloat': 8.2
      }); //4
      eic.insert({
        'testid': 11,
        'testString': 'yyy',
        'testFloat': 4.2
      }); //5
      eic.insert({
        'testid': 22,
        'testString': 'bbb',
        'testFloat': 9.2
      }); //6
      eic.insert({
        'testid': 23,
        'testString': 'm',
        'testFloat': 2.2
      }); //7
 
      // coll.find $and
      expect(eic.find({
        '$and': [{
          'testid': 1
        }, {
          'testString': 'bbb'
        }]
      }).length).toEqual(1);
 
      // resultset.find $and
      expect(eic.chain().find({
        '$and': [{
          'testid': 1
        }, {
          'testString': 'bbb'
        }]
      }).data().length).toEqual(1);
 
      // resultset.find explicit operators
      expect(eic.chain().find({
        '$and': [{
          'testid': {
            '$eq': 1
          }
        }, {
          'testFloat': {
            '$gt': 6.0
          }
        }]
      }).data().length).toEqual(1);
 
      // coll.find $or
      expect(eic.find({
        '$or': [{
          'testid': 1
        }, {
          'testString': 'bbb'
        }]
      }).length).toEqual(3);
 
      // resultset.find $or
      expect(eic.chain().find({
        '$or': [{
          'testid': 1
        }, {
          'testString': 'bbb'
        }]
      }).data().length).toEqual(3);
 
      // resultset.find explicit operators
      expect(eic.chain().find({
        '$or': [{
          'testid': 1
        }, {
          'testFloat': {
            '$gt': 7.0
          }
        }]
      }).data().length).toEqual(5);
 
      // add index and repeat final test
      eic.ensureIndex('testid');
 
      expect(eic.chain().find({
        '$and': [{
          'testid': {
            '$eq': 1
          }
        }, {
          'testFloat': {
            '$gt': 6.0
          }
        }]
      }).data().length).toEqual(1);
 
      expect(eic.chain().find({
        '$or': [{
          'testid': 1
        }, {
          'testFloat': {
            '$gt': 7.0
          }
        }]
      }).data().length).toEqual(5);
 
      db.removeCollection('eic');
    });
  });
 
  describe('findOne', function () {
    it('works', function () {
      var eic = db.addCollection('eic');
 
      eic.insert({
        'testid': 1,
        'testString': 'hhh',
        'testFloat': 5.2
      }); //0
      eic.insert({
        'testid': 1,
        'testString': 'bbb',
        'testFloat': 6.2
      }); //1
      eic.insert({
        'testid': 5,
        'testString': 'zzz',
        'testFloat': 7.2
      }); //2
 
      // coll.findOne return type
      it('return type', function () {
        expect(typeof eic.findOne({
          'testid': 1
        })).toEqual('object');
      });
 
      // coll.findOne return match
      it('should match 7.2', function () {
        expect(eic.findOne({
          'testid': 5
        }).testFloat).toEqual(7.2);
 
      });
 
      // findOne with $and op
      it('findOne with $and op', function () {
        expect(eic.findOne({
          '$and': [{
            'testid': 1
          }, {
            'testString': 'bbb'
          }]
        }).testFloat, 6.2);
      });
 
      it('findOne with $or op', function () {
        expect(eic.findOne({
          '$or': [{
            'testid': 2
          }, {
            'testString': 'zzz'
          }]
        }).testFloat).toEqual(7.2);
 
      });
      // findOne with $and op
 
      db.removeCollection('eic');
    })
  });
 
  describe('chained removes', function() {
    it('works', function() {
      var rsc = db.addCollection('rsc');
 
      rsc.insert({
        'testid': 1,
        'testString': 'hhh',
        'testFloat': 5.2
      });
      rsc.insert({
        'testid': 1,
        'testString': 'bbb',
        'testFloat': 6.2
      });
      rsc.insert({
        'testid': 2,
        'testString': 'ccc',
        'testFloat': 6.2
      });
      rsc.insert({
        'testid': 5,
        'testString': 'zzz',
        'testFloat': 7.2
      });
 
      var docCount = rsc.find().length;
 
      // verify initial doc count
      expect(docCount).toEqual(4);
 
      // remove middle documents
      rsc.chain().find({testFloat: 6.2}).remove();
 
 
      // verify new doc count
      expect(rsc.find().length).toEqual(2);
      expect(rsc.chain().data().length).toEqual(2);
 
      // now fetch and retain all remaining documents
      var results = rsc.chain().simplesort('testString').data();
 
      // make sure its the documents we expect
      expect(results[0].testString).toEqual('hhh');
      expect(results[1].testString).toEqual('zzz');
    })
  });
 
  /* Dynamic View Tests */
  describe('stepEvaluateDocument', function () {
    it('works', function () {
      var view = users.addDynamicView('test');
      var query = {
        'age': {
          '$gt': 24
        }
      };
 
      view.applyFind(query);
 
      // churn evaluateDocuments() to make sure it works right
      jonas.age = 23;
      users.update(jonas);
      it('evaluate documents', function () {
        expect(view.data().length).toEqual(users.data.length - 1);
        jonas.age = 30;
        users.update(jonas);
        expect(view.data().length).toEqual(users.data.length);
        jonas.age = 23;
        users.update(jonas);
        expect(view.data().length).toEqual(users.data.length - 1);
        jonas.age = 30;
        users.update(jonas);
        expect(view.data().length).toEqual(users.data.length);
      });
 
      // assert set equality of docArrays irrelevant of sort/sequence
      var result1 = users.find(query).sort(docCompare);
      var result2 = view.data().sort(docCompare);
      result1.forEach(function (obj) {
        delete obj.meta
      });
      result2.forEach(function (obj) {
        delete obj.meta
      });
 
      it('Result data Equality', function () {
        expect(result1).toEqual(result2);
      });
 
      it('Strict Equality', function () {
        expect(users.find(query) === view.data()).toBeTruthy();
      });
 
      it('View data equality', function () {
        expect(view.resultset).toEqual(view.resultset.copy());
      });
 
      it('View data copy strict equality', function () {
        expect(view.resultset === view.resultset.copy()).toBeFalsy();
      });
 
 
      return view;
    });
  });
 
  describe('stepDynamicViewPersistence', function () {
    it('works', function stepDynamicViewPersistence() {
      var query = {
        'age': {
          '$gt': 24
        }
      };
 
      // set up a persistent dynamic view with sort
      var pview = users.addDynamicView('test2', {
        persistent: true
      });
      pview.applyFind(query);
      pview.applySimpleSort('age');
 
      // the dynamic view depends on an internal resultset
      // the persistent dynamic view also depends on an internal resultdata data array
      // filteredrows should be applied immediately to resultset will be lazily built into resultdata later when data() is called
      it('dynamic view initialization 1', function () {
        expect(pview.resultset.filteredrows.length).toEqual(3);
      })
      it('dynamic view initialization 2', function () {
        expect(pview.resultdata.length).toEqual(0);
      });
 
 
      // compare how many documents are in results before adding new ones
      var pviewResultsetLenBefore = pview.resultset.filteredrows.length;
 
      users.insert({
        name: 'abc',
        age: 21,
        lang: 'English'
      });
 
      users.insert({
        name: 'def',
        age: 25,
        lang: 'English'
      });
 
      // now see how many are in resultset (without rebuilding persistent view)
      var pviewResultsetLenAfter = pview.resultset.filteredrows.length;
 
      // only one document should have been added to resultset (1 was filtered out)
      it('dv resultset is valid',
        function () {
          expect(pviewResultsetLenBefore + 1).toEqual(pviewResultsetLenAfter);
        });
 
 
      // Test sorting and lazy build of resultdata
 
      // retain copy of internal resultset's filteredrows before lazy sort
      var frcopy = pview.resultset.filteredrows.slice();
      pview.data();
      // now make a copy of internal result's filteredrows after lazy sort
      var frcopy2 = pview.resultset.filteredrows.slice();
 
      // verify filteredrows logically matches resultdata (irrelevant of sort)
      for (var idxFR = 0; idxFR < frcopy2.length; idxFR++) {
        it('dynamic view resultset/resultdata consistency', function () {
          expect(pview.resultdata[idxFR]).toEqual(pview.collection.data[frcopy2[idxFR]]);
        });
      }
      // now verify they are not exactly equal (verify sort moved stuff)
      it('dynamic view sort', function () {
        expect(frcopy).toEqual(frcopy2)
      });
    });
  });
 
  describe('stepDynamicViewPersistence', function () {
    it('works', function duplicateItemFoundOnIndex() {
      var test = db.addCollection('nodupes', ['index']);
 
      var item = test.insert({
        index: 'key',
        a: 1
      });
 
      var results = test.find({
        index: 'key'
      });
      it('one result exists', function () {
        expect(results.length).toEqual(1);
      });
      it('the correct result is returned', function () {
        expect(results[0].a).toEqual(1);
      });
 
 
      item.a = 2;
      test.update(item);
 
      results = test.find({
        index: 'key'
      });
 
      it('one result exists', function () {
        expect(results.length).toEqual(1);
      });
      it('the correct result is returned', function () {
        expect(results[0].a).toEqual(2);
      });
    });
  });
 
  describe('stepDynamicViewPersistence', function () {
    it('works', function testEmptyTableWithIndex() {
      var itc = db.addCollection('test', ['testindex']);
 
      var resultsNoIndex = itc.find({
        'testid': 2
      });
      expect(resultsNoIndex.length).toEqual(0);
 
      var resultsWithIndex = itc.find({
        'testindex': 4
      });
      it('no results found', function () {
        expect(resultsWithIndex.length).toEqual(0);
      });
    });
  });
 
  describe('stepDynamicViewPersistence', function () {
    it('works', function testAnonym() {
      var coll = db.anonym([{
        name: 'joe'
      }, {
        name: 'jack'
      }], ['name']);
      it('Anonym collection', function () {
        expect(coll.data.length).toEqual(2);
      });
      it('Collection not found', function () {
        expect(db.getCollection('anonym')).toEqual(null);
      });
      coll.name = 'anonym';
      db.loadCollection(coll);
      it('Anonym collection loaded', function () {
        expect(!!db.getCollection('anonym') === true).toBeTruthy();
      });
      coll.clear();
      it('No data after coll.clear()', function () {
        expect(0).toEqual(coll.data.length)
      });
    });
  });
 
  describe('stepDynamicViewPersistence', function () {
    it('works', function testCollections() {
      var db = new loki('testCollections');
      db.name = 'testCollections';
      it('DB name', function () {
        expect(db.getName()).toEqual('testCollections');
      });
      var t = db.addCollection('test1', {
        transactional: true
      });
      db.addCollection('test2');
      suite.assertThrows('Throw error on wrong remove', function () {
        t.remove('foo');
      }, Error);
      suite.assertThrows('Throw error on non-synced doc', function () {
        t.remove({
          name: 'joe'
        });
      }, Error);
      it('List collections', function () {
        expect(db.listCollections().length).toEqual(2);
      });
      t.clear();
      var users = [{
        name: 'joe'
      }, {
        name: 'dave'
      }];
      t.insert(users);
 
      it('2 docs after array insert', function () {
        expect(2).toEqual(t.data.length)
      });
      t.remove(users);
      it('0 docs after array remove', function () {
        expect(0).toEqual(t.data.length)
      });
 
      function TestError() {}
      TestError.prototype = new Error;
      db.autosaveEnable();
      db.on('close', function () {
        throw new TestError;
      });
      suite.assertThrows('Throw error on purpose on close', function () {
        db.close(function () {
          return;
        });
      }, TestError);
    });
  });
});