Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct node creation & update (discrimination type) #5

Merged
merged 6 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DbE2eHelper } from "~/app/backend/e2e/db-e2e/db-e2e.helper";
import { GraphHttpClient } from "~/app/backend/e2e/http/clients";
import { GraphArcQueryDto } from "~/lib/common/app/graph/dtos/arc";
import { generateGraphArcsEndpoint } from "~/lib/common/app/graph/endpoints";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind/node-kind.type";
import { BASE_SEED } from "~/lib/common/seeds";

describe("Backend HTTP GraphArcs", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { GraphHttpClient } from "~/app/backend/e2e/http/clients";
import { GraphNodeKindUpdateDto } from "~/lib/common/app/graph/dtos/node";
import { generateGraphNodesEndpoint } from "~/lib/common/app/graph/endpoints";
import { NodeQueryDto, NodeUpdateDto } from "~/lib/common/app/node/dtos";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind/node-kind.type";
import { omit } from "~/lib/common/utils/object-fns";

describe("Backend HTTP GraphNodes", () => {
Expand Down
64 changes: 64 additions & 0 deletions apps/backend-e2e/src/backend/http/v1/nodes.http.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { DbE2eHelper } from "~/app/backend/e2e/db-e2e/db-e2e.helper";
import { NodeHttpClient } from "~/app/backend/e2e/http/clients/node.http-client";
import { NodeCreateDto, NodeUpdateDto } from "~/lib/common/app/node/dtos";
import { NodeBehaviorType } from "~/lib/common/app/node/dtos/behaviors/node-behavior.type";
import { NodeKindEdgeDto } from "~/lib/common/app/node/dtos/kind";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind/node-kind.type";
import { BASE_SEED } from "~/lib/common/seeds";
import { jsonify } from "~/lib/common/utils/jsonify";

describe("Backend HTTP Nodes", () => {
const client = new NodeHttpClient();

const dbHelper = DbE2eHelper.getHelper("base");
const db = jsonify(BASE_SEED);

beforeAll(async () => {
const [{ email, password }] = db.users;

await dbHelper.refresh();
await client.setAuth(email, password);
});

describe("POST", () => {
beforeEach(() => dbHelper.refresh());

it("should create a node (different `kinds`)", async () => {
for (const toCreate of [
{
behavior: { type: NodeBehaviorType.VARIABLE, value: 1 },
kind: { __graph: 1, position: { x: 0, y: 0 }, type: NodeKindType.EDGE },
name: "-new node1"
},
{
behavior: { type: NodeBehaviorType.VARIABLE, value: 1 },
kind: { active: false, type: NodeKindType.TEMPLATE },
name: "-new node2"
}
] satisfies NodeCreateDto[]) {
const { behavior, kind, name } = await client.create(toCreate);

expect(name).toBe(toCreate.name);
expect(behavior).toStrictEqual(toCreate.behavior);
expect(kind).toStrictEqual(toCreate.kind);
}
});
});

describe("PATCH", () => {
beforeEach(() => dbHelper.refresh());

it("should update a node (kind = edge)", async () => {
const node = db.graph.nodes[0];

const toUpdate = {
kind: { position: { x: 250, y: 250 }, type: NodeKindType.EDGE }
} as const satisfies NodeUpdateDto;
const updated = await client.update(node._id, toUpdate);

const { position } = updated.kind as NodeKindEdgeDto;
expect(position.x).toBe(toUpdate.kind.position.x);
expect(position.y).toBe(toUpdate.kind.position.y);
});
});
});
10 changes: 10 additions & 0 deletions apps/backend-e2e/src/support/http/clients/node.http-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { NodeDto } from "~/lib/common/app/node/dtos";
import { NODES_ENDPOINT_PREFIX } from "~/lib/common/app/node/endpoints";

import { EntityHttpClient } from "./_lib/entity.http-client";

export class NodeHttpClient extends EntityHttpClient<NodeDto> {
public override getEndpoint(): string {
return NODES_ENDPOINT_PREFIX;
}
}
2 changes: 1 addition & 1 deletion apps/backend/src/app/graph/arc/graph-arc.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
GraphArcResultsDto
} from "~/lib/common/app/graph/dtos/arc";
import { generateGraphArcsEndpoint, GraphArcEndpoint } from "~/lib/common/app/graph/endpoints";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind/node-kind.type";
import { EntityId } from "~/lib/common/dtos/entity";
import { UnshiftParameters } from "~/lib/common/types";

Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/app/graph/arc/graph-arc.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
import { MethodNotAllowedException } from "@nestjs/common";
import { Test, TestingModule } from "@nestjs/testing";
import { GraphArcCreateDto } from "~/lib/common/app/graph/dtos/arc";
import { NodeBehaviorType } from "~/lib/common/app/node/dtos/behaviors";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind";
import { NodeBehaviorType } from "~/lib/common/app/node/dtos/behaviors/node-behavior.type";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind/node-kind.type";
import { BASE_SEED, MockSeed } from "~/lib/common/seeds";

import { GraphArcDifferentGraphException } from "./exceptions";
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/app/graph/arc/graph-arc.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { graphHasCycle } from "~/lib/common/app/graph/algorithms";
import { GraphArcCreateDto } from "~/lib/common/app/graph/dtos/arc";
import { getAdjacencyList } from "~/lib/common/app/graph/transformations";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind/node-kind.type";
import { EntityId } from "~/lib/common/dtos/entity";
import { EntitiesToPopulate, EntityFilter, EntityFindParams } from "~/lib/common/endpoints";

Expand Down
5 changes: 1 addition & 4 deletions apps/backend/src/app/graph/exceptions/graph.exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ export class GraphException extends BadLogicException {
* @param message An additional message to the exception
*/
public constructor(code: GraphErrorCode, message?: string) {
super({
code,
message
});
super({ code, message });
}
}
4 changes: 2 additions & 2 deletions apps/backend/src/app/graph/executor/graph.executor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Test } from "@nestjs/testing";
import { lastValueFrom, toArray } from "rxjs";
import { NodeBehaviorType } from "~/lib/common/app/node/dtos/behaviors";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind";
import { NodeBehaviorType } from "~/lib/common/app/node/dtos/behaviors/node-behavior.type";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind/node-kind.type";
import { BASE_SEED } from "~/lib/common/seeds";

import { GraphExecutorStartingNodeException } from "./exceptions";
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/app/graph/node/graph-node.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { GraphNodeUpdateDto } from "~/lib/common/app/graph/dtos/node";
import { GraphNodeCreateDto } from "~/lib/common/app/graph/dtos/node/graph-node.create.dto";
import { generateGraphNodesEndpoint, GraphNodeEndpoint } from "~/lib/common/app/graph/endpoints";
import { NodeDto, NodeQueryDto } from "~/lib/common/app/node/dtos";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind/node-kind.type";
import { EntityId } from "~/lib/common/dtos/entity";
import { UnshiftParameters } from "~/lib/common/types";

Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/app/node/behaviors/node-behavior.base.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Entity, EntityOptions, Enum, OneToOne } from "@mikro-orm/core";
import {
NodeBehaviorBaseDto,
NodeBehaviorDiscriminatorKey,
NodeBehaviorType
NodeBehaviorDiscriminatorKey
} from "~/lib/common/app/node/dtos/behaviors";
import { NodeBehaviorType } from "~/lib/common/app/node/dtos/behaviors/node-behavior.type";

import { NodeEntity } from "../node.entity";

Expand Down
3 changes: 2 additions & 1 deletion apps/backend/src/app/node/behaviors/node-behavior.code.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Entity, Property } from "@mikro-orm/core";
import { NodeBehaviorCodeDto as DTO, NodeBehaviorType } from "~/lib/common/app/node/dtos/behaviors";
import { NodeBehaviorCodeDto as DTO } from "~/lib/common/app/node/dtos/behaviors";
import { NodeBehaviorType } from "~/lib/common/app/node/dtos/behaviors/node-behavior.type";

import { NodeBehaviorBase } from "./node-behavior.base";

Expand Down
6 changes: 2 additions & 4 deletions apps/backend/src/app/node/behaviors/node-behavior.function.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Entity, OneToOne } from "@mikro-orm/core";
import { applyDecorators } from "@nestjs/common";
import {
NodeBehaviorFunctionDto as DTO,
NodeBehaviorType
} from "~/lib/common/app/node/dtos/behaviors";
import { NodeBehaviorFunctionDto as DTO } from "~/lib/common/app/node/dtos/behaviors";
import { NodeBehaviorType } from "~/lib/common/app/node/dtos/behaviors/node-behavior.type";

import { NodeBehaviorBase } from "./node-behavior.base";
import { ManyToOneParams } from "../../_lib/entity/decorators";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Entity } from "@mikro-orm/core";
import { NodeDto } from "~/lib/common/app/node/dtos";
import {
NodeBehaviorReferenceDto as DTO,
NodeBehaviorType
} from "~/lib/common/app/node/dtos/behaviors";
import { NodeBehaviorReferenceDto as DTO } from "~/lib/common/app/node/dtos/behaviors";
import { NodeBehaviorType } from "~/lib/common/app/node/dtos/behaviors/node-behavior.type";
import { EntityId } from "~/lib/common/dtos/entity";

import { NodeBehaviorBase } from "./node-behavior.base";
Expand Down
6 changes: 2 additions & 4 deletions apps/backend/src/app/node/behaviors/node-behavior.trigger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Embedded, Entity } from "@mikro-orm/core";
import {
NodeBehaviorTriggerDto as DTO,
NodeBehaviorType
} from "~/lib/common/app/node/dtos/behaviors";
import { NodeBehaviorTriggerDto as DTO } from "~/lib/common/app/node/dtos/behaviors";
import { NodeBehaviorType } from "~/lib/common/app/node/dtos/behaviors/node-behavior.type";

import { NodeBehaviorBase } from "./node-behavior.base";
import { NODE_TRIGGER_ENTITIES, NodeTriggerEntity } from "./triggers";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Entity } from "@mikro-orm/core";
import {
NodeBehaviorParameterBaseDto,
NodeBehaviorParameterType
} from "~/lib/common/app/node/dtos/behaviors";
import { NodeBehaviorParameterBaseDto } from "~/lib/common/app/node/dtos/behaviors";
import { NodeBehaviorParameterType } from "~/lib/common/app/node/dtos/behaviors/node-behavior.type";

import { NODE_BEHAVIOR_ENTITY_OPTIONS, NodeBehaviorBase } from "../node-behavior.base";

Expand All @@ -14,4 +12,4 @@ export abstract class NodeBehaviorParameterBase<
T extends NodeBehaviorParameterType = NodeBehaviorParameterType
>
extends NodeBehaviorBase<T>
implements NodeBehaviorParameterBaseDto {}
implements NodeBehaviorParameterBaseDto<T> {}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Entity } from "@mikro-orm/core";
import {
NodeBehaviorParameterInputDto,
NodeBehaviorType
} from "~/lib/common/app/node/dtos/behaviors";
import { NodeBehaviorParameterInputDto } from "~/lib/common/app/node/dtos/behaviors";
import { NodeBehaviorType } from "~/lib/common/app/node/dtos/behaviors/node-behavior.type";

import { NodeBehaviorParameterBase } from "./node-behavior.parameter-base";
import { ManyToOneFactory } from "../../../_lib/entity/decorators";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Entity } from "@mikro-orm/core";
import {
NodeBehaviorParameterOutputDto,
NodeBehaviorType
} from "~/lib/common/app/node/dtos/behaviors";
import { NodeBehaviorParameterOutputDto } from "~/lib/common/app/node/dtos/behaviors";
import { NodeBehaviorType } from "~/lib/common/app/node/dtos/behaviors/node-behavior.type";

import { NodeBehaviorParameterBase } from "./node-behavior.parameter-base";
import { ManyToOneFactory } from "../../../_lib/entity/decorators";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Entity, Property } from "@mikro-orm/core";
import { NodeBehaviorType, NodeBehaviorVariableDto } from "~/lib/common/app/node/dtos/behaviors";
import { NodeBehaviorVariableDto } from "~/lib/common/app/node/dtos/behaviors";
import { NodeBehaviorType } from "~/lib/common/app/node/dtos/behaviors/node-behavior.type";
import { NodeIoValue } from "~/lib/common/app/node/io";

import { NodeBehaviorParameterBase } from "./node-behavior.parameter-base";
Expand Down
2 changes: 2 additions & 0 deletions apps/backend/src/app/node/exceptions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./node.exception";
export * from "./node.readonly-kind-type.exception";
18 changes: 18 additions & 0 deletions apps/backend/src/app/node/exceptions/node.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { NodeErrorCode } from "~/lib/common/app/node/error-codes";

import { BadLogicException } from "../../_lib/exceptions";

/**
* An exception to use when related to "graph"
*/
export class NodeException extends BadLogicException {
/**
* Creates the exception
*
* @param code Error code for the exception
* @param message An additional message to the exception
*/
public constructor(code: NodeErrorCode, message?: string) {
super({ code, message });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { NodeErrorCode } from "~/lib/common/app/node/error-codes";

import { NodeException } from "./node.exception";

/**
* An exception when someone tries to change the node-kind type
*/
export class NodeReadonlyKindTypeException extends NodeException {
/**
* Creates the exception
*/
public constructor() {
super(NodeErrorCode.KIND_TYPE_READONLY, "The kind-type of a node can not be changed");
}
}
4 changes: 2 additions & 2 deletions apps/backend/src/app/node/executor/node.executor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Test } from "@nestjs/testing";
import { NodeBehaviorType } from "~/lib/common/app/node/dtos/behaviors";
import { NodeBehaviorType } from "~/lib/common/app/node/dtos/behaviors/node-behavior.type";
import { NodeTriggerType } from "~/lib/common/app/node/dtos/behaviors/triggers";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind/node-kind.type";
import { BASE_SEED } from "~/lib/common/seeds";

import { NodeExecutorMissingInputException } from "./exceptions";
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/app/node/executor/node.executor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { forwardRef, Inject, Injectable } from "@nestjs/common";
import { filter, lastValueFrom, toArray } from "rxjs";
import { NodeBehaviorType } from "~/lib/common/app/node/dtos/behaviors";
import { NodeBehaviorType } from "~/lib/common/app/node/dtos/behaviors/node-behavior.type";
import { NodeTriggerType } from "~/lib/common/app/node/dtos/behaviors/triggers";
import { castNodeIoValueTo, NODE_IO_VOID, NodeIoType, NodeIoValue } from "~/lib/common/app/node/io";
import { EntityId } from "~/lib/common/dtos/entity";
Expand Down
7 changes: 2 additions & 5 deletions apps/backend/src/app/node/kind/node-kind.base.entity.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Entity, Enum, OneToOne } from "@mikro-orm/core";
import {
NodeKindBaseDto,
NodeKindDiscriminatorKey,
NodeKindType
} from "~/lib/common/app/node/dtos/kind";
import { NodeKindBaseDto, NodeKindDiscriminatorKey } from "~/lib/common/app/node/dtos/kind";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind/node-kind.type";

import { NodeEntity } from "../node.entity";

Expand Down
3 changes: 2 additions & 1 deletion apps/backend/src/app/node/kind/node-kind.edge.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Embedded, Entity } from "@mikro-orm/core";
import { NodeKindEdgeDto, NodeKindType } from "~/lib/common/app/node/dtos/kind";
import { NodeKindEdgeDto } from "~/lib/common/app/node/dtos/kind";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind/node-kind.type";
import { EntityId } from "~/lib/common/dtos/entity";

import { NodeKindBaseEntity } from "./node-kind.base.entity";
Expand Down
3 changes: 2 additions & 1 deletion apps/backend/src/app/node/kind/node-kind.template.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Entity, Property } from "@mikro-orm/core";
import { NodeKindTemplateDto, NodeKindType } from "~/lib/common/app/node/dtos/kind";
import { NodeKindTemplateDto } from "~/lib/common/app/node/dtos/kind";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind/node-kind.type";

import { NodeKindBaseEntity } from "./node-kind.base.entity";

Expand Down
18 changes: 16 additions & 2 deletions apps/backend/src/app/node/node.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { NotFoundError } from "@mikro-orm/core";
import { Test, TestingModule } from "@nestjs/testing";
import { GraphNode } from "~/lib/common/app/graph/endpoints";
import { NodeCreateDto, NodeUpdateDto } from "~/lib/common/app/node/dtos";
import { NodeBehaviorType } from "~/lib/common/app/node/dtos/behaviors";
import { NodeBehaviorType } from "~/lib/common/app/node/dtos/behaviors/node-behavior.type";
import { NodeTriggerType } from "~/lib/common/app/node/dtos/behaviors/triggers";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind";
import { NodeKindType } from "~/lib/common/app/node/dtos/kind/node-kind.type";
import { BASE_SEED } from "~/lib/common/seeds";

import { NodeReadonlyKindTypeException } from "./exceptions";
import { NodeInputRepository } from "./input";
import { NodeKindEdgeEntity } from "./kind";
import { NodeModule } from "./node.module";
Expand Down Expand Up @@ -292,6 +293,19 @@ describe("NodeService", () => {
});
});

describe("Kind", () => {
beforeEach(() => dbTest.refresh());

it("should not allow to change the type on an update", async () => {
const node = await service.findById(db.graph.nodes[0]._id);
expect(node.kind.type).toBe(NodeKindType.EDGE);

await expect(() =>
service.update(node._id, { kind: { active: true, type: NodeKindType.TEMPLATE } })
).rejects.toThrow(NodeReadonlyKindTypeException);
});
});

describe("CRUD basic", () => {
describe("Read", () => {
beforeEach(() => dbTest.refresh());
Expand Down
Loading
Loading