Skip to content

Commit

Permalink
adding more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
yawetse committed Mar 27, 2017
1 parent 4670dd4 commit 90c227b
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 72 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
service_name: travis-pro
repo_token: zgpod0geOMNC14TXI2rEVKebvyveiVfMQ
2 changes: 1 addition & 1 deletion example/sampledb.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"filename":"/Users/yawetse/Developer/github/typesettin/other-projects/lowkie/example/sampledb.json","collections":[{"name":"kittens","data":[],"idIndex":[201,202,203,204,205,206,207],"binaryIndices":{},"constraints":null,"uniqueNames":[],"transforms":{},"objType":"kittens","dirty":true,"cachedIndex":null,"cachedBinaryIndex":null,"cachedData":null,"adaptiveBinaryIndices":true,"transactional":false,"cloneObjects":false,"cloneMethod":"parse-stringify","asyncListeners":false,"disableChangesApi":true,"autoupdate":false,"ttl":null,"maxId":207,"DynamicViews":[],"events":{"insert":[null],"update":[null],"pre-insert":[],"pre-update":[],"close":[],"flushbuffer":[],"error":[],"delete":[null],"warning":[null]},"changes":[]}],"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"}
{"filename":"/Users/yawetse/Developer/github/typesettin/lowkie/example/sampledb.json","collections":[{"name":"kittens","data":[],"idIndex":[201,202,208,209],"binaryIndices":{},"constraints":null,"uniqueNames":[],"transforms":{},"objType":"kittens","dirty":true,"cachedIndex":null,"cachedBinaryIndex":null,"cachedData":null,"adaptiveBinaryIndices":true,"transactional":false,"cloneObjects":false,"cloneMethod":"parse-stringify","asyncListeners":false,"disableChangesApi":true,"autoupdate":false,"ttl":null,"maxId":209,"DynamicViews":[],"events":{"insert":[null],"update":[null],"pre-insert":[],"pre-update":[],"close":[],"flushbuffer":[],"error":[],"delete":[null],"warning":[null]},"changes":[]}],"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"}
2 changes: 2 additions & 0 deletions example/sampledb.json.0
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
{"name":"king0.2726792766487611","entitytype":"kat","_id":"fbd8080a9272ecaa15d1bb6d0f4b3314","meta":{"revision":0,"created":1490576236063,"version":0},"$loki":201}
{"name":"prince0.8735373934837287","entitytype":"kat","description":10983021,"_id":"d48e3dde06a8e758b6afa6bf2d103cb2","meta":{"revision":0,"created":1490576507732,"version":0},"$loki":202}
{"name":"queen0.8482004465847226","entitytype":"kat","description":10983021,"_id":"80a605aae9ecb2599b176f1e92dc64e8","meta":{"revision":0,"created":1490632925062,"version":0},"$loki":208}
{"name":"jester0.26873625459623796","entitytype":"kat","_id":"fdf9c37302d3a58538bea96d72584632","meta":{"revision":0,"created":1490632930071,"version":0},"$loki":209}
42 changes: 2 additions & 40 deletions lib/lowkie.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,6 @@ const lowkieSchema = require('./schema');
const lowkieModel = require('./model');
const lowkieConnect = require('./connect');
const lowkieProxyHandler = require('./lowkieProxyHandler');
/**
* lowkie ORM singleton class
*
* @class lowkie
*/
class lowkie {
/**
* Creates an instance of lowkie.
* @param {any} [options={}]
*
* @memberOf lowkie
*/
constructor(options = {}) {
this.config = Object.assign({
adapterType: 'file',
debug: false,
strictSchemas: true,
}, options);
this.connections = new Map();
this.db = undefined;
this.models = undefined;
this.connection = new events.EventEmitter();
this.model = lowkieModel.bind(this);
this.connect = lowkieConnect.bind(this);
this.Schema.Types = lowkieSchema.Types;
return new Proxy(this, lowkieProxyHandler.call(this));
}
/**
* creates lowkie schema, also includes helpers for document validations
*
* @param {object} scheme
* @returns instance of lowkieSchema
*
* @memberOf lowkie
*/
Schema(scheme) {
return new lowkieSchema(scheme, this);
}
}
const lowkieClass = require('./lowkieClass');

module.exports = new lowkie({});
module.exports = new lowkieClass({});
47 changes: 47 additions & 0 deletions lib/lowkieClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';
const events = require('events');
const lowkieSchema = require('./schema');
const lowkieModel = require('./model');
const lowkieConnect = require('./connect');
const lowkieProxyHandler = require('./lowkieProxyHandler');
/**
* lowkie ORM singleton class
*
* @class lowkie
*/
class lowkie {
/**
* Creates an instance of lowkie.
* @param {any} [options={}]
*
* @memberOf lowkie
*/
constructor(options = {}) {
this.config = Object.assign({
adapterType: 'file',
debug: false,
strictSchemas: true,
}, options);
this.connections = new Map();
this.db = undefined;
this.models = undefined;
this.connection = new events.EventEmitter();
this.model = lowkieModel.bind(this);
this.connect = lowkieConnect.bind(this);
this.Schema.Types = lowkieSchema.Types;
return new Proxy(this, lowkieProxyHandler.call(this));
}
/**
* creates lowkie schema, also includes helpers for document validations
*
* @param {object} scheme
* @returns instance of lowkieSchema
*
* @memberOf lowkie
*/
Schema(scheme) {
return new lowkieSchema(scheme, this);
}
}

module.exports = lowkie;
6 changes: 3 additions & 3 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class lowkieSchema {
this.lowkie = lowkieSingleton;
this.createDoc = this.createDocument.bind(this);
this.validNames.push('_id');
console.log(this.lowkie.config);
// console.log(this.lowkie.config);
// this.properties = properties;
return this;
}
Expand All @@ -39,7 +39,7 @@ class lowkieSchema {
if (newDoc[ key ]) {
if (typeof newDoc[ key ] === 'string' && this.scheme[ key ] === String) {
result[ key ] = newDoc[ key ].toString();
} else if (typeof newDoc[ key ] === 'boolean' && this.scheme[ key ] === Boolean) {
} else if (this.scheme[ key ] === Boolean) {
result[ key ] = (newDoc[ key ]) ? true : false;
} else if (this.scheme[ key ] === Number) {
result[ key ] = Number(newDoc[ key ]);
Expand All @@ -61,7 +61,7 @@ class lowkieSchema {
*
* @memberOf lowkieSchema
*/
insert(options) {
insert(options = {}) {
let lokiCollectionInsert = options.target;
let lowkieInstance = options.thisArg;
let lowkieDocument = options.argumentsList;
Expand Down
8 changes: 4 additions & 4 deletions test/unit/lowkie_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ const chai = require('chai');
const expect = require('chai').expect;
let lowkie = require('../../index');
let lowkieSchema = require('../../lib/schema');

let lowkieClass = require('../../lib/lowkieClass');

describe('lowkie', 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));
expect(lowkie)
.to.deep.equal(lowkie2)
.and.to.be.an.instanceof(lowkieClass);
});
it('should be implemented with configurable default settings', () => {
expect(Object.keys(lowkie.config).length).to.be.greaterThan(0);
Expand Down
160 changes: 136 additions & 24 deletions test/unit/schema_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,145 @@
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/schematestdb.json');
let lowkie = require('../../index');
let lowkieSchema = require('../../lib/schema');
let testUserSchemaScheme = {
name: String,
email: String,
active: Boolean,
age: Number,
profile: {
type: String,
default: 'no profile',
},
};
let testUserSchema;
let testUserModel;


describe('lowkie', 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));
});
it('should be implemented with configurable default settings', () => {
expect(Object.keys(lowkie.config).length).to.be.greaterThan(0);
});
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 have connection that emit events', () => {
expect(lowkie.connection).to.be.an.instanceof(events.EventEmitter);
});
// it('should export instance of lowkie class proxy', () => {
// expect(lowkie).to.be.an.instanceof(Proxy);
// });
describe('Schema', function () {
this.timeout(10000);
before('intialize lowkie instances', (done) => {
fs.removeSync(testSchemaDBPath);
lowkie.connect(testSchemaDBPath)
.then((/*db*/) => {
// console.log('connected schematestdb');
testUserSchema = lowkie.Schema(testUserSchemaScheme);
testUserModel = lowkie.model('testuser', testUserSchema);
// console.log({testUserSchema})
done();
})
.catch(done);
});
describe('Lowkie Schema', function () {
it('should be an instance of a lowkieSchema', function () {
expect(testUserSchema).to.be.an.instanceof(lowkieSchema)
.and.to.be.an('object');
expect(testUserSchema).and.to.have.property('createDoc');
expect(testUserSchema).and.to.have.property('insert');
});
it('should have a static property that exports Schema Types', () => {
expect(lowkieSchema.Types).to.be.an('object');
expect(lowkieSchema.Types.String).to.deep.equal(String);
});
});
describe('#LowkieSchema', () => {
it('should include _id in valid schema properties', () => {
expect(Object.keys(testUserSchemaScheme).concat([ '_id', ])).to.eql(testUserSchema.validNames);
});
});
describe('#createDoc', () => {
it('should always generate an Id', () => {
expect(testUserSchema.createDoc({})._id).to.be.an('string');
expect(Object.keys(testUserSchema.createDoc({})).length).to.eql(1);
});
it('should allow for custom Ids', () => {
let customId = '1234';
expect(testUserSchema.createDoc({ _id:customId, })._id).to.eql(customId);
});
it('should ignore invalid schema props', () => {
let newUser = testUserSchema.createDoc({
name: 'testuser',
email: 'user@domain.tld',
profile: 'mocha test',
active: true,
age: 18,
invalidprop: 'whatever',
});
expect(newUser).to.be.an('object');
expect(newUser.invalidprop).to.not.exist;
expect(Object.keys(newUser).length).to.eql(6);
expect(newUser._id).to.be.a('string');
});
it('should convert values to proper type', () => {
let newUser = testUserSchema.createDoc({
name: 'testuser',
email: 'user@domain.tld',
profile: 'mocha test',
active: "true",
age: "18",
invalidprop: 'whatever',
});
expect(newUser.active).to.be.a('boolean');
expect(newUser.age).to.be.a('number');
});
});
describe('#insert', () => {
it('should return a promise', () => {
expect(testUserSchema.insert()).to.be.an.instanceof(Promise);
});
it('should insert documents', (done) => {
testUserSchema.insert({
target: testUserModel.insert,
thisArg: lowkie,
argumentsList: {
name: 'testuser',
email: 'user@domain.tld',
profile: 'mocha test',
active: true,
age: 18,
invalidprop: 'whatever',
},
})
.then(newdoc => {
expect(newdoc).to.be.an('object');
done();
})
.catch(done);
});
it('should insert multiple documents', (done) => {
testUserSchema.insert({
target: testUserModel.insert,
thisArg: lowkie,
argumentsList: [{
name: 'testuser',
email: 'user@domain.tld',
profile: 'mocha test',
active: true,
age: 18,
invalidprop: 'whatever',
},
{
name: 'testuser2',
email: 'user2domain.tld',
profile: 'mocha test2',
active: false,
age: 19,
invalidprop: 'whatever',
}],
})
.then(newdocs => {
// console.log({newdocs})
expect(newdocs).to.be.an('array');
done();
})
.catch(done);
});
});
after('remove test schema db', () => {
fs.removeSync(testSchemaDBPath);
fs.removeSync(testSchemaDBPath + '.0');
});
});

0 comments on commit 90c227b

Please sign in to comment.