Skip to content

Commit 14f90ef

Browse files
committed
test: jest.fn
1 parent ee9925b commit 14f90ef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+3840
-23
lines changed

packages/datasource-customizer/test/plugins/import-field.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('importField', () => {
4242
const { dataSource } = setupMocks();
4343
const dataSourceCustomizer = new DataSourceCustomizer({
4444
ignoreMissingSchemaElementErrors: false,
45-
restartAgentFunction: async () => {},
45+
restartAgentFunction: jest.fn(),
4646
});
4747
dataSourceCustomizer.addDataSource(async () => dataSource);
4848

packages/datasource-mongo/test/index.integration.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,7 @@ describe('Datasource Mongo', () => {
148148
},
149149
});
150150

151-
const dataSource = await factory(
152-
() => {},
153-
async () => {},
154-
);
151+
const dataSource = await factory(jest.fn(), jest.fn());
155152

156153
expect(dataSource.collections).toHaveLength(2);
157154
expect(dataSource.collections.map(c => c.name)).toEqual(

packages/datasource-mongoose/test/index.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ describe('exports', () => {
1212

1313
it('createMongooseDataSource should return a datasource factory', async () => {
1414
const factory = createMongooseDataSource(mongoose.connection, {});
15-
const dataSource = await factory(
16-
() => {},
17-
async () => {},
18-
);
15+
const dataSource = await factory(jest.fn(), jest.fn());
1916

2017
expect(factory).toBeInstanceOf(Function);
2118
expect(dataSource).toBeInstanceOf(MongooseDatasource);

packages/datasource-replica/test/integrations/factories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ export const makeReplicaDataSource = async (
2525
): Promise<DataSource> => {
2626
const replicaFactory = createReplicaDataSource(options ?? {});
2727

28-
return replicaFactory(logger ?? makeLogger(), async () => {});
28+
return replicaFactory(logger ?? makeLogger(), jest.fn());
2929
};

packages/datasource-sequelize/test/integration/aggregation/count.integration.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ describe('Count', () => {
5252
{ name: null },
5353
]);
5454

55-
const datasource = await createSequelizeDataSource(sequelize)(
56-
undefined as any,
57-
async () => {},
58-
);
55+
const datasource = await createSequelizeDataSource(sequelize)(jest.fn(), jest.fn());
5956
collection = datasource.getCollection('User') as SequelizeCollection;
6057
}, 30_000);
6158

packages/datasource-sequelize/test/integration/list/filter.integration.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,10 +442,7 @@ describe('Filter tests on collection', () => {
442442
{ tags: null },
443443
]);
444444

445-
const datasource = await createSequelizeDataSource(sequelize)(
446-
undefined as any,
447-
async () => {},
448-
);
445+
const datasource = await createSequelizeDataSource(sequelize)(jest.fn(), jest.fn());
449446
collection = datasource.getCollection('Objects') as SequelizeCollection;
450447
});
451448

packages/datasource-sql/src/connection/connection-options.js

Lines changed: 375 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
var __generator = (this && this.__generator) || function (thisArg, body) {
12+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14+
function verb(n) { return function (v) { return step([n, v]); }; }
15+
function step(op) {
16+
if (f) throw new TypeError("Generator is already executing.");
17+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
18+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19+
if (y = 0, t) op = [op[0] & 2, t.value];
20+
switch (op[0]) {
21+
case 0: case 1: t = op; break;
22+
case 4: _.label++; return { value: op[1], done: false };
23+
case 5: _.label++; y = op[1]; op = [0]; continue;
24+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
25+
default:
26+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30+
if (t[2]) _.ops.pop();
31+
_.trys.pop(); continue;
32+
}
33+
op = body.call(thisArg, _);
34+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36+
}
37+
};
38+
exports.__esModule = true;
39+
var errors_1 = require("./errors");
40+
/** Test connection. If this doesn't resolve after 10s, throw a timeout error */
41+
function testConnectionWithTimeOut(sequelize, databaseUri, timeout) {
42+
var _a;
43+
if (timeout === void 0) { timeout = 10000; }
44+
return __awaiter(this, void 0, void 0, function () {
45+
var timeoutId, timeoutPromise;
46+
return __generator(this, function (_b) {
47+
switch (_b.label) {
48+
case 0:
49+
timeoutPromise = new Promise(function (resolve, reject) {
50+
timeoutId = setTimeout(function () {
51+
reject(new errors_1.DatabaseConnectError('Connection to database timed out', databaseUri));
52+
}, timeout);
53+
});
54+
return [4 /*yield*/, Promise.race([
55+
(_a = sequelize.authenticate()) === null || _a === void 0 ? void 0 : _a["finally"](function () {
56+
clearTimeout(timeoutId);
57+
}),
58+
timeoutPromise,
59+
])];
60+
case 1:
61+
_b.sent();
62+
return [2 /*return*/];
63+
}
64+
});
65+
});
66+
}
67+
exports["default"] = testConnectionWithTimeOut;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"use strict";
2+
var __extends = (this && this.__extends) || (function () {
3+
var extendStatics = function (d, b) {
4+
extendStatics = Object.setPrototypeOf ||
5+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7+
return extendStatics(d, b);
8+
};
9+
return function (d, b) {
10+
if (typeof b !== "function" && b !== null)
11+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12+
extendStatics(d, b);
13+
function __() { this.constructor = d; }
14+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15+
};
16+
})();
17+
exports.__esModule = true;
18+
exports.SshForwardError = exports.SshConnectError = exports.ProxyForwardError = exports.ProxyConnectError = exports.DatabaseConnectError = void 0;
19+
/* eslint-disable max-classes-per-file */
20+
var datasource_toolkit_1 = require("@forestadmin/datasource-toolkit");
21+
var BaseError = /** @class */ (function (_super) {
22+
__extends(BaseError, _super);
23+
function BaseError(message, debugUri, details) {
24+
var _this = _super.call(this, details ? "".concat(message, "\n").concat(details) : message) || this;
25+
_this.details = details;
26+
_this.debugUri = debugUri;
27+
return _this;
28+
}
29+
return BaseError;
30+
}(datasource_toolkit_1.BusinessError));
31+
var DatabaseConnectError = /** @class */ (function (_super) {
32+
__extends(DatabaseConnectError, _super);
33+
function DatabaseConnectError(message, debugDatabaseUri, source) {
34+
if (source === void 0) { source = 'Database'; }
35+
var _this = this;
36+
// remove tcp protocol because its not added by the user
37+
var sanitizedUri = debugDatabaseUri.replace('tcp://', '');
38+
_this = _super.call(this, "Unable to connect to the given uri: ".concat(sanitizedUri, "."), sanitizedUri, message) || this;
39+
_this.source = source;
40+
return _this;
41+
}
42+
return DatabaseConnectError;
43+
}(BaseError));
44+
exports.DatabaseConnectError = DatabaseConnectError;
45+
var ProxyConnectError = /** @class */ (function (_super) {
46+
__extends(ProxyConnectError, _super);
47+
function ProxyConnectError(message, debugProxyUri) {
48+
var _this = _super.call(this, "Your proxy has encountered an error. Unable to connect to the given uri: ".concat(debugProxyUri, "."), debugProxyUri, message) || this;
49+
_this.source = 'Proxy';
50+
return _this;
51+
}
52+
return ProxyConnectError;
53+
}(BaseError));
54+
exports.ProxyConnectError = ProxyConnectError;
55+
var ProxyForwardError = /** @class */ (function (_super) {
56+
__extends(ProxyForwardError, _super);
57+
function ProxyForwardError(message, debugForwardedUri) {
58+
var _this = _super.call(this, "Your proxy forwarded connection has encountered an error." +
59+
"Unable to connect to the given uri: ".concat(debugForwardedUri, "."), debugForwardedUri, message) || this;
60+
_this.source = 'Proxy';
61+
return _this;
62+
}
63+
return ProxyForwardError;
64+
}(BaseError));
65+
exports.ProxyForwardError = ProxyForwardError;
66+
var SshConnectError = /** @class */ (function (_super) {
67+
__extends(SshConnectError, _super);
68+
function SshConnectError(message, debugSshUri, source) {
69+
if (source === void 0) { source = 'Ssh'; }
70+
var _this = _super.call(this, "Your ssh connection has encountered an error. " +
71+
"Unable to connect to the given ssh uri: ".concat(debugSshUri), debugSshUri, message) || this;
72+
_this.source = source;
73+
return _this;
74+
}
75+
return SshConnectError;
76+
}(BaseError));
77+
exports.SshConnectError = SshConnectError;
78+
var SshForwardError = /** @class */ (function (_super) {
79+
__extends(SshForwardError, _super);
80+
function SshForwardError(message, debugForwardedUri) {
81+
var _this = _super.call(this, "Your ssh forward connection has encountered an error. " +
82+
"Unable to connect to the given ssh uri: ".concat(debugForwardedUri), debugForwardedUri, message) || this;
83+
_this.source = 'Ssh';
84+
return _this;
85+
}
86+
return SshForwardError;
87+
}(BaseError));
88+
exports.SshForwardError = SshForwardError;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
var sequelize_1 = require("sequelize");
4+
var errors_1 = require("./errors");
5+
function handleErrors(error, options) {
6+
if (error instanceof errors_1.ProxyForwardError) {
7+
// means that the ssh is not reachable
8+
if (options.sshOptions) {
9+
throw new errors_1.SshConnectError(null, error.debugUri, 'Proxy');
10+
}
11+
else {
12+
// if there is no sshOptions, then the database is the destination and
13+
// it means that the database is not reachable
14+
throw new errors_1.DatabaseConnectError(null, error.debugUri, 'Proxy');
15+
}
16+
}
17+
// it means that the database is not reachable
18+
if (error instanceof errors_1.SshForwardError) {
19+
throw new errors_1.DatabaseConnectError(null, error.debugUri, 'Ssh');
20+
}
21+
if (error instanceof sequelize_1.BaseError) {
22+
var nameWithoutSequelize = error.name.replace('Sequelize', '');
23+
var nameWithSpaces = nameWithoutSequelize.replace(/([a-z])([A-Z])/g, function (_, m1, m2) { return "".concat(m1, " ").concat(m2.toLowerCase()); });
24+
throw new errors_1.DatabaseConnectError("".concat(nameWithSpaces, ": ").concat(error.message), options.debugDatabaseUri);
25+
}
26+
throw error;
27+
}
28+
exports["default"] = handleErrors;

0 commit comments

Comments
 (0)