test: should update json file

This commit is contained in:
Typicode
2016-05-15 16:31:48 +02:00
parent 2ce4590e93
commit 71b6c95b4a
5 changed files with 28 additions and 10 deletions

View File

@ -28,7 +28,7 @@ module.exports = function (source, cb) {
} else if (is.JSON(source)) {
data = low(source, { storage: fileAsync }).state()
data = low(source, { storage: fileAsync }).getState()
cb(null, data)
} else {

View File

@ -24,7 +24,7 @@ module.exports = function (source) {
var db
if (_.isObject(source)) {
db = low()
db.state(source)
db.setState(source)
} else {
db = low(source, { storage: fileAsync })
}
@ -45,7 +45,7 @@ module.exports = function (source) {
// GET /db
function showDatabase (req, res, next) {
res.locals.data = db.state()
res.locals.data = db.getState()
next()
}

View File

@ -246,7 +246,7 @@ module.exports = function (db, name) {
var resource = db.get(name).removeById(utils.toNative(req.params.id)).value()
// Remove dependents documents
var removable = db._.getRemovable(db.state())
var removable = db._.getRemovable(db.getState())
_.each(removable, function (item) {
db.get(item.name).removeById(item.id).value()

View File

@ -31,8 +31,15 @@ describe('cli', function () {
beforeEach(function () {
rmrf.sync(tmpDir)
fs.mkdirSync(tmpDir)
fs.writeFileSync(dbFile, JSON.stringify({ posts: [{ 'id': 1, '_id': 2 }] }))
fs.writeFileSync(routesFile, JSON.stringify({ '/blog/': '/' }))
fs.writeFileSync(dbFile, JSON.stringify({
posts: [
{ id: 1 },
{_id: 2 }
]
}))
fs.writeFileSync(routesFile, JSON.stringify({
'/blog/': '/'
}))
++PORT
request = supertest('http://localhost:' + PORT)
})
@ -49,7 +56,7 @@ describe('cli', function () {
serverReady(PORT, done)
})
it('should support JSON dbFile', function (done) {
it('should support JSON file', function (done) {
request.get('/posts').expect(200, done)
})
@ -62,6 +69,17 @@ describe('cli', function () {
.expect(200, done)
})
it('should update JSON file', function (done) {
request.post('/posts')
.send({ title: 'hello' })
.end(function () {
var str = fs.readFileSync(dbFile, 'utf8')
setTimeout(function () {
assert(str.indexOf('hello') !== -1)
done()
}, 1000)
})
})
})
describe('seed.js', function () {

View File

@ -556,7 +556,7 @@ describe('Server', function () {
describe('Database state', function () {
it('should be accessible', function () {
assert(router.db.state())
assert(router.db.getState())
})
})
@ -614,7 +614,7 @@ describe('Server', function () {
describe('router.db._.id', function (done) {
beforeEach(function () {
router.db.state({
router.db.getState({
posts: [
{ _id: 1 }
]
@ -627,7 +627,7 @@ describe('Server', function () {
request(server)
.get('/posts/1')
.expect('Content-Type', /json/)
.expect(router.db.state().posts[0])
.expect(router.db.getState().posts[0])
.expect(200, done)
})