mirror of
https://github.com/typicode/json-server.git
synced 2025-07-30 13:42:11 +08:00
test: should update json file
This commit is contained in:
@ -28,7 +28,7 @@ module.exports = function (source, cb) {
|
|||||||
|
|
||||||
} else if (is.JSON(source)) {
|
} else if (is.JSON(source)) {
|
||||||
|
|
||||||
data = low(source, { storage: fileAsync }).state()
|
data = low(source, { storage: fileAsync }).getState()
|
||||||
cb(null, data)
|
cb(null, data)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
@ -24,7 +24,7 @@ module.exports = function (source) {
|
|||||||
var db
|
var db
|
||||||
if (_.isObject(source)) {
|
if (_.isObject(source)) {
|
||||||
db = low()
|
db = low()
|
||||||
db.state(source)
|
db.setState(source)
|
||||||
} else {
|
} else {
|
||||||
db = low(source, { storage: fileAsync })
|
db = low(source, { storage: fileAsync })
|
||||||
}
|
}
|
||||||
@ -45,7 +45,7 @@ module.exports = function (source) {
|
|||||||
|
|
||||||
// GET /db
|
// GET /db
|
||||||
function showDatabase (req, res, next) {
|
function showDatabase (req, res, next) {
|
||||||
res.locals.data = db.state()
|
res.locals.data = db.getState()
|
||||||
next()
|
next()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -246,7 +246,7 @@ module.exports = function (db, name) {
|
|||||||
var resource = db.get(name).removeById(utils.toNative(req.params.id)).value()
|
var resource = db.get(name).removeById(utils.toNative(req.params.id)).value()
|
||||||
|
|
||||||
// Remove dependents documents
|
// Remove dependents documents
|
||||||
var removable = db._.getRemovable(db.state())
|
var removable = db._.getRemovable(db.getState())
|
||||||
|
|
||||||
_.each(removable, function (item) {
|
_.each(removable, function (item) {
|
||||||
db.get(item.name).removeById(item.id).value()
|
db.get(item.name).removeById(item.id).value()
|
||||||
|
@ -31,8 +31,15 @@ describe('cli', function () {
|
|||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
rmrf.sync(tmpDir)
|
rmrf.sync(tmpDir)
|
||||||
fs.mkdirSync(tmpDir)
|
fs.mkdirSync(tmpDir)
|
||||||
fs.writeFileSync(dbFile, JSON.stringify({ posts: [{ 'id': 1, '_id': 2 }] }))
|
fs.writeFileSync(dbFile, JSON.stringify({
|
||||||
fs.writeFileSync(routesFile, JSON.stringify({ '/blog/': '/' }))
|
posts: [
|
||||||
|
{ id: 1 },
|
||||||
|
{_id: 2 }
|
||||||
|
]
|
||||||
|
}))
|
||||||
|
fs.writeFileSync(routesFile, JSON.stringify({
|
||||||
|
'/blog/': '/'
|
||||||
|
}))
|
||||||
++PORT
|
++PORT
|
||||||
request = supertest('http://localhost:' + PORT)
|
request = supertest('http://localhost:' + PORT)
|
||||||
})
|
})
|
||||||
@ -49,7 +56,7 @@ describe('cli', function () {
|
|||||||
serverReady(PORT, done)
|
serverReady(PORT, done)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should support JSON dbFile', function (done) {
|
it('should support JSON file', function (done) {
|
||||||
request.get('/posts').expect(200, done)
|
request.get('/posts').expect(200, done)
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -62,6 +69,17 @@ describe('cli', function () {
|
|||||||
.expect(200, done)
|
.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 () {
|
describe('seed.js', function () {
|
||||||
|
@ -556,7 +556,7 @@ describe('Server', function () {
|
|||||||
|
|
||||||
describe('Database state', function () {
|
describe('Database state', function () {
|
||||||
it('should be accessible', 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) {
|
describe('router.db._.id', function (done) {
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
router.db.state({
|
router.db.getState({
|
||||||
posts: [
|
posts: [
|
||||||
{ _id: 1 }
|
{ _id: 1 }
|
||||||
]
|
]
|
||||||
@ -627,7 +627,7 @@ describe('Server', function () {
|
|||||||
request(server)
|
request(server)
|
||||||
.get('/posts/1')
|
.get('/posts/1')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
.expect(router.db.state().posts[0])
|
.expect(router.db.getState().posts[0])
|
||||||
.expect(200, done)
|
.expect(200, done)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user