Skip to content

Commit

Permalink
updated test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
yawetse committed Mar 27, 2017
1 parent 03ebb29 commit 8803434
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- "6.2.0"
- "6.9.1"
env:
- CXX=g++-4.8
addons:
Expand Down Expand Up @@ -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:
Expand All @@ -42,4 +42,4 @@ git:
submodules: false

script:
- npm set progress=false && grunt test
- npm set progress=false && grunt test && grunt coveralls
3 changes: 2 additions & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];},*/
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lowkie",
"version": "0.1.0",
"version": "0.1.1",
"license": "MIT",
"main": "index.js",
"maintainers": [
Expand Down
1 change: 0 additions & 1 deletion test/mock/connecttestthrowaway.json

This file was deleted.

12 changes: 7 additions & 5 deletions test/unit/connect_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -46,19 +47,20 @@ 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) => {
expect(status).to.be.an('object');
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();
Expand Down
82 changes: 62 additions & 20 deletions test/unit/model_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 8803434

Please sign in to comment.