|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 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 | +'use strict'; |
| 17 | + |
| 18 | +const expect = require('chai').expect; |
| 19 | + |
| 20 | +const fs = require('fs'); |
| 21 | + |
| 22 | +const marklogic = require('../'); |
| 23 | + |
| 24 | +const connectdef = require('../config-optic/connectdef.js'); |
| 25 | + |
| 26 | +const dbName = connectdef.name; |
| 27 | + |
| 28 | +// Client to query generated views |
| 29 | +const db = marklogic.createDatabaseClient( |
| 30 | +{ |
| 31 | + database: dbName, |
| 32 | + host: connectdef.host, |
| 33 | + port: connectdef.port, |
| 34 | + user: 'admin', |
| 35 | + password: 'admin', |
| 36 | + authType: connectdef.authType |
| 37 | +} |
| 38 | +); |
| 39 | + |
| 40 | +// Client to query schema database to verify view is stored |
| 41 | +// Make sure you have qbvuser user created. See XQuery script nodejs-optic-setup.xml of internal repo (SVN). |
| 42 | +var dbModClient = marklogic.createDatabaseClient({ |
| 43 | + database: dbName+'Modules', |
| 44 | + host: connectdef.host, |
| 45 | + port: connectdef.port, |
| 46 | + user: 'qbvuser', |
| 47 | + password: 'qbvuser', |
| 48 | + authType: connectdef.authType |
| 49 | +}); |
| 50 | + |
| 51 | +// Client to generate views |
| 52 | +const dbClient = marklogic.createDatabaseClient( |
| 53 | +{ |
| 54 | + database: dbName, |
| 55 | + host: connectdef.host, |
| 56 | + port: connectdef.port, |
| 57 | + user: 'qbvuser', |
| 58 | + password: 'qbvuser', |
| 59 | + authType: connectdef.authType |
| 60 | +} |
| 61 | +); |
| 62 | + |
| 63 | +const pb = marklogic.planBuilder; |
| 64 | + |
| 65 | +function waitForViewCreate(wTime) |
| 66 | +{ |
| 67 | + return it('View creation delay', done => |
| 68 | + { |
| 69 | + setTimeout(() => done(), wTime) |
| 70 | + |
| 71 | + }).timeout(wTime + 120) |
| 72 | +} |
| 73 | + |
| 74 | +describe('Nodejs Optic generate views test', function(){ |
| 75 | + it('TEST 1 - join left outer with array of on', function(done){ |
| 76 | + |
| 77 | + const plan1 = |
| 78 | + pb.fromView('opticFunctionalTest', 'detail') |
| 79 | + .orderBy(pb.schemaCol('opticFunctionalTest', 'detail', 'id')); |
| 80 | + const plan2 = |
| 81 | + pb.fromView('opticFunctionalTest', 'master') |
| 82 | + .orderBy(pb.schemaCol('opticFunctionalTest', 'master' , 'id')); |
| 83 | + const output = |
| 84 | + plan1.joinLeftOuter(plan2, [pb.on(pb.viewCol('master', 'id'), pb.viewCol('detail', 'masterId')), pb.on(pb.viewCol('detail', 'name'), pb.viewCol('master', 'name'))]) |
| 85 | + .select([ |
| 86 | + pb.as('MasterName', pb.schemaCol('opticFunctionalTest', 'master', 'name')), |
| 87 | + pb.schemaCol('opticFunctionalTest', 'master', 'date'), |
| 88 | + pb.as('DetailName', pb.schemaCol('opticFunctionalTest', 'detail', 'name')), |
| 89 | + pb.schemaCol('opticFunctionalTest', 'detail', 'amount'), |
| 90 | + pb.schemaCol('opticFunctionalTest', 'detail', 'color') |
| 91 | + ]) |
| 92 | + .orderBy(pb.desc(pb.col('DetailName'))); |
| 93 | + |
| 94 | + const plan = JSON.stringify(output.export()); |
| 95 | + dbClient.rows.generateView(plan, 'InnerJoin', 'keymatch') |
| 96 | + .then(function(res) { |
| 97 | + //console.log(JSON.stringify(res, null, 2)); |
| 98 | + dbModClient.documents.write({ |
| 99 | + uri: '/qbv-InnerJoin-keymatch.xml', |
| 100 | + collections: 'http://marklogic.com/xdmp/qbv', |
| 101 | + contentType: 'application/xml', |
| 102 | + content: res |
| 103 | + }). |
| 104 | + result(function(response){ |
| 105 | + // console.log(JSON.stringify(response, null, 4)); |
| 106 | + }); |
| 107 | + done();}, done ); |
| 108 | + }); |
| 109 | + |
| 110 | + waitForViewCreate(1000); |
| 111 | + |
| 112 | + it('TEST 1a - Verify InnerJoin keymatch view', function(done){ |
| 113 | + const qv = pb.fromView('InnerJoin', 'keymatch'); |
| 114 | + |
| 115 | + db.rows.query(qv, { format: 'json', structure: 'object', columnTypes: 'rows' }) |
| 116 | + .then(function(output) { |
| 117 | + //console.log(JSON.stringify(output, null, 2)); |
| 118 | + expect(output.rows.length).to.equal(6); |
| 119 | + var row0 = output.rows[0]; |
| 120 | + var row3 = output.rows[3]; |
| 121 | + var row5 = output.rows[5]; |
| 122 | + |
| 123 | + expect(row0["InnerJoin.keymatch.amount"].value).to.equal(60.06); |
| 124 | + expect(row0["InnerJoin.keymatch.color"].value).to.equal('green'); |
| 125 | + |
| 126 | + expect(row3["InnerJoin.keymatch.DetailName"].value).to.equal('Detail 3'); |
| 127 | + expect(row5["InnerJoin.keymatch.amount"].value).to.equal(10.01); |
| 128 | + done(); |
| 129 | + }, function(error) { |
| 130 | + console.log(JSON.stringify(error, null, 2)); |
| 131 | + done(); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + //Refer to TEST 3 of optic-query-by-views.js |
| 136 | + it('TEST 2 - sparql group by with min', function(done){ |
| 137 | + const output = |
| 138 | + pb.fromSPARQL("PREFIX demov: <http://demo/verb#> \ |
| 139 | + PREFIX vcard: <http://www.w3.org/2006/vcard/ns#> \ |
| 140 | + SELECT ?country (MIN (?sales) AS ?min_sales ) \ |
| 141 | + FROM </optic/sparql/test/companies.ttl> \ |
| 142 | + WHERE { \ |
| 143 | + ?company a vcard:Organization . \ |
| 144 | + ?company demov:sales ?sales . \ |
| 145 | + ?company vcard:hasAddress [ vcard:country-name ?country ] \ |
| 146 | + } \ |
| 147 | + GROUP BY ?country \ |
| 148 | + ORDER BY ASC( ?min_sales ) ?country"); |
| 149 | + |
| 150 | + const plan = JSON.stringify(output.export()); |
| 151 | + dbClient.rows.generateView(plan, 'sparql', 'groupmin') |
| 152 | + .then(function(res) { |
| 153 | + //console.log(JSON.stringify(res, null, 2)); |
| 154 | + dbModClient.documents.write({ |
| 155 | + uri: '/qbv-sparql-groupmin.xml', |
| 156 | + collections: 'http://marklogic.com/xdmp/qbv', |
| 157 | + contentType: 'application/xml', |
| 158 | + content: res |
| 159 | + }). |
| 160 | + result(function(response){ |
| 161 | + // console.log(JSON.stringify(response, null, 4)); |
| 162 | + }); |
| 163 | + done();}, done ); |
| 164 | + }); |
| 165 | + |
| 166 | + waitForViewCreate(1000); |
| 167 | + |
| 168 | + it('TEST 2a - Verify sparql groupmin view', function(done){ |
| 169 | + const qv = pb.fromView('sparql', 'groupmin'); |
| 170 | + |
| 171 | + db.rows.query(qv, { format: 'json', structure: 'object', columnTypes: 'rows' }) |
| 172 | + .then(function(output) { |
| 173 | + //console.log(JSON.stringify(output, null, 2)); |
| 174 | + expect(output.rows.length).to.equal(8); |
| 175 | + var row0 = output.rows[0]; |
| 176 | + var row7 = output.rows[7]; |
| 177 | + |
| 178 | + expect(row0["sparql.groupmin.country"].value).to.equal('China'); |
| 179 | + expect(row0["sparql.groupmin.min_sales"].value).to.equal(8); |
| 180 | + expect(row7["sparql.groupmin.country"].value).to.equal('USA'); |
| 181 | + expect(row7["sparql.groupmin.min_sales"].value).to.equal(10000000); |
| 182 | + done(); |
| 183 | + }, function(error) { |
| 184 | + console.log(JSON.stringify(error, null, 2)); |
| 185 | + done(); |
| 186 | + }); |
| 187 | + }); |
| 188 | + |
| 189 | + //Refer to TEST 6 of optic-query-by-views.js |
| 190 | + it('TEST 3 - sparql group by with min', function(done){ |
| 191 | + const popCol = pb.col('popularity'); |
| 192 | + const dateCol = pb.col('date'); |
| 193 | + const plan1 = |
| 194 | + pb.fromLexicons( |
| 195 | + { |
| 196 | + uri: pb.cts.uriReference(), |
| 197 | + city: pb.cts.jsonPropertyReference('city'), |
| 198 | + popularity: pb.cts.jsonPropertyReference('popularity'), |
| 199 | + date: pb.cts.jsonPropertyReference('date'), |
| 200 | + distance: pb.cts.jsonPropertyReference('distance'), |
| 201 | + point: pb.cts.jsonPropertyReference('latLonPoint') |
| 202 | + }, 'myCity' |
| 203 | + ); |
| 204 | + const output = plan1 |
| 205 | + .where(pb.gt(popCol, 2)) |
| 206 | + .orderBy(pb.asc('date')) |
| 207 | + .select(['city', 'popularity', 'date', 'distance', 'point']); |
| 208 | + |
| 209 | + const plan = JSON.stringify(output.export()); |
| 210 | + dbClient.rows.generateView(plan, 'lexicons', 'orderbyselect') |
| 211 | + .then(function(res) { |
| 212 | + //console.log(JSON.stringify(res, null, 2)); |
| 213 | + dbModClient.documents.write({ |
| 214 | + uri: '/qbv-lexicons-orderby-select.xml', |
| 215 | + collections: 'http://marklogic.com/xdmp/qbv', |
| 216 | + contentType: 'application/xml', |
| 217 | + content: res |
| 218 | + }). |
| 219 | + result(function(response){ |
| 220 | + // console.log(JSON.stringify(response, null, 4)); |
| 221 | + }); |
| 222 | + done();}, done ); |
| 223 | + }); |
| 224 | + |
| 225 | + waitForViewCreate(1000); |
| 226 | + |
| 227 | + it('TEST 3a - Verify sparql groupmin view', function(done){ |
| 228 | + const qv = pb.fromView('lexicons', 'orderbyselect'); |
| 229 | + |
| 230 | + db.rows.query(qv, { format: 'json', structure: 'object', columnTypes: 'rows' }) |
| 231 | + .then(function(output) { |
| 232 | + //console.log(JSON.stringify(output, null, 2)); |
| 233 | + expect(output.rows.length).to.equal(4); |
| 234 | + var row0 = output.rows[0]; |
| 235 | + var row3 = output.rows[3]; |
| 236 | + |
| 237 | + expect(row0["lexicons.orderbyselect.popularity"].value).to.equal(5); |
| 238 | + expect(row0["lexicons.orderbyselect.date"].value).to.equal('1981-11-09'); |
| 239 | + expect(row3["lexicons.orderbyselect.popularity"].value).to.equal(5); |
| 240 | + expect(row3["lexicons.orderbyselect.date"].value).to.equal('2007-01-01'); |
| 241 | + done(); |
| 242 | + }, function(error) { |
| 243 | + console.log(JSON.stringify(error, null, 2)); |
| 244 | + done(); |
| 245 | + }); |
| 246 | + }); |
| 247 | +}); |
0 commit comments