Skip to content

Commit

Permalink
feat(db-ql): add update unit tests;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Oct 16, 2021
1 parent 0481a8c commit aefd260
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,13 @@
"dockerode",
"EJSON",
"lafyun",
"objs",
"signin",
"uids",
"upsert",
"upserted",
"vitepress"
]
"vitepress",
"withs"
],
"mocha.subdirectory": "packages"
}
2 changes: 1 addition & 1 deletion packages/database-ql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"module": "dist/esm/index.js",
"scripts": {
"watch": "tsc -w",
"test": "mocha tests/units/*.test.js",
"test": "mocha tests/units/**/*.test.js",
"build": "tsc -p tsconfig.json && tsc -p tsconfig.esm.json",
"eslint": "eslint \"./**/*.ts\"",
"fix": "eslint --fix \"./**/*.ts\"",
Expand Down
21 changes: 17 additions & 4 deletions packages/database-ql/tests/units/_utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

const { Db } = require('../../dist/commonjs/index')
const Actions = {
add: 'database.addDocument',
get: 'database.queryDocument',
Expand Down Expand Up @@ -36,11 +36,15 @@ class MockRequest {
}

if (action === Actions.update) {
data = { updated: 0, upsert_id: '0' }
data = { updated: 1, upsert_id: '0' }
}

if (action === Actions.remove) {
data = { deleted: 0 }
data = { deleted: 1 }
}

if (action === Actions.add) {
data = { _id: '0', insertedCount: 1 }
}

return {
Expand All @@ -51,7 +55,16 @@ class MockRequest {
}
}


// mock db
function getDb() {
const req = new MockRequest()
const db = new Db({ request: req })
return { db, req }
}

module.exports = {
MockRequest,
Actions
Actions,
getDb
}
40 changes: 40 additions & 0 deletions packages/database-ql/tests/units/update/update.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

const { Actions, getDb } = require('../_utils')
const assert = require('assert')
const { ObjectId, Binary } = require('bson')

describe('db::update()', () => {
it('update() should be ok', async () => {
const { db, req } = getDb()

const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72])
const uid = new ObjectId()
const res = await db.collection('tasks')
.where({ name: 'laf' })
.update({
uid: uid,
created_at: new Date(),
pic: new Binary(buf)
})

assert.strictEqual(req.action, Actions.update)
assert.strictEqual(req.params.collectionName, 'tasks')

// check options
assert.equal(req.params.multi, false)
assert.equal(req.params.merge, true)
assert.equal(req.params.upsert, false)

// check query
assert.deepEqual(req.params.query, { name: 'laf' })

// check data
assert.deepEqual(req.params.data.$set.uid, { $oid: uid.toHexString() })
assert.ok(req.params.data.$set.created_at.$date)
assert.equal(req.params.data.$set.pic.$binary.base64, buf.toString('base64'))

// check result
assert.ok(!res.code)
assert.equal(res.updated,1)
})
})

0 comments on commit aefd260

Please sign in to comment.