Skip to content

Commit

Permalink
Merge pull request #283 from AppQuality/refactor-candidates
Browse files Browse the repository at this point in the history
rework: Refactor candidates
  • Loading branch information
d-beezee authored Feb 27, 2024
2 parents 9fec62e + ecf4682 commit f23fcd5
Show file tree
Hide file tree
Showing 20 changed files with 837 additions and 746 deletions.
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

0 comments on commit f23fcd5

Please sign in to comment.