Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/src/project-db.spec.db.ts
2497 views
1
/**
2
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { Project } from "@gitpod/gitpod-protocol";
8
import { suite, test } from "@testdeck/mocha";
9
import * as chai from "chai";
10
import { testContainer } from "./test-container";
11
import { resetDB } from "./test/reset-db";
12
import { ProjectDBImpl } from "./typeorm/project-db-impl";
13
import { TypeORM } from "./typeorm/typeorm";
14
import { TypeORMUserDBImpl } from "./typeorm/user-db-impl";
15
const expect = chai.expect;
16
17
@suite
18
class ProjectDBSpec {
19
projectDb = testContainer.get<ProjectDBImpl>(ProjectDBImpl);
20
userDb = testContainer.get<TypeORMUserDBImpl>(TypeORMUserDBImpl);
21
22
async before() {
23
await this.wipeRepo();
24
}
25
26
async after() {
27
await this.wipeRepo();
28
}
29
30
async wipeRepo() {
31
const typeorm = testContainer.get<TypeORM>(TypeORM);
32
await resetDB(typeorm);
33
}
34
35
@test()
36
public async findProjectBySearchTerm() {
37
const user = await this.userDb.newUser();
38
user.identities.push({
39
authProviderId: "GitHub",
40
authId: "1234",
41
authName: "newUser",
42
primaryEmail: "[email protected]",
43
});
44
await this.userDb.storeUser(user);
45
46
const project = Project.create({
47
name: "some-project",
48
cloneUrl: "some-random-clone-url",
49
teamId: "team-1",
50
appInstallationId: "app-1",
51
});
52
const searchTerm = "rand";
53
const storedProject = await this.projectDb.storeProject(project);
54
const foundProject = await this.projectDb.findProjectsBySearchTerm({
55
offset: 0,
56
limit: 10,
57
orderBy: "creationTime",
58
orderDir: "DESC",
59
searchTerm,
60
});
61
62
expect(foundProject.rows[0].id).to.eq(storedProject.id);
63
64
const foundProjectByName = await this.projectDb.findProjectsBySearchTerm({
65
offset: 0,
66
limit: 10,
67
orderBy: "creationTime",
68
orderDir: "DESC",
69
searchTerm: "some-proj",
70
});
71
expect(foundProjectByName.rows[0].id).to.eq(storedProject.id);
72
73
const foundProjectEmptySearch = await this.projectDb.findProjectsBySearchTerm({
74
offset: 0,
75
limit: 10,
76
orderBy: "creationTime",
77
orderDir: "DESC",
78
searchTerm: " ",
79
});
80
expect(foundProjectEmptySearch.rows[0].id).to.eq(storedProject.id);
81
}
82
83
@test()
84
public async findProjectBySearchTermPagniation() {
85
const user = await this.userDb.newUser();
86
user.identities.push({
87
authProviderId: "GitHub",
88
authId: "1234",
89
authName: "newUser",
90
primaryEmail: "[email protected]",
91
});
92
await this.userDb.storeUser(user);
93
94
const project1 = Project.create({
95
name: "some-project",
96
cloneUrl: "some-random-clone-url",
97
teamId: "team-1",
98
appInstallationId: "",
99
});
100
const project2 = Project.create({
101
name: "some-project-2",
102
cloneUrl: "some-random-clone-url-2",
103
teamId: "team-1",
104
appInstallationId: "",
105
});
106
const project3 = Project.create({
107
name: "some-project-3",
108
cloneUrl: "some-random-clone-url-1",
109
teamId: "team-1",
110
appInstallationId: "",
111
});
112
const project4 = Project.create({
113
name: "some-project-4",
114
cloneUrl: "some-random-clone-url-1",
115
teamId: "team-1",
116
appInstallationId: "",
117
});
118
const project5 = Project.create({
119
name: "some-project-5",
120
cloneUrl: "some-random-clone-url-1",
121
teamId: "team-1",
122
appInstallationId: "",
123
});
124
const storedProject1 = await this.projectDb.storeProject(project1);
125
const storedProject2 = await this.projectDb.storeProject(project2);
126
const storedProject3 = await this.projectDb.storeProject(project3);
127
const storedProject4 = await this.projectDb.storeProject(project4);
128
const storedProject5 = await this.projectDb.storeProject(project5);
129
130
const allResults = await this.projectDb.findProjectsBySearchTerm({
131
offset: 0,
132
limit: 10,
133
orderBy: "name",
134
orderDir: "ASC",
135
});
136
expect(allResults.total).equals(5);
137
expect(allResults.rows.length).equal(5);
138
expect(allResults.rows[0].id).to.eq(storedProject1.id);
139
expect(allResults.rows[1].id).to.eq(storedProject2.id);
140
expect(allResults.rows[2].id).to.eq(storedProject3.id);
141
expect(allResults.rows[3].id).to.eq(storedProject4.id);
142
expect(allResults.rows[4].id).to.eq(storedProject5.id);
143
144
const pageSize = 3;
145
const page1 = await this.projectDb.findProjectsBySearchTerm({
146
offset: 0,
147
limit: pageSize,
148
orderBy: "name",
149
orderDir: "ASC",
150
});
151
expect(page1.total).equals(5);
152
expect(page1.rows.length).equal(3);
153
expect(page1.rows[0].id).to.eq(storedProject1.id);
154
expect(page1.rows[1].id).to.eq(storedProject2.id);
155
expect(page1.rows[2].id).to.eq(storedProject3.id);
156
157
const page2 = await this.projectDb.findProjectsBySearchTerm({
158
offset: pageSize * 1,
159
limit: pageSize,
160
orderBy: "name",
161
orderDir: "ASC",
162
});
163
expect(page2.total).equals(5);
164
expect(page2.rows.length).equal(2);
165
expect(page2.rows[0].id).to.eq(storedProject4.id);
166
expect(page2.rows[1].id).to.eq(storedProject5.id);
167
}
168
169
@test()
170
public async findProjectBySearchTermOrganizationId() {
171
const user = await this.userDb.newUser();
172
user.identities.push({
173
authProviderId: "GitHub",
174
authId: "1234",
175
authName: "newUser",
176
primaryEmail: "[email protected]",
177
});
178
await this.userDb.storeUser(user);
179
180
const project1 = Project.create({
181
name: "some-project",
182
cloneUrl: "some-random-clone-url",
183
teamId: "team-1",
184
appInstallationId: "",
185
});
186
const project2 = Project.create({
187
name: "some-project-2",
188
cloneUrl: "some-random-clone-url-2",
189
teamId: "team-2",
190
appInstallationId: "",
191
});
192
const storedProject1 = await this.projectDb.storeProject(project1);
193
const storedProject2 = await this.projectDb.storeProject(project2);
194
195
const team1Results = await this.projectDb.findProjectsBySearchTerm({
196
offset: 0,
197
limit: 10,
198
orderBy: "name",
199
orderDir: "ASC",
200
organizationId: "team-1",
201
});
202
expect(team1Results.total).equals(1);
203
expect(team1Results.rows[0].id).to.eq(storedProject1.id);
204
205
const team2Results = await this.projectDb.findProjectsBySearchTerm({
206
offset: 0,
207
limit: 10,
208
orderBy: "name",
209
orderDir: "ASC",
210
organizationId: "team-2",
211
});
212
expect(team2Results.total).equals(1);
213
expect(team2Results.rows[0].id).to.eq(storedProject2.id);
214
215
const noResults = await this.projectDb.findProjectsBySearchTerm({
216
offset: 0,
217
limit: 10,
218
orderBy: "name",
219
orderDir: "ASC",
220
organizationId: "does-not-exist",
221
});
222
expect(noResults.total).equals(0);
223
}
224
225
@test()
226
public async findProjectBySearchTermPrebuildsEnabled() {
227
const user = await this.userDb.newUser();
228
user.identities.push({
229
authProviderId: "GitHub",
230
authId: "1234",
231
authName: "newUser",
232
primaryEmail: "[email protected]",
233
});
234
await this.userDb.storeUser(user);
235
236
const noPrebuilds = Project.create({
237
name: "no-prebuilds",
238
cloneUrl: "some-random-clone-url",
239
teamId: "team-1",
240
appInstallationId: "",
241
settings: {
242
prebuilds: {
243
enable: false,
244
},
245
},
246
});
247
248
const withPrebuilds = Project.create({
249
name: "with-prebuilds",
250
cloneUrl: "some-random-clone-url-2",
251
teamId: "team-1",
252
appInstallationId: "",
253
settings: {
254
prebuilds: {
255
enable: true,
256
},
257
},
258
});
259
260
const storedNoPrebuilds = await this.projectDb.storeProject(noPrebuilds);
261
const storedWithPrebuilds = await this.projectDb.storeProject(withPrebuilds);
262
263
const noPrebuildsResults = await this.projectDb.findProjectsBySearchTerm({
264
offset: 0,
265
limit: 10,
266
orderBy: "name",
267
orderDir: "ASC",
268
prebuildsEnabled: false,
269
});
270
expect(noPrebuildsResults.total).equals(1);
271
expect(noPrebuildsResults.rows[0].id).to.eq(storedNoPrebuilds.id);
272
273
const withPrebuildsResults = await this.projectDb.findProjectsBySearchTerm({
274
offset: 0,
275
limit: 10,
276
orderBy: "name",
277
orderDir: "ASC",
278
prebuildsEnabled: true,
279
});
280
expect(withPrebuildsResults.total).equals(1);
281
expect(withPrebuildsResults.rows[0].id).to.eq(storedWithPrebuilds.id);
282
283
const noPrebuildsFilterResults = await this.projectDb.findProjectsBySearchTerm({
284
offset: 0,
285
limit: 10,
286
orderBy: "name",
287
orderDir: "ASC",
288
prebuildsEnabled: undefined,
289
});
290
expect(noPrebuildsFilterResults.total).equals(2);
291
}
292
}
293
294
module.exports = new ProjectDBSpec();
295
296