Skip to content

Commit ffaaecf

Browse files
authored
Merge pull request #55 from PhilanthropyDataCommons/30-add-get-canonical-fields
Add GET /canonicalFields endpoint
2 parents c6462e9 + bc0b98c commit ffaaecf

18 files changed

Lines changed: 327 additions & 42 deletions

package-lock.json

Lines changed: 135 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
"typescript": "^4.7.3"
6060
},
6161
"dependencies": {
62+
"ajv": "^8.11.0",
63+
"ajv-keywords": "^5.1.0",
6264
"dotenv": "^16.0.1",
6365
"express": "^4.18.1",
6466
"pino": "^8.1.0",
Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,65 @@
11
import request from 'supertest';
22
import { app } from '../app';
3+
import { db } from '../database';
4+
import type { Result } from 'tinypg';
35

46
const agent = request.agent(app);
57

68
describe('/canonicalFields', () => {
79
describe('/', () => {
8-
it('should return HTTP Status Code 200 OK', async () => {
10+
it('returns an empty array when no data is present', async () => {
911
await agent
1012
.get('/canonicalFields')
11-
.expect(200);
13+
.expect(200, []);
14+
});
15+
16+
it('returns all canonical fields present in the database', async () => {
17+
await db.query(`
18+
INSERT INTO canonical_fields (
19+
label,
20+
short_code,
21+
data_type,
22+
created_at
23+
)
24+
VALUES
25+
( 'First Name', 'firstName', 'string', '2022-07-20 12:00:00+0000' ),
26+
( 'Last Name', 'lastName', 'string', '2022-07-20 12:00:00+0000' );
27+
`);
28+
await agent
29+
.get('/canonicalFields')
30+
.expect(
31+
200,
32+
[
33+
{
34+
createdAt: '2022-07-20T12:00:00.000Z',
35+
dataType: 'string',
36+
id: 1,
37+
label: 'First Name',
38+
shortCode: 'firstName',
39+
},
40+
{
41+
createdAt: '2022-07-20T12:00:00.000Z',
42+
dataType: 'string',
43+
id: 2,
44+
label: 'Last Name',
45+
shortCode: 'lastName',
46+
},
47+
],
48+
);
49+
});
50+
51+
it('should error if the database returns an unexpected data structure', async () => {
52+
jest.spyOn(db, 'sql')
53+
.mockImplementationOnce(async () => ({
54+
rows: [{ foo: 'not a valid result' }],
55+
}) as Result<object>);
56+
const result = await agent
57+
.get('/canonicalFields')
58+
.expect(500);
59+
expect(result.body).toMatchObject({
60+
name: 'ValidationError',
61+
errors: expect.any(Array) as unknown[],
62+
});
1263
});
1364
});
1465
});

src/ajv.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Ajv from 'ajv';
2+
import ajvKeywords from 'ajv-keywords';
3+
4+
export const ajv = new Ajv();
5+
ajvKeywords(ajv, 'instanceof');

0 commit comments

Comments
 (0)