add tests

This commit is contained in:
zkldi
2021-06-25 16:56:44 +01:00
parent 5cb4d7db54
commit 9fb260032d
2 changed files with 36 additions and 3 deletions
+33
View File
@@ -0,0 +1,33 @@
import t from "tap";
import { CloseAllConnections } from "../../test-utils/close-connections";
import ResetDBState from "../../test-utils/resets";
import db from "./db";
t.beforeEach(ResetDBState);
t.test("ID field autoprojection", async (t) => {
const res = await db.scores.findOne();
t.equal(res!._id, undefined);
// @ts-expect-error todo: #205 type properly
const res2 = await db.scores.findOne({}, { projectID: true });
t.not(res2!._id, undefined);
const res3 = await db.scores.findOne({}, { projection: { scoreID: 1 } });
t.equal(res3!._id, undefined);
// @ts-expect-error todo: #205 type properly
const res4 = await db.scores.findOne({}, { projection: { scoreID: 1 }, projectID: true });
t.not(res4!._id, undefined);
const res5 = await db.scores.findOne({}, { projection: { scoreID: 0 } });
t.equal(res5!._id, undefined);
// @ts-expect-error todo: #205 type properly
const res6 = await db.scores.findOne({}, { projection: { scoreID: 0 }, projectID: true });
t.not(res6!._id, undefined);
t.end();
});
t.teardown(CloseAllConnections);
+3 -3
View File
@@ -72,10 +72,10 @@ const RemoveIDMiddleware: TMiddleware =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(args: any, method) => {
if ((method === "find" || method === "findOne") && !args.options.projectID) {
if (args.options.project) {
args.options.project._id = 0;
if (args.options.projection) {
args.options.projection._id = 0;
} else {
args.options.project = { _id: 0 };
args.options.projection = { _id: 0 };
}
}