Skip to content
Browse files

change test setup, also fix few bugs

  • Loading branch information...
1 parent 78edd51 commit 63d8ab32475fe3fdc0141f850768fd9b1aa73f6e @ssinghi ssinghi committed Feb 4, 2011
View
30 docs/example.js
@@ -1,23 +1,13 @@
-__dirname = '.'
-require.paths.push(__dirname);
-//require.paths.push(__dirname + '/..');
-//require.paths.push(__dirname + '/../deps');
-require.paths.push(__dirname + '/deps');
-require.paths.push(__dirname + '/lib');
-
-var RDX = require('index').RDX;
-var cfg = require("tests/fixtures/config").cfg;
-
-var ConnectionPool = RDX.ConnectionPool;
-var Model = RDX.Model;
-var Query = RDX.Query;
-var ToMany = RDX.ToMany;
-var BelongsTo = RDX.BelongsTo;
-
-var cp = new ConnectionPool(cfg);
-var User = new Model(cp, 'users');
-var Collection = new Model(cp, 'collections');
-var Item = new Model(cp, 'items');
+var rdx = require('rdx')
+var cfg = { adapter: 'mysql-libmysqlclient',
+ host: "localhost",
+ database: "test",
+ };
+
+var cp = new rdx.ConnectionPool(cfg);
+var User = new rdx.Model(cp, 'users');
+var Collection = new rdx.Model(cp, 'collections');
+var Item = new rdx.Model(cp, 'items');
User.toMany('collections', Collection, Collection.fields["user_id"]);
View
2 lib/belongs_to.js
@@ -16,7 +16,7 @@ var BelongsTo = function(chained_object, join_key, join_table) {
return t;
};
-modules.exports = BelongsTo;
+module.exports = BelongsTo;
BelongsTo.prototype.query = function() {
var q = new Query();
View
10 lib/model.js
@@ -97,26 +97,22 @@ Model.prototype.create = function(value, callback) {
connection.commitSync();
return new_object;
} else {
- new_object.save(function(err, res) {
- if(typeof(callback) === 'function') {
- callback(res,new_object);
- }
- });
+ new_object.save(callback);
}
}
Model.prototype.toMany = function(prop, joinModel, joinKey) {
if(!(joinModel instanceof Model)) new Error(joinModel + " is not an instance of Model");
if(!(joinKey instanceof Field)) new Error(joinKey + " is not an instance of Field");
- var ToMany = require('./to_many').ToMany;
+ var ToMany = require('./to_many');
Object.defineProperty(this.prototype, prop, {get: function(){return new ToMany(this, joinModel, joinKey);}});
Object.defineProperty(this, prop, {get: function(){return new ToMany(this, joinModel, joinKey);}});
}
Model.prototype.belongsTo = function(prop, joinKey, joinModel) {
if(!(joinModel instanceof Model)) new Error(joinModel + " is not an instance of Model");
if(!(joinKey instanceof Field)) new Error(joinKey + " is not an instance of Field");
- var BelongsTo = require('./belongs_to').BelongsTo;
+ var BelongsTo = require('./belongs_to');
Object.defineProperty(this.prototype, prop, {get: function(){return new BelongsTo(this, joinKey, joinModel);}});
Object.defineProperty(this, prop, {get: function(){return new BelongsTo(this, joinKey, joinModel);}});
}
View
2 lib/record.js
@@ -28,7 +28,7 @@ Record.prototype.save = function(callback) {
} else {
base_class.emit('beforeCreate');
res = connection.query(sql, function (err, res) {
- if(res.insertId != 0) { me[base_class.primaryKey.name] = res.insertId; res = true; this.newRecord = false;}
+ if(res && res.insertId != 0) { me[base_class.primaryKey.name] = res.insertId; res = true; this.newRecord = false;}
if(typeof(callback) === 'function') {callback.call(me, err, res);}});
base_class.emit('afterCreate', me);
}
View
2 lib/to_many.js
@@ -3,7 +3,7 @@ var Model = require('./model');
var Field = require('./field');
var Query = require('./query');
-var ToMany = function(chained_object, join_table, join_key ){
+var ToMany = function(chained_object, join_table, join_key ) {
if(!(join_table instanceof Model)) new Error(join_table + " is not an instance of Model");
if(!(join_key instanceof Field)) new Error(join_key + " is not an instance of Field");
View
2 test.js
@@ -20,6 +20,6 @@ catch(e) {
}
process.chdir(__dirname);
-reporter.run(['tests/test-model.js','tests/test-query.js', 'tests/test-field.js']);
+reporter.run(['tests/test-model.js','tests/test-query.js', 'tests/test-field.js', 'tests/test-to-many.js', 'tests/test-belongs-to.js']);
View
10 tests/collection.js
@@ -1,8 +1,6 @@
-var cp = require('./setup').RDXTestConnectionPool();
-var Model = require('model');
+var User = require('./index').User;
+var Collection = require('./index').Collection;
+var Item = require('./index').Item;
-
-var Item = require('./item').Item;
-
-var Collection = exports.Collection = new Model(cp, 'collections');
Collection.toMany('items', Item, Item.fields["collection_id"]);
+Collection.belongsTo('users',Collection.fields["user_id"], User);
View
2 tests/fixtures/config.js
@@ -1,8 +1,6 @@
exports.cfg = {
adapter: 'mysql-libmysqlclient',
host: "localhost",
- user: "root",
- password: "Mapinkbo",
database: "test",
};
View
2 tests/fixtures/db_schema.sql
@@ -3,7 +3,7 @@ CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`first_name` varchar(255) default NULL,
`last_name` varchar(255) default NULL,
- `email` varchar(255) default '',
+ `email` varchar(255) default NULL,
`user_type` int(11) NOT NULL default '0',
`hashed_password` varchar(40) default NULL,
`salt` varchar(40) default NULL,
View
15 tests/index.js
@@ -0,0 +1,15 @@
+var cfg = require("./fixtures/config").cfg;
+var ConnectionPool = require('connection').ConnectionPool;
+var Model = require('model');
+
+var exports = module.exports = new Object;
+
+var cp = new ConnectionPool(cfg);
+
+exports.User = new Model(cp, 'users');
+exports.Collection = new Model(cp, 'collections');
+exports.Item = new Model(cp, 'items');
+
+require('./user');
+require('./collection');
+require('./item');
View
6 tests/item.js
@@ -1,4 +1,4 @@
-var cp = require('./setup').RDXTestConnectionPool();
-var Model = require('model');
+var Collection = require('./index').Collection;
+var Item = require('./index').Item;
-var Item = exports.Item = new Model(cp, 'items');
+Item.belongsTo('collections',Item.fields["collection_id"], Collection);
View
9 tests/setup.js
@@ -1,9 +0,0 @@
-var cfg = require("./fixtures/config").cfg;
-var ConnectionPool = require('connection').ConnectionPool;
-
-var cp;
-
-exports.RDXTestConnectionPool = function() {
- if(cp) { return cp; }
- return (cp = new ConnectionPool(cfg));
-};
View
6 tests/test-belongs-to.js
@@ -1,7 +1,5 @@
-var User = require('./user').User;
-var Collection = require('./collection').Collection;
-
-Collection.belongsTo('users',Collection.fields["user_id"], User);
+var User = require('./index').User;
+var Collection = require('./index').Collection;
exports.testBelongsTo = function(test) {
test.expect(1);
View
2 tests/test-field.js
@@ -1,4 +1,4 @@
-var User = require('./user').User;
+var User = require('./index').User;
exports.testFieldObjectCreation = function(test) {
test.expect(5);
View
4 tests/test-model.js
@@ -1,8 +1,8 @@
var Model = require('model');
var Query = require('query');
-var User = require('./user').User;
-var Collection = require('./collection').Collection;
+var User = require('./index').User;
+var Collection = require('./index').Collection;
exports.testModelObject = function(test) {
test.expect(3);
View
5 tests/test-query.js
@@ -1,9 +1,8 @@
var Query = require('query');
var Field = require('field');
-var Collection = require('./collection').Collection;
-var User = require('./user').User;
-
+var Collection = require('./index').Collection;
+var User = require('./index').User;
exports.testQueryWhereSync = function(test) {
test.expect(3);
View
10 tests/test-to-many.js
@@ -1,11 +1,9 @@
-var User = require('./user').User;
-var Collection = require('./collection').Collection;
-
-User.toMany('collection', Collection, Collection.fields["user_id"]);
+var User = require('./index').User;
+var Collection = require('./index').Collection;
exports.testToMany = function(test) {
test.expect(2);
- test.equals(User.collection._chain.joinKey.name, "user_id", "Field name is correct")
- test.equals(User.find("1",false).collection.find('1', false).id, 1, "Return value should correvt");
+ test.equals(User.collections._chain.joinKey.name, "user_id", "Field name is correct")
+ test.equals(User.find("1",false).collections.find('1', false).id, 1, "Return value should be correct");
test.done();
};
View
7 tests/user.js
@@ -1,7 +1,4 @@
-var cp = require('./setup').RDXTestConnectionPool();
-var Model = require('model');
+var User = require('./index').User;
+var Collection = require('./index').Collection;
-var Collection = require('./collection').Collection;
-
-var User = exports.User = new Model(cp, 'users');
User.toMany('collections', Collection, Collection.fields["user_id"]);

0 comments on commit 63d8ab3

Please sign in to comment.
Something went wrong with that request. Please try again.