@tomsd/mongodbclient 3.0.9 → 3.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomsd/mongodbclient",
3
- "version": "3.0.9",
3
+ "version": "3.0.10",
4
4
  "description": "It's a handy mongodb client for easy-use.",
5
5
  "main": "dist/cjs/mongodbclient.js",
6
6
  "module": "dist/esm/mongodbclient.js",
@@ -15,8 +15,8 @@
15
15
  "lint:test": "eslint test/**/*.ts",
16
16
  "prepare": "husky install",
17
17
  "serve:doc": "mdbook --serve --directory docs",
18
- "test": "exit 0",
19
- "test_with_dburi": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' mocha -r ts-node/register \"test/test.ts\" --timeout 30000"
18
+ "test": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' mocha -r ts-node/register \"test/mclient.test.ts\" --timeout 30000",
19
+ "test_with_dburi": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' mocha -r ts-node/register \"test/with_dburi.test.ts\" --timeout 30000"
20
20
  },
21
21
  "lint-staged": {
22
22
  "docs/**/*.html": [
@@ -48,6 +48,7 @@
48
48
  "@tomsd/md-book": "^0.16.0",
49
49
  "@types/mocha": "^9.1.1",
50
50
  "@types/node": "^14.14.5",
51
+ "@types/sinon": "^10.0.13",
51
52
  "@types/uuid": "^8.3.4",
52
53
  "@typescript-eslint/eslint-plugin": "^5.38.0",
53
54
  "dotenv": "^16.0.1",
@@ -61,6 +62,7 @@
61
62
  "lint-staged": "^13.0.3",
62
63
  "mocha": "^7.0.1",
63
64
  "prettier": "^2.7.1",
65
+ "sinon": "^14.0.0",
64
66
  "ts-node": "^10.8.1",
65
67
  "typescript": "^4.8.3"
66
68
  }
@@ -0,0 +1,239 @@
1
+ import { describe, it } from "mocha";
2
+ import { MClient } from "../src/mongodbclient";
3
+ import { strict as assert } from "assert";
4
+ import { mock } from "sinon";
5
+
6
+ const testConfig = {
7
+ uri: "mongodb+srv://...",
8
+ db: "db",
9
+ collection: "collection",
10
+ };
11
+ let mdbc: MClient;
12
+
13
+ describe("MClient", () => {
14
+ before(() => {
15
+ mdbc = new MClient(testConfig.uri, testConfig.db, testConfig.collection);
16
+ });
17
+
18
+ it("property uri", () => {
19
+ assert.equal(mdbc.uri, testConfig.uri);
20
+ });
21
+
22
+ it("properti db", () => {
23
+ assert.equal(mdbc.db, testConfig.db);
24
+ });
25
+
26
+ it("property collection", () => {
27
+ assert.equal(mdbc.collection, testConfig.collection);
28
+ });
29
+
30
+ it("upsert()", async () => {
31
+ const connection = {
32
+ collection: {
33
+ updateOne: async (obj: any) =>
34
+ await Promise.resolve({
35
+ upsertedCount: 1,
36
+ modifiedCount: 0,
37
+ }),
38
+ },
39
+ client: {
40
+ close: () => undefined,
41
+ },
42
+ };
43
+ const mocked = mock(mdbc);
44
+ mocked
45
+ .expects("getConnected")
46
+ .once()
47
+ .withArgs()
48
+ .returns(Promise.resolve(connection));
49
+
50
+ assert.equal(
51
+ await mdbc
52
+ .upsert({ name: "test" })
53
+ .then(({ upsertedCount }) => upsertedCount),
54
+ 1
55
+ );
56
+
57
+ mocked.verify();
58
+ mocked.restore();
59
+ });
60
+
61
+ it("read()", async () => {
62
+ const returningDocuments = [{ name: "test" }];
63
+ const connection = {
64
+ collection: {
65
+ find: (condition: any) => ({
66
+ toArray: async () => await Promise.resolve(returningDocuments),
67
+ }),
68
+ },
69
+ client: {
70
+ close: () => undefined,
71
+ },
72
+ };
73
+
74
+ const mocked = mock(mdbc);
75
+ mocked
76
+ .expects("getConnected")
77
+ .once()
78
+ .withArgs()
79
+ .returns(Promise.resolve(connection));
80
+
81
+ assert.equal(
82
+ JSON.stringify(await mdbc.read({ name: "test" })),
83
+ JSON.stringify(returningDocuments)
84
+ );
85
+
86
+ mocked.verify();
87
+ mocked.restore();
88
+ });
89
+
90
+ it("distinct()", async () => {
91
+ const returningValues = ["test1", "test2"];
92
+ const connection = {
93
+ collection: {
94
+ distinct: async (key: string) => await Promise.resolve(returningValues),
95
+ },
96
+ client: {
97
+ close: () => undefined,
98
+ },
99
+ };
100
+ const mocked = mock(mdbc);
101
+ mocked
102
+ .expects("getConnected")
103
+ .once()
104
+ .withArgs()
105
+ .returns(Promise.resolve(connection));
106
+
107
+ assert.equal(
108
+ JSON.stringify(await mdbc.distinct("name")),
109
+ JSON.stringify(returningValues)
110
+ );
111
+
112
+ mocked.verify();
113
+ mocked.restore();
114
+ });
115
+
116
+ it("remove()", async () => {
117
+ const returningValue = { deletedCount: 1 };
118
+ const connection = {
119
+ collection: {
120
+ deleteMany: async (condition: any) =>
121
+ await Promise.resolve(returningValue),
122
+ },
123
+ client: {
124
+ close: () => undefined,
125
+ },
126
+ };
127
+ const mocked = mock(mdbc);
128
+ mocked
129
+ .expects("getConnected")
130
+ .once()
131
+ .withArgs()
132
+ .returns(Promise.resolve(connection));
133
+
134
+ assert.equal(
135
+ JSON.stringify(await mdbc.remove({ name: "test" })),
136
+ JSON.stringify(returningValue)
137
+ );
138
+
139
+ mocked.verify();
140
+ mocked.restore();
141
+ });
142
+
143
+ it("stats()", async () => {
144
+ const returningValue = { storageSize: 100 };
145
+ const connection = {
146
+ collection: {
147
+ stats: async () => await Promise.resolve(returningValue),
148
+ },
149
+ client: {
150
+ close: () => undefined,
151
+ },
152
+ };
153
+ const mocked = mock(mdbc);
154
+ mocked.expects("getConnected").once().withArgs().returns(connection);
155
+
156
+ assert.equal(
157
+ await mdbc.stats().then(({ storageSize }) => storageSize),
158
+ 100
159
+ );
160
+
161
+ mocked.verify();
162
+ mocked.restore();
163
+ });
164
+
165
+ it("count()", async () => {
166
+ const resultValue = 100;
167
+ const connection = {
168
+ collection: {
169
+ countDocuments: async (condition: any) =>
170
+ await Promise.resolve(resultValue),
171
+ },
172
+ client: {
173
+ close: () => undefined,
174
+ },
175
+ };
176
+ const mocked = mock(mdbc);
177
+ mocked.expects("getConnected").once().withArgs().returns(connection);
178
+
179
+ assert.equal(await mdbc.count({ name: "test" }), resultValue);
180
+
181
+ mocked.verify();
182
+ mocked.restore();
183
+ });
184
+
185
+ it("insertMany()", async () => {
186
+ const insertingItems = [
187
+ { name: "test1" },
188
+ { name: "test2" },
189
+ { name: "test3" },
190
+ ];
191
+ const connection = {
192
+ collection: {
193
+ insertMany: async (savingItems: any[]) =>
194
+ await Promise.resolve({
195
+ insertedCount: savingItems.length,
196
+ }),
197
+ },
198
+ client: {
199
+ close: () => undefined,
200
+ },
201
+ };
202
+
203
+ const mocked = mock(mdbc);
204
+ mocked.expects("getConnected").once().withArgs().returns(connection);
205
+
206
+ assert.equal(
207
+ await mdbc
208
+ .insertMany(insertingItems)
209
+ .then(({ insertedCount }) => insertedCount),
210
+ insertingItems.length
211
+ );
212
+
213
+ mocked.verify();
214
+ mocked.restore();
215
+ });
216
+
217
+ it("dbStats()", async () => {
218
+ const resultValue = { storageSize: 100 };
219
+ const connection = {
220
+ db: {
221
+ stats: async () => await Promise.resolve(resultValue),
222
+ },
223
+ client: {
224
+ close: () => undefined,
225
+ },
226
+ };
227
+
228
+ const mocked = mock(mdbc);
229
+ mocked.expects("getConnected").once().withArgs().returns(connection);
230
+
231
+ assert.equal(
232
+ await mdbc.dbStats().then(({ storageSize }) => storageSize),
233
+ 100
234
+ );
235
+
236
+ mocked.verify();
237
+ mocked.restore();
238
+ });
239
+ });
File without changes