2017-07-20 02:06:12 +07:00
|
|
|
import Assert = require("assert");
|
|
|
|
import Sinon = require("sinon");
|
|
|
|
import MongoDB = require("mongodb");
|
|
|
|
import BluebirdPromise = require("bluebird");
|
2017-10-07 05:09:42 +07:00
|
|
|
import { IMongoClient } from "../../../src/lib/connectors/mongo/IMongoClient";
|
|
|
|
import { MongoConnector } from "../../../src/lib/connectors/mongo/MongoConnector";
|
2017-07-20 02:06:12 +07:00
|
|
|
|
|
|
|
describe("MongoConnector", function () {
|
|
|
|
let mongoClientConnectStub: Sinon.SinonStub;
|
2018-03-29 05:04:59 +07:00
|
|
|
|
2017-07-20 02:06:12 +07:00
|
|
|
describe("create", function () {
|
|
|
|
before(function () {
|
|
|
|
mongoClientConnectStub = Sinon.stub(MongoDB.MongoClient, "connect");
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function() {
|
|
|
|
mongoClientConnectStub.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should create a connector", function () {
|
2018-03-29 05:04:59 +07:00
|
|
|
const client = { db: Sinon.mock() };
|
|
|
|
mongoClientConnectStub.yields(undefined, client);
|
2017-07-20 02:06:12 +07:00
|
|
|
|
|
|
|
const url = "mongodb://test.url";
|
|
|
|
const connector = new MongoConnector(url);
|
2018-03-29 05:04:59 +07:00
|
|
|
return connector.connect("database")
|
2017-07-20 02:06:12 +07:00
|
|
|
.then(function (client: IMongoClient) {
|
|
|
|
Assert(client);
|
|
|
|
Assert(mongoClientConnectStub.calledWith(url));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fail creating a connector", function () {
|
|
|
|
mongoClientConnectStub.yields(new Error("Error while creating mongo client"));
|
|
|
|
|
|
|
|
const url = "mongodb://test.url";
|
|
|
|
const connector = new MongoConnector(url);
|
2018-03-29 05:04:59 +07:00
|
|
|
return connector.connect("database")
|
2017-07-20 02:06:12 +07:00
|
|
|
.then(function () { return BluebirdPromise.reject(new Error("It should not be here")); })
|
|
|
|
.error(function (client: IMongoClient) {
|
|
|
|
Assert(client);
|
|
|
|
Assert(mongoClientConnectStub.calledWith(url));
|
|
|
|
return BluebirdPromise.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|