Skip to content

Commit 30197a7

Browse files
steven-supersoliddrew-gross
authored andcommitted
Expose DatabaseAdapter to simplify application tests (#1121)
* Move helper.clearData to DatabaseAdapter. Expose DatabaseAdapter in index * fix indentation * Export DatabaseAdapter in index.js * Rename clearData to destroyAllDataPermanently. Only export destroyAllDataPermanently from DatabaseAdapter. Update helper * Expose wrapped TestUtils from index.js. TestUtils exposed select functions from other modules, only in test environment
1 parent ab6925a commit 30197a7

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

spec/helper.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var express = require('express');
88
var facebook = require('../src/authDataManager/facebook');
99
var ParseServer = require('../src/index').ParseServer;
1010
var path = require('path');
11+
var TestUtils = require('../src/index').TestUtils;
1112

1213
var databaseURI = process.env.DATABASE_URI;
1314
var cloudMain = process.env.CLOUD_CODE_MAIN || './spec/cloud/main.js';
@@ -88,7 +89,7 @@ beforeEach(function(done) {
8889

8990
afterEach(function(done) {
9091
Parse.User.logOut().then(() => {
91-
return clearData();
92+
return TestUtils.destroyAllDataPermanently();
9293
}).then(() => {
9394
done();
9495
}, (error) => {
@@ -232,14 +233,6 @@ function mockFacebook() {
232233
return facebook;
233234
}
234235

235-
function clearData() {
236-
var promises = [];
237-
for (var conn in DatabaseAdapter.dbConnections) {
238-
promises.push(DatabaseAdapter.dbConnections[conn].deleteEverything());
239-
}
240-
return Promise.all(promises);
241-
}
242-
243236
// This is polluting, but, it makes it way easier to directly port old tests.
244237
global.Parse = Parse;
245238
global.TestObject = TestObject;

src/DatabaseAdapter.js

+13
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ function clearDatabaseSettings() {
4949
appDatabaseOptions = {};
5050
}
5151

52+
//Used by tests
53+
function destroyAllDataPermanently() {
54+
if (process.env.TESTING) {
55+
var promises = [];
56+
for (var conn in dbConnections) {
57+
promises.push(dbConnections[conn].deleteEverything());
58+
}
59+
return Promise.all(promises);
60+
}
61+
throw 'Only supported in test environment';
62+
}
63+
5264
function getDatabaseConnection(appId: string, collectionPrefix: string) {
5365
if (dbConnections[appId]) {
5466
return dbConnections[appId];
@@ -71,5 +83,6 @@ module.exports = {
7183
setAppDatabaseOptions: setAppDatabaseOptions,
7284
setAppDatabaseURI: setAppDatabaseURI,
7385
clearDatabaseSettings: clearDatabaseSettings,
86+
destroyAllDataPermanently: destroyAllDataPermanently,
7487
defaultDatabaseURI: databaseURI
7588
};

src/TestUtils.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { destroyAllDataPermanently } from './DatabaseAdapter';
2+
3+
let unsupported = function() {
4+
throw 'Only supported in test environment';
5+
};
6+
7+
let _destroyAllDataPermanently;
8+
if (process.env.TESTING) {
9+
_destroyAllDataPermanently = destroyAllDataPermanently;
10+
} else {
11+
_destroyAllDataPermanently = unsupported;
12+
}
13+
14+
export default {
15+
destroyAllDataPermanently: _destroyAllDataPermanently};

src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import winston from 'winston';
22
import ParseServer from './ParseServer';
33
import S3Adapter from 'parse-server-s3-adapter'
44
import FileSystemAdapter from 'parse-server-fs-adapter'
5+
import TestUtils from './TestUtils';
56
import { useExternal } from './deprecated'
67

78
// Factory function
@@ -15,4 +16,4 @@ _ParseServer.createLiveQueryServer = ParseServer.createLiveQueryServer;
1516
let GCSAdapter = useExternal('GCSAdapter', 'parse-server-gcs-adapter');
1617

1718
export default ParseServer;
18-
export { S3Adapter, GCSAdapter, FileSystemAdapter, _ParseServer as ParseServer };
19+
export { S3Adapter, GCSAdapter, FileSystemAdapter, TestUtils, _ParseServer as ParseServer };

0 commit comments

Comments
 (0)