practical.test.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /**
  2. * Module dependencies
  3. */
  4. var assert = require('assert');
  5. var _ = require('@sailshq/lodash');
  6. var find = require('./fixtures/find.fixture');
  7. var validateButWith9CustomMethods = require('./fixtures/validate-but-with-9-custom-methods.fixture');
  8. var findButWithTimeout = require('./fixtures/find-but-with-timeout.fixture');
  9. var findButWithFinalAfterExecLC = require('./fixtures/find-but-with-final-after-exec-lc.fixture');
  10. /**
  11. * practical.test.js
  12. *
  13. * A simple test verifiying a couple of real-world use cases.
  14. */
  15. describe('practical.test.js', function() {
  16. // ██████╗ ██████╗ ███████╗████████╗███████╗███╗ ██╗██████╗
  17. // ██╔══██╗██╔══██╗██╔════╝╚══██╔══╝██╔════╝████╗ ██║██╔══██╗
  18. // ██████╔╝██████╔╝█████╗ ██║ █████╗ ██╔██╗ ██║██║ ██║
  19. // ██╔═══╝ ██╔══██╗██╔══╝ ██║ ██╔══╝ ██║╚██╗██║██║ ██║
  20. // ██║ ██║ ██║███████╗ ██║ ███████╗██║ ╚████║██████╔╝
  21. // ╚═╝ ╚═╝ ╚═╝╚══════╝ ╚═╝ ╚══════╝╚═╝ ╚═══╝╚═════╝
  22. //
  23. // ███████╗██╗███╗ ██╗██████╗ ██╗██╗
  24. // ██╔════╝██║████╗ ██║██╔══██╗██╔╝╚██╗
  25. // █████╗ ██║██╔██╗ ██║██║ ██║██║ ██║
  26. // ██╔══╝ ██║██║╚██╗██║██║ ██║██║ ██║
  27. // ██╗██║ ██║██║ ╚████║██████╔╝╚██╗██╔╝
  28. // ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚═════╝ ╚═╝╚═╝
  29. //
  30. describe('calling a simplified mock of Waterline\'s `find()` model method', function(){
  31. describe('simulated success', function(){
  32. it('should work with explicit callback', function(done){
  33. find(function (err, result) {
  34. if (err) { return done(err); }
  35. try {
  36. assert.deepEqual(result, [undefined]);
  37. } catch (e) { return done(e); }
  38. return done();
  39. });
  40. });
  41. it('should work with 1st arg + explicit callback', function(done){
  42. find({ where: {id:3} }, function (err, result) {
  43. if (err) { return done(err); }
  44. try {
  45. assert.deepEqual(result, [{ where:{id:3} }]);
  46. } catch (e) { return done(e); }
  47. return done();
  48. });
  49. });
  50. it('should work with .exec()', function(done){
  51. find().exec(function (err, result) {
  52. if (err) { return done(err); }
  53. try {
  54. assert.deepEqual(result, [undefined]);
  55. } catch (e) { return done(e); }
  56. return done();
  57. });
  58. });
  59. it('should work with 1st arg + .exec()', function(done){
  60. find({ where: {id:3} }).exec(function (err, result) {
  61. if (err) { return done(err); }
  62. try {
  63. assert.deepEqual(result, [{ where:{id:3} }]);
  64. } catch (e) { return done(e); }
  65. return done();
  66. });
  67. });
  68. it('should work with .where() + .exec()', function(done){
  69. find()
  70. .where({id:4})
  71. .exec(function (err, result) {
  72. if (err) { return done(err); }
  73. try {
  74. assert.deepEqual(result, [{ where:{id:4} }]);
  75. } catch (e) { return done(e); }
  76. return done();
  77. });
  78. });
  79. it('should work with 1st arg + .where() + .exec()', function(done){
  80. find({ where: {id:3, x:30} })
  81. .where({id:4})
  82. .exec(function (err, result) {
  83. if (err) { return done(err); }
  84. try {
  85. assert.deepEqual(result, [{ where:{id:4} }]);
  86. } catch (e) { return done(e); }
  87. return done();
  88. });
  89. });
  90. });//</ simulated success >
  91. });//</ calling simulated .find() >
  92. // ██╗ ██╗███████╗██╗███╗ ██╗ ██████╗ ██████╗██╗ ██╗███████╗████████╗ ██████╗ ███╗ ███╗
  93. // ██║ ██║██╔════╝██║████╗ ██║██╔════╝ ██╔════╝██║ ██║██╔════╝╚══██╔══╝██╔═══██╗████╗ ████║
  94. // ██║ ██║███████╗██║██╔██╗ ██║██║ ███╗ ██║ ██║ ██║███████╗ ██║ ██║ ██║██╔████╔██║
  95. // ██║ ██║╚════██║██║██║╚██╗██║██║ ██║ ██║ ██║ ██║╚════██║ ██║ ██║ ██║██║╚██╔╝██║
  96. // ╚██████╔╝███████║██║██║ ╚████║╚██████╔╝ ╚██████╗╚██████╔╝███████║ ██║ ╚██████╔╝██║ ╚═╝ ██║
  97. // ╚═════╝ ╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚══════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝
  98. //
  99. // ███╗ ███╗███████╗████████╗██╗ ██╗ ██████╗ ██████╗ ███████╗
  100. // ████╗ ████║██╔════╝╚══██╔══╝██║ ██║██╔═══██╗██╔══██╗██╔════╝
  101. // ██╔████╔██║█████╗ ██║ ███████║██║ ██║██║ ██║███████╗
  102. // ██║╚██╔╝██║██╔══╝ ██║ ██╔══██║██║ ██║██║ ██║╚════██║
  103. // ██║ ╚═╝ ██║███████╗ ██║ ██║ ██║╚██████╔╝██████╔╝███████║
  104. // ╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝
  105. //
  106. describe('calling something that takes advantage of parley\'s built-in support for custom methods', function(){
  107. it('should work with explicit callback', function(done){
  108. validateButWith9CustomMethods(function (err, result) {
  109. if (err) { return done(err); }
  110. try {
  111. assert.strictEqual(result, undefined);
  112. } catch (e) { return done(e); }
  113. return done();
  114. });
  115. });
  116. it('should work with .exec()', function(done){
  117. validateButWith9CustomMethods().exec(function (err, result) {
  118. if (err) { return done(err); }
  119. try {
  120. assert.strictEqual(result, undefined);
  121. } catch (e) { return done(e); }
  122. return done();
  123. });
  124. });
  125. it('should work with .then()', function(done){
  126. validateButWith9CustomMethods()
  127. .then(function (result) {
  128. try {
  129. assert.strictEqual(result, undefined);
  130. } catch (e) { return done(e); }
  131. return done();
  132. }).catch(function(err) { return done(err); });
  133. });
  134. it('should work with .b() + .exec()', function(done){
  135. validateButWith9CustomMethods()
  136. .b({id:4})
  137. .exec(function (err, result) {
  138. if (err) { return done(err); }
  139. try {
  140. assert.strictEqual(result, undefined);
  141. } catch (e) { return done(e); }
  142. return done();
  143. });
  144. });
  145. it('should work with .b() + .then()', function(done){
  146. validateButWith9CustomMethods()
  147. .b({id:4})
  148. .then(function (result) {
  149. try {
  150. assert.strictEqual(result, undefined);
  151. } catch (e) { return done(e); }
  152. return done();
  153. }).catch(function(err) { return done(err); });
  154. });
  155. });//</ calling something that takes advantage of parley\'s built-in support for custom methods >
  156. // ██╗ ██╗███████╗██╗███╗ ██╗ ██████╗
  157. // ██║ ██║██╔════╝██║████╗ ██║██╔════╝
  158. // ██║ ██║███████╗██║██╔██╗ ██║██║ ███╗
  159. // ██║ ██║╚════██║██║██║╚██╗██║██║ ██║
  160. // ╚██████╔╝███████║██║██║ ╚████║╚██████╔╝
  161. // ╚═════╝ ╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
  162. //
  163. // ████████╗██╗███╗ ███╗███████╗ ██████╗ ██╗ ██╗████████╗
  164. // ╚══██╔══╝██║████╗ ████║██╔════╝██╔═══██╗██║ ██║╚══██╔══╝
  165. // ██║ ██║██╔████╔██║█████╗ ██║ ██║██║ ██║ ██║
  166. // ██║ ██║██║╚██╔╝██║██╔══╝ ██║ ██║██║ ██║ ██║
  167. // ██║ ██║██║ ╚═╝ ██║███████╗╚██████╔╝╚██████╔╝ ██║
  168. // ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═════╝ ╚═╝
  169. //
  170. describe('calling something that takes advantage of parley\'s built-in support for timeouts', function(){
  171. describe('in cases where timeout is explicitly unsupported', function(){
  172. it('should NOT RESPECT TIMEOUT WHEN given an explicit callback', function(done){
  173. findButWithTimeout(function (err, result) {
  174. if (err) { return done(err); }
  175. try {
  176. assert.deepEqual(result, [undefined]);
  177. } catch (e) { return done(e); }
  178. return done();
  179. });
  180. });
  181. it('should NOT RESPECT TIMEOUT WHEN given 1st arg + explicit callback', function(done){
  182. findButWithTimeout({ where: {id:3} }, function (err, result) {
  183. if (err) { return done(err); }
  184. try {
  185. assert.deepEqual(result, [{ where:{id:3} }]);
  186. } catch (e) { return done(e); }
  187. return done();
  188. });
  189. });
  190. });
  191. describe('in cases where timeout is supposed to work', function(){
  192. it('should time out properly given .exec()', function(done){
  193. findButWithTimeout().exec(function (err, result) {
  194. try {
  195. assert.equal(err.name, 'TimeoutError');
  196. } catch (e) { return done(e); }
  197. return done();
  198. });
  199. });
  200. it('should time out properly given 1st arg + .exec()', function(done){
  201. findButWithTimeout({ where: {id:3} }).exec(function (err, result) {
  202. try {
  203. assert.equal(err.name, 'TimeoutError');
  204. } catch (e) { return done(e); }
  205. return done();
  206. });
  207. });
  208. it('should time out properly given .where() + .exec()', function(done){
  209. findButWithTimeout()
  210. .where({id:4})
  211. .exec(function (err, result) {
  212. try {
  213. assert.equal(err.name, 'TimeoutError');
  214. } catch (e) { return done(e); }
  215. return done();
  216. });
  217. });
  218. it('should time out properly given 1st arg + .where() + .exec()', function(done){
  219. findButWithTimeout({ where: {id:3, x:30} })
  220. .where({id:4})
  221. .exec(function (err) {
  222. try {
  223. assert.equal(err.name, 'TimeoutError');
  224. } catch (e) { return done(e); }
  225. return done();
  226. });
  227. });
  228. });
  229. });//</ calling something that takes advantage of parley\'s built-in support for timeouts >
  230. // ██╗ ██╗███████╗██╗███╗ ██╗ ██████╗ ██╗ ██████╗
  231. // ██║ ██║██╔════╝██║████╗ ██║██╔════╝ ██║ ██╔════╝██╗
  232. // ██║ ██║███████╗██║██╔██╗ ██║██║ ███╗ ██║ ██║ ╚═╝
  233. // ██║ ██║╚════██║██║██║╚██╗██║██║ ██║ ██║ ██║ ██╗
  234. // ╚██████╔╝███████║██║██║ ╚████║╚██████╔╝ ███████╗╚██████╗╚═╝
  235. // ╚═════╝ ╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ ╚══════╝ ╚═════╝
  236. //
  237. // ██╗███╗ ██╗████████╗███████╗██████╗ ██████╗███████╗██████╗ ████████╗ █████╗ ███████╗████████╗███████╗██████╗ ███████╗██╗ ██╗███████╗ ██████╗
  238. // ██║████╗ ██║╚══██╔══╝██╔════╝██╔══██╗██╔════╝██╔════╝██╔══██╗╚══██╔══╝██╔══██╗██╔════╝╚══██╔══╝██╔════╝██╔══██╗██╔════╝╚██╗██╔╝██╔════╝██╔════╝
  239. // ██║██╔██╗ ██║ ██║ █████╗ ██████╔╝██║ █████╗ ██████╔╝ ██║ ███████║█████╗ ██║ █████╗ ██████╔╝█████╗ ╚███╔╝ █████╗ ██║
  240. // ██║██║╚██╗██║ ██║ ██╔══╝ ██╔══██╗██║ ██╔══╝ ██╔═══╝ ██║ ██╔══██║██╔══╝ ██║ ██╔══╝ ██╔══██╗██╔══╝ ██╔██╗ ██╔══╝ ██║
  241. // ██║██║ ╚████║ ██║ ███████╗██║ ██║╚██████╗███████╗██║ ██║ ██║ ██║██║ ██║ ███████╗██║ ██║███████╗██╔╝ ██╗███████╗╚██████╗
  242. // ╚═╝╚═╝ ╚═══╝ ╚═╝ ╚══════╝╚═╝ ╚═╝ ╚═════╝╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚══════╝ ╚═════╝
  243. //
  244. describe('calling something that takes advantage of interceptAfterExec', function(){
  245. describe('in cases where interceptAfterExec is explicitly unsupported given an explicit callback with success (i.e. where the LC might normally modify the result/error if we were using .exec())', function(){
  246. it('should NOT RESPECT LC WHEN given explicit callback as first arg', function(done){
  247. findButWithFinalAfterExecLC(function (err, result) {
  248. if (err) { return done(err); }
  249. try {
  250. assert.deepEqual(result, [undefined]);
  251. } catch (e) { return done(e); }
  252. return done();
  253. });
  254. });
  255. it('should NOT RESPECT LC WHEN given 1st arg + explicit callback', function(done){
  256. findButWithFinalAfterExecLC({ where: {id:3} }, function (err, result) {
  257. if (err) { return done(err); }
  258. try {
  259. assert.deepEqual(result, [{ where:{id:3} }]);
  260. } catch (e) { return done(e); }
  261. return done();
  262. });
  263. });
  264. });
  265. describe('in cases where this is supposed to work', function(){
  266. it('should work normally given .exec() with an error, where the LC is a pass-through', function(done){
  267. findButWithFinalAfterExecLC(false).exec(function (err) {
  268. try {
  269. assert(_.isError(err), 'Expecting `err` to be an Error instance! But instead got: '+err);
  270. assert.equal(err.code, 'E_SOME_ERROR', 'Expected error with a `code` of "E_SOME_ERROR". But instead, got an error with a different code (`'+err.code+'`). Here\'s the error: '+err);
  271. } catch (e) { return done(e); }
  272. return done();
  273. });
  274. });
  275. it('should work normally given .exec() with success, where the LC is a pass-through', function(done){
  276. findButWithFinalAfterExecLC(true).exec(function (err, result) {
  277. try {
  278. assert(!err, 'Got unexpected error in test: '+err);
  279. assert(_.isArray(result), 'Expecting result to be an array! But instead got: '+result);
  280. assert.equal(result.length, 1);
  281. assert.equal(result[0], true);
  282. } catch (e) { return done(e); }
  283. return done();
  284. });
  285. });
  286. it('should properly apply changes from LC given .exec() with an error', function(done){
  287. findButWithFinalAfterExecLC(null).exec(function (err) {
  288. try {
  289. assert(_.isError(err), 'Expecting `err` to be an Error instance! But instead got: '+err);
  290. assert.equal(err.code, 'E_SOME_UNRECOGNIZED_ERROR', 'Expected error with a `code` of "E_SOME_UNRECOGNIZED_ERROR". But instead, got an error with a different code (`'+err.code+'`). Here\'s the error: '+err);
  291. } catch (e) { return done(e); }
  292. return done();
  293. });
  294. });
  295. it('should properly apply changes from LC given .exec() with success', function(done){
  296. findButWithFinalAfterExecLC().exec(function (err, result) {
  297. try {
  298. assert(!err, 'Got unexpected error in test: '+err);
  299. assert(_.isArray(result), 'Expecting result to be an array! But instead got: '+result);
  300. assert.equal(result.length, 2);
  301. assert.equal(result[0], undefined);
  302. assert.deepEqual(result[1], { fake: true });
  303. } catch (e) { return done(e); }
  304. return done();
  305. });
  306. });
  307. });
  308. });//</ calling something that takes advantage of interceptAfterExec >
  309. // ██╗███╗ ██╗████████╗███████╗██████╗ ██████╗███████╗██████╗ ████████╗ ██╗██╗
  310. // ██║████╗ ██║╚══██╔══╝██╔════╝██╔══██╗██╔════╝██╔════╝██╔══██╗╚══██╔══╝██╔╝╚██╗
  311. // ██║██╔██╗ ██║ ██║ █████╗ ██████╔╝██║ █████╗ ██████╔╝ ██║ ██║ ██║
  312. // ██║██║╚██╗██║ ██║ ██╔══╝ ██╔══██╗██║ ██╔══╝ ██╔═══╝ ██║ ██║ ██║
  313. // ██╗██║██║ ╚████║ ██║ ███████╗██║ ██║╚██████╗███████╗██║ ██║ ╚██╗██╔╝
  314. // ╚═╝╚═╝╚═╝ ╚═══╝ ╚═╝ ╚══════╝╚═╝ ╚═╝ ╚═════╝╚══════╝╚═╝ ╚═╝ ╚═╝╚═╝
  315. //
  316. describe('calling find().intercept()', function(){
  317. it('should ignore `.intercept()` if find() returns successfully', function(done){
  318. var didRunIntercept;
  319. find()
  320. .intercept('E_SOME_ERROR', function(err){
  321. didRunIntercept = true;
  322. return err;
  323. })
  324. .exec(function (err, result) {
  325. if (err) { return done(err); }
  326. try {
  327. assert.deepEqual(result, [undefined]);
  328. assert(!didRunIntercept);
  329. } catch (e) { return done(e); }
  330. return done();
  331. });
  332. });
  333. it('should ignore `.intercept()` if find() throws an unrelated exception', function(done){
  334. var didRunIntercept;
  335. find(false)
  336. .intercept('E_SOME_OTHER_ERROR_THAT_WONT_BE_THROWN', function(err){
  337. didRunIntercept = true;
  338. return err;
  339. })
  340. .exec(function (err) {
  341. try {
  342. assert(_.isError(err), 'Expecting `err` to be an Error instance! But instead got: '+err);
  343. assert.equal(err.code, 'E_SOME_ERROR', 'Expected error with a `code` of "E_SOME_ERROR". But instead, got an error with a different code (`'+err.code+'`). Here\'s the error: '+err);
  344. assert(!didRunIntercept);
  345. } catch (e) { return done(e); }
  346. return done();
  347. });
  348. });
  349. it('should run `.intercept()` if find() throws a matching exception, and the final error thrown should be whatever .intercept() returned', function(done){
  350. var didRunIntercept;
  351. find(false)
  352. .intercept('E_SOME_ERROR', function(err){
  353. didRunIntercept = true;
  354. var newErr = new Error('Some new error (original err:'+err+')');
  355. newErr.code = 'E_MASHED_POTATOES';
  356. return newErr;
  357. })
  358. .exec(function (err) {
  359. try {
  360. assert(_.isError(err), 'Expecting `err` to be an Error instance! But instead got: '+err);
  361. assert.equal(err.code, 'E_MASHED_POTATOES', 'Expected error with a `code` of "E_MASHED_POTATOES", because it should have been intercepted and rethrown automatically using the return value from .intercept(). But instead, got an error with a different code (`'+err.code+'`). Here\'s the error: '+err);
  362. assert(didRunIntercept);
  363. } catch (e) { return done(e); }
  364. return done();
  365. });
  366. });
  367. it('should pass in the proper error as the first and only argument to the LC', function(done){
  368. var argReceivedInLC;
  369. find(false)
  370. .intercept('E_SOME_ERROR', function(err){
  371. argReceivedInLC = err;
  372. var newErr = new Error('Some new error (original err:'+err+')');
  373. newErr.code = 'E_MASHED_POTATOES';
  374. return newErr;
  375. })
  376. .exec(function () {
  377. try {
  378. assert(_.isError(argReceivedInLC), 'Expecting arg received in LC to be an Error instance! But instead got: '+argReceivedInLC);
  379. assert.equal(argReceivedInLC.code, 'E_SOME_ERROR', 'Expected error with a `code` of "E_SOME_ERROR". But instead, got an error with a different code (`'+argReceivedInLC.code+'`). Here\'s the error: '+argReceivedInLC);
  380. } catch (e) { return done(e); }
  381. return done();
  382. });
  383. });
  384. it('should still call the final after exec LC from implementorland, if one was configured (and it should call it last, with the modifications from this LC already taken into account)', function(done){
  385. findButWithFinalAfterExecLC(false)
  386. .intercept(function(err){
  387. return new Error('Some other error (interecepted from original error: '+err.stack+')');
  388. })
  389. .exec(function (err) {
  390. try {
  391. assert(_.isError(err), 'Expecting `err` to be an Error instance! But instead got: '+err);
  392. assert.equal(err.code, 'E_SOME_UNRECOGNIZED_ERROR', 'Expected error with a `code` of "E_SOME_UNRECOGNIZED_ERROR". But instead, got an error with a different code (`'+err.code+'`). Here\'s the error: '+err);
  393. } catch (e) { return done(e); }
  394. return done();
  395. });
  396. });
  397. });//</ calling find().intercept() >
  398. // ████████╗ ██████╗ ██╗ ███████╗██████╗ █████╗ ████████╗███████╗ ██╗██╗
  399. // ╚══██╔══╝██╔═══██╗██║ ██╔════╝██╔══██╗██╔══██╗╚══██╔══╝██╔════╝██╔╝╚██╗
  400. // ██║ ██║ ██║██║ █████╗ ██████╔╝███████║ ██║ █████╗ ██║ ██║
  401. // ██║ ██║ ██║██║ ██╔══╝ ██╔══██╗██╔══██║ ██║ ██╔══╝ ██║ ██║
  402. // ██╗██║ ╚██████╔╝███████╗███████╗██║ ██║██║ ██║ ██║ ███████╗╚██╗██╔╝
  403. // ╚═╝╚═╝ ╚═════╝ ╚══════╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚══════╝ ╚═╝╚═╝
  404. //
  405. describe('calling .find().tolerate()', function(){
  406. it('should ignore `.tolerate()` if find() returns successfully', function(done){
  407. var didRunTolerate;
  408. find()
  409. .tolerate('E_SOME_ERROR', function(){
  410. didRunTolerate = true;
  411. })
  412. .exec(function (err, result) {
  413. if (err) { return done(err); }
  414. try {
  415. assert.deepEqual(result, [undefined]);
  416. assert(!didRunTolerate);
  417. } catch (e) { return done(e); }
  418. return done();
  419. });
  420. });
  421. it('should ignore `.tolerate()` if find() throws an unrelated exception', function(done){
  422. var didRunTolerate;
  423. find(false)
  424. .tolerate('E_SOME_OTHER_ERROR_THAT_WONT_BE_THROWN', function(){
  425. didRunTolerate = true;
  426. })
  427. .exec(function (err) {
  428. try {
  429. assert(_.isError(err), 'Expecting `err` to be an Error instance! But instead got: '+err);
  430. assert.equal(err.code, 'E_SOME_ERROR', 'Expected error with a `code` of "E_SOME_ERROR". But instead, got an error with a different code (`'+err.code+'`). Here\'s the error: '+err);
  431. assert(!didRunTolerate);
  432. } catch (e) { return done(e); }
  433. return done();
  434. });
  435. });
  436. it('should run `.tolerate()` if find() throws a matching exception, and the final result should be whatever .tolerate() returned', function(done){
  437. var didRunTolerate;
  438. find(false)
  439. .tolerate('E_SOME_ERROR', function(){
  440. didRunTolerate = true;
  441. return 'mm mmm mashed potatoes';
  442. })
  443. .exec(function (err, result) {
  444. if (err) { return done(err); }
  445. try {
  446. assert(didRunTolerate);
  447. assert.deepEqual(result, 'mm mmm mashed potatoes');
  448. } catch (e) { return done(e); }
  449. return done();
  450. });
  451. });
  452. it('should honor `.tolerate()` even if it doesn\'t have a LC -- assuming find() throws a matching exception. (In this case, the final result should be `undefined`.)', function(done){
  453. find(false)
  454. .tolerate('E_SOME_ERROR')
  455. .exec(function (err, result) {
  456. if (err) { return done(err); }
  457. try {
  458. assert.deepEqual(result, undefined);
  459. } catch (e) { return done(e); }
  460. return done();
  461. });
  462. });
  463. it('should run `.tolerate()` even if it is completely generic (i.e. not filtered by any rule at all) -- assuming find() throws any kind of error. (In this case, the final result should be whatever the `.tolerate()` LC returned.)', function(done){
  464. var didRunTolerate;
  465. find(false)
  466. .tolerate(function(){
  467. didRunTolerate = true;
  468. return 'mm mmm mashed potatoes';
  469. })
  470. .exec(function (err, result) {
  471. if (err) { return done(err); }
  472. try {
  473. assert(didRunTolerate);
  474. assert.deepEqual(result, 'mm mmm mashed potatoes');
  475. } catch (e) { return done(e); }
  476. return done();
  477. });
  478. });
  479. it('should pass in the proper error as the first and only argument to the LC', function(done){
  480. var argReceivedInLC;
  481. find(false)
  482. .tolerate(function(err){
  483. argReceivedInLC = err;
  484. })
  485. .exec(function () {
  486. try {
  487. assert(_.isError(argReceivedInLC), 'Expecting arg received in LC to be an Error instance! But instead got: '+argReceivedInLC);
  488. assert.equal(argReceivedInLC.code, 'E_SOME_ERROR', 'Expected error with a `code` of "E_SOME_ERROR". But instead, got an error with a different code (`'+argReceivedInLC.code+'`). Here\'s the error: '+argReceivedInLC);
  489. } catch (e) { return done(e); }
  490. return done();
  491. });
  492. });
  493. it('should still call the final after exec LC from implementorland, if one was configured (and it should call it last, with the modifications from this LC already taken into account)', function(done){
  494. findButWithFinalAfterExecLC(false)
  495. .tolerate(function(){ return ['how many', 'mashed potatoes', 'will it take??!']; })
  496. .exec(function (err, result) {
  497. if (err) { return done(err); }
  498. try {
  499. assert.deepEqual(result, ['how many', 'mashed potatoes', 'will it take??!', {fake: true}]);
  500. } catch (e) { return done(e); }
  501. return done();
  502. });
  503. });
  504. });//</ calling .find().tolerate() >
  505. });