it('util.inspect(bone)', async function() { const post = await Post.findOne({ title: 'New Post' }); const result = require('util').inspect(post); expect(result).to.be.an('string'); expect(result).to.contain('Post'); expect(result).to.contain('New Post'); });
it('bone.toJSON()', async function() { const post = await Post.findOne({ title: 'New Post' }); const result = post.toJSON(); delete result.updatedAt; delete result.createdAt; expect(result).to.be.an('object'); expect(result.id).to.be.ok(); expect(result.title).to.equal('New Post'); expect(result.extra).to.eql({ versions: [2, 3] }); });
// the other difference between `bone.toJSON()` and `bone.toObject()` it('bone.toObject() prefers bone.attribute(name) over getter properties', async function() { const post = await Post.findOne({ title: 'New Post' }); const result = post.toObject(); expect(result).to.be.an('object'); expect(result.title).to.be('New Post'); });
it('Bone.join().count()', async function() { const query = Post.find({ title: ['Archangel Tyrael', 'New Post'] }); const count = await query.count(); const posts = await query.order('title').with('attachment'); expect(posts.length).to.equal(count); expect(posts[0]).to.be.a(Post); expect(posts[0].title).to.equal('Archangel Tyrael'); expect(posts[0].attachment).to.be(null); expect(posts[1].attachment).to.be.an(Attachment); expect(posts[1].attachment.postId).to.equal(posts[1].id); });
it('Bone.group() subquery', async function() { const posts = await Post.find({ id: Comment.select('articleId').from( Comment.select('articleId').group('articleId').count().having('count > 0') ) }).order('id').with('attachment'); expect(posts.length).to.be(2); expect(posts[0].title).to.eql('Archbishop Lazarus'); expect(posts[0].attachment).to.be.an(Attachment); expect(posts[0].attachment.id).to.be.ok(); expect(posts[1].title).to.eql('Archangel Tyrael'); expect(posts[1].attachment).to.be(null); });
describe('.defineBaseModel()', function() { it('should generate a valid basic model', function() { let model = Schema.defineBaseModel('table_name'); expect(model).to.be.a('string'); // remove module.exports model = model.replace('module.exports = ', ''); // The module should be valid structure let modelContent = JSON.parse(model); expect(modelContent).to.only.have.keys([ 'tableName', 'schema', '_fid' ]); expect(modelContent.tableName).to.eql('table_name'); let model2 = Schema.defineBaseModel('table_name', true); expect(model2).to.be.an('object'); }); });
const json = Bone.cast('{"test":1}', JSON); expect(json.test).to.eql(1); expect(json).to.be.an('object');