From 8803434b700225ee6144055b161d3f3b1ee45812 Mon Sep 17 00:00:00 2001 From: Yaw Joseph Etse Date: Mon, 27 Mar 2017 15:59:36 -0400 Subject: [PATCH] updated test coverage --- .gitignore | 1 + .travis.yml | 8 +-- lib/model.js | 3 +- package.json | 2 +- test/mock/connecttestthrowaway.json | 1 - test/unit/connect_spec.js | 12 +++-- test/unit/model_spec.js | 82 ++++++++++++++++++++++------- 7 files changed, 77 insertions(+), 32 deletions(-) delete mode 100644 test/mock/connecttestthrowaway.json diff --git a/.gitignore b/.gitignore index 5148e52..cbe8b3d 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ coverage # nyc test coverage .nyc_output +test.db.json # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt diff --git a/.travis.yml b/.travis.yml index aa69046..8ae8c43 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: node_js node_js: - - "6.2.0" + - "6.9.1" env: - CXX=g++-4.8 addons: @@ -29,10 +29,10 @@ notifications: skip_join: false before_script: - - npm install --skip_app_post_install=true + - npm install install: - - npm install grunt-cli istanbul -g + - npm install grunt-cli istanbul -g branches: only: @@ -42,4 +42,4 @@ git: submodules: false script: - - npm set progress=false && grunt test + - npm set progress=false && grunt test && grunt coveralls diff --git a/lib/model.js b/lib/model.js index 90a0e6d..bb0dc31 100644 --- a/lib/model.js +++ b/lib/model.js @@ -13,12 +13,13 @@ function model(model, Schema, collectionOptions = {}) { try { // console.log('this.connections.size', this.connections.size); if (!this.connections.size) throw new Error('There has to be an active lowkie connection before creating models, lowkie.connect is asynchronous'); + if (!model || typeof model !=='string') throw new Error('model name must be a valid string'); if (!(Schema instanceof lowkieSchema)) throw new Error(`${model} must be an instance of a lowkieSchema`); let modelName = pluralize(model); let existingCollection = this.db.getCollection(modelName); let modelProxy = (existingCollection) ? existingCollection - : this.db.addCollection(modelName,collectionOptions); + : this.db.addCollection(modelName, collectionOptions); let modelHandler = { /*get: function (target, name) {return target[ name ];},*/ }; diff --git a/package.json b/package.json index 15397c2..a2b2140 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lowkie", - "version": "0.1.0", + "version": "0.1.1", "license": "MIT", "main": "index.js", "maintainers": [ diff --git a/test/mock/connecttestthrowaway.json b/test/mock/connecttestthrowaway.json deleted file mode 100644 index 518eb5b..0000000 --- a/test/mock/connecttestthrowaway.json +++ /dev/null @@ -1 +0,0 @@ -{"filename":"/Users/yawetse/Developer/github/typesettin/lowkie/test/mock/connecttestthrowaway.json","collections":[],"databaseVersion":1.1,"engineVersion":1.1,"autosave":false,"autosaveInterval":5000,"autosaveHandle":null,"throttledSaves":true,"options":{"serializationMethod":"normal","destructureDelimiter":"$<\n"},"persistenceMethod":"fs","persistenceAdapter":null,"verbose":false,"events":{"init":[null],"loaded":[],"flushChanges":[],"close":[],"changes":[],"warning":[]},"ENV":"NODEJS"} diff --git a/test/unit/connect_spec.js b/test/unit/connect_spec.js index da44844..a0c79af 100644 --- a/test/unit/connect_spec.js +++ b/test/unit/connect_spec.js @@ -6,9 +6,10 @@ const chai = require('chai'); const fs = require('fs-extra'); const expect = require('chai').expect; const testConnectDBPath = path.join(__dirname, '../mock/connecttestdb.json'); +const removeTestDB = require('../util/removeTestDB'); let lowkie = require('../../index'); let lowkieConnectTest = require('../../lib/connect'); -const removeTestDB = require('../util/removeTestDB'); +let lowkieClass = require('../../lib/lowkieClass'); describe('Connect', function () { this.timeout(10000); @@ -46,7 +47,7 @@ describe('Connect', function () { done(); }); }); - it('should emit connected event once connected to an existing db filepath', () => { + it('should emit connected event once connected to an existing db filepath', (done) => { let lowkieConnect = lowkieConnectTest.bind(lowkie); lowkieConnect(testConnectDBPath); lowkie.connection.once('connected', (status) => { @@ -54,11 +55,12 @@ describe('Connect', function () { done(); }); }); - it('should emit connected event once connected to a new db filepath', () => { - let lowkieConnect = lowkieConnectTest.bind(lowkie); + it('should emit connected event once connected to a new db filepath', (done) => { + let newLOWKIE = new lowkieClass({}); + let lowkieConnect = lowkieConnectTest.bind(newLOWKIE); let throwawayfilepath = path.join(__dirname, '../mock/connecttestthrowaway.json'); lowkieConnect(throwawayfilepath); - lowkie.connection.once('connected', (status) => { + newLOWKIE.connection.once('connected', (status) => { expect(status).to.be.an('object'); removeTestDB(throwawayfilepath); done(); diff --git a/test/unit/model_spec.js b/test/unit/model_spec.js index 32f83c7..2519b4a 100644 --- a/test/unit/model_spec.js +++ b/test/unit/model_spec.js @@ -3,33 +3,75 @@ const path = require('path'); const events = require('events'); const chai = require('chai'); +const fs = require('fs-extra'); const expect = require('chai').expect; +const testSchemaDBPath = path.join(__dirname, '../mock/modeltestdb.json'); +const removeTestDB = require('../util/removeTestDB'); let lowkie = require('../../index'); -let lowkieSchema = require('../../lib/schema'); - +let lowkieClass = require('../../lib/lowkieClass'); +let testUserModelScheme = { + name: String, + email: String, + active: Boolean, + age: Number, + profile: { + type: String, + default: 'no profile', + }, +}; +let testUserModel; +// let testUserModel; describe('Model', function () { - describe('Represents a singleton module', function () { - it('should always reference the same instance of lowkie when required', function () { - let lowkie2 = require('../../index'); - expect(lowkie).to.deep.equal(lowkie2); - // expect([1, 2, 3].indexOf(5)).to.equal(-1 ); - // should.equal(-1, [1, 2, 3].indexOf(0)); + this.timeout(10000); + before('intialize lowkie instance', (done) => { + removeTestDB(testSchemaDBPath, false); + lowkie.connect(testSchemaDBPath) + .then((/*db*/) => { + // console.log('connected modeltestdb'); + // testUserModel = lowkie.Schema(testUserModelScheme); + // testUserModel = lowkie.model('testuser', testUserModel); + // console.log({testUserModel}) + done(); + }) + .catch(done); + }); + describe('Creation', function () { + it('should throw an error if creating a model before databse is loaded', function () { + let newLOWKIE = new lowkieClass({}); + expect(newLOWKIE.model.bind(newLOWKIE)).to.throw('There has to be an active lowkie connection before creating models, lowkie.connect is asynchronous'); }); - it('should be implemented with configurable default settings', () => { - expect(Object.keys(lowkie.config).length).to.be.greaterThan(0); + it('should throw an error if model name is not a valid string', function (done) { + let newLOWKIE = new lowkieClass({}); + let throwawayfilepath = path.join(__dirname, '../mock/modelthrowaway1.json'); + newLOWKIE.connect(throwawayfilepath) + .then(() => { + expect(newLOWKIE.model.bind(newLOWKIE)).to.throw('model name must be a valid string'); + removeTestDB(throwawayfilepath); + done() + }) + .catch(done); }); - it('should export schema types', () => { - expect(lowkie.Schema.Types).to.be.an('object'); - expect(lowkie.Schema.Types).to.have.property('String'); - expect(lowkie.Schema.Types.String).to.deep.equal(String); - expect(lowkie.Schema.Types).to.have.property('ObjectId'); + it('should throw an error if schema isnt an instance of lowkie schema', function (done) { + let newLOWKIE = new lowkieClass({}); + let throwawayfilepath = path.join(__dirname, '../mock/modelthrowaway2.json'); + let model = 'testmodel'; + newLOWKIE.connect(throwawayfilepath) + .then(() => { + expect(newLOWKIE.model.bind(newLOWKIE, model, { name:String,description:String, })).to.throw(`${model} must be an instance of a lowkieSchema`); + removeTestDB(throwawayfilepath); + done(); + }) + .catch(done); }); - it('should have connection that emit events', () => { - expect(lowkie.connection).to.be.an.instanceof(events.EventEmitter); + it('should register models globally', () => { + let goodModelSchema = lowkie.Schema(testUserModelScheme); + lowkie.model('goodModel', goodModelSchema); + console.log(lowkie.models); + expect(Object.keys(lowkie.models)).to.have.length.above(0); }); - // it('should export instance of lowkie class proxy', () => { - // expect(lowkie).to.be.an.instanceof(Proxy); - // }); + }); + after('remove test schema db', () => { + removeTestDB(testSchemaDBPath, true); }); }); \ No newline at end of file