Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rework: Refactor candidates #283

Merged
merged 6 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions src/features/db/class/Level.ts

This file was deleted.

23 changes: 0 additions & 23 deletions src/features/db/class/PreselectionFormData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,6 @@ type PreselectionFormDataType = {
value: string;
};

class PreselectionFormDataObject {
id: number;
campaign_id: number;
field_id: number;
value: string;
tester_id: number;

constructor(item: PreselectionFormDataType) {
this.id = item.id;
this.campaign_id = item.campaign_id;
this.tester_id = item.tester_id;
this.field_id = item.field_id;
this.value = item.value;
}
}

class PreselectionFormData extends Database<{
fields: PreselectionFormDataType;
}> {
Expand All @@ -36,12 +20,5 @@ class PreselectionFormData extends Database<{
: ["id", "campaign_id", "field_id", "value", "tester_id"],
});
}

public createObject(
row: PreselectionFormDataType
): PreselectionFormDataObject {
return new PreselectionFormDataObject(row);
}
}
export default PreselectionFormData;
export { PreselectionFormDataObject };
37 changes: 0 additions & 37 deletions src/features/db/class/UserLevel.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/features/db/mysql.ts

This file was deleted.

5 changes: 1 addition & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import app from "@src/app";
import config from "@src/config";
import connectionManager from "@src/features/db/mysql";
const PORT = config.port || 3000;

connectionManager.connectToServer(() => {
app.listen(PORT, () => console.info("api listening on port " + PORT));
});
app.listen(PORT, () => console.info("api listening on port " + PORT));
145 changes: 145 additions & 0 deletions src/middleware/getExample/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import getExample from ".";

describe("Get Example middleware", () => {
it("should return a middleware", () => {
expect(getExample).toBeInstanceOf(Function);
});
it("Should return false if path does not exists", () => {
const result = getExample(
{ definition: { paths: {} } },
"/endpoint",
"get",
"400",
undefined
);
expect(result).toBe(false);
});
it("Should return false if path does not have method", () => {
const result = getExample(
{ definition: { paths: { "/endpoint": {} } } },
"/endpoint",
"get",
"400",
undefined
);
expect(result).toBe(false);
});
it("Should return false if path method does not have response", () => {
const result = getExample(
{ definition: { paths: { "/endpoint": { get: { responses: {} } } } } },
"/endpoint",
"get",
"400",
undefined
);
expect(result).toBe(false);
});
it("Should return false if path method does not have response with requested status", () => {
const result = getExample(
{
definition: {
paths: { "/endpoint": { get: { responses: { "200": {} } } } },
},
},
"/endpoint",
"get",
"400",
undefined
);
expect(result).toBe(false);
});
it("Should return false if path method response does not have content", () => {
const result = getExample(
{
definition: {
paths: { "/endpoint": { get: { responses: { "400": {} } } } },
},
},
"/endpoint",
"get",
"400",
undefined
);
expect(result).toBe(false);
});
it("Should return false if path method response does not have application/json content", () => {
const result = getExample(
{
definition: {
paths: {
"/endpoint": {
get: { responses: { "400": { content: { "text/html": {} } } } },
},
},
},
},
"/endpoint",
"get",
"400",
undefined
);
expect(result).toBe(false);
});
it("Should return requested example if a specific example is requested", () => {
const result = getExample(
{
definition: {
paths: {
"/endpoint": {
get: {
responses: {
"400": {
content: {
"application/json": {
examples: {
"my-example": { value: { hello: "world" } },
},
},
},
},
},
},
},
},
},
},
"/endpoint",
"get",
"400",
"my-example"
);
expect(result).toEqual({ hello: "world" });
});

it("Should return first example if no specific example is requested", () => {
const result = getExample(
{
definition: {
paths: {
"/endpoint": {
get: {
responses: {
"400": {
content: {
"application/json": {
examples: {
"my-other-example": { value: { hello: "universe" } },
"my-example": { value: { hello: "world" } },
},
},
},
},
},
},
},
},
},
},
"/endpoint",
"get",
"400",
undefined
);
expect(result).toEqual({ hello: "universe" });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export default (
) {
return responseData.examples[example].value;
}
const firstExample = Object.values(response.examples).pop() as { value: any };
const firstExample = Object.values(responseData.examples).shift() as {
value: any;
};
if (!firstExample.hasOwnProperty("value")) {
return false;
}
Expand Down
13 changes: 13 additions & 0 deletions src/middleware/notFound/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import app from "@src/app";
import request from "supertest";

describe("Check NotFound middleware", () => {
it("should return 404 and a not found err", async () => {
const response = await request(app).get("/fakes/route-that-doesnt-exist");

expect(response.status).toBe(404);
expect(response.body).toEqual({
err: "not found",
});
});
});
File renamed without changes.
23 changes: 23 additions & 0 deletions src/middleware/notImplemented/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import notImplemented from ".";
describe("Not Implemented", () => {
it("should return a not implemented error", async () => {
const json = jest.fn();
const status = jest.fn(() => ({ json }));
// @ts-ignore
const middleware = notImplemented({
// @ts-ignore
mockResponseForOperation: (operation) => ({
status: 501,
mock: {
message: "Not Implemented",
},
}),
});
// @ts-ignore
middleware({ operation: { operationId: "" } }, {}, { status });
expect(json).toBeCalledTimes(1);
expect(json).toBeCalledWith({ message: "Not Implemented" });
expect(status).toBeCalledTimes(1);
expect(status).toBeCalledWith(501);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import OpenAPIBackend, { Context } from "openapi-backend";
export default (api: OpenAPIBackend) =>
async (c: Context, req: Request, res: OpenapiResponse) => {
res.skip_post_response_handler = true;
if (process.env && process.env.DEBUG) {
console.log(`Mocking ${c.operation.operationId}`);
}

const { status, mock } = api.mockResponseForOperation(
c.operation.operationId || ""
);
Expand Down
Loading
Loading