Skip to content

Commit

Permalink
test(knex): add a few more tests to verify aliases overriding
Browse files Browse the repository at this point in the history
closes #3374
  • Loading branch information
derevnjuk committed Aug 9, 2022
1 parent 3a00280 commit 4e837db
Showing 1 changed file with 49 additions and 7 deletions.
56 changes: 49 additions & 7 deletions tests/QueryBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2746,35 +2746,77 @@ describe('QueryBuilder', () => {
});

test('from an entity', async () => {
const qb = orm.em.createQueryBuilder(Publisher2).from(Author2);
const qb = orm.em.createQueryBuilder(Publisher2);
qb.from(Author2);
expect(qb.getQuery()).toEqual('select `e0`.* from `author2` as `e0`');
});

test('update from', async () => {
const qb = orm.em.createQueryBuilder(Publisher2);
qb.update({ name: 'test' }).from(Author2);
expect(qb.getQuery()).toEqual('update `author2` set `name` = ?');
});

test('delete from', async () => {
const qb = orm.em.createQueryBuilder(Publisher2);
qb.delete().where({ id: 1 }).from(Author2);
expect(qb.getQuery()).toEqual('delete from `author2` where `id` = ?');
});

test('override previously used aliases in select statement', async () => {
const qb = orm.em.createQueryBuilder(Publisher2, 'p').select('p.*');
qb.from(Author2, 'a');
expect(qb.getQuery()).toEqual('select `a`.* from `author2` as `a`');
});

test('override previously used aliases in where clause', async () => {
const qb = orm.em.createQueryBuilder(Publisher2, 'p').where({ 'p.id': 1 });
qb.from(Author2, 'a');
expect(qb.getQuery()).toEqual('select `a`.* from `author2` as `a` where `a`.`id` = ?');
});

test('override previously used aliases in order by clause', async () => {
const qb = orm.em.createQueryBuilder(Publisher2, 'p').orderBy({ 'p.id': 'desc' });
qb.from(Author2, 'a');
expect(qb.getQuery()).toEqual('select `a`.* from `author2` as `a` order by `a`.`id` desc');
});

test('override previously used aliases in join statement', async () => {
const qb = orm.em.createQueryBuilder(Publisher2, 'p').select(['p.*', 'b.*']).leftJoin('books', 'b');
qb.from(Author2, 'a');
expect(qb.getQuery()).toEqual('select `a`.*, `b`.* from `author2` as `a` left join `book2` as `b` on `a`.`id` = `b`.`publisher_id`');
});

test('from an entity with alias', async () => {
const qb = orm.em.createQueryBuilder(Publisher2, 'p').from(Author2, 'a');
const qb = orm.em.createQueryBuilder(Publisher2, 'p');
qb.from(Author2, 'a');
expect(qb.getQuery()).toEqual('select `a`.* from `author2` as `a`');
});

test('from a query builder with where and order by clauses', async () => {
const qb1 = orm.em.createQueryBuilder(Author2).where({ createdAt: { $lte: new Date() } }).orderBy({ createdAt: 'DESC' });
const qb2 = orm.em.createQueryBuilder(Author2).from(qb1.clone()).orderBy({ createdAt: 'ASC' });
const qb2 = orm.em.createQueryBuilder(Author2);
qb2.from(qb1.clone()).orderBy({ createdAt: 'ASC' });
expect(qb2.getQuery()).toEqual('select `e1`.* from (select `e0`.* from `author2` as `e0` where `e0`.`created_at` <= ? order by `e0`.`created_at` desc) as `e1` order by `e1`.`created_at` asc');
});

test('from a query builder with joins', async () => {
const qb1 = orm.em.createQueryBuilder(Author2).where({ createdAt: { $lte: new Date() } }).leftJoin('books2', 'b').orderBy({ 'b.createdAt': 'DESC' });
const qb2 = orm.em.createQueryBuilder(Author2).from(qb1.clone()).orderBy({ 'b.createdAt': 'ASC' });
const qb2 = orm.em.createQueryBuilder(Author2);
qb2.from(qb1.clone()).orderBy({ 'b.createdAt': 'ASC' });
expect(qb2.getQuery()).toEqual('select `e1`.* from (select `e0`.* from `author2` as `e0` left join `book2` as `b` on `e0`.`id` = `b`.`author_id` where `e0`.`created_at` <= ? order by `b`.`created_at` desc) as `e1` order by `b`.`created_at` asc');
});

test('from a string', async () => {
const qb2 = orm.em.createQueryBuilder(Publisher2).from('Author2');
expect(qb2.getQuery()).toEqual('select `e0`.* from `author2` as `e0`');
const qb = orm.em.createQueryBuilder(Publisher2);
qb.from('Author2');
expect(qb.getQuery()).toEqual('select `e0`.* from `author2` as `e0`');
});

test('from a query builder', async () => {
const qb1 = orm.em.createQueryBuilder(Author2);
const qb2 = orm.em.createQueryBuilder(Publisher2).from(qb1);
const qb2 = orm.em.createQueryBuilder(Publisher2);
qb2.from(qb1);
expect(qb2.getQuery()).toEqual('select `e1`.* from (select `e0`.* from `author2` as `e0`) as `e1`');
});

Expand Down

0 comments on commit 4e837db

Please sign in to comment.