Permalink
Browse files

tweaks to unit testing

  • Loading branch information...
bingomanatee committed Sep 5, 2012
1 parent 2b41de7 commit de14ca7c12c4de0e5ce2fc445974c72ce70c58c7
View
@@ -80,6 +80,10 @@ function _error(target, rs, err, props) {
props = {};
}
+ if (!rs){
+ throw new Error('no rs');
+ }
+
if (_DEBUG) {
console.log('target class: %s', target.CLASS);
console.log('rs class: %s', rs.CLASS);
@@ -92,8 +96,8 @@ function _error(target, rs, err, props) {
if (_DEBUG){ console.log('shifting parameters: error is now %s', err)}
}
- if ((!rs instanceof Req_State) && (!!rs instanceof Req_State_Mock)) {
- throw new Error('bad Req_State %s', util.inspect(rs, false, 0));
+ if ((!rs.CLASS == 'Req_State') && (!!rs.CLASS == 'Req_State_Mock')) {
+ throw new Error(util.format('bad Req_State ; class = %s', rs.CLASS));
}
if (_DEBUG) console.log('_error(target = %s, rs = %s, err = %s, props = %s) ----- this = %s',
@@ -153,8 +157,9 @@ function _console_log_error(target, rs, err, props) {
*/
function _error_response(rs, error, props) {
- if ((!rs instanceof Req_State) || (rs instanceof Req_State_Mock)) {
- throw new Error('bad Req_State %s', util.inspect(rs, false, 0));
+
+ if ((!rs.CLASS == 'Req_State') && (!!rs.CLASS == 'Req_State_Mock')) {
+ throw new Error(util.format('bad Req_State; class = %s', rs.CLASS));
}
if (_DEBUG) {
View
@@ -17,14 +17,14 @@ function Req_State_Mock(action, props) {
this._config = props.config ? props.config : {};
this.flash_msgs = [];
this.go_places = [];
- this.CLASS = 'REQ_STATE_MOCK'
+ this.CLASS = 'Req_State_Mock'
}
_.extend(Req_State_Mock.prototype , _session);
_.extend(Req_State_Mock.prototype, _validation);
_.extend(Req_State_Mock.prototype , {
- 'CLASS': 'REQ_STATE_MOCK',
+ 'CLASS': 'Req_State_Mock',
get_config: function(key){
return this._config[key];
View
@@ -6,83 +6,97 @@ var async = require('async');
/* ***************** CLOSURE ************* */
-var MAX_LOG = 500;
-var FLUSH_TIME = 5000;
var _DEBUG = false;
-var _log = [];
var _log_path = false;
-var _logger;
-var _flush_timeout;
+var _write_buffer = false;
/* ***************** MODULE *********** */
module.exports = {
- set_log_file: function(file, params){
+ set_log_file:function (file, params) {
- if (params && params.reset){
- if (fs.existsSync(file)){
+ _log_path = file;
+ if (!fs.existsSync(file)){
+ throw new Error('bad log file passed: %s', file);
+ }
+ if (params && params.reset) {
+ if (fs.existsSync(file)) {
_log_path = file;
- fs.unlink(_log_path)
+ try {
+
+ fs.unlink(_log_path, function (err) {
+ if (err) {
+ throw err;
+ }
+ })
+ } catch (err) {
+ console.log('error unlinking: %s', err.message);
+ }
}
}
_log_path = file;
},
- _flush_log: function(){
- try {
- if (_log_path){
- var h = fs.createWriteStream(_log_path, {flags: 'w+', encoding: 'utf8'});
- _log.forEach(function(item){
- h.write("\r\n" + item.time + ': ' + item.task);
- })
- h.end();
+ log_to_obj: function(cb){
+ fs.readFile(_log_path, 'utf8', function(err, log){
+ log= util.format('[%s{"task": "end"}]', log);
+ try {
+ cb(null, JSON.parse(log));
+ } catch(err){
+ cb(null, log);
}
- } catch(err){
- console.log('cannot write to log %s: %s', _log_path, util.inspect(err));
- }
+ });
+ },
- delete _log;
- _log = [];
- if (_flush_timeout) {
- clearTimeout(_flush_timeout);
+ log:function (task, content) {
+ if (!_log_path) {
+ return;
}
- },
- start_flush_timer: function(){
- if (!_flush_timeout){
- _flush_timeout = setTimeout(this._flush_log, FLUSH_TIME);
+ var time = new Date().getTime();
+
+ var msg = {
+ time: time,
+ task: task
}
- },
- log:function (task, content) {
- if (_.isString(task)) {
- if (content) {
- task = {msg:task, content:content};
- } else {
- task = {msg:task};
+ if (content){
+ msg.content = content;
+ }
+ try {
+ var m = JSON.stringify(msg);
+ var _write_buffer = fs.createWriteStream(_log_path, {encoding:'utf8', flags:'a+'});
+ _write_buffer.write(m + ",\n");
+ _write_buffer.end();
+ } catch(err){
+ // console.log('write error: %s', err.message);
+ var msg = {
+ time: time,
+ task: task
+ }
+ try {
+ var m = util.inspect(msg);
+ var _write_buffer = fs.createWriteStream(_log_path, {encoding:'utf8', flags:'a+'});
+ _write_buffer.write(m + ",\n");
+ _write_buffer.end();
+ } catch(err){
+ // console.log('write error2: %s', err.message);
+ // throw err;
}
}
- _log.push({time: new Date().getTime(),
- task:util.inspect(task, true, 3)});
- if (_log.length > MAX_LOG){
- this._flush_log();
- } else if (!_flush_timeout) {
- this.start_flush_timer();
- }
},
error:function (task) {
this.log('_______ ERROR _________');
if (task instanceof Error) {
- this.log(task.toString());
- throw task;
- } else{
+ this.log(task.message);
+ } else {
this.log(task);
- if (_.isString(task)){
+ if (_.isString(task)) {
throw new Error(task);
}
@@ -1 +0,0 @@
-[{"foo":1},{"bar":2}]
@@ -15,7 +15,7 @@ module.exports = function () {
gender: Number // 1 = male, -1 = female, 0/other == ??
});
- _model = Mongoose_Model.create(schema, {type: 'model', name: 'folks'});
+ _model = Mongoose_Model.create(schema, {type: 'model', name: 'folks'}, mongoose);
}
return _model;
}
@@ -1,3 +1,3 @@
{
- "port": 3334
+ "port": 3434
}
@@ -15,7 +15,7 @@ module.exports = function () {
gender: Number // 1 = male, -1 = female, 0/other == ??
});
- _model = Mongoose_Model.create(schema, {type: 'model', name: 'folks'});
+ _model = Mongoose_Model.create(schema, {type: 'model', name: 'folks'}, mongoose);
}
return _model;
}
@@ -37,7 +37,7 @@ module.exports = {
post_validate:function (rs) {
var self = this;
delete rs.req_props.id;
- console.log('models: %s', util.inspect(this.models));
+ // console.log('models: %s', util.inspect(this.models));
var trial_model = new this.models.folks.model(rs.req_props);
trial_model.validate(function (errs) {
@@ -15,7 +15,7 @@ module.exports = function () {
gender: Number // 1 = male, -1 = female, 0/other == ??
});
- _model = Mongoose_Model.create(schema, {type: 'model', name: 'folks'});
+ _model = Mongoose_Model.create(schema, {type: 'model', name: 'folks'}, mongoose);
}
return _model;
}
@@ -15,7 +15,7 @@ module.exports = function () {
gender: Number // 1 = male, -1 = female, 0/other == ??
});
- _model = Mongoose_Model.create(schema, {type: 'model', name: 'folks'});
+ _model = Mongoose_Model.create(schema, {type: 'model', name: 'folks'}, mongoose);
}
return _model;
}
@@ -48,9 +48,10 @@ module.exports = {
component = new NE.Component({path:component_path});
controller = new NE.Controller({path:controller_path});
+ var fr = new NE.Framework({path:framework_path });
component.start_load(function () {
test.done();
- }, component_path);
+ }, component_path, fr);
},
test_component_loader:function (test) {
@@ -62,6 +63,7 @@ module.exports = {
},
test_controller_loader:function (test) {
+ var fr = new NE.Framework({path:framework_path });
controller.start_load(function () {
test.deepEqual(controller_actions, _ss(controller.action_names()), 'controller action nanes');
test.equal(controller.heritage(), 'alpha', 'controller heritage');
@@ -71,7 +73,7 @@ module.exports = {
test.deepEqual(controller_loader_actions, action_h, 'action heritages');
test.done();
- }, controller_path);
+ }, controller_path, fr);
},
test_framework_loader:function (test) {
@@ -93,7 +95,7 @@ module.exports = {
test.done();
- }, framework_path);
+ }, framework_path, fr);
}
}
@@ -19,10 +19,10 @@ module.exports = {
framework.start_load(function () {
framework.log('Framework loaded');
console.log("ALL DONE LADING FRAMEWORK!");
- logger.init(__dirname + './../test_reports/Controller_loader_test/log.json');
+ //logger.init(__dirname + './../test_reports/Controller_loader_test/log.json');
test.deepEqual(['beta', 'direct'], _.map(framework.controller_names(), _.identity));
test.done();
- }, app_path);
+ }, app_path, framework);
/* var i = setInterval(function(){
if (framework.load_done){
View
@@ -12,7 +12,7 @@ var framework;
var app_path = path.resolve(__dirname + '/../test_resources/Server_Action_test/app');
var root = 'http://localhost:3333/';
var started = false;
-logger.init(__dirname + './../test_reports/Get_Config_test/log.json');
+//logger.init(__dirname + './../test_reports/Get_Config_test/log.json');
module.exports = {
test_start:function (t) {
framework = new NE.Framework({path:app_path});
@@ -1,4 +1,4 @@
-var util = require('util');
+/* var util = require('util');
var path = require('path');
var fs = require('fs');
var events = require('events')
@@ -10,8 +10,10 @@ var framework;
var request = require('request');
var mongoose = require('mongoose');
var app_uri = 'http://localhost:3344';
-
+*/
+// this should be done in the layout module which isn't in core anymore
module.exports = {
+ /*
test_setup:function (test) {
framework = new NE.Framework({path:app_path});
framework.start_load(function () {
@@ -67,5 +69,5 @@ module.exports = {
mongoose.disconnect();
test.done();
}
-
+ */
}
View
@@ -7,33 +7,37 @@ var _ = require('underscore');
var logger = require('./../lib/utility/logger');
var log_path;
-var foo_msg;
-var bar_msg;
+var foo_msg = {foo:1};
+var bar_msg = {bar:2};
module.exports = {
- setup:function (test) {
+ setUp:function (cb) {
log_path = __dirname + '/../test_reports/Logger_tests/log.json';
- logger.init(log_path);
- test.done();
- foo_msg = {foo:1};
- bar_msg = {bar:2};
+ logger.set_log_file(log_path, {reset:true});
+ setTimeout(cb, 500);
},
test_logger:function (test) {
- logger.log(foo_msg);
- logger.log(bar_msg);
- test.done();
- },
+ logger.log('foo', foo_msg);
+ logger.log('bar', bar_msg);
+
+ function _notime(d){
+ delete d.time;
+ return d
+ }
+
+ setTimeout(function () {
+ logger.log_to_obj(function (err, content) {
+ console.log('content: %s (%s)', util.inspect(content), typeof content);
+ test.deepEqual([
+ { task:'foo', content:{ foo:1 } },
+ { task:'bar', content:{ bar:2 } },
+ { task:'end' }]
+ , _.map(content, _notime), 'content of log');
+ test.done();
+ })
- test_logger_output:function (test) {
- fs.readFile(path.normalize(log_path), 'utf8', function(err, contents){
- if (err){
- throw err;
- }
- console.log('log = %s', contents);
- test.equal(contents, JSON.stringify([foo_msg, bar_msg]), 'contents equal JSON.');
- test.done();
- });
+ }, 200);
}
};
Oops, something went wrong.

0 comments on commit de14ca7

Please sign in to comment.