Skip to content
Browse files

added specs

  • Loading branch information...
1 parent 7734268 commit c50b632a48d7f5b358602ed761c791c7f391db2b @andrzejsliwa committed
Showing with 100 additions and 12 deletions.
  1. +3 −0 .gitmodules
  2. +7 −7 autopush.watchr
  3. +5 −0 db/views/byCollection/map.js
  4. +1 −0 js/client
  5. +7 −0 js/couchwatch.js
  6. +29 −3 js/vendor/backbone.couchdb.js
  7. +1 −0 js/vendor/jquery.couch.js
  8. +4 −0 spec.html
  9. +1 −1 spec/spec.helper.js
  10. +21 −1 spec/spec.js
  11. +10 −0 updates/logger.js
  12. +11 −0 validate_doc_update.js
View
3 .gitmodules
@@ -0,0 +1,3 @@
+[submodule "js/client"]
+ path = js/client
+ url = git@github.com:andrzejsliwa/couch-watch-js.git
View
14 autopush.watchr
@@ -21,13 +21,13 @@ def push(dbname, appname, growl)
growl.notify("CouchApp AutoPush", "CouchAPP '#{appname}' Pushed", "default & test", 1, true)
couch = CouchRest.new("http://localhost:5984")
db = couch.database!(dbname)
- puts "cleanup test database!"
- db.documents["rows"].each do |doc|
- unless doc["id"] == "_design/#{appname}"
- puts "removing : #{doc}"
- db.delete_doc({"_id" => doc["id"], "_rev" => doc["value"]["rev"]})
- end
- end
+# puts "cleanup test database!"
+# db.documents["rows"].each do |doc|
+# unless doc["id"] == "_design/#{appname}"
+# puts "removing : #{doc}"
+# db.delete_doc({"_id" => doc["id"], "_rev" => doc["value"]["rev"]})
+# end
+# end
run "soca push test"
run "soca push"
View
5 db/views/byCollection/map.js
@@ -0,0 +1,5 @@
+function(doc) {
+ if (doc.collection) {
+ emit(doc.collection, doc);
+ }
+};
1 js/client
@@ -0,0 +1 @@
+Subproject commit 5cc64e001ab98dc5e3eb84831b63c1c7689a78ed
View
7 js/couchwatch.js
@@ -1,3 +1,10 @@
$(function(){
+ window.Item = Backbone.Model.extend({
+ url: "/logger"
+ });
+ window.ItemsList = Backbone.Collection.extend({
+ url: "/logger",
+ model: Item
+ });
});
View
32 js/vendor/backbone.couchdb.js
@@ -32,7 +32,6 @@ Backbone.couchConnector = {
if(split.length > 1){
collectionName = split[0];
}
- console.log(collectionName);
return collectionName;
},
// Creates a couchDB object from the given model object.
@@ -81,9 +80,10 @@ Backbone.couchConnector = {
// Query equals ddocName/viewName
db.view(query,{
// Only return docs that have this collection's name as key.
- descending: descending,
+ //descending: descending,
keys : [collection],
success:function(result){
+ console.log(result.rows)
if(result.rows.length > 0){
var arr = [];
var model = {};
@@ -244,6 +244,31 @@ Backbone.couchConnector = {
}
}
return { "coll" : coll , "elem" : elem};
+ },
+
+ destroyAllData : function() {
+ var db = this.makeDb(this.databaseName),
+ currentDoc = "_design/" + this.ddocName;
+ db.allDocs({
+ success: function(result) {
+ var docs = _.select(result.rows, function(doc) {
+ return doc.id !== currentDoc;
+ });
+
+ if (docs.length > 0) {
+
+ var toRemove = _.map(docs, function(doc) {
+ return { "_rev": doc.value.rev, "_id": doc.id };
+ });
+ db.bulkRemove({ docs:toRemove }, {
+ success: function() {
+ },
+ error: function(response_code, msg) {
+ }
+ });
+ }
+ }
+ });
}
};
@@ -256,8 +281,9 @@ Backbone.sync = function(method, model, success, error) {
// Decide whether to read a whole collection or just one specific model
if(model.collection)
Backbone.couchConnector.readModel(model, success, error);
- else
+ else {
Backbone.couchConnector.readCollection(model, success, error);
+ }
}else if(method == "delete"){
Backbone.couchConnector.del(model, success, error);
}
View
1 js/vendor/jquery.couch.js
@@ -437,6 +437,7 @@
doc._deleted = true;
}
);
+ console.log(docs.docs);
$.extend(options, {successStatus: 201});
ajax({
type: "POST",
View
4 spec.html
@@ -16,6 +16,7 @@
.script("js/vendor/backbone.js").wait()
.script("js/vendor/backbone.couchdb.js").wait(function() {
+
Backbone.couchConnector.databaseName = "couchwatch-test";
Backbone.couchConnector.ddocName = "couchwatch";
Backbone.couchConnector.viewName = "byCollection";
@@ -25,9 +26,12 @@
console.log("restarting...");
window.location.reload();
+
});
+ Backbone.couchConnector.destroyAllData();
})
.script("js/couchwatch.js")
+ .script("js/client/src/couchwatch.js")
.script("spec/vendor/jasmine.js").wait()
.script("spec/vendor/jasmine.html.js")
.script("spec/vendor/jasmine.jquery.js")
View
2 spec/spec.helper.js
@@ -1,3 +1,3 @@
beforeEach(function() {
-
+ Backbone.couchConnector.destroyAllData();
});
View
22 spec/spec.js
@@ -1,5 +1,25 @@
-describe("", function() {
+describe("CouchWatch JavaScript Client", function() {
+ it("it should be available", function () {
+ expect(couchwatch.enableCouchWatch).toBeDefined();
+ });
+ it("should push logs to CouchWatch Service", function () {
+ runs(function() {
+ window.Items = new ItemsList();
+
+ couchwatch.enableCouchWatch("http://localhost:5984/couchwatch-test");
+ couchwatch.debug("ello");
+ });
+
+ waitsFor(function() {
+ Items.fetch();
+ return Items.length > 0;
+ },"", 10000);
+
+ runs(function(){
+ expect(Items.first().get("message")).toEqual("ello");
+ });
+ })
});
View
10 updates/logger.js
@@ -0,0 +1,10 @@
+function (doc, req) {
+ var newDoc = {};
+ newDoc._id = req.uuid;
+ newDoc.create_at = new Date().toString();
+ newDoc.severity = req.form.severity;
+ newDoc.message = req.form.message;
+ newDoc.peer = req.peer;
+ newDoc.collection = "logger";
+ return [newDoc, "ok"]
+}
View
11 validate_doc_update.js
@@ -0,0 +1,11 @@
+function(newDoc, oldDoc, userCtx) {
+ function require(field, message) {
+ message = message || "Document must have a " + field;
+ if (!newDoc[field]) throw({forbidden : message});
+ };
+
+ if(newDoc.collection == "logger") {
+ require("severity");
+ require("message");
+ }
+}

0 comments on commit c50b632

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