diff --git a/packages/database-ql/tests/units/doc/create.test.js b/packages/database-ql/tests/units/doc/create.test.js new file mode 100644 index 0000000000..b0057e61ef --- /dev/null +++ b/packages/database-ql/tests/units/doc/create.test.js @@ -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') + }) + }) +}) \ No newline at end of file diff --git a/packages/database-ql/tests/units/doc/get.test.js b/packages/database-ql/tests/units/doc/get.test.js new file mode 100644 index 0000000000..0052ffb60d --- /dev/null +++ b/packages/database-ql/tests/units/doc/get.test.js @@ -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 }) + }) +}) \ No newline at end of file diff --git a/packages/database-ql/tests/units/doc/update.test.js b/packages/database-ql/tests/units/doc/update.test.js new file mode 100644 index 0000000000..3aa3ad3e9d --- /dev/null +++ b/packages/database-ql/tests/units/doc/update.test.js @@ -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) + }) +}) \ No newline at end of file diff --git a/packages/database-ql/tests/units/get/field.js b/packages/database-ql/tests/units/get/field.js new file mode 100644 index 0000000000..50fce59194 --- /dev/null +++ b/packages/database-ql/tests/units/get/field.js @@ -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 }) + }) +}) \ No newline at end of file diff --git a/packages/database-ql/tests/units/get/get.test.js b/packages/database-ql/tests/units/get/get.test.js new file mode 100644 index 0000000000..27d861e515 --- /dev/null +++ b/packages/database-ql/tests/units/get/get.test.js @@ -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) + }) +}) \ No newline at end of file diff --git a/packages/database-ql/tests/units/get/limit.skip.test.js b/packages/database-ql/tests/units/get/limit.skip.test.js new file mode 100644 index 0000000000..128bd5d524 --- /dev/null +++ b/packages/database-ql/tests/units/get/limit.skip.test.js @@ -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 }) + }) +}) \ No newline at end of file diff --git a/packages/database-ql/tests/units/get/orderby.test.js b/packages/database-ql/tests/units/get/orderby.test.js new file mode 100644 index 0000000000..46b1100de1 --- /dev/null +++ b/packages/database-ql/tests/units/get/orderby.test.js @@ -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' }) + }) +}) \ No newline at end of file diff --git a/packages/database-ql/tests/units/get.test.js b/packages/database-ql/tests/units/get/where.test.js similarity index 59% rename from packages/database-ql/tests/units/get.test.js rename to packages/database-ql/tests/units/get/where.test.js index e3aada088e..d1295ab5c0 100644 --- a/packages/database-ql/tests/units/get.test.js +++ b/packages/database-ql/tests/units/get/where.test.js @@ -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') @@ -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()