|
| 1 | +var test = require('tape') |
| 2 | +var Jawn = require('../') |
| 3 | +var memdb = require('memdb') |
| 4 | + |
| 5 | +test('import json to jawn', function (t) { |
| 6 | + var jawn = freshJawn() |
| 7 | + var importStream = jawn.createImportStream({'format': 'json'}) |
| 8 | + // Imitate the stream that would come from reading sample.csv |
| 9 | + // importStream should parse the CSV correctly, identifying the first row as headers |
| 10 | + // This is the same as doing |
| 11 | + // var data = fs.createReadStream('./test/data/sample.csv') |
| 12 | + // data.pipe(importStream) |
| 13 | + // except the writes are being performed synchronously/inline so we can call importStream.end() after writing the contents into it. |
| 14 | + importStream.write('{foo: "bar", name: "josie"}') |
| 15 | + importStream.write('{foo: "baz", name: "eloise"}') |
| 16 | + importStream.write('{foo: "baz", name: "francoise"}') |
| 17 | + |
| 18 | + // This should be expecting JSON objects, not strings. |
| 19 | + // temporarily expecting strings in order to hand off the code as-is |
| 20 | + var expected = [ |
| 21 | + '{foo: "bar", name: "josie"}', |
| 22 | + '{foo: "baz", name: "eloise"}', |
| 23 | + '{foo: "baz", name: "francoise"}' |
| 24 | + ] |
| 25 | + |
| 26 | + importStream.end(function () { |
| 27 | + var feedId = importStream.id.toString('hex') |
| 28 | + var rs = jawn.core.createReadStream(feedId) |
| 29 | + rs.on('data', function (block) { |
| 30 | + t.same(block.toString(), expected.shift(), 'block matches imported line') |
| 31 | + }) |
| 32 | + t.same(jawn.core.get(feedId).blocks, 3, 'correct number of blocks returned') |
| 33 | + t.end() |
| 34 | + }) |
| 35 | +}) |
| 36 | + |
| 37 | +function freshJawn () { |
| 38 | + return new Jawn({db: memdb()}) |
| 39 | +} |
0 commit comments