From e338f4039aa4079da309a942bb822b097b340dfe Mon Sep 17 00:00:00 2001 From: haad Date: Thu, 9 Mar 2023 09:32:11 +0200 Subject: [PATCH] Add tests for dropping databases --- test/drop.test.js | 60 ------------------- test/orbitdb-drop.test.js | 117 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 60 deletions(-) delete mode 100644 test/drop.test.js create mode 100644 test/orbitdb-drop.test.js diff --git a/test/drop.test.js b/test/drop.test.js deleted file mode 100644 index 58dec96..0000000 --- a/test/drop.test.js +++ /dev/null @@ -1,60 +0,0 @@ -// import assert from 'assert' -// import fs from 'fs' -// import path from 'path' -// import rmrf from 'rimraf' -// import OrbitDB from '../src/OrbitDB.js' - -// // Include test utilities -// import { -// config, -// startIpfs, -// stopIpfs, -// testAPIs, -// } from 'orbit-db-test-utils' - -// const dbPath = './orbitdb/tests/drop' - -// Object.keys(testAPIs).forEach(API => { -// describe(`orbit-db - Drop Database (${API})`, function() { -// this.timeout(config.timeout) - -// let ipfsd, ipfs, orbitdb, db, address -// let localDataPath - -// before(async () => { -// rmrf.sync(dbPath) -// ipfsd = await startIpfs(API, config.daemon1) -// ipfs = ipfsd.api -// orbitdb = await OrbitDB.createInstance(ipfs, { directory: dbPath }) -// }) - -// after(async () => { -// if(orbitdb) -// await orbitdb.stop() - -// if (ipfsd) -// await stopIpfs(ipfsd) - -// rmrf.sync(dbPath) -// }) - -// describe('Drop', function() { -// before(async () => { -// db = await orbitdb.create('first', 'feed') -// localDataPath = path.join(dbPath) -// assert.equal(fs.existsSync(localDataPath), true) -// }) - -// it('removes local database cache', async () => { -// await db.drop() -// await db._cache.open() -// assert.equal(await db._cache.get(db.localHeadsPath), undefined) -// assert.equal(await db._cache.get(db.remoteHeadsPath), undefined) -// assert.equal(await db._cache.get(db.snapshotPath), undefined) -// assert.equal(await db._cache.get(db.queuePath), undefined) -// assert.equal(await db._cache.get(db.manifestPath), undefined) -// await db._cache.close() -// }) -// }) -// }) -// }) diff --git a/test/orbitdb-drop.test.js b/test/orbitdb-drop.test.js new file mode 100644 index 0000000..9ef8457 --- /dev/null +++ b/test/orbitdb-drop.test.js @@ -0,0 +1,117 @@ +import { strictEqual } from 'assert' +import rmrf from 'rimraf' +import * as IPFS from 'ipfs' +import { OrbitDB } from '../src/index.js' +import config from './config.js' + +describe('Drop databases', function () { + this.timeout(5000) + + let ipfs + let orbitdb1 + let db + + before(async () => { + ipfs = await IPFS.create({ ...config.daemon1, repo: './ipfs' }) + }) + + after(async () => { + if (ipfs) { + await ipfs.stop() + } + await rmrf('./orbitdb') + await rmrf('./ipfs') + }) + + describe('dropping a database', () => { + const amount = 10 + + before(async () => { + orbitdb1 = await OrbitDB({ ipfs, id: 'user1' }) + db = await orbitdb1.open('helloworld') + }) + + after(async () => { + if (db) { + await db.close() + } + if (orbitdb1) { + await orbitdb1.stop() + } + await rmrf('./orbitdb') + }) + + it('returns no entries in the database after dropping it', async () => { + for (let i = 0; i < amount; i++) { + await db.add('hello' + i) + } + + const before = await db.all() + strictEqual(before.length, amount) + + await db.drop() + + const after = await db.all() + strictEqual(after.length, 0) + }) + + it('returns no heads for the database oplog after dropping it', async () => { + for (let i = 0; i < amount; i++) { + await db.add('hello' + i) + } + + const before = await db.log.heads() + strictEqual(before.length, 1) + + await db.drop() + + const after = await db.log.heads() + strictEqual(after.length, 0) + }) + + it('returns no entries when a dropped database is opened again after closing', async () => { + for (let i = 0; i < amount; i++) { + await db.add('hello' + i) + } + + const before = await db.all() + strictEqual(before.length, amount) + + await db.drop() + await db.close() + + db = await orbitdb1.open('helloworld') + + const after = await db.all() + strictEqual(after.length, 0) + }) + }) + + describe('dropping an empty database', () => { + before(async () => { + orbitdb1 = await OrbitDB({ ipfs, id: 'user1' }) + db = await orbitdb1.open('helloworld') + }) + + after(async () => { + if (db) { + await db.drop() + await db.close() + } + if (orbitdb1) { + await orbitdb1.stop() + } + await rmrf('./orbitdb1') + }) + + it('doesn\'t error when dropping an empty database', async () => { + let err + try { + await db.drop() + } catch (e) { + err = e + } + strictEqual(err, undefined) + }) + }) +})