-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c411e18
commit 7c26f1d
Showing
1 changed file
with
95 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
packages/moleculer-db-adapter-mongoose/test/integration/virtuals.spec.js
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,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); | ||
}); | ||
}); |