index.js 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925
  1. 'use strict';
  2. const mquery = require('../');
  3. const assert = require('assert');
  4. describe('mquery', function() {
  5. let col;
  6. before(function(done) {
  7. // get the env specific collection interface
  8. require('./env').getCollection(function(err, collection) {
  9. assert.ifError(err);
  10. col = collection;
  11. done();
  12. });
  13. });
  14. after(function(done) {
  15. require('./env').dropCollection(done);
  16. });
  17. describe('mquery', function() {
  18. it('is a function', function() {
  19. assert.equal('function', typeof mquery);
  20. });
  21. it('creates instances with the `new` keyword', function() {
  22. assert.ok(mquery() instanceof mquery);
  23. });
  24. describe('defaults', function() {
  25. it('are set', function() {
  26. const m = mquery();
  27. assert.strictEqual(undefined, m.op);
  28. assert.deepEqual({}, m.options);
  29. });
  30. });
  31. describe('criteria', function() {
  32. it('if collection-like is used as collection', function() {
  33. const m = mquery(col);
  34. assert.equal(col, m._collection.collection);
  35. });
  36. it('non-collection-like is used as criteria', function() {
  37. const m = mquery({ works: true });
  38. assert.ok(!m._collection);
  39. assert.deepEqual({ works: true }, m._conditions);
  40. });
  41. });
  42. describe('options', function() {
  43. it('are merged when passed', function() {
  44. let m;
  45. m = mquery(col, { w: 'majority' });
  46. assert.deepEqual({ w: 'majority' }, m.options);
  47. m = mquery({ name: 'mquery' }, { w: 'majority' });
  48. assert.deepEqual({ w: 'majority' }, m.options);
  49. });
  50. });
  51. });
  52. describe('toConstructor', function() {
  53. it('creates subclasses of mquery', function() {
  54. const opts = { safe: { w: 'majority' }, readPreference: 'p' };
  55. const match = { name: 'test', count: { $gt: 101 } };
  56. const select = { name: 1, count: 0 };
  57. const update = { $set: { x: true } };
  58. const path = 'street';
  59. const q = mquery().setOptions(opts);
  60. q.where(match);
  61. q.select(select);
  62. q.updateOne(update);
  63. q.where(path);
  64. q.find();
  65. const M = q.toConstructor();
  66. const m = M();
  67. assert.ok(m instanceof mquery);
  68. assert.deepEqual(opts, m.options);
  69. assert.deepEqual(match, m._conditions);
  70. assert.deepEqual(select, m._fields);
  71. assert.deepEqual(update, m._update);
  72. assert.equal(path, m._path);
  73. assert.equal('find', m.op);
  74. });
  75. });
  76. describe('setOptions', function() {
  77. it('calls associated methods', function() {
  78. const m = mquery();
  79. assert.equal(m._collection, null);
  80. m.setOptions({ collection: col });
  81. assert.equal(m._collection.collection, col);
  82. });
  83. it('directly sets option when no method exists', function() {
  84. const m = mquery();
  85. assert.equal(m.options.woot, null);
  86. m.setOptions({ woot: 'yay' });
  87. assert.equal(m.options.woot, 'yay');
  88. });
  89. it('is chainable', function() {
  90. const m = mquery();
  91. let n;
  92. n = m.setOptions();
  93. assert.equal(m, n);
  94. n = m.setOptions({ x: 1 });
  95. assert.equal(m, n);
  96. });
  97. });
  98. describe('collection', function() {
  99. it('sets the _collection', function() {
  100. const m = mquery();
  101. m.collection(col);
  102. assert.equal(m._collection.collection, col);
  103. });
  104. it('is chainable', function() {
  105. const m = mquery();
  106. const n = m.collection(col);
  107. assert.equal(m, n);
  108. });
  109. });
  110. describe('$where', function() {
  111. it('sets the $where condition', function() {
  112. const m = mquery();
  113. function go() {}
  114. m.$where(go);
  115. assert.ok(go === m._conditions.$where);
  116. });
  117. it('is chainable', function() {
  118. const m = mquery();
  119. const n = m.$where('x');
  120. assert.equal(m, n);
  121. });
  122. });
  123. describe('where', function() {
  124. it('without arguments', function() {
  125. const m = mquery();
  126. m.where();
  127. assert.deepEqual({}, m._conditions);
  128. });
  129. it('with non-string/object argument', function() {
  130. const m = mquery();
  131. assert.throws(function() {
  132. m.where([]);
  133. }, /path must be a string or object/);
  134. });
  135. describe('with one argument', function() {
  136. it('that is an object', function() {
  137. const m = mquery();
  138. m.where({ name: 'flawed' });
  139. assert.strictEqual(m._conditions.name, 'flawed');
  140. });
  141. it('that is a query', function() {
  142. const m = mquery({ name: 'first' });
  143. const n = mquery({ name: 'changed' });
  144. m.where(n);
  145. assert.strictEqual(m._conditions.name, 'changed');
  146. });
  147. it('that is a string', function() {
  148. const m = mquery();
  149. m.where('name');
  150. assert.equal('name', m._path);
  151. assert.strictEqual(m._conditions.name, undefined);
  152. });
  153. });
  154. it('with two arguments', function() {
  155. const m = mquery();
  156. m.where('name', 'The Great Pumpkin');
  157. assert.equal('name', m._path);
  158. assert.strictEqual(m._conditions.name, 'The Great Pumpkin');
  159. });
  160. it('is chainable', function() {
  161. const m = mquery();
  162. let n = m.where('x', 'y');
  163. assert.equal(m, n);
  164. n = m.where();
  165. assert.equal(m, n);
  166. });
  167. });
  168. describe('equals', function() {
  169. it('must be called after where()', function() {
  170. const m = mquery();
  171. assert.throws(function() {
  172. m.equals();
  173. }, /must be used after where/);
  174. });
  175. it('sets value of path set with where()', function() {
  176. const m = mquery();
  177. m.where('age').equals(1000);
  178. assert.deepEqual({ age: 1000 }, m._conditions);
  179. });
  180. it('is chainable', function() {
  181. const m = mquery();
  182. const n = m.where('x').equals(3);
  183. assert.equal(m, n);
  184. });
  185. });
  186. describe('eq', function() {
  187. it('is alias of equals', function() {
  188. const m = mquery();
  189. m.where('age').eq(1000);
  190. assert.deepEqual({ age: 1000 }, m._conditions);
  191. });
  192. });
  193. describe('or', function() {
  194. it('pushes onto the internal $or condition', function() {
  195. const m = mquery();
  196. m.or({ 'Nightmare Before Christmas': true });
  197. assert.deepEqual([{ 'Nightmare Before Christmas': true }], m._conditions.$or);
  198. });
  199. it('allows passing arrays', function() {
  200. const m = mquery();
  201. const arg = [{ 'Nightmare Before Christmas': true }, { x: 1 }];
  202. m.or(arg);
  203. assert.deepEqual(arg, m._conditions.$or);
  204. });
  205. it('allows calling multiple times', function() {
  206. const m = mquery();
  207. const arg = [{ looper: true }, { x: 1 }];
  208. m.or(arg);
  209. m.or({ y: 1 });
  210. m.or([{ w: 'oo' }, { z: 'oo' }]);
  211. assert.deepEqual([{ looper: true }, { x: 1 }, { y: 1 }, { w: 'oo' }, { z: 'oo' }], m._conditions.$or);
  212. });
  213. it('is chainable', function() {
  214. const m = mquery();
  215. m.or({ o: 'k' }).where('name', 'table');
  216. assert.deepEqual({ name: 'table', $or: [{ o: 'k' }] }, m._conditions);
  217. });
  218. });
  219. describe('nor', function() {
  220. it('pushes onto the internal $nor condition', function() {
  221. const m = mquery();
  222. m.nor({ 'Nightmare Before Christmas': true });
  223. assert.deepEqual([{ 'Nightmare Before Christmas': true }], m._conditions.$nor);
  224. });
  225. it('allows passing arrays', function() {
  226. const m = mquery();
  227. const arg = [{ 'Nightmare Before Christmas': true }, { x: 1 }];
  228. m.nor(arg);
  229. assert.deepEqual(arg, m._conditions.$nor);
  230. });
  231. it('allows calling multiple times', function() {
  232. const m = mquery();
  233. const arg = [{ looper: true }, { x: 1 }];
  234. m.nor(arg);
  235. m.nor({ y: 1 });
  236. m.nor([{ w: 'oo' }, { z: 'oo' }]);
  237. assert.deepEqual([{ looper: true }, { x: 1 }, { y: 1 }, { w: 'oo' }, { z: 'oo' }], m._conditions.$nor);
  238. });
  239. it('is chainable', function() {
  240. const m = mquery();
  241. m.nor({ o: 'k' }).where('name', 'table');
  242. assert.deepEqual({ name: 'table', $nor: [{ o: 'k' }] }, m._conditions);
  243. });
  244. });
  245. describe('and', function() {
  246. it('pushes onto the internal $and condition', function() {
  247. const m = mquery();
  248. m.and({ 'Nightmare Before Christmas': true });
  249. assert.deepEqual([{ 'Nightmare Before Christmas': true }], m._conditions.$and);
  250. });
  251. it('allows passing arrays', function() {
  252. const m = mquery();
  253. const arg = [{ 'Nightmare Before Christmas': true }, { x: 1 }];
  254. m.and(arg);
  255. assert.deepEqual(arg, m._conditions.$and);
  256. });
  257. it('allows calling multiple times', function() {
  258. const m = mquery();
  259. const arg = [{ looper: true }, { x: 1 }];
  260. m.and(arg);
  261. m.and({ y: 1 });
  262. m.and([{ w: 'oo' }, { z: 'oo' }]);
  263. assert.deepEqual([{ looper: true }, { x: 1 }, { y: 1 }, { w: 'oo' }, { z: 'oo' }], m._conditions.$and);
  264. });
  265. it('is chainable', function() {
  266. const m = mquery();
  267. m.and({ o: 'k' }).where('name', 'table');
  268. assert.deepEqual({ name: 'table', $and: [{ o: 'k' }] }, m._conditions);
  269. });
  270. });
  271. function generalCondition(type) {
  272. return function() {
  273. it('accepts 2 args', function() {
  274. const m = mquery()[type]('count', 3);
  275. const check = {};
  276. check['$' + type] = 3;
  277. assert.deepEqual(m._conditions.count, check);
  278. });
  279. it('uses previously set `where` path if 1 arg passed', function() {
  280. const m = mquery().where('count')[type](3);
  281. const check = {};
  282. check['$' + type] = 3;
  283. assert.deepEqual(m._conditions.count, check);
  284. });
  285. it('throws if 1 arg was passed but no previous `where` was used', function() {
  286. assert.throws(function() {
  287. mquery()[type](3);
  288. }, /must be used after where/);
  289. });
  290. it('is chainable', function() {
  291. const m = mquery().where('count')[type](3).where('x', 8);
  292. const check = { x: 8, count: {} };
  293. check.count['$' + type] = 3;
  294. assert.deepEqual(m._conditions, check);
  295. });
  296. it('overwrites previous value', function() {
  297. const m = mquery().where('count')[type](3)[type](8);
  298. const check = {};
  299. check['$' + type] = 8;
  300. assert.deepEqual(m._conditions.count, check);
  301. });
  302. };
  303. }
  304. 'gt gte lt lte ne in nin regex size maxDistance minDistance'.split(' ').forEach(function(type) {
  305. describe(type, generalCondition(type));
  306. });
  307. describe('mod', function() {
  308. describe('with 1 argument', function() {
  309. it('requires a previous where()', function() {
  310. assert.throws(function() {
  311. mquery().mod([30, 10]);
  312. }, /must be used after where/);
  313. });
  314. it('works', function() {
  315. const m = mquery().where('madmen').mod([10, 20]);
  316. assert.deepEqual(m._conditions, { madmen: { $mod: [10, 20] } });
  317. });
  318. });
  319. describe('with 2 arguments and second is non-Array', function() {
  320. it('requires a previous where()', function() {
  321. assert.throws(function() {
  322. mquery().mod('x', 10);
  323. }, /must be used after where/);
  324. });
  325. it('works', function() {
  326. const m = mquery().where('madmen').mod(10, 20);
  327. assert.deepEqual(m._conditions, { madmen: { $mod: [10, 20] } });
  328. });
  329. });
  330. it('with 2 arguments and second is an array', function() {
  331. const m = mquery().mod('madmen', [10, 20]);
  332. assert.deepEqual(m._conditions, { madmen: { $mod: [10, 20] } });
  333. });
  334. it('with 3 arguments', function() {
  335. const m = mquery().mod('madmen', 10, 20);
  336. assert.deepEqual(m._conditions, { madmen: { $mod: [10, 20] } });
  337. });
  338. it('is chainable', function() {
  339. const m = mquery().mod('madmen', 10, 20).where('x', 8);
  340. const check = { madmen: { $mod: [10, 20] }, x: 8 };
  341. assert.deepEqual(m._conditions, check);
  342. });
  343. });
  344. describe('exists', function() {
  345. it('with 0 args', function() {
  346. it('throws if not used after where()', function() {
  347. assert.throws(function() {
  348. mquery().exists();
  349. }, /must be used after where/);
  350. });
  351. it('works', function() {
  352. const m = mquery().where('name').exists();
  353. const check = { name: { $exists: true } };
  354. assert.deepEqual(m._conditions, check);
  355. });
  356. });
  357. describe('with 1 arg', function() {
  358. describe('that is boolean', function() {
  359. it('throws if not used after where()', function() {
  360. assert.throws(function() {
  361. mquery().exists();
  362. }, /must be used after where/);
  363. });
  364. it('works', function() {
  365. const m = mquery().exists('name', false);
  366. const check = { name: { $exists: false } };
  367. assert.deepEqual(m._conditions, check);
  368. });
  369. });
  370. describe('that is not boolean', function() {
  371. it('sets the value to `true`', function() {
  372. const m = mquery().where('name').exists('yummy');
  373. const check = { yummy: { $exists: true } };
  374. assert.deepEqual(m._conditions, check);
  375. });
  376. });
  377. });
  378. describe('with 2 args', function() {
  379. it('works', function() {
  380. const m = mquery().exists('yummy', false);
  381. const check = { yummy: { $exists: false } };
  382. assert.deepEqual(m._conditions, check);
  383. });
  384. });
  385. it('is chainable', function() {
  386. const m = mquery().where('name').exists().find({ x: 1 });
  387. const check = { name: { $exists: true }, x: 1 };
  388. assert.deepEqual(m._conditions, check);
  389. });
  390. });
  391. describe('elemMatch', function() {
  392. describe('with null/undefined first argument', function() {
  393. assert.throws(function() {
  394. mquery().elemMatch();
  395. }, /Invalid argument/);
  396. assert.throws(function() {
  397. mquery().elemMatch(null);
  398. }, /Invalid argument/);
  399. assert.doesNotThrow(function() {
  400. mquery().elemMatch('', {});
  401. });
  402. });
  403. describe('with 1 argument', function() {
  404. it('throws if not a function or object', function() {
  405. assert.throws(function() {
  406. mquery().elemMatch([]);
  407. }, /Invalid argument/);
  408. });
  409. describe('that is an object', function() {
  410. it('throws if no previous `where` was used', function() {
  411. assert.throws(function() {
  412. mquery().elemMatch({});
  413. }, /must be used after where/);
  414. });
  415. it('works', function() {
  416. const m = mquery().where('comment').elemMatch({ author: 'joe', votes: { $gte: 3 } });
  417. assert.deepEqual({ comment: { $elemMatch: { author: 'joe', votes: { $gte: 3 } } } }, m._conditions);
  418. });
  419. });
  420. describe('that is a function', function() {
  421. it('throws if no previous `where` was used', function() {
  422. assert.throws(function() {
  423. mquery().elemMatch(function() {});
  424. }, /must be used after where/);
  425. });
  426. it('works', function() {
  427. const m = mquery().where('comment').elemMatch(function(query) {
  428. query.where({ author: 'joe', votes: { $gte: 3 } });
  429. });
  430. assert.deepEqual({ comment: { $elemMatch: { author: 'joe', votes: { $gte: 3 } } } }, m._conditions);
  431. });
  432. });
  433. });
  434. describe('with 2 arguments', function() {
  435. describe('and the 2nd is an object', function() {
  436. it('works', function() {
  437. const m = mquery().elemMatch('comment', { author: 'joe', votes: { $gte: 3 } });
  438. assert.deepEqual({ comment: { $elemMatch: { author: 'joe', votes: { $gte: 3 } } } }, m._conditions);
  439. });
  440. });
  441. describe('and the 2nd is a function', function() {
  442. it('works', function() {
  443. const m = mquery().elemMatch('comment', function(query) {
  444. query.where({ author: 'joe', votes: { $gte: 3 } });
  445. });
  446. assert.deepEqual({ comment: { $elemMatch: { author: 'joe', votes: { $gte: 3 } } } }, m._conditions);
  447. });
  448. });
  449. it('and the 2nd is not a function or object', function() {
  450. assert.throws(function() {
  451. mquery().elemMatch('comment', []);
  452. }, /Invalid argument/);
  453. });
  454. });
  455. });
  456. describe('within', function() {
  457. it('is chainable', function() {
  458. const m = mquery();
  459. assert.equal(m.where('a').within(), m);
  460. });
  461. describe('when called with arguments', function() {
  462. it('must follow where()', function() {
  463. assert.throws(function() {
  464. mquery().within([]);
  465. }, /must be used after where/);
  466. });
  467. describe('of length 1', function() {
  468. it('throws if not a recognized shape', function() {
  469. assert.throws(function() {
  470. mquery().where('loc').within({});
  471. }, /Invalid argument/);
  472. assert.throws(function() {
  473. mquery().where('loc').within(null);
  474. }, /Invalid argument/);
  475. });
  476. it('delegates to circle when center exists', function() {
  477. const m = mquery().where('loc').within({ center: [10, 10], radius: 3 });
  478. assert.deepEqual({ $geoWithin: { $center: [[10, 10], 3] } }, m._conditions.loc);
  479. });
  480. it('delegates to box when exists', function() {
  481. const m = mquery().where('loc').within({ box: [[10, 10], [11, 14]] });
  482. assert.deepEqual({ $geoWithin: { $box: [[10, 10], [11, 14]] } }, m._conditions.loc);
  483. });
  484. it('delegates to polygon when exists', function() {
  485. const m = mquery().where('loc').within({ polygon: [[10, 10], [11, 14], [10, 9]] });
  486. assert.deepEqual({ $geoWithin: { $polygon: [[10, 10], [11, 14], [10, 9]] } }, m._conditions.loc);
  487. });
  488. it('delegates to geometry when exists', function() {
  489. const m = mquery().where('loc').within({ type: 'Polygon', coordinates: [[10, 10], [11, 14], [10, 9]] });
  490. assert.deepEqual({ $geoWithin: { $geometry: { type: 'Polygon', coordinates: [[10, 10], [11, 14], [10, 9]] } } }, m._conditions.loc);
  491. });
  492. });
  493. describe('of length 2', function() {
  494. it('delegates to box()', function() {
  495. const m = mquery().where('loc').within([1, 2], [2, 5]);
  496. assert.deepEqual(m._conditions.loc, { $geoWithin: { $box: [[1, 2], [2, 5]] } });
  497. });
  498. });
  499. describe('of length > 2', function() {
  500. it('delegates to polygon()', function() {
  501. const m = mquery().where('loc').within([1, 2], [2, 5], [2, 4], [1, 3]);
  502. assert.deepEqual(m._conditions.loc, { $geoWithin: { $polygon: [[1, 2], [2, 5], [2, 4], [1, 3]] } });
  503. });
  504. });
  505. });
  506. });
  507. describe('geoWithin', function() {
  508. before(function() {
  509. mquery.use$geoWithin = false;
  510. });
  511. after(function() {
  512. mquery.use$geoWithin = true;
  513. });
  514. describe('when called with arguments', function() {
  515. describe('of length 1', function() {
  516. it('delegates to circle when center exists', function() {
  517. const m = mquery().where('loc').within({ center: [10, 10], radius: 3 });
  518. assert.deepEqual({ $within: { $center: [[10, 10], 3] } }, m._conditions.loc);
  519. });
  520. it('delegates to box when exists', function() {
  521. const m = mquery().where('loc').within({ box: [[10, 10], [11, 14]] });
  522. assert.deepEqual({ $within: { $box: [[10, 10], [11, 14]] } }, m._conditions.loc);
  523. });
  524. it('delegates to polygon when exists', function() {
  525. const m = mquery().where('loc').within({ polygon: [[10, 10], [11, 14], [10, 9]] });
  526. assert.deepEqual({ $within: { $polygon: [[10, 10], [11, 14], [10, 9]] } }, m._conditions.loc);
  527. });
  528. it('delegates to geometry when exists', function() {
  529. const m = mquery().where('loc').within({ type: 'Polygon', coordinates: [[10, 10], [11, 14], [10, 9]] });
  530. assert.deepEqual({ $within: { $geometry: { type: 'Polygon', coordinates: [[10, 10], [11, 14], [10, 9]] } } }, m._conditions.loc);
  531. });
  532. });
  533. describe('of length 2', function() {
  534. it('delegates to box()', function() {
  535. const m = mquery().where('loc').within([1, 2], [2, 5]);
  536. assert.deepEqual(m._conditions.loc, { $within: { $box: [[1, 2], [2, 5]] } });
  537. });
  538. });
  539. describe('of length > 2', function() {
  540. it('delegates to polygon()', function() {
  541. const m = mquery().where('loc').within([1, 2], [2, 5], [2, 4], [1, 3]);
  542. assert.deepEqual(m._conditions.loc, { $within: { $polygon: [[1, 2], [2, 5], [2, 4], [1, 3]] } });
  543. });
  544. });
  545. });
  546. });
  547. describe('box', function() {
  548. describe('with 1 argument', function() {
  549. it('throws', function() {
  550. assert.throws(function() {
  551. mquery().box('sometihng');
  552. }, /Invalid argument/);
  553. });
  554. });
  555. describe('with > 3 arguments', function() {
  556. it('throws', function() {
  557. assert.throws(function() {
  558. mquery().box(1, 2, 3, 4);
  559. }, /Invalid argument/);
  560. });
  561. });
  562. describe('with 2 arguments', function() {
  563. it('throws if not used after where()', function() {
  564. assert.throws(function() {
  565. mquery().box([], []);
  566. }, /must be used after where/);
  567. });
  568. it('works', function() {
  569. const m = mquery().where('loc').box([1, 2], [3, 4]);
  570. assert.deepEqual(m._conditions.loc, { $geoWithin: { $box: [[1, 2], [3, 4]] } });
  571. });
  572. });
  573. describe('with 3 arguments', function() {
  574. it('works', function() {
  575. const m = mquery().box('loc', [1, 2], [3, 4]);
  576. assert.deepEqual(m._conditions.loc, { $geoWithin: { $box: [[1, 2], [3, 4]] } });
  577. });
  578. });
  579. });
  580. describe('polygon', function() {
  581. describe('when first argument is not a string', function() {
  582. it('throws if not used after where()', function() {
  583. assert.throws(function() {
  584. mquery().polygon({});
  585. }, /must be used after where/);
  586. assert.doesNotThrow(function() {
  587. mquery().where('loc').polygon([1, 2], [2, 3], [3, 6]);
  588. });
  589. });
  590. it('assigns arguments to within polygon condition', function() {
  591. const m = mquery().where('loc').polygon([1, 2], [2, 3], [3, 6]);
  592. assert.deepEqual(m._conditions, { loc: { $geoWithin: { $polygon: [[1, 2], [2, 3], [3, 6]] } } });
  593. });
  594. });
  595. describe('when first arg is a string', function() {
  596. it('assigns remaining arguments to within polygon condition', function() {
  597. const m = mquery().polygon('loc', [1, 2], [2, 3], [3, 6]);
  598. assert.deepEqual(m._conditions, { loc: { $geoWithin: { $polygon: [[1, 2], [2, 3], [3, 6]] } } });
  599. });
  600. });
  601. });
  602. describe('circle', function() {
  603. describe('with one arg', function() {
  604. it('must follow where()', function() {
  605. assert.throws(function() {
  606. mquery().circle('x');
  607. }, /must be used after where/);
  608. assert.doesNotThrow(function() {
  609. mquery().where('loc').circle({ center: [0, 0], radius: 3 });
  610. });
  611. });
  612. it('works', function() {
  613. const m = mquery().where('loc').circle({ center: [0, 0], radius: 3 });
  614. assert.deepEqual(m._conditions, { loc: { $geoWithin: { $center: [[0, 0], 3] } } });
  615. });
  616. });
  617. describe('with 3 args', function() {
  618. it('throws', function() {
  619. assert.throws(function() {
  620. mquery().where('loc').circle(1, 2, 3);
  621. }, /Invalid argument/);
  622. });
  623. });
  624. describe('requires radius and center', function() {
  625. assert.throws(function() {
  626. mquery().circle('loc', { center: 1 });
  627. }, /center and radius are required/);
  628. assert.throws(function() {
  629. mquery().circle('loc', { radius: 1 });
  630. }, /center and radius are required/);
  631. assert.doesNotThrow(function() {
  632. mquery().circle('loc', { center: [1, 2], radius: 1 });
  633. });
  634. });
  635. });
  636. describe('geometry', function() {
  637. // within + intersects
  638. const point = { type: 'Point', coordinates: [[0, 0], [1, 1]] };
  639. it('must be called after within or intersects', function(done) {
  640. assert.throws(function() {
  641. mquery().where('a').geometry(point);
  642. }, /must come after/);
  643. assert.doesNotThrow(function() {
  644. mquery().where('a').within().geometry(point);
  645. });
  646. assert.doesNotThrow(function() {
  647. mquery().where('a').intersects().geometry(point);
  648. });
  649. done();
  650. });
  651. describe('when called with one argument', function() {
  652. describe('after within()', function() {
  653. it('and arg quacks like geoJSON', function(done) {
  654. const m = mquery().where('a').within().geometry(point);
  655. assert.deepEqual({ a: { $geoWithin: { $geometry: point } } }, m._conditions);
  656. done();
  657. });
  658. });
  659. describe('after intersects()', function() {
  660. it('and arg quacks like geoJSON', function(done) {
  661. const m = mquery().where('a').intersects().geometry(point);
  662. assert.deepEqual({ a: { $geoIntersects: { $geometry: point } } }, m._conditions);
  663. done();
  664. });
  665. });
  666. it('and arg does not quack like geoJSON', function(done) {
  667. assert.throws(function() {
  668. mquery().where('b').within().geometry({ type: 1, coordinates: 2 });
  669. }, /Invalid argument/);
  670. done();
  671. });
  672. });
  673. describe('when called with zero arguments', function() {
  674. it('throws', function(done) {
  675. assert.throws(function() {
  676. mquery().where('a').within().geometry();
  677. }, /Invalid argument/);
  678. done();
  679. });
  680. });
  681. describe('when called with more than one arguments', function() {
  682. it('throws', function(done) {
  683. assert.throws(function() {
  684. mquery().where('a').within().geometry({ type: 'a', coordinates: [] }, 2);
  685. }, /Invalid argument/);
  686. done();
  687. });
  688. });
  689. });
  690. describe('intersects', function() {
  691. it('must be used after where()', function(done) {
  692. const m = mquery();
  693. assert.throws(function() {
  694. m.intersects();
  695. }, /must be used after where/);
  696. done();
  697. });
  698. it('sets geo comparison to "$intersects"', function(done) {
  699. const n = mquery().where('a').intersects();
  700. assert.equal('$geoIntersects', n._geoComparison);
  701. done();
  702. });
  703. it('is chainable', function() {
  704. const m = mquery();
  705. assert.equal(m.where('a').intersects(), m);
  706. });
  707. it('calls geometry if argument quacks like geojson', function(done) {
  708. const m = mquery();
  709. const o = { type: 'LineString', coordinates: [[0, 1], [3, 40]] };
  710. let ran = false;
  711. m.geometry = function(arg) {
  712. ran = true;
  713. assert.deepEqual(o, arg);
  714. };
  715. m.where('a').intersects(o);
  716. assert.ok(ran);
  717. done();
  718. });
  719. it('throws if argument is not geometry-like', function(done) {
  720. const m = mquery().where('a');
  721. assert.throws(function() {
  722. m.intersects(null);
  723. }, /Invalid argument/);
  724. assert.throws(function() {
  725. m.intersects(undefined);
  726. }, /Invalid argument/);
  727. assert.throws(function() {
  728. m.intersects(false);
  729. }, /Invalid argument/);
  730. assert.throws(function() {
  731. m.intersects({});
  732. }, /Invalid argument/);
  733. assert.throws(function() {
  734. m.intersects([]);
  735. }, /Invalid argument/);
  736. assert.throws(function() {
  737. m.intersects(function() {});
  738. }, /Invalid argument/);
  739. assert.throws(function() {
  740. m.intersects(NaN);
  741. }, /Invalid argument/);
  742. done();
  743. });
  744. });
  745. describe('near', function() {
  746. // near nearSphere
  747. describe('with 0 args', function() {
  748. it('is compatible with geometry()', function(done) {
  749. const q = mquery().where('x').near().geometry({ type: 'Point', coordinates: [180, 11] });
  750. assert.deepEqual({ $near: { $geometry: { type: 'Point', coordinates: [180, 11] } } }, q._conditions.x);
  751. done();
  752. });
  753. });
  754. describe('with 1 arg', function() {
  755. it('throws if not used after where()', function() {
  756. assert.throws(function() {
  757. mquery().near(1);
  758. }, /must be used after where/);
  759. });
  760. it('does not throw if used after where()', function() {
  761. assert.doesNotThrow(function() {
  762. mquery().where('loc').near({ center: [1, 1] });
  763. });
  764. });
  765. });
  766. describe('with > 2 args', function() {
  767. it('throws', function() {
  768. assert.throws(function() {
  769. mquery().near(1, 2, 3);
  770. }, /Invalid argument/);
  771. });
  772. });
  773. it('creates $geometry args for GeoJSON', function() {
  774. const m = mquery().where('loc').near({ center: { type: 'Point', coordinates: [10, 10] } });
  775. assert.deepEqual({ $near: { $geometry: { type: 'Point', coordinates: [10, 10] } } }, m._conditions.loc);
  776. });
  777. it('expects `center`', function() {
  778. assert.throws(function() {
  779. mquery().near('loc', { maxDistance: 3 });
  780. }, /center is required/);
  781. assert.doesNotThrow(function() {
  782. mquery().near('loc', { center: [3, 4] });
  783. });
  784. });
  785. it('accepts spherical conditions', function() {
  786. const m = mquery().where('loc').near({ center: [1, 2], spherical: true });
  787. assert.deepEqual(m._conditions, { loc: { $nearSphere: [1, 2] } });
  788. });
  789. it('is non-spherical by default', function() {
  790. const m = mquery().where('loc').near({ center: [1, 2] });
  791. assert.deepEqual(m._conditions, { loc: { $near: [1, 2] } });
  792. });
  793. it('supports maxDistance', function() {
  794. const m = mquery().where('loc').near({ center: [1, 2], maxDistance: 4 });
  795. assert.deepEqual(m._conditions, { loc: { $near: [1, 2], $maxDistance: 4 } });
  796. });
  797. it('supports minDistance', function() {
  798. const m = mquery().where('loc').near({ center: [1, 2], minDistance: 4 });
  799. assert.deepEqual(m._conditions, { loc: { $near: [1, 2], $minDistance: 4 } });
  800. });
  801. it('is chainable', function() {
  802. const m = mquery().where('loc').near({ center: [1, 2], maxDistance: 4 }).find({ x: 1 });
  803. assert.deepEqual(m._conditions, { loc: { $near: [1, 2], $maxDistance: 4 }, x: 1 });
  804. });
  805. describe('supports passing GeoJSON, gh-13', function() {
  806. it('with center', function() {
  807. const m = mquery().where('loc').near({
  808. center: { type: 'Point', coordinates: [1, 1] },
  809. maxDistance: 2
  810. });
  811. const expect = {
  812. loc: {
  813. $near: {
  814. $geometry: {
  815. type: 'Point',
  816. coordinates: [1, 1]
  817. },
  818. $maxDistance: 2
  819. }
  820. }
  821. };
  822. assert.deepEqual(m._conditions, expect);
  823. });
  824. });
  825. });
  826. // fields
  827. describe('select', function() {
  828. describe('with 0 args', function() {
  829. it('is chainable', function() {
  830. const m = mquery();
  831. assert.equal(m, m.select());
  832. });
  833. });
  834. it('accepts an object', function() {
  835. const o = { x: 1, y: 1 };
  836. const m = mquery().select(o);
  837. assert.deepEqual(m._fields, o);
  838. });
  839. it('accepts a string', function() {
  840. const o = 'x -y';
  841. const m = mquery().select(o);
  842. assert.deepEqual(m._fields, { x: 1, y: 0 });
  843. });
  844. it('does accept an array', function() {
  845. const o = ['x', '-y'];
  846. const m = mquery().select(o);
  847. assert.deepEqual(m._fields, { x: 1, y: 0 });
  848. });
  849. it('merges previous arguments', function() {
  850. const o = { x: 1, y: 0, a: 1 };
  851. const m = mquery().select(o);
  852. m.select('z -u w').select({ x: 0 });
  853. assert.deepEqual(m._fields, {
  854. x: 0,
  855. y: 0,
  856. z: 1,
  857. u: 0,
  858. w: 1,
  859. a: 1
  860. });
  861. });
  862. it('rejects non-string, object, arrays', function() {
  863. assert.throws(function() {
  864. mquery().select(function() {});
  865. }, /Invalid select\(\) argument/);
  866. });
  867. it('accepts arguments objects', function() {
  868. const m = mquery();
  869. function t() {
  870. m.select(arguments);
  871. assert.deepEqual(m._fields, { x: 1, y: 0 });
  872. }
  873. t('x', '-y');
  874. });
  875. noDistinct('select');
  876. });
  877. describe('selected', function() {
  878. it('returns true when fields have been selected', function(done) {
  879. let m;
  880. m = mquery().select({ name: 1 });
  881. assert.ok(m.selected());
  882. m = mquery().select('name');
  883. assert.ok(m.selected());
  884. done();
  885. });
  886. it('returns false when no fields have been selected', function(done) {
  887. const m = mquery();
  888. assert.strictEqual(false, m.selected());
  889. done();
  890. });
  891. });
  892. describe('selectedInclusively', function() {
  893. describe('returns false', function() {
  894. it('when no fields have been selected', function(done) {
  895. assert.strictEqual(false, mquery().selectedInclusively());
  896. assert.equal(false, mquery().select({}).selectedInclusively());
  897. done();
  898. });
  899. it('when any fields have been excluded', function(done) {
  900. assert.strictEqual(false, mquery().select('-name').selectedInclusively());
  901. assert.strictEqual(false, mquery().select({ name: 0 }).selectedInclusively());
  902. assert.strictEqual(false, mquery().select('name bio -_id').selectedInclusively());
  903. assert.strictEqual(false, mquery().select({ name: 1, _id: 0 }).selectedInclusively());
  904. done();
  905. });
  906. it('when using $meta', function(done) {
  907. assert.strictEqual(false, mquery().select({ name: { $meta: 'textScore' } }).selectedInclusively());
  908. done();
  909. });
  910. });
  911. describe('returns true', function() {
  912. it('when fields have been included', function(done) {
  913. assert.equal(true, mquery().select('name').selectedInclusively());
  914. assert.equal(true, mquery().select({ name: 1 }).selectedInclusively());
  915. done();
  916. });
  917. });
  918. });
  919. describe('selectedExclusively', function() {
  920. describe('returns false', function() {
  921. it('when no fields have been selected', function(done) {
  922. assert.equal(false, mquery().selectedExclusively());
  923. assert.equal(false, mquery().select({}).selectedExclusively());
  924. done();
  925. });
  926. it('when fields have only been included', function(done) {
  927. assert.equal(false, mquery().select('name').selectedExclusively());
  928. assert.equal(false, mquery().select({ name: 1 }).selectedExclusively());
  929. done();
  930. });
  931. });
  932. describe('returns true', function() {
  933. it('when any field has been excluded', function(done) {
  934. assert.equal(true, mquery().select('-name').selectedExclusively());
  935. assert.equal(true, mquery().select({ name: 0 }).selectedExclusively());
  936. assert.equal(true, mquery().select('-_id').selectedExclusively());
  937. assert.strictEqual(true, mquery().select('name bio -_id').selectedExclusively());
  938. assert.strictEqual(true, mquery().select({ name: 1, _id: 0 }).selectedExclusively());
  939. done();
  940. });
  941. });
  942. });
  943. describe('slice', function() {
  944. describe('with 0 args', function() {
  945. it('is chainable', function() {
  946. const m = mquery();
  947. assert.equal(m, m.slice());
  948. });
  949. it('is a noop', function() {
  950. const m = mquery().slice();
  951. assert.deepEqual(m._fields, undefined);
  952. });
  953. });
  954. describe('with 1 arg', function() {
  955. it('throws if not called after where()', function() {
  956. assert.throws(function() {
  957. mquery().slice(1);
  958. }, /must be used after where/);
  959. assert.doesNotThrow(function() {
  960. mquery().where('a').slice(1);
  961. });
  962. });
  963. it('that is a number', function() {
  964. const query = mquery();
  965. query.where('collection').slice(5);
  966. assert.deepEqual(query._fields, { collection: { $slice: 5 } });
  967. });
  968. it('that is an array', function() {
  969. const query = mquery();
  970. query.where('collection').slice([5, 10]);
  971. assert.deepEqual(query._fields, { collection: { $slice: [5, 10] } });
  972. });
  973. it('that is an object', function() {
  974. const query = mquery();
  975. query.slice({ collection: [5, 10] });
  976. assert.deepEqual(query._fields, { collection: { $slice: [5, 10] } });
  977. });
  978. });
  979. describe('with 2 args', function() {
  980. describe('and first is a number', function() {
  981. it('throws if not called after where', function() {
  982. assert.throws(function() {
  983. mquery().slice(2, 3);
  984. }, /must be used after where/);
  985. });
  986. it('does not throw if used after where', function() {
  987. const query = mquery();
  988. query.where('collection').slice(2, 3);
  989. assert.deepEqual(query._fields, { collection: { $slice: [2, 3] } });
  990. });
  991. });
  992. it('and first is not a number', function() {
  993. const query = mquery().slice('collection', [-5, 2]);
  994. assert.deepEqual(query._fields, { collection: { $slice: [-5, 2] } });
  995. });
  996. });
  997. describe('with 3 args', function() {
  998. it('works', function() {
  999. const query = mquery();
  1000. query.slice('collection', 14, 10);
  1001. assert.deepEqual(query._fields, { collection: { $slice: [14, 10] } });
  1002. });
  1003. });
  1004. noDistinct('slice');
  1005. no('count', 'slice');
  1006. });
  1007. // options
  1008. describe('sort', function() {
  1009. describe('with 0 args', function() {
  1010. it('chains', function() {
  1011. const m = mquery();
  1012. assert.equal(m, m.sort());
  1013. });
  1014. it('has no affect', function() {
  1015. const m = mquery();
  1016. assert.equal(m.options.sort, undefined);
  1017. });
  1018. });
  1019. it('works', function() {
  1020. let query = mquery();
  1021. query.sort('a -c b');
  1022. assert.deepEqual(query.options.sort, { a: 1, b: 1, c: -1 });
  1023. query = mquery();
  1024. query.sort({ a: 1, c: -1, b: 'asc', e: 'descending', f: 'ascending' });
  1025. assert.deepEqual(query.options.sort, { a: 1, c: -1, b: 1, e: -1, f: 1 });
  1026. query = mquery();
  1027. query.sort([['a', -1], ['c', 1], ['b', 'desc'], ['e', 'ascending'], ['f', 'descending']]);
  1028. assert.deepEqual(query.options.sort, [['a', -1], ['c', 1], ['b', -1], ['e', 1], ['f', -1]]);
  1029. query = mquery();
  1030. let e = undefined;
  1031. try {
  1032. query.sort([['a', 1], { b: 5 }]);
  1033. } catch (err) {
  1034. e = err;
  1035. }
  1036. assert.ok(e, 'uh oh. no error was thrown');
  1037. assert.equal(e.message, 'Invalid sort() argument, must be array of arrays');
  1038. query = mquery();
  1039. e = undefined;
  1040. try {
  1041. query.sort('a', 1, 'c', -1, 'b', 1);
  1042. } catch (err) {
  1043. e = err;
  1044. }
  1045. assert.ok(e, 'uh oh. no error was thrown');
  1046. assert.equal(e.message, 'Invalid sort() argument. Must be a string, object, or array.');
  1047. });
  1048. it('handles $meta sort options', function() {
  1049. const query = mquery();
  1050. query.sort({ score: { $meta: 'textScore' } });
  1051. assert.deepEqual(query.options.sort, { score: { $meta: 'textScore' } });
  1052. });
  1053. it('array syntax', function() {
  1054. const query = mquery();
  1055. query.sort([['field', 1], ['test', -1]]);
  1056. assert.deepEqual(query.options.sort, [['field', 1], ['test', -1]]);
  1057. });
  1058. it('throws with mixed array/object syntax', function() {
  1059. const query = mquery();
  1060. assert.throws(function() {
  1061. query.sort({ field: 1 }).sort([['test', -1]]);
  1062. }, /Can't mix sort syntaxes/);
  1063. assert.throws(function() {
  1064. query.sort([['field', 1]]).sort({ test: 1 });
  1065. }, /Can't mix sort syntaxes/);
  1066. });
  1067. it('works with maps', function() {
  1068. if (typeof Map === 'undefined') {
  1069. return this.skip();
  1070. }
  1071. const query = mquery();
  1072. query.sort(new Map().set('field', 1).set('test', -1));
  1073. assert.deepEqual(query.options.sort, new Map().set('field', 1).set('test', -1));
  1074. });
  1075. });
  1076. function simpleOption(type, options) {
  1077. describe(type, function() {
  1078. it('sets the ' + type + ' option', function() {
  1079. const m = mquery()[type](2);
  1080. const optionName = options.name || type;
  1081. assert.equal(2, m.options[optionName]);
  1082. });
  1083. it('is chainable', function() {
  1084. const m = mquery();
  1085. assert.equal(m[type](3), m);
  1086. });
  1087. if (!options.distinct) noDistinct(type);
  1088. if (!options.count) no('count', type);
  1089. });
  1090. }
  1091. const negated = {
  1092. limit: { distinct: false, count: true },
  1093. skip: { distinct: false, count: true },
  1094. maxScan: { distinct: false, count: false },
  1095. batchSize: { distinct: false, count: false },
  1096. maxTime: { distinct: true, count: true, name: 'maxTimeMS' }
  1097. };
  1098. Object.keys(negated).forEach(function(key) {
  1099. simpleOption(key, negated[key]);
  1100. });
  1101. describe('snapshot', function() {
  1102. it('works', function() {
  1103. let query;
  1104. query = mquery();
  1105. query.snapshot();
  1106. assert.equal(true, query.options.snapshot);
  1107. query = mquery();
  1108. query.snapshot(true);
  1109. assert.equal(true, query.options.snapshot);
  1110. query = mquery();
  1111. query.snapshot(false);
  1112. assert.equal(false, query.options.snapshot);
  1113. });
  1114. noDistinct('snapshot');
  1115. no('count', 'snapshot');
  1116. });
  1117. describe('hint', function() {
  1118. it('accepts an object', function() {
  1119. const query2 = mquery();
  1120. query2.hint({ a: 1, b: -1 });
  1121. assert.deepEqual(query2.options.hint, { a: 1, b: -1 });
  1122. });
  1123. it('accepts a string', function() {
  1124. const query2 = mquery();
  1125. query2.hint('a');
  1126. assert.deepEqual(query2.options.hint, 'a');
  1127. });
  1128. it('rejects everything else', function() {
  1129. assert.throws(function() {
  1130. mquery().hint(['c']);
  1131. }, /Invalid hint./);
  1132. assert.throws(function() {
  1133. mquery().hint(1);
  1134. }, /Invalid hint./);
  1135. });
  1136. describe('does not have side affects', function() {
  1137. it('on invalid arg', function() {
  1138. const m = mquery();
  1139. try {
  1140. m.hint(1);
  1141. } catch (err) {
  1142. // ignore
  1143. }
  1144. assert.equal(undefined, m.options.hint);
  1145. });
  1146. it('on missing arg', function() {
  1147. const m = mquery().hint();
  1148. assert.equal(undefined, m.options.hint);
  1149. });
  1150. });
  1151. noDistinct('hint');
  1152. });
  1153. describe('j', function() {
  1154. it('works', function() {
  1155. const m = mquery().j(true);
  1156. assert.equal(true, m.options.j);
  1157. });
  1158. });
  1159. describe('slaveOk', function() {
  1160. it('works', function() {
  1161. let query;
  1162. query = mquery();
  1163. query.slaveOk();
  1164. assert.equal(true, query.options.slaveOk);
  1165. query = mquery();
  1166. query.slaveOk(true);
  1167. assert.equal(true, query.options.slaveOk);
  1168. query = mquery();
  1169. query.slaveOk(false);
  1170. assert.equal(false, query.options.slaveOk);
  1171. });
  1172. });
  1173. describe('read', function() {
  1174. it('sets associated readPreference option', function() {
  1175. const m = mquery();
  1176. m.read('p');
  1177. assert.equal('primary', m.options.readPreference);
  1178. });
  1179. it('is chainable', function() {
  1180. const m = mquery();
  1181. assert.equal(m, m.read('sp'));
  1182. });
  1183. });
  1184. describe('readConcern', function() {
  1185. it('sets associated readConcern option', function() {
  1186. let m;
  1187. m = mquery();
  1188. m.readConcern('s');
  1189. assert.deepEqual({ level: 'snapshot' }, m.options.readConcern);
  1190. m = mquery();
  1191. m.r('local');
  1192. assert.deepEqual({ level: 'local' }, m.options.readConcern);
  1193. });
  1194. it('is chainable', function() {
  1195. const m = mquery();
  1196. assert.equal(m, m.readConcern('lz'));
  1197. });
  1198. });
  1199. describe('tailable', function() {
  1200. it('works', function() {
  1201. let query;
  1202. query = mquery();
  1203. query.tailable();
  1204. assert.equal(true, query.options.tailable);
  1205. query = mquery();
  1206. query.tailable(true);
  1207. assert.equal(true, query.options.tailable);
  1208. query = mquery();
  1209. query.tailable(false);
  1210. assert.equal(false, query.options.tailable);
  1211. });
  1212. it('is chainable', function() {
  1213. const m = mquery();
  1214. assert.equal(m, m.tailable());
  1215. });
  1216. noDistinct('tailable');
  1217. no('count', 'tailable');
  1218. });
  1219. describe('writeConcern', function() {
  1220. it('sets associated writeConcern option', function() {
  1221. let m;
  1222. m = mquery();
  1223. m.writeConcern('majority');
  1224. assert.equal('majority', m.options.w);
  1225. m = mquery();
  1226. m.writeConcern('m'); // m is alias of majority
  1227. assert.equal('majority', m.options.w);
  1228. m = mquery();
  1229. m.writeConcern(1);
  1230. assert.equal(1, m.options.w);
  1231. });
  1232. it('accepts object', function() {
  1233. let m;
  1234. m = mquery().writeConcern({ w: 'm', j: true, wtimeout: 1000 });
  1235. assert.equal('m', m.options.w); // check it does not convert m to majority
  1236. assert.equal(true, m.options.j);
  1237. assert.equal(1000, m.options.wtimeout);
  1238. m = mquery().w('m').w({ j: false, wtimeout: 0 });
  1239. assert.equal('majority', m.options.w);
  1240. assert.strictEqual(false, m.options.j);
  1241. assert.strictEqual(0, m.options.wtimeout);
  1242. });
  1243. it('is chainable', function() {
  1244. const m = mquery();
  1245. assert.equal(m, m.writeConcern('majority'));
  1246. });
  1247. });
  1248. // query utilities
  1249. describe('merge', function() {
  1250. describe('with falsy arg', function() {
  1251. it('returns itself', function() {
  1252. const m = mquery();
  1253. assert.equal(m, m.merge());
  1254. assert.equal(m, m.merge(null));
  1255. assert.equal(m, m.merge(0));
  1256. });
  1257. });
  1258. describe('with an argument', function() {
  1259. describe('that is not a query or plain object', function() {
  1260. it('throws', function() {
  1261. assert.throws(function() {
  1262. mquery().merge([]);
  1263. }, /Invalid argument/);
  1264. assert.throws(function() {
  1265. mquery().merge('merge');
  1266. }, /Invalid argument/);
  1267. assert.doesNotThrow(function() {
  1268. mquery().merge({});
  1269. }, /Invalid argument/);
  1270. });
  1271. });
  1272. describe('that is a query', function() {
  1273. it('merges conditions, field selection, and options', function() {
  1274. const m = mquery({ x: 'hi' }, { select: 'x y', another: true });
  1275. const n = mquery().merge(m);
  1276. assert.deepEqual(n._conditions, m._conditions);
  1277. assert.deepEqual(n._fields, m._fields);
  1278. assert.deepEqual(n.options, m.options);
  1279. });
  1280. it('clones update arguments', function(done) {
  1281. const original = { $set: { iTerm: true } };
  1282. const m = mquery().updateOne(original);
  1283. const n = mquery().merge(m);
  1284. m.updateOne({ $set: { x: 2 } });
  1285. assert.notDeepEqual(m._update, n._update);
  1286. done();
  1287. });
  1288. it('is chainable', function() {
  1289. const m = mquery({ x: 'hi' });
  1290. const n = mquery();
  1291. assert.equal(n, n.merge(m));
  1292. });
  1293. });
  1294. describe('that is an object', function() {
  1295. it('merges', function() {
  1296. const m = { x: 'hi' };
  1297. const n = mquery().merge(m);
  1298. assert.deepEqual(n._conditions, { x: 'hi' });
  1299. });
  1300. it('clones update arguments', function(done) {
  1301. const original = { $set: { iTerm: true } };
  1302. const m = mquery().updateOne(original);
  1303. const n = mquery().merge(original);
  1304. m.updateOne({ $set: { x: 2 } });
  1305. assert.notDeepEqual(m._update, n._update);
  1306. done();
  1307. });
  1308. it('is chainable', function() {
  1309. const m = { x: 'hi' };
  1310. const n = mquery();
  1311. assert.equal(n, n.merge(m));
  1312. });
  1313. });
  1314. });
  1315. });
  1316. // queries
  1317. describe('find', function() {
  1318. describe('with no callback', function() {
  1319. it('does not execute', function() {
  1320. const m = mquery();
  1321. assert.doesNotThrow(function() {
  1322. m.find();
  1323. });
  1324. assert.doesNotThrow(function() {
  1325. m.find({ x: 1 });
  1326. });
  1327. });
  1328. });
  1329. it('is chainable', function() {
  1330. const m = mquery().find({ x: 1 }).find().find({ y: 2 });
  1331. assert.deepEqual(m._conditions, { x: 1, y: 2 });
  1332. });
  1333. it('merges other queries', function() {
  1334. const m = mquery({ name: 'mquery' });
  1335. m.tailable();
  1336. m.select('_id');
  1337. const a = mquery().find(m);
  1338. assert.deepEqual(a._conditions, m._conditions);
  1339. assert.deepEqual(a.options, m.options);
  1340. assert.deepEqual(a._fields, m._fields);
  1341. });
  1342. describe('executes', function() {
  1343. before(function(done) {
  1344. col.insertOne({ name: 'mquery' }, done);
  1345. });
  1346. after(function(done) {
  1347. col.remove({ name: 'mquery' }, done);
  1348. });
  1349. it('when criteria is passed with a callback', function(done) {
  1350. mquery(col).find({ name: 'mquery' }, function(err, docs) {
  1351. assert.ifError(err);
  1352. assert.equal(1, docs.length);
  1353. done();
  1354. });
  1355. });
  1356. it('when Query is passed with a callback', function(done) {
  1357. const m = mquery({ name: 'mquery' });
  1358. mquery(col).find(m, function(err, docs) {
  1359. assert.ifError(err);
  1360. assert.equal(1, docs.length);
  1361. done();
  1362. });
  1363. });
  1364. it('when just a callback is passed', function(done) {
  1365. mquery({ name: 'mquery' }).collection(col).find(function(err, docs) {
  1366. assert.ifError(err);
  1367. assert.equal(1, docs.length);
  1368. done();
  1369. });
  1370. });
  1371. });
  1372. });
  1373. describe('findOne', function() {
  1374. describe('with no callback', function() {
  1375. it('does not execute', function() {
  1376. const m = mquery();
  1377. assert.doesNotThrow(function() {
  1378. m.findOne();
  1379. });
  1380. assert.doesNotThrow(function() {
  1381. m.findOne({ x: 1 });
  1382. });
  1383. });
  1384. });
  1385. it('is chainable', function() {
  1386. const m = mquery();
  1387. const n = m.findOne({ x: 1 }).findOne().findOne({ y: 2 });
  1388. assert.equal(m, n);
  1389. assert.deepEqual(m._conditions, { x: 1, y: 2 });
  1390. assert.equal('findOne', m.op);
  1391. });
  1392. it('merges other queries', function() {
  1393. const m = mquery({ name: 'mquery' });
  1394. m.read('nearest');
  1395. m.select('_id');
  1396. const a = mquery().findOne(m);
  1397. assert.deepEqual(a._conditions, m._conditions);
  1398. assert.deepEqual(a.options, m.options);
  1399. assert.deepEqual(a._fields, m._fields);
  1400. });
  1401. describe('executes', function() {
  1402. before(function(done) {
  1403. col.insertOne({ name: 'mquery findone' }, done);
  1404. });
  1405. after(function(done) {
  1406. col.remove({ name: 'mquery findone' }, done);
  1407. });
  1408. it('when criteria is passed with a callback', function(done) {
  1409. mquery(col).findOne({ name: 'mquery findone' }, function(err, doc) {
  1410. assert.ifError(err);
  1411. assert.ok(doc);
  1412. assert.equal('mquery findone', doc.name);
  1413. done();
  1414. });
  1415. });
  1416. it('when Query is passed with a callback', function(done) {
  1417. const m = mquery(col).where({ name: 'mquery findone' });
  1418. mquery(col).findOne(m, function(err, doc) {
  1419. assert.ifError(err);
  1420. assert.ok(doc);
  1421. assert.equal('mquery findone', doc.name);
  1422. done();
  1423. });
  1424. });
  1425. it('when just a callback is passed', function(done) {
  1426. mquery({ name: 'mquery findone' }).collection(col).findOne(function(err, doc) {
  1427. assert.ifError(err);
  1428. assert.ok(doc);
  1429. assert.equal('mquery findone', doc.name);
  1430. done();
  1431. });
  1432. });
  1433. });
  1434. });
  1435. describe('count', function() {
  1436. describe('with no callback', function() {
  1437. it('does not execute', function() {
  1438. const m = mquery();
  1439. assert.doesNotThrow(function() {
  1440. m.count();
  1441. });
  1442. assert.doesNotThrow(function() {
  1443. m.count({ x: 1 });
  1444. });
  1445. });
  1446. });
  1447. it('is chainable', function() {
  1448. const m = mquery();
  1449. const n = m.count({ x: 1 }).count().count({ y: 2 });
  1450. assert.equal(m, n);
  1451. assert.deepEqual(m._conditions, { x: 1, y: 2 });
  1452. assert.equal('count', m.op);
  1453. });
  1454. it('merges other queries', function() {
  1455. const m = mquery({ name: 'mquery' });
  1456. m.read('nearest');
  1457. m.select('_id');
  1458. const a = mquery().count(m);
  1459. assert.deepEqual(a._conditions, m._conditions);
  1460. assert.deepEqual(a.options, m.options);
  1461. assert.deepEqual(a._fields, m._fields);
  1462. });
  1463. describe('executes', function() {
  1464. before(function(done) {
  1465. col.insertOne({ name: 'mquery count' }, done);
  1466. });
  1467. after(function(done) {
  1468. col.remove({ name: 'mquery count' }, done);
  1469. });
  1470. it('when criteria is passed with a callback', function(done) {
  1471. mquery(col).count({ name: 'mquery count' }, function(err, count) {
  1472. assert.ifError(err);
  1473. assert.ok(count);
  1474. assert.ok(1 === count);
  1475. done();
  1476. });
  1477. });
  1478. it('when Query is passed with a callback', function(done) {
  1479. const m = mquery({ name: 'mquery count' });
  1480. mquery(col).count(m, function(err, count) {
  1481. assert.ifError(err);
  1482. assert.ok(count);
  1483. assert.ok(1 === count);
  1484. done();
  1485. });
  1486. });
  1487. it('when just a callback is passed', function(done) {
  1488. mquery({ name: 'mquery count' }).collection(col).count(function(err, count) {
  1489. assert.ifError(err);
  1490. assert.ok(1 === count);
  1491. done();
  1492. });
  1493. });
  1494. });
  1495. describe('validates its option', function() {
  1496. it('sort', function(done) {
  1497. assert.doesNotThrow(function() {
  1498. mquery().sort('x').count();
  1499. });
  1500. done();
  1501. });
  1502. it('select', function(done) {
  1503. assert.throws(function() {
  1504. mquery().select('x').count();
  1505. }, /field selection and slice cannot be used with count/);
  1506. done();
  1507. });
  1508. it('slice', function(done) {
  1509. assert.throws(function() {
  1510. mquery().where('x').slice(-3).count();
  1511. }, /field selection and slice cannot be used with count/);
  1512. done();
  1513. });
  1514. it('limit', function(done) {
  1515. assert.doesNotThrow(function() {
  1516. mquery().limit(3).count();
  1517. });
  1518. done();
  1519. });
  1520. it('skip', function(done) {
  1521. assert.doesNotThrow(function() {
  1522. mquery().skip(3).count();
  1523. });
  1524. done();
  1525. });
  1526. it('batchSize', function(done) {
  1527. assert.throws(function() {
  1528. mquery({}, { batchSize: 3 }).count();
  1529. }, /batchSize cannot be used with count/);
  1530. done();
  1531. });
  1532. it('maxScan', function(done) {
  1533. assert.throws(function() {
  1534. mquery().maxScan(300).count();
  1535. }, /maxScan cannot be used with count/);
  1536. done();
  1537. });
  1538. it('snapshot', function(done) {
  1539. assert.throws(function() {
  1540. mquery().snapshot().count();
  1541. }, /snapshot cannot be used with count/);
  1542. done();
  1543. });
  1544. it('tailable', function(done) {
  1545. assert.throws(function() {
  1546. mquery().tailable().count();
  1547. }, /tailable cannot be used with count/);
  1548. done();
  1549. });
  1550. });
  1551. });
  1552. describe('distinct', function() {
  1553. describe('with no callback', function() {
  1554. it('does not execute', function() {
  1555. const m = mquery();
  1556. assert.doesNotThrow(function() {
  1557. m.distinct();
  1558. });
  1559. assert.doesNotThrow(function() {
  1560. m.distinct('name');
  1561. });
  1562. assert.doesNotThrow(function() {
  1563. m.distinct({ name: 'mquery distinct' });
  1564. });
  1565. assert.doesNotThrow(function() {
  1566. m.distinct({ name: 'mquery distinct' }, 'name');
  1567. });
  1568. });
  1569. });
  1570. it('is chainable', function() {
  1571. const m = mquery({ x: 1 }).distinct('name');
  1572. const n = m.distinct({ y: 2 });
  1573. assert.equal(m, n);
  1574. assert.deepEqual(n._conditions, { x: 1, y: 2 });
  1575. assert.equal('name', n._distinct);
  1576. assert.equal('distinct', n.op);
  1577. });
  1578. it('overwrites field', function() {
  1579. const m = mquery({ name: 'mquery' }).distinct('name');
  1580. m.distinct('rename');
  1581. assert.equal(m._distinct, 'rename');
  1582. m.distinct({ x: 1 }, 'renamed');
  1583. assert.equal(m._distinct, 'renamed');
  1584. });
  1585. it('merges other queries', function() {
  1586. const m = mquery().distinct({ name: 'mquery' }, 'age');
  1587. m.read('nearest');
  1588. const a = mquery().distinct(m);
  1589. assert.deepEqual(a._conditions, m._conditions);
  1590. assert.deepEqual(a.options, m.options);
  1591. assert.deepEqual(a._fields, m._fields);
  1592. assert.deepEqual(a._distinct, m._distinct);
  1593. });
  1594. describe('executes', function() {
  1595. before(function(done) {
  1596. col.insertOne({ name: 'mquery distinct', age: 1 }, done);
  1597. });
  1598. after(function(done) {
  1599. col.remove({ name: 'mquery distinct' }, done);
  1600. });
  1601. it('when distinct arg is passed with a callback', function(done) {
  1602. mquery(col).distinct('distinct', function(err, doc) {
  1603. assert.ifError(err);
  1604. assert.ok(doc);
  1605. done();
  1606. });
  1607. });
  1608. describe('when criteria is passed with a callback', function() {
  1609. it('if distinct arg was declared', function(done) {
  1610. mquery(col).distinct('age').distinct({ name: 'mquery distinct' }, function(err, doc) {
  1611. assert.ifError(err);
  1612. assert.ok(doc);
  1613. done();
  1614. });
  1615. });
  1616. it('but not if distinct arg was not declared', function() {
  1617. assert.throws(function() {
  1618. mquery(col).distinct({ name: 'mquery distinct' }, function() {});
  1619. }, /No value for `distinct`/);
  1620. });
  1621. });
  1622. describe('when Query is passed with a callback', function() {
  1623. const m = mquery({ name: 'mquery distinct' });
  1624. it('if distinct arg was declared', function(done) {
  1625. mquery(col).distinct('age').distinct(m, function(err, doc) {
  1626. assert.ifError(err);
  1627. assert.ok(doc);
  1628. done();
  1629. });
  1630. });
  1631. it('but not if distinct arg was not declared', function() {
  1632. assert.throws(function() {
  1633. mquery(col).distinct(m, function() {});
  1634. }, /No value for `distinct`/);
  1635. });
  1636. });
  1637. describe('when just a callback is passed', function() {
  1638. it('if distinct arg was declared', function(done) {
  1639. const m = mquery({ name: 'mquery distinct' });
  1640. m.collection(col);
  1641. m.distinct('age');
  1642. m.distinct(function(err, doc) {
  1643. assert.ifError(err);
  1644. assert.ok(doc);
  1645. done();
  1646. });
  1647. });
  1648. it('but not if no distinct arg was declared', function() {
  1649. const m = mquery();
  1650. m.collection(col);
  1651. assert.throws(function() {
  1652. m.distinct(function() {});
  1653. }, /No value for `distinct`/);
  1654. });
  1655. });
  1656. });
  1657. describe('validates its option', function() {
  1658. it('sort', function(done) {
  1659. assert.throws(function() {
  1660. mquery().sort('x').distinct();
  1661. }, /sort cannot be used with distinct/);
  1662. done();
  1663. });
  1664. it('select', function(done) {
  1665. assert.throws(function() {
  1666. mquery().select('x').distinct();
  1667. }, /field selection and slice cannot be used with distinct/);
  1668. done();
  1669. });
  1670. it('slice', function(done) {
  1671. assert.throws(function() {
  1672. mquery().where('x').slice(-3).distinct();
  1673. }, /field selection and slice cannot be used with distinct/);
  1674. done();
  1675. });
  1676. it('limit', function(done) {
  1677. assert.throws(function() {
  1678. mquery().limit(3).distinct();
  1679. }, /limit cannot be used with distinct/);
  1680. done();
  1681. });
  1682. it('skip', function(done) {
  1683. assert.throws(function() {
  1684. mquery().skip(3).distinct();
  1685. }, /skip cannot be used with distinct/);
  1686. done();
  1687. });
  1688. it('batchSize', function(done) {
  1689. assert.throws(function() {
  1690. mquery({}, { batchSize: 3 }).distinct();
  1691. }, /batchSize cannot be used with distinct/);
  1692. done();
  1693. });
  1694. it('maxScan', function(done) {
  1695. assert.throws(function() {
  1696. mquery().maxScan(300).distinct();
  1697. }, /maxScan cannot be used with distinct/);
  1698. done();
  1699. });
  1700. it('snapshot', function(done) {
  1701. assert.throws(function() {
  1702. mquery().snapshot().distinct();
  1703. }, /snapshot cannot be used with distinct/);
  1704. done();
  1705. });
  1706. it('hint', function(done) {
  1707. assert.throws(function() {
  1708. mquery().hint({ x: 1 }).distinct();
  1709. }, /hint cannot be used with distinct/);
  1710. done();
  1711. });
  1712. it('tailable', function(done) {
  1713. assert.throws(function() {
  1714. mquery().tailable().distinct();
  1715. }, /tailable cannot be used with distinct/);
  1716. done();
  1717. });
  1718. });
  1719. });
  1720. describe('update', function() {
  1721. describe('with no callback', function() {
  1722. it('does not execute', function() {
  1723. const m = mquery();
  1724. assert.doesNotThrow(function() {
  1725. m.updateOne({ name: 'old' }, { name: 'updated' }, { multi: true });
  1726. });
  1727. assert.doesNotThrow(function() {
  1728. m.updateOne({ name: 'old' }, { name: 'updated' });
  1729. });
  1730. assert.doesNotThrow(function() {
  1731. m.updateOne({ name: 'updated' });
  1732. });
  1733. assert.doesNotThrow(function() {
  1734. m.updateOne();
  1735. });
  1736. });
  1737. });
  1738. it('is chainable', function() {
  1739. const m = mquery({ x: 1 }).updateOne({ y: 2 });
  1740. const n = m.where({ y: 2 });
  1741. assert.equal(m, n);
  1742. assert.deepEqual(n._conditions, { x: 1, y: 2 });
  1743. assert.deepEqual({ y: 2 }, n._update);
  1744. assert.equal('updateOne', n.op);
  1745. });
  1746. it('merges update doc arg', function() {
  1747. const a = [1, 2];
  1748. const m = mquery().where({ name: 'mquery' }).updateOne({ x: 'stuff', a: a });
  1749. m.updateOne({ z: 'stuff' });
  1750. assert.deepEqual(m._update, { z: 'stuff', x: 'stuff', a: a });
  1751. assert.deepEqual(m._conditions, { name: 'mquery' });
  1752. assert.ok(!m.options.overwrite);
  1753. m.updateOne({}, { z: 'renamed' }, { overwrite: true });
  1754. assert.ok(m.options.overwrite === true);
  1755. assert.deepEqual(m._conditions, { name: 'mquery' });
  1756. assert.deepEqual(m._update, { z: 'renamed', x: 'stuff', a: a });
  1757. a.push(3);
  1758. assert.notDeepEqual(m._update, { z: 'renamed', x: 'stuff', a: a });
  1759. });
  1760. describe('executes', function() {
  1761. let id;
  1762. before(function(done) {
  1763. col.insertOne({ name: 'mquery update', age: 1 }, function(err, res) {
  1764. id = res.insertedId;
  1765. done();
  1766. });
  1767. });
  1768. after(function(done) {
  1769. col.remove({ _id: id }, done);
  1770. });
  1771. describe('when conds + doc + opts + callback passed', function() {
  1772. it('works', function(done) {
  1773. const m = mquery(col).where({ _id: id });
  1774. m.updateOne({}, { name: 'Sparky' }, {}, function(err, res) {
  1775. assert.ifError(err);
  1776. assert.equal(res.modifiedCount, 1);
  1777. m.findOne(function(err, doc) {
  1778. assert.ifError(err);
  1779. assert.equal(doc.name, 'Sparky');
  1780. done();
  1781. });
  1782. });
  1783. });
  1784. });
  1785. describe('when conds + doc + callback passed', function() {
  1786. it('works', function(done) {
  1787. const m = mquery(col).updateOne({ _id: id }, { name: 'fairgrounds' }, function(err, num) {
  1788. assert.ifError(err);
  1789. assert.ok(1, num);
  1790. m.findOne(function(err, doc) {
  1791. assert.ifError(err);
  1792. assert.equal(doc.name, 'fairgrounds');
  1793. done();
  1794. });
  1795. });
  1796. });
  1797. });
  1798. describe('when doc + callback passed', function() {
  1799. it('works', function(done) {
  1800. const m = mquery(col).where({ _id: id }).updateOne({ name: 'changed' }, function(err, num) {
  1801. assert.ifError(err);
  1802. assert.ok(1, num);
  1803. m.findOne(function(err, doc) {
  1804. assert.ifError(err);
  1805. assert.equal(doc.name, 'changed');
  1806. done();
  1807. });
  1808. });
  1809. });
  1810. });
  1811. describe('when just callback passed', function() {
  1812. it('works', function(done) {
  1813. const m = mquery(col).where({ _id: id });
  1814. m.updateOne({ name: 'Frankenweenie' });
  1815. m.updateOne(function(err, res) {
  1816. assert.ifError(err);
  1817. assert.equal(res.modifiedCount, 1);
  1818. m.findOne(function(err, doc) {
  1819. assert.ifError(err);
  1820. assert.equal(doc.name, 'Frankenweenie');
  1821. done();
  1822. });
  1823. });
  1824. });
  1825. });
  1826. describe('without a callback', function() {
  1827. it('when forced by exec()', function(done) {
  1828. const m = mquery(col).where({ _id: id });
  1829. m.setOptions({ w: 'majority' });
  1830. m.updateOne({ name: 'forced' });
  1831. const update = m._collection.update;
  1832. m._collection.updateOne = function(conds, doc, opts) {
  1833. m._collection.update = update;
  1834. assert.equal(opts.w, 'majority');
  1835. assert.equal('forced', doc.$set.name);
  1836. done();
  1837. };
  1838. m.exec();
  1839. });
  1840. });
  1841. describe('except when update doc is empty and missing overwrite flag', function() {
  1842. it('works', function(done) {
  1843. const m = mquery(col).where({ _id: id });
  1844. m.updateOne({}, function(err, num) {
  1845. assert.ifError(err);
  1846. assert.ok(0 === num);
  1847. setTimeout(function() {
  1848. m.findOne(function(err, doc) {
  1849. assert.ifError(err);
  1850. assert.equal(3, mquery.utils.keys(doc).length);
  1851. assert.equal(id, doc._id.toString());
  1852. assert.equal('Frankenweenie', doc.name);
  1853. done();
  1854. });
  1855. }, 300);
  1856. });
  1857. });
  1858. });
  1859. describe('when boolean (true) - exec()', function() {
  1860. it('works', function(done) {
  1861. const m = mquery(col).where({ _id: id });
  1862. m.updateOne({ name: 'bool' }).updateOne(true);
  1863. setTimeout(function() {
  1864. m.findOne(function(err, doc) {
  1865. assert.ifError(err);
  1866. assert.ok(doc);
  1867. assert.equal('bool', doc.name);
  1868. done();
  1869. });
  1870. }, 300);
  1871. });
  1872. });
  1873. });
  1874. });
  1875. describe('remove', function() {
  1876. describe('with 0 args', function() {
  1877. const name = 'remove: no args test';
  1878. before(function(done) {
  1879. col.insertOne({ name: name }, done);
  1880. });
  1881. after(function(done) {
  1882. col.remove({ name: name }, done);
  1883. });
  1884. it('does not execute', function(done) {
  1885. const remove = col.remove;
  1886. col.remove = function() {
  1887. col.remove = remove;
  1888. done(new Error('remove executed!'));
  1889. };
  1890. mquery(col).where({ name: name }).remove();
  1891. setTimeout(function() {
  1892. col.remove = remove;
  1893. done();
  1894. }, 10);
  1895. });
  1896. it('chains', function() {
  1897. const m = mquery();
  1898. assert.equal(m, m.remove());
  1899. });
  1900. });
  1901. describe('with 1 argument', function() {
  1902. const name = 'remove: 1 arg test';
  1903. before(function(done) {
  1904. col.insertOne({ name: name }, done);
  1905. });
  1906. after(function(done) {
  1907. col.remove({ name: name }, done);
  1908. });
  1909. describe('that is a', function() {
  1910. it('plain object', function() {
  1911. const m = mquery(col).remove({ name: 'Whiskers' });
  1912. m.remove({ color: '#fff' });
  1913. assert.deepEqual({ name: 'Whiskers', color: '#fff' }, m._conditions);
  1914. });
  1915. it('query', function() {
  1916. const q = mquery({ color: '#fff' });
  1917. const m = mquery(col).remove({ name: 'Whiskers' });
  1918. m.remove(q);
  1919. assert.deepEqual({ name: 'Whiskers', color: '#fff' }, m._conditions);
  1920. });
  1921. it('function', function(done) {
  1922. mquery(col).where({ name: name }).remove(function(err) {
  1923. assert.ifError(err);
  1924. mquery(col).findOne({ name: name }, function(err, doc) {
  1925. assert.ifError(err);
  1926. assert.equal(null, doc);
  1927. done();
  1928. });
  1929. });
  1930. });
  1931. it('boolean (true) - execute', function(done) {
  1932. col.insertOne({ name: name }, function(err) {
  1933. assert.ifError(err);
  1934. mquery(col).findOne({ name: name }, function(err, doc) {
  1935. assert.ifError(err);
  1936. assert.ok(doc);
  1937. mquery(col).remove(true);
  1938. setTimeout(function() {
  1939. mquery(col).find(function(err, docs) {
  1940. assert.ifError(err);
  1941. assert.ok(docs);
  1942. assert.equal(0, docs.length);
  1943. done();
  1944. });
  1945. }, 300);
  1946. });
  1947. });
  1948. });
  1949. });
  1950. });
  1951. describe('with 2 arguments', function() {
  1952. const name = 'remove: 2 arg test';
  1953. beforeEach(function(done) {
  1954. col.remove({}, function(err) {
  1955. assert.ifError(err);
  1956. col.insertMany([{ name: 'shelly' }, { name: name }], function(err) {
  1957. assert.ifError(err);
  1958. mquery(col).find(function(err, docs) {
  1959. assert.ifError(err);
  1960. assert.equal(2, docs.length);
  1961. done();
  1962. });
  1963. });
  1964. });
  1965. });
  1966. describe('plain object + callback', function() {
  1967. it('works', function(done) {
  1968. mquery(col).remove({ name: name }, function(err) {
  1969. assert.ifError(err);
  1970. mquery(col).find(function(err, docs) {
  1971. assert.ifError(err);
  1972. assert.ok(docs);
  1973. assert.equal(1, docs.length);
  1974. assert.equal('shelly', docs[0].name);
  1975. done();
  1976. });
  1977. });
  1978. });
  1979. });
  1980. describe('mquery + callback', function() {
  1981. it('works', function(done) {
  1982. const m = mquery({ name: name });
  1983. mquery(col).remove(m, function(err) {
  1984. assert.ifError(err);
  1985. mquery(col).find(function(err, docs) {
  1986. assert.ifError(err);
  1987. assert.ok(docs);
  1988. assert.equal(1, docs.length);
  1989. assert.equal('shelly', docs[0].name);
  1990. done();
  1991. });
  1992. });
  1993. });
  1994. });
  1995. });
  1996. });
  1997. function validateFindAndModifyOptions(method) {
  1998. describe('validates its option', function() {
  1999. it('sort', function(done) {
  2000. assert.doesNotThrow(function() {
  2001. mquery().sort('x')[method]();
  2002. });
  2003. done();
  2004. });
  2005. it('select', function(done) {
  2006. assert.doesNotThrow(function() {
  2007. mquery().select('x')[method]();
  2008. });
  2009. done();
  2010. });
  2011. it('limit', function(done) {
  2012. assert.throws(function() {
  2013. mquery().limit(3)[method]();
  2014. }, new RegExp('limit cannot be used with ' + method));
  2015. done();
  2016. });
  2017. it('skip', function(done) {
  2018. assert.throws(function() {
  2019. mquery().skip(3)[method]();
  2020. }, new RegExp('skip cannot be used with ' + method));
  2021. done();
  2022. });
  2023. it('batchSize', function(done) {
  2024. assert.throws(function() {
  2025. mquery({}, { batchSize: 3 })[method]();
  2026. }, new RegExp('batchSize cannot be used with ' + method));
  2027. done();
  2028. });
  2029. it('maxScan', function(done) {
  2030. assert.throws(function() {
  2031. mquery().maxScan(300)[method]();
  2032. }, new RegExp('maxScan cannot be used with ' + method));
  2033. done();
  2034. });
  2035. it('snapshot', function(done) {
  2036. assert.throws(function() {
  2037. mquery().snapshot()[method]();
  2038. }, new RegExp('snapshot cannot be used with ' + method));
  2039. done();
  2040. });
  2041. it('tailable', function(done) {
  2042. assert.throws(function() {
  2043. mquery().tailable()[method]();
  2044. }, new RegExp('tailable cannot be used with ' + method));
  2045. done();
  2046. });
  2047. });
  2048. }
  2049. describe('findOneAndUpdate', function() {
  2050. let name = 'findOneAndUpdate + fn';
  2051. validateFindAndModifyOptions('findOneAndUpdate');
  2052. describe('with 0 args', function() {
  2053. it('makes no changes', function() {
  2054. const m = mquery();
  2055. const n = m.findOneAndUpdate();
  2056. assert.deepEqual(m, n);
  2057. });
  2058. });
  2059. describe('with 1 arg', function() {
  2060. describe('that is an object', function() {
  2061. it('updates the doc', function() {
  2062. const m = mquery();
  2063. const n = m.findOneAndUpdate({ $set: { name: '1 arg' } });
  2064. assert.deepEqual(n._update, { $set: { name: '1 arg' } });
  2065. });
  2066. });
  2067. describe('that is a query', function() {
  2068. it('updates the doc', function() {
  2069. const m = mquery({ name: name }).updateOne({ x: 1 });
  2070. const n = mquery().findOneAndUpdate(m);
  2071. assert.deepEqual(n._update, { x: 1 });
  2072. });
  2073. });
  2074. it('that is a function', function(done) {
  2075. col.insertOne({ name: name }, function(err) {
  2076. assert.ifError(err);
  2077. const m = mquery({ name: name }).collection(col);
  2078. name = '1 arg';
  2079. const n = m.updateOne({ $set: { name: name } }).setOptions({ returnDocument: 'after' });
  2080. n.findOneAndUpdate(function(err, res) {
  2081. assert.ifError(err);
  2082. assert.ok(res.value);
  2083. assert.equal(res.value.name, name);
  2084. done();
  2085. });
  2086. });
  2087. });
  2088. });
  2089. describe('with 2 args', function() {
  2090. it('conditions + update', function() {
  2091. const m = mquery(col);
  2092. m.findOneAndUpdate({ name: name }, { age: 100 });
  2093. assert.deepEqual({ name: name }, m._conditions);
  2094. assert.deepEqual({ age: 100 }, m._update);
  2095. });
  2096. it('query + update', function() {
  2097. const n = mquery({ name: name });
  2098. const m = mquery(col);
  2099. m.findOneAndUpdate(n, { age: 100 });
  2100. assert.deepEqual({ name: name }, m._conditions);
  2101. assert.deepEqual({ age: 100 }, m._update);
  2102. });
  2103. it('update + callback', function(done) {
  2104. const m = mquery(col).where({ name: name });
  2105. m.findOneAndUpdate({}, { $inc: { age: 10 } }, { returnDocument: 'after' }, function(err, res) {
  2106. assert.ifError(err);
  2107. assert.equal(10, res.value.age);
  2108. done();
  2109. });
  2110. });
  2111. });
  2112. describe('with 3 args', function() {
  2113. it('conditions + update + options', function() {
  2114. const m = mquery();
  2115. const n = m.findOneAndUpdate({ name: name }, { works: true }, { returnDocument: 'before' });
  2116. assert.deepEqual({ name: name }, n._conditions);
  2117. assert.deepEqual({ works: true }, n._update);
  2118. assert.deepEqual({ returnDocument: 'before' }, n.options);
  2119. });
  2120. it('conditions + update + callback', function(done) {
  2121. const m = mquery(col);
  2122. m.findOneAndUpdate({ name: name }, { works: true }, { returnDocument: 'after' }, function(err, res) {
  2123. assert.ifError(err);
  2124. assert.ok(res.value);
  2125. assert.equal(name, res.value.name);
  2126. assert.ok(true === res.value.works);
  2127. done();
  2128. });
  2129. });
  2130. });
  2131. describe('with 4 args', function() {
  2132. it('conditions + update + options + callback', function(done) {
  2133. const m = mquery(col);
  2134. m.findOneAndUpdate({ name: name }, { works: false }, {}, function(err, res) {
  2135. assert.ifError(err);
  2136. assert.ok(res.value);
  2137. assert.equal(name, res.value.name);
  2138. assert.ok(true === res.value.works);
  2139. done();
  2140. });
  2141. });
  2142. });
  2143. });
  2144. describe('findOneAndRemove', function() {
  2145. let name = 'findOneAndRemove';
  2146. validateFindAndModifyOptions('findOneAndRemove');
  2147. describe('with 0 args', function() {
  2148. it('makes no changes', function() {
  2149. const m = mquery();
  2150. const n = m.findOneAndRemove();
  2151. assert.deepEqual(m, n);
  2152. });
  2153. });
  2154. describe('with 1 arg', function() {
  2155. describe('that is an object', function() {
  2156. it('updates the doc', function() {
  2157. const m = mquery();
  2158. const n = m.findOneAndRemove({ name: '1 arg' });
  2159. assert.deepEqual(n._conditions, { name: '1 arg' });
  2160. });
  2161. });
  2162. describe('that is a query', function() {
  2163. it('updates the doc', function() {
  2164. const m = mquery({ name: name });
  2165. const n = m.findOneAndRemove(m);
  2166. assert.deepEqual(n._conditions, { name: name });
  2167. });
  2168. });
  2169. it('that is a function', function(done) {
  2170. col.insertOne({ name: name }, function(err) {
  2171. assert.ifError(err);
  2172. const m = mquery({ name: name }).collection(col);
  2173. m.findOneAndRemove(function(err, res) {
  2174. assert.ifError(err);
  2175. assert.ok(res.value);
  2176. assert.equal(name, res.value.name);
  2177. done();
  2178. });
  2179. });
  2180. });
  2181. });
  2182. describe('with 2 args', function() {
  2183. it('conditions + options', function() {
  2184. const m = mquery(col);
  2185. m.findOneAndRemove({ name: name }, { returnDocument: 'after' });
  2186. assert.deepEqual({ name: name }, m._conditions);
  2187. assert.deepEqual({ returnDocument: 'after' }, m.options);
  2188. });
  2189. it('query + options', function() {
  2190. const n = mquery({ name: name });
  2191. const m = mquery(col);
  2192. m.findOneAndRemove(n, { sort: { x: 1 } });
  2193. assert.deepEqual({ name: name }, m._conditions);
  2194. assert.deepEqual({ sort: { x: 1 } }, m.options);
  2195. });
  2196. it('conditions + callback', function(done) {
  2197. col.insertOne({ name: name }, function(err) {
  2198. assert.ifError(err);
  2199. const m = mquery(col);
  2200. m.findOneAndRemove({ name: name }, function(err, res) {
  2201. assert.ifError(err);
  2202. assert.equal(name, res.value.name);
  2203. done();
  2204. });
  2205. });
  2206. });
  2207. it('query + callback', function(done) {
  2208. col.insertOne({ name: name }, function(err) {
  2209. assert.ifError(err);
  2210. const n = mquery({ name: name });
  2211. const m = mquery(col);
  2212. m.findOneAndRemove(n, function(err, res) {
  2213. assert.ifError(err);
  2214. assert.equal(name, res.value.name);
  2215. done();
  2216. });
  2217. });
  2218. });
  2219. });
  2220. describe('with 3 args', function() {
  2221. it('conditions + options + callback', function(done) {
  2222. name = 'findOneAndRemove + conds + options + cb';
  2223. col.insertMany([{ name: name }, { name: 'a' }], function(err) {
  2224. assert.ifError(err);
  2225. const m = mquery(col);
  2226. m.findOneAndRemove({ name: name }, { sort: { name: 1 } }, function(err, res) {
  2227. assert.ifError(err);
  2228. assert.ok(res.value);
  2229. assert.equal(name, res.value.name);
  2230. done();
  2231. });
  2232. });
  2233. });
  2234. });
  2235. });
  2236. describe('exec', function() {
  2237. beforeEach(function(done) {
  2238. col.insertMany([{ name: 'exec', age: 1 }, { name: 'exec', age: 2 }], done);
  2239. });
  2240. afterEach(function(done) {
  2241. mquery(col).remove(done);
  2242. });
  2243. it('requires an op', function() {
  2244. assert.throws(function() {
  2245. mquery().exec();
  2246. }, /Missing query type/);
  2247. });
  2248. describe('find', function() {
  2249. it('works', function(done) {
  2250. const m = mquery(col).find({ name: 'exec' });
  2251. m.exec(function(err, docs) {
  2252. assert.ifError(err);
  2253. assert.equal(2, docs.length);
  2254. done();
  2255. });
  2256. });
  2257. it('works with readPreferences', function(done) {
  2258. const m = mquery(col).find({ name: 'exec' });
  2259. try {
  2260. const ReadPreference = require('mongodb').ReadPreference;
  2261. const rp = new ReadPreference('primary');
  2262. m.read(rp);
  2263. } catch (e) {
  2264. done(e.code === 'MODULE_NOT_FOUND' ? null : e);
  2265. return;
  2266. }
  2267. m.exec(function(err, docs) {
  2268. assert.ifError(err);
  2269. assert.equal(2, docs.length);
  2270. done();
  2271. });
  2272. });
  2273. it('works with hint', function(done) {
  2274. mquery(col).hint({ _id: 1 }).find({ name: 'exec' }).exec(function(err, docs) {
  2275. assert.ifError(err);
  2276. assert.equal(2, docs.length);
  2277. mquery(col).hint('_id_').find({ age: 1 }).exec(function(err, docs) {
  2278. assert.ifError(err);
  2279. assert.equal(1, docs.length);
  2280. done();
  2281. });
  2282. });
  2283. });
  2284. it('works with readConcern', function(done) {
  2285. const m = mquery(col).find({ name: 'exec' });
  2286. m.readConcern('l');
  2287. m.exec(function(err, docs) {
  2288. assert.ifError(err);
  2289. assert.equal(2, docs.length);
  2290. done();
  2291. });
  2292. });
  2293. it('works with collation', function(done) {
  2294. const m = mquery(col).find({ name: 'EXEC' });
  2295. m.collation({ locale: 'en_US', strength: 1 });
  2296. m.exec(function(err, docs) {
  2297. assert.ifError(err);
  2298. assert.equal(2, docs.length);
  2299. done();
  2300. });
  2301. });
  2302. });
  2303. it('findOne', function(done) {
  2304. const m = mquery(col).findOne({ age: 2 });
  2305. m.exec(function(err, doc) {
  2306. assert.ifError(err);
  2307. assert.equal(2, doc.age);
  2308. done();
  2309. });
  2310. });
  2311. it('count', function(done) {
  2312. const m = mquery(col).count({ name: 'exec' });
  2313. m.exec(function(err, count) {
  2314. assert.ifError(err);
  2315. assert.equal(2, count);
  2316. done();
  2317. });
  2318. });
  2319. it('distinct', function(done) {
  2320. const m = mquery({ name: 'exec' });
  2321. m.collection(col);
  2322. m.distinct('age');
  2323. m.exec(function(err, array) {
  2324. assert.ifError(err);
  2325. assert.ok(Array.isArray(array));
  2326. assert.equal(2, array.length);
  2327. assert(~array.indexOf(1));
  2328. assert(~array.indexOf(2));
  2329. done();
  2330. });
  2331. });
  2332. describe('update', function() {
  2333. describe('updateMany', function() {
  2334. it('works', function(done) {
  2335. mquery(col).updateMany({ name: 'exec' }, { name: 'test' }).
  2336. exec(function(error) {
  2337. assert.ifError(error);
  2338. mquery(col).count({ name: 'test' }).exec(function(error, res) {
  2339. assert.ifError(error);
  2340. assert.equal(res, 2);
  2341. done();
  2342. });
  2343. });
  2344. });
  2345. it('works with write concern', function(done) {
  2346. mquery(col).updateMany({ name: 'exec' }, { name: 'test' })
  2347. .w(1).j(true).wtimeout(1000)
  2348. .exec(function(error) {
  2349. assert.ifError(error);
  2350. mquery(col).count({ name: 'test' }).exec(function(error, res) {
  2351. assert.ifError(error);
  2352. assert.equal(res, 2);
  2353. done();
  2354. });
  2355. });
  2356. });
  2357. });
  2358. describe('updateOne', function() {
  2359. it('works', function(done) {
  2360. mquery(col).updateOne({ name: 'exec' }, { name: 'test' }).
  2361. exec(function(error) {
  2362. assert.ifError(error);
  2363. mquery(col).count({ name: 'test' }).exec(function(error, res) {
  2364. assert.ifError(error);
  2365. assert.equal(res, 1);
  2366. done();
  2367. });
  2368. });
  2369. });
  2370. });
  2371. describe('replaceOne', function() {
  2372. it('works', function(done) {
  2373. mquery(col).replaceOne({ name: 'exec' }, { name: 'test' }).
  2374. exec(function(error) {
  2375. assert.ifError(error);
  2376. mquery(col).findOne({ name: 'test' }).exec(function(error, res) {
  2377. assert.ifError(error);
  2378. assert.equal(res.name, 'test');
  2379. assert.ok(res.age == null);
  2380. done();
  2381. });
  2382. });
  2383. });
  2384. });
  2385. });
  2386. describe('remove', function() {
  2387. it('with a callback', function(done) {
  2388. const m = mquery(col).where({ age: 2 }).remove();
  2389. m.exec(function(err, res) {
  2390. assert.ifError(err);
  2391. assert.equal(1, res.deletedCount);
  2392. done();
  2393. });
  2394. });
  2395. it('without a callback', function(done) {
  2396. const m = mquery(col).where({ age: 1 }).remove();
  2397. m.exec();
  2398. setTimeout(function() {
  2399. mquery(col).where('name', 'exec').count(function(err, num) {
  2400. assert.equal(1, num);
  2401. done();
  2402. });
  2403. }, 200);
  2404. });
  2405. });
  2406. describe('deleteOne', function() {
  2407. it('with a callback', function(done) {
  2408. const m = mquery(col).where({ age: { $gte: 0 } }).deleteOne();
  2409. m.exec(function(err, res) {
  2410. assert.ifError(err);
  2411. assert.equal(res.deletedCount, 1);
  2412. done();
  2413. });
  2414. });
  2415. it('with justOne set', function(done) {
  2416. const m = mquery(col).where({ age: { $gte: 0 } }).
  2417. // Should ignore `justOne`
  2418. setOptions({ justOne: false }).
  2419. deleteOne();
  2420. m.exec(function(err, res) {
  2421. assert.ifError(err);
  2422. assert.equal(res.deletedCount, 1);
  2423. done();
  2424. });
  2425. });
  2426. });
  2427. describe('deleteMany', function() {
  2428. it('with a callback', function(done) {
  2429. const m = mquery(col).where({ age: { $gte: 0 } }).deleteMany();
  2430. m.exec(function(err, res) {
  2431. assert.ifError(err);
  2432. assert.equal(res.deletedCount, 2);
  2433. done();
  2434. });
  2435. });
  2436. });
  2437. describe('findOneAndUpdate', function() {
  2438. it('with a callback', function(done) {
  2439. const m = mquery(col);
  2440. m.findOneAndUpdate({ name: 'exec', age: 1 }, { $set: { name: 'findOneAndUpdate' } }, { returnDocument: 'after' });
  2441. m.exec(function(err, res) {
  2442. assert.ifError(err);
  2443. assert.equal('findOneAndUpdate', res.value.name);
  2444. done();
  2445. });
  2446. });
  2447. });
  2448. describe('findOneAndRemove', function() {
  2449. it('with a callback', function(done) {
  2450. const m = mquery(col);
  2451. m.findOneAndRemove({ name: 'exec', age: 2 });
  2452. m.exec(function(err, res) {
  2453. assert.ifError(err);
  2454. assert.equal('exec', res.value.name);
  2455. assert.equal(2, res.value.age);
  2456. mquery(col).count({ name: 'exec' }, function(err, num) {
  2457. assert.ifError(err);
  2458. assert.equal(1, num);
  2459. done();
  2460. });
  2461. });
  2462. });
  2463. });
  2464. });
  2465. describe('setTraceFunction', function() {
  2466. beforeEach(function(done) {
  2467. col.insertMany([{ name: 'trace', age: 93 }], done);
  2468. });
  2469. it('calls trace function when executing query', function(done) {
  2470. const m = mquery(col);
  2471. let resultTraceCalled;
  2472. m.setTraceFunction(function(method, queryInfo) {
  2473. try {
  2474. assert.equal('findOne', method);
  2475. assert.equal('trace', queryInfo.conditions.name);
  2476. } catch (e) {
  2477. done(e);
  2478. }
  2479. return function(err, result, millis) {
  2480. try {
  2481. assert.equal(93, result.age);
  2482. assert.ok(typeof millis === 'number');
  2483. } catch (e) {
  2484. done(e);
  2485. }
  2486. resultTraceCalled = true;
  2487. };
  2488. });
  2489. m.findOne({ name: 'trace' }, function(err, doc) {
  2490. assert.ifError(err);
  2491. assert.equal(resultTraceCalled, true);
  2492. assert.equal(93, doc.age);
  2493. done();
  2494. });
  2495. });
  2496. it('inherits trace function when calling toConstructor', function(done) {
  2497. function traceFunction() { return function() {}; }
  2498. const tracedQuery = mquery().setTraceFunction(traceFunction).toConstructor();
  2499. const query = tracedQuery();
  2500. assert.equal(traceFunction, query._traceFunction);
  2501. done();
  2502. });
  2503. });
  2504. describe('thunk', function() {
  2505. it('returns a function', function(done) {
  2506. assert.equal('function', typeof mquery().thunk());
  2507. done();
  2508. });
  2509. it('passes the fn arg to `exec`', function(done) {
  2510. function cb() {}
  2511. const m = mquery();
  2512. m.exec = function testing(fn) {
  2513. assert.equal(this, m);
  2514. assert.equal(cb, fn);
  2515. done();
  2516. };
  2517. m.thunk()(cb);
  2518. });
  2519. });
  2520. describe('then', function() {
  2521. before(function(done) {
  2522. col.insertMany([{ name: 'then', age: 1 }, { name: 'then', age: 2 }], done);
  2523. });
  2524. after(function(done) {
  2525. mquery(col).remove({ name: 'then' }).exec(done);
  2526. });
  2527. it('returns a promise A+ compat object', function(done) {
  2528. const m = mquery(col).find();
  2529. assert.equal('function', typeof m.then);
  2530. done();
  2531. });
  2532. it('creates a promise that is resolved on success', function(done) {
  2533. const promise = mquery(col).count({ name: 'then' }).then();
  2534. promise.then(function(count) {
  2535. assert.equal(2, count);
  2536. done();
  2537. }, done);
  2538. });
  2539. it('supports exec() cb being called synchronously #66', function(done) {
  2540. const query = mquery(col).count({ name: 'then' });
  2541. query.exec = function(cb) {
  2542. cb(null, 66);
  2543. };
  2544. query.then(success, done);
  2545. function success(count) {
  2546. assert.equal(66, count);
  2547. done();
  2548. }
  2549. });
  2550. });
  2551. describe('stream', function() {
  2552. before(function(done) {
  2553. col.insertMany([{ name: 'stream', age: 1 }, { name: 'stream', age: 2 }], done);
  2554. });
  2555. after(function(done) {
  2556. mquery(col).remove({ name: 'stream' }).exec(done);
  2557. });
  2558. describe('throws', function() {
  2559. describe('if used with non-find operations', function() {
  2560. const ops = ['update', 'findOneAndUpdate', 'remove', 'count', 'distinct'];
  2561. ops.forEach(function(op) {
  2562. assert.throws(function() {
  2563. mquery(col)[op]().stream();
  2564. });
  2565. });
  2566. });
  2567. });
  2568. it('returns a stream', function(done) {
  2569. const stream = mquery(col).find({ name: 'stream' }).cursor().stream();
  2570. let count = 0;
  2571. let err;
  2572. stream.on('data', function(doc) {
  2573. assert.equal('stream', doc.name);
  2574. ++count;
  2575. });
  2576. stream.on('error', function(er) {
  2577. err = er;
  2578. });
  2579. stream.on('end', function() {
  2580. if (err) return done(err);
  2581. assert.equal(2, count);
  2582. done();
  2583. });
  2584. });
  2585. });
  2586. function noDistinct(type) {
  2587. it('cannot be used with distinct()', function(done) {
  2588. assert.throws(function() {
  2589. mquery().distinct('name')[type](4);
  2590. }, new RegExp(type + ' cannot be used with distinct'));
  2591. done();
  2592. });
  2593. }
  2594. function no(method, type) {
  2595. it('cannot be used with ' + method + '()', function(done) {
  2596. assert.throws(function() {
  2597. mquery()[method]()[type](4);
  2598. }, new RegExp(type + ' cannot be used with ' + method));
  2599. done();
  2600. });
  2601. }
  2602. // query internal
  2603. describe('_updateForExec', function() {
  2604. it('returns a clone of the update object with same key order #19', function(done) {
  2605. const update = {};
  2606. update.$push = { n: { $each: [{ x: 10 }], $slice: -1, $sort: { x: 1 } } };
  2607. const q = mquery().updateOne({ x: 1 }, update);
  2608. // capture original key order
  2609. const order = [];
  2610. let key;
  2611. for (key in q._update.$push.n) {
  2612. order.push(key);
  2613. }
  2614. // compare output
  2615. const doc = q._updateForExec();
  2616. let i = 0;
  2617. for (key in doc.$push.n) {
  2618. assert.equal(key, order[i]);
  2619. i++;
  2620. }
  2621. done();
  2622. });
  2623. });
  2624. });