-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add acceptance-level tests for
repository.execute()
Signed-off-by: Miroslav Bajtoš <[email protected]>
- Loading branch information
Showing
9 changed files
with
158 additions
and
0 deletions.
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
58 changes: 58 additions & 0 deletions
58
acceptance/repository-mongodb/src/__tests__/mongodb-repository-execute.acceptance.ts
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,58 @@ | ||
// Copyright IBM Corp. 2019. All Rights Reserved. | ||
// Node module: @loopback/test-repository-mongodb | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import { | ||
DefaultTransactionalRepository, | ||
Entity, | ||
juggler, | ||
model, | ||
property, | ||
} from '@loopback/repository'; | ||
import {expect, toJSON} from '@loopback/testlab'; | ||
import {MONGODB_CONFIG} from './mongodb.datasource'; | ||
|
||
describe('MongoDB repository.execute()', () => { | ||
it('executes a custom MongoDB query', async () => { | ||
const db = new juggler.DataSource(MONGODB_CONFIG); | ||
|
||
@model() | ||
class Product extends Entity { | ||
@property({id: true, generated: true}) | ||
id: string; | ||
|
||
@property({required: true}) | ||
name: string; | ||
|
||
@property() | ||
owner: string; | ||
} | ||
|
||
const repo = new DefaultTransactionalRepository<Product, string, {}>( | ||
Product, | ||
db, | ||
); | ||
await db.automigrate(Product.modelName); | ||
await repo.create({name: 'Pen', owner: 'bajtos'}); | ||
await repo.create({name: 'Car', owner: 'admin'}); | ||
await repo.create({name: 'Chair', owner: 'admin'}); | ||
|
||
// MongoDB's aggregate() command returns an AggregationCursor instance | ||
const cursor = await repo.execute('Product', 'aggregate', [ | ||
{ | ||
$group: { | ||
_id: '$owner', | ||
count: {$sum: 1}, | ||
}, | ||
}, | ||
]); | ||
// Fetch all items from the cursor at once | ||
const result = await cursor.toArray(); | ||
|
||
expect(toJSON(result)).to.deepEqual([ | ||
{_id: 'bajtos', count: 1}, | ||
{_id: 'admin', count: 2}, | ||
]); | ||
}); | ||
}); |
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
43 changes: 43 additions & 0 deletions
43
acceptance/repository-mysql/src/__tests__/mysql-repository-execute.acceptance.ts
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,43 @@ | ||
// Copyright IBM Corp. 2019. All Rights Reserved. | ||
// Node module: @loopback/test-repository-mysql | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import { | ||
DefaultTransactionalRepository, | ||
Entity, | ||
juggler, | ||
model, | ||
property, | ||
} from '@loopback/repository'; | ||
import {expect, toJSON} from '@loopback/testlab'; | ||
import {MYSQL_CONFIG} from './mysql.datasource'; | ||
|
||
describe('MySQL repository.execute()', () => { | ||
it('executes a parameterized native SQL query', async () => { | ||
const db = new juggler.DataSource(MYSQL_CONFIG); | ||
|
||
@model() | ||
class Product extends Entity { | ||
@property({id: true, generated: true}) | ||
id: number; | ||
|
||
@property({required: true}) | ||
name: string; | ||
} | ||
|
||
const repo = new DefaultTransactionalRepository<Product, number, {}>( | ||
Product, | ||
db, | ||
); | ||
await db.automigrate(Product.modelName); | ||
const pen = await repo.create({name: 'Pen'}); | ||
await repo.create({name: 'Car'}); | ||
|
||
const result = await repo.execute('SELECT * FROM Product WHERE name = ?', [ | ||
'Pen', | ||
]); | ||
|
||
expect(toJSON(result)).to.deepEqual([toJSON(pen)]); | ||
}); | ||
}); |
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
45 changes: 45 additions & 0 deletions
45
acceptance/repository-postgresql/src/__tests__/postgresql-repository-execute.acceptance.ts
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,45 @@ | ||
// Copyright IBM Corp. 2019. All Rights Reserved. | ||
// Node module: @loopback/test-repository-postgresql | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import { | ||
DefaultTransactionalRepository, | ||
Entity, | ||
juggler, | ||
model, | ||
property, | ||
} from '@loopback/repository'; | ||
import {expect, toJSON} from '@loopback/testlab'; | ||
import {POSTGRESQL_CONFIG} from './postgresql.datasource'; | ||
|
||
describe('PostgreSQL repository.execute()', () => { | ||
// FIXME(bajtos) This test passes when executed in isolation, but fails | ||
// on connection timeout when executed after repository-test suite. | ||
it.skip('executes a parameterized native SQL query', async () => { | ||
const db = new juggler.DataSource(POSTGRESQL_CONFIG); | ||
|
||
@model() | ||
class Product extends Entity { | ||
@property({id: true, generated: true}) | ||
id: number; | ||
|
||
@property({required: true}) | ||
name: string; | ||
} | ||
|
||
const repo = new DefaultTransactionalRepository<Product, number, {}>( | ||
Product, | ||
db, | ||
); | ||
await db.automigrate(Product.modelName); | ||
const pen = await repo.create({name: 'Pen'}); | ||
await repo.create({name: 'Car'}); | ||
|
||
const result = await repo.execute('SELECT * FROM Product WHERE name = $1', [ | ||
'Pen', | ||
]); | ||
|
||
expect(toJSON(result)).to.deepEqual([toJSON(pen)]); | ||
}); | ||
}); |
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