-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathdocuments-quick.js
More file actions
200 lines (190 loc) · 6.71 KB
/
documents-quick.js
File metadata and controls
200 lines (190 loc) · 6.71 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
/*
* Copyright (c) 2015-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
var should = require('should');
var fs = require('fs');
var stream = require('stream');
var util = require('util');
var concatStream = require('concat-stream');
var valcheck = require('core-util-is');
var testconfig = require('../etc/test-config.js');
var testutil = require('./test-util.js');
var marklogic = require('../');
var q = marklogic.queryBuilder;
var db = marklogic.createDatabaseClient(testconfig.restWriterConnection);
describe('quick path', function(){
before(function(done){
db.documents.write({
uri: '/test/quick/object1.json',
collections: '/quickdocs',
contentType: 'application/json',
content: {quickKey: 'quickMatch'}
},{
uri: '/test/quick/object2.json',
collections: '/quickdocs',
contentType: 'application/json',
content: {quickKey: 'quickMatch'}
})
.result(function(response){done();})
.catch(done);
});
it('should create objects', function(done){
db.createCollection(
'/quickdocs',
{quickKey: 'quick value 1'},
{quickKey: 'quick value 2'}
)
.result(function(uris){
valcheck.isUndefined(uris).should.equal(false);
uris.length.should.equal(2);
return db.documents.read({uris:uris, categories:['content', 'collections']}).result();
})
.then(function(documents) {
valcheck.isUndefined(documents).should.equal(false);
documents.length.should.equal(2);
documents[0].should.have.property('content');
documents[0].content.should.have.property('quickKey');
documents[0].should.have.property('collections');
documents[0].collections[0].should.equal('/quickdocs');
documents[1].should.have.property('content');
documents[1].content.should.have.property('quickKey');
documents[1].should.have.property('collections');
documents[1].collections[0].should.equal('/quickdocs');
done();
})
.catch(done);
});
it('should write a mapping', function(done){
db.writeCollection(
'/quickmapped',
{
'/quickmapped/doc1.json': {mappedKey: 'quick value 1'},
'/quickmapped/doc2.xml': '<quickDoc>quick content 2</quickDoc>'
}
)
.result(function(uris){
valcheck.isUndefined(uris).should.equal(false);
uris.length.should.equal(2);
return db.documents.read({uris:uris, categories:['content', 'collections']}).result();
})
.then(function(documents) {
valcheck.isUndefined(documents).should.equal(false);
documents.length.should.equal(2);
documents[0].should.have.property('content');
documents[0].content.should.have.property('mappedKey');
documents[0].should.have.property('collections');
documents[0].collections[0].should.equal('/quickmapped');
documents[1].should.have.property('content');
documents[1].content.should.containEql('<quickDoc>quick content 2</quickDoc>');
documents[1].should.have.property('collections');
documents[1].collections[0].should.equal('/quickmapped');
done();
})
.catch(done);
});
it('should read objects', function(done){
db.read('/test/quick/object1.json', '/test/quick/object2.json')
.result(function(objects) {
valcheck.isUndefined(objects).should.equal(false);
objects.length.should.equal(2);
objects[0].should.have.property('quickKey');
objects[0].quickKey.should.equal('quickMatch');
objects[1].should.have.property('quickKey');
objects[1].quickKey.should.equal('quickMatch');
done();
})
.catch(done);
});
it('should query objects', function(done){
db.queryCollection(
'/quickdocs',
q.where(q.byExample({
quickKey: 'quickMatch'
})))
.result(function(objects) {
valcheck.isUndefined(objects).should.equal(false);
objects.length.should.equal(2);
objects[0].should.have.property('quickKey');
objects[0].quickKey.should.equal('quickMatch');
objects[1].should.have.property('quickKey');
objects[1].quickKey.should.equal('quickMatch');
done();
})
.catch(done);
});
it('should remove a document', function(done) {
var docUri = '/test/quick/object1.json';
db.remove(docUri)
.result(function(response) {
response.should.be.an.Array;
response.length.should.equal(1);
return db.probe(docUri).result();
})
.then(function(exists) {
exists.should.eql(false);
done();
})
.catch(done);
});
it('should probe', function(done){
db.probe('/test/quick/object2.json')
.result(function(exists) {
exists.should.eql(true);
done();
})
.catch(done);
});
it('should remove a collection', function(done){
var collectionUri = '/quickdocs';
this.timeout(3000);
db.removeCollection(collectionUri)
.result(function(collection) {
collectionUri.should.eql(collection);
return db.probe('/test/quick/object2.json').result();
})
.then(function(exists) {
exists.should.eql(false);
done();
})
.catch(done);
});
});
describe('check connection', function(){
var assert = require('assert');
it('should check the connection', function(done){
var flag = false;
db.checkConnection()
.result(function(response) {
assert(response.connected === true);
done();
}).catch(done);
});
it('should give 403 when user is not authorized', function(done){
var config = {host:testconfig.testConnection.host, user:testconfig.testConnection.user, password: testconfig.testConnection.password, port:testconfig.testConnection.port,
authType:testconfig.testConnection.authType};
var db1 = marklogic.createDatabaseClient(config);
var assert = require('assert');
db1.checkConnection()
.result(function(response) {
assert(response.connected === false);
assert(response.httpStatusCode === 403);
assert(response.httpStatusMessage === 'Forbidden');
done();
})
.catch(done);
});
it('should give 401 when does not exist', function(done){
var config = {host:testconfig.restReaderConnection.host, user:testconfig.testConnection.user, password: 'invalid', port:testconfig.testConnection.port,
authType:testconfig.restReaderConnection.authType};
var db1 = marklogic.createDatabaseClient(config);
var assert = require('assert');
db1.checkConnection()
.result(function(response) {
assert(response.connected === false);
assert(response.httpStatusCode === 401);
assert(response.httpStatusMessage === 'Unauthorized');
done();
})
.catch(done);
});
});