webgl test and class validator mocker
This commit is contained in:
parent
b9a89a4ba7
commit
3ff2186deb
17 changed files with 368 additions and 72 deletions
|
@ -76,7 +76,9 @@ export class App {
|
|||
}
|
||||
|
||||
async loadAppDependencies() {
|
||||
await new DataBaseConnectUseCase().call();
|
||||
if ((await new DataBaseConnectUseCase().call()).isFailure()) {
|
||||
console.log("database connect error");
|
||||
}
|
||||
await new CheckAndCreateStaticFilesFolderUseCase().call();
|
||||
await new SetLastActivePipelineToRealTimeServiceScenario().call();
|
||||
}
|
||||
|
|
82
server/src/core/helpers/class_validator_mocket.ts
Normal file
82
server/src/core/helpers/class_validator_mocket.ts
Normal file
|
@ -0,0 +1,82 @@
|
|||
import { randomBytes, randomInt, randomUUID } from "crypto";
|
||||
import { getMetadataStorage, IS_BOOLEAN, IS_MONGO_ID, IS_NUMBER, IS_STRING, IS_UUID } from "class-validator";
|
||||
import { ValidationMetadata } from "class-validator/types/metadata/ValidationMetadata";
|
||||
|
||||
type AvailableTypes = string | number | boolean | undefined;
|
||||
|
||||
export class ClassValidatorMocker {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
public static create<T>(constructor: Function, partial: Partial<T> = {}): T {
|
||||
return new ClassValidatorMocker().create(constructor, partial);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
public create<T>(constructor: Function, partial: Partial<T> = {}): T {
|
||||
const metadataStorage = getMetadataStorage();
|
||||
const targetMetadatas = metadataStorage.getTargetValidationMetadatas(constructor, "", false, false);
|
||||
const groupedMetadatas = metadataStorage.groupByPropertyName(targetMetadatas);
|
||||
// nestedValidation
|
||||
console.log(targetMetadatas);
|
||||
let randomFixture = {} as T;
|
||||
|
||||
for (const propertyName of Object.keys(groupedMetadatas)) {
|
||||
const metadatas = groupedMetadatas[propertyName];
|
||||
const value = this.generatePropertyValueFromMetadatas(metadatas);
|
||||
|
||||
if (value !== undefined) {
|
||||
randomFixture = {
|
||||
...randomFixture,
|
||||
[propertyName]: value,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return { ...randomFixture, ...partial };
|
||||
}
|
||||
|
||||
private generatePropertyValueFromMetadatas(metadatas: ValidationMetadata[]): AvailableTypes {
|
||||
for (const metadata of metadatas) {
|
||||
const constraints = getMetadataStorage().getTargetValidatorConstraints(metadata.constraintCls);
|
||||
|
||||
for (const constraint of constraints) {
|
||||
switch (constraint.name) {
|
||||
case IS_MONGO_ID:
|
||||
return this.randomUUID();
|
||||
case IS_STRING:
|
||||
return this.randomString();
|
||||
case IS_NUMBER:
|
||||
return this.randomNumber();
|
||||
case IS_BOOLEAN:
|
||||
return this.randomBoolean();
|
||||
case IS_UUID:
|
||||
return this.randomUUID();
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private randomString(): string {
|
||||
return randomBytes(randomInt(1, 10)).toString("hex");
|
||||
}
|
||||
|
||||
private randomNumber(): number {
|
||||
return randomInt(0, 99_999);
|
||||
}
|
||||
|
||||
private randomBoolean(): boolean {
|
||||
return randomInt(0, 1) === 1;
|
||||
}
|
||||
|
||||
private randomUUID(): string {
|
||||
if (randomUUID != null) {
|
||||
return randomUUID();
|
||||
}
|
||||
|
||||
return randomBytes(16).toString("hex");
|
||||
}
|
||||
}
|
|
@ -2,7 +2,7 @@ import { IsOptional, ValidateNested } from "class-validator";
|
|||
import { IPipeline, IProcess, StackGenerateType } from "../../../core/models/process_model";
|
||||
import { Type } from "class-transformer";
|
||||
import { ProcessModel } from "../../process/models/process_validation_model";
|
||||
import { TriggerModel } from "../../triggers/models/trigger_validation_model";
|
||||
import { TriggerModelValidationModel } from "../../triggers/models/trigger_validation_model";
|
||||
|
||||
export class PipelineModel implements IPipeline {
|
||||
@ValidateNested()
|
||||
|
@ -10,8 +10,8 @@ export class PipelineModel implements IPipeline {
|
|||
public process: IProcess;
|
||||
|
||||
@ValidateNested()
|
||||
@Type(() => TriggerModel)
|
||||
public trigger: TriggerModel;
|
||||
@Type(() => TriggerModelValidationModel)
|
||||
public trigger: TriggerModelValidationModel;
|
||||
|
||||
@IsOptional()
|
||||
public env = null;
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
import { IsMongoId, IsOptional } from "class-validator";
|
||||
import { IProcess, StackGenerateType } from "../../../core/models/process_model";
|
||||
import { TriggerModel } from "../../triggers/models/trigger_validation_model";
|
||||
import { TriggerModelValidationModel } from "../../triggers/models/trigger_validation_model";
|
||||
|
||||
export class PipelineValidationModel {
|
||||
@IsMongoId()
|
||||
public process: IProcess;
|
||||
|
||||
@IsMongoId()
|
||||
public trigger: TriggerModel;
|
||||
public trigger: TriggerModelValidationModel;
|
||||
|
||||
@IsOptional()
|
||||
public env = null;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { IsArray, IsOptional, IsEnum, IsString } from "class-validator";
|
||||
import { ITriggerModel, TriggerType } from "./trigger_database_model";
|
||||
|
||||
export class TriggerModel implements ITriggerModel {
|
||||
export class TriggerModelValidationModel implements ITriggerModel {
|
||||
@IsOptional()
|
||||
public _id: string;
|
||||
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
import { CrudController } from "../../core/controllers/crud_controller";
|
||||
import { TriggerDBModel } from "./models/trigger_database_model";
|
||||
import { TriggerModel } from "./models/trigger_validation_model";
|
||||
import { TriggerModelValidationModel as TriggerValidationMode } from "./models/trigger_validation_model";
|
||||
|
||||
export class TriggerPresentation extends CrudController<TriggerModel, typeof TriggerDBModel> {
|
||||
export class TriggerPresentation extends CrudController<TriggerValidationMode, typeof TriggerDBModel> {
|
||||
constructor() {
|
||||
super({
|
||||
url: "trigger",
|
||||
validationModel: TriggerModel,
|
||||
validationModel: TriggerValidationMode,
|
||||
databaseModel: TriggerDBModel,
|
||||
});
|
||||
}
|
||||
|
|
|
@ -9,11 +9,11 @@ import { ProcessPresentation } from "./features/process/process_presentation";
|
|||
import { RealTimePresentation, pipelineRealTimeService } from "./features/realtime/realtime_presentation";
|
||||
import { extensions } from "./core/extensions/extensions";
|
||||
import { ProjectInstancePresentation } from "./features/project_instance/project_instance_presentation";
|
||||
import { NixStoreManagerPresentation as NixStoreManagerPresentation } from "./features/nix_store_manager/nix_store_manager";
|
||||
import { NixStoreManagerPresentation } from "./features/nix_store_manager/nix_store_manager";
|
||||
|
||||
extensions();
|
||||
|
||||
const httpRoutes: Routes[] = [
|
||||
export const httpRoutes: Routes[] = [
|
||||
new TriggerPresentation(),
|
||||
new ProjectsPresentation(),
|
||||
new ProcessPresentation(),
|
||||
|
|
25
server/test/helper/class_validator_mocker_test.ts
Normal file
25
server/test/helper/class_validator_mocker_test.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
import { Type } from "class-transformer";
|
||||
import { ClassValidatorMocker } from "../../src/core/helpers/class_validator_mocket";
|
||||
import { IsString, IsNumber, IsBoolean, IsUUID, IsMongoId, ValidateNested } from "class-validator";
|
||||
|
||||
class Foo {}
|
||||
class MyClass {
|
||||
@ValidateNested()
|
||||
@Type(() => Foo)
|
||||
model: Foo;
|
||||
|
||||
@IsNumber()
|
||||
numberProperty: number;
|
||||
|
||||
@IsBoolean()
|
||||
booleanProperty: boolean;
|
||||
|
||||
@IsUUID()
|
||||
uuidProperty: string;
|
||||
}
|
||||
|
||||
const myClassDataMock = ClassValidatorMocker.create<MyClass>(MyClass);
|
||||
|
||||
export const mainTest = () => {
|
||||
console.log(myClassDataMock);
|
||||
};
|
|
@ -1,3 +1,4 @@
|
|||
import "reflect-metadata";
|
||||
import { TestCore } from "./core/test_core";
|
||||
// import { UnitTestEnv } from "../src/core/di/env";
|
||||
import { dirname } from "path";
|
||||
|
@ -13,6 +14,7 @@ import { UpdateDataBaseModelUseCaseTest } from "./usecases/update_database_model
|
|||
import { PaginationDataBaseModelUseCaseTest } from "./usecases/pagination_database_model_usecase_test";
|
||||
import { extensions } from "../src/core/extensions/extensions";
|
||||
import { DataBaseConnectUseCase } from "../src/core/usecases/database_connect_usecase";
|
||||
import { mainTest } from "./helper/class_validator_mocker_test";
|
||||
|
||||
extensions();
|
||||
|
||||
|
@ -34,20 +36,22 @@ const init = async () => {
|
|||
};
|
||||
|
||||
const test = async () => {
|
||||
await new ExecutorProgramServiceTest(dirname__).test();
|
||||
await new FilesChangerTest(dirname__).test();
|
||||
await new StackServiceTest(dirname__ + "/context/").test();
|
||||
await new TriggerServiceTest().test();
|
||||
await new CreateDataBaseModelUseCaseTest().test();
|
||||
// await new ExecutorProgramServiceTest(dirname__).test();
|
||||
// await new FilesChangerTest(dirname__).test();
|
||||
// await new StackServiceTest(dirname__ + "/context/").test();
|
||||
// await new TriggerServiceTest().test();
|
||||
// await new CreateDataBaseModelUseCaseTest().test();
|
||||
|
||||
await new CreateDataBaseModelUseCaseTest().test();
|
||||
await new DeleteDataBaseModelUseCaseTest().test();
|
||||
await new ReadDataBaseModelUseCaseTest().test();
|
||||
await new UpdateDataBaseModelUseCaseTest().test();
|
||||
// await new PipelineRealTimeServiceTest().test()
|
||||
for await (const usecase of tests) {
|
||||
testCore.assert(await new usecase().test(), usecase.name);
|
||||
}
|
||||
// await new CreateDataBaseModelUseCaseTest().test();
|
||||
// await new DeleteDataBaseModelUseCaseTest().test();
|
||||
// await new ReadDataBaseModelUseCaseTest().test();
|
||||
// await new UpdateDataBaseModelUseCaseTest().test();
|
||||
// // await new PipelineRealTimeServiceTest().test()
|
||||
// for await (const usecase of tests) {
|
||||
// testCore.assert(await new usecase().test(), usecase.name);
|
||||
// }
|
||||
|
||||
mainTest();
|
||||
};
|
||||
const main = async () => {
|
||||
await init();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue