Skip to content

Commit

Permalink
added tests for #813
Browse files Browse the repository at this point in the history
  • Loading branch information
Umed Khudoiberdiev committed Sep 6, 2017
1 parent 815cb51 commit 440f0b0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/query-builder/SelectQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,8 @@ export class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> {

const selectString = Object.keys(orderBys)
.map(columnName => {
if (columnName.indexOf(".") === -1) return;

const [aliasName, propertyPath] = columnName.split(".");
const alias = this.expressionMap.findAliasByName(aliasName);
const column = alias.metadata.findColumnWithPropertyName(propertyPath);
Expand All @@ -1593,6 +1595,8 @@ export class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> {

const orderByObject: OrderByCondition = {};
Object.keys(orderBys).forEach(columnName => {
if (columnName.indexOf(".") === -1) return;

const [aliasName, propertyPath] = columnName.split(".");
const alias = this.expressionMap.findAliasByName(aliasName);
const column = alias.metadata.findColumnWithPropertyName(propertyPath);
Expand Down
14 changes: 14 additions & 0 deletions test/github-issues/813/entity/Post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {Entity} from "../../../../src/decorator/entity/Entity";
import {PrimaryGeneratedColumn} from "../../../../src/decorator/columns/PrimaryGeneratedColumn";
import {Column} from "../../../../src/decorator/columns/Column";

@Entity()
export class Post {

@PrimaryGeneratedColumn()
id: number;

@Column()
title: string;

}
53 changes: 53 additions & 0 deletions test/github-issues/813/issue-813.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import "reflect-metadata";
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";
import {Connection} from "../../../src/connection/Connection";
import {Post} from "./entity/Post";

describe("github issues > #813 order by must support functions", () => {

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 work perfectly", () => Promise.all(connections.map(async connection => {

const post = new Post();
post.title = "About order by";
await connection.manager.save(post);

const posts = await connection.createQueryBuilder(Post, "post")
.orderBy("RAND()")
.getMany();

posts.should.be.eql([{
id: 1,
title: "About order by"
}]);

})));

it("should work perfectly with pagination as well", () => Promise.all(connections.map(async connection => {

const post = new Post();
post.title = "About order by";
await connection.manager.save(post);

const posts = await connection.createQueryBuilder(Post, "post")
.orderBy("RAND()")
.skip(0)
.take(1)
.getMany();

posts.should.be.eql([{
id: 1,
title: "About order by"
}]);

})));

});

0 comments on commit 440f0b0

Please sign in to comment.