Commits

remosu committed f4d238a

Initial commit

Comments (0)

Files changed (6)

+syntax: glob
+
+*.pyc
+*~
+
+
+syntax: regexp
+
+
+test-acceptance:
+	./test/acceptance/mongodb_setup.py
+	mocha --reporter spec \
+	 test/acceptance/*.js
+
+.PHONY: test
+import pymongo
+import json
+from bson import json_util
+from pymongo.objectid import ObjectId
+
+CONNECTION = pymongo.Connection()
+DATABASE = "backmongo"
+DB = CONNECTION[DATABASE]
+
+def dict_to_json_converter(obj, **kwargs):
+    if isinstance(obj, ObjectId):
+        return str(obj)
+    else:
+        return json_util.default(obj, **kwargs)
+
+def read(collection_name, id=None):
+    if id is not None:
+        result = DB[collection_name].find_one(ObjectId(id))
+    else:
+        result = list(DB[collection_name].find())
+    
+    return json.dumps(result, default=dict_to_json_converter)
+
+def delete(collection_name, id):
+    DB[collection_name].remove(ObjectId(id))
+
+def save(collection_name, model, id=None):
+    if id is not None:
+        model['_id'] = ObjectId(model['_id'])
+    return DB[collection_name].save(model, manipulate=False)
+from flask import Flask, request, jsonify, Response
+
+import backmongo
+
+
+app = Flask(__name__)
+    
+@app.route("/<collection>/", methods=['GET'])
+@app.route("/<collection>/<id>", methods=['GET'])
+def read(collection, id=None):
+    result = backmongo.read(collection, id)
+    return Response(result, mimetype='application/json')
+
+@app.route("/<collection>/<id>", methods=['DELETE'])
+def delete(collection, id):
+    backmongo.delete(collection, id)
+    return Response("{status: OK}", mimetype='application/json')
+
+@app.route("/<collection>/", methods=['POST'])
+@app.route("/<collection>/<id>", methods=['PUT'])
+def save(collection, id=None):
+    result = backmongo.save(collection, request.json, id)
+    return Response(result, mimetype='application/json')
+
+if __name__ == "__main__":
+    app.run(debug=True)

test/acceptance/backmongo.js

+var assert = require('assert');
+var should = require('should');
+var jQuery = require("jquery");
+var Backbone = require('backbone');
+
+Backbone.setDomLibrary(jQuery);
+
+var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
+
+jQuery.support.cors = true;
+jQuery.ajaxSettings.xhr = function () {
+    return new XMLHttpRequest;
+}
+
+var BMModel = Backbone.Model.extend({
+    idAttribute: "_id",
+    urlRoot: 'http://localhost:5000/collection_tests/'
+});
+
+var BMCollection = Backbone.Collection.extend({
+    model: BMModel,
+    url: 'http://localhost:5000/collection_tests/'         
+});
+
+
+describe("BMCollection", function(){
+    var bmcollection;
+
+    beforeEach(function(){
+         bmcollection = new BMCollection();
+    });  
+        
+    it("is defined", function(){
+        bmcollection.should.not.equal(undefined);
+    });
+});
+
+describe("BackMongo", function(){
+    var bmcollection = new BMCollection();
+
+    describe("read all", function(){
+        var expected;
+   
+        beforeEach(function(done){
+            bmcollection.fetch({success: function(){
+                done();
+            }});
+        });
+
+        it("gets contents from mongo", function(){
+            bmcollection.models.should.have.lengthOf(2);
+        });
+
+        it("has this models", function(){
+            expected = [
+                {
+                    "_id": "4f6b75e052ad624ff62cfc8b",
+                    "name": "manolo",
+                    "age": 36
+                },
+                {
+                    "_id": "4f6b75e952ad624ff62cfc8c",
+                    "name": "silvio",
+                    "age": 31
+                }
+            ];
+            for(var i=0; i<2; i++){
+                expected[i].name.should.equal(bmcollection.models[i].get('name'));
+                expected[i].age.should.equal(bmcollection.models[i].get('age'));
+                expected[i]._id.should.equal(bmcollection.models[i].get('_id'));
+                expected[i]._id.should.equal(bmcollection.models[i].id);
+            }
+        });
+    });
+
+    describe("read one", function(){
+        var model;
+
+        before(function(done){
+            model = new BMModel({_id: '4f6b75e952ad624ff62cfc8c'});
+            model.fetch({success: function(){done();}});
+        });
+
+        it("fetchs a given model", function(){
+            model.get('name').should.equal("silvio");
+            model.get('age').should.equal(31);
+        });
+    });
+
+    describe("create", function(){
+        before(function(){
+            bmcollection.create({
+                "name": "paco",
+                "age": 32
+            }, {success: function(){done();}});
+        });
+
+        before(function(done){
+            bmcollection.fetch({success: function(){done();}});
+        });
+
+        it("adds a new model to collection", function(){
+            bmcollection.models.should.have.lengthOf(3);
+        });
+
+        it("adds the correct model", function(){
+            var model = bmcollection.find(function(model){
+                return model.get('name') == 'paco';
+            });
+            should.exist(model);     
+        });
+    });
+
+    describe("delete", function(){
+        before(function(done){
+            bmcollection.fetch({success: function(){done();}});
+        });
+
+        before(function(){
+            var model = bmcollection.get('4f6b75e952ad624ff62cfc8c');
+            model.destroy({success: function(){done();}});
+        });
+
+        before(function(done){
+            bmcollection.fetch({success: function(){done();}});
+        });
+
+        it("there is one model less in collection", function(){
+            bmcollection.models.should.have.lengthOf(2);
+        });
+
+        it("deletes the right one", function(){
+            var model = bmcollection.find(function(model){
+                return model.get('_id') == '4f6b75e952ad624ff62cfc8c';
+            });
+            should.not.exist(model);     
+        });
+    });
+
+    describe("update", function(){
+        before(function(done){
+            bmcollection.fetch({success: function(){done();}});
+        });
+
+        before(function(){
+            var model = bmcollection.get('4f6b75e052ad624ff62cfc8b');
+            model.set('name', 'pepito');
+            model.save({success: function(){done();}});
+        });
+
+        before(function(done){
+            bmcollection.fetch({success: function(){done();}});
+        });
+
+        it("update the right one", function(){
+            var model = bmcollection.find(function(model){
+                return model.id == '4f6b75e052ad624ff62cfc8b' && 
+                    model.get('name') == 'pepito' && model.get('age') == 36;
+            });
+            should.exist(model);     
+        });
+    });
+});

test/acceptance/mongodb_setup.py

+#! /usr/bin/env python
+
+import pymongo
+from pymongo.objectid import ObjectId
+
+CONNECTION = pymongo.Connection()
+DATABASE = "backmongo"
+
+CONNECTION[DATABASE].drop_collection("collection_tests")
+collection_tests = CONNECTION[DATABASE]['collection_tests']
+
+collection_tests.insert({'_id': ObjectId('4f6b75e052ad624ff62cfc8b'), 'name':'manolo', 'age':36})
+collection_tests.insert({'_id': ObjectId('4f6b75e952ad624ff62cfc8c'), 'name':'silvio', 'age':31})