|
1 | 1 | var test = require('tape')
|
| 2 | +var fs = require('fs') |
| 3 | +var path = require('path') |
2 | 4 | var Jawn = require('../')
|
3 | 5 | var memdb = require('memdb')
|
4 | 6 |
|
5 | 7 | test('import json to jawn', function (t) {
|
6 | 8 | 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"}') |
| 9 | + importFromFile(jawn, 'dummy.json', {'format': 'json'}, verify) |
| 10 | + var expected = [ |
| 11 | + '{"foo":"bar","name":"josie","age":"35"}', |
| 12 | + '{"foo":"baz","name":"eloise","age":"71"}', |
| 13 | + '{"foo":"baz","name":"francoise","age":"5"}' |
| 14 | + ] |
| 15 | + function verify (err, feedId) { |
| 16 | + if (err) { console.log(err) } |
| 17 | + var rs = jawn.core.createReadStream(feedId) |
| 18 | + rs.on('data', function (block) { |
| 19 | + t.same(block.toString(), expected.shift(), 'block matches imported line') |
| 20 | + }) |
| 21 | + t.same(jawn.core.get(feedId).blocks, 3, 'correct number of blocks returned') |
| 22 | + t.end() |
| 23 | + } |
| 24 | +}) |
17 | 25 |
|
18 |
| - // This should be expecting JSON objects, not strings. |
19 |
| - // temporarily expecting strings in order to hand off the code as-is |
| 26 | +test('import csv to jawn', function (t) { |
| 27 | + var jawn = freshJawn() |
| 28 | + importFromFile(jawn, 'sample.csv', {'format': 'csv'}, verify) |
20 | 29 | var expected = [
|
21 |
| - '{foo: "bar", name: "josie"}', |
22 |
| - '{foo: "baz", name: "eloise"}', |
23 |
| - '{foo: "baz", name: "francoise"}' |
| 30 | + '{"Type of Experience":"Writing software in any programming language","Little/No Experience":"1","Some Experience":"5","Very Familiar":"4"}', |
| 31 | + '{"Type of Experience":"Frontend Web Development","Little/No Experience":"4","Some Experience":"3","Very Familiar":"3"}', |
| 32 | + '{"Type of Experience":"Server-side (“backend”) Web Development","Little/No Experience":"4","Some Experience":"4","Very Familiar":"2"}', |
| 33 | + '{"Type of Experience":"Using Git to track changes and share code (add, commit, push, pull)","Little/No Experience":"2","Some Experience":"5","Very Familiar":"3"}' |
24 | 34 | ]
|
25 | 35 |
|
26 |
| - importStream.end(function () { |
27 |
| - var feedId = importStream.id.toString('hex') |
| 36 | + function verify (err, feedId) { |
| 37 | + if (err) { console.log(err) } |
28 | 38 | var rs = jawn.core.createReadStream(feedId)
|
29 | 39 | rs.on('data', function (block) {
|
30 | 40 | t.same(block.toString(), expected.shift(), 'block matches imported line')
|
31 | 41 | })
|
32 |
| - t.same(jawn.core.get(feedId).blocks, 3, 'correct number of blocks returned') |
| 42 | + t.same(jawn.core.get(feedId).blocks, 4, 'correct number of blocks returned') |
33 | 43 | t.end()
|
34 |
| - }) |
| 44 | + } |
35 | 45 | })
|
36 | 46 |
|
| 47 | +// helpers |
| 48 | + |
| 49 | +function fixture (name) { |
| 50 | + return path.join(__dirname, 'data', name) |
| 51 | +} |
| 52 | + |
37 | 53 | function freshJawn () {
|
38 | 54 | return new Jawn({db: memdb()})
|
39 | 55 | }
|
| 56 | + |
| 57 | +function importFromFile (jawn, file, opts, callback) { |
| 58 | + var importPipeline = jawn.createImportPipeline(opts, callback) |
| 59 | + var data = fs.createReadStream(fixture(file)) |
| 60 | + data.pipe(importPipeline) |
| 61 | +} |
0 commit comments