Skip to content

Commit fe946de

Browse files
Convert serialize tests to nock
1 parent ed98365 commit fe946de

9 files changed

Lines changed: 56 additions & 45 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"async": "^0.9.x",
3232
"jsonld": "^0.4.5",
3333
"n3": "^0.4.1",
34+
"node-fetch": "^1.7.1",
3435
"xmldom": "^0.1.22",
3536
"xmlhttprequest": "^1.7.0"
3637
},
@@ -40,7 +41,7 @@
4041
"babel-register": "^6.18.0",
4142
"babelify": "^7.3.0",
4243
"browserify": "^13.0.0",
43-
"chai": "^4.0.2",
44+
"chai": "^4.1.0",
4445
"diff": "^3.2.0",
4546
"dirty-chai": "^2.0.0",
4647
"fs-grep": "^0.0.5",

tests/serialize/data.js

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44
//
55
// See http://www.w3.org/TR/rdfa-syntax/ etc
66
//
7-
87
const $rdf = require('../../lib')
9-
var fs = require('fs')
8+
const fs = require('fs')
9+
const nock = require('nock')
1010

11-
var kb = $rdf.graph()
12-
var fetcher = $rdf.fetcher(kb)
11+
const kb = $rdf.graph()
12+
const fetcher = $rdf.fetcher(kb)
1313

1414
var contentType = 'text/turtle'
15-
var base = 'file://' + process.cwd() + '/'
16-
var uri
17-
var targetDocument = $rdf.sym(base + 'stdin') // defaul URI of test data
15+
var base = 'http://localhost/'
16+
var server
17+
var fileName
18+
var targetDocument // = $rdf.sym(base + 'stdin') // defaul URI of test data
19+
20+
var inDocument
21+
var outDocument
1822

1923
var check = function (ok, message, status) {
2024
if (!ok) {
@@ -50,7 +54,6 @@ var stackString = function (e) {
5054
return str
5155
}
5256

53-
5457
var exitMessage = function (message) {
5558
console.log(message)
5659
process.exit(4)
@@ -78,19 +81,24 @@ var doNext = function (remaining) {
7881
break
7982

8083
case '-in':
81-
targetDocument = $rdf.sym($rdf.uri.join(right, base))
82-
// console.log("Document is " + targetDocument)
83-
fetcher.nowOrWhenFetched(targetDocument, {}, function (ok, body, xhr) {
84+
let targetUri = $rdf.uri.join(right, base)
85+
inDocument = $rdf.sym(targetUri)
86+
let docContents = fs.readFileSync(right, 'utf8')
87+
88+
server = nock('http://localhost').get('/' + right).reply(200, docContents)
89+
90+
fetcher.nowOrWhenFetched(inDocument, {}, function (ok, body, xhr) {
8491
check(ok, body, xhr ? xhr.status : undefined)
85-
console.log('Loaded ' + targetDocument)
92+
// console.log(kb.statementsMatching().map(ea => ea.toString() + ' why:' + ea.why))
8693
doNext(remaining)
8794
}); // target, kb, base, contentType, callback
8895
return // STOP processing at this level
8996

9097
case '-out':
9198
try {
9299
var options = {flags: 'z'} // Only applies to RDF/XML
93-
var out = $rdf.serialize(targetDocument, kb, targetDocument.uri, contentType, undefined, options)
100+
101+
var out = $rdf.serialize(inDocument, kb, inDocument.uri, contentType, undefined, options)
94102
} catch(e) {
95103
exitMessage('Error in serializer: ' + e + stackString(e))
96104
}
@@ -99,11 +107,12 @@ var doNext = function (remaining) {
99107
doNext(remaining)
100108
return
101109
}
102-
doc = $rdf.sym($rdf.uri.join(right, base))
103-
if (doc.uri.slice(0, 7) !== 'file://') {
104-
exitMessage('Can only write files just now, sorry: ' + doc.uri)
105-
}
106-
var fileName = doc.uri.slice(7) //
110+
// doc = $rdf.sym($rdf.uri.join(right, base))
111+
// if (doc.uri.slice(0, 7) !== 'file://') {
112+
// exitMessage('Can only write files just now, sorry: ' + doc.uri)
113+
// }
114+
fileName = right
115+
107116
fs.writeFile(fileName, out, function (err) {
108117
if (err) {
109118
exitMessage('Error writing file <' + right + '> :' + err)
@@ -120,10 +129,11 @@ var doNext = function (remaining) {
120129
} catch(e) {
121130
exitMessage('Error in serializer: ' + e + stackString(e))
122131
}
123-
if (doc.uri.slice(0, 7) !== 'file://') {
124-
exitMessage('Can only write files just now, sorry: ' + doc.uri)
125-
}
126-
var fileName = doc.uri.slice(7) //
132+
// if (doc.uri.slice(0, 7) !== 'file://') {
133+
// exitMessage('Can only write files just now, sorry: ' + doc.uri)
134+
// }
135+
// fileName = doc.uri.slice(7) //
136+
fileName = doc.uri.split('/')[doc.uri.length - 1]
127137
fs.writeFile(fileName, out, function (err) {
128138
if (err) {
129139
exitMessage('Error writing file <' + right + '> :' + err)

tests/serialize/t10-ref.ttl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@prefix : <#>.
1+
@prefix : </details.ttl#>.
22
@prefix dc: <http://purl.org/dc/elements/1.1/>.
33
@prefix sch: <http://www.w3.org/ns/pim/schedule#>.
44
@prefix c: <https://www.w3.org/People/Berners-Lee/card#>.
@@ -26,7 +26,7 @@
2626
:id1442316628781;
2727
sch:option :id1442316021318, :id1442316021319;
2828
sch:ready "2015-09-15T11:20:21Z"^^XML:dateTime;
29-
sch:results <results.ttl>.
29+
sch:results </results.ttl>.
3030
:id1442316021316 n0:mbox "timbl@w3.org"; n0:name "Tim BL".
3131

3232
:id1442316021318 ic:dtstart "2016-01-13".

tests/serialize/t11-ref.xml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<rdf:RDF
22
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
3-
xmlns:str="#">
4-
<rdf:Description rdf:about="#MLK">
3+
xmlns:str="/structures.n3#">
4+
<rdf:Description rdf:about="/structures.n3#MLK">
55
<str:born rdf:datatype="http://www.w3.org/2001/XMLSchema#date">1929-01-17</str:born>
66
<str:died rdf:datatype="http://www.w3.org/2001/XMLSchema#date">1968-01-15</str:died>
77
<str:friend rdf:parseType="Resource">
@@ -15,45 +15,45 @@
1515
</rdf:Description>
1616
<rdf:Description rdf:nodeID="n5">
1717
<str:offspring rdf:parseType="Collection">
18-
<rdf:Description rdf:about="#MLK3">
18+
<rdf:Description rdf:about="/structures.n3#MLK3">
1919
<str:name>Martin Luther King III</str:name>
2020
</rdf:Description>
21-
<rdf:Description rdf:about="#DSK">
21+
<rdf:Description rdf:about="/structures.n3#DSK">
2222
<str:name>Dexter Scott King</str:name>
2323
</rdf:Description>
24-
<rdf:Description rdf:about="#YK">
24+
<rdf:Description rdf:about="/structures.n3#YK">
2525
<str:name>Yolanda King</str:name>
2626
</rdf:Description>
2727
<rdf:Description>
2828
<str:name>Bernice King</str:name>
2929
</rdf:Description>
3030
</str:offspring>
31-
<str:spouse rdf:resource="#MLK"/>
31+
<str:spouse rdf:resource="/structures.n3#MLK"/>
3232
<str:spouse rdf:nodeID="n4"/>
33-
<rdf:type rdf:resource="#Marriage"/>
33+
<rdf:type rdf:resource="/structures.n3#Marriage"/>
3434
</rdf:Description>
35-
<rdf:Description rdf:about="#MLK3">
35+
<rdf:Description rdf:about="/structures.n3#MLK3">
3636
<str:name>Martin Luther King III</str:name>
3737
</rdf:Description>
38-
<rdf:Description rdf:about="#DSK">
38+
<rdf:Description rdf:about="/structures.n3#DSK">
3939
<str:name>Dexter Scott King</str:name>
4040
</rdf:Description>
41-
<rdf:Description rdf:about="#YK">
41+
<rdf:Description rdf:about="/structures.n3#YK">
4242
<str:name>Yolanda King</str:name>
4343
</rdf:Description>
4444
<rdf:Description rdf:nodeID="n9">
45-
<str:friend rdf:resource="#MLK"/>
45+
<str:friend rdf:resource="/structures.n3#MLK"/>
4646
</rdf:Description>
4747
<rdf:Description rdf:nodeID="n10">
4848
<str:loves rdf:parseType="Resource"></str:loves>
4949
</rdf:Description>
5050
<rdf:Description rdf:nodeID="n12">
51-
<str:author rdf:resource="#MLK"/>
51+
<str:author rdf:resource="/structures.n3#MLK"/>
5252
<str:date rdf:datatype="http://www.w3.org/2001/XMLSchema#date">1963-08-23</str:date>
5353
<str:title>I have a dream</str:title>
54-
<rdf:type rdf:resource="#Speech"/>
54+
<rdf:type rdf:resource="/structures.n3#Speech"/>
5555
</rdf:Description>
56-
<rdf:Description rdf:about="#ZooReading1">
56+
<rdf:Description rdf:about="/structures.n3#ZooReading1">
5757
<str:boolDisabled rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">0</str:boolDisabled>
5858
<str:boolEnabled rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">1</str:boolEnabled>
5959
<str:date rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2015-02-28</str:date>

tests/serialize/t12-ref.ttl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@prefix : <#>.
1+
@prefix : </structures.n3#>.
22
@prefix XML: <http://www.w3.org/2001/XMLSchema#>.
33

44
:DSK :name "Dexter Scott King".

tests/serialize/t13-ref.ttl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
@prefix : <#>.
1+
@prefix : </,structures.nt#>.
22
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
3-
@prefix str: <structures.n3#>.
3+
@prefix str: </structures.n3#>.
44
@prefix XML: <http://www.w3.org/2001/XMLSchema#>.
55

66
str:DSK str:name "Dexter Scott King".

tests/serialize/t4-ref.ttl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@prefix : <#>.
1+
@prefix : </t3.ttl#>.
22
@prefix n0: <https://example.net/67890#>.
33
@prefix n1: <https://example.net/88888#>.
44
@prefix n2: <https://example.org/67890#>.

tests/serialize/t5-ref.ttl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@prefix : <#>.
1+
@prefix : </t5.n3#>.
22
@prefix owl: <http://www.w3.org/2002/07/owl#>.
33

44
:subj a :TestCase; owl:sameAs :other.

tests/serialize/t6-ref.n3

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@prefix : <#>.
1+
@prefix : </t5.n3#>.
22

33
:subj a :TestCase; = :other.
44

0 commit comments

Comments
 (0)