|
| 1 | +/* |
| 2 | +* Copyright (c) 2015-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. |
| 3 | +*/ |
| 4 | + |
| 5 | +/// <reference path="../marklogic.d.ts" /> |
| 6 | + |
| 7 | +/** |
| 8 | + * Runtime validation tests for DatabaseClient convenience methods. |
| 9 | + * |
| 10 | + * These tests verify that the simplified convenience methods on DatabaseClient |
| 11 | + * have correct TypeScript types and work as expected. |
| 12 | + * |
| 13 | + * Run with: npm run test:compile && npx mocha test-typescript/*.js |
| 14 | + */ |
| 15 | + |
| 16 | +const should = require('should'); |
| 17 | +import type { DatabaseClient } from 'marklogic'; |
| 18 | + |
| 19 | +const testConfig = require('../etc/test-config.js'); |
| 20 | +const marklogic = require('../lib/marklogic.js'); |
| 21 | + |
| 22 | +describe('DatabaseClient convenience methods runtime validation', function() { |
| 23 | + let client: DatabaseClient; |
| 24 | + const testUri = '/test-typescript/convenience-test.json'; |
| 25 | + const testContent = { message: 'Convenience method test', timestamp: Date.now() }; |
| 26 | + |
| 27 | + before(function() { |
| 28 | + client = marklogic.createDatabaseClient(testConfig.restWriterConnection); |
| 29 | + }); |
| 30 | + |
| 31 | + after(function() { |
| 32 | + client.release(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should create collection with createCollection()', async function() { |
| 36 | + const uris = await client.createCollection('typescript-convenience-test', testContent).result(); |
| 37 | + |
| 38 | + uris.should.be.an.Array(); |
| 39 | + uris.length.should.equal(1); |
| 40 | + uris[0].should.be.a.String(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should probe document with probe()', async function() { |
| 44 | + // Write a test document first |
| 45 | + await client.documents.write({ |
| 46 | + uri: testUri, |
| 47 | + content: testContent, |
| 48 | + collections: ['typescript-convenience-test'] |
| 49 | + }).result(); |
| 50 | + |
| 51 | + const exists = await client.probe(testUri).result(); |
| 52 | + |
| 53 | + exists.should.be.a.Boolean(); |
| 54 | + exists.should.equal(true); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should read document with read()', async function() { |
| 58 | + const contents = await client.read(testUri).result(); |
| 59 | + |
| 60 | + contents.should.be.an.Array(); |
| 61 | + contents.length.should.equal(1); |
| 62 | + contents[0].should.have.property('message'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should query collection with queryCollection()', async function() { |
| 66 | + const results = await client.queryCollection('typescript-convenience-test').result(); |
| 67 | + |
| 68 | + results.should.be.an.Array(); |
| 69 | + results.length.should.be.greaterThan(0); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should write collection with writeCollection()', async function() { |
| 73 | + const uriMap = { |
| 74 | + '/test-typescript/conv-1.json': { id: 1, name: 'Test 1' }, |
| 75 | + '/test-typescript/conv-2.json': { id: 2, name: 'Test 2' } |
| 76 | + }; |
| 77 | + |
| 78 | + const uris = await client.writeCollection('typescript-convenience-test', uriMap).result(); |
| 79 | + |
| 80 | + uris.should.be.an.Array(); |
| 81 | + uris.length.should.equal(2); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should remove document with remove()', async function() { |
| 85 | + const uris = await client.remove('/test-typescript/conv-1.json', '/test-typescript/conv-2.json').result(); |
| 86 | + |
| 87 | + uris.should.be.an.Array(); |
| 88 | + uris.length.should.equal(2); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should remove collection with removeCollection()', async function() { |
| 92 | + const result = await client.removeCollection('typescript-convenience-test').result(); |
| 93 | + |
| 94 | + result.should.be.a.String(); |
| 95 | + result.should.equal('typescript-convenience-test'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should create timestamp with createTimestamp()', function() { |
| 99 | + const ts1 = client.createTimestamp(); |
| 100 | + ts1.should.have.property('value'); |
| 101 | + |
| 102 | + const ts2 = client.createTimestamp('2025-11-25T12:00:00Z'); |
| 103 | + ts2.should.have.property('value'); |
| 104 | + if (ts2.value !== null) { |
| 105 | + ts2.value.should.equal('2025-11-25T12:00:00Z'); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + it('should evaluate JavaScript with eval()', async function() { |
| 110 | + const results = await client.eval('xdmp.toJSON({message: "Hello from eval"})').result(); |
| 111 | + |
| 112 | + results.should.be.an.Array(); |
| 113 | + results.length.should.equal(1); |
| 114 | + results[0].should.have.property('format'); |
| 115 | + results[0].should.have.property('datatype'); |
| 116 | + results[0].should.have.property('value'); |
| 117 | + results[0].format.should.equal('json'); |
| 118 | + results[0].value.should.have.property('message'); |
| 119 | + results[0].value.message.should.equal('Hello from eval'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should evaluate JavaScript with variables', async function() { |
| 123 | + const results = await client.eval( |
| 124 | + 'var x; xdmp.toJSON({result: x * 2})', |
| 125 | + { x: 21 } |
| 126 | + ).result(); |
| 127 | + |
| 128 | + results.should.be.an.Array(); |
| 129 | + results[0].value.result.should.equal(42); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should evaluate XQuery with xqueryEval()', async function() { |
| 133 | + const results = await client.xqueryEval('"Hello from XQuery"').result(); |
| 134 | + |
| 135 | + results.should.be.an.Array(); |
| 136 | + results.length.should.equal(1); |
| 137 | + results[0].format.should.equal('text'); |
| 138 | + results[0].datatype.should.equal('string'); |
| 139 | + results[0].value.should.equal('Hello from XQuery'); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should evaluate XQuery with variables', async function() { |
| 143 | + const results = await client.xqueryEval( |
| 144 | + 'declare variable $x as xs:integer external; $x * 3', |
| 145 | + { x: 14 } |
| 146 | + ).result(); |
| 147 | + |
| 148 | + results.should.be.an.Array(); |
| 149 | + results[0].value.should.equal(42); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should invoke a module with invoke()', async function() { |
| 153 | + const results = await client.invoke('/hello.xqy').result(); |
| 154 | + |
| 155 | + results.should.be.an.Array(); |
| 156 | + results.length.should.equal(1); |
| 157 | + results[0].should.have.property('format'); |
| 158 | + results[0].should.have.property('value'); |
| 159 | + results[0].value.should.be.a.String(); |
| 160 | + }); |
| 161 | +}); |
0 commit comments