Skip to content

Commit 882c1df

Browse files
added testcases
1 parent b8f5074 commit 882c1df

13 files changed

+220
-29
lines changed

.DS_Store

6 KB
Binary file not shown.

.jshintrc

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"bitwise" : true,
3+
"curly" : true,
4+
"eqeqeq" : true,
5+
"forin" : true,
6+
"immed" : true,
7+
"latedef" : true,
8+
"newcap" : true,
9+
"noarg" : true,
10+
"noempty" : true,
11+
"nonew" : true,
12+
"plusplus" : true,
13+
"regexp" : true,
14+
"undef" : false,
15+
"strict" : false,
16+
"trailing" : true,
17+
"asi" : false,
18+
"boss" : false,
19+
"debug" : false,
20+
"eqnull" : false,
21+
"es5" : false,
22+
"esnext" : false,
23+
"evil" : false,
24+
"expr" : false,
25+
"funcscope" : false,
26+
"globalstrict" : false,
27+
"iterator" : false,
28+
"lastsemic" : false,
29+
"laxbreak" : false,
30+
"laxcomma" : false,
31+
"loopfunc" : false,
32+
"multistr" : false,
33+
"onecase" : false,
34+
"proto" : false,
35+
"regexdash" : false,
36+
"scripturl" : false,
37+
"smarttabs" : false,
38+
"shadow" : false,
39+
"sub" : false,
40+
"supernew" : false,
41+
"validthis" : false,
42+
"browser" : false,
43+
"couch" : false,
44+
"devel" : false,
45+
"dojo" : false,
46+
"jquery" : false,
47+
"mootools" : false,
48+
"node" : true,
49+
"nonstandard" : false,
50+
"prototypejs" : false,
51+
"rhino" : false,
52+
"wsh" : false,
53+
"nomen" : false,
54+
"onevar" : false,
55+
"passfail" : false,
56+
"white" : false,
57+
"maxerr" : 100,
58+
"predef" : [
59+
"it",
60+
"describe"
61+
],
62+
"indent" : 4
63+
}

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- 0.8

Backbone.mongoose.js

+33-26
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
11
var mongoose = require("mongoose"),
2-
fs = require('fs'),
3-
files = fs.readdirSync('./schema'),
4-
connection, mongooseSync;
2+
fs = require('fs');
53

6-
files.forEach(function(fileName) {
7-
require('./schema/' + fileName);
8-
});
4+
var backboneMongoose = function(config) {
95

10-
connection = mongoose.createConnection("url to connect mongodb");
6+
var files = fs.readdirSync(config.schema_dir),
7+
connection, mongooseSync;
118

12-
mongooseSync = function(method, model, options) {
9+
connection = mongoose.createConnection(config.db_url);
1310

14-
var MongooseModel = connection && connection.model(model.mongooseModel),
11+
files.forEach(function(fileName) {
12+
require(config.schema_dir +'/' + fileName);
13+
});
1514

16-
process = function(err, docs) {
17-
if(err) {
18-
if(options.error) {
19-
options.error(model, err, options);
15+
mongooseSync = function(method, model, options) {
16+
17+
var MongooseModel = connection && connection.model(model.mongooseModel),
18+
19+
process = function(err, docs) {
20+
if(err) {
21+
if(options.error) {
22+
options.error(model, err, options);
23+
}
2024
}
21-
}
22-
if(options.success) {
23-
options.success(model, docs, options);
24-
}
25-
},
25+
if(options.success) {
26+
options.success(model, docs, options);
27+
}
28+
},
2629

27-
data = model.toJSON() || {};
30+
data = model.toJSON() || {};
2831

29-
options || (options = {});
32+
options = options || (options = {});
3033

31-
if(!MongooseModel) {
32-
return;
33-
}
34+
if(!MongooseModel) {
35+
return;
36+
}
3437

35-
switch(method) {
38+
switch(method) {
3639
case 'create':
3740
MongooseModel.create(data, process);
3841
break;
@@ -47,7 +50,11 @@ mongooseSync = function(method, model, options) {
4750
break;
4851
case 'read':
4952
MongooseModel.find(data, process);
50-
}
53+
}
54+
};
55+
return mongooseSync;
5156
};
5257

53-
exports = module.exports = mongooseSync;
58+
backboneMongoose.VERSION = "0.1.1";
59+
60+
exports = module.exports = backboneMongoose;

Makefile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
TESTS = test/*.js
2+
EXAMPLE = example/*.js
3+
REPORTER = dot
4+
5+
test: jshint test-unit
6+
7+
jshint:
8+
@./node_modules/.bin/jshint $(EXAMPLE) --config .jshintrc
9+
@./node_modules/.bin/jshint $(TESTS) --config .jshintrc
10+
@./node_modules/.bin/jshint Backbone.mongoose.js --config .jshintrc
11+
12+
test-unit:
13+
@./node_modules/.bin/mocha \
14+
--require should \
15+
--reporter $(REPORTER) \
16+
--growl \
17+
--timeout 5000 \
18+
$(TESTS)
19+
20+
.PHONY: test jshint test-unit bench

app.js example/app.js

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

package.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
{
2-
"name": "Backbone.Mongodb",
2+
"name": "Backbone.mongoose",
33
"description": "Backbone sync for the Mongodb",
4-
"version": "0.1.0",
4+
"version": "0.1.1",
55
"author": {
66
"name": "Prathamesh Satpute",
77
"email": "[email protected]"
88
},
99
"keywords": [
10-
"node",
1110
"backbone",
1211
"mongoose"
1312
],
@@ -23,6 +22,11 @@
2322
"backbone": "~0.9.10",
2423
"mongoose": "~3.5.7"
2524
},
25+
"devDependencies": {
26+
"should": "~1.2.2",
27+
"mocha": "~1.8.1",
28+
"jshint": "~1.0.0"
29+
},
2630
"engines": {
2731
"node": "0.8.x",
2832
"npm": "1.1.x"

test/.DS_Store

6 KB
Binary file not shown.

test/Backbone.mongoose.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Test dependencies.
3+
*/
4+
5+
var mongooseSync = require('../Backbone.mongoose'),
6+
should = require('should'),
7+
Backbone = require('Backbone'),
8+
mongoose = require('mongoose'),
9+
config = {
10+
db_url : "url will go here",
11+
schema_dir : __dirname + "/schema"
12+
};
13+
14+
//replace Backbone.sync with the mongoose sync
15+
Backbone.sync = mongooseSync(config);
16+
17+
18+
//Initialize sample Backbone Models and collection fot the tests
19+
var Book = Backbone.Model.extend({
20+
mongooseModel: "Book",
21+
defaults: {
22+
"name": ""
23+
}
24+
});
25+
26+
var Library = Backbone.Collection.extend({
27+
model: Book,
28+
mongooseModel: "Book"
29+
});
30+
31+
/**
32+
* Tests.
33+
*/
34+
describe('Backbone.mongoose', function() {
35+
var connection = mongoose.createConnection(config.db_url),
36+
MongooseModel = connection && connection.model("Book"),
37+
mongooseModel = new MongooseModel();
38+
39+
//clear collection before each test
40+
beforeEach(function(done) {
41+
mongooseModel.collection.drop(function(err) {
42+
if (err && err.message !== 'ns not found') {
43+
done(err);
44+
}
45+
done();
46+
});
47+
});
48+
49+
it('must expose version number', function() {
50+
mongooseSync.VERSION.should.match(/[0-9]+\.[0-9]+\.[0-9]+/);
51+
});
52+
53+
it('must match version number', function() {
54+
mongooseSync.VERSION.should.equal("0.1.1");
55+
});
56+
57+
it('Backbone Model.save should save without error', function(done){
58+
var book = new Book({name: "High Performance Javascript"});
59+
book.on('change', function(doc) {
60+
doc.toJSON().name.should.equal("High Performance Javascript");
61+
done();
62+
});
63+
book.save();
64+
});
65+
66+
it('Backbone Model.update should update without error', function(done){
67+
var book = new Book({name: "High Performance Javascript"});
68+
book.on('change', function(doc) {
69+
doc.toJSON().name.should.equal("High Performance Javascript");
70+
done();
71+
});
72+
book.save();
73+
});
74+
75+
it('Backbone Collection.fetch should fetch without error', function(done){
76+
var library = new Library(),
77+
success = function(docs) {
78+
done();
79+
},
80+
error = function(err) {
81+
done(err);
82+
};
83+
library.fetch({success: success, error: error});
84+
});
85+
86+
87+
});

test/schema/book.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var mongoose = require('mongoose');
2+
3+
var BookSchema = new mongoose.Schema({
4+
name: String
5+
});
6+
7+
exports = module.exports = mongoose.model('Book', BookSchema);

0 commit comments

Comments
 (0)