Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: find descendants of a non-existing tree parent (#8557)
* fix: find descendants of a non-existing tree parent Closes: #8556 * Change default connection name(try another build) Co-authored-by: Astrit Shehu <a.shehu@linkplus-ks.com>
- Loading branch information
1 parent
1cfd7b9
commit cbb61eb
Showing
3 changed files
with
79 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,19 @@ | ||
import {Column, PrimaryGeneratedColumn, Tree, TreeParent, TreeChildren} from "../../../../src"; | ||
import {Entity} from "../../../../src/decorator/entity/Entity"; | ||
|
||
@Entity() | ||
@Tree("materialized-path") | ||
export class Category { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
name: string; | ||
|
||
@TreeChildren() | ||
children: Category[]; | ||
|
||
@TreeParent() | ||
parent: Category; | ||
} |
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,59 @@ | ||
import "reflect-metadata"; | ||
import { | ||
createTestingConnections, | ||
closeTestingConnections, | ||
reloadTestingDatabases, | ||
generateRandomText | ||
} from "../../utils/test-utils"; | ||
import {Connection} from "../../../src/connection/Connection"; | ||
import {expect} from "chai"; | ||
import {Category} from "./entity/category.entity"; | ||
import {TreeRepository} from "../../../src"; | ||
|
||
describe("github issues > #8556 TreeRepository.findDescendants/Tree should return empty if tree parent entity does not exist", () => { | ||
let connections: Connection[]; | ||
before( | ||
async () => | ||
(connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
dropSchema: true, | ||
schemaCreate: true, | ||
name: generateRandomText(10)// Use a different name to avoid a random failure in build pipeline | ||
})) | ||
); | ||
|
||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should load descendants when findDescendants is called for a tree entity", () => | ||
Promise.all( | ||
connections.map(async connection => { | ||
const repo: TreeRepository<Category> = connection.getTreeRepository(Category); | ||
const root: Category = await repo.save({ id: 1, name: "root" } as Category); | ||
await repo.save({ id: 2, name: "child", parent: root } as Category); | ||
const descendantsIncludingParent = await repo.findDescendants(root); | ||
expect(descendantsIncludingParent.length).to.be.equal(2); | ||
const descendantTree = await repo.findDescendantsTree(root); | ||
expect(descendantTree.children.length).to.be.equal(1); | ||
const countDescendantsIncludingParent = await repo.countDescendants(root) | ||
expect(countDescendantsIncludingParent).to.be.equal(2); | ||
}) | ||
) | ||
); | ||
|
||
it("should return empty when findDescendants is called for a non existing tree entity", () => | ||
Promise.all( | ||
connections.map(async connection => { | ||
const repo: TreeRepository<Category> = connection.getTreeRepository(Category); | ||
const root: Category = await repo.save({ id: 1, name: "root" } as Category); | ||
await repo.save({ id: 2, name: "child", parent: root } as Category); | ||
const descendantsOfNonExistingParent = await repo.findDescendants({id: -1} as Category); | ||
expect(descendantsOfNonExistingParent.length).to.be.equal(0); | ||
const descendantTree = await repo.findDescendantsTree({id: -1} as Category); | ||
expect(descendantTree.children.length).to.be.equal(0); | ||
const countDescendantsIncludingParent = await repo.countDescendants({id: -1} as Category) | ||
expect(countDescendantsIncludingParent).to.be.equal(0); | ||
}) | ||
) | ||
); | ||
}); |