Skip to content

Commit 29439ca

Browse files
committed
fix postgres tests
1 parent 32f118c commit 29439ca

File tree

5 files changed

+86
-77
lines changed

5 files changed

+86
-77
lines changed

spec/ParseServer.spec.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,23 @@ describe('Server Url Checks', () => {
3131

3232
it('validate good server url', done => {
3333
Parse.serverURL = 'http://localhost:13376';
34-
ParseServer.verifyServerUrl(function (result) {
34+
ParseServer.verifyServerUrl(async result => {
3535
if (!result) {
3636
done.fail('Did not pass valid url');
3737
}
38-
Parse.serverURL = 'http://localhost:8378/1';
38+
await reconfigureServer();
3939
done();
4040
});
4141
});
4242

4343
it('mark bad server url', done => {
4444
spyOn(console, 'warn').and.callFake(() => {});
4545
Parse.serverURL = 'notavalidurl';
46-
ParseServer.verifyServerUrl(function (result) {
46+
ParseServer.verifyServerUrl(async result => {
4747
if (result) {
4848
done.fail('Did not mark invalid url');
4949
}
50-
Parse.serverURL = 'http://localhost:8378/1';
50+
await reconfigureServer();
5151
done();
5252
});
5353
});
@@ -105,10 +105,11 @@ describe('Server Url Checks', () => {
105105
parseServerProcess.stderr.on('data', data => {
106106
stderr = data.toString();
107107
});
108-
parseServerProcess.on('close', code => {
108+
parseServerProcess.on('close', async code => {
109109
expect(code).toEqual(1);
110110
expect(stdout).toBeUndefined();
111111
expect(stderr).toContain('MongoServerSelectionError');
112+
await reconfigureServer();
112113
done();
113114
});
114115
});

spec/PostgresInitOptions.spec.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ describe_only_db('postgres')('Postgres database init options', () => {
5252

5353
afterAll(done => {
5454
if (server) {
55+
Parse.serverURL = 'http://localhost:8378/1';
5556
server.close(done);
5657
}
5758
});
@@ -73,7 +74,10 @@ describe_only_db('postgres')('Postgres database init options', () => {
7374
});
7475
return score.save();
7576
})
76-
.then(done, done.fail);
77+
.then(async () => {
78+
await reconfigureServer();
79+
done();
80+
}, done.fail);
7781
});
7882

7983
it('should fail to create server if schema databaseOptions does not exist', done => {
@@ -83,6 +87,9 @@ describe_only_db('postgres')('Postgres database init options', () => {
8387
databaseOptions: databaseOptions2,
8488
});
8589

86-
createParseServer({ databaseAdapter: adapter }).then(done.fail, () => done());
90+
createParseServer({ databaseAdapter: adapter }).then(done.fail, async () => {
91+
await reconfigureServer();
92+
done();
93+
});
8794
});
8895
});

spec/PostgresStorageAdapter.spec.js

Lines changed: 62 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ describe_only_db('postgres')('PostgresStorageAdapter', () => {
154154
objectId: { type: 'String' },
155155
username: { type: 'String' },
156156
email: { type: 'String' },
157+
emailVerified: { type: 'Boolean' },
158+
createdAt: { type: 'Date' },
159+
updatedAt: { type: 'Date' },
160+
authData: { type: 'Object' },
157161
},
158162
};
159163
const client = adapter._client;
@@ -173,77 +177,66 @@ describe_only_db('postgres')('PostgresStorageAdapter', () => {
173177
const caseInsensitiveData = 'bugs';
174178
const originalQuery = 'SELECT * FROM $1:name WHERE lower($2:name)=lower($3)';
175179
const analyzedExplainQuery = adapter.createExplainableQuery(originalQuery, true);
176-
await client
177-
.one(analyzedExplainQuery, [tableName, 'objectId', caseInsensitiveData])
178-
.then(explained => {
179-
const preIndexPlan = explained;
180-
181-
preIndexPlan['QUERY PLAN'].forEach(element => {
182-
//Make sure search returned with only 1 result
183-
expect(element.Plan['Actual Rows']).toBe(1);
184-
expect(element.Plan['Node Type']).toBe('Seq Scan');
185-
});
186-
const indexName = 'test_case_insensitive_column';
187-
188-
adapter.ensureIndex(tableName, schema, ['objectId'], indexName, true).then(() => {
189-
client
190-
.one(analyzedExplainQuery, [tableName, 'objectId', caseInsensitiveData])
191-
.then(explained => {
192-
const postIndexPlan = explained;
193-
194-
postIndexPlan['QUERY PLAN'].forEach(element => {
195-
//Make sure search returned with only 1 result
196-
expect(element.Plan['Actual Rows']).toBe(1);
197-
//Should not be a sequential scan
198-
expect(element.Plan['Node Type']).not.toContain('Seq Scan');
199-
200-
//Should be using the index created for this
201-
element.Plan.Plans.forEach(innerElement => {
202-
expect(innerElement['Index Name']).toBe(indexName);
203-
});
204-
});
205-
206-
//These are the same query so should be the same size
207-
for (let i = 0; i < preIndexPlan['QUERY PLAN'].length; i++) {
208-
//Sequential should take more time to execute than indexed
209-
expect(preIndexPlan['QUERY PLAN'][i]['Execution Time']).toBeGreaterThan(
210-
postIndexPlan['QUERY PLAN'][i]['Execution Time']
211-
);
212-
}
213-
214-
//Test explaining without analyzing
215-
const basicExplainQuery = adapter.createExplainableQuery(originalQuery);
216-
client
217-
.one(basicExplainQuery, [tableName, 'objectId', caseInsensitiveData])
218-
.then(explained => {
219-
explained['QUERY PLAN'].forEach(element => {
220-
//Check that basic query plans isn't a sequential scan
221-
expect(element.Plan['Node Type']).not.toContain('Seq Scan');
222-
223-
//Basic query plans shouldn't have an execution time
224-
expect(element['Execution Time']).toBeUndefined();
225-
});
226-
});
227-
});
228-
});
229-
})
230-
.catch(error => {
231-
// Query on non existing table, don't crash
232-
if (error.code !== '42P01') {
233-
throw error;
234-
}
235-
return [];
180+
const preIndexPlan = await client.one(analyzedExplainQuery, [
181+
tableName,
182+
'objectId',
183+
caseInsensitiveData,
184+
]);
185+
preIndexPlan['QUERY PLAN'].forEach(element => {
186+
//Make sure search returned with only 1 result
187+
expect(element.Plan['Actual Rows']).toBe(1);
188+
expect(element.Plan['Node Type']).toBe('Seq Scan');
189+
});
190+
const indexName = 'test_case_insensitive_column';
191+
await adapter.ensureIndex(tableName, schema, ['objectId'], indexName, true);
192+
193+
const postIndexPlan = await client.one(analyzedExplainQuery, [
194+
tableName,
195+
'objectId',
196+
caseInsensitiveData,
197+
]);
198+
postIndexPlan['QUERY PLAN'].forEach(element => {
199+
//Make sure search returned with only 1 result
200+
expect(element.Plan['Actual Rows']).toBe(1);
201+
//Should not be a sequential scan
202+
expect(element.Plan['Node Type']).not.toContain('Seq Scan');
203+
204+
//Should be using the index created for this
205+
element.Plan.Plans.forEach(innerElement => {
206+
expect(innerElement['Index Name']).toBe(indexName);
236207
});
208+
});
209+
210+
//These are the same query so should be the same size
211+
for (let i = 0; i < preIndexPlan['QUERY PLAN'].length; i++) {
212+
//Sequential should take more time to execute than indexed
213+
expect(preIndexPlan['QUERY PLAN'][i]['Execution Time']).toBeGreaterThan(
214+
postIndexPlan['QUERY PLAN'][i]['Execution Time']
215+
);
216+
}
217+
//Test explaining without analyzing
218+
const basicExplainQuery = adapter.createExplainableQuery(originalQuery);
219+
const explained = await client.one(basicExplainQuery, [
220+
tableName,
221+
'objectId',
222+
caseInsensitiveData,
223+
]);
224+
explained['QUERY PLAN'].forEach(element => {
225+
//Check that basic query plans isn't a sequential scan
226+
expect(element.Plan['Node Type']).not.toContain('Seq Scan');
227+
228+
//Basic query plans shouldn't have an execution time
229+
expect(element['Execution Time']).toBeUndefined();
230+
});
231+
await dropTable(client, tableName);
237232
});
238233

239234
it('should use index for caseInsensitive query', async () => {
240235
const tableName = '_User';
241-
await adapter.deleteClass(tableName);
242-
await reconfigureServer();
243236

244237
const user = new Parse.User();
245-
user.set('username', 'Bugs');
246-
user.set('password', 'Bunny');
238+
user.set('username', 'Elmer');
239+
user.set('password', 'Fudd');
247240
await user.signUp();
248241
const database = Config.get(Parse.applicationId).database;
249242

@@ -253,7 +246,7 @@ describe_only_db('postgres')('PostgresStorageAdapter', () => {
253246
'INSERT INTO $1:name ($2:name, $3:name) SELECT gen_random_uuid(), gen_random_uuid() FROM generate_series(1,5000)',
254247
[tableName, 'objectId', 'username']
255248
);
256-
const caseInsensitiveData = 'bugs';
249+
const caseInsensitiveData = 'elmer';
257250
const fieldToSearch = 'username';
258251
//Check using find method for Parse
259252
const preIndexPlan = await database.find(
@@ -265,7 +258,7 @@ describe_only_db('postgres')('PostgresStorageAdapter', () => {
265258
preIndexPlan.forEach(element => {
266259
element['QUERY PLAN'].forEach(innerElement => {
267260
//Check that basic query plans isn't a sequential scan, be careful as find uses "any" to query
268-
expect(innerElement.Plan['Node Type']).toBe('Bitmap Heap Scan');
261+
expect(innerElement.Plan['Node Type']).toBe('Seq Scan');
269262
//Basic query plans shouldn't have an execution time
270263
expect(innerElement['Execution Time']).toBeUndefined();
271264
});
@@ -296,8 +289,8 @@ describe_only_db('postgres')('PostgresStorageAdapter', () => {
296289
it('should use index for caseInsensitive query using default indexname', async () => {
297290
const tableName = '_User';
298291
const user = new Parse.User();
299-
user.set('username', 'Bugs');
300-
user.set('password', 'Bunny');
292+
user.set('username', 'Tweety');
293+
user.set('password', 'Bird');
301294
await user.signUp();
302295
const database = Config.get(Parse.applicationId).database;
303296
const fieldToSearch = 'username';
@@ -312,7 +305,7 @@ describe_only_db('postgres')('PostgresStorageAdapter', () => {
312305
[tableName, 'objectId', 'username']
313306
);
314307

315-
const caseInsensitiveData = 'buGs';
308+
const caseInsensitiveData = 'tweeTy';
316309
//Check using find method for Parse
317310
const indexPlan = await database.find(
318311
tableName,

spec/RestQuery.spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ describe('rest query', () => {
315315
});
316316

317317
describe('RestQuery.each', () => {
318+
beforeEach(() => {
319+
config = Config.get('test');
320+
});
318321
it('should run each', async () => {
319322
const objects = [];
320323
while (objects.length != 10) {

src/Adapters/Storage/Postgres/PostgresClient.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ export function createClient(uri, databaseOptions) {
2020

2121
if (process.env.PARSE_SERVER_LOG_LEVEL === 'debug') {
2222
const monitor = require('pg-monitor');
23-
monitor.attach(initOptions);
23+
try {
24+
monitor.attach(initOptions);
25+
} catch (e) {
26+
monitor.detach();
27+
monitor.attach(initOptions);
28+
}
2429
}
2530

2631
if (dbOptions.pgOptions) {

0 commit comments

Comments
 (0)