All files / src/dev/lib orm.ts

100% Statements 49/49
100% Branches 0/0
100% Functions 14/14
100% Lines 49/49

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                          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 } from 'vitest';
import BadEntity from '../testClass/db/entity/BadEntity';
import {
  getColumnMeta,
  getEntityMeta,
  instanceOfEntity,
  validateColumnMeta,
  validateEntity,
} from '../../classes';
import TestEnt from '../testClass/db/entity/TestEnt';
import { XUtil } from '../../utils';
 
export default function jestOrm() {
  describe('orm', () => {
    describe('annotation', () => {
      const first = new TestEnt({
        name: 'Bob',
        notAColumn: 'test',
        simpleNumber: 1,
        invalidKey: 0.1,
      });
      test('entity props', () => {
        const classMeta = getEntityMeta(first);
        expect(classMeta?.name).toBe(first.constructor.name);
        expect(classMeta?.version).toBe(1);
      });
      test('entity props empty', () => {
        const classMeta = getEntityMeta(new BadEntity());
        expect(classMeta).toBeUndefined();
      });
      test('instance test', () => {
        expect(instanceOfEntity(new TestEnt())).toBeTruthy();
      });
      test('util test', () => {
        expect(XUtil.camelToSnakeCase('')).toBe('');
      });
      test('e_id is primary key', () => {
        const meta = getColumnMeta(first, 'e_id');
        expect(meta).not.toBeUndefined();
        expect(meta?.primaryKey).toBeTruthy();
        expect(validateColumnMeta(meta)).toBeTruthy();
      });
      test('name is valid', () => {
        const meta = getColumnMeta(first, 'name');
        expect(meta).not.toBeUndefined();
        expect(meta?.canBeNull).toBeTruthy();
        expect(meta?.dataType).toBe('text');
        expect(validateColumnMeta(meta)).toBeTruthy();
      });
      test('detect key not a column', () => {
        const meta = getColumnMeta(first, 'notAColumn');
        expect(meta).toBeUndefined();
        expect(validateColumnMeta(meta)).toBeFalsy();
      });
      test('simple data type', () => {
        const meta = getColumnMeta(first, 'simpleNumber');
        expect(meta).not.toBeUndefined();
        expect(meta).toStrictEqual({
          dataType: 'int',
        });
        expect(validateColumnMeta(meta)).toBeTruthy();
      });
 
      test('detect primaryKeyNull', () => {
        const meta = getColumnMeta(first, 'primaryKeyNull');
        expect(meta).not.toBeUndefined();
        expect(meta?.canBeNull).toBeTruthy();
        expect(meta?.primaryKey).toBeTruthy();
 
        expect(validateColumnMeta(meta)).toBeFalsy();
      });
      test('detect invalidKey', () => {
        const meta = getColumnMeta(first, 'invalidKey');
        expect(meta).not.toBeUndefined();
        expect(meta?.canBeNull).toBeTruthy();
        expect(meta?.dataType).toBe('float');
        expect(meta?.foreignKey).not.toBeUndefined();
        expect(validateColumnMeta(meta)).toBeFalsy();
      });
      test('full validation', () => {
        expect(validateEntity(first)).toBeFalsy();
      });
    });
  });
}