/tests/tests.js

https://github.com/JeromeParadis/server-backbone-redis · JavaScript · 276 lines · 267 code · 8 blank · 1 comment · 5 complexity · e4976c02bf42486a1c8815d75f33da2d MD5 · raw file

  1. var testCase = require('nodeunit').testCase;
  2. var Backbone = require('../server-backbone-redis')
  3. var async = require('async');
  4. var rc = redis.createClient();
  5. module.exports = testCase({
  6. setUp: function (callback) {
  7. this.models = {};
  8. this.models.Backbone = Backbone;
  9. this.models.TestChatMessage = Backbone.Model.extend({
  10. defaults: {
  11. "user": null,
  12. "message": null
  13. },
  14. name: "testchat"
  15. });
  16. this.models.TestVolatile = Backbone.Model.extend({
  17. defaults: {
  18. "user": null,
  19. "message": null
  20. },
  21. name: "testvol",
  22. expiration: 1 // 1 second expiration
  23. });
  24. this.models.TestChatMessageCollection = Backbone.Collection.extend({
  25. model: this.models.TestChatMessage
  26. });
  27. callback();
  28. },
  29. tearDown: function (callback) {
  30. // clean up
  31. callback();
  32. },
  33. testSetClient: function (test) {
  34. test.expect(1);
  35. test.ok(Backbone.setClient(rc), 'setClient failed().');
  36. test.done();
  37. },
  38. testCreateRecords: function (test) {
  39. test.expect(9);
  40. var chat1 = new this.models.TestChatMessage({user: 1, message:'Test 1'});
  41. test.equal(chat1.xport(),'{"id":null,"cid":"c0","attrs":{"user":1,"message":"Test 1"}}');
  42. var chat2 = new this.models.TestChatMessage({user: 2, message:'Test 2'});
  43. test.equal(chat2.xport(),'{"id":null,"cid":"c1","attrs":{"user":2,"message":"Test 2"}}');
  44. var chat3 = new this.models.TestChatMessage({user: 1, message:'Test 3'});
  45. test.equal(chat3.xport(),'{"id":null,"cid":"c2","attrs":{"user":1,"message":"Test 3"}}');
  46. async.series(
  47. [async.apply(function(cb) {
  48. chat1.save({},{success: function(model) {
  49. test.ok(parseInt(model.get("id")) > 0,'ID attribute not created.');
  50. test.ok(parseInt(model.id) > 0, 'ID object not created.');
  51. cb(null,1);
  52. }, error: function(err) {
  53. cb("Error saving chat1",1);
  54. }});
  55. }),
  56. async.apply(function(cb) {
  57. chat2.save({},{success: function(model) {
  58. test.ok(parseInt(model.get("id")) > 0,'ID attribute not created.');
  59. test.ok(parseInt(model.id) > 0, 'ID object not created.');
  60. cb(null,2);
  61. }, error: function(err) {
  62. cb("Error saving chat2",2);
  63. }});
  64. }),
  65. async.apply(function(cb) {
  66. chat3.save({},{success: function(model) {
  67. test.ok(parseInt(model.get("id")) > 0,'ID attribute not created.');
  68. test.ok(parseInt(model.id) > 0, 'ID object not created.');
  69. cb(null,3);
  70. }, error: function(err) {
  71. cb("Error saving chat3",3);
  72. }});
  73. })
  74. ],function(err,results) {
  75. if (err) {
  76. throw new Error("Unexpected error running tests: " + err);
  77. }
  78. test.done();
  79. }
  80. );
  81. },
  82. testSaveIdLoadRecords: function (test) {
  83. var models = this.models;
  84. test.expect(10);
  85. async.series(
  86. [async.apply(function(cb) {
  87. Backbone.search_delete(models.TestChatMessage,"*",function(results) {
  88. test.ok(parseInt(results) > 0,'Could not cleanup records.');
  89. cb(null,1);
  90. },function(err) {
  91. cb("Error saving chat1",1);
  92. });
  93. }),async.apply(function(cb) {
  94. var chat1 = new models.TestChatMessage({id:"test1",user: 1, message:'Test 1'});
  95. chat1.save({id:"test1"},{success: function(model) {
  96. test.equal(model.get("id"),"test1",'ID attribute not created.');
  97. test.equal(model.id,"test1", 'ID object not created.');
  98. cb(null,1);
  99. }, error: function(err) {
  100. cb("Error saving chat1",1);
  101. }});
  102. }),async.apply(function(cb) {
  103. var chat1 = new models.TestChatMessage({id: "test1"});
  104. chat1.fetch({success: function(model) {
  105. test.equal(parseInt(model.get("user")),1,"test1",'No the right record.');
  106. test.equal(model.id,"test1",'No the right record.');
  107. test.equal(model.get('message'),"Test 1", 'Not the right record.');
  108. cb(null,2);
  109. }, error: function(err) {
  110. cb("Error saving chat1",2);
  111. }});
  112. }),async.apply(function(cb) {
  113. var chat2 = new models.TestChatMessage({id:"test2",user: 2, message:'Test 2'});
  114. chat2.save({id:"test2"},{success: function(model) {
  115. test.equal(model.get("id"),"test2",'ID attribute not created.');
  116. test.equal(model.id,"test2", 'ID object not created.');
  117. cb(null,1);
  118. }, error: function(err) {
  119. cb("Error saving chat2",1);
  120. }});
  121. }),async.apply(function(cb) {
  122. var chat3 = new models.TestChatMessage({id:"test3",user: 1, message:'Test 3'});
  123. chat3.save({id:"test3"},{success: function(model) {
  124. test.equal(model.get("id"),"test3",'ID attribute not created.');
  125. test.equal(model.id,"test3", 'ID object not created.');
  126. cb(null,1);
  127. }, error: function(err) {
  128. cb("Error saving chat3",1);
  129. }});
  130. })
  131. ],function(err,results) {
  132. if (err) {
  133. throw new Error("Unexpected error running tests: " + err);
  134. }
  135. test.done();
  136. }
  137. );
  138. },testBulkLoadRecords: function (test) {
  139. var models = this.models;
  140. test.expect(4);
  141. async.series(
  142. [async.apply(function(cb) {
  143. var chats = new models.TestChatMessageCollection();
  144. chats.fetch({success: function(results) {
  145. test.equal(results.length,3,"Incorrect number of records found.");
  146. cb(null,1);
  147. }, error: function(err) {
  148. cb("Error loading chats",1);
  149. }});
  150. }),async.apply(function(cb) {
  151. Backbone.search(models.TestChatMessage,"test1",function(results) {
  152. test.equal(results.length,1,"Incorrect number of records found.");
  153. cb(null,2);
  154. }, function(err) {
  155. cb("Error loading chat",2);
  156. });
  157. }),async.apply(function(cb) {
  158. Backbone.search(models.TestChatMessage,"*",function(results) {
  159. test.equal(results.length,3,"Incorrect number of records found.");
  160. cb(null,3);
  161. }, function(err) {
  162. cb("Error loading chat",3);
  163. });
  164. }),async.apply(function(cb) {
  165. Backbone.search_delete(models.TestChatMessage,"*",function(deleted) {
  166. test.equal(parseInt(deleted),3,"Incorrect number of records deleted.");
  167. cb(null,3);
  168. }, function(err) {
  169. cb("Error cleaning up",3);
  170. });
  171. })
  172. ],function(err,results) {
  173. if (err) {
  174. throw new Error("Unexpected error running tests: " + err);
  175. }
  176. test.done();
  177. }
  178. );
  179. },testUpdates: function (test) {
  180. var models = this.models;
  181. test.expect(7);
  182. var chat1 = new models.TestChatMessage({id:"test1",user: 1, message:'Test 1'});
  183. async.series(
  184. [async.apply(function(cb) {
  185. chat1.save({id:"test1"},{success: function(model) {
  186. test.equal(model.get("id"),"test1",'ID attribute not created.');
  187. test.equal(model.id,"test1", 'ID object not created.');
  188. cb(null,1);
  189. }, error: function(err) {
  190. cb("Error saving chat1",1);
  191. }});
  192. }),async.apply(function(cb) {
  193. chat1.save({message:"Test 1 Updated"},{success: function(model) {
  194. test.equal(model.get('message'),"Test 1 Updated", 'Not the right record.');
  195. cb(null,2);
  196. }, error: function(err) {
  197. cb("Error updating chat1",2);
  198. }});
  199. }),async.apply(function(cb) {
  200. var chat_s = new models.TestChatMessage({id: "test1"});
  201. chat_s.fetch({success: function(model) {
  202. test.equal(parseInt(model.get("user")),1,"test1",'No the right record.');
  203. test.equal(model.id,"test1",'Not the right record.');
  204. test.equal(model.get('message'),"Test 1 Updated", 'Not the right record.');
  205. cb(null,2);
  206. }, error: function(err) {
  207. cb("Error saving chat1",3);
  208. }});
  209. }),async.apply(function(cb) {
  210. Backbone.search_delete(models.TestChatMessage,"*",function(deleted) {
  211. test.equal(parseInt(deleted),1,"Incorrect number of records deleted.");
  212. cb(null,3);
  213. }, function(err) {
  214. cb("Error cleaning up",4);
  215. });
  216. })
  217. ],function(err,results) {
  218. if (err) {
  219. throw new Error("Unexpected error running tests: " + err);
  220. }
  221. test.done();
  222. }
  223. );
  224. },testVolatile: function (test) {
  225. var models = this.models;
  226. test.expect(4);
  227. var vol1 = new models.TestVolatile({id:"testvol1",message:'Test 1'});
  228. async.series(
  229. [async.apply(function(cb) { //create record
  230. vol1.save({id:"testvol1"},{success: function(model) {
  231. test.equal(model.get("id"),"testvol1",'ID attribute not created.');
  232. test.equal(model.id,"testvol1", 'ID object not created.');
  233. cb(null,1);
  234. }, error: function(err) {
  235. cb("Error saving vol1",1);
  236. }});
  237. }),async.apply(function(cb) { // check it was created
  238. var vol_s = new models.TestVolatile({id: "testvol1"});
  239. vol_s.fetch({success: function(model) {
  240. test.equal(model.id,"testvol1",'Not the right record.');
  241. cb(null,2);
  242. }, error: function(err) {
  243. cb("Error loading vol1",3);
  244. }});
  245. }),async.apply(function(cb) { //wait more than one second
  246. setTimeout(function() {
  247. cb(null,3);
  248. },1500);
  249. }),async.apply(function(cb) { //wait more than one second
  250. var vol_s = new models.TestVolatile({id: "testvol1"});
  251. vol_s.fetch({success: function(model) {
  252. console.log("Volatile: " + model);
  253. test.ok(false,'Volatile record found when should be deleted.');
  254. cb(null,4);
  255. }, error: function(err) {
  256. test.ok(true);
  257. cb(null,4);
  258. }});
  259. })
  260. ],function(err,results) {
  261. if (err) {
  262. throw new Error("Unexpected error running tests: " + err);
  263. }
  264. test.done();
  265. }
  266. );
  267. }
  268. });