From 7c26f1d83585c09b70aeddadaa7e36a7a26f0cd3 Mon Sep 17 00:00:00 2001 From: Nico Date: Sun, 30 Apr 2023 13:10:18 +0200 Subject: [PATCH] Create virtuals.spec.js --- .../test/integration/virtuals.spec.js | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 packages/moleculer-db-adapter-mongoose/test/integration/virtuals.spec.js diff --git a/packages/moleculer-db-adapter-mongoose/test/integration/virtuals.spec.js b/packages/moleculer-db-adapter-mongoose/test/integration/virtuals.spec.js new file mode 100644 index 00000000..72ab2c59 --- /dev/null +++ b/packages/moleculer-db-adapter-mongoose/test/integration/virtuals.spec.js @@ -0,0 +1,95 @@ +"use strict"; + +const mongoose = require("mongoose"); +const { ServiceBroker } = require("moleculer"); +const DbService = require("../../../moleculer-db/src"); +const MongooseStoreAdapter = require("../../src"); +const User = require("../models/users"); +const Post = require("../models/posts"); + +describe("Test virtuals population feature", () => { + // Create broker + const broker = new ServiceBroker({ + logger: console, + logLevel: "error", + }); + + beforeAll(async () => { + // Load posts service + broker.createService(DbService, { + name: "posts", + adapter: new MongooseStoreAdapter("mongodb://localhost"), + model: Post.Model, + settings: { + fields: ["_id", "title", "content", "votes", "author"], + populates: { + author: "users.get", + }, + }, + }); + + // Load users service + broker.createService(DbService, { + name: "users", + adapter: new MongooseStoreAdapter("mongodb://localhost"), + model: User.Model, + settings: { + fields: ["_id", "firstName", "lastName"], + populates: { + posts: "posts.get", + lastPost: "posts.get", + }, + }, + }); + + await broker.start(); + }); + + afterAll(async () => { + await broker.stop(); + }); + + beforeEach(async () => { + // clean collection for replayability + await Post.Model.deleteMany({}); + await User.Model.deleteMany({}); + }); + + it("Should populate virtuals", async () => { + const _user = await User.Model.create({ + firstName: "John", + lastName: "Doe", + }); + + const _post1 = await Post.Model.create({ + title: "post_1", + content: "content 1", + author: _user._id, + }); + + const _post2 = await Post.Model.create({ + title: "post_2", + content: "content 2", + author: _user._id, + }); + + const user = await broker.call("users.get", { + id: _user.id, + populate: ["posts", "lastPost", "postCount"], + }); + + expect(user).toHaveProperty("firstName", "John"); + expect(user).toHaveProperty("lastName", "Doe"); + // virtual function without populate + expect(user).toHaveProperty("fullName", "John Doe"); + // virtual populate with ref and count option + expect(user).toHaveProperty("postCount", 2); + // virtual populate with ref + expect(user).toHaveProperty("posts"); + expect(user.posts).toHaveLength(2); + expect(user.posts.map((p) => p._id)).toEqual([_post2.id, _post1.id]); + // virtual populate with justOne option set to "true" + expect(user).toHaveProperty("lastPost"); + expect(user.lastPost).toHaveProperty("_id", _post2.id); + }); +});