-
Notifications
You must be signed in to change notification settings - Fork 3
/
LoggingCollection.spec.ts
69 lines (59 loc) · 3.26 KB
/
LoggingCollection.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { ICollection } from "../../Services/ICollection";
import { mock, anything, instance, when, capture } from "ts-mockito";
import { InsertOneWriteOpResult } from "mongodb";
import { IAppInsightsService } from "../../Services/app-insights/app-insights-service";
import { LoggingCollection } from "../../Services/LoggingCollection";
import { expect } from "chai";
describe("LoggingCollection", async function (): Promise<void> {
it("Tracks dependencies for succesful database calls", async function(): Promise<void> {
const mockCollection = mock<ICollection>();
const expectedResult = createOneInsertResult();
const expectedDoc = {key:"value"};
when(mockCollection.insertOne(expectedDoc, anything())).thenResolve(expectedResult);
const mockAppInsightsService = mock<IAppInsightsService>();
const expectedCollectionName = "collectionName";
const expectedDbName = "dbName";
const appInsightsService = instance(mockAppInsightsService);
const collection = new LoggingCollection(instance(mockCollection), appInsightsService, expectedCollectionName, expectedDbName);
const result = await collection.insertOne(expectedDoc, {});
expect(result).is.equal(expectedResult);
const [actualTelemetry] = capture(mockAppInsightsService.trackDependency).first();
expect(actualTelemetry.data).is.equal("{\"insertOne\":{\"options\":{}}}");
expect(actualTelemetry.dependencyTypeName).is.equal("mongodb");
expect(actualTelemetry.resultCode).is.equal(0);
expect(actualTelemetry.success).is.equal(true);
expect(actualTelemetry.name).is.equal(expectedDbName);
expect(actualTelemetry.target).is.equal(expectedCollectionName);
});
it("Tracks dependencies for failed database calls", async function(): Promise<void> {
const mockCollection = mock<ICollection>();
const expectedDoc = {key:"value"};
const expectedError = new Error("expectedError");
const expectedErrorString = JSON.stringify(expectedError, Object.getOwnPropertyNames(expectedError));
when(mockCollection.insertOne(expectedDoc, anything()))
.thenThrow(expectedError);
const mockAppInsightsService = mock<IAppInsightsService>();
const expectedCollectionName = "collectionName";
const expectedDbName = "dbName";
const appInsightsService = instance(mockAppInsightsService);
const collection = new LoggingCollection(instance(mockCollection), appInsightsService, expectedCollectionName, expectedDbName);
await expect(collection.insertOne(expectedDoc, {})).to.be.rejectedWith(expectedError);
const [actualTelemetry] = capture(mockAppInsightsService.trackDependency).first();
expect(actualTelemetry.data).is.equal("{\"insertOne\":{\"options\":{}}}");
expect(actualTelemetry.dependencyTypeName).is.equal("mongodb");
expect(actualTelemetry.resultCode).is.equal(expectedErrorString);
expect(actualTelemetry.success).is.equal(false);
expect(actualTelemetry.name).is.equal(expectedDbName);
expect(actualTelemetry.target).is.equal(expectedCollectionName);
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function createOneInsertResult(): InsertOneWriteOpResult<any> {
return {
insertedCount: 1,
ops: [],
insertedId: {},
connection: {},
result: { ok: 1, n: 1 }
};
}
});