-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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 building tree entities with embeded primary column (…
- Loading branch information
Showing
4 changed files
with
125 additions
and
2 deletions.
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,26 @@ | ||
import { | ||
Column, | ||
Entity, | ||
Tree, | ||
TreeChildren, | ||
TreeParent, | ||
} from "../../../../src"; | ||
import { Slug } from "./Slug"; | ||
|
||
@Entity() | ||
@Tree("materialized-path") | ||
export class Category { | ||
@Column((type) => Slug, { prefix: false }) | ||
id: Slug; | ||
|
||
@TreeChildren() | ||
children: Category[]; | ||
|
||
@TreeParent() | ||
parent: Category; | ||
|
||
constructor(slug: string, parent?: Category) { | ||
this.id = new Slug(slug); | ||
if (parent) this.parent = parent; | ||
} | ||
} |
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,10 @@ | ||
import { PrimaryColumn } from "../../../../src"; | ||
|
||
export class Slug { | ||
@PrimaryColumn() | ||
slug: string; | ||
|
||
constructor(slug: string) { | ||
this.slug = slug; | ||
} | ||
} |
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,87 @@ | ||
import "reflect-metadata"; | ||
import { | ||
createTestingConnections, | ||
closeTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../utils/test-utils"; | ||
import { Connection } from "../../../src/connection/Connection"; | ||
import { Category } from "./entity/Category"; | ||
import { Slug } from "./entity/Slug"; | ||
import { expect } from "chai"; | ||
|
||
describe("github issues > #7415 Tree entities with embedded primary columns are not built correctly", () => { | ||
let connections: Connection[]; | ||
before( | ||
async () => | ||
(connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
})) | ||
); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should build tree entities with embedded primary columns correctly", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
const manager = connection.manager; | ||
|
||
const a1 = new Category("1"); | ||
await manager.save(a1); | ||
|
||
const a2 = new Category("2"); | ||
await manager.save(a2); | ||
|
||
const a11 = new Category("1.1", a1); | ||
await manager.save(a11); | ||
|
||
const a12 = new Category("1.2", a1); | ||
await manager.save(a12); | ||
|
||
const a13 = new Category("1.3", a1); | ||
await manager.save(a13); | ||
|
||
const a121 = new Category("1.2.1", a12); | ||
await manager.save(a121); | ||
|
||
const a122 = new Category("1.2.2", a12); | ||
await manager.save(a122); | ||
|
||
const repository = manager.getTreeRepository(Category); | ||
|
||
const descendantsTree = await repository.findDescendantsTree( | ||
a1 | ||
); | ||
|
||
const expectedDescendantsTree = { | ||
id: new Slug("1"), | ||
children: [ | ||
{ id: new Slug("1.1"), children: [] }, | ||
{ | ||
id: new Slug("1.2"), | ||
children: [ | ||
{ id: new Slug("1.2.1"), children: [] }, | ||
{ id: new Slug("1.2.2"), children: [] }, | ||
], | ||
}, | ||
{ id: new Slug("1.3"), children: [] }, | ||
], | ||
}; | ||
|
||
expect(descendantsTree).to.be.eql(expectedDescendantsTree); | ||
|
||
const ancestorsTree = await repository.findAncestorsTree(a121); | ||
|
||
const expectedAncestorsTree = { | ||
id: new Slug("1.2.1"), | ||
parent: { | ||
id: new Slug("1.2"), | ||
parent: { id: new Slug("1") }, | ||
}, | ||
}; | ||
|
||
expect(ancestorsTree).to.be.eql(expectedAncestorsTree); | ||
}) | ||
)); | ||
}); |