-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.js
174 lines (137 loc) · 4.42 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
var test = require('tap').test
, seneca = require('seneca')
, importer = require('./')
, Joi = require('joi')
test('importing csv lines as entities', function(t) {
var s = seneca()
, pear = s.make('pear')
, instance = importer.entity(s, 'pear')
instance.end(new Buffer('name,price\nhello,200\n'))
instance.on('importCompleted', function() {
pear.list$({}, function(err, res) {
t.notOk(err, 'no error')
t.equal(res.length, 1, 'one result')
t.equal(res[0].name, 'hello', 'same name')
t.equal(res[0].price, '200', 'same price')
t.end()
})
})
})
test('skipping the first N rows for entities', function(t) {
var s = seneca()
, pear = s.make('pear')
, instance = importer.entity(s, 'pear', { skip: 1 })
instance.write(new Buffer('name,price\na,400\n'))
instance.end(new Buffer('hello,200\n'))
instance.on('importCompleted', function() {
pear.list$({}, function(err, res) {
t.notOk(err, 'no error')
t.equal(res.length, 1, 'one result')
t.equal(res[0].name, 'hello', 'same name')
t.equal(res[0].price, '200', 'same price')
t.end()
})
})
})
test('rowImported event', function(t) {
var s = seneca()
, pear = s.make('pear')
, instance = importer.entity(s, 'pear')
, rows = 0
instance.on('rowImported', function() {
rows++
})
instance.on('importCompleted', function() {
t.equal(rows, 2)
t.end()
})
instance.write(new Buffer('name,price\na,400\n'))
instance.end(new Buffer('hello,200\n'))
})
test('rowsImported property', function(t) {
var s = seneca()
, pear = s.make('pear')
, instance = importer.entity(s, 'pear')
instance.on('importCompleted', function() {
t.equal(instance.rowsImported, 2)
t.end()
})
instance.write(new Buffer('name,price\na,400\n'))
instance.end(new Buffer('hello,200\n'))
})
test('acting on every csv line', function(t) {
t.plan(1)
var s = seneca()
, pear = s.make('pear')
, pattern = { sample: 'call' }
, instance = importer.act(s, pattern)
, message = { sample: 'call', name: 'hello', price: '200' }
function check(arrived, done) {
delete arrived.actid$
t.deepEqual(arrived, message)
done(null)
}
s.add(pattern, check);
instance.end(new Buffer('name,price\nhello,200\n'))
})
test('skipping the first N rows for acting', function(t) {
t.plan(1)
var s = seneca()
, pear = s.make('pear')
, pattern = { sample: 'call' }
, instance = importer.act(s, pattern, { skip: 1 })
, message = { sample: 'call', name: 'hello', price: '200' }
function check(arrived, done) {
delete arrived.actid$
t.deepEqual(arrived, message)
done(null)
}
s.add(pattern, check);
instance.write(new Buffer('name,price\na,400\n'))
instance.end(new Buffer('hello,200\n'))
})
test('converting values', function(t) {
var s = seneca()
, event = s.make('event')
, schema = Joi.object().keys({
name: Joi.string()
, price: Joi.number().integer()
, date: Joi.date()
})
, instance = importer.entity(s, 'event', { schema: schema })
, now = new Date()
instance.write(new Buffer('name,price,date\n'))
instance.end(new Buffer('hello,200,' + now.toISOString()+ '\n'))
instance.on('importCompleted', function() {
event.list$({}, function(err, res) {
t.notOk(err, 'no error')
t.equal(res.length, 1, 'one result')
t.equal(res[0].name, 'hello', 'same name')
t.equal(res[0].price, 200, 'same price')
t.deepEqual(res[0].date, now, 'same date')
t.end()
})
})
})
test('stripping unknown keys', function(t) {
var s = seneca()
, event = s.make('event')
, schema = Joi.object().keys({
name: Joi.string()
, price: Joi.number().integer()
})
, instance = importer.entity(s, 'event', { schema: schema })
, now = new Date()
instance.write(new Buffer('name,price,date\n'))
instance.end(new Buffer('hello,200,' + now.toISOString()+ '\n'))
instance.on('importCompleted', function() {
event.list$({}, function(err, res) {
t.notOk(err, 'no error')
t.equal(res.length, 1, 'one result')
t.equal(res[0].name, 'hello', 'same name')
t.equal(res[0].price, 200, 'same price')
t.equal(res[0].date, undefined, 'no date')
t.end()
})
})
})