import { IEntity } from '@antjs/ant-js/src/model/IEntity'; import { IKeyGenParams } from '@antjs/ant-js/src/model/IKeyGenParams'; import { AntMySqlSecondaryEntityManager } from '../../../persistence/secondary/AntMySqlSecondaryEntityManager'; import { KnexDriver } from '../../../persistence/secondary/KnexDriver'; import { SecondaryEntityManagerHelper } from '../../../persistence/secondary/SecondayEntityManagerHelper'; import { dbServerAwaiterManager } from '../../await/DbServerAwaiterManager'; import { ITest } from '../../ITest'; import { modelGenerator } from '../../model/AntSqlModelGenerator'; import { DBConnectionWrapper } from './DBConnectionWrapper'; import { DBTestManager } from './DBTestManager'; const MAX_SAFE_TIMEOUT = Math.pow(2, 31) - 1; const namedModelGenerator = ( keyGen: IKeyGenParams, autoGeneratedId: boolean = false, ) => modelGenerator(keyGen, ['name'], autoGeneratedId ? 'id' : null); const tableGeneratorOtherColumns: { [key: string]: 'string' | 'number'; } = { name: 'string' }; type NamedEntityTest = {id: number, name: string} & IEntity; export class SecondaryEntityManagerHelperTest implements ITest { protected _dbConnectionWrapper: DBConnectionWrapper; protected _dbTestManager: DBTestManager; protected _declareName: string; public constructor() { this._dbConnectionWrapper = new DBConnectionWrapper(); this._dbTestManager = new DBTestManager(); this._declareName = SecondaryEntityManagerHelperTest.name; } public performTests(): void { describe(this._declareName, () => { this._itMustBeInitializable(); this._itMustInsertMultipleEntitiesOnAutoIncrementWithNoMultipleReturning(); this._itMustInsertZeroEntitiesOnAutoIncrementWithNoMultipleReturning(); }); } private _itMustBeInitializable(): void { const itsName = 'mustBeInitializable'; it(itsName, async (done) => { expect(() => { // tslint:disable-next-line:no-unused-expression new SecondaryEntityManagerHelper(null, null); }).not.toThrowError(); done(); }, MAX_SAFE_TIMEOUT); } private _itMustInsertMultipleEntitiesOnAutoIncrementWithNoMultipleReturning(): void { const itsName = 'mustInsertMultipleEntitiesOnAutoIncrementWithNoMultipleReturning'; const prefix = this._declareName + '/' + itsName + '/'; it(itsName, async (done) => { await dbServerAwaiterManager.getTablesDeletedAwaiter(KnexDriver.MYSQL); const model = namedModelGenerator({ prefix: prefix }, true); const entity: NamedEntityTest = { id: undefined, name: 'name' }; const entity2: NamedEntityTest = { id: undefined, name: 'name2' }; await this._dbTestManager.createTable( this._dbConnectionWrapper.mySqlConfig.connection, model.tableName, { name: 'id', type: 'increments' }, tableGeneratorOtherColumns, ); const secondaryEntityManager = new AntMySqlSecondaryEntityManager( model, this._dbConnectionWrapper.mySqlConfig.connection, ); const helper = new SecondaryEntityManagerHelper( model, this._dbConnectionWrapper.mySqlConfig.connection, ); await helper.mInsertOnAutoincrementWithNoMultipleReturning( [entity, entity2], model.getAutoGeneratedColumn(), ); const entityFound = await secondaryEntityManager.getById(entity.id); const entityFound2 = await secondaryEntityManager.getById(entity2.id); expect(typeof entityFound.id).toBe('number'); expect(entityFound.name).toBe(entity.name); expect(typeof entityFound2.id).toBe('number'); expect(entityFound2.name).toBe(entity2.name); done(); }, MAX_SAFE_TIMEOUT); } private _itMustInsertZeroEntitiesOnAutoIncrementWithNoMultipleReturning(): void { const itsName = 'mustInsertZeroEntitiesOnAutoIncrementWithNoMultipleReturning'; const prefix = this._declareName + '/' + itsName + '/'; it(itsName, async (done) => { await dbServerAwaiterManager.getTablesDeletedAwaiter(KnexDriver.MYSQL); const model = namedModelGenerator({ prefix: prefix }, true); await this._dbTestManager.createTable( this._dbConnectionWrapper.mySqlConfig.connection, model.tableName, { name: 'id', type: 'increments' }, tableGeneratorOtherColumns, ); const helper = new SecondaryEntityManagerHelper( model, this._dbConnectionWrapper.mySqlConfig.connection, ); expectAsync( helper.mInsertOnAutoincrementWithNoMultipleReturning( new Array(), model.getAutoGeneratedColumn(), ), ).toBeResolved(); done(); }, MAX_SAFE_TIMEOUT); } }