Skip to content

Commit 8e30075

Browse files
test: Add tests for extension handling
1 parent 582f513 commit 8e30075

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
test/test-repo-for*
2+
13
# Logs
24
logs
35
*.log

test/node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const IPFSRepo = require('../src')
1212
describe('IPFS Repo Tests on on Node.js', () => {
1313
const testRepoPath = path.join(__dirname, 'test-repo')
1414
const date = Date.now().toString()
15-
const repoPath = testRepoPath + date
15+
const repoPath = testRepoPath + '-for-' + date
1616

1717
before((done) => {
1818
ncp(testRepoPath, repoPath, (err) => {

test/repo-test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const fileA = isNode
1313
? fs.readFileSync(process.cwd() + '/test/test-repo/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.data')
1414
: require('buffer!./test-repo/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.data')
1515

16+
const fileAExt = isNode
17+
? fs.readFileSync(process.cwd() + '/test/test-repo/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.ext')
18+
: require('buffer!./test-repo/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.ext')
19+
1620
module.exports = function (repo) {
1721
describe('IPFS Repo Tests', function () {
1822
it('check if Repo exists', (done) => {
@@ -109,6 +113,7 @@ module.exports = function (repo) {
109113

110114
describe('datastore', function () {
111115
const baseFileHash = 'QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVzxTt3qVe'
116+
const baseExtFileHash = 'QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVzxTt3qVe'
112117

113118
it('reads block', function (done) {
114119
const buf = new Buffer(base58.decode(baseFileHash))
@@ -120,6 +125,16 @@ module.exports = function (repo) {
120125
}))
121126
})
122127

128+
it('reads block, with custom extension', function (done) {
129+
const buf = new Buffer(base58.decode(baseFileHash))
130+
repo.datastore.createReadStream(buf, 'ext')
131+
.pipe(bl((err, data) => {
132+
expect(err).to.not.exist
133+
expect(data.equals(fileAExt)).to.equal(true)
134+
done()
135+
}))
136+
})
137+
123138
it('write a block', function (done) {
124139
const rnd = 'QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVtesthash'
125140
const mh = new Buffer(base58.decode(rnd))
@@ -132,6 +147,18 @@ module.exports = function (repo) {
132147
}).end(data)
133148
})
134149

150+
it('write a block with custom extension', function (done) {
151+
const rnd = 'QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVtesthash'
152+
const mh = new Buffer(base58.decode(rnd))
153+
const data = new Buffer('Oh the data')
154+
155+
repo.datastore.createWriteStream(mh, 'ext', (err, metadata) => {
156+
expect(err).to.not.exist
157+
expect(metadata.key).to.equal('12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07605f55537ce990.ext')
158+
done()
159+
}).end(data)
160+
})
161+
135162
it('block exists', function (done) {
136163
const buf = new Buffer(base58.decode(baseFileHash))
137164

@@ -142,6 +169,16 @@ module.exports = function (repo) {
142169
})
143170
})
144171

172+
it('block exists, with custom extension', function (done) {
173+
const buf = new Buffer(base58.decode(baseExtFileHash))
174+
175+
repo.datastore.exists(buf, 'ext', (err, exists) => {
176+
expect(err).to.not.exist
177+
expect(exists).to.equal(true)
178+
done()
179+
})
180+
})
181+
145182
it('check for non existent block', function (done) {
146183
const buf = new Buffer('random buffer')
147184

@@ -163,6 +200,18 @@ module.exports = function (repo) {
163200
})
164201
})
165202
})
203+
204+
it('remove a block, with custom extension', function (done) {
205+
const buf = new Buffer(base58.decode(baseExtFileHash))
206+
repo.datastore.remove(buf, 'ext', (err) => {
207+
expect(err).to.not.exist
208+
repo.datastore.exists(buf, 'ext', (err, exists) => {
209+
expect(err).to.not.exist
210+
expect(exists).to.equal(false)
211+
done()
212+
})
213+
})
214+
})
166215
})
167216

168217
describe('datastore-legacy', () => {})

0 commit comments

Comments
 (0)