-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathcheckConnection-runtime.test.ts
More file actions
97 lines (78 loc) · 3.57 KB
/
checkConnection-runtime.test.ts
File metadata and controls
97 lines (78 loc) · 3.57 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
/*
* Copyright (c) 2015-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
/// <reference path="../marklogic.d.ts" />
/**
* TypeScript runtime test to validate that checkConnection returns ResultProvider.
* This test ensures the TypeScript definitions match the actual runtime behavior
* by making real calls to MarkLogic AND verifying types at compile time.
*/
import should = require('should');
const marklogic = require('..');
const testconfig = require('../etc/test-config-qa.js');
const db = marklogic.createDatabaseClient(testconfig.restReaderConnection);
// Type alias for easier reference
type ResultProvider<T> = import('marklogic').ResultProvider<T>;
type ConnectionCheckResult = import('marklogic').ConnectionCheckResult;
type DatabaseClientConfig = import('marklogic').DatabaseClientConfig;
describe('checkConnection ResultProvider validation', function() {
it('should return ResultProvider with .result() method', function(done) {
// This validates that checkConnection returns a ResultProvider
// TypeScript will verify the type at compile time
const resultProvider: ResultProvider<ConnectionCheckResult> = db.checkConnection();
// Verify it has a .result() method (core requirement for ResultProvider)
should(resultProvider).have.property('result');
should(resultProvider.result).be.a.Function();
// Call .result() to get a Promise
const promise: Promise<ConnectionCheckResult> = resultProvider.result();
// Verify .result() returns a Promise (thenable)
should(promise).have.property('then');
should(promise.then).be.a.Function();
// Verify the Promise resolves to ConnectionCheckResult
promise.then((response: ConnectionCheckResult) => {
should(response).have.property('connected');
should(response.connected).be.a.Boolean();
if (response.connected === true) {
done();
} else {
done(new Error('Expected connection to succeed but got: ' + JSON.stringify(response)));
}
}).catch(done);
});
it('should work with async/await pattern', async function() {
// TypeScript verifies the return type matches ConnectionCheckResult
const result: ConnectionCheckResult = await db.checkConnection().result();
// Verify result shape matches ConnectionCheckResult
should(result).have.property('connected');
should(result.connected).be.a.Boolean();
should(result.connected).equal(true);
});
it('should have error properties when connection fails', function(done) {
// Test with wrong password to get a failed connection
const config: DatabaseClientConfig = {
host: testconfig.restReaderConnection.host,
user: testconfig.restReaderConnection.user,
password: 'wrongpassword', // Invalid password
port: testconfig.restReaderConnection.port,
authType: testconfig.restReaderConnection.authType
};
const db1 = marklogic.createDatabaseClient(config);
db1.checkConnection().result().then((response: ConnectionCheckResult) => {
should(response).have.property('connected');
should(response.connected).be.a.Boolean();
if (response.connected === false) {
// When connected is false, optional error properties should exist
should(response).have.property('httpStatusCode');
should(response.httpStatusCode).be.a.Number();
should(response).have.property('httpStatusMessage');
should(response.httpStatusMessage).be.a.String();
}
db1.release();
done();
}).catch(done);
});
after(function(done) {
db.release();
done();
});
});