/test/testrunner/test.js

https://github.com/andynelson/backbone-couchdb · JavaScript · 302 lines · 268 code · 33 blank · 1 comment · 2 complexity · 1b85017123350ba022656aaa2d99f469 MD5 · raw file

  1. QUnit.begin = function(){
  2. Backbone.couch_connector.config.db_name = "backbone_test_db";
  3. Backbone.couch_connector.config.ddoc_name = "backbone_connector_test";
  4. Backbone.couch_connector.config.global_changes = false;
  5. // overwrite the defaul requests, because /db is the new db namespace
  6. $.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
  7. options.url = '/db' + options.url;
  8. });
  9. };
  10. module("helpers");
  11. test("extracts all possible urls correctly", function(){
  12. raises(function(){
  13. Backbone.couch_connector.helpers.extract_collection_name();
  14. }, "throws error when no model is passed to function");
  15. equals(Backbone.couch_connector.helpers.extract_collection_name({}), "", "Returns empty string when no url specified");
  16. var model_w_url_field = {
  17. collection : {
  18. url : "comments"
  19. }
  20. };
  21. equals(Backbone.couch_connector.helpers.extract_collection_name(model_w_url_field), "comments", "obj with url field");
  22. model_w_url_field.collection.url = "comments/"
  23. equals(Backbone.couch_connector.helpers.extract_collection_name(model_w_url_field), "comments", "obj with url field (trailing slash)");
  24. model_w_url_field.collection.url = "/comments"
  25. equals(Backbone.couch_connector.helpers.extract_collection_name(model_w_url_field), "comments", "obj with url field (leading slash)");
  26. model_w_url_field.collection.url = "/comments/"
  27. equals(Backbone.couch_connector.helpers.extract_collection_name(model_w_url_field), "comments", "obj with url field (leading & trailing slash)");
  28. var model_w_url_field_and_id = {
  29. collection : {
  30. url : "comments/3"
  31. }
  32. };
  33. equals(Backbone.couch_connector.helpers.extract_collection_name(model_w_url_field_and_id), "comments", "obj with url field and id");
  34. var model_w_url_method = {
  35. collection : {
  36. url : function(){
  37. return "comments"
  38. }
  39. }
  40. };
  41. equals(Backbone.couch_connector.helpers.extract_collection_name(model_w_url_method), "comments", "obj with url method");
  42. var model_w_url_method_and_id = {
  43. collection : {
  44. url : function(){
  45. return "comments/32"
  46. }
  47. }
  48. };
  49. equals(Backbone.couch_connector.helpers.extract_collection_name(model_w_url_method_and_id), "comments", "obj with url method that also returns an id");
  50. });
  51. test("creates a proper db object", function(){
  52. ok(true);
  53. });
  54. module("db relevant", {
  55. setup : function(){
  56. stop();
  57. db = $.couch.db("backbone_test_db")
  58. db.create({
  59. success : function(){
  60. var ddoc = {
  61. "_id": "_design/backbone_connector_test",
  62. "language": "javascript",
  63. "views": {
  64. "byCollection": {
  65. "map": "function(doc) {\n if (doc.collection) {\n emit(doc.collection, doc);\n }\n};"
  66. },
  67. "testView": {
  68. "map": "function(doc) {\n if (doc.body && doc.body == 'test3') {\n emit(doc.title, doc);\n }\n};"
  69. }
  70. }
  71. };
  72. var test_doc_1 = { _id : "test_id_4711", collection: "comments", title: "test1", body : "test1" };
  73. var test_doc_2 = { collection: "comments", title: "test2", body : "test2" };
  74. var test_doc_3 = { collection: "tests", title: "test3", body : "test3" };
  75. var test_doc_4 = { collection: "hallo", title: "test4", body : "test3" };
  76. ct = 0
  77. opts = { success : function(){ ct++; if(ct == 5){ start(); } }, error : function(){ alert("could no create a test doc"); }};
  78. db.saveDoc(ddoc, opts);
  79. db.saveDoc(test_doc_1, opts);
  80. db.saveDoc(test_doc_2, opts);
  81. db.saveDoc(test_doc_3, opts);
  82. db.saveDoc(test_doc_4, opts);
  83. },
  84. error : function(error){
  85. stop();
  86. console.log(arguments);
  87. alert("could not create testdb, please delete it manually");
  88. }
  89. });
  90. },
  91. teardown : function(){
  92. stop();
  93. $.couch.db("backbone_test_db").drop({
  94. success : function(){
  95. start();
  96. },
  97. error : function(){
  98. alert("could not delete testdb, please delete it manually");
  99. }
  100. });
  101. }
  102. });
  103. asyncTest("read collection" , function(){
  104. var CommentModel = Backbone.Model.extend({
  105. __testing : function(){}
  106. });
  107. var CommentList = Backbone.Collection.extend({
  108. db : {
  109. changes : false
  110. },
  111. url : "/comments",
  112. model : CommentModel
  113. });
  114. var Comments = new CommentList();
  115. Comments.fetch({
  116. success : function(){
  117. equals(Comments.length, 2, "Collection contains the right amount of docs after fetching");
  118. notEqual(Comments.get("test_id_4711"), undefined, "Element that had an _id before is also in Collection");
  119. notEqual(Comments.get("test_id_4711").__testing, undefined, "Element has the right type");
  120. start();
  121. }
  122. });
  123. });
  124. asyncTest("read collection with custom view" , function(){
  125. var CommentList = Backbone.Collection.extend({
  126. db : {
  127. view : "testView",
  128. changes : false,
  129. keys : null
  130. },
  131. url : "/comments"
  132. });
  133. var Comments = new CommentList();
  134. Comments.fetch({
  135. success : function(){
  136. equals(Comments.length, 2, "Collection contains the right amount of docs after fetching");
  137. start();
  138. },
  139. error : function(){
  140. console.log("error", arguments);
  141. }
  142. });
  143. });
  144. asyncTest("read collection with custom view and custom keys" , function(){
  145. var CommentList = Backbone.Collection.extend({
  146. db : {
  147. view : "testView",
  148. changes : false,
  149. keys : ["test4"]
  150. },
  151. url : "/comments"
  152. });
  153. var Comments = new CommentList();
  154. Comments.fetch({
  155. success : function(){
  156. equals(Comments.length, 1, "Collection contains the right amount of docs after fetching");
  157. start();
  158. },
  159. error : function(){
  160. console.log("error");
  161. }
  162. });
  163. });
  164. asyncTest("read model", function(){
  165. var CommentModel = Backbone.Model.extend({});
  166. mymodel = new CommentModel({
  167. _id : "test_id_4711"
  168. });
  169. mymodel.fetch({
  170. success : function(){
  171. equals(mymodel.id, "test_id_4711", "Model has the same id after fetching it");
  172. equals(mymodel.get('body'), "test1", "Model has a certain body field");
  173. start();
  174. },
  175. error : function(){
  176. alert("Model could not be fetched");
  177. }
  178. });
  179. broken_model = new CommentModel();
  180. raises(function(){
  181. broken_model.fetch();
  182. }, "throws error when model has no id property");
  183. });
  184. asyncTest("create model", function(){
  185. var CommentModel = Backbone.Model.extend({});
  186. mymodel = new CommentModel({
  187. body : "I'm new",
  188. random : "string"
  189. });
  190. mymodel.url = "";
  191. mymodel.save({},{
  192. success : function(model){
  193. notEqual(model.id, undefined, "The model shoud have an id");
  194. notEqual(model.toJSON()._id, undefined, "The model shoud have an _id when converted to JSON");
  195. notEqual(model.toJSON()._rev, undefined, "The model shoud have a _rev field");
  196. start();
  197. },
  198. error : function(){
  199. console.log("in err cb", arguments);
  200. }
  201. });
  202. });
  203. asyncTest("update model", function(){
  204. var CommentModel = Backbone.Model.extend({});
  205. mymodel = new CommentModel({
  206. _id : "test_id_4711"
  207. });
  208. mymodel.url = "";
  209. var changed_text = "I've changed!!!";
  210. mymodel.fetch({
  211. success : function(){
  212. mymodel.set({text : changed_text});
  213. var the_rev = mymodel.get('_rev');
  214. mymodel.save({},{
  215. success : function(){
  216. var new_model = new CommentModel({
  217. _id : "test_id_4711"
  218. });
  219. new_model.fetch({
  220. success: function(){
  221. start();
  222. equals(new_model.get('text'), changed_text, "The new text should have been saved to the model");
  223. notEqual(new_model.get('_rev'), the_rev, "The _rev attribute should have changed");
  224. }
  225. });
  226. },
  227. error : function(){}
  228. })
  229. },
  230. error : function(){
  231. alert("Model could not be fetched");
  232. }
  233. });
  234. });
  235. asyncTest("delete model", function(){
  236. var CommentModel = Backbone.Model.extend({});
  237. mymodel = new CommentModel({
  238. _id : "test_id_4711"
  239. });
  240. mymodel.fetch({
  241. success : function(){
  242. mymodel.destroy({
  243. success : function(){
  244. mymodel.fetch({
  245. success : function(){
  246. start();
  247. ok(false, "Model has not been deleted in the DB");
  248. },
  249. error : function(){
  250. start();
  251. ok(true, "Model has been deleted and could not get retrieved again.")
  252. }
  253. });
  254. },
  255. error : function(){
  256. start();
  257. ok(false, "Model could not be deleted from the DB")
  258. }
  259. });
  260. },
  261. error : function(){
  262. ok(false, "error retrieving the model");
  263. start();
  264. }
  265. });
  266. });
  267. QUnit.done = function(){
  268. console.log("done")
  269. };