Skip to content

Commit

Permalink
test: add acceptance-level tests for repository.execute()
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
bajtos committed Aug 4, 2020
1 parent 0d9ed9e commit e461787
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions acceptance/repository-mongodb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@loopback/eslint-config": "^8.0.4",
"@loopback/repository": "^2.10.0",
"@loopback/repository-tests": "^0.12.10",
"@loopback/testlab": "^3.2.1",
"@types/node": "^10.17.28",
"loopback-connector-mongodb": "^5.3.0",
"tslib": "^2.0.0"
Expand Down
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},
]);
});
});
3 changes: 3 additions & 0 deletions acceptance/repository-mongodb/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
},
{
"path": "../../packages/repository/tsconfig.json"
},
{
"path": "../../packages/testlab/tsconfig.json"
}
]
}
1 change: 1 addition & 0 deletions acceptance/repository-mysql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@loopback/eslint-config": "^8.0.4",
"@loopback/repository": "^2.10.0",
"@loopback/repository-tests": "^0.12.10",
"@loopback/testlab": "^3.2.1",
"@types/node": "^10.17.28",
"loopback-connector-mysql": "^5.4.4",
"tslib": "^2.0.0"
Expand Down
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)]);
});
});
3 changes: 3 additions & 0 deletions acceptance/repository-mysql/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
},
{
"path": "../../packages/repository/tsconfig.json"
},
{
"path": "../../packages/testlab/tsconfig.json"
}
]
}
1 change: 1 addition & 0 deletions acceptance/repository-postgresql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@loopback/eslint-config": "^8.0.4",
"@loopback/repository": "^2.10.0",
"@loopback/repository-tests": "^0.12.10",
"@loopback/testlab": "^3.2.1",
"@types/node": "^10.17.28",
"loopback-connector-postgresql": "^5.0.2",
"tslib": "^2.0.0"
Expand Down
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)]);
});
});
3 changes: 3 additions & 0 deletions acceptance/repository-postgresql/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
},
{
"path": "../../packages/repository/tsconfig.json"
},
{
"path": "../../packages/testlab/tsconfig.json"
}
]
}

0 comments on commit e461787

Please sign in to comment.