Skip to content

Commit

Permalink
Updated error handling in queries and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pelotfr committed Oct 19, 2023
1 parent 0584d86 commit d38bfa3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
34 changes: 25 additions & 9 deletions src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,35 @@ async function makeQuery(query: string, format: string = 'JSONObjectEachRow'): P
export async function salesCountQuery(collection_name: string): Promise<SalesCountQueryResponseSchema> {
const query = `SELECT COUNT(sale_id) FROM ${config.name} WHERE (collection_name == '${collection_name}')`;
const json = await makeQuery(query);

return SalesCountQueryResponseSchema.parse({
collection_name: collection_name,
sales_count: Object.values(json as JSONObjectEachRow)[0]["count(sale_id)"]
});

let responseSchema;
try {
responseSchema = SalesCountQueryResponseSchema.parse({
collection_name: collection_name,
sales_count: Object.values(json as JSONObjectEachRow)[0]["count(sale_id)"]
})
} catch {
throw new HTTPException(500, {
message: `Collection invalid, sales_count received : ${Object.values(json as JSONObjectEachRow)[0]["count(sale_id)"]} `
});
}
return responseSchema;
}

export async function totalVolumeQuery(collection_name: string) {
const query = `SELECT SUM(listing_price) FROM ${config.name} WHERE (collection_name == '${collection_name}')`;
const json = await makeQuery(query);

return TotalVolumeQueryResponseSchema.parse({
collection_name: collection_name,
total_volume: Object.values(json as JSONObjectEachRow)[0]["sum(listing_price)"]
});
let responseSchema;
try {
responseSchema = TotalVolumeQueryResponseSchema.parse({
collection_name: collection_name,
total_volume: Object.values(json as JSONObjectEachRow)[0]["sum(listing_price)"]
})
} catch {
throw new HTTPException(500, {
message: `Collection invalid, total_volume received : ${Object.values(json as JSONObjectEachRow)[0]["sum(listing_price)"]} `
});
}
return responseSchema;
}
38 changes: 24 additions & 14 deletions src/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,40 @@ describe('Sales count query page (/salescount?collection_name=<string>)', () =>
valid_collection_name = 'pomelo';
});

/*it.each(['', -1])('Should fail on missing or invalid collection name parameter: collection_name=%s', async (collection_name: string) => {
it.each(['', -1])('Should fail on missing or invalid collection name parameter: collection_name=%s', async (collection_name: string) => {
const res = await app.request(`/salescount?collection_name=${collection_name}`);
expect(res.status).toBe(400);
expect(res.status).toBe(500);

// Need to adapt to my use case
/*
const json = await res.json();
expect(json.success).toBe(false);
expect(json.ok).toBe(false);
expect(['invalid_union', 'too_small']).toContain(json.error.issues[0].code);
*/
});
/*it(`Should not allow more than the maximum number of elements to be queried (${config.maxElementsQueried})`, async () => {

/*
it(`Should not allow more than the maximum number of elements to be queried (${config.maxElementsQueried})`, async () => {
const res = await app.request(`/sales_count?collection_name=${Array(config.maxElementsQueried + 1).fill(valid_collection_name).toString()}`);
expect(res.status).toBe(400);
const json = await res.json();
expect(json.success).toBe(false);
expect(json.error.issues[0].code).toBe('too_big');
});*/
});
*/

it('Should return (200) on valid input', async () => {
const res = await app.request(`/salescount?collection_name=${valid_collection_name}`);
expect(res.status).toBe(200);

/*
const json = await res.json();
expect(json).toHaveLength(0);*/
expect(json).toHaveLength(0);
*/
});
});
/*

describe('Total volume query page (/totalvolume?collection_name=<string>)', () => {
let valid_collection_name: string;

Expand All @@ -65,27 +71,31 @@ describe('Total volume query page (/totalvolume?collection_name=<string>)', () =

it.each(['', -1])('Should fail on missing or invalid collection name parameter: collection_name=', async (collection_name: string) => {
const res = await app.request(`/totalvolume?collection_name=${collection_name}`);
expect(res.status).toBe(400);
expect(res.status).toBe(500);
/*
const json = await res.json();
expect(json.success).toBe(false);
expect(['invalid_union', 'too_small']).toContain(json.error.issues[0].code);
*/
});

/*it(`Should not allow more than the maximum number of elements to be queried (${config.maxElementsQueried})`, async () => {
/*
it(`Should not allow more than the maximum number of elements to be queried (${config.maxElementsQueried})`, async () => {
const res = await app.request(`/totalvolume?collection_name=${Array(config.maxElementsQueried + 1).fill(valid_collection_name).toString()}`);
expect(res.status).toBe(400);
const json = await res.json();
expect(json.success).toBe(false);
expect(json.error.issues[0].code).toBe('too_big');
});
*/

it('Should return (200) empty JSON on valid input', async () => {
const res = await app.request(`/totalvolume?collection_name=${valid_collection_name}`);
expect(res.status).toBe(200);
/*
const json = await res.json();
expect(json).toHaveLength(0);
*/
});
});*/
});

0 comments on commit d38bfa3

Please sign in to comment.