Skip to content

Commit

Permalink
fix: url options (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
brickyang authored May 10, 2018
1 parent 38771cb commit ca57361
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class MongoDB extends EventEmitter {

if (options) {
let uriOp = '?';
Object.keys(options).forEach(key => (uriOp += `${key}=${options[key]}`));
url += uriOp;
Object.keys(options).forEach(key => (uriOp += `${key}=${options[key]}&`));
url += uriOp.slice(0, -1);
}

return url;
Expand Down
12 changes: 8 additions & 4 deletions test/fixtures/apps/mongo-clients-test/config/config.unittest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
exports.mongo = {
clients: {
foo: {
host: 'localhost',
port: 27017,
name: 'foo',
},
bar: {
host: 'localhost',
port: 27017,
name: 'bar',
},
},
default: {
host: 'localhost',
port: 27017,
options: {
ssl: false,
connectTimeoutMS: 30000,
},
},
};
24 changes: 13 additions & 11 deletions test/multi-clients.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,31 @@ describe('test/mongo-clients.test.js', () => {
await app.ready();
});

afterEach(async () => {
beforeEach(async () => {
await app.mongo.get('foo').deleteMany(NAME, { filter: {} });
await app.mongo.get('bar').deleteMany(NAME, { filter: {} });
mock.restore();
});

after(async () => {
await app.mongo.get('foo').deleteMany(NAME, { filter: {} });
await app.mongo.get('bar').deleteMany(NAME, { filter: {} });
app.close();
});
afterEach(mock.restore);
after(() => app.close());

describe('app.mongo.get()', () => {
it('should get mongo configs foo info', async () => {
const client = await app.mongo.get('foo');
assert(client.url === 'mongodb://localhost:27017/foo');
assert(client.db.databaseName === 'foo');
assert.equal(
client.url,
'mongodb://localhost:27017/foo?ssl=false&connectTimeoutMS=30000'
);
assert.equal(client.db.databaseName, 'foo');
});

it('should get mongo configs bar info', async () => {
const client = await app.mongo.get('bar');
assert(client.url === 'mongodb://localhost:27017/bar');
assert(client.db.databaseName === 'bar');
assert.equal(
client.url,
'mongodb://localhost:27017/bar?ssl=false&connectTimeoutMS=30000'
);
assert.equal(client.db.databaseName, 'bar');
});
});
});

0 comments on commit ca57361

Please sign in to comment.