-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapi-test.environment.js
More file actions
174 lines (161 loc) · 4.67 KB
/
api-test.environment.js
File metadata and controls
174 lines (161 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const fs = require('fs');
const NodeEnvironment = require('jest-environment-node');
const { Client: PgClient } = require('pg');
const prisma = require('../../lib/prisma');
const {
standards,
likertScaleQuestions,
wordsQuestions,
} = require('../../seedData');
const getClient = async () => {
const client = new PgClient({ connectionString: process.env.DATABASE_URL });
await client.connect();
return client;
};
class ApiTestEnvironment extends NodeEnvironment {
constructor(config, context) {
super(config, context);
}
async setup() {
await super.setup();
const client = await getClient();
await client.query('DROP SCHEMA IF EXISTS public CASCADE;');
await client.query('CREATE SCHEMA public;');
const schema = fs.readFileSync('schema.sql', 'utf8');
const schemaLines = schema.split(';');
for (let i = 0; i < schemaLines.length; i++) {
const line = schemaLines[i].trim() + ';';
if (line.startsWith('--')) continue;
await client.query(line);
}
await Promise.all([
prisma.users.create({
data: {
id: 'clinician',
user_type: 'clinician',
},
}),
prisma.users.create({
data: {
id: 'department_manager',
user_type: 'department_manager',
},
}),
prisma.users.create({
data: {
id: 'hospital',
user_type: 'hospital',
},
}),
prisma.users.create({
data: {
id: 'health_board',
user_type: 'health_board',
},
}),
prisma.health_boards.create({
data: {
id: 1,
name: 'Test Health Board',
hospitals: {
create: [
{
id: 1,
name: 'Test Hospital',
departments: {
create: [
{ id: 1, name: 'Test Department' },
{ id: 2, name: 'Test Department 2' },
],
},
},
{
id: 2,
name: 'Test Hospital 2',
departments: {
create: [{ id: 3, name: 'Test Department 3' }],
},
},
],
},
},
}),
prisma.health_boards.create({
data: {
id: 2,
name: 'Test Health Board 2',
hospitals: {
create: [
{
id: 3,
name: 'Test Hospital 3',
departments: {
create: [
{ id: 4, name: 'Test Department 4' },
{ id: 5, name: 'Test Department 5' },
],
},
},
{
id: 4,
name: 'Test Hospital 4',
departments: {
create: [{ id: 6, name: 'Test Department 6' }],
},
},
],
},
},
}),
...standards.map((standard, i) =>
prisma.standards.create({ data: { name: standard, id: i + 1 } })
),
]);
await Promise.all([
...likertScaleQuestions.map((question, i) =>
prisma.questions.create({
data: {
default_url: question.url,
standard_id: question.standardId,
type: 'likert_scale',
body: question.question,
},
})
),
]);
await Promise.all([
...wordsQuestions.map((question, i) =>
prisma.questions.create({
data: {
id: question.id,
default_url: question.url,
standard_id: question.standardId,
type: 'words',
body: question.question,
},
})
),
]);
// Start auto-incrementing at 1000 (large number) so the hard-coded values
// in the tests don't cause conflicts
await client.query('ALTER SEQUENCE departments_id_seq RESTART 1000;');
await client.query('ALTER SEQUENCE hospitals_id_seq RESTART 1000;');
await client.query('ALTER SEQUENCE health_boards_id_seq RESTART 1000;');
await client.query('ALTER SEQUENCE questions_id_seq RESTART 1000;');
await client.query('ALTER SEQUENCE responses_id_seq RESTART 1000;');
await client.query('ALTER SEQUENCE standards_id_seq RESTART 1000;');
await client.query('ALTER SEQUENCE words_id_seq RESTART 1000;');
await client.end();
}
async teardown() {
await prisma.$disconnect();
const client = await getClient();
await client.query(`DROP SCHEMA public CASCADE;`);
await client.end();
await super.teardown();
}
runScript(script) {
return super.runScript(script);
}
}
module.exports = ApiTestEnvironment;