-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: add acceptance-level tests for repository.execute()
#6046
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
63 changes: 63 additions & 0 deletions
63
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,63 @@ | ||
// 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}, | ||
}, | ||
}, | ||
{ | ||
$sort: { | ||
_id: 1, | ||
}, | ||
}, | ||
]); | ||
// Fetch all items from the cursor at once | ||
const result = await cursor.toArray(); | ||
|
||
expect(toJSON(result)).to.deepEqual([ | ||
{_id: 'admin', count: 2}, | ||
{_id: 'bajtos', count: 1}, | ||
]); | ||
}); | ||
}); |
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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
source ./node_modules/loopback-connector-postgresql/setup.sh | ||
export POSTGRESQL_DATABASE=repository_tests |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm interesting. It is indicative of some sort of bug somewhere (maybe a shared global state somehere?). I think we should resolve this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I agree this needs to be fixed. See #6046 (comment)
My primary motivation in this pull request is to ensure MongoDB commands can be executed. I don't want to keep this pull request open for too long, to avoid merge conflicts.
I am going to investigate the failing test as part of #3342, I added a sub-task to the TODO list.