|
1 |
| -var fs = require('fs'); |
2 |
| -var path = require('path'); |
3 |
| -var tape = require('tape'); |
4 |
| -var level = require('level'); |
5 |
| -var hypercore = require('hypercore'); |
6 |
| - |
7 |
| -var Jawn = require('../'); |
8 |
| - |
9 |
| -tape('test file ingest', function (t) { |
10 |
| - |
11 |
| - var db = level('test.db'); |
12 |
| - var core = hypercore(db); |
13 |
| - |
14 |
| - var J = new Jawn(); |
15 |
| - |
16 |
| - var path = './test.csv'; |
17 |
| - |
18 |
| - var ws = J.import(core, path); |
19 |
| - |
20 |
| - var filecontents = fs.readFileSync(path) |
21 |
| - .toString() |
22 |
| - .split('\n') |
23 |
| - .filter(function (line) { |
24 |
| - return line !== ''; |
25 |
| - }); |
26 |
| - |
27 |
| - // get number of rows in test.csv |
28 |
| - var numrows = filecontents.length; |
29 |
| - |
30 |
| - ws.end(function () { |
31 |
| - |
32 |
| - var id = ws.id.toString('hex'); |
33 |
| - |
34 |
| - t.ok(ws.id, 'has id'); |
35 |
| - |
36 |
| - t.same(ws.blocks, numrows, 'there should be one row for each block'); |
37 |
| - |
38 |
| - // Now create a read stream with the same id and read the data |
39 |
| - var rs = core.createReadStream(id); |
40 |
| - |
41 |
| - var feedcontents = []; |
42 |
| - |
43 |
| - rs.on('data', function (data) { |
44 |
| - // gather feed contents |
45 |
| - feedcontents.push(data.toString()); |
46 |
| - }); |
47 |
| - |
48 |
| - rs.on('end', function () { |
49 |
| - t.same(filecontents, feedcontents, 'contents of feed should match file contents'); |
50 |
| - }); |
51 |
| - |
52 |
| - }); |
53 |
| - |
54 |
| - t.end(); |
55 |
| - |
56 |
| -}); |
| 1 | +var test = require('tape') |
| 2 | +var Jawn = require('../') |
| 3 | +var fs = require('fs') |
| 4 | + |
| 5 | +var j = new Jawn() |
| 6 | + |
| 7 | +test('import csv to jawn', function (t) { |
| 8 | + var data = fs.createReadStream('./test/sample/sample.csv') |
| 9 | + var ws = j.import(data) |
| 10 | + var expected = [ |
| 11 | + {'Type of Experience': 'Writing software in any programming language', 'Little/No Experience': 1, 'Some Experience': 5, 'Very Familiar': 4}, |
| 12 | + {'Type of Experience': 'Frontend Web Development', 'Little/No Experience': 4, 'Some Experience': 3, 'Very Familiar': 3}, |
| 13 | + {'Type of Experience': 'Server-side (backend) Web Development', 'Little/No Experience': 4, 'Some Experience': 4, 'Very Familiar': 2}, |
| 14 | + {'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} |
| 15 | + ] |
| 16 | + ws.on('end', function () { |
| 17 | + var feedId = ws.id.toString('hex') |
| 18 | + var rs = j.core.createReadStream(feedId) |
| 19 | + |
| 20 | + rs.on('data', function (block) { |
| 21 | + t.same(block.toString(), expected.shift(), 'block matches imported line') |
| 22 | + }) |
| 23 | + |
| 24 | + var blocks = j.core.get(feedId).blocks |
| 25 | + |
| 26 | + t.same(blocks, 4, 'correct number of blocks returned') |
| 27 | + t.end() |
| 28 | + }) |
| 29 | +}) |
0 commit comments