Skip to content

Commit

Permalink
fix: update test with complete sql sentence & add a test without orde…
Browse files Browse the repository at this point in the history
…rBy argument
  • Loading branch information
dagmawig committed Jan 25, 2022
1 parent 05bc1d3 commit 15377c1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
22 changes: 20 additions & 2 deletions server/infra/database/BaseRepository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,33 @@ describe('BaseRepository', () => {
tracker.uninstall()
tracker.install()
tracker.on('query', (query) => {
expect(query.sql).toMatch(/select.*testTable.*name.*order.*by.*desc.*/)
expect(query.sql).toMatch('select \* from "testTable" where \("name" \= $1\) order by "id" desc'
)
query.response([{ id: 1 }])
})
const result = await baseRepository.getByFilter(
{
name: 'testName',
},
{
order: { column: 'id', direction: 'desc' }
orderBy: { column: 'id', direction: 'desc' }
}
)
expect(result).toHaveLength(1)
expect(result[0]).toHaveProperty('id', 1)
})

it('getByFiliter without order', async () => {
tracker.uninstall()
tracker.install()
tracker.on('query', (query) => {
expect(query.sql).toMatch('select \* from "testTable" where \("name" \= $1\)'
)
query.response([{ id: 1 }])
})
const result = await baseRepository.getByFilter(
{
name: 'testName',
}
)
expect(result).toHaveLength(1)
Expand Down
8 changes: 4 additions & 4 deletions server/infra/database/BaseRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class BaseRepository<T> {
*/
async getByFilter<T>(
filter: T,
options: { limit?: number, order?: { column: string, direction?: 'asc' | 'desc' } } | undefined = undefined,
options: { limit?: number, orderBy?: { column: string, direction?: 'asc' | 'desc' } } | undefined = undefined,
) {
const whereBuilder = function (object: any, builder: Knex.QueryBuilder) {
let result = builder
Expand Down Expand Up @@ -76,9 +76,9 @@ export default class BaseRepository<T> {
if (options && options.limit) {
promise = promise.limit(options && options.limit)
}
if (options && options.order) {
let direction = (options.order.direction !== undefined) ? options.order.direction : 'asc';
promise = promise.orderBy(options.order.column, direction)
if (options && options.orderBy) {
let direction = (options.orderBy.direction !== undefined) ? options.orderBy.direction : 'asc';
promise = promise.orderBy(options.orderBy.column, direction)
}
const result = await promise
return result
Expand Down

0 comments on commit 15377c1

Please sign in to comment.