2022-01-13 17:02:55 +00:00
|
|
|
import { Eltro as t, assert, stub } from 'eltro'
|
|
|
|
import fs from 'fs/promises'
|
2022-01-11 16:51:15 +00:00
|
|
|
import lowdb from '../core/db.mjs'
|
2022-01-13 17:02:55 +00:00
|
|
|
import Util from '../core/util.mjs'
|
|
|
|
|
|
|
|
var util = new Util(import.meta.url)
|
|
|
|
var logger = {
|
|
|
|
info: stub(),
|
2022-01-18 11:48:35 +00:00
|
|
|
warn: stub(),
|
2022-01-13 17:02:55 +00:00
|
|
|
error: stub(),
|
|
|
|
}
|
|
|
|
|
|
|
|
t.before(function() {
|
|
|
|
return fs.rm('./test/db_test.json')
|
|
|
|
.catch(function() {})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.afterEach(function() {
|
|
|
|
return fs.rm('./test/db_test.json')
|
|
|
|
.catch(function() { })
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('Should auto create file with some defaults', async function() {
|
|
|
|
await assert.isRejected(fs.stat('./test/db_test.json'))
|
|
|
|
|
2022-01-21 02:43:48 +00:00
|
|
|
let db = await lowdb({}, util, logger, 'db_test.json')
|
2022-01-13 17:02:55 +00:00
|
|
|
|
|
|
|
let stat = await fs.stat('./test/db_test.json')
|
|
|
|
assert.ok(stat.size > 0)
|
|
|
|
|
|
|
|
assert.ok(db.data.core)
|
2022-01-18 11:48:35 +00:00
|
|
|
assert.ok(db.data.core.version)
|
|
|
|
assert.notOk(db.data.core.app)
|
|
|
|
assert.notOk(db.data.core.manager)
|
|
|
|
})
|
|
|
|
|
2022-01-21 02:43:48 +00:00
|
|
|
t.test('Should map config to config', async function() {
|
|
|
|
const assertConfig = { a: 1 }
|
|
|
|
|
|
|
|
let db = await lowdb(assertConfig, util, logger, 'db_test.json')
|
|
|
|
|
|
|
|
assert.strictEqual(db.config, assertConfig)
|
|
|
|
})
|
|
|
|
|
2022-01-18 11:48:35 +00:00
|
|
|
t.test('Should apply defaults to existing file', async function() {
|
|
|
|
await assert.isRejected(fs.stat('./test/db_test.json'))
|
|
|
|
await fs.writeFile('./test/db_test.json', '{"test":true}')
|
|
|
|
|
2022-01-21 02:43:48 +00:00
|
|
|
let db = await lowdb({}, util, logger, 'db_test.json')
|
2022-01-18 11:48:35 +00:00
|
|
|
|
|
|
|
let stat = await fs.stat('./test/db_test.json')
|
|
|
|
assert.ok(stat.size > 0)
|
|
|
|
|
|
|
|
assert.ok(db.data.core)
|
|
|
|
assert.ok(db.data.core.version)
|
|
|
|
assert.strictEqual(db.data.test, true)
|
|
|
|
assert.notOk(db.data.core.app)
|
|
|
|
assert.notOk(db.data.core.manager)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('Should not fail if db is invalid', async function() {
|
|
|
|
logger.warn.reset()
|
|
|
|
await assert.isRejected(fs.stat('./test/db_test.json'))
|
|
|
|
await fs.writeFile('./test/db_test.json', '[]')
|
|
|
|
|
2022-01-21 02:43:48 +00:00
|
|
|
let db = await lowdb({}, util, logger, 'db_test.json')
|
2022-01-18 11:48:35 +00:00
|
|
|
|
|
|
|
let stat = await fs.stat('./test/db_test.json')
|
|
|
|
assert.ok(stat.size > 0)
|
|
|
|
|
|
|
|
assert.ok(db.data.core)
|
|
|
|
assert.ok(db.data.core.version)
|
|
|
|
assert.notOk(db.data.core.app)
|
|
|
|
assert.notOk(db.data.core.manager)
|
|
|
|
assert.ok(logger.warn.called)
|
|
|
|
assert.match(logger.warn.firstCall[0], /db_test.json/i)
|
|
|
|
assert.match(logger.warn.firstCall[0], /clear/i)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('Should support adding an application with defaults', async function() {
|
|
|
|
await assert.isRejected(fs.stat('./test/db_test.json'))
|
|
|
|
|
2022-01-21 02:43:48 +00:00
|
|
|
let db = await lowdb({}, util, logger, 'db_test.json')
|
2022-01-18 11:48:35 +00:00
|
|
|
|
|
|
|
let stat = await fs.stat('./test/db_test.json')
|
|
|
|
assert.ok(stat.size > 0)
|
|
|
|
|
|
|
|
assert.ok(db.data.core)
|
|
|
|
assert.ok(db.data.core.version)
|
|
|
|
assert.notOk(db.data.core.app)
|
|
|
|
assert.notOk(db.data.core.manager)
|
|
|
|
|
|
|
|
db.addApplication('app')
|
|
|
|
|
2022-01-13 17:02:55 +00:00
|
|
|
assert.ok(db.data.core.app)
|
|
|
|
assert.ok(db.data.core.app.versions)
|
2022-01-18 11:48:35 +00:00
|
|
|
assert.strictEqual(db.data.core.app.active, null)
|
|
|
|
assert.strictEqual(db.data.core.app.latestInstalled, null)
|
|
|
|
assert.strictEqual(db.data.core.app.latestVersion, null)
|
|
|
|
|
|
|
|
assert.notOk(db.data.core.herpderp)
|
|
|
|
|
|
|
|
db.addApplication('herpderp')
|
|
|
|
|
|
|
|
assert.ok(db.data.core.herpderp)
|
|
|
|
assert.ok(db.data.core.herpderp.versions)
|
|
|
|
assert.strictEqual(db.data.core.herpderp.active, null)
|
|
|
|
assert.strictEqual(db.data.core.herpderp.latestInstalled, null)
|
|
|
|
assert.strictEqual(db.data.core.herpderp.latestVersion, null)
|
2022-01-13 17:02:55 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('Should support reading from db', async function() {
|
|
|
|
const assertValue = { a: 1 }
|
|
|
|
await assert.isRejected(fs.stat('./test/db_test.json'))
|
|
|
|
|
2022-01-21 02:43:48 +00:00
|
|
|
let db = await lowdb({}, util, logger, 'db_test.json')
|
2022-01-13 17:02:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
db.data.test = assertValue
|
|
|
|
await db.write()
|
|
|
|
|
|
|
|
assert.strictEqual(db.data.test, assertValue)
|
|
|
|
|
2022-01-21 02:43:48 +00:00
|
|
|
let dbSecondary = await lowdb({}, util, logger, 'db_test.json')
|
2022-01-13 17:02:55 +00:00
|
|
|
assert.notStrictEqual(dbSecondary.data.test, assertValue)
|
|
|
|
assert.deepStrictEqual(dbSecondary.data.test, assertValue)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should throw if unable to write to file', function() {
|
2022-01-21 02:43:48 +00:00
|
|
|
return assert.isRejected(lowdb({}, util, logger, '../test'))
|
2022-01-13 17:02:55 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should have basic database-like functions defined', async function() {
|
|
|
|
const assertItem1 = { a: 1 }
|
|
|
|
const assertItem2 = { a: 2 }
|
|
|
|
const assertItem3 = { a: 3 }
|
2022-01-21 02:43:48 +00:00
|
|
|
let db = await lowdb({}, util, logger, 'db_test.json')
|
2022-01-13 17:02:55 +00:00
|
|
|
assert.strictEqual(db.id, 'id')
|
|
|
|
|
|
|
|
db.data.myarr = []
|
|
|
|
db.upsert(db.data.myarr, assertItem1)
|
|
|
|
db.upsert(db.data.myarr, assertItem2)
|
|
|
|
|
|
|
|
assert.strictEqual(db.data.myarr.length, 2)
|
|
|
|
assert.ok(assertItem1.id)
|
|
|
|
assert.ok(assertItem2.id)
|
|
|
|
assert.notStrictEqual(assertItem1.id, assertItem2.id)
|
|
|
|
|
|
|
|
assert.strictEqual(db.get(db.data.myarr, assertItem1.id), assertItem1)
|
|
|
|
assert.strictEqual(db.get(db.data.myarr, assertItem2.id), assertItem2)
|
|
|
|
assert.strictEqual(db.get(db.data.myarr, assertItem2.id + 1), null)
|
|
|
|
|
|
|
|
await db.write()
|
|
|
|
|
2022-01-21 02:43:48 +00:00
|
|
|
let dbSec = await lowdb({}, util, logger, 'db_test.json')
|
2022-01-13 17:02:55 +00:00
|
|
|
|
|
|
|
assert.strictEqual(dbSec.data.myarr.length, 2)
|
|
|
|
assert.notStrictEqual(dbSec.get(dbSec.data.myarr, assertItem1.id), assertItem1)
|
|
|
|
assert.notStrictEqual(dbSec.get(dbSec.data.myarr, assertItem2.id), assertItem2)
|
|
|
|
assert.deepEqual(dbSec.get(dbSec.data.myarr, assertItem1.id), assertItem1)
|
|
|
|
assert.deepEqual(dbSec.get(dbSec.data.myarr, assertItem2.id), assertItem2)
|
|
|
|
|
|
|
|
dbSec.upsert(dbSec.data.myarr, assertItem3)
|
|
|
|
assert.strictEqual(dbSec.data.myarr.length, 3)
|
|
|
|
assert.ok(assertItem3.id)
|
|
|
|
assert.strictEqual(dbSec.get(dbSec.data.myarr, assertItem3.id), assertItem3)
|
|
|
|
assert.strictEqual(assertItem2.id + 1, assertItem3.id)
|
|
|
|
|
|
|
|
const assertItem4 = { a: 4, id: assertItem2.id }
|
|
|
|
dbSec.upsert(dbSec.data.myarr, assertItem4)
|
|
|
|
|
|
|
|
assert.strictEqual(dbSec.data.myarr.length, 3)
|
|
|
|
assert.notDeepEqual(dbSec.get(dbSec.data.myarr, assertItem2.id), assertItem2)
|
|
|
|
assert.deepEqual(dbSec.get(dbSec.data.myarr, assertItem2.id), assertItem4)
|
|
|
|
|
|
|
|
const assertItem5 = { a: 5, id: assertItem1.id }
|
|
|
|
dbSec.upsert(dbSec.data.myarr, assertItem5)
|
|
|
|
|
|
|
|
assert.strictEqual(dbSec.data.myarr.length, 3)
|
|
|
|
assert.notDeepEqual(dbSec.get(dbSec.data.myarr, assertItem1.id), assertItem1)
|
|
|
|
assert.deepEqual(dbSec.get(dbSec.data.myarr, assertItem1.id), assertItem5)
|
|
|
|
|
|
|
|
dbSec.remove(dbSec.data.myarr, assertItem2.id)
|
|
|
|
assert.strictEqual(dbSec.data.myarr.length, 2)
|
|
|
|
assert.strictEqual(dbSec.get(dbSec.data.myarr, assertItem2.id), null)
|
|
|
|
|
|
|
|
dbSec.remove(dbSec.data.myarr, assertItem1.id)
|
|
|
|
assert.strictEqual(dbSec.data.myarr.length, 1)
|
|
|
|
assert.strictEqual(dbSec.get(dbSec.data.myarr, assertItem1.id), null)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should have basic database-like functions with string-like name of collection', async function() {
|
|
|
|
const assertItem1 = { a: 1 }
|
|
|
|
const assertItem2 = { a: 2 }
|
|
|
|
const assertItem3 = { a: 3 }
|
2022-01-21 02:43:48 +00:00
|
|
|
let db = await lowdb({}, util, logger, 'db_test.json')
|
2022-01-13 17:02:55 +00:00
|
|
|
assert.strictEqual(db.id, 'id')
|
|
|
|
|
|
|
|
db.data.myarr = []
|
|
|
|
db.upsert('myarr', assertItem1)
|
|
|
|
db.upsert('myarr', assertItem2)
|
|
|
|
|
|
|
|
assert.strictEqual(db.data.myarr.length, 2)
|
|
|
|
assert.ok(assertItem1.id)
|
|
|
|
assert.ok(assertItem2.id)
|
|
|
|
assert.notStrictEqual(assertItem1.id, assertItem2.id)
|
|
|
|
|
|
|
|
assert.strictEqual(db.get('myarr', assertItem1.id), assertItem1)
|
|
|
|
assert.strictEqual(db.get('myarr', assertItem2.id), assertItem2)
|
|
|
|
assert.strictEqual(db.get('myarr', assertItem2.id + 1), null)
|
|
|
|
|
|
|
|
await db.write()
|
|
|
|
|
2022-01-21 02:43:48 +00:00
|
|
|
let dbSec = await lowdb({}, util, logger, 'db_test.json')
|
2022-01-13 17:02:55 +00:00
|
|
|
|
|
|
|
assert.strictEqual(dbSec.data.myarr.length, 2)
|
|
|
|
assert.notStrictEqual(dbSec.get('myarr', assertItem1.id), assertItem1)
|
|
|
|
assert.notStrictEqual(dbSec.get('myarr', assertItem2.id), assertItem2)
|
|
|
|
assert.deepEqual(dbSec.get('myarr', assertItem1.id), assertItem1)
|
|
|
|
assert.deepEqual(dbSec.get('myarr', assertItem2.id), assertItem2)
|
|
|
|
|
|
|
|
dbSec.upsert('myarr', assertItem3)
|
|
|
|
assert.strictEqual(dbSec.data.myarr.length, 3)
|
|
|
|
assert.ok(assertItem3.id)
|
|
|
|
assert.strictEqual(dbSec.get('myarr', assertItem3.id), assertItem3)
|
|
|
|
assert.strictEqual(assertItem2.id + 1, assertItem3.id)
|
|
|
|
|
|
|
|
const assertItem4 = { a: 4, id: assertItem2.id }
|
|
|
|
dbSec.upsert('myarr', assertItem4)
|
|
|
|
|
|
|
|
assert.strictEqual(dbSec.data.myarr.length, 3)
|
|
|
|
assert.notDeepEqual(dbSec.get('myarr', assertItem2.id), assertItem2)
|
|
|
|
assert.deepEqual(dbSec.get('myarr', assertItem2.id), assertItem4)
|
|
|
|
|
|
|
|
const assertItem5 = { a: 5, id: assertItem1.id }
|
|
|
|
dbSec.upsert('myarr', assertItem5)
|
|
|
|
|
|
|
|
assert.strictEqual(dbSec.data.myarr.length, 3)
|
|
|
|
assert.notDeepEqual(dbSec.get('myarr', assertItem1.id), assertItem1)
|
|
|
|
assert.deepEqual(dbSec.get('myarr', assertItem1.id), assertItem5)
|
|
|
|
|
|
|
|
dbSec.remove('myarr', assertItem2.id)
|
|
|
|
assert.strictEqual(dbSec.data.myarr.length, 2)
|
|
|
|
assert.strictEqual(dbSec.get('myarr', assertItem2.id), null)
|
|
|
|
|
|
|
|
dbSec.remove('myarr', assertItem1.id)
|
|
|
|
assert.strictEqual(dbSec.data.myarr.length, 1)
|
|
|
|
assert.strictEqual(dbSec.get('myarr', assertItem1.id), null)
|
|
|
|
})
|