|
|
|
@ -6,6 +6,7 @@ const Connection = require('../../src/services/Connection')
|
|
|
|
|
const { Cursor } = require('mongodb')
|
|
|
|
|
|
|
|
|
|
const Model = require('../../src/model/Model')
|
|
|
|
|
const Schema = require('../../src/schema/Schema')
|
|
|
|
|
|
|
|
|
|
describe('the base model class', function() {
|
|
|
|
|
let di
|
|
|
|
@ -18,7 +19,10 @@ describe('the base model class', function() {
|
|
|
|
|
|
|
|
|
|
class TestModel extends Model {
|
|
|
|
|
static get schema() {
|
|
|
|
|
return { num: Number, name: String }
|
|
|
|
|
return {
|
|
|
|
|
num: Number,
|
|
|
|
|
name: String
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Test = TestModel
|
|
|
|
@ -68,7 +72,6 @@ describe('the base model class', function() {
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// TODO describe the cursor method
|
|
|
|
|
describe('the static cursor method', function() {
|
|
|
|
|
it('should be a function', function() {
|
|
|
|
|
expect(Test.cursor).to.be.a('function')
|
|
|
|
@ -91,9 +94,6 @@ describe('the base model class', function() {
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// TODO describe the find method
|
|
|
|
|
|
|
|
|
|
// TODO describe the from_cursor method
|
|
|
|
|
describe('the static from_cursor method', function() {
|
|
|
|
|
let fake_cursor
|
|
|
|
|
let fake_arrays
|
|
|
|
@ -119,7 +119,79 @@ describe('the base model class', function() {
|
|
|
|
|
|
|
|
|
|
it('should return an array of instances from the cursor data', async function() {
|
|
|
|
|
const ret = await Test.from_cursor(fake_cursor)
|
|
|
|
|
expect(ret.length).to.be.equal(2)
|
|
|
|
|
expect(ret[0].num).to.be.equal(fake_arrays[0].num)
|
|
|
|
|
expect(ret[0].name).to.be.equal(fake_arrays[0].name)
|
|
|
|
|
expect(ret[1].num).to.be.equal(fake_arrays[1].num)
|
|
|
|
|
expect(ret[1].name).to.be.equal(fake_arrays[1].name)
|
|
|
|
|
expect(ret[0]).to.be.an.instanceof(Test)
|
|
|
|
|
expect(ret[1]).to.be.an.instanceof(Test)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// TODO describe the find method
|
|
|
|
|
describe('the find method', function() {
|
|
|
|
|
function check_range(input, min = 1, max = 100) {
|
|
|
|
|
const should_have = (max - min) + 1
|
|
|
|
|
if ( input.length !== should_have ) return false
|
|
|
|
|
const does_have = Array(max+1).fill(false)
|
|
|
|
|
input.forEach((rec) => {
|
|
|
|
|
if (
|
|
|
|
|
rec.num <= max
|
|
|
|
|
&& rec.name === 'number '+rec.num
|
|
|
|
|
) does_have[rec.num] = true
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return does_have.slice(min).every(Boolean)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
beforeEach(async function() {
|
|
|
|
|
for ( let i = 1; i <= 100; i++ ) {
|
|
|
|
|
await scaffold.collection('TestModel').insertOne({
|
|
|
|
|
num: i,
|
|
|
|
|
name: 'number '+i
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
afterEach(async function() {
|
|
|
|
|
await scaffold.collection('TestModel').deleteMany()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should be a function', function() {
|
|
|
|
|
expect(Test.find).to.be.a('function')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should return a promise', async function() {
|
|
|
|
|
const p = Test.find()
|
|
|
|
|
expect(p).to.be.a('promise')
|
|
|
|
|
await p
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should resolve to instances of the model', async function() {
|
|
|
|
|
const recs = await Test.find()
|
|
|
|
|
expect(recs).to.be.an('array')
|
|
|
|
|
expect(recs[0]).to.be.an.instanceof(Test)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should return all instances of the model by default', async function() {
|
|
|
|
|
const recs = await Test.find()
|
|
|
|
|
expect(check_range(recs)).to.be.true
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should respect passed in filters', async function() {
|
|
|
|
|
let recs = await Test.find({num: 5})
|
|
|
|
|
expect(recs.length).to.be.equal(1)
|
|
|
|
|
expect(check_range(recs, 5, 5)).to.be.true
|
|
|
|
|
|
|
|
|
|
recs = await Test.find({num: {$gt: 5}})
|
|
|
|
|
expect(check_range(recs, 6)).to.be.true
|
|
|
|
|
|
|
|
|
|
recs = await Test.find({num: {$in: [44, 45, 46, 47, 48, 49]}})
|
|
|
|
|
expect(check_range(recs, 44, 49)).to.be.true
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// TODO it should respect scopes applied to the model
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// TODO describe the findById method
|
|
|
|
|