Skip to content

Commit

Permalink
fix: find descendants of a non-existing tree parent (#8557)
Browse files Browse the repository at this point in the history
* 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
astritsh and astrit-shehu committed Jan 31, 2022
1 parent 1cfd7b9 commit cbb61eb
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/repository/TreeRepository.ts
Expand Up @@ -137,7 +137,7 @@ export class TreeRepository<Entity> extends Repository<Entity> {
if (this.manager.connection.driver instanceof AbstractSqliteDriver) {
return `${alias}.${this.metadata.materializedPathColumn!.propertyPath} LIKE ${subQuery.getQuery()} || '%'`;
} else {
return `${alias}.${this.metadata.materializedPathColumn!.propertyPath} LIKE CONCAT(${subQuery.getQuery()}, '%')`;
return `${alias}.${this.metadata.materializedPathColumn!.propertyPath} LIKE NULLIF(CONCAT(${subQuery.getQuery()}, '%'), '%')`;
}
});
}
Expand Down
19 changes: 19 additions & 0 deletions test/github-issues/8556/entity/category.entity.ts
@@ -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;
}
59 changes: 59 additions & 0 deletions test/github-issues/8556/issue-8556.ts
@@ -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);
})
)
);
});

0 comments on commit cbb61eb

Please sign in to comment.