Skip to content

Commit

Permalink
getThemesSearch tests
Browse files Browse the repository at this point in the history
  • Loading branch information
confused-Techie committed Sep 19, 2023
1 parent 90d40ce commit 37def97
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 18 deletions.
6 changes: 0 additions & 6 deletions src/controllers/getThemesSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ module.exports = {
);

if (!packs.ok) {
if (packs.short === "not_found") {
const sso = new context.ssoPaginate();

return sso.isOk().addContent([]);
}

const sso = new context.sso();

return sso.notOk().addContent(packs)
Expand Down
14 changes: 7 additions & 7 deletions src/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ async function insertNewPackage(pack) {
RETURNING pointer;
`;
} catch (e) {
throw `A constraint has been violated while inserting ${pack.name} in packages table.`;
throw `A constraint has been violated while inserting ${pack.name} in packages table: ${e.toString()}`;
}

if (!insertNewPack?.count) {
Expand Down Expand Up @@ -204,11 +204,11 @@ async function insertNewPackage(pack) {
})
.catch((err) => {
return typeof err === "string"
? { ok: false, content: err, short: "Server Error" }
? { ok: false, content: err, short: "server_error" }
: {
ok: false,
content: `A generic error occurred while inserting ${pack.name} package`,
short: "Server Error",
short: "server_error",
error: err,
};
});
Expand Down Expand Up @@ -337,11 +337,11 @@ async function insertNewPackageVersion(packJSON, oldName = null) {
})
.catch((err) => {
return typeof err === "string"
? { ok: false, content: err, short: "Server Error" }
? { ok: false, content: err, short: "server_error" }
: {
ok: false,
content: `A generic error occured while inserting the new package version ${packJSON.name}`,
short: "Server Error",
short: "server_error",
error: err,
};
});
Expand Down Expand Up @@ -1437,7 +1437,7 @@ async function simpleSearch(term, page, dir, sort, themes = false) {
return {
ok: false,
content: `Unrecognized Sorting Method Provided: ${sort}`,
short: "Server Error",
short: "server_error",
};
}

Expand Down Expand Up @@ -1498,7 +1498,7 @@ async function simpleSearch(term, page, dir, sort, themes = false) {
return {
ok: false,
content: "Generic Error",
short: "Server Error",
short: "server_error",
error: err,
};
}
Expand Down
2 changes: 1 addition & 1 deletion tests/http/getThemesFeatured.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe("Behaves as expected", () => {
});

const sso = await endpoint.logic({}, context);

expect(sso.ok).toBe(true);
expect(sso.content).toBeArray();
expect(sso.content.length).toBe(1);
Expand Down
93 changes: 93 additions & 0 deletions tests/http/getThemesSearch.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const endpoint = require("../../src/controllers/getThemesSearch.js");
const database = require("../../src/database.js");
const context = require("../../src/context.js");

describe("Behaves as expected", () => {
test("Calls the correct function", async () => {
const localContext = context;
const spy = jest.spyOn(localContext.database, "simpleSearch");

await endpoint.logic({}, localContext);

expect(spy).toBeCalledTimes(1);

spy.mockClear();
});

test("Returns zero length array when not found", async () => {
const sso = await endpoint.logic({
sort: "downloads",
page: "1",
direction: "desc",
query: "hello-world"
}, context);

expect(sso.ok).toBe(true);
expect(sso.content).toBeArray();
expect(sso.content.length).toBe(0);
});

test("Returns array on success", async () => {
const newPack = await database.insertNewPackage({
name: "atom-material-syntax",
repository: "https://github.com/confused-Techie/package-backend",
creation_method: "Test Package",
releases: {
latest: "1.1.0"
},
reamde: "This is a readme!",
metadata: {
name: "atom-material-syntax",
theme: "ui"
},
versions: {
"1.1.0": {
dist: {
tarball: "download-url",
sha: "1234"
},
name: "atom-material-syntax"
},
"1.0.0": {
dist: {
tarball: "download-url",
sha: "1234"
},
name: "atom-material-syntax"
}
}
});

const sso = await endpoint.logic({
sort: "downloads",
page: "1",
direction: "desc",
query: "atom-material"
}, context);

expect(sso.ok).toBe(true);
expect(sso.content).toBeArray();
expect(sso.content.length).toBe(1);
expect(sso.content[0].name).toBe("atom-material-syntax");
// TODO ensure it matches known good object
await database.removePackageByName("atom-material-syntax", true);
});

test("Returns error on db call failure", async () => {
// Moved to last position, since it modifies our shallow copied context
const localContext = context;
localContext.database = {
simpleSearch: () => { return { ok: false, content: "Test Error" } }
};

const sso = await endpoint.logic({
sort: "downloads",
page: "1",
direction: "desc",
query: "hello-world"
}, localContext);

expect(sso.ok).toBe(false);
expect(sso.content.content).toBe("Test Error");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ describe("Has features expected", () => {
describe("Parameters behave as expected", () => {
test("Returns valid 'page'", () => {
const req = {
params: {
query: {
page: "1"
}
};

const res = endpoint.params.page(context, req);
expect(res).toBe("1");
expect(res).toBe(1);
});
test("Returns valid 'sort'", () => {
const req = {
params: {
query: {
sort: "downloads"
}
};
Expand All @@ -41,7 +41,7 @@ describe("Parameters behave as expected", () => {
});
test("Returns valid 'direction'", () => {
const req = {
params: {
query: {
direction: "desc"
}
};
Expand Down
54 changes: 54 additions & 0 deletions tests/unit/controllers/getThemesSearch.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const endpoint = require("../../../src/controllers/getThemesSearch.js");
const context = require("../../../src/context.js");

describe("Has features expected", () => {
test("Has correct endpoint features", () => {
const expected = {
method: "GET",
paths: [ "/api/themes/search" ],
rateLimit: "generic",
successStatus: 200
};

expect(endpoint.endpoint).toMatchObject(expected);
});

test("Has correct functions", () => {
expect(endpoint.logic).toBeTypeof("function");
});
});

describe("Parameters behave as expected", () => {
test("Returns valid 'sort'", () => {
const req = {
query: { sort: "downloads" }
};

const res = endpoint.params.sort(context, req);
expect(res).toBe("downloads");
});
test("Returns valid 'page'", () => {
const req = {
query: { page: "1" }
};

const res = endpoint.params.page(context, req);
expect(res).toBe(1);
});
test("Returns valid 'direction'", () => {
const req = {
query: { direction: "desc" }
};

const res = endpoint.params.direction(context, req);
expect(res).toBe("desc");
});
test("Returns valid 'query'", () => {
const req = {
query: { q: "hello" }
};

const res = endpoint.params.query(context, req);
expect(res).toBe("hello");
});
});

0 comments on commit 37def97

Please sign in to comment.