|
| 1 | +/* |
| 2 | + * Copyright 2014-2015 MarkLogic Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +var should = require('should'); |
| 17 | + |
| 18 | +var fs = require('fs'); |
| 19 | +var stream = require('stream'); |
| 20 | +var util = require('util'); |
| 21 | + |
| 22 | +var concatStream = require('concat-stream'); |
| 23 | +var valcheck = require('core-util-is'); |
| 24 | +var testconfig = require('../etc/test-config-qa.js'); |
| 25 | + |
| 26 | +var marklogic = require('../'); |
| 27 | +var q = marklogic.queryBuilder; |
| 28 | + |
| 29 | +var dbReader = marklogic.createDatabaseClient(testconfig.restReaderConnection); |
| 30 | +var dbWriter = marklogic.createDatabaseClient(testconfig.restWriterConnection); |
| 31 | +var dbAdmin = marklogic.createDatabaseClient(testconfig.restAdminConnection); |
| 32 | + |
| 33 | +describe('Large document write test', function(){ |
| 34 | + var binaryPath = './node-client-api/test-complete/data/shaks200all.xml'; |
| 35 | + var uri = '/test/binary/shaks200all.xml'; |
| 36 | + var binaryValue = null; |
| 37 | + before(function(done){ |
| 38 | + this.timeout(10000); |
| 39 | + fs.createReadStream(binaryPath). |
| 40 | + pipe(concatStream({encoding: 'buffer'}, function(value){ |
| 41 | + binaryValue = value; |
| 42 | + done(); |
| 43 | + })); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should write the large document with Readable stream', function(done){ |
| 47 | + this.timeout(30000); |
| 48 | + var uri = '/test/write/shaks200all.xml'; |
| 49 | + var readableBinary = new ValueStream(binaryValue); |
| 50 | + //readableBinary.pause(); |
| 51 | + dbWriter.documents.write({ |
| 52 | + uri: uri, |
| 53 | + contentType: 'application/xml', |
| 54 | + quality: 25, |
| 55 | + properties: {prop1: 'large file'}, |
| 56 | + content: readableBinary |
| 57 | + }). |
| 58 | + result(function(response){ |
| 59 | + response.should.have.property('documents'); |
| 60 | + done(); |
| 61 | + }, done); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should read the large document with Readable stream', function(done){ |
| 65 | + this.timeout(30000); |
| 66 | + var uri = '/test/write/shaks200all.xml'; |
| 67 | + dbReader.documents.read(uri). |
| 68 | + result(function(documents){ |
| 69 | + var count = (JSON.stringify(documents).match(/TITLE/g) || []).length; |
| 70 | + //console.log(count); |
| 71 | + count.should.equal(2064); |
| 72 | + //console.log(JSON.stringify(documents, null, 2)); |
| 73 | + done(); |
| 74 | + }, done); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should read the large document metadata', function(done) { |
| 78 | + this.timeout(10000); |
| 79 | + var uri = '/test/write/shaks200all.xml'; |
| 80 | + dbReader.documents.read({uris: uri, categories:['metadata']}) |
| 81 | + .result(function(documents) { |
| 82 | + var document = documents[0]; |
| 83 | + document.quality.should.equal(25); |
| 84 | + document.properties.prop1.should.equal('large file'); |
| 85 | + done(); |
| 86 | + }, done); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should delete the xml file file', function(done){ |
| 90 | + dbAdmin.documents.removeAll({ |
| 91 | + all: true |
| 92 | + }). |
| 93 | + result(function(response) { |
| 94 | + done(); |
| 95 | + }, done); |
| 96 | + }); |
| 97 | + |
| 98 | +}); |
| 99 | + |
| 100 | +function ValueStream(value) { |
| 101 | + stream.Readable.call(this); |
| 102 | + this.value = value; |
| 103 | +} |
| 104 | +util.inherits(ValueStream, stream.Readable); |
| 105 | +ValueStream.prototype._read = function() { |
| 106 | + this.push(this.value); |
| 107 | + this.push(null); |
| 108 | +}; |
0 commit comments