|
4 | 4 |
|
5 | 5 | const Repo = require('../src/index') |
6 | 6 | const expect = require('chai').expect |
7 | | -const base58 = require('bs58') |
8 | | -const bl = require('bl') |
9 | | -const fs = require('fs') |
10 | | -const join = require('path').join |
11 | | - |
12 | | -const fileA = fs.readFileSync(join(__dirname, 'test-repo/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.data')) |
13 | | - |
14 | | -const fileAExt = fs.readFileSync(join(__dirname, 'test-repo/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.ext')) |
| 7 | +const Block = require('ipfs-block') |
15 | 8 |
|
16 | 9 | module.exports = function (repo) { |
17 | 10 | describe('IPFS Repo Tests', function () { |
@@ -138,127 +131,147 @@ module.exports = function (repo) { |
138 | 131 | }) |
139 | 132 |
|
140 | 133 | describe('datastore', function () { |
141 | | - const baseFileHash = 'QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVzxTt3qVe' |
142 | | - const baseExtFileHash = 'QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVzxTt3qVe' |
| 134 | + const helloKey = '1220b94d/1220b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9.data' |
| 135 | + const helloIpldKey = '1220b94d/1220b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9.ipld' |
143 | 136 |
|
144 | | - it('reads block', function (done) { |
145 | | - const buf = new Buffer(base58.decode(baseFileHash)) |
146 | | - repo.datastore.createReadStream(buf) |
147 | | - .pipe(bl((err, data) => { |
| 137 | + describe('.put', () => { |
| 138 | + it('simple', function (done) { |
| 139 | + const b = new Block('hello world') |
| 140 | + repo.datastore.put(b, (err, meta) => { |
148 | 141 | expect(err).to.not.exist |
149 | | - expect(data.equals(fileA)).to.equal(true) |
| 142 | + expect(meta.key).to.be.eql(helloKey) |
150 | 143 | done() |
151 | | - })) |
152 | | - }) |
| 144 | + }) |
| 145 | + }) |
153 | 146 |
|
154 | | - it('reads block, with custom extension', function (done) { |
155 | | - const buf = new Buffer(base58.decode(baseFileHash)) |
156 | | - repo.datastore.createReadStream(buf, 'ext') |
157 | | - .pipe(bl((err, data) => { |
158 | | - expect(err).to.not.exist |
159 | | - expect(data.equals(fileAExt)).to.equal(true) |
160 | | - done() |
161 | | - })) |
162 | | - }) |
| 147 | + it('multi write (locks)', (done) => { |
| 148 | + const b = new Block('hello world') |
163 | 149 |
|
164 | | - it('write a block', function (done) { |
165 | | - const rnd = 'QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVtesthash' |
166 | | - const mh = new Buffer(base58.decode(rnd)) |
167 | | - const data = new Buffer('Oh the data') |
| 150 | + let i = 0 |
| 151 | + const finish = () => { |
| 152 | + i++ |
| 153 | + if (i === 2) done() |
| 154 | + } |
168 | 155 |
|
169 | | - repo.datastore.createWriteStream(mh, (err, metadata) => { |
170 | | - expect(err).to.not.exist |
171 | | - expect(metadata.key).to.equal('12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07605f55537ce990.data') |
172 | | - done() |
173 | | - }).end(data) |
174 | | - }) |
| 156 | + repo.datastore.put(b, (err, meta) => { |
| 157 | + expect(err).to.not.exist |
| 158 | + expect(meta.key).to.equal(helloKey) |
| 159 | + finish() |
| 160 | + }) |
175 | 161 |
|
176 | | - it('write a block with custom extension', function (done) { |
177 | | - const rnd = 'QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVtesthash' |
178 | | - const mh = new Buffer(base58.decode(rnd)) |
179 | | - const data = new Buffer('Oh the data') |
| 162 | + repo.datastore.put(b, (err, meta) => { |
| 163 | + expect(err).to.not.exist |
| 164 | + expect(meta.key).to.equal(helloKey) |
| 165 | + finish() |
| 166 | + }) |
| 167 | + }) |
180 | 168 |
|
181 | | - repo.datastore.createWriteStream(mh, 'ext', (err, metadata) => { |
182 | | - expect(err).to.not.exist |
183 | | - expect(metadata.key).to.equal('12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07605f55537ce990.ext') |
184 | | - done() |
185 | | - }).end(data) |
| 169 | + it('custom extension', function (done) { |
| 170 | + const b = new Block('hello world', 'ipld') |
| 171 | + repo.datastore.put(b, (err, meta) => { |
| 172 | + expect(err).to.not.exist |
| 173 | + expect(meta.key).to.be.eql(helloIpldKey) |
| 174 | + done() |
| 175 | + }) |
| 176 | + }) |
| 177 | + |
| 178 | + it('returns an error on invalid block', (done) => { |
| 179 | + repo.datastore.put('hello', (err) => { |
| 180 | + expect(err.message).to.be.eql('Invalid block') |
| 181 | + done() |
| 182 | + }) |
| 183 | + }) |
186 | 184 | }) |
187 | 185 |
|
188 | | - it('write locks', (done) => { |
189 | | - const rnd = 'QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVtesthash' |
190 | | - const mh = new Buffer(base58.decode(rnd)) |
191 | | - const data = new Buffer('Oh the data') |
| 186 | + describe('.get', () => { |
| 187 | + it('simple', (done) => { |
| 188 | + const b = new Block('hello world') |
192 | 189 |
|
193 | | - let i = 0 |
194 | | - const finish = () => { |
195 | | - i++ |
196 | | - if (i === 2) done() |
197 | | - } |
| 190 | + repo.datastore.get(b.key, (err, data) => { |
| 191 | + expect(err).to.not.exist |
| 192 | + expect(data).to.be.eql(b) |
198 | 193 |
|
199 | | - repo.datastore.createWriteStream(mh, (err, metadata) => { |
200 | | - expect(err).to.not.exist |
201 | | - expect(metadata.key).to.equal('12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07605f55537ce990.data') |
202 | | - finish() |
203 | | - }).end(data) |
| 194 | + done() |
| 195 | + }) |
| 196 | + }) |
204 | 197 |
|
205 | | - repo.datastore.createWriteStream(mh, (err, metadata) => { |
206 | | - expect(err).to.not.exist |
207 | | - expect(metadata.key).to.equal('12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07605f55537ce990.data') |
208 | | - finish() |
209 | | - }).end(data) |
210 | | - }) |
| 198 | + it('custom extension', (done) => { |
| 199 | + const b = new Block('hello world', 'ipld') |
211 | 200 |
|
212 | | - it('block exists', function (done) { |
213 | | - const buf = new Buffer(base58.decode(baseFileHash)) |
| 201 | + repo.datastore.get(b.key, b.extension, (err, data) => { |
| 202 | + expect(err).to.not.exist |
| 203 | + expect(data).to.be.eql(b) |
214 | 204 |
|
215 | | - repo.datastore.exists(buf, (err, exists) => { |
216 | | - expect(err).to.not.exist |
217 | | - expect(exists).to.equal(true) |
218 | | - done() |
| 205 | + done() |
| 206 | + }) |
| 207 | + }) |
| 208 | + |
| 209 | + it('returns an error on invalid block', (done) => { |
| 210 | + repo.datastore.get(null, (err) => { |
| 211 | + expect(err.message).to.be.eql('Invalid key') |
| 212 | + done() |
| 213 | + }) |
219 | 214 | }) |
220 | 215 | }) |
221 | 216 |
|
222 | | - it('block exists, with custom extension', function (done) { |
223 | | - const buf = new Buffer(base58.decode(baseExtFileHash)) |
| 217 | + describe('.has', () => { |
| 218 | + it('existing block', (done) => { |
| 219 | + const b = new Block('hello world') |
224 | 220 |
|
225 | | - repo.datastore.exists(buf, 'ext', (err, exists) => { |
226 | | - expect(err).to.not.exist |
227 | | - expect(exists).to.equal(true) |
228 | | - done() |
| 221 | + repo.datastore.has(b.key, (err, exists) => { |
| 222 | + expect(err).to.not.exist |
| 223 | + expect(exists).to.equal(true) |
| 224 | + done() |
| 225 | + }) |
229 | 226 | }) |
230 | | - }) |
231 | 227 |
|
232 | | - it('check for non existent block', function (done) { |
233 | | - const buf = new Buffer('random buffer') |
| 228 | + it('with extension', (done) => { |
| 229 | + const b = new Block('hello world') |
234 | 230 |
|
235 | | - repo.datastore.exists(buf, (err, exists) => { |
236 | | - expect(err).to.not.exist |
237 | | - expect(exists).to.equal(false) |
238 | | - done() |
| 231 | + repo.datastore.has(b.key, 'data', (err, exists) => { |
| 232 | + expect(err).to.not.exist |
| 233 | + expect(exists).to.equal(true) |
| 234 | + done() |
| 235 | + }) |
239 | 236 | }) |
240 | | - }) |
241 | 237 |
|
242 | | - it('remove a block', function (done) { |
243 | | - const buf = new Buffer(base58.decode(baseFileHash)) |
244 | | - repo.datastore.remove(buf, (err) => { |
245 | | - expect(err).to.not.exist |
246 | | - repo.datastore.exists(buf, (err, exists) => { |
| 238 | + it('non existent block', (done) => { |
| 239 | + const b = new Block('wooot') |
| 240 | + |
| 241 | + repo.datastore.has(b.key, (err, exists) => { |
247 | 242 | expect(err).to.not.exist |
248 | 243 | expect(exists).to.equal(false) |
249 | 244 | done() |
250 | 245 | }) |
251 | 246 | }) |
252 | 247 | }) |
253 | 248 |
|
254 | | - it('remove a block, with custom extension', function (done) { |
255 | | - const buf = new Buffer(base58.decode(baseExtFileHash)) |
256 | | - repo.datastore.remove(buf, 'ext', (err) => { |
257 | | - expect(err).to.not.exist |
258 | | - repo.datastore.exists(buf, 'ext', (err, exists) => { |
| 249 | + describe('.delete', () => { |
| 250 | + it('simple', (done) => { |
| 251 | + const b = new Block('hello world') |
| 252 | + |
| 253 | + repo.datastore.delete(b.key, (err) => { |
259 | 254 | expect(err).to.not.exist |
260 | | - expect(exists).to.equal(false) |
261 | | - done() |
| 255 | + |
| 256 | + repo.datastore.has(b.key, (err, exists) => { |
| 257 | + expect(err).to.not.exist |
| 258 | + expect(exists).to.equal(false) |
| 259 | + done() |
| 260 | + }) |
| 261 | + }) |
| 262 | + }) |
| 263 | + |
| 264 | + it('custom extension', (done) => { |
| 265 | + const b = new Block('hello world', 'ipld') |
| 266 | + |
| 267 | + repo.datastore.delete(b.key, b.extension, (err) => { |
| 268 | + expect(err).to.not.exist |
| 269 | + |
| 270 | + repo.datastore.has(b.key, b.extension, (err, exists) => { |
| 271 | + expect(err).to.not.exist |
| 272 | + expect(exists).to.equal(false) |
| 273 | + done() |
| 274 | + }) |
262 | 275 | }) |
263 | 276 | }) |
264 | 277 | }) |
|
0 commit comments