Skip to content

Commit 0bc98d4

Browse files
Added unit tests for the observations model
1 parent abdc679 commit 0bc98d4

File tree

3 files changed

+162
-2
lines changed

3 files changed

+162
-2
lines changed

api/test/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
require('./indicators');
2+
require('./observations');
23
require('./legacy');

api/test/observations.js

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/* eslint-env mocha */
2+
/* eslint "node/no-unpublished-require": 0 */
3+
const assert = require('assert');
4+
5+
describe('Observations', () => {
6+
describe('Model', () => {
7+
const observations = require('../endpoints/observations/model');
8+
9+
describe('`list` function', () => {
10+
it('should return an object containing list of observations and total count', async () => {
11+
return new Promise(async (resolve, reject) => {
12+
const list = await observations.list(0, 10).catch(reject);
13+
try {
14+
assert.strictEqual(typeof list, 'object');
15+
assert.strictEqual(list.length, 13);
16+
assert.strictEqual(list.list.length, 10);
17+
resolve();
18+
} catch (e) {
19+
reject(e);
20+
}
21+
});
22+
});
23+
24+
it('should return the same object as the `get` function', async () => {
25+
return new Promise(async (resolve, reject) => {
26+
const list = await observations.list(0, 10).catch(reject);
27+
const indicator = await observations.get(list.list[0].id).catch(reject);
28+
try {
29+
assert.strictEqual(JSON.stringify(list.list[0]), JSON.stringify(indicator));
30+
resolve();
31+
} catch (e) {
32+
reject(e);
33+
}
34+
});
35+
});
36+
37+
it('should offset the list by the `start` argument', async () => {
38+
return new Promise(async (resolve, reject) => {
39+
const start0 = await observations.list(0, 10).catch(reject);
40+
const start1 = await observations.list(1, 10).catch(reject);
41+
try {
42+
assert.strictEqual(start1.list[0].id, start0.list[1].id);
43+
resolve();
44+
} catch (e) {
45+
reject(e);
46+
}
47+
});
48+
});
49+
50+
it('should limit the list to the `count` argument', async () => {
51+
return new Promise(async (resolve, reject) => {
52+
const list = await observations.list(0, 1).catch(reject);
53+
try {
54+
assert.strictEqual(list.list.length, 1);
55+
resolve();
56+
} catch (e) {
57+
reject(e);
58+
}
59+
});
60+
});
61+
62+
describe('Sorting', () => {
63+
it.skip('should have tests');
64+
});
65+
66+
describe('Filtering', () => {
67+
describe('by Date', () => {
68+
it.skip('should have tests');
69+
});
70+
71+
describe('by Timeseries', () => {
72+
it.skip('should have tests');
73+
});
74+
75+
describe('by Indicator', () => {
76+
it.skip('should have tests');
77+
});
78+
79+
describe('by Dimension', () => {
80+
it.skip('should have tests');
81+
});
82+
});
83+
});
84+
85+
describe('`isValid` function', () => {
86+
it('should return true for a valid observation id', () => {
87+
assert.strictEqual(observations.isValid('235fd621-a670-4b62-b829-dc1da213e062'), true);
88+
});
89+
90+
it('should return true for a valid observation id', () => {
91+
assert.strictEqual(observations.isValid('%invalid-id'), false);
92+
assert.strictEqual(observations.isValid('235zz621-z670-4z62-z829-zz1dz213z062'), false);
93+
});
94+
});
95+
96+
describe('`exists` function', () => {
97+
it('should return `true` for existing observation', async () => {
98+
return new Promise(async (resolve, reject) => {
99+
const exists = await observations.exists('41f7ef3e-2dd1-4864-ac77-5daa4c13f04f').catch(reject);
100+
try {
101+
assert.strictEqual(exists, true);
102+
resolve();
103+
} catch (e) {
104+
reject(e);
105+
}
106+
});
107+
});
108+
109+
it('should return `false` for non-existing observation', async () => {
110+
return new Promise(async (resolve, reject) => {
111+
const exists = await observations.exists('8a9be92b-8b1b-4783-b9ff-4bec20e067a8').catch(reject);
112+
try {
113+
assert.strictEqual(exists, false);
114+
resolve();
115+
} catch (e) {
116+
reject(e);
117+
}
118+
});
119+
});
120+
});
121+
122+
describe('`get` function', () => {
123+
it('should return an observation object', async () => {
124+
return new Promise(async (resolve, reject) => {
125+
const indicator = await observations.get('41f7ef3e-2dd1-4864-ac77-5daa4c13f04f').catch(reject);
126+
try {
127+
assert.strictEqual(indicator.type, 'observation');
128+
assert.strictEqual(indicator.id, '41f7ef3e-2dd1-4864-ac77-5daa4c13f04f');
129+
resolve();
130+
} catch (e) {
131+
reject(e);
132+
}
133+
});
134+
});
135+
136+
it('should return `undefined` for non-existing observation', async () => {
137+
return new Promise(async (resolve, reject) => {
138+
const indicator = await observations.get('8a9be92b-8b1b-4783-b9ff-4bec20e067a8').catch(reject);
139+
try {
140+
assert.strictEqual(indicator, undefined);
141+
resolve();
142+
} catch (e) {
143+
reject(e);
144+
}
145+
});
146+
});
147+
});
148+
});
149+
150+
describe('API', () => {
151+
describe('Output', () => {
152+
153+
});
154+
155+
describe('Routes', () => {
156+
157+
});
158+
});
159+
});

test_data/test_data.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ VALUES ('ac139d12-22ce-45ba-a63e-14f1dc9f3ec4', (SELECT id FROM type_sex WHERE n
159159
REFRESH MATERIALIZED VIEW "mvTimeseriesDimensions";
160160

161161
WITH o AS (
162-
INSERT INTO observations (period, timeseries_id)
163-
VALUES ('2019-01-01', 'd3a06c6b-82a7-4efa-8f0f-6cb453deff2c')
162+
INSERT INTO observations (id, period, timeseries_id)
163+
VALUES ('41f7ef3e-2dd1-4864-ac77-5daa4c13f04f', '2019-01-01', 'd3a06c6b-82a7-4efa-8f0f-6cb453deff2c')
164164
RETURNING id
165165
)
166166
INSERT INTO observation_values (observation_id, value)

0 commit comments

Comments
 (0)