|
| 1 | +// ESM version of integration test for PATCH with application/sparql-update |
| 2 | +import { describe, it, after } from 'mocha'; |
| 3 | +import { strict as assert } from 'assert'; |
| 4 | +import path from 'path'; |
| 5 | +import { fileURLToPath } from 'url'; |
| 6 | +import { rm, write, read } from '../utils.mjs'; |
| 7 | +// import supertest from 'supertest'; |
| 8 | +// import ldnode from '../../index.js'; |
| 9 | +import { createRequire } from 'module' |
| 10 | + |
| 11 | + |
| 12 | +const require = createRequire(import.meta.url); |
| 13 | +const ldnode = require('../../index.js'); |
| 14 | +const supertest = require('supertest'); |
| 15 | +const __filename = fileURLToPath(import.meta.url); |
| 16 | +const __dirname = path.dirname(__filename); |
| 17 | +const fse = require('fs-extra'); |
| 18 | + |
| 19 | +// Starting LDP |
| 20 | +const ldp = ldnode.createServer({ |
| 21 | + root: path.join(__dirname, '../resources/sampleContainer'), |
| 22 | + mount: '/test-esm', |
| 23 | + webid: false |
| 24 | +}); |
| 25 | +const server = supertest(ldp); |
| 26 | + |
| 27 | +before(function () { |
| 28 | + fse.ensureDirSync(path.join(__dirname, '../resources/sampleContainer')); |
| 29 | +}); |
| 30 | + |
| 31 | +describe('PATCH through application/sparql-update', function () { |
| 32 | + it('should create a new file if file does not exist', function (done) { |
| 33 | + rm('sampleContainer/notExisting.ttl'); |
| 34 | + const sampleContainerPath = path.join(__dirname, '../resources/sampleContainer'); |
| 35 | +fse.ensureDirSync(sampleContainerPath); |
| 36 | + server.patch('/notExisting.ttl') |
| 37 | + .set('content-type', 'application/sparql-update') |
| 38 | + .send('INSERT DATA { :test :hello 456 .}') |
| 39 | + .expect(201) |
| 40 | + .end(function (err, res) { |
| 41 | + assert.equal( |
| 42 | + read('sampleContainer/notExisting.ttl'), |
| 43 | + '@prefix : </notExisting.ttl#>.\n\n:test :hello 456 .\n\n' |
| 44 | + ); |
| 45 | + rm('sampleContainer/notExisting.ttl'); |
| 46 | + done(err); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('DELETE', function () { |
| 51 | + it('should be an empty resource if last triple is deleted', function (done) { |
| 52 | + write( |
| 53 | + '<#current> <#temp> 123 .', |
| 54 | + 'sampleContainer/existingTriple.ttl' |
| 55 | + ); |
| 56 | + server.post('/existingTriple.ttl') |
| 57 | + .set('content-type', 'application/sparql-update') |
| 58 | + .send('DELETE { :current :temp 123 .}') |
| 59 | + .expect(200) |
| 60 | + .end(function (err, res) { |
| 61 | + assert.equal( |
| 62 | + read('sampleContainer/existingTriple.ttl'), |
| 63 | + '@prefix : </existingTriple.ttl#>.\n\n' |
| 64 | + ); |
| 65 | + rm('sampleContainer/existingTriple.ttl'); |
| 66 | + done(err); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should delete a single triple from a pad document', function (done) { |
| 71 | + const expected = `@prefix : </existingTriple.ttl#>.\n@prefix cal: <http://www.w3.org/2002/12/cal/ical#>.\n@prefix dc: <http://purl.org/dc/elements/1.1/>.\n@prefix meeting: <http://www.w3.org/ns/pim/meeting#>.\n@prefix pad: <http://www.w3.org/ns/pim/pad#>.\n@prefix sioc: <http://rdfs.org/sioc/ns#>.\n@prefix ui: <http://www.w3.org/ns/ui#>.\n@prefix wf: <http://www.w3.org/2005/01/wf/flow#>.\n@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.\n@prefix c: <https://www.w3.org/People/Berners-Lee/card#>.\n@prefix ind: </parent/index.ttl#>.\n\n:id1477502276660 dc:author c:i; sioc:content \"\"; pad:next :this.\n\n:id1477522707481\n cal:dtstart \"2016-10-26T22:58:27Z\"^^xsd:dateTime;\n wf:participant c:i;\n ui:backgroundColor \"#c1d0c8\".\n:this\n a pad:Notepad;\n dc:author c:i;\n dc:created \"2016-10-25T15:44:42Z\"^^xsd:dateTime;\n dc:title \"Shared Notes\";\n pad:next :id1477502276660 .\nind:this wf:participation :id1477522707481; meeting:sharedNotes :this.\n\n`; |
| 72 | + write(`\n\n @prefix dc: <http://purl.org/dc/elements/1.1/>.\n @prefix mee: <http://www.w3.org/ns/pim/meeting#>.\n @prefix c: <https://www.w3.org/People/Berners-Lee/card#>.\n @prefix XML: <http://www.w3.org/2001/XMLSchema#>.\n @prefix p: <http://www.w3.org/ns/pim/pad#>.\n @prefix ind: </parent/index.ttl#>.\n @prefix n: <http://rdfs.org/sioc/ns#>.\n @prefix flow: <http://www.w3.org/2005/01/wf/flow#>.\n @prefix ic: <http://www.w3.org/2002/12/cal/ical#>.\n @prefix ui: <http://www.w3.org/ns/ui#>.\n\n <#this>\n dc:author\n c:i;\n dc:created\n \"2016-10-25T15:44:42Z\"^^XML:dateTime;\n dc:title\n \"Shared Notes\";\n a p:Notepad;\n p:next\n <#id1477502276660>.\n ind:this flow:participation <#id1477522707481>; mee:sharedNotes <#this> .\n <#id1477502276660> dc:author c:i; n:content \"\"; p:indent 1; p:next <#this> .\n <#id1477522707481>\n ic:dtstart\n \"2016-10-26T22:58:27Z\"^^XML:dateTime;\n flow:participant\n c:i;\n ui:backgroundColor\n \"#c1d0c8\".\n`, |
| 73 | + 'sampleContainer/existingTriple.ttl' |
| 74 | + ); |
| 75 | + server.post('/existingTriple.ttl') |
| 76 | + .set('content-type', 'application/sparql-update') |
| 77 | + .send('DELETE { <#id1477502276660> <http://www.w3.org/ns/pim/pad#indent> 1 .}') |
| 78 | + .expect(200) |
| 79 | + .end(function (err, res) { |
| 80 | + assert.equal( |
| 81 | + read('sampleContainer/existingTriple.ttl'), |
| 82 | + expected |
| 83 | + ); |
| 84 | + rm('sampleContainer/existingTriple.ttl'); |
| 85 | + done(err); |
| 86 | + }); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('DELETE and INSERT', function () { |
| 91 | + after(() => rm('sampleContainer/prefixSparql.ttl')); |
| 92 | + |
| 93 | + it('should update a resource using SPARQL-query using `prefix`', function (done) { |
| 94 | + write( |
| 95 | + '@prefix schema: <http://schema.org/> .\n' + |
| 96 | + '@prefix pro: <http://ogp.me/ns/profile#> .\n' + |
| 97 | + '# <http://example.com/timbl#> a schema:Person ;\n' + |
| 98 | + '<#> a schema:Person ;\n' + |
| 99 | + ' pro:first_name "Tim" .\n', |
| 100 | + 'sampleContainer/prefixSparql.ttl' |
| 101 | + ); |
| 102 | + server.post('/prefixSparql.ttl') |
| 103 | + .set('content-type', 'application/sparql-update') |
| 104 | + .send('@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n' + |
| 105 | + '@prefix schema: <http://schema.org/> .\n' + |
| 106 | + '@prefix pro: <http://ogp.me/ns/profile#> .\n' + |
| 107 | + '@prefix ex: <http://example.org/vocab#> .\n' + |
| 108 | + 'DELETE { <#> pro:first_name "Tim" }\n' + |
| 109 | + 'INSERT { <#> pro:first_name "Timothy" }') |
| 110 | + .expect(200) |
| 111 | + .end(function (err, res) { |
| 112 | + assert.equal( |
| 113 | + read('sampleContainer/prefixSparql.ttl'), |
| 114 | + '@prefix : </prefixSparql.ttl#>.\n@prefix schema: <http://schema.org/>.\n@prefix pro: <http://ogp.me/ns/profile#>.\n\n: a schema:Person; pro:first_name "Timothy".\n\n' |
| 115 | + ); |
| 116 | + done(err); |
| 117 | + }); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('INSERT', function () { |
| 122 | + it('should add a new triple', function (done) { |
| 123 | + write( |
| 124 | + '<#current> <#temp> 123 .', |
| 125 | + 'sampleContainer/addingTriple.ttl' |
| 126 | + ); |
| 127 | + server.post('/addingTriple.ttl') |
| 128 | + .set('content-type', 'application/sparql-update') |
| 129 | + .send('INSERT DATA { :test :hello 456 .}') |
| 130 | + .expect(200) |
| 131 | + .end(function (err, res) { |
| 132 | + assert.equal( |
| 133 | + read('sampleContainer/addingTriple.ttl'), |
| 134 | + '@prefix : </addingTriple.ttl#>.\n\n:current :temp 123 .\n\n:test :hello 456 .\n\n' |
| 135 | + ); |
| 136 | + rm('sampleContainer/addingTriple.ttl'); |
| 137 | + done(err); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should add value to existing triple', function (done) { |
| 142 | + write( |
| 143 | + '<#current> <#temp> 123 .', |
| 144 | + 'sampleContainer/addingTripleValue.ttl' |
| 145 | + ); |
| 146 | + server.post('/addingTripleValue.ttl') |
| 147 | + .set('content-type', 'application/sparql-update') |
| 148 | + .send('INSERT DATA { :current :temp 456 .}') |
| 149 | + .expect(200) |
| 150 | + .end(function (err, res) { |
| 151 | + assert.equal( |
| 152 | + read('sampleContainer/addingTripleValue.ttl'), |
| 153 | + '@prefix : </addingTripleValue.ttl#>.\n\n:current :temp 123, 456 .\n\n' |
| 154 | + ); |
| 155 | + rm('sampleContainer/addingTripleValue.ttl'); |
| 156 | + done(err); |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should add value to same subject', function (done) { |
| 161 | + write( |
| 162 | + '<#current> <#temp> 123 .', |
| 163 | + 'sampleContainer/addingTripleSubj.ttl' |
| 164 | + ); |
| 165 | + server.post('/addingTripleSubj.ttl') |
| 166 | + .set('content-type', 'application/sparql-update') |
| 167 | + .send('INSERT DATA { :current :temp2 456 .}') |
| 168 | + .expect(200) |
| 169 | + .end(function (err, res) { |
| 170 | + assert.equal( |
| 171 | + read('sampleContainer/addingTripleSubj.ttl'), |
| 172 | + '@prefix : </addingTripleSubj.ttl#>.\n\n:current :temp 123; :temp2 456 .\n\n' |
| 173 | + ); |
| 174 | + rm('sampleContainer/addingTripleSubj.ttl'); |
| 175 | + done(err); |
| 176 | + }); |
| 177 | + }); |
| 178 | + }); |
| 179 | + |
| 180 | + it('nothing should change with empty patch', function (done) { |
| 181 | + write( |
| 182 | + '<#current> <#temp> 123 .', |
| 183 | + 'sampleContainer/emptyExample.ttl' |
| 184 | + ); |
| 185 | + server.post('/emptyExample.ttl') |
| 186 | + .set('content-type', 'application/sparql-update') |
| 187 | + .send('') |
| 188 | + .expect(200) |
| 189 | + .end(function (err, res) { |
| 190 | + assert.equal( |
| 191 | + read('sampleContainer/emptyExample.ttl'), |
| 192 | + '@prefix : </emptyExample.ttl#>.\n\n:current :temp 123 .\n\n' |
| 193 | + ); |
| 194 | + rm('sampleContainer/emptyExample.ttl'); |
| 195 | + done(err); |
| 196 | + }); |
| 197 | + }); |
| 198 | +}); |
0 commit comments