write more unit tests

master
garrettmills 4 years ago
parent 337f2f7d62
commit 903a7e6290
No known key found for this signature in database
GPG Key ID: 6ACD58D6ADACFC6E

@ -8,6 +8,7 @@
"license": "MIT",
"dependencies": {
"chai": "^4.2.0",
"dotenv": "^8.2.0",
"flitter-di": "^0.4.0",
"json-stringify-safe": "^5.0.1",
"mocha": "^7.1.0",

@ -52,8 +52,7 @@ class Model extends Injectable {
* @private
*/
static get __schema() {
if ( !this.__schema_instance ) this.__schema_instance = new Schema(this.schema)
return this.__schema_instance
return new Schema(this.schema)
}
/**

@ -1,4 +1,6 @@
const uuid = require('uuid/v4')
require('dotenv').config()
const mongo_host = process.env.TEST_MONGO_HOST || 'mongo'
module.exports = exports = {
test_connect: 'mongodb://mongo:27017/flitter_orm_test_'+uuid()
test_connect: 'mongodb://'+mongo_host+':27017/flitter_orm_test_'+uuid()
}

@ -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

@ -242,6 +242,11 @@ diff@^4.0.2:
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
dotenv@^8.2.0:
version "8.2.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==
emoji-regex@^7.0.1:
version "7.0.3"
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-7.0.3.tgz#933a04052860c85e83c122479c4748a8e4c72156"

Loading…
Cancel
Save