-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathdbclient-convenience-runtime.test.ts
More file actions
204 lines (159 loc) · 6.5 KB
/
Copy pathdbclient-convenience-runtime.test.ts
File metadata and controls
204 lines (159 loc) · 6.5 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright (c) 2015-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
/// <reference path="../marklogic.d.ts" />
/**
* Runtime validation tests for DatabaseClient convenience methods.
*
* These tests verify that the simplified convenience methods on DatabaseClient
* have correct TypeScript types and work as expected.
*
* Run with: npm run test:compile && npx mocha test-typescript/*.js
*/
const should = require('should');
import type { DatabaseClient } from 'marklogic';
const testConfig = require('../etc/test-config.js');
const marklogic = require('../lib/marklogic.js');
describe('DatabaseClient convenience methods runtime validation', function() {
let client: DatabaseClient;
const testUri = '/test-typescript/convenience-test.json';
const testContent = { message: 'Convenience method test', timestamp: Date.now() };
before(function() {
client = marklogic.createDatabaseClient(testConfig.restWriterConnection);
});
after(function() {
client.release();
});
it('should create collection with createCollection()', async function() {
const uris = await client.createCollection('typescript-convenience-test', testContent).result();
uris.should.be.an.Array();
uris.length.should.equal(1);
uris[0].should.be.a.String();
});
it('should probe document with probe()', async function() {
// Write a test document first
await client.documents.write({
uri: testUri,
content: testContent,
collections: ['typescript-convenience-test']
}).result();
const exists = await client.probe(testUri).result();
exists.should.be.a.Boolean();
exists.should.equal(true);
});
it('should read document with read()', async function() {
const contents = await client.read(testUri).result();
contents.should.be.an.Array();
contents.length.should.equal(1);
contents[0].should.have.property('message');
});
it('should query collection with queryCollection()', async function() {
const results = await client.queryCollection('typescript-convenience-test').result();
results.should.be.an.Array();
results.length.should.be.greaterThan(0);
});
it('should write collection with writeCollection()', async function() {
const uriMap = {
'/test-typescript/conv-1.json': { id: 1, name: 'Test 1' },
'/test-typescript/conv-2.json': { id: 2, name: 'Test 2' }
};
const uris = await client.writeCollection('typescript-convenience-test', uriMap).result();
uris.should.be.an.Array();
uris.length.should.equal(2);
});
it('should remove document with remove()', async function() {
const uris = await client.remove('/test-typescript/conv-1.json', '/test-typescript/conv-2.json').result();
uris.should.be.an.Array();
uris.length.should.equal(2);
});
it('should remove collection with removeCollection()', async function() {
const result = await client.removeCollection('typescript-convenience-test').result();
result.should.be.a.String();
result.should.equal('typescript-convenience-test');
});
it('should create timestamp with createTimestamp()', function() {
const ts1 = client.createTimestamp();
ts1.should.have.property('value');
const ts2 = client.createTimestamp('2025-11-25T12:00:00Z');
ts2.should.have.property('value');
if (ts2.value !== null) {
ts2.value.should.equal('2025-11-25T12:00:00Z');
}
});
it('should evaluate JavaScript with eval()', async function() {
const results = await client.eval('xdmp.toJSON({message: "Hello from eval"})').result();
results.should.be.an.Array();
results.length.should.equal(1);
results[0].should.have.property('format');
results[0].should.have.property('datatype');
results[0].should.have.property('value');
results[0].format.should.equal('json');
results[0].value.should.have.property('message');
results[0].value.message.should.equal('Hello from eval');
});
it('should evaluate JavaScript with variables', async function() {
const results = await client.eval(
'var x; xdmp.toJSON({result: x * 2})',
{ x: 21 }
).result();
results.should.be.an.Array();
results[0].value.result.should.equal(42);
});
it('should evaluate XQuery with xqueryEval()', async function() {
const results = await client.xqueryEval('"Hello from XQuery"').result();
results.should.be.an.Array();
results.length.should.equal(1);
results[0].format.should.equal('text');
results[0].datatype.should.equal('string');
results[0].value.should.equal('Hello from XQuery');
});
it('should evaluate XQuery with variables', async function() {
const results = await client.xqueryEval(
'declare variable $x as xs:integer external; $x * 3',
{ x: 14 }
).result();
results.should.be.an.Array();
results[0].value.should.equal(42);
});
it('should invoke a module with invoke()', async function() {
const results = await client.invoke('/hello.xqy').result();
results.should.be.an.Array();
results.length.should.equal(1);
results[0].should.have.property('format');
results[0].should.have.property('value');
results[0].value.should.be.a.String();
});
it('should use setLogger with a level string', async function() {
// Set logger to 'info' level
client.setLogger('info');
// Write a test document
await client.documents.write({
uri: '/test-typescript/setlogger-test.json',
content: { test: 'setLogger' }
}).result();
// Verify client is still functional after setting logger
const exists = await client.probe('/test-typescript/setlogger-test.json').result();
exists.should.be.a.Boolean();
exists.should.equal(true);
});
it('should use setLogger with a logger object', async function() {
// Create a simple logger object
const testLogger = {
debug: (msg: string) => console.log('DEBUG:', msg),
info: (msg: string) => console.log('INFO:', msg),
warn: (msg: string) => console.log('WARN:', msg),
error: (msg: string) => console.log('ERROR:', msg)
};
// Set logger with object
client.setLogger(testLogger, false);
// Write a test document
await client.documents.write({
uri: '/test-typescript/setlogger-test2.json',
content: { test: 'setLogger with object' }
}).result();
// Verify client is still functional after setting logger
const exists = await client.probe('/test-typescript/setlogger-test2.json').result();
exists.should.be.a.Boolean();
exists.should.equal(true);
});
});