<commit_before>"use strict";

const { ServiceBroker } = require("moleculer");
const MongooseStoreAdapter = require("../../src");
const mongoose = require("mongoose");

function protectReject(err) {
	expect(err).toBe(true);
}

const doc = {
	toJSON: jest.fn()
};
const docs = [doc];

const execCB = jest.fn(() => Promise.resolve());
const saveCB = jest.fn(() => Promise.resolve());
const leanCB = jest.fn(() => ({ exec: execCB }));
const countCB = jest.fn(() => ({ exec: execCB }));
const query = jest.fn(() => ({ lean: leanCB, count: countCB }));

const fakeCollection = Object.assign(jest.fn(() => ({ save: saveCB })), {
	find: jest.fn(() => query()),
	findById: jest.fn(() => query()),
	insertMany: jest.fn(() => Promise.resolve()),
	update: jest.fn(() => Promise.resolve(docs)),
	findByIdAndUpdate: jest.fn(() => Promise.resolve(doc)),
	remove: jest.fn(() => Promise.resolve()),
	findByIdAndRemove: jest.fn(() => Promise.resolve()),
});


let fakeConn = Promise.resolve();
fakeConn.connection = {
	on: jest.fn(),
	close: jest.fn()
};

describe("Test MongooseStoreAdapter", () => {
	const broker = new ServiceBroker();
	const service = broker.createService({
		name: "store",
		collection: fakeCollection
	});

	const opts = {};
	const adapter = new MongooseStoreAdapter(opts);

	it("should be created", () => {
		expect(adapter).toBeDefined();
		expect(adapter.opts).toBe(opts);
		expect(adapter.init).toBeDefined();
		expect(adapter.connect).toBeDefined();
		expect(adapter.disconnect).toBeDefined();
		expect(adapter.findAll).toBeDefined();
		expect(adapter.findById).toBeDefined();
		expect(adapter.findByIds).toBeDefined();
		expect(adapter.count).toBeDefined();
		expect(adapter.insert).toBeDefined();
		expect(adapter.insertMany).toBeDefined();
		expect(adapter.update).toBeDefined();
		expect(adapter.updateById).toBeDefined();
		expect(adapter.remove).toBeDefined();
		expect(adapter.removeById).toBeDefined();
		expect(adapter.clear).toBeDefined();
	});

	it("call init", () => {
		adapter.init(broker, service);
		expect(adapter.broker).toBe(broker);
		expect(adapter.service).toBe(service);
		expect(adapter.collection).toBe(fakeCollection);
	});

	it("call connect with uri", () => {
		fakeConn.connection.on.mockClear();

		mongoose.connect = jest.fn(() => fakeConn);
		adapter.opts = "mongodb://server";
		return adapter.connect().catch(protectReject).then(() => {
			expect(mongoose.connect).toHaveBeenCalledTimes(1);
			expect(mongoose.connect).toHaveBeenCalledWith("mongodb://server", undefined);

			expect(adapter.db).toBe(fakeConn.connection);
			expect(adapter.db.on).toHaveBeenCalledTimes(1);
			expect(adapter.db.on).toHaveBeenCalledWith("disconnected", jasmine.any(Function));
		});
	});

	it("call connect with uri & opts", () => {
		fakeConn.connection.on.mockClear();

		mongoose.connect = jest.fn(() => fakeConn);
		adapter.opts = {
			uri: "mongodb://server",
			opts: {
				user: "admin",
				pass: "<PASSWORD>"
			}
		};

		return adapter.connect().catch(protectReject).then(() => {
			expect(mongoose.connect).toHaveBeenCalledTimes(1);
			expect(mongoose.connect).toHaveBeenCalledWith(adapter.opts.uri, adapter.opts.opts);
		});
	});

	it("call disconnect", () => {
		fakeConn.connection.close.mockClear();

		return adapter.disconnect().catch(protectReject).then(() => {
			expect(fakeConn.connection.close).toHaveBeenCalledTimes(1);
		});
	});


	describe("Test doFiltering", () => {
		it("init", () => {
			adapter.collection.find = jest.fn(() => ({
				find: jest.fn(),
				sort: jest.fn(),
				skip: jest.fn(),
				limit: jest.fn(),
				lean: leanCB, 
				count: countCB
			}));
		});

		it("call without params", () => {
			adapter.collection.find.mockClear();
			let q = adapter.doFiltering();
			expect(adapter.collection.find).toHaveBeenCalledTimes(1);
			expect(adapter.collection.find).toHaveBeenCalledWith();
		});

		it("call with query", () => {
			adapter.collection.find.mockClear();
			let query = {};
			let q = adapter.doFiltering({ query });
			expect(adapter.collection.find).toHaveBeenCalledTimes(1);
			expect(adapter.collection.find).toHaveBeenCalledWith(query);
		});

		it("call with sort string", () => {
			adapter.collection.find.mockClear();
			let query = {};
			let q = adapter.doFiltering({ query, sort: "-votes title" });
			expect(adapter.collection.find).toHaveBeenCalledTimes(1);
			expect(adapter.collection.find).toHaveBeenCalledWith(query);
			
			expect(q.sort).toHaveBeenCalledTimes(1);
			expect(q.sort).toHaveBeenCalledWith("-votes title");
		});

		it("call with sort array", () => {
			adapter.collection.find.mockClear();
			let query = {};
			let q = adapter.doFiltering({ query, sort: ["createdAt", "title"] });
			expect(adapter.collection.find).toHaveBeenCalledTimes(1);
			expect(adapter.collection.find).toHaveBeenCalledWith(query);
			
			expect(q.sort).toHaveBeenCalledTimes(1);
			expect(q.sort).toHaveBeenCalledWith("createdAt title");
		});

		it("call with limit & offset", () => {
			adapter.collection.find.mockClear();
			let q = adapter.doFiltering({ limit: 5, offset: 10 });
			expect(adapter.collection.find).toHaveBeenCalledTimes(1);<commit_msg>fix lints<commit_after>"use strict";

const { ServiceBroker } = require("moleculer");
const MongooseStoreAdapter = require("../../src");
const mongoose = require("mongoose");

function protectReject(err) {
	expect(err).toBe(true);
}

const doc = {
	toJSON: jest.fn()
};
const docs = [doc];

const execCB = jest.fn(() => Promise.resolve());
const saveCB = jest.fn(() => Promise.resolve());
const leanCB = jest.fn(() => ({ exec: execCB }));
const countCB = jest.fn(() => ({ exec: execCB }));
const query = jest.fn(() => ({ lean: leanCB, count: countCB }));

const fakeCollection = Object.assign(jest.fn(() => ({ save: saveCB })), {
	find: jest.fn(() => query()),
	findById: jest.fn(() => query()),
	insertMany: jest.fn(() => Promise.resolve()),
	update: jest.fn(() => Promise.resolve(docs)),
	findByIdAndUpdate: jest.fn(() => Promise.resolve(doc)),
	remove: jest.fn(() => Promise.resolve()),
	findByIdAndRemove: jest.fn(() => Promise.resolve()),
});


let fakeConn = Promise.resolve();
fakeConn.connection = {
	on: jest.fn(),
	close: jest.fn()
};

describe("Test MongooseStoreAdapter", () => {
	const broker = new ServiceBroker();
	const service = broker.createService({
		name: "store",
		collection: fakeCollection
	});

	const opts = {};
	const adapter = new MongooseStoreAdapter(opts);

	it("should be created", () => {
		expect(adapter).toBeDefined();
		expect(adapter.opts).toBe(opts);
		expect(adapter.init).toBeDefined();
		expect(adapter.connect).toBeDefined();
		expect(adapter.disconnect).toBeDefined();
		expect(adapter.findAll).toBeDefined();
		expect(adapter.findById).toBeDefined();
		expect(adapter.findByIds).toBeDefined();
		expect(adapter.count).toBeDefined();
		expect(adapter.insert).toBeDefined();
		expect(adapter.insertMany).toBeDefined();
		expect(adapter.update).toBeDefined();
		expect(adapter.updateById).toBeDefined();
		expect(adapter.remove).toBeDefined();
		expect(adapter.removeById).toBeDefined();
		expect(adapter.clear).toBeDefined();
	});

	it("call init", () => {
		adapter.init(broker, service);
		expect(adapter.broker).toBe(broker);
		expect(adapter.service).toBe(service);
		expect(adapter.collection).toBe(fakeCollection);
	});

	it("call connect with uri", () => {
		fakeConn.connection.on.mockClear();

		mongoose.connect = jest.fn(() => fakeConn);
		adapter.opts = "mongodb://server";
		return adapter.connect().catch(protectReject).then(() => {
			expect(mongoose.connect).toHaveBeenCalledTimes(1);
			expect(mongoose.connect).toHaveBeenCalledWith("mongodb://server", undefined);

			expect(adapter.db).toBe(fakeConn.connection);
			expect(adapter.db.on).toHaveBeenCalledTimes(1);
			expect(adapter.db.on).toHaveBeenCalledWith("disconnected", jasmine.any(Function));
		});
	});

	it("call connect with uri & opts", () => {
		fakeConn.connection.on.mockClear();

		mongoose.connect = jest.fn(() => fakeConn);
		adapter.opts = {
			uri: "mongodb://server",
			opts: {
				user: "admin",
				pass: "<PASSWORD>"
			}
		};

		return adapter.connect().catch(protectReject).then(() => {
			expect(mongoose.connect).toHaveBeenCalledTimes(1);
			expect(mongoose.connect).toHaveBeenCalledWith(adapter.opts.uri, adapter.opts.opts);
		});
	});

	it("call disconnect", () => {
		fakeConn.connection.close.mockClear();

		return adapter.disconnect().catch(protectReject).then(() => {
			expect(fakeConn.connection.close).toHaveBeenCalledTimes(1);
		});
	});


	describe("Test doFiltering", () => {
		it("init", () => {
			adapter.collection.find = jest.fn(() => ({
				find: jest.fn(),
				sort: jest.fn(),
				skip: jest.fn(),
				limit: jest.fn(),
				lean: leanCB, 
				count: countCB
			}));
		});

		it("call without params", () => {
			adapter.collection.find.mockClear();
			adapter.doFiltering();
			expect(adapter.collection.find).toHaveBeenCalledTimes(1);
			expect(adapter.collection.find).toHaveBeenCalledWith();
		});

		it("call with query", () => {
			adapter.collection.find.mockClear();
			let query = {};
			adapter.doFiltering({ query });
			expect(adapter.collection.find).toHaveBeenCalledTimes(1);
			expect(adapter.collection.find).toHaveBeenCalledWith(query);
		});

		it("call with sort string", () => {
			adapter.collection.find.mockClear();
			let query = {};
			let q = adapter.doFiltering({ query, sort: "-votes title" });
			expect(adapter.collection.find).toHaveBeenCalledTimes(1);
			expect(adapter.collection.find).toHaveBeenCalledWith(query);
			
			expect(q.sort).toHaveBeenCalledTimes(1);
			expect(q.sort).toHaveBeenCalledWith("-votes title");
		});

		it("call with sort array", () => {
			adapter.collection.find.mockClear();
			let query = {};
			let q = adapter.doFiltering({ query, sort: ["createdAt", "title"] });
			expect(adapter.collection.find).toHaveBeenCalledTimes(1);
			expect(adapter.collection.find).toHaveBeenCalledWith(query);
			
			expect(q.sort).toHaveBeenCalledTimes(1);
			expect(q.sort).toHaveBeenCalledWith("createdAt title");
		});

		it("call with limit & offset", () => {
			adapter.collection.find.mockClear();
			let q = adapter.doFiltering({ limit: 5, offset: 10 });
			expect(adapter.collection.find).toHaveBeenCalledTimes(1);
{'fraction_non_alphanumeric': 0.1911993769470405, 'fraction_numerical': 0.0027258566978193145, 'mean_word_length': 4.751959686450168, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5997028', 'n_tokens_mistral': 3606, 'n_tokens_neox': 3292, 'n_words': 518}