All files / src/dev/lib dbcon.ts

99.1% Statements 441/445
50% Branches 64/128
100% Functions 102/102
99.09% Lines 437/441

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                      1x 1x 1x   1x 1x 1x   1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x         1x 1x 1x 1x 1x   1x 1x 1x   1x 1x 1x 1x   1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x     1x             2x               2x               2x                     2x               2x                 2x 2x             2x 2x     2x 2x 2x 1x   1x     2x             2x 2x 2x 2x     2x 2x 2x 2x 2x 2x     2x 2x 2x 2x                       2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x       2x 2x 2x 2x 2x       2x 2x 2x 2x             2x 2x 2x 2x             2x 2x 2x 2x             2x 2x 2x 2x             2x 2x 2x 2x                         2x 2x 2x 2x                             2x 2x 2x 2x                       2x 2x 2x 2x                       2x 2x 2x 2x                       2x 2x 2x 2x                                   2x 2x 2x 2x             2x 2x 2x 2x             2x 2x 2x 2x             2x 2x 2x 2x             2x 2x 2x 2x             2x 2x 2x 2x             2x 2x 2x 2x             2x 2x   2x 2x               2x 2x     2x 2x 2x 2x                     2x 2x 2x 2x 2x   2x     2x 2x 2x 2x       2x 2x 2x 2x 2x 2x 2x   2x     2x       2x   2x 4x 4x   2x       2x 2x 2x 2x     2x 2x 2x 2x     2x 2x 2x 2x     2x 2x 2x 2x     2x 2x 2x 2x                     2x 2x 2x     2x 2x 2x 2x                     2x 2x 2x 2x     2x 2x 2x 2x                     2x 2x 2x     2x 2x 2x 2x                     2x 2x 2x 2x 2x     2x 2x 2x 2x 2x     2x     2x 2x 2x   2x 2x 2x 2x   2x 2x 2x 2x 2x     2x 2x       1x         2x 2x 2x 2x             2x 2x 2x 2x 1x   1x     2x 2x 2x     2x 2x 2x 2x 2x 200x               200x 200x 200x 200x 200x                       2x 2x 2x 2x     2x 2x 2x 2x     2x 2x 2x 2x     2x 2x 2x 2x 2x 2x       2x 2x 2x 2x       2x       2x 2x 2x     2x 2x 2x 2x 200x   2x       1x           1x 1x 1x   1x 1x   1x 1x   1x 1x   1x 1x   1x 1x 1x 1x 1x 1x   1x 1x   1x 1x   1x 1x   1x 1x   1x 1x               1x 1x 1x 1x   1x 1x               1x 1x 1x   1x 1x     1x       1x 1x           1x 1x   1x 1x     1x   1x 1x   1x 1x   1x 1x   1x 1x   1x 1x   1x 1x     1x 1x   1x 1x               1x       1x 1x 1x 1x   1x 1x 1x   1x 1x 1x 1x     1x     1x 1x 1x     1x 1x 1x     1x   1x 1x 1x 1x                   1x                       1x 1x     1x 1x               1x 1x       1x 1x 1x   1x 1x     1x            
import { describe, test, expect, beforeAll } from 'vitest';
import TestEntity from '../testClass/db/entity/TestEntity';
import {
  CoreDBCon,
  CoreDBPrefab,
  CoreEntityWrapper,
  validateEntity,
} from '../../classes';
import type { ICoreKernelModule, IDataBase } from '../../lib';
import TestPrefab from '../testClass/db/TestPrefab';
 
import TestEntityLinked from '../testClass/db/entity/TestEntityLinked';
import { EntityValidator } from '../../utils';
import TestContext from '../TestContext';
import { InMemCache } from '../../modules';
import BadEntity from '../testClass/db/entity/BadEntity';
import TestModule from '../testClass/TestModule';
 
export default function jestDb() {
  describe('dbcon', () => {
    const testText = 'hello_world';
    const [kernel] = TestContext.getEntity();
 
    beforeAll(() => {
      const db = kernel.getDb();
      expect(db.isConnected()).toBeTruthy();
    });
    describe('Database', () => {
      test('get version', async () => {
        expect(kernel.getState()).toBe('running');
        const db = kernel.getDb();
        expect(db).not.toBeNull();
        const conf = await db?.getConfig('dbversion');
        expect(conf?.c_value).not.toBeNull();
      });
 
      test('config test', async () => {
        expect(kernel.getState()).toBe('running');
        const db = kernel.getDb();
        expect(db).not.toBeNull();
        Eif (db) {
          const conf = await db.setConfig(testText, testText);
          expect(conf).toBeTruthy();
          const res = await db.getConfig(testText);
          expect(res?.c_value).toBe(testText);
          await db.removeConfig(testText);
          const res2 = await db.getConfig(testText);
          expect(res2).toBeUndefined();
 
          expect(await db?.setConfig('test', 'test')).toBeTruthy();
          await db?.removeConfig('test');
          expect(await db?.configExist('test')).toBeFalsy();
        }
      });
    });
 
    describe('TestDatabase', () => {
      test('get version', async () => {
        const db = kernel.getChildModule('testModule')?.getDb();
        const conf = await db?.getConfig('dbversion');
        expect(conf?.c_value).not.toBeNull();
      });
      test('isConnected', async () => {
        const db = kernel.getChildModule('testModule')?.getDb() as TestPrefab;
        expect(db.isConnected()).toBeTruthy();
      });
      test('test entity', async () => {
        const db = kernel.getChildModule('testModule')?.getDb() as TestPrefab;
        expect(db.getEntityMeta()).toHaveLength(2);
        expect(await db.ent.getObjList()).toHaveLength(0);
      });
      test('manual update', async () => {
        const dbpre = kernel
          .getChildModule('testModule')
          ?.getDb() as CoreDBPrefab<any>;
        const db = dbpre.getPrefabDB();
        const conf = await db.getConfig('dbversion');
        expect(conf?.c_value).not.toBeNull();
        expect(db.dbVersion).toBe('0');
        db.dbVersion = '2';
        expect(await db.canUpdate()).toBeTruthy();
        expect(await db.update()).toBeTruthy();
        expect(await db.getCurrenDBVersion()).toBe('2');
        await db.setConfig('dbversion', '0');
      });
    });
    describe.each([
      ['noCache', false],
      ['withCache', true],
    ])('Entity :(%s):', (name: string, hasCace: boolean) => {
      let wrapper: undefined | CoreEntityWrapper<TestEntity>;
      let wrapper2: undefined | CoreEntityWrapper<TestEntityLinked>;
 
      let entity: TestEntity = new TestEntity({
        name: 'Bob',
        age: 30,
        address: 'home',
        time: new Date(),
        raw: null,
        json: { some: 'value' },
      });
      let entity2: TestEntity = new TestEntity({
        name: 'Alice',
        age: 29,
        address: null,
        time: null,
        raw: null,
        json: [{ some: 'value' }, { some: 'array' }],
      });
      let entity3: TestEntityLinked = new TestEntityLinked({
        name: 'Alice',
        age: 29,
        address: null,
        time: null,
        link: null,
        raw: Buffer.from('message'),
        json: null,
        flag: true,
        floating: 0.9,
      });
      let entity4: TestEntity = new TestEntity({
        name: 'Bulk01',
        age: 1,
        address: null,
        time: null,
        raw: null,
        json: null,
      });
      let entity5: TestEntity = new TestEntity({
        name: 'Bulk02',
        age: 1,
        address: null,
        time: null,
        raw: null,
        json: null,
      });
 
      test('get wrapper class', async () => {
        const mod = kernel.getChildModule('testModule') as ICoreKernelModule<
          any,
          any,
          any,
          any,
          any
        >;
        const db = mod.getDb() as CoreDBCon<any, any>;
        expect(
          EntityValidator.validateManualObj(db, 'TestEntity', entity, false),
        ).toBeTruthy();
        wrapper = db.getEntityWrapper<TestEntity>('TestEntity');
        Eif (wrapper) {
          if (hasCace) {
            wrapper.cache = mod.getCache() || new InMemCache(mod);
          } else {
            wrapper.cache = null;
          }
        }
        Iif (wrapper2) {
          if (hasCace) {
            wrapper2.cache = mod.getCache() || new InMemCache(mod);
          } else {
            wrapper2.cache = null;
          }
        }
        wrapper2 = db.getEntityWrapper<TestEntityLinked>('TestEntityLinked');
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect((await wrapper.getObjList()).length).toBe(0);
        }
      });
      test('create new', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          entity = (await wrapper.createObject(entity)) || entity;
          expect(entity?.e_id).not.toBeNull();
          expect((await wrapper.getObjList()).length).toBe(1);
        }
      });
      test('create new - failing', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(
            wrapper!.createObject({
              name: 'FailBob',
              age: 30,
              address: 'home',
              time: new Date(),
              raw: null,
              json: { some: 'value' },
            } as any),
          ).rejects.toThrow();
        }
      });
      test('create new 2', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          entity2 = (await wrapper.createObject(entity2)) || entity2;
          expect(entity2?.e_id).not.toBeNull();
          expect((await wrapper.getObjList()).length).toBe(2);
        }
      });
      test('create new 3', async () => {
        expect(wrapper2).not.toBeUndefined();
        Eif (wrapper2) {
          entity3.link = entity.e_id;
          entity3 = (await wrapper2.createObject(entity3)) || entity3;
          expect(entity2?.e_id).not.toBeNull();
          expect((await wrapper2.getObjList()).length).toBe(1);
        }
      });
      test('create new 4', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          entity4 = (await wrapper.createObject(entity4)) || entity4;
          expect(entity4?.e_id).not.toBeNull();
          expect((await wrapper.getObjList()).length).toBe(3);
        }
      });
      test('create new 5', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          entity5 = (await wrapper.createObject(entity5)) || entity5;
          expect(entity5?.e_id).not.toBeNull();
          expect((await wrapper.getObjList()).length).toBe(4);
        }
      });
 
      test('get by id', async () => {
        expect(wrapper).not.toBeUndefined();
        expect(entity.e_id).not.toBeNull();
        Eif (wrapper) {
          expect(await wrapper.getObjById(entity.e_id)).not.toBeNull();
        }
      });
 
      test('listing search id', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(
            await wrapper.getObjList({
              search: { e_id: entity.e_id },
            }),
          ).toHaveLength(1);
        }
      });
      test('listing search fk id', async () => {
        expect(wrapper2).not.toBeUndefined();
        Eif (wrapper2) {
          expect(
            await wrapper2.getObjList({
              search: { link: entity.e_id },
            }),
          ).toHaveLength(1);
        }
      });
      test('listing search bob', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(
            await wrapper.getObjList({
              search: { name: 'Bob' },
            }),
          ).toHaveLength(1);
        }
      });
      test('listing search multi 01', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(
            await wrapper.getObjList({
              search: { age: 1, name: 'Bulk01', address: null },
            }),
          ).toHaveLength(1);
        }
      });
      test('listing search multi 02', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(
            await wrapper.getObjList({
              search: {
                age: {
                  mode: 'equals',
                  value: 1,
                },
                name: 'Bulk01',
              },
            }),
          ).toHaveLength(1);
        }
      });
      test('listing search multi 03', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(
            await wrapper.getObjList({
              search: {
                age: [
                  {
                    mode: 'equals',
                    value: 1,
                  },
                ],
                name: 'Bulk01',
              },
            }),
          ).toHaveLength(1);
        }
      });
      test('listing search bob like', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(
            await wrapper.getObjList({
              search: {
                name: {
                  value: 'ob',
                  mode: 'like',
                },
              },
            }),
          ).toHaveLength(1);
        }
      });
      test('listing search les age', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(
            await wrapper.getObjList({
              search: {
                age: {
                  value: 30,
                  mode: 'smallerThan',
                },
              },
            }),
          ).toHaveLength(3);
        }
      });
      test('listing search greater age', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(
            await wrapper.getObjList({
              search: {
                age: {
                  value: 29,
                  mode: 'greaterThan',
                },
              },
            }),
          ).toHaveLength(1);
        }
      });
      test('listing search multiple', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(
            await wrapper.getObjList({
              search: {
                age: [
                  {
                    value: 1,
                    mode: 'greaterThan',
                  },
                  {
                    value: 30,
                    mode: 'smallerThan',
                  },
                ],
              },
            }),
          ).toHaveLength(1);
        }
      });
      test('listing search in', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(
            await wrapper.getObjList({
              search: { age: { value: [1, 29], mode: 'in' } },
            }),
          ).toHaveLength(3);
        }
      });
      test('listing search notIn', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(
            await wrapper.getObjList({
              search: { age: { value: [1, 29], mode: 'notIn' } },
            }),
          ).toHaveLength(1);
        }
      });
      test('listing search between', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(
            await wrapper.getObjList({
              search: { age: { value: [1, 29], mode: 'between' } },
            }),
          ).toHaveLength(3);
        }
      });
      test('listing search isNull', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(
            await wrapper.getObjList({
              search: { address: { mode: 'isNull' } },
            }),
          ).toHaveLength(3);
        }
      });
      test('listing search isNotNull', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(
            await wrapper.getObjList({
              search: { address: { mode: 'isNotNull' } },
            }),
          ).toHaveLength(1);
        }
      });
      test('listing search startsWith', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(
            await wrapper.getObjList({
              search: { name: { value: 'B', mode: 'startsWith' } },
            }),
          ).toHaveLength(3);
        }
      });
      test('listing search endsWith', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(
            await wrapper.getObjList({
              search: { name: { value: '01', mode: 'endsWith' } },
            }),
          ).toHaveLength(1);
        }
      });
      test('listing search bob check date', async () => {
        expect(wrapper).not.toBeUndefined();
 
        Eif (wrapper) {
          const bonb = await wrapper.getObjList({
            search: {
              name: {
                value: 'Bob',
                mode: 'equals',
              },
            },
          });
          expect(bonb).toHaveLength(1);
          expect(bonb[0].time).not.toBeNull();
        }
      });
      test('find entity by id', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(
            await wrapper.findObj({
              e_id: {
                value: entity.e_id,
                mode: 'equals',
              },
            }),
          ).not.toBeNull();
        }
      });
 
      test('update', async () => {
        expect(wrapper).not.toBeUndefined();
        expect(entity.e_id).not.toBeNull();
        Eif (wrapper) {
          expect(entity.name).toBe('Bob');
 
          await wrapper.updateObject(entity.e_id, {
            name: 'Bobi',
          });
          const update = await wrapper.getObjById(entity.e_id);
          expect(update).not.toBeNull();
          expect(update?.name).toBe('Bobi');
          expect((await wrapper.getObjList()).length).toBe(4);
        }
      });
 
      test('update-bulk', async () => {
        expect(wrapper).not.toBeUndefined();
        expect(entity4.e_id).not.toBeNull();
        expect(entity5.e_id).not.toBeNull();
        Eif (wrapper) {
          expect(entity4.name).toBe('Bulk01');
          expect(entity5.name).toBe('Bulk02');
 
          await wrapper.updateObjectBulk([entity4.e_id, entity5.e_id], {
            name: 'BulkXX',
          });
          const update = await wrapper.getObjByIdBulk([
            entity4.e_id,
            entity5.e_id,
          ]);
          expect(update.length).toBe(2);
 
          for (const el of update) {
            expect(el).not.toBeNull();
            expect(el?.name).toBe('BulkXX');
          }
          expect((await wrapper.getObjList()).length).toBe(4);
        }
      });
 
      test('count all', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(await wrapper.count()).toBe(4);
        }
      });
      test('count with search', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(await wrapper.count({ age: 1 })).toBe(2);
        }
      });
      test('exists - true', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(await wrapper.exists({ name: 'Bobi' })).toBe(true);
        }
      });
      test('exists - false', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(await wrapper.exists({ name: 'Bob' })).toBe(false);
        }
      });
      test('upsert - update existing', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          const result = await wrapper.upsert(
            { name: 'Bobi' },
            new TestEntity({
              name: 'Bobi',
              age: 31,
              address: 'home',
              time: null,
              raw: null,
              json: null,
            }),
          );
          expect(result.age).toBe(31);
          expect(await wrapper.count()).toBe(4);
          await wrapper.updateObject(result.e_id, { age: 30 });
        }
      });
      test('upsert - create new', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          const result = await wrapper.upsert(
            { name: 'Temp' },
            new TestEntity({
              name: 'Temp',
              age: 5,
              address: null,
              time: null,
              raw: null,
              json: null,
            }),
          );
          expect(result.name).toBe('Temp');
          expect(await wrapper.count()).toBe(5);
          await wrapper.delete(result.e_id);
          expect(await wrapper.count()).toBe(4);
        }
      });
      test('findOrCreate - existing', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          const { entity: et, created } = await wrapper.findOrCreate(
            { name: 'Bobi' },
            new TestEntity({
              name: 'Bobi',
              age: 30,
              address: 'home',
              time: null,
              raw: null,
              json: null,
            }),
          );
          expect(created).toBe(false);
          expect(et.name).toBe('Bobi');
          expect(await wrapper.count()).toBe(4);
        }
      });
      test('findOrCreate - new', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          const { entity: et, created } = await wrapper.findOrCreate(
            { name: 'NewGuy' },
            new TestEntity({
              name: 'NewGuy',
              age: 50,
              address: null,
              time: null,
              raw: null,
              json: null,
            }),
          );
          expect(created).toBe(true);
          expect(et.name).toBe('NewGuy');
          expect(await wrapper.count()).toBe(5);
          await wrapper.delete(et.e_id);
          expect(await wrapper.count()).toBe(4);
        }
      });
      test('delete-bulk', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect((await wrapper.getObjList()).length).toBe(4);
          expect(
            await wrapper.deleteBulk([entity4.e_id, entity5.e_id]),
          ).toBeTruthy();
          expect((await wrapper.getObjList()).length).toBe(2);
        }
      });
      test('delete', async () => {
        expect(wrapper).not.toBeUndefined();
        expect(wrapper2).not.toBeUndefined();
 
        Eif (wrapper2) {
          expect((await wrapper2.getObjList()).length).toBe(1);
          expect(await wrapper2.delete(entity3.e_id)).toBeTruthy();
          expect((await wrapper2.getObjList()).length).toBe(0);
        }
        Eif (wrapper) {
          expect(await wrapper.delete(entity.e_id)).toBeTruthy();
          expect((await wrapper.getObjList()).length).toBe(1);
          expect(await wrapper.delete(entity2.e_id)).toBeTruthy();
          expect((await wrapper.getObjList()).length).toBe(0);
        }
      });
      test('full validation', () => {
        expect(validateEntity(entity)).toBeTruthy();
      });
    });
 
    describe.each([
      ['noCache', false],
      ['withCache', true],
    ])('Bulk Entity :(%s):', (name: string, hasCace: boolean) => {
      let wrapper: undefined | CoreEntityWrapper<TestEntity>;
      const idList: string[] = [];
      const max = 100;
      test('get wrapper class', async () => {
        const mod = kernel.getChildModule('testModule') as ICoreKernelModule<
          any,
          any,
          any,
          any,
          any
        >;
        const db = mod.getDb() as CoreDBCon<any, any>;
        wrapper = db.getEntityWrapper<TestEntity>('TestEntity');
        Eif (wrapper) {
          if (hasCace) {
            wrapper.cache = mod.getCache() || new InMemCache(mod);
          } else {
            wrapper.cache = null;
          }
        }
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect((await wrapper.getObjList()).length).toBe(0);
        }
      });
      test('create new', async () => {
        expect(wrapper).not.toBeUndefined();
        expect(wrapper).not.toBeNull();
        if (wrapper) {
          for (let i = 0; i < max; i++) {
            let entity: TestEntity = new TestEntity({
              name: 'Bob',
              age: i,
              address: 'home',
              time: new Date(),
              raw: null,
              json: { some: 'value', index: i },
            });
            entity = (await wrapper.createObject(entity)) || entity;
            expect(entity.e_id).not.toBeNull();
            idList.push(entity.e_id);
            expect((await wrapper.getObjList()).length).toBe(i + 1);
            expect(
              (
                await wrapper.getObjList({
                  search: { time: entity.time },
                })
              ).length,
            ).toBeGreaterThanOrEqual(1);
          }
        } else E{
          expect(false).toBeTruthy();
        }
      });
      test('listing search id limit 0', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(await wrapper.getObjList({ limit: 0 })).toHaveLength(0);
        }
      });
      test('listing search id limit 1', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(await wrapper.getObjList({ limit: 1 })).toHaveLength(1);
        }
      });
      test('listing search id limit 2', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          expect(await wrapper.getObjList({ limit: 2 })).toHaveLength(2);
        }
      });
      test('test offset', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          const first = (await wrapper.getObjList({ limit: 1 }))[0];
          const sec = (await wrapper.getObjList({ limit: 1, offset: 1 }))[0];
          expect(first.e_id).not.toBe(sec.e_id);
        }
      });
 
      test('listing search id limit ASC DESC', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          const a = await wrapper.getObjList({
            limit: 2,
            order: [{ key: 'e_id', order: 'ASC' }],
          });
          const b = await wrapper.getObjList({
            limit: 2,
            order: [{ key: 'e_id', order: 'DESC' }],
          });
          expect(a.length).toBe(2);
          expect(b.length).toBe(2);
          expect(a[0].e_id !== b[0].e_id).toBeTruthy();
        }
      });
      test('delete', async () => {
        expect(wrapper).not.toBeUndefined();
        Eif (wrapper) {
          for (const el of idList) {
            expect(await wrapper.delete(el)).toBeTruthy();
          }
          expect((await wrapper.getObjList()).length).toBe(0);
        }
      });
    });
    describe('CoreDBPrefab delegating methods', () => {
      let prefab: TestPrefab;
      let wrapper: CoreEntityWrapper<TestEntity>;
      let ent1: TestEntity;
      let ent2: TestEntity;
 
      test('prepare', () => {
        prefab = kernel.getChildModule('testModule').getDb() as TestPrefab;
        wrapper = prefab.getEntityWrapper('TestEntity');
      });
      test('getCurrenDBVersion', async () => {
        expect(typeof (await prefab.getCurrenDBVersion())).toBe('string');
      });
      test('isConnected', () => {
        expect(prefab.isConnected()).toBeTruthy();
      });
      test('execScripts', async () => {
        expect(Array.isArray(await prefab.runScripts())).toBe(true);
      });
      test('queryScript', async () => {
        expect(await prefab.queryScript({ exec: '', param: [] })).toBeDefined();
      });
      test('setConfig, getConfig, configExist, removeConfig', async () => {
        await prefab.setConfig('_cov_key', 'cov_val');
        expect(await prefab.configExist('_cov_key')).toBe(true);
        expect((await prefab.getConfig('_cov_key'))?.c_value).toBe('cov_val');
        await prefab.removeConfig('_cov_key');
        expect(await prefab.configExist('_cov_key')).toBe(false);
      });
      test('getRawDBObject', async () => {
        expect(await prefab.getRawDBObject()).not.toBeNull();
      });
      test('getEntityWrapper', () => {
        expect(prefab.getEntityWrapper('TestEntity')).not.toBeUndefined();
      });
      test('getConfigStore via CoreElement', () => {
        expect(prefab.getConfigStore()).not.toBeNull();
      });
      test('getCClient via CoreElement', () => {
        expect(prefab.getCClient()).not.toBeNull();
      });
      test('createEntity and getEntityById', async () => {
        ent1 = new TestEntity({
          name: 'Prefab1',
          age: 10,
          address: null,
          time: null,
          raw: null,
          json: null,
        });
        ent1 = await wrapper.createObject(ent1);
        expect(ent1.e_id).not.toBeNull();
        const found = await wrapper.getObjById(ent1.e_id);
        expect(found?.name).toBe('Prefab1');
      });
      test('getEntityBulkById', async () => {
        ent2 = new TestEntity({
          name: 'Prefab2',
          age: 20,
          address: null,
          time: null,
          raw: null,
          json: null,
        });
        ent2 = await wrapper.createObject(ent2);
        const bulk = await wrapper.getObjByIdBulk([ent1.e_id, ent2.e_id]);
        expect(bulk.length).toBe(2);
      });
      test('updateEntity', async () => {
        await wrapper.updateObject(ent1.e_id, {
          name: 'PrefabUpdated',
        });
        expect((await wrapper.getObjById(ent1.e_id))?.name).toBe(
          'PrefabUpdated',
        );
      });
      test('updateBulkEntity', async () => {
        expect(
          await wrapper.updateObjectBulk([ent1.e_id, ent2.e_id], {
            name: 'BulkPrefab',
          }),
        ).toBe(true);
      });
      test('getEntityList', async () => {
        expect(Array.isArray(await wrapper.getObjList())).toBe(true);
      });
      test('findEntity', async () => {
        const found = await wrapper.findObj({
          name: 'BulkPrefab',
        });
        expect(found).not.toBeNull();
      });
      test('countEntity', async () => {
        expect(await wrapper.count()).toBeGreaterThanOrEqual(2);
      });
      test('initEntity', async () => {
        expect(await prefab.initEntity('_CovTmp', new TestEntity())).toBe(true);
      });
      test('deleteEntityById', async () => {
        expect(await wrapper.delete(ent1.e_id)).toBe(true);
      });
      test('deleteEntityBulkById', async () => {
        expect(await wrapper.deleteBulk([ent2.e_id])).toBe(true);
      });
      test('countEntity - after', async () => {
        expect(await wrapper.count()).toBe(0);
      });
      test('getCache', () => {
        expect(prefab.getCache() !== undefined).toBe(true);
      });
 
      test('populateMany with empty array returns []', async () => {
        expect(await prefab.populateMany([], 'link')).toHaveLength(0);
      });
      test('populate - field without foreignKey throws', async () => {
        const entity = new TestEntity({
          name: 'T',
          age: 1,
          address: null,
          time: null,
          raw: null,
          json: null,
        });
        await expect(prefab.populate(entity, 'name')).rejects.toThrow();
      });
    });
 
    describe('CoreDBCon error paths', () => {
      test('getEntityWrapper - unknown entity throws', () => {
        const db = kernel.getDb() as any;
        expect(() => db.getEntityWrapper('__NoSuchEntity__')).toThrow();
      });
      test('setUpdateChain - empty args logs and returns', () => {
        const db = kernel.getDb() as any;
        expect(() => db.setUpdateChain()).not.toThrow();
      });
      test('registerEntity - invalid entity (no @Entity decorator) throws', () => {
        const mod = kernel.getChildModule('testModule') as any;
        const innerDb = mod.getDb().getPrefabDB();
        expect(() => innerDb.registerEntity(new BadEntity())).toThrow();
      });
    });
    describe('DBEdgeCase edge cases', () => {
      let db: IDataBase<any, any>;
 
      beforeAll(() => {
        const mod = kernel.getChildModule<TestModule>('testModule');
        db = mod.getDb() as IDataBase<any, any>;
      });
 
      test('runScripts returns empty array', async () => {
        const result = await db.runScripts();
        expect(result).toHaveLength(0);
      });
 
      describe('aFilter modes via getEntityList', () => {
        let wrapper: CoreEntityWrapper<TestEntity>;
        beforeAll(async () => {
          wrapper = await db.registerEntity(new TestEntity(), false);
          await db.setConfig('dbversion', '0');
          await wrapper.createObject(
            new TestEntity({
              address: null,
              json: null,
              raw: null,
              time: null,
              name: 'Alice',
              age: 25,
            }),
          );
          await wrapper.createObject(
            new TestEntity({
              address: null,
              json: null,
              raw: null,
              time: null,
              name: 'Bob',
              age: 30,
            }),
          );
        });
 
        test('not mode excludes matching value', async () => {
          const result = await wrapper.getObjList({
            search: { name: { mode: 'not', value: 'Alice' } } as any,
          });
          expect(result).toHaveLength(1);
          expect((result[0] as any).name).toBe('Bob');
        });
        /*        test('default/unknown mode returns no match', async () => {
          const result = await wrapper.getObjList({
            search: { name: { mode: 'unknownMode', value: 'x' } } as any,
          });
          expect(result).toHaveLength(0);
        }); */
        test('updateEntity - entity not found throws', async () => {
          expect(
            await wrapper.updateObject('nonexistent-id', { name: 'X' }),
          ).toBe(false);
        });
        test('countEntity without search returns total', async () => {
          const count = await wrapper.count();
          expect(count).toBe(2);
        });
        test('getEntityList with offset beyond length returns []', async () => {
          const result = await wrapper.getObjList({
            offset: 9999,
          });
          expect(result).toHaveLength(0);
        });
      });
    });
  });
}