Skip to content

Commit 76ba6f2

Browse files
authored
Merge pull request #1005 from stevebio/feature/MLE-24755-binary-doc-test-fixes
MLE-24755 binary doc test fixes
2 parents ff2f974 + 1d50e90 commit 76ba6f2

File tree

2 files changed

+121
-135
lines changed

2 files changed

+121
-135
lines changed

test-complete/nodejs-documents-binary-pdf.js

Lines changed: 54 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,72 @@
11
/*
22
* Copyright (c) 2015-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
33
*/
4-
var should = require('should');
4+
const should = require('should');
55

6-
var fs = require('fs');
7-
var stream = require('stream');
8-
var util = require('util');
6+
const fs = require('fs');
7+
const stream = require('stream');
8+
const util = require('util');
99

10-
var concatStream = require('concat-stream');
11-
var valcheck = require('core-util-is');
12-
var testconfig = require('../etc/test-config-qa.js');
10+
const concatStream = require('concat-stream');
11+
const valcheck = require('core-util-is');
12+
const testconfig = require('../etc/test-config-qa.js');
1313

14-
var marklogic = require('../');
15-
var q = marklogic.queryBuilder;
14+
const marklogic = require('../');
15+
const q = marklogic.queryBuilder;
16+
17+
const dbReader = marklogic.createDatabaseClient(testconfig.restReaderConnection);
18+
const dbWriter = marklogic.createDatabaseClient(testconfig.restWriterConnection);
19+
const dbAdmin = marklogic.createDatabaseClient(testconfig.restAdminConnection);
1620

17-
var dbReader = marklogic.createDatabaseClient(testconfig.restReaderConnection);
18-
var dbWriter = marklogic.createDatabaseClient(testconfig.restWriterConnection);
19-
var dbAdmin = marklogic.createDatabaseClient(testconfig.restAdminConnection);
2021

2122
describe('Binary documents test', function () {
22-
var binaryPath = __dirname + '/data/somePdfFile.pdf';
23-
var uri = '/test/binary/somePdfFile.pdf';
24-
var binaryValue = null;
25-
before(function (done) {
26-
this.timeout(10000);
27-
fs.createReadStream(binaryPath).
28-
pipe(concatStream({ encoding: 'buffer' }, function (value) {
29-
binaryValue = value;
30-
done();
31-
}));
32-
});
23+
const binaryPath = __dirname + '/data/somePdfFile.pdf';
24+
let binaryValue = null;
3325

34-
it('should write the binary with Readable stream', function (done) {
35-
this.timeout(10000);
36-
var uri = '/test/write/somePdfFile.pdf';
37-
var readableBinary = new ValueStream(binaryValue);
38-
//readableBinary.pause();
39-
dbWriter.documents.write({
40-
uri: uri,
41-
contentType: 'application/pdf',
42-
quality: 25,
43-
properties: { prop1: 'foo' },
44-
content: readableBinary
45-
}).
46-
result(function (response) {
47-
response.should.have.property('documents');
48-
done();
49-
}, done);
50-
});
26+
const uri = '/test/write/somePdfFile.pdf';
5127

52-
it('should read the binary with Readable stream', function (done) {
53-
this.timeout(10000);
54-
var uri = '/test/write/somePdfFile.pdf';
55-
dbReader.documents.read(uri).
56-
result(function (documents) {
57-
//console.log(JSON.stringify(documents, null, 2));
58-
JSON.stringify(binaryValue).should.equal(
59-
JSON.stringify(documents[0].content));
60-
done();
61-
}, done);
28+
before(async function () {
29+
binaryValue = await new Promise((resolve, reject) => {
30+
fs.createReadStream(binaryPath)
31+
.pipe(concatStream({ encoding: 'buffer' }, function (value) {
32+
resolve(value);
33+
}))
34+
.on('error', reject);
35+
});
6236
});
6337

64-
it('should read the binary document metadata', function (done) {
65-
this.timeout(10000);
66-
var uri = '/test/write/somePdfFile.pdf';
67-
dbReader.documents.read({ uris: uri, categories: ['metadata'] })
68-
.result(function (documents) {
69-
var document = documents[0];
70-
document.quality.should.equal(25);
71-
document.properties.prop1.should.equal('foo');
72-
done();
73-
}, done);
74-
});
38+
it('should write, read, read metadata, verify and delete the binary pdf content', async function () {
7539

76-
it('should delete the pdf file', function (done) {
77-
dbAdmin.documents.removeAll({
78-
all: true
79-
}).
80-
result(function (response) {
81-
done();
82-
}, done);
83-
});
40+
const readableBinary = new ValueStream(binaryValue);
41+
try {
42+
const writeResponse = await dbWriter.documents.write({
43+
uri: uri,
44+
contentType: 'application/pdf',
45+
quality: 25,
46+
properties: { prop1: 'foo' },
47+
content: readableBinary
48+
}).result();
49+
writeResponse.should.have.property('documents');
8450

51+
const docReadResp = await dbReader.documents.read(uri).result();
52+
docReadResp[0].contentType.should.equal('application/pdf');
53+
docReadResp[0].content.length.should.equal(binaryValue.length);
54+
Buffer.compare(binaryValue, docReadResp[0].content).should.equal(0);
55+
56+
const docReadRespMeta = await dbReader.documents.read(
57+
{ uris: uri, categories: ['metadata'] }
58+
).result();
59+
docReadRespMeta[0].quality.should.equal(25);
60+
docReadRespMeta[0].properties.prop1.should.equal('foo');
61+
62+
// original test deleted all documents. This one just deletes the one we wrote.
63+
await dbAdmin.documents.remove(uri).result();
64+
65+
} catch (error) {
66+
console.error(error);
67+
throw error;
68+
}
69+
});
8570
});
8671

8772
function ValueStream(value) {

test-complete/nodejs-documents-read-chunk.js

Lines changed: 67 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,84 @@
11
/*
22
* Copyright (c) 2015-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
33
*/
4-
var should = require('should');
4+
const should = require('should');
55

6-
var fs = require('fs');
7-
var stream = require('stream');
8-
var util = require('util');
6+
const fs = require('fs');
7+
const stream = require('stream');
8+
const util = require('util');
99

10-
var concatStream = require('concat-stream');
11-
var valcheck = require('core-util-is');
12-
var testconfig = require('../etc/test-config-qa.js');
10+
const concatStream = require('concat-stream');
11+
const valcheck = require('core-util-is');
12+
const testconfig = require('../etc/test-config-qa.js');
1313

14-
var marklogic = require('../');
15-
var q = marklogic.queryBuilder;
14+
const marklogic = require('../');
15+
const q = marklogic.queryBuilder;
1616

17-
var dbReader = marklogic.createDatabaseClient(testconfig.restReaderConnection);
18-
var dbWriter = marklogic.createDatabaseClient(testconfig.restWriterConnection);
19-
var dbAdmin = marklogic.createDatabaseClient(testconfig.restAdminConnection);
17+
const dbReader = marklogic.createDatabaseClient(testconfig.restReaderConnection);
18+
const dbWriter = marklogic.createDatabaseClient(testconfig.restWriterConnection);
19+
const dbAdmin = marklogic.createDatabaseClient(testconfig.restAdminConnection);
2020

2121
describe('Binary documents test', function () {
22-
var binaryPath = __dirname + '/data/somePdfFile.pdf';
23-
var uri = '/test/binary/somePdfFile.pdf';
24-
var binaryValue = null;
25-
before(function (done) {
26-
this.timeout(10000);
27-
fs.createReadStream(binaryPath).
28-
pipe(concatStream({ encoding: 'buffer' }, function (value) {
29-
binaryValue = value;
30-
done();
31-
}));
32-
});
22+
const binaryPath = __dirname + '/data/somePdfFile.pdf';
23+
let binaryValue = null;
24+
25+
const uri = '/test/binary/somePdfFile.pdf';
3326

34-
it('should write the binary with Readable stream', function (done) {
35-
this.timeout(10000);
36-
var uri = '/test/write/somePdfFile.pdf';
37-
var readableBinary = new ValueStream(binaryValue);
38-
//readableBinary.pause();
39-
dbWriter.documents.write({
40-
uri: uri,
41-
contentType: 'application/pdf',
42-
quality: 25,
43-
properties: { prop1: 'foo' },
44-
content: readableBinary
45-
}).
46-
result(function (response) {
47-
response.should.have.property('documents');
48-
done();
49-
}, done);
27+
before(async function () {
28+
binaryValue = await new Promise((resolve, reject) => {
29+
fs.createReadStream(binaryPath)
30+
.pipe(concatStream({ encoding: 'buffer' }, function (value) {
31+
resolve(value);
32+
}))
33+
.on('error', reject);
34+
});
5035
});
5136

52-
it('should wait for the document to be written', function (done) {
53-
setTimeout(function () {
54-
done();
55-
}, 10000);
56-
});
37+
it('should write, read using stream, verify and delete the binary content', async function () {
5738

58-
it('should read the binary in chunk', function (done) {
59-
this.timeout(10000);
60-
var uri = '/test/write/somePdfFile.pdf';
61-
setTimeout(function () {
62-
dbReader.documents.read(uri).stream('chunked').
63-
on('data', function (data) {
64-
var strData = data.toString();
65-
strData.should.containEql('CVISION Technologies');
66-
}).
67-
on('end', function () {
68-
done();
69-
}, done);
70-
}, 3000);
71-
});
72-
it('should delete all documents', function (done) {
73-
dbAdmin.documents.removeAll({
74-
all: true
75-
}).
76-
result(function (response) {
77-
done();
78-
}, done);
79-
});
39+
let readableBinary = new ValueStream(binaryValue);
40+
try {
41+
const response = await dbWriter.documents.write({
42+
uri: uri,
43+
contentType: 'application/pdf',
44+
quality: 25,
45+
properties: { prop1: 'foo' },
46+
content: readableBinary
47+
}).result();
48+
49+
response.should.have.property('documents');
8050

51+
const streamData = await new Promise((resolve, reject) => {
52+
const chunks = [];
53+
const readStream = dbReader.documents.read(uri).stream('chunked');
54+
55+
readStream.on('data', function (data) {
56+
chunks.push(data);
57+
});
58+
59+
readStream.on('end', function () {
60+
resolve(Buffer.concat(chunks));
61+
});
62+
63+
readStream.on('error', function (error) {
64+
reject(error);
65+
});
66+
});
67+
68+
Buffer.compare(binaryValue, streamData).should.equal(0);
69+
70+
// Verify the binary content has a string near the end of the file
71+
const strData = streamData.toString();
72+
strData.should.containEql('CVISION Technologies');
73+
74+
// original test deleted all documents. This one just deletes the one we wrote.
75+
await dbAdmin.documents.remove(uri).result();
76+
77+
} catch (error) {
78+
console.error("Error writing document: ", error);
79+
throw error;
80+
}
81+
});
8182
});
8283

8384
function ValueStream(value) {

0 commit comments

Comments
 (0)