-
-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
269 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters