forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: correctly handle mongo replica set driver option (typeorm#7908)
* test: migrates should syntax to expect So all testcase use a uniform expectation library * fix: adds replicaSet and tls option to mongo connection url fixes 7130 * test: add a unit test for the connection string building
- Loading branch information
1 parent
fff19f2
commit 423aef5
Showing
4 changed files
with
112 additions
and
21 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
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,54 @@ | ||
import { expect } from "chai"; | ||
import sinon from "sinon"; | ||
import { Connection } from "../../../src"; | ||
import { DriverUtils } from "../../../src/driver/DriverUtils"; | ||
import { MongoDriver } from "../../../src/driver/mongodb/MongoDriver"; | ||
|
||
describe("MongoDriver", () => { | ||
async function getConnectionUrlFromFakedMongoClient(url: string): Promise<string> { | ||
const options = DriverUtils.buildMongoDBDriverOptions({ url}); | ||
|
||
// Setup a MongoDriver with a mocked connect method, so we can get the connection | ||
// url from the actual call afterwards. | ||
const driver = new MongoDriver({ | ||
options | ||
} as Connection); | ||
const connect = sinon.fake(); | ||
driver.mongodb = { | ||
...driver.mongodb, | ||
MongoClient: { | ||
connect | ||
} | ||
}; | ||
|
||
const connectPromise = driver.connect(); | ||
|
||
// Take the promise parameter that we receive in the callback, call it, so the underlying promise gets resolved. | ||
const firstMethodCall = connect.args[0]; | ||
const callback = firstMethodCall[2]; | ||
callback(undefined, {}); | ||
await connectPromise; | ||
|
||
return firstMethodCall[0]; | ||
} | ||
|
||
describe("connection string", () => { | ||
|
||
it("should create a connection string for replica sets", async () => { | ||
const url = "mongodb://username:password@someHost1:27017,someHost2:27018/myDatabase?replicaSet=abc&tls=true"; | ||
|
||
const connectionUrl = await getConnectionUrlFromFakedMongoClient(url); | ||
|
||
expect(connectionUrl).to.eql(url); | ||
}); | ||
|
||
it("should create a connection string for non replica sets", async() => { | ||
const url = "mongodb://username:password@someHost1:27017/myDatabase?tls=true"; | ||
|
||
const connectionUrl = await getConnectionUrlFromFakedMongoClient(url); | ||
|
||
expect(connectionUrl).to.eql(url); | ||
}); | ||
}); | ||
|
||
}); |
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,38 @@ | ||
import { expect } from "chai"; | ||
import { DriverUtils } from "../../../src/driver/DriverUtils"; | ||
|
||
describe("DriverUtils", () => { | ||
describe("parse mongo url", () => { | ||
it("should return a mongo url with a replica set", () => { | ||
const url = "mongodb://username:password@someHost1:27017,someHost2:27018/myDatabase?replicaSet=abc&tls=true"; | ||
const result = DriverUtils.buildMongoDBDriverOptions({ url}); | ||
|
||
expect(result).to.eql({ | ||
database: "myDatabase", | ||
hostReplicaSet: "someHost1:27017,someHost2:27018", | ||
password: "password", | ||
replicaSet: "abc", | ||
tls: "true", | ||
type: "mongodb", | ||
url, | ||
username: "username" | ||
}); | ||
}); | ||
|
||
it("should return a mongo url without a replica set", () => { | ||
const url = "mongodb://username:password@someHost1:27017/myDatabase?tls=true"; | ||
const result = DriverUtils.buildMongoDBDriverOptions({ url}); | ||
|
||
expect(result).to.eql({ | ||
database: "myDatabase", | ||
host: "someHost1", | ||
port: 27017, | ||
password: "password", | ||
tls: "true", | ||
type: "mongodb", | ||
url, | ||
username: "username" | ||
}); | ||
}); | ||
}); | ||
}); |
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