-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: resolve issue when enum that has functions is used in entity (#7653
) Closes: #7651
- Loading branch information
Showing
4 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Column, Entity, PrimaryColumn } from "../../../../src"; | ||
import { Order } from "./order"; | ||
|
||
@Entity({ name: "order_test" }) | ||
export class OrderTestEntity { | ||
|
||
@PrimaryColumn() | ||
id: number; | ||
|
||
@Column({ type: "enum", enum: Order, default: Order.FIRST }) | ||
order: Order; | ||
|
||
@Column({ type: "enum", enum: Order, default: [Order.FIRST], array: true }) | ||
orders: Order[]; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export enum Order { | ||
FIRST = "first", | ||
SECOND = "second", | ||
THIRD = "third" | ||
} | ||
|
||
export namespace Order { | ||
export function from(value: string): Order { | ||
switch (value) { | ||
case "_1": | ||
return Order.FIRST; | ||
case "_2": | ||
return Order.SECOND; | ||
case "_3": | ||
return Order.THIRD; | ||
default: | ||
return Order.FIRST; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import "reflect-metadata"; | ||
import { Connection } from "../../../src"; | ||
import { closeTestingConnections, createTestingConnections, reloadTestingDatabases } from "../../utils/test-utils"; | ||
import { Order } from "./entity/order"; | ||
import { OrderTestEntity } from "./entity/order-test.entity"; | ||
|
||
describe("github issues > #7651 Enum that contains functions is not accordingly translated to SQL", () => { | ||
|
||
describe("entity", () => { | ||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
enabledDrivers: ["postgres"] | ||
})); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should correctly save and retrieve enum fields when declaration merging technique is used and enum contains functions", () => Promise.all(connections.map(async connection => { | ||
// GIVEN | ||
const orderEntity = new OrderTestEntity(); | ||
orderEntity.id = 1; | ||
orderEntity.order = Order.from("_2"); // example of function call on enum to retrieve enum from string | ||
|
||
const orderEntityRepository = connection.getRepository(OrderTestEntity); | ||
await orderEntityRepository.save(orderEntity); | ||
|
||
// WHEN | ||
const loadedOrderEntity = await orderEntityRepository.findOne(1); | ||
|
||
// THEN | ||
loadedOrderEntity!.order.should.be.eq(Order.SECOND); | ||
}))); | ||
|
||
it("should correctly save and retrieve enum array", () => Promise.all(connections.map(async connection => { | ||
// GIVEN | ||
const orderEntity = new OrderTestEntity(); | ||
orderEntity.id = 1; | ||
orderEntity.orders = [Order.from("_2"), Order.THIRD]; | ||
|
||
const orderEntityRepository = connection.getRepository(OrderTestEntity); | ||
await orderEntityRepository.save(orderEntity); | ||
|
||
// WHEN | ||
const loadedOrderEntity = await orderEntityRepository.findOne(1); | ||
|
||
// THEN | ||
loadedOrderEntity!.orders.should.be.eql([Order.SECOND, Order.THIRD]); | ||
}))); | ||
}); | ||
|
||
describe("schema", () => { | ||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
schemaCreate: false, | ||
dropSchema: true, | ||
enabledDrivers: ["postgres"], | ||
migrations: [] | ||
})); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should contain SQL for enum type without function", () => Promise.all(connections.map(async connection => { | ||
const sqlInMemory = await connection.driver.createSchemaBuilder().log(); | ||
|
||
sqlInMemory!.upQueries.length.should.be.greaterThan(0); | ||
sqlInMemory.upQueries.forEach(query => { | ||
// there should be no function string in query when our ENUM TYPE is provided | ||
query.query.should.not.contain("function"); | ||
}); | ||
}))); | ||
}); | ||
|
||
}); |