Skip to content

Commit

Permalink
Added test cases for PR / #261
Browse files Browse the repository at this point in the history
  • Loading branch information
ayaan-qadri committed Oct 12, 2024
1 parent cf4b544 commit caac0af
Showing 1 changed file with 87 additions and 22 deletions.
109 changes: 87 additions & 22 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1545,8 +1545,8 @@ describe("serverless-wsgi", () => {
});

describe("exec", () => {
const mockCli = Object({log: () => {}});
const mockCli = Object({ log: () => {} });

it("fails when invoked without command or file", () => {
var plugin = new Plugin(
{
Expand Down Expand Up @@ -1681,7 +1681,7 @@ describe("serverless-wsgi", () => {
});

describe("exec local", () => {
const mockCli = {log: () => {}};
const mockCli = { log: () => {} };

it("fails when invoked without command or file", () => {
var plugin = new Plugin(
Expand Down Expand Up @@ -1788,7 +1788,7 @@ describe("serverless-wsgi", () => {
});

describe("command", () => {
const mockCli = {log: () => {}};
const mockCli = { log: () => {} };

it("fails when no wsgi handler is set", () => {
var plugin = new Plugin(
Expand Down Expand Up @@ -1915,7 +1915,7 @@ describe("serverless-wsgi", () => {
});

describe("command local", () => {
const mockCli = {log: () => {}};
const mockCli = { log: () => {} };

it("fails when no wsgi handler is set", () => {
var plugin = new Plugin(
Expand Down Expand Up @@ -2046,34 +2046,48 @@ describe("serverless-wsgi", () => {
});

describe("manage", () => {
const mockCli = Object({log: () => {}});
const mockCli = {
log: sinon.spy(),
};

it("calls handler to execute manage commands remotely from argument", () => {
var plugin = new Plugin(
let plugin;
let sandbox;

beforeEach(() => {

Check failure on line 2056 in index.test.js

View workflow job for this annotation

GitHub Actions / 3.9

'beforeEach' is not defined

Check failure on line 2056 in index.test.js

View workflow job for this annotation

GitHub Actions / 3.8

'beforeEach' is not defined

Check failure on line 2056 in index.test.js

View workflow job for this annotation

GitHub Actions / 3.7

'beforeEach' is not defined
sandbox = sinon.createSandbox();
plugin = new Plugin(
{
config: { servicePath: "/tmp" },
service: {
provider: { runtime: "python2.7" },
custom: { wsgi: { app: "api.app" } },
functions: { app: { handler: "wsgi_handler.handler" } },
functions: {
app: { handler: "wsgi_handler.handler" },
otherFunc: { handler: "other_handler.handler" },
},
},
classes: { Error: Error },
cli: mockCli,
pluginManager: {
cliOptions: {},
run: (command) =>
new BbPromise((resolve) => {
expect(command).to.deep.equal(["invoke"]);
console.log('[0, "manage command output"]'); // eslint-disable-line no-console
resolve();
}),
run: sandbox.stub().resolves(),
},
},
{ command: "check" }
);
});

afterEach(() => {

Check failure on line 2080 in index.test.js

View workflow job for this annotation

GitHub Actions / 3.9

'afterEach' is not defined

Check failure on line 2080 in index.test.js

View workflow job for this annotation

GitHub Actions / 3.8

'afterEach' is not defined

Check failure on line 2080 in index.test.js

View workflow job for this annotation

GitHub Actions / 3.7

'afterEach' is not defined
sandbox.restore();
});

it("calls handler to execute manage commands remotely from argument", () => {
plugin.serverless.pluginManager.run.callsFake((command) => {
expect(command).to.deep.equal(["invoke"]);
console.log('[0, "manage command output"]');
return BbPromise.resolve();
});

var sandbox = sinon.createSandbox();
let loggerSpy = sandbox.spy(mockCli, "log");
return plugin.hooks["wsgi:manage:manage"]().then(() => {
expect(plugin.serverless.pluginManager.cliOptions.f).to.equal("app");
expect(plugin.options.function).to.equal("app");
Expand All @@ -2083,14 +2097,65 @@ describe("serverless-wsgi", () => {
expect(plugin.options.data).to.equal(
'{"_serverless-wsgi":{"command":"manage","data":"check"}}'
);
expect(loggerSpy.calledWith("manage command output")).to.be.true;
sandbox.restore();
expect(mockCli.log.calledWith("manage command output")).to.be.true;
});
});

it("uses the function specified by --function", () => {
plugin.options.function = "otherFunc";

return plugin.hooks["wsgi:manage:manage"]().then(() => {
expect(plugin.serverless.pluginManager.cliOptions.f).to.equal(
"otherFunc"
);
expect(plugin.options.function).to.equal("otherFunc");
});
});

it("uses the function specified by -f", () => {
plugin.options.f = "otherFunc";

return plugin.hooks["wsgi:manage:manage"]().then(() => {
expect(plugin.serverless.pluginManager.cliOptions.f).to.equal(
"otherFunc"
);
expect(plugin.options.function).to.equal("otherFunc");
});
});

it("throws an error when specified function is not found", () => {
plugin.options.function = "nonExistentFunc";

return expect(plugin.hooks["wsgi:manage:manage"]()).to.be.rejectedWith(
'Function "nonExistentFunc" not found.'
);
});

it("falls back to finding wsgi handler when no function is specified", () => {
delete plugin.options.function;
delete plugin.options.f;

return plugin.hooks["wsgi:manage:manage"]().then(() => {
expect(plugin.serverless.pluginManager.cliOptions.f).to.equal("app");
expect(plugin.options.function).to.equal("app");
});
});

it("rejects when no wsgi handler is found and no function is specified", () => {
delete plugin.options.function;
delete plugin.options.f;
plugin.serverless.service.functions = {
someFunc: { handler: "some_handler.handler" },
};

return expect(plugin.hooks["wsgi:manage:manage"]()).to.be.rejectedWith(
"No functions were found with handler: wsgi_handler.handler"
);
});
});

describe("manage local", () => {
const mockCli = Object({log: () => {}});
const mockCli = Object({ log: () => {} });

it("calls handler to execute manage commands locally from argument", () => {
var plugin = new Plugin(
Expand Down Expand Up @@ -2137,7 +2202,7 @@ describe("serverless-wsgi", () => {
});

describe("flask", () => {
const mockCli = Object({log: () => {}});
const mockCli = Object({ log: () => {} });
it("calls handler to execute flask commands remotely from argument", () => {
var plugin = new Plugin(
{
Expand Down Expand Up @@ -2180,7 +2245,7 @@ describe("serverless-wsgi", () => {
});

describe("flask local", () => {
const mockCli = Object({log: () => {}});
const mockCli = Object({ log: () => {} });
it("calls handler to execute flask commands locally from argument", () => {
var plugin = new Plugin(
{
Expand Down

0 comments on commit caac0af

Please sign in to comment.