-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathnodejs-graphs-merge-stream.js
More file actions
129 lines (116 loc) · 5.24 KB
/
Copy pathnodejs-graphs-merge-stream.js
File metadata and controls
129 lines (116 loc) · 5.24 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
/*
* 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 valcheck = require('core-util-is');
var concatStream = require('concat-stream');
var testconfig = require('../etc/test-config-qa.js');
var marklogic = require('../');
var q = marklogic.queryBuilder;
var db = marklogic.createDatabaseClient(testconfig.restWriterConnection);
describe('merge stream graph test', function () {
var graphUri = 'marklogic.com/stream/merge/people';
var graphPath1 = __dirname + '/data/people3.ttl';
var graphPath2 = __dirname + '/data/people4.ttl';
var sparqlPath = __dirname + '/data/people.rq';
it('should write the first graph with stream', function (done) {
this.timeout(10000);
var ws = db.graphs.createWriteStream(graphUri, 'text/turtle');
ws.result(function (response) {
//console.log(JSON.stringify(response, null, 4));
response.should.have.property('graph');
response.graph.should.equal(graphUri);
done();
}, done);
fs.createReadStream(graphPath1).pipe(ws);
});
it('should merge the second graph with stream', function (done) {
this.timeout(10000);
var ws = db.graphs.createMergeStream(graphUri, 'text/turtle');
ws.result(function (response) {
//console.log(JSON.stringify(response, null, 4));
response.should.have.property('graph');
response.graph.should.equal(graphUri);
done();
}, done);
fs.createReadStream(graphPath2).pipe(ws);
});
it('should wait for the graphs to get merged', function (done) {
setTimeout(function () {
done();
}, 10000);
});
it('should read the merged graph as a stream', function (done) {
this.timeout(10000);
db.graphs.read(graphUri, 'text/n3').stream('chunked').
on('data', function (data) {
(!valcheck.isNullOrUndefined(data)).should.equal(true);
var strData = data.toString();
//console.log(strData);
strData.should.containEql('p0:person1 a p0:Person ;');
strData.should.containEql(' foaf:knows p0:person2 ;');
strData.should.containEql('p0:person12 a p0:Person ;');
strData.should.containEql(' foaf:knows p0:person12 ;');
}).
on('end', function () {
done();
}, done);
});
it('should list the merged graph', function (done) {
this.timeout(10000);
db.graphs.list().
result(function (collections) {
//console.log(collections);
collections.some(function (collection) {
return collection === graphUri;
}).should.equal(true);
done();
}, done);
});
it('should check the merged graph', function (done) {
this.timeout(10000);
db.graphs.probe(graphUri).
result(function (response) {
response.should.have.property('graph');
response.graph.should.equal(graphUri);
response.should.have.property('exists');
response.exists.should.equal(true);
done();
}, done);
});
it('should run a SPARQL query against the merged graph', function (done) {
this.timeout(10000);
db.graphs.sparql('application/sparql-results+json', fs.createReadStream(sparqlPath)).
result(function (response) {
response.should.have.property('head');
response.head.should.have.property('vars');
response.head.vars.length.should.equal(2);
response.head.vars[0].should.equal('personName1');
response.head.vars[1].should.equal('personName2');
response.should.have.property('results');
response.results.should.have.property('bindings');
var strResponse = JSON.stringify(response);
//console.log(strResponse);
strResponse.should.containEql('Person 2');
strResponse.should.containEql('Person 12');
/*response.results.bindings[0].should.have.property('personName1');
response.results.bindings[0].personName1.should.have.property('value');
response.results.bindings[0].personName1.value.should.equal('Person 1');
response.results.bindings[0].should.have.property('personName2');
response.results.bindings[0].personName2.should.have.property('value');
response.results.bindings[0].personName2.value.should.equal('Person 2');
response.results.bindings[1].personName1.value.should.equal('Person 9');
response.results.bindings[1].personName2.value.should.equal('Person 12');*/
//console.log(JSON.stringify(response, null, 4))
done();
}, done);
});
it('should remove the graph', function (done) {
this.timeout(10000);
db.graphs.remove(graphUri).
result(function (response) {
done();
}, done);
});
});