-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
118 lines (93 loc) · 3.01 KB
/
test.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict';
var assert = require('assert');
var mongoose = require('mongoose');
var async = require('async');
var through = require('through');
var mcsv = require('./');
var user;
function models() {
user = new mongoose.Schema({
username: String,
secret: { type : String, csv : false },
date: Date,
number: Number,
nested: {
foo: String,
secret: { type : String, csv : false },
},
arr: [{
}]
});
user.plugin(mcsv);
user = mongoose.model('user', user);
}
function test() {
var user1 = new user({
_id : '5605710130d525e34c6e6afb',
username: 'foobar',
secret: (~~(Math.random(0) * 1e7)).toString(16),
date: (new Date('Fri Sep 25 2015 11:56:30 GMT-0400 (EDT)')),
number: 1,
nested: {
foo: 'bar',
secret: (~~(Math.random(0) * 1e7)).toString(16),
},
arr: [{ 'foo' : 'bar' }]
});
// check api methods
assert.equal(typeof user.find().csv, 'function');
assert.equal(typeof user.csv_header, 'function');
assert.equal(typeof user1.toCSV, 'function');
// check api results
var header = '"_id","username","date","number","nested.foo"\n';
assert.equal(header, user.csv_header(), 'malformed CSV header');
var user1_csv = '"5605710130d525e34c6e6afb","foobar","Fri Sep 25 2015 11:56:30 GMT-0400 (EDT)","1","bar"\n';
assert.equal(user1_csv, user1.toCSV(), 'malformed CSV row');
// create documents
async.waterfall([
function(cb) {
user.remove(function(err) {
assert.equal(err, null, 'remove failed');
cb(err);
});
},
function(cb) {
user1.save(function(err, doc) {
assert.equal(err, null);
// ensure non CSV values exist on the document
assert(doc.arr instanceof Array);
assert(doc.secret);
assert(doc.nested.secret);
cb(err);
});
}, function(cb) {
var stream = through(function write(data) {
this.emit('data', data);
}, function end() {
this.emit('end');
});
var lines = [];
stream.on('data', function(data) {
lines.push(data);
}).on('end', function() {
assert.equal(lines.length, 2, 'wrong number of lines');
assert.equal(header, lines[0], 'malformed CSV header');
assert.equal(user1_csv, lines[1], 'malformed CSV row');
cb();
});
// call and invoke the stream
user.find().csv(stream);
}
], function(err) {
assert.equal(null, err);
if (!/node-dev$/.test(process.env._)) {
mongoose.disconnect();
process.exit(0);
}
});
}
// connect and test
mongoose.connect('mongodb://localhost/mcsv-test', function() {
models();
test();
});