File: make-tests.js

package info (click to toggle)
node-rdf-canonize 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 328 kB
  • sloc: javascript: 2,218; makefile: 15; sh: 1
file content (67 lines) | stat: -rw-r--r-- 1,623 bytes parent folder | download | duplicates (2)
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
/**
 * Make tests for benchmarks.
 *
 * @author Dave Longley
 * @author David I. Lehn
 *
 * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved.
 */
/* eslint-disable indent */
(async () => {
const fs = require('fs');
const jsonld = require('../../jsonld.js');

const b1json = fs.readFileSync('./block-1.json');
const b1 = JSON.parse(b1json);

const loader = (url, callback) => {
  if(url === 'https://w3id.org/test/v1') {
    callback(null, {
      document: JSON.parse(fs.readFileSync('./test-v1.jsonld'))
    });
  }
  if(url === 'https://w3id.org/webledger/v1') {
    return callback(null, {
      document: JSON.parse(fs.readFileSync('./webledger-v1.jsonld'))
    });
  }
  return jsonld.loadDocument(url, callback);
};

async function bn(b, n) {
  console.log(`Make block-${n}-{in,out}.nq`);
  const data = JSON.parse(JSON.stringify(b));
  //console.log('nq0', data);

  const ev = data.event[0];
  data.event = [];
  //console.log('nq1', data);
  for(let i = 0; i < n; ++i) {
    // copy and change id
    const newEv = JSON.parse(JSON.stringify(ev));
    newEv.input[0].id = newEv.input[0].id + '-' + i;
    //console.log('push', newEv);
    data.event.push(newEv);
  }
  //console.log('nq2', data);

  const nq = await jsonld.toRDF(data, {
    format: 'application/n-quads',
    documentLoader: loader
  });
  fs.writeFileSync(`./block-${n}-in.nq`, nq);
  const can = await jsonld.canonize(data, {
    documentLoader: loader
  });
  fs.writeFileSync(`./block-${n}-out.nq`, can);
}

Promise.all([
  bn(b1, 1),
  bn(b1, 2),
  bn(b1, 10),
  bn(b1, 100),
  bn(b1, 1000)
]).catch(e => console.error(e));

})();