Skip to content

Commit

Permalink
feat(test): add doc() unit tests;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Oct 16, 2021
1 parent 5c28d8d commit 0481a8c
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 51 deletions.
50 changes: 50 additions & 0 deletions packages/database-ql/tests/units/doc/create.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

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

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

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

assert.strictEqual(req.action, Actions.add)
assert.strictEqual(req.params.collectionName, 'tasks')
assert.equal(req.params.multi, false)
assert.equal(req.params.query, undefined)

// validate data
assert.ok(req.params.data.uid.$oid)
assert.strictEqual(req.params.data.name, 'laf')
assert.ok(req.params.data.created_at.$date)
assert.ok(req.params.data.pic.$binary.base64)
assert.ok(req.params.data.pic.$binary.subType)
assert.equal(req.params.data.pic.$binary.base64, buf.toString('base64'))


assert.ok(!res.code)
assert.ok(res.insertedCount === 1)
assert.equal(res.id, '0')
})

it('create() with empty object should be rejected', async () => {
const { db, req } = getDb()

const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72])
await db.collection('tasks')
.doc('nonsense_id')
.create({})
.catch(err => {
assert.equal(err.toString(), 'Error: data cannot be empty object')
})
})
})
58 changes: 58 additions & 0 deletions packages/database-ql/tests/units/doc/get.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

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

describe('db::doc().get()', () => {
it('get() with string id should be ok', async () => {
const { db, req } = getDb()
const res = await db.collection('tasks')
.doc('test_id')
.get()

assert.strictEqual(req.action, Actions.get)
assert.strictEqual(req.params.collectionName, 'tasks')
assert.equal(req.params.limit, 1)
assert.deepEqual(req.params.query, { _id: 'test_id'})

assert.ok(!res.code)
assert.ok(res.data === null)
})

it('get() with ObjectId should be ok', async () => {
const { db, req } = getDb()

const id = new ObjectId()
const res = await db.collection('tasks')
.doc(id)
.get()

assert.deepEqual(req.params.query, { _id: { $oid: id.toHexString() }})
})

it('field().get() with array projection should be ok', async () => {
const { db, req } = getDb()

const res = await db.collection('tasks')
.doc('test_id')
.field(['name', 'age', 'gender'])
.get()

assert.deepEqual(req.params.query, { _id: 'test_id'})
assert.deepEqual(req.params.query, { _id: 'test_id' })
assert.deepEqual(req.params.projection, { age: 1, gender: 1, name: 1 })
})

it('field().get() with object projection should be ok', async () => {
const { db, req } = getDb()

const res = await db.collection('tasks')
.doc('test_id')
.field({ name: 0, age: 0, gender: 0 })
.get()

assert.deepEqual(req.params.query, { _id: 'test_id'})
assert.deepEqual(req.params.query, { _id: 'test_id' })
assert.deepEqual(req.params.projection, { age: 0, gender: 0, name: 0 })
})
})
38 changes: 38 additions & 0 deletions packages/database-ql/tests/units/doc/update.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

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

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

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

assert.strictEqual(req.action, Actions.update)
assert.strictEqual(req.params.collectionName, 'tasks')
assert.equal(req.params.multi, false)
assert.equal(req.params.merge, true)
assert.equal(req.params.upsert, false)
assert.deepEqual(req.params.query, { _id: 'test_id' })


// 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)
})
})
31 changes: 31 additions & 0 deletions packages/database-ql/tests/units/get/field.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { getDb } = require('../_utils')
const assert = require('assert')

describe('db::field()', () => {
it('field(value: string[]) should be ok', async () => {
const { db, req } = getDb()
const res = await db.collection('tasks')
.field(['name', 'gender'])
.get()

assert.deepEqual(req.params.projection, { name: 1, gender: 1 })
})

it('field(value: { [key: string]: 1 }) should be ok', async () => {
const { db, req } = getDb()
const res = await db.collection('tasks')
.field({ name: 1, gender: 1 })
.get()

assert.deepEqual(req.params.projection, { name: 1, gender: 1 })
})

it('field(value: { [key: string]: 0 }) should be ok', async () => {
const { db, req } = getDb()
const res = await db.collection('tasks')
.field({ name: 0, gender: 0 })
.get()

assert.deepEqual(req.params.projection, { name: 0, gender: 0 })
})
})
33 changes: 33 additions & 0 deletions packages/database-ql/tests/units/get/get.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

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

describe('db::get()', () => {
it('get() without query options should be ok', async () => {
const { db, req } = getDb()
const res = await db.collection('tasks')
.get()

assert.strictEqual(req.action, Actions.get)
assert.strictEqual(req.params.collectionName, 'tasks')
assert.equal(req.params.limit, 100)
assert.deepEqual(req.params.query, undefined)

assert.ok(!res.code)
assert.ok(res.data instanceof Array)
})

it('getOne() without query options should be ok', async () => {
const { db, req } = getDb()
const res = await db.collection('tasks')
.getOne()

assert.strictEqual(req.action, Actions.get)
assert.strictEqual(req.params.collectionName, 'tasks')
assert.equal(req.params.limit, 1)
assert.deepEqual(req.params.query, undefined)

assert.ok(!res.code)
assert.ok(res.data === null)
})
})
39 changes: 39 additions & 0 deletions packages/database-ql/tests/units/get/limit.skip.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { getDb } = require('../_utils')
const assert = require('assert')

describe('db::limit() & skip()', () => {
it('get() default limit is 100 should be ok', async () => {
const { db, req } = getDb()
const res = await db.collection('tasks')
.get()

assert.equal(req.params.limit, 100)
})

it('limit().skip() should be ok', async () => {
const { db, req } = getDb()
const res = await db.collection('tasks')
.limit(33)
.skip(77)
.get()

assert.strictEqual(req.params.limit, 33)
assert.strictEqual(req.params.offset, 77)
})

it('where({ filters }) with limit() skip() should be ok', async () => {
const { db, req } = getDb()
const res = await db.collection('tasks')
.where({
name: 'laf',
status: 1
})
.limit(555)
.skip(999)
.get()

assert.strictEqual(req.params.limit, 555)
assert.strictEqual(req.params.offset, 999)
assert.deepEqual(req.params.query, { name: 'laf', status: 1 })
})
})
16 changes: 16 additions & 0 deletions packages/database-ql/tests/units/get/orderby.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { getDb } = require('../_utils')
const assert = require('assert')

describe('db::orderBy()', () => {
it('orderBy() should be ok', async () => {
const { db, req } = getDb()
const res = await db.collection('tasks')
.orderBy('age', 'asc')
.orderBy('score', 'desc')
.get()

assert.ok(req.params.order.length === 2)
assert.deepEqual(req.params.order[0], { field: 'age', direction: 'asc' })
assert.deepEqual(req.params.order[1], { field: 'score', direction: 'desc' })
})
})
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@

const { Db } = require('../../dist/commonjs')
const { MockRequest, Actions } = require('./_utils')

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

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

describe('db::get()', () => {
it('get() without query options should be ok', async () => {
const { db, req } = getDb()
const res = await db.collection('tasks')
.get()

assert.strictEqual(req.action, Actions.get)
assert.strictEqual(req.params.collectionName, 'tasks')
assert.equal(req.params.limit, 100)
assert.deepEqual(req.params.query, undefined)

assert.ok(!res.code)
assert.ok(res.data instanceof Array)
})

describe('db::where()', () => {
it('where({}) should be ok', async () => {
const { db, req } = getDb()
const res = await db.collection('tasks')
Expand All @@ -47,33 +27,6 @@ describe('db::get()', () => {
assert.deepEqual(req.params.query, { name: 'laf', status: 1 })
})

it('limit() skip() should be ok', async () => {
const { db, req } = getDb()
const res = await db.collection('tasks')
.limit(33)
.skip(77)
.get()

assert.strictEqual(req.params.limit, 33)
assert.strictEqual(req.params.offset, 77)
})

it('where({ filters }) with limit() skip() should be ok', async () => {
const { db, req } = getDb()
const res = await db.collection('tasks')
.where({
name: 'laf',
status: 1
})
.limit(555)
.skip(999)
.get()

assert.strictEqual(req.params.limit, 555)
assert.strictEqual(req.params.offset, 999)
assert.deepEqual(req.params.query, { name: 'laf', status: 1 })
})

it('where() with date type should be ok', async () => {
const { db, req } = getDb()
const current = new Date()
Expand Down

0 comments on commit 0481a8c

Please sign in to comment.