-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
197 lines (153 loc) · 3.84 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict'
const join = require('psjoin')
const drain = require('psp-drain')
const collect = require('psp-collect')
const test = require('tape')
const {GridFSBucket, MongoClient, ObjectId, Binary} = require('mongodb')
const {pull, values} = require('pull-stream')
const muuid = require('mongo-uuid')
const uuid = (i) => muuid(Binary, i)
const client = new MongoClient(
'mongodb://localhost:' + process.env.MONGODB_PORT + '/pull_mongo_files_test'
)
const db = client.db()
const mfs = require('./')(GridFSBucket, db)
const nonceChunks = ['47', '61', '72', '66', '69', '65', '6c', '64'].map((nc) =>
Buffer.from(nc, 'hex')
)
const nonce = Buffer.concat(nonceChunks)
test.onFinish(() => db.dropDatabase().then(() => client.close()))
test('Write', (t) => {
t.test((t) => {
t.plan(2)
const id = new ObjectId()
const write = pull(values(nonceChunks), mfs.write(id)).then((err) =>
t.notOk(err)
)
join(id, write, mfs.read).then((file) => t.ok(file))
})
t.test('Aborting', (t) => {
t.plan(2)
const id = new ObjectId()
let count = 0
const write = pull((end, cb) => {
if (end !== null) return cb(end)
if (count++ < 5) return cb(null, Buffer.from('00', 'hex')) // write a byte
cb(new DummyError())
}, mfs.write(id)).catch((err) => t.ok(err))
join(id, write, (id) => pull(mfs.read(id), drain())).catch((err) =>
t.ok(err.message.includes('FileNotFound'))
)
})
t.test('With meta data', (t) => {
t.plan(2)
const id = new ObjectId()
const write = pull(
values(nonceChunks),
mfs.write(id, {
name: 'wallpaper.jpg',
created: new Date(),
})
).then((err) => t.notOk(err))
join(id, write, mfs.read).then((file) => t.ok(file))
})
})
test('Read', (t) => {
const id = new ObjectId()
const write = pull(values(nonceChunks), mfs.write(id))
t.test((t) => {
t.plan(1)
write.then(() =>
pull(mfs.read(id), collect()).then((cs) =>
t.ok(Buffer.concat(cs).equals(nonce))
)
)
})
t.test('Not found', (t) => {
t.plan(1)
pull(mfs.read(new ObjectId()), collect()).catch((err) =>
t.ok(err.message.includes('FileNotFound'))
)
})
})
test('Stat', (t) => {
const id = new ObjectId()
const write = pull(values(nonceChunks), mfs.write(id))
t.test((t) => {
t.plan(1)
join(id, write, mfs.stat).then((file) =>
t.deepEqual(file, {
id: id,
meta: {},
})
)
})
t.test('With meta data', (t) => {
t.plan(1)
const id = new ObjectId()
const name = 'wallpaper.jpg'
const created = new Date()
const write = pull(
values(nonceChunks),
mfs.write(id, {
name,
created,
})
)
join(id, write, mfs.stat).then((file) =>
t.deepEqual(file, {
id: id,
meta: {
name,
created,
},
})
)
})
t.test('Not found', (t) => {
t.plan(1)
mfs.stat(new ObjectId()).then((stat) => t.equal(stat, null))
})
})
test('Exists', (t) => {
const id = new ObjectId()
const write = pull(values(nonceChunks), mfs.write(id))
t.test((t) => {
t.plan(1)
join(id, write, mfs.exists).then((exists) => t.equal(exists, true))
})
t.test('Not found', (t) => {
t.plan(1)
mfs.exists(new ObjectId()).then((exists) => t.equal(exists, false))
})
})
test('Using UUIDs', (t) => {
const id = uuid()
const write = pull(values(nonceChunks), mfs.write(id))
t.test('Write', (t) => {
t.plan(1)
write.then((err) => t.notOk(err))
})
t.test('Read', (t) => {
t.plan(1)
write
.then(() => pull(mfs.read(id), collect()))
.then((chunks) => t.ok(Buffer.concat(chunks).equals(nonce)))
})
t.test('Stat', (t) => {
t.plan(1)
join(id, write, mfs.stat).then((file) =>
t.deepEqual(file, {
id: file.id,
meta: {},
})
)
})
t.test('Exists', (t) => {
t.plan(2)
join(id, write, mfs.exists).then((exists) => t.equal(exists, true))
mfs.exists(uuid()).then((exists) => t.equal(exists, false))
})
})
function DummyError() {}
DummyError.prototype = Object.create(Error.prototype)