add test for addJob
This commit is contained in:
1 parent
61518f898b
commit
b063fa267a
3 files changed
+49
-8
No files matched your search
@@ -19,13 +19,11 @@ const getMetrics = async (req, res, next) => {
|
|||||||
const getJobs = async (req, res, next) => {
|
const getJobs = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const jobs = await req.jobQueue.getJobStats();
|
const jobs = await req.jobQueue.getJobStats();
|
||||||
return res
|
return res.status(200).json({
|
||||||
.status(200)
|
success: true,
|
||||||
.json({
|
msg: successMessages.QUEUE_GET_METRICS,
|
||||||
success: true,
|
data: jobs,
|
||||||
msg: successMessages.QUEUE_GET_METRICS,
|
});
|
||||||
data: jobs,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "getJobs"));
|
next(handleError(error, SERVICE_NAME, "getJobs"));
|
||||||
return;
|
return;
|
||||||
@@ -35,7 +33,10 @@ const getJobs = async (req, res, next) => {
|
|||||||
const addJob = async (req, res, next) => {
|
const addJob = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
await req.jobQueue.addJob(Math.random().toString(36).substring(7));
|
await req.jobQueue.addJob(Math.random().toString(36).substring(7));
|
||||||
return res.send("Added job");
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: successMessages.QUEUE_ADD_JOB,
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "addJob"));
|
next(handleError(error, SERVICE_NAME, "addJob"));
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -90,3 +90,41 @@ describe("Queue Controller - getJobs", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("Queue Controller - addJob", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
req = {
|
||||||
|
headers: {},
|
||||||
|
params: {},
|
||||||
|
body: {},
|
||||||
|
db: {},
|
||||||
|
jobQueue: {
|
||||||
|
addJob: sinon.stub(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
res = {
|
||||||
|
status: sinon.stub().returnsThis(),
|
||||||
|
json: sinon.stub(),
|
||||||
|
};
|
||||||
|
next = sinon.stub();
|
||||||
|
handleError = sinon.stub();
|
||||||
|
});
|
||||||
|
afterEach(() => {
|
||||||
|
sinon.restore();
|
||||||
|
});
|
||||||
|
it("should reject with an error if addJob throws an error", async () => {
|
||||||
|
req.jobQueue.addJob.throws(new Error("addJob error"));
|
||||||
|
await addJob(req, res, next);
|
||||||
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
|
expect(next.firstCall.args[0].message).to.equal("addJob error");
|
||||||
|
});
|
||||||
|
it("should return a success message if addJob is successful", async () => {
|
||||||
|
req.jobQueue.addJob.resolves();
|
||||||
|
await addJob(req, res, next);
|
||||||
|
expect(res.status.firstCall.args[0]).to.equal(200);
|
||||||
|
expect(res.json.firstCall.args[0]).to.deep.equal({
|
||||||
|
success: true,
|
||||||
|
msg: successMessages.QUEUE_ADD_JOB,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -89,6 +89,8 @@ const successMessages = {
|
|||||||
// Queue Controller
|
// Queue Controller
|
||||||
QUEUE_GET_METRICS: "Got metrics successfully",
|
QUEUE_GET_METRICS: "Got metrics successfully",
|
||||||
QUEUE_GET_METRICS: "Got job stats successfully",
|
QUEUE_GET_METRICS: "Got job stats successfully",
|
||||||
|
QUEUE_ADD_JOB: "Job added successfully",
|
||||||
|
QUEUE_OBLITERATE: "Queue obliterated",
|
||||||
|
|
||||||
//Job Queue
|
//Job Queue
|
||||||
JOB_QUEUE_DELETE_JOB: "Job removed successfully",
|
JOB_QUEUE_DELETE_JOB: "Job removed successfully",
|
||||||
|
|||||||
Reference in new issue
Block a user