index.js 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954
  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. comment: { distinct: false, count: false }
  1098. };
  1099. Object.keys(negated).forEach(function(key) {
  1100. simpleOption(key, negated[key]);
  1101. });
  1102. describe('snapshot', function() {
  1103. it('works', function() {
  1104. let query;
  1105. query = mquery();
  1106. query.snapshot();
  1107. assert.equal(true, query.options.snapshot);
  1108. query = mquery();
  1109. query.snapshot(true);
  1110. assert.equal(true, query.options.snapshot);
  1111. query = mquery();
  1112. query.snapshot(false);
  1113. assert.equal(false, query.options.snapshot);
  1114. });
  1115. noDistinct('snapshot');
  1116. no('count', 'snapshot');
  1117. });
  1118. describe('hint', function() {
  1119. it('accepts an object', function() {
  1120. const query2 = mquery();
  1121. query2.hint({ a: 1, b: -1 });
  1122. assert.deepEqual(query2.options.hint, { a: 1, b: -1 });
  1123. });
  1124. it('accepts a string', function() {
  1125. const query2 = mquery();
  1126. query2.hint('a');
  1127. assert.deepEqual(query2.options.hint, 'a');
  1128. });
  1129. it('rejects everything else', function() {
  1130. assert.throws(function() {
  1131. mquery().hint(['c']);
  1132. }, /Invalid hint./);
  1133. assert.throws(function() {
  1134. mquery().hint(1);
  1135. }, /Invalid hint./);
  1136. });
  1137. describe('does not have side affects', function() {
  1138. it('on invalid arg', function() {
  1139. const m = mquery();
  1140. try {
  1141. m.hint(1);
  1142. } catch (err) {
  1143. // ignore
  1144. }
  1145. assert.equal(undefined, m.options.hint);
  1146. });
  1147. it('on missing arg', function() {
  1148. const m = mquery().hint();
  1149. assert.equal(undefined, m.options.hint);
  1150. });
  1151. });
  1152. noDistinct('hint');
  1153. });
  1154. describe('j', function() {
  1155. it('works', function() {
  1156. const m = mquery().j(true);
  1157. assert.equal(true, m.options.j);
  1158. });
  1159. });
  1160. describe('slaveOk', function() {
  1161. it('works', function() {
  1162. let query;
  1163. query = mquery();
  1164. query.slaveOk();
  1165. assert.equal(true, query.options.slaveOk);
  1166. query = mquery();
  1167. query.slaveOk(true);
  1168. assert.equal(true, query.options.slaveOk);
  1169. query = mquery();
  1170. query.slaveOk(false);
  1171. assert.equal(false, query.options.slaveOk);
  1172. });
  1173. });
  1174. describe('read', function() {
  1175. it('sets associated readPreference option', function() {
  1176. const m = mquery();
  1177. m.read('p');
  1178. assert.equal('primary', m.options.readPreference);
  1179. });
  1180. it('is chainable', function() {
  1181. const m = mquery();
  1182. assert.equal(m, m.read('sp'));
  1183. });
  1184. });
  1185. describe('readConcern', function() {
  1186. it('sets associated readConcern option', function() {
  1187. let m;
  1188. m = mquery();
  1189. m.readConcern('s');
  1190. assert.deepEqual({ level: 'snapshot' }, m.options.readConcern);
  1191. m = mquery();
  1192. m.r('local');
  1193. assert.deepEqual({ level: 'local' }, m.options.readConcern);
  1194. });
  1195. it('is chainable', function() {
  1196. const m = mquery();
  1197. assert.equal(m, m.readConcern('lz'));
  1198. });
  1199. });
  1200. describe('tailable', function() {
  1201. it('works', function() {
  1202. let query;
  1203. query = mquery();
  1204. query.tailable();
  1205. assert.equal(true, query.options.tailable);
  1206. query = mquery();
  1207. query.tailable(true);
  1208. assert.equal(true, query.options.tailable);
  1209. query = mquery();
  1210. query.tailable(false);
  1211. assert.equal(false, query.options.tailable);
  1212. });
  1213. it('is chainable', function() {
  1214. const m = mquery();
  1215. assert.equal(m, m.tailable());
  1216. });
  1217. noDistinct('tailable');
  1218. no('count', 'tailable');
  1219. });
  1220. describe('writeConcern', function() {
  1221. it('sets associated writeConcern option', function() {
  1222. let m;
  1223. m = mquery();
  1224. m.writeConcern('majority');
  1225. assert.equal('majority', m.options.w);
  1226. m = mquery();
  1227. m.writeConcern('m'); // m is alias of majority
  1228. assert.equal('majority', m.options.w);
  1229. m = mquery();
  1230. m.writeConcern(1);
  1231. assert.equal(1, m.options.w);
  1232. });
  1233. it('accepts object', function() {
  1234. let m;
  1235. m = mquery().writeConcern({ w: 'm', j: true, wtimeout: 1000 });
  1236. assert.equal('m', m.options.w); // check it does not convert m to majority
  1237. assert.equal(true, m.options.j);
  1238. assert.equal(1000, m.options.wtimeout);
  1239. m = mquery().w('m').w({ j: false, wtimeout: 0 });
  1240. assert.equal('majority', m.options.w);
  1241. assert.strictEqual(false, m.options.j);
  1242. assert.strictEqual(0, m.options.wtimeout);
  1243. });
  1244. it('is chainable', function() {
  1245. const m = mquery();
  1246. assert.equal(m, m.writeConcern('majority'));
  1247. });
  1248. });
  1249. // query utilities
  1250. describe('merge', function() {
  1251. describe('with falsy arg', function() {
  1252. it('returns itself', function() {
  1253. const m = mquery();
  1254. assert.equal(m, m.merge());
  1255. assert.equal(m, m.merge(null));
  1256. assert.equal(m, m.merge(0));
  1257. });
  1258. });
  1259. describe('with an argument', function() {
  1260. describe('that is not a query or plain object', function() {
  1261. it('throws', function() {
  1262. assert.throws(function() {
  1263. mquery().merge([]);
  1264. }, /Invalid argument/);
  1265. assert.throws(function() {
  1266. mquery().merge('merge');
  1267. }, /Invalid argument/);
  1268. assert.doesNotThrow(function() {
  1269. mquery().merge({});
  1270. }, /Invalid argument/);
  1271. });
  1272. });
  1273. describe('that is a query', function() {
  1274. it('merges conditions, field selection, and options', function() {
  1275. const m = mquery({ x: 'hi' }, { select: 'x y', another: true });
  1276. const n = mquery().merge(m);
  1277. assert.deepEqual(n._conditions, m._conditions);
  1278. assert.deepEqual(n._fields, m._fields);
  1279. assert.deepEqual(n.options, m.options);
  1280. });
  1281. it('clones update arguments', function(done) {
  1282. const original = { $set: { iTerm: true } };
  1283. const m = mquery().updateOne(original);
  1284. const n = mquery().merge(m);
  1285. m.updateOne({ $set: { x: 2 } });
  1286. assert.notDeepEqual(m._update, n._update);
  1287. done();
  1288. });
  1289. it('is chainable', function() {
  1290. const m = mquery({ x: 'hi' });
  1291. const n = mquery();
  1292. assert.equal(n, n.merge(m));
  1293. });
  1294. });
  1295. describe('that is an object', function() {
  1296. it('merges', function() {
  1297. const m = { x: 'hi' };
  1298. const n = mquery().merge(m);
  1299. assert.deepEqual(n._conditions, { x: 'hi' });
  1300. });
  1301. it('clones update arguments', function(done) {
  1302. const original = { $set: { iTerm: true } };
  1303. const m = mquery().updateOne(original);
  1304. const n = mquery().merge(original);
  1305. m.updateOne({ $set: { x: 2 } });
  1306. assert.notDeepEqual(m._update, n._update);
  1307. done();
  1308. });
  1309. it('is chainable', function() {
  1310. const m = { x: 'hi' };
  1311. const n = mquery();
  1312. assert.equal(n, n.merge(m));
  1313. });
  1314. });
  1315. });
  1316. });
  1317. // queries
  1318. describe('find', function() {
  1319. describe('with no callback', function() {
  1320. it('does not execute', function() {
  1321. const m = mquery();
  1322. assert.doesNotThrow(function() {
  1323. m.find();
  1324. });
  1325. assert.doesNotThrow(function() {
  1326. m.find({ x: 1 });
  1327. });
  1328. });
  1329. });
  1330. it('is chainable', function() {
  1331. const m = mquery().find({ x: 1 }).find().find({ y: 2 });
  1332. assert.deepEqual(m._conditions, { x: 1, y: 2 });
  1333. });
  1334. it('merges other queries', function() {
  1335. const m = mquery({ name: 'mquery' });
  1336. m.tailable();
  1337. m.select('_id');
  1338. const a = mquery().find(m);
  1339. assert.deepEqual(a._conditions, m._conditions);
  1340. assert.deepEqual(a.options, m.options);
  1341. assert.deepEqual(a._fields, m._fields);
  1342. });
  1343. describe('executes', function() {
  1344. before(function(done) {
  1345. col.insertOne({ name: 'mquery' }, done);
  1346. });
  1347. after(function(done) {
  1348. col.remove({ name: 'mquery' }, done);
  1349. });
  1350. it('when criteria is passed with a callback', function(done) {
  1351. mquery(col).find({ name: 'mquery' }, function(err, docs) {
  1352. assert.ifError(err);
  1353. assert.equal(1, docs.length);
  1354. done();
  1355. });
  1356. });
  1357. it('when Query is passed with a callback', function(done) {
  1358. const m = mquery({ name: 'mquery' });
  1359. mquery(col).find(m, function(err, docs) {
  1360. assert.ifError(err);
  1361. assert.equal(1, docs.length);
  1362. done();
  1363. });
  1364. });
  1365. it('when just a callback is passed', function(done) {
  1366. mquery({ name: 'mquery' }).collection(col).find(function(err, docs) {
  1367. assert.ifError(err);
  1368. assert.equal(1, docs.length);
  1369. done();
  1370. });
  1371. });
  1372. });
  1373. });
  1374. describe('findOne', function() {
  1375. describe('with no callback', function() {
  1376. it('does not execute', function() {
  1377. const m = mquery();
  1378. assert.doesNotThrow(function() {
  1379. m.findOne();
  1380. });
  1381. assert.doesNotThrow(function() {
  1382. m.findOne({ x: 1 });
  1383. });
  1384. });
  1385. });
  1386. it('is chainable', function() {
  1387. const m = mquery();
  1388. const n = m.findOne({ x: 1 }).findOne().findOne({ y: 2 });
  1389. assert.equal(m, n);
  1390. assert.deepEqual(m._conditions, { x: 1, y: 2 });
  1391. assert.equal('findOne', m.op);
  1392. });
  1393. it('merges other queries', function() {
  1394. const m = mquery({ name: 'mquery' });
  1395. m.read('nearest');
  1396. m.select('_id');
  1397. const a = mquery().findOne(m);
  1398. assert.deepEqual(a._conditions, m._conditions);
  1399. assert.deepEqual(a.options, m.options);
  1400. assert.deepEqual(a._fields, m._fields);
  1401. });
  1402. describe('executes', function() {
  1403. before(function(done) {
  1404. col.insertOne({ name: 'mquery findone' }, done);
  1405. });
  1406. after(function(done) {
  1407. col.remove({ name: 'mquery findone' }, done);
  1408. });
  1409. it('when criteria is passed with a callback', function(done) {
  1410. mquery(col).findOne({ name: 'mquery findone' }, function(err, doc) {
  1411. assert.ifError(err);
  1412. assert.ok(doc);
  1413. assert.equal('mquery findone', doc.name);
  1414. done();
  1415. });
  1416. });
  1417. it('when Query is passed with a callback', function(done) {
  1418. const m = mquery(col).where({ name: 'mquery findone' });
  1419. mquery(col).findOne(m, function(err, doc) {
  1420. assert.ifError(err);
  1421. assert.ok(doc);
  1422. assert.equal('mquery findone', doc.name);
  1423. done();
  1424. });
  1425. });
  1426. it('when just a callback is passed', function(done) {
  1427. mquery({ name: 'mquery findone' }).collection(col).findOne(function(err, doc) {
  1428. assert.ifError(err);
  1429. assert.ok(doc);
  1430. assert.equal('mquery findone', doc.name);
  1431. done();
  1432. });
  1433. });
  1434. });
  1435. });
  1436. describe('count', function() {
  1437. describe('with no callback', function() {
  1438. it('does not execute', function() {
  1439. const m = mquery();
  1440. assert.doesNotThrow(function() {
  1441. m.count();
  1442. });
  1443. assert.doesNotThrow(function() {
  1444. m.count({ x: 1 });
  1445. });
  1446. });
  1447. });
  1448. it('is chainable', function() {
  1449. const m = mquery();
  1450. const n = m.count({ x: 1 }).count().count({ y: 2 });
  1451. assert.equal(m, n);
  1452. assert.deepEqual(m._conditions, { x: 1, y: 2 });
  1453. assert.equal('count', m.op);
  1454. });
  1455. it('merges other queries', function() {
  1456. const m = mquery({ name: 'mquery' });
  1457. m.read('nearest');
  1458. m.select('_id');
  1459. const a = mquery().count(m);
  1460. assert.deepEqual(a._conditions, m._conditions);
  1461. assert.deepEqual(a.options, m.options);
  1462. assert.deepEqual(a._fields, m._fields);
  1463. });
  1464. describe('executes', function() {
  1465. before(function(done) {
  1466. col.insertOne({ name: 'mquery count' }, done);
  1467. });
  1468. after(function(done) {
  1469. col.remove({ name: 'mquery count' }, done);
  1470. });
  1471. it('when criteria is passed with a callback', function(done) {
  1472. mquery(col).count({ name: 'mquery count' }, function(err, count) {
  1473. assert.ifError(err);
  1474. assert.ok(count);
  1475. assert.ok(1 === count);
  1476. done();
  1477. });
  1478. });
  1479. it('when Query is passed with a callback', function(done) {
  1480. const m = mquery({ name: 'mquery count' });
  1481. mquery(col).count(m, function(err, count) {
  1482. assert.ifError(err);
  1483. assert.ok(count);
  1484. assert.ok(1 === count);
  1485. done();
  1486. });
  1487. });
  1488. it('when just a callback is passed', function(done) {
  1489. mquery({ name: 'mquery count' }).collection(col).count(function(err, count) {
  1490. assert.ifError(err);
  1491. assert.ok(1 === count);
  1492. done();
  1493. });
  1494. });
  1495. });
  1496. describe('validates its option', function() {
  1497. it('sort', function(done) {
  1498. assert.doesNotThrow(function() {
  1499. mquery().sort('x').count();
  1500. });
  1501. done();
  1502. });
  1503. it('select', function(done) {
  1504. assert.throws(function() {
  1505. mquery().select('x').count();
  1506. }, /field selection and slice cannot be used with count/);
  1507. done();
  1508. });
  1509. it('slice', function(done) {
  1510. assert.throws(function() {
  1511. mquery().where('x').slice(-3).count();
  1512. }, /field selection and slice cannot be used with count/);
  1513. done();
  1514. });
  1515. it('limit', function(done) {
  1516. assert.doesNotThrow(function() {
  1517. mquery().limit(3).count();
  1518. });
  1519. done();
  1520. });
  1521. it('skip', function(done) {
  1522. assert.doesNotThrow(function() {
  1523. mquery().skip(3).count();
  1524. });
  1525. done();
  1526. });
  1527. it('batchSize', function(done) {
  1528. assert.throws(function() {
  1529. mquery({}, { batchSize: 3 }).count();
  1530. }, /batchSize cannot be used with count/);
  1531. done();
  1532. });
  1533. it('comment', function(done) {
  1534. assert.throws(function() {
  1535. mquery().comment('mquery').count();
  1536. }, /comment cannot be used with count/);
  1537. done();
  1538. });
  1539. it('maxScan', function(done) {
  1540. assert.throws(function() {
  1541. mquery().maxScan(300).count();
  1542. }, /maxScan cannot be used with count/);
  1543. done();
  1544. });
  1545. it('snapshot', function(done) {
  1546. assert.throws(function() {
  1547. mquery().snapshot().count();
  1548. }, /snapshot cannot be used with count/);
  1549. done();
  1550. });
  1551. it('tailable', function(done) {
  1552. assert.throws(function() {
  1553. mquery().tailable().count();
  1554. }, /tailable cannot be used with count/);
  1555. done();
  1556. });
  1557. });
  1558. });
  1559. describe('distinct', function() {
  1560. describe('with no callback', function() {
  1561. it('does not execute', function() {
  1562. const m = mquery();
  1563. assert.doesNotThrow(function() {
  1564. m.distinct();
  1565. });
  1566. assert.doesNotThrow(function() {
  1567. m.distinct('name');
  1568. });
  1569. assert.doesNotThrow(function() {
  1570. m.distinct({ name: 'mquery distinct' });
  1571. });
  1572. assert.doesNotThrow(function() {
  1573. m.distinct({ name: 'mquery distinct' }, 'name');
  1574. });
  1575. });
  1576. });
  1577. it('is chainable', function() {
  1578. const m = mquery({ x: 1 }).distinct('name');
  1579. const n = m.distinct({ y: 2 });
  1580. assert.equal(m, n);
  1581. assert.deepEqual(n._conditions, { x: 1, y: 2 });
  1582. assert.equal('name', n._distinct);
  1583. assert.equal('distinct', n.op);
  1584. });
  1585. it('overwrites field', function() {
  1586. const m = mquery({ name: 'mquery' }).distinct('name');
  1587. m.distinct('rename');
  1588. assert.equal(m._distinct, 'rename');
  1589. m.distinct({ x: 1 }, 'renamed');
  1590. assert.equal(m._distinct, 'renamed');
  1591. });
  1592. it('merges other queries', function() {
  1593. const m = mquery().distinct({ name: 'mquery' }, 'age');
  1594. m.read('nearest');
  1595. const a = mquery().distinct(m);
  1596. assert.deepEqual(a._conditions, m._conditions);
  1597. assert.deepEqual(a.options, m.options);
  1598. assert.deepEqual(a._fields, m._fields);
  1599. assert.deepEqual(a._distinct, m._distinct);
  1600. });
  1601. describe('executes', function() {
  1602. before(function(done) {
  1603. col.insertOne({ name: 'mquery distinct', age: 1 }, done);
  1604. });
  1605. after(function(done) {
  1606. col.remove({ name: 'mquery distinct' }, done);
  1607. });
  1608. it('when distinct arg is passed with a callback', function(done) {
  1609. mquery(col).distinct('distinct', function(err, doc) {
  1610. assert.ifError(err);
  1611. assert.ok(doc);
  1612. done();
  1613. });
  1614. });
  1615. describe('when criteria is passed with a callback', function() {
  1616. it('if distinct arg was declared', function(done) {
  1617. mquery(col).distinct('age').distinct({ name: 'mquery distinct' }, function(err, doc) {
  1618. assert.ifError(err);
  1619. assert.ok(doc);
  1620. done();
  1621. });
  1622. });
  1623. it('but not if distinct arg was not declared', function() {
  1624. assert.throws(function() {
  1625. mquery(col).distinct({ name: 'mquery distinct' }, function() {});
  1626. }, /No value for `distinct`/);
  1627. });
  1628. });
  1629. describe('when Query is passed with a callback', function() {
  1630. const m = mquery({ name: 'mquery distinct' });
  1631. it('if distinct arg was declared', function(done) {
  1632. mquery(col).distinct('age').distinct(m, function(err, doc) {
  1633. assert.ifError(err);
  1634. assert.ok(doc);
  1635. done();
  1636. });
  1637. });
  1638. it('but not if distinct arg was not declared', function() {
  1639. assert.throws(function() {
  1640. mquery(col).distinct(m, function() {});
  1641. }, /No value for `distinct`/);
  1642. });
  1643. });
  1644. describe('when just a callback is passed', function() {
  1645. it('if distinct arg was declared', function(done) {
  1646. const m = mquery({ name: 'mquery distinct' });
  1647. m.collection(col);
  1648. m.distinct('age');
  1649. m.distinct(function(err, doc) {
  1650. assert.ifError(err);
  1651. assert.ok(doc);
  1652. done();
  1653. });
  1654. });
  1655. it('but not if no distinct arg was declared', function() {
  1656. const m = mquery();
  1657. m.collection(col);
  1658. assert.throws(function() {
  1659. m.distinct(function() {});
  1660. }, /No value for `distinct`/);
  1661. });
  1662. });
  1663. });
  1664. describe('validates its option', function() {
  1665. it('sort', function(done) {
  1666. assert.throws(function() {
  1667. mquery().sort('x').distinct();
  1668. }, /sort cannot be used with distinct/);
  1669. done();
  1670. });
  1671. it('select', function(done) {
  1672. assert.throws(function() {
  1673. mquery().select('x').distinct();
  1674. }, /field selection and slice cannot be used with distinct/);
  1675. done();
  1676. });
  1677. it('slice', function(done) {
  1678. assert.throws(function() {
  1679. mquery().where('x').slice(-3).distinct();
  1680. }, /field selection and slice cannot be used with distinct/);
  1681. done();
  1682. });
  1683. it('limit', function(done) {
  1684. assert.throws(function() {
  1685. mquery().limit(3).distinct();
  1686. }, /limit cannot be used with distinct/);
  1687. done();
  1688. });
  1689. it('skip', function(done) {
  1690. assert.throws(function() {
  1691. mquery().skip(3).distinct();
  1692. }, /skip cannot be used with distinct/);
  1693. done();
  1694. });
  1695. it('batchSize', function(done) {
  1696. assert.throws(function() {
  1697. mquery({}, { batchSize: 3 }).distinct();
  1698. }, /batchSize cannot be used with distinct/);
  1699. done();
  1700. });
  1701. it('comment', function(done) {
  1702. assert.throws(function() {
  1703. mquery().comment('mquery').distinct();
  1704. }, /comment cannot be used with distinct/);
  1705. done();
  1706. });
  1707. it('maxScan', function(done) {
  1708. assert.throws(function() {
  1709. mquery().maxScan(300).distinct();
  1710. }, /maxScan cannot be used with distinct/);
  1711. done();
  1712. });
  1713. it('snapshot', function(done) {
  1714. assert.throws(function() {
  1715. mquery().snapshot().distinct();
  1716. }, /snapshot cannot be used with distinct/);
  1717. done();
  1718. });
  1719. it('hint', function(done) {
  1720. assert.throws(function() {
  1721. mquery().hint({ x: 1 }).distinct();
  1722. }, /hint cannot be used with distinct/);
  1723. done();
  1724. });
  1725. it('tailable', function(done) {
  1726. assert.throws(function() {
  1727. mquery().tailable().distinct();
  1728. }, /tailable cannot be used with distinct/);
  1729. done();
  1730. });
  1731. });
  1732. });
  1733. describe('update', function() {
  1734. describe('with no callback', function() {
  1735. it('does not execute', function() {
  1736. const m = mquery();
  1737. assert.doesNotThrow(function() {
  1738. m.updateOne({ name: 'old' }, { name: 'updated' }, { multi: true });
  1739. });
  1740. assert.doesNotThrow(function() {
  1741. m.updateOne({ name: 'old' }, { name: 'updated' });
  1742. });
  1743. assert.doesNotThrow(function() {
  1744. m.updateOne({ name: 'updated' });
  1745. });
  1746. assert.doesNotThrow(function() {
  1747. m.updateOne();
  1748. });
  1749. });
  1750. });
  1751. it('is chainable', function() {
  1752. const m = mquery({ x: 1 }).updateOne({ y: 2 });
  1753. const n = m.where({ y: 2 });
  1754. assert.equal(m, n);
  1755. assert.deepEqual(n._conditions, { x: 1, y: 2 });
  1756. assert.deepEqual({ y: 2 }, n._update);
  1757. assert.equal('updateOne', n.op);
  1758. });
  1759. it('merges update doc arg', function() {
  1760. const a = [1, 2];
  1761. const m = mquery().where({ name: 'mquery' }).updateOne({ x: 'stuff', a: a });
  1762. m.updateOne({ z: 'stuff' });
  1763. assert.deepEqual(m._update, { z: 'stuff', x: 'stuff', a: a });
  1764. assert.deepEqual(m._conditions, { name: 'mquery' });
  1765. assert.ok(!m.options.overwrite);
  1766. m.updateOne({}, { z: 'renamed' }, { overwrite: true });
  1767. assert.ok(m.options.overwrite === true);
  1768. assert.deepEqual(m._conditions, { name: 'mquery' });
  1769. assert.deepEqual(m._update, { z: 'renamed', x: 'stuff', a: a });
  1770. a.push(3);
  1771. assert.notDeepEqual(m._update, { z: 'renamed', x: 'stuff', a: a });
  1772. });
  1773. describe('executes', function() {
  1774. let id;
  1775. before(function(done) {
  1776. col.insertOne({ name: 'mquery update', age: 1 }, function(err, res) {
  1777. id = res.insertedId;
  1778. done();
  1779. });
  1780. });
  1781. after(function(done) {
  1782. col.remove({ _id: id }, done);
  1783. });
  1784. describe('when conds + doc + opts + callback passed', function() {
  1785. it('works', function(done) {
  1786. const m = mquery(col).where({ _id: id });
  1787. m.updateOne({}, { name: 'Sparky' }, {}, function(err, res) {
  1788. assert.ifError(err);
  1789. assert.equal(res.modifiedCount, 1);
  1790. m.findOne(function(err, doc) {
  1791. assert.ifError(err);
  1792. assert.equal(doc.name, 'Sparky');
  1793. done();
  1794. });
  1795. });
  1796. });
  1797. });
  1798. describe('when conds + doc + callback passed', function() {
  1799. it('works', function(done) {
  1800. const m = mquery(col).updateOne({ _id: id }, { name: 'fairgrounds' }, 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, 'fairgrounds');
  1806. done();
  1807. });
  1808. });
  1809. });
  1810. });
  1811. describe('when doc + callback passed', function() {
  1812. it('works', function(done) {
  1813. const m = mquery(col).where({ _id: id }).updateOne({ name: 'changed' }, function(err, num) {
  1814. assert.ifError(err);
  1815. assert.ok(1, num);
  1816. m.findOne(function(err, doc) {
  1817. assert.ifError(err);
  1818. assert.equal(doc.name, 'changed');
  1819. done();
  1820. });
  1821. });
  1822. });
  1823. });
  1824. describe('when just callback passed', function() {
  1825. it('works', function(done) {
  1826. const m = mquery(col).where({ _id: id });
  1827. m.updateOne({ name: 'Frankenweenie' });
  1828. m.updateOne(function(err, res) {
  1829. assert.ifError(err);
  1830. assert.equal(res.modifiedCount, 1);
  1831. m.findOne(function(err, doc) {
  1832. assert.ifError(err);
  1833. assert.equal(doc.name, 'Frankenweenie');
  1834. done();
  1835. });
  1836. });
  1837. });
  1838. });
  1839. describe('without a callback', function() {
  1840. it('when forced by exec()', function(done) {
  1841. const m = mquery(col).where({ _id: id });
  1842. m.setOptions({ w: 'majority' });
  1843. m.updateOne({ name: 'forced' });
  1844. const update = m._collection.update;
  1845. m._collection.updateOne = function(conds, doc, opts) {
  1846. m._collection.update = update;
  1847. assert.equal(opts.w, 'majority');
  1848. assert.equal('forced', doc.$set.name);
  1849. done();
  1850. };
  1851. m.exec();
  1852. });
  1853. });
  1854. describe('except when update doc is empty and missing overwrite flag', function() {
  1855. it('works', function(done) {
  1856. const m = mquery(col).where({ _id: id });
  1857. m.updateOne({}, function(err, num) {
  1858. assert.ifError(err);
  1859. assert.ok(0 === num);
  1860. setTimeout(function() {
  1861. m.findOne(function(err, doc) {
  1862. assert.ifError(err);
  1863. assert.equal(3, mquery.utils.keys(doc).length);
  1864. assert.equal(id, doc._id.toString());
  1865. assert.equal('Frankenweenie', doc.name);
  1866. done();
  1867. });
  1868. }, 300);
  1869. });
  1870. });
  1871. });
  1872. describe('when boolean (true) - exec()', function() {
  1873. it('works', function(done) {
  1874. const m = mquery(col).where({ _id: id });
  1875. m.updateOne({ name: 'bool' }).updateOne(true);
  1876. setTimeout(function() {
  1877. m.findOne(function(err, doc) {
  1878. assert.ifError(err);
  1879. assert.ok(doc);
  1880. assert.equal('bool', doc.name);
  1881. done();
  1882. });
  1883. }, 300);
  1884. });
  1885. });
  1886. });
  1887. });
  1888. describe('remove', function() {
  1889. describe('with 0 args', function() {
  1890. const name = 'remove: no args test';
  1891. before(function(done) {
  1892. col.insertOne({ name: name }, done);
  1893. });
  1894. after(function(done) {
  1895. col.remove({ name: name }, done);
  1896. });
  1897. it('does not execute', function(done) {
  1898. const remove = col.remove;
  1899. col.remove = function() {
  1900. col.remove = remove;
  1901. done(new Error('remove executed!'));
  1902. };
  1903. mquery(col).where({ name: name }).remove();
  1904. setTimeout(function() {
  1905. col.remove = remove;
  1906. done();
  1907. }, 10);
  1908. });
  1909. it('chains', function() {
  1910. const m = mquery();
  1911. assert.equal(m, m.remove());
  1912. });
  1913. });
  1914. describe('with 1 argument', function() {
  1915. const name = 'remove: 1 arg test';
  1916. before(function(done) {
  1917. col.insertOne({ name: name }, done);
  1918. });
  1919. after(function(done) {
  1920. col.remove({ name: name }, done);
  1921. });
  1922. describe('that is a', function() {
  1923. it('plain object', function() {
  1924. const m = mquery(col).remove({ name: 'Whiskers' });
  1925. m.remove({ color: '#fff' });
  1926. assert.deepEqual({ name: 'Whiskers', color: '#fff' }, m._conditions);
  1927. });
  1928. it('query', function() {
  1929. const q = mquery({ color: '#fff' });
  1930. const m = mquery(col).remove({ name: 'Whiskers' });
  1931. m.remove(q);
  1932. assert.deepEqual({ name: 'Whiskers', color: '#fff' }, m._conditions);
  1933. });
  1934. it('function', function(done) {
  1935. mquery(col).where({ name: name }).remove(function(err) {
  1936. assert.ifError(err);
  1937. mquery(col).findOne({ name: name }, function(err, doc) {
  1938. assert.ifError(err);
  1939. assert.equal(null, doc);
  1940. done();
  1941. });
  1942. });
  1943. });
  1944. it('boolean (true) - execute', function(done) {
  1945. col.insertOne({ name: name }, function(err) {
  1946. assert.ifError(err);
  1947. mquery(col).findOne({ name: name }, function(err, doc) {
  1948. assert.ifError(err);
  1949. assert.ok(doc);
  1950. mquery(col).remove(true);
  1951. setTimeout(function() {
  1952. mquery(col).find(function(err, docs) {
  1953. assert.ifError(err);
  1954. assert.ok(docs);
  1955. assert.equal(0, docs.length);
  1956. done();
  1957. });
  1958. }, 300);
  1959. });
  1960. });
  1961. });
  1962. });
  1963. });
  1964. describe('with 2 arguments', function() {
  1965. const name = 'remove: 2 arg test';
  1966. beforeEach(function(done) {
  1967. col.remove({}, function(err) {
  1968. assert.ifError(err);
  1969. col.insertMany([{ name: 'shelly' }, { name: name }], function(err) {
  1970. assert.ifError(err);
  1971. mquery(col).find(function(err, docs) {
  1972. assert.ifError(err);
  1973. assert.equal(2, docs.length);
  1974. done();
  1975. });
  1976. });
  1977. });
  1978. });
  1979. describe('plain object + callback', function() {
  1980. it('works', function(done) {
  1981. mquery(col).remove({ name: name }, function(err) {
  1982. assert.ifError(err);
  1983. mquery(col).find(function(err, docs) {
  1984. assert.ifError(err);
  1985. assert.ok(docs);
  1986. assert.equal(1, docs.length);
  1987. assert.equal('shelly', docs[0].name);
  1988. done();
  1989. });
  1990. });
  1991. });
  1992. });
  1993. describe('mquery + callback', function() {
  1994. it('works', function(done) {
  1995. const m = mquery({ name: name });
  1996. mquery(col).remove(m, function(err) {
  1997. assert.ifError(err);
  1998. mquery(col).find(function(err, docs) {
  1999. assert.ifError(err);
  2000. assert.ok(docs);
  2001. assert.equal(1, docs.length);
  2002. assert.equal('shelly', docs[0].name);
  2003. done();
  2004. });
  2005. });
  2006. });
  2007. });
  2008. });
  2009. });
  2010. function validateFindAndModifyOptions(method) {
  2011. describe('validates its option', function() {
  2012. it('sort', function(done) {
  2013. assert.doesNotThrow(function() {
  2014. mquery().sort('x')[method]();
  2015. });
  2016. done();
  2017. });
  2018. it('select', function(done) {
  2019. assert.doesNotThrow(function() {
  2020. mquery().select('x')[method]();
  2021. });
  2022. done();
  2023. });
  2024. it('limit', function(done) {
  2025. assert.throws(function() {
  2026. mquery().limit(3)[method]();
  2027. }, new RegExp('limit cannot be used with ' + method));
  2028. done();
  2029. });
  2030. it('skip', function(done) {
  2031. assert.throws(function() {
  2032. mquery().skip(3)[method]();
  2033. }, new RegExp('skip cannot be used with ' + method));
  2034. done();
  2035. });
  2036. it('batchSize', function(done) {
  2037. assert.throws(function() {
  2038. mquery({}, { batchSize: 3 })[method]();
  2039. }, new RegExp('batchSize cannot be used with ' + method));
  2040. done();
  2041. });
  2042. it('maxScan', function(done) {
  2043. assert.throws(function() {
  2044. mquery().maxScan(300)[method]();
  2045. }, new RegExp('maxScan cannot be used with ' + method));
  2046. done();
  2047. });
  2048. it('snapshot', function(done) {
  2049. assert.throws(function() {
  2050. mquery().snapshot()[method]();
  2051. }, new RegExp('snapshot cannot be used with ' + method));
  2052. done();
  2053. });
  2054. it('hint', function(done) {
  2055. assert.throws(function() {
  2056. mquery().hint({ x: 1 })[method]();
  2057. }, new RegExp('hint cannot be used with ' + method));
  2058. done();
  2059. });
  2060. it('tailable', function(done) {
  2061. assert.throws(function() {
  2062. mquery().tailable()[method]();
  2063. }, new RegExp('tailable cannot be used with ' + method));
  2064. done();
  2065. });
  2066. it('comment', function(done) {
  2067. assert.throws(function() {
  2068. mquery().comment('mquery')[method]();
  2069. }, new RegExp('comment cannot be used with ' + method));
  2070. done();
  2071. });
  2072. });
  2073. }
  2074. describe('findOneAndUpdate', function() {
  2075. let name = 'findOneAndUpdate + fn';
  2076. validateFindAndModifyOptions('findOneAndUpdate');
  2077. describe('with 0 args', function() {
  2078. it('makes no changes', function() {
  2079. const m = mquery();
  2080. const n = m.findOneAndUpdate();
  2081. assert.deepEqual(m, n);
  2082. });
  2083. });
  2084. describe('with 1 arg', function() {
  2085. describe('that is an object', function() {
  2086. it('updates the doc', function() {
  2087. const m = mquery();
  2088. const n = m.findOneAndUpdate({ $set: { name: '1 arg' } });
  2089. assert.deepEqual(n._update, { $set: { name: '1 arg' } });
  2090. });
  2091. });
  2092. describe('that is a query', function() {
  2093. it('updates the doc', function() {
  2094. const m = mquery({ name: name }).updateOne({ x: 1 });
  2095. const n = mquery().findOneAndUpdate(m);
  2096. assert.deepEqual(n._update, { x: 1 });
  2097. });
  2098. });
  2099. it('that is a function', function(done) {
  2100. col.insertOne({ name: name }, function(err) {
  2101. assert.ifError(err);
  2102. const m = mquery({ name: name }).collection(col);
  2103. name = '1 arg';
  2104. const n = m.updateOne({ $set: { name: name } }).setOptions({ returnDocument: 'after' });
  2105. n.findOneAndUpdate(function(err, res) {
  2106. assert.ifError(err);
  2107. assert.ok(res.value);
  2108. assert.equal(res.value.name, name);
  2109. done();
  2110. });
  2111. });
  2112. });
  2113. });
  2114. describe('with 2 args', function() {
  2115. it('conditions + update', function() {
  2116. const m = mquery(col);
  2117. m.findOneAndUpdate({ name: name }, { age: 100 });
  2118. assert.deepEqual({ name: name }, m._conditions);
  2119. assert.deepEqual({ age: 100 }, m._update);
  2120. });
  2121. it('query + update', function() {
  2122. const n = mquery({ name: name });
  2123. const m = mquery(col);
  2124. m.findOneAndUpdate(n, { age: 100 });
  2125. assert.deepEqual({ name: name }, m._conditions);
  2126. assert.deepEqual({ age: 100 }, m._update);
  2127. });
  2128. it('update + callback', function(done) {
  2129. const m = mquery(col).where({ name: name });
  2130. m.findOneAndUpdate({}, { $inc: { age: 10 } }, { returnDocument: 'after' }, function(err, res) {
  2131. assert.ifError(err);
  2132. assert.equal(10, res.value.age);
  2133. done();
  2134. });
  2135. });
  2136. });
  2137. describe('with 3 args', function() {
  2138. it('conditions + update + options', function() {
  2139. const m = mquery();
  2140. const n = m.findOneAndUpdate({ name: name }, { works: true }, { returnDocument: 'before' });
  2141. assert.deepEqual({ name: name }, n._conditions);
  2142. assert.deepEqual({ works: true }, n._update);
  2143. assert.deepEqual({ returnDocument: 'before' }, n.options);
  2144. });
  2145. it('conditions + update + callback', function(done) {
  2146. const m = mquery(col);
  2147. m.findOneAndUpdate({ name: name }, { works: true }, { returnDocument: 'after' }, function(err, res) {
  2148. assert.ifError(err);
  2149. assert.ok(res.value);
  2150. assert.equal(name, res.value.name);
  2151. assert.ok(true === res.value.works);
  2152. done();
  2153. });
  2154. });
  2155. });
  2156. describe('with 4 args', function() {
  2157. it('conditions + update + options + callback', function(done) {
  2158. const m = mquery(col);
  2159. m.findOneAndUpdate({ name: name }, { works: false }, {}, function(err, res) {
  2160. assert.ifError(err);
  2161. assert.ok(res.value);
  2162. assert.equal(name, res.value.name);
  2163. assert.ok(true === res.value.works);
  2164. done();
  2165. });
  2166. });
  2167. });
  2168. });
  2169. describe('findOneAndRemove', function() {
  2170. let name = 'findOneAndRemove';
  2171. validateFindAndModifyOptions('findOneAndRemove');
  2172. describe('with 0 args', function() {
  2173. it('makes no changes', function() {
  2174. const m = mquery();
  2175. const n = m.findOneAndRemove();
  2176. assert.deepEqual(m, n);
  2177. });
  2178. });
  2179. describe('with 1 arg', function() {
  2180. describe('that is an object', function() {
  2181. it('updates the doc', function() {
  2182. const m = mquery();
  2183. const n = m.findOneAndRemove({ name: '1 arg' });
  2184. assert.deepEqual(n._conditions, { name: '1 arg' });
  2185. });
  2186. });
  2187. describe('that is a query', function() {
  2188. it('updates the doc', function() {
  2189. const m = mquery({ name: name });
  2190. const n = m.findOneAndRemove(m);
  2191. assert.deepEqual(n._conditions, { name: name });
  2192. });
  2193. });
  2194. it('that is a function', function(done) {
  2195. col.insertOne({ name: name }, function(err) {
  2196. assert.ifError(err);
  2197. const m = mquery({ name: name }).collection(col);
  2198. m.findOneAndRemove(function(err, res) {
  2199. assert.ifError(err);
  2200. assert.ok(res.value);
  2201. assert.equal(name, res.value.name);
  2202. done();
  2203. });
  2204. });
  2205. });
  2206. });
  2207. describe('with 2 args', function() {
  2208. it('conditions + options', function() {
  2209. const m = mquery(col);
  2210. m.findOneAndRemove({ name: name }, { returnDocument: 'after' });
  2211. assert.deepEqual({ name: name }, m._conditions);
  2212. assert.deepEqual({ returnDocument: 'after' }, m.options);
  2213. });
  2214. it('query + options', function() {
  2215. const n = mquery({ name: name });
  2216. const m = mquery(col);
  2217. m.findOneAndRemove(n, { sort: { x: 1 } });
  2218. assert.deepEqual({ name: name }, m._conditions);
  2219. assert.deepEqual({ sort: { x: 1 } }, m.options);
  2220. });
  2221. it('conditions + callback', function(done) {
  2222. col.insertOne({ name: name }, function(err) {
  2223. assert.ifError(err);
  2224. const m = mquery(col);
  2225. m.findOneAndRemove({ name: name }, function(err, res) {
  2226. assert.ifError(err);
  2227. assert.equal(name, res.value.name);
  2228. done();
  2229. });
  2230. });
  2231. });
  2232. it('query + callback', function(done) {
  2233. col.insertOne({ name: name }, function(err) {
  2234. assert.ifError(err);
  2235. const n = mquery({ name: name });
  2236. const m = mquery(col);
  2237. m.findOneAndRemove(n, function(err, res) {
  2238. assert.ifError(err);
  2239. assert.equal(name, res.value.name);
  2240. done();
  2241. });
  2242. });
  2243. });
  2244. });
  2245. describe('with 3 args', function() {
  2246. it('conditions + options + callback', function(done) {
  2247. name = 'findOneAndRemove + conds + options + cb';
  2248. col.insertMany([{ name: name }, { name: 'a' }], function(err) {
  2249. assert.ifError(err);
  2250. const m = mquery(col);
  2251. m.findOneAndRemove({ name: name }, { sort: { name: 1 } }, function(err, res) {
  2252. assert.ifError(err);
  2253. assert.ok(res.value);
  2254. assert.equal(name, res.value.name);
  2255. done();
  2256. });
  2257. });
  2258. });
  2259. });
  2260. });
  2261. describe('exec', function() {
  2262. beforeEach(function(done) {
  2263. col.insertMany([{ name: 'exec', age: 1 }, { name: 'exec', age: 2 }], done);
  2264. });
  2265. afterEach(function(done) {
  2266. mquery(col).remove(done);
  2267. });
  2268. it('requires an op', function() {
  2269. assert.throws(function() {
  2270. mquery().exec();
  2271. }, /Missing query type/);
  2272. });
  2273. describe('find', function() {
  2274. it('works', function(done) {
  2275. const m = mquery(col).find({ name: 'exec' });
  2276. m.exec(function(err, docs) {
  2277. assert.ifError(err);
  2278. assert.equal(2, docs.length);
  2279. done();
  2280. });
  2281. });
  2282. it('works with readPreferences', function(done) {
  2283. const m = mquery(col).find({ name: 'exec' });
  2284. try {
  2285. const ReadPreference = require('mongodb').ReadPreference;
  2286. const rp = new ReadPreference('primary');
  2287. m.read(rp);
  2288. } catch (e) {
  2289. done(e.code === 'MODULE_NOT_FOUND' ? null : e);
  2290. return;
  2291. }
  2292. m.exec(function(err, docs) {
  2293. assert.ifError(err);
  2294. assert.equal(2, docs.length);
  2295. done();
  2296. });
  2297. });
  2298. it('works with hint', function(done) {
  2299. mquery(col).hint({ _id: 1 }).find({ name: 'exec' }).exec(function(err, docs) {
  2300. assert.ifError(err);
  2301. assert.equal(2, docs.length);
  2302. mquery(col).hint('_id_').find({ age: 1 }).exec(function(err, docs) {
  2303. assert.ifError(err);
  2304. assert.equal(1, docs.length);
  2305. done();
  2306. });
  2307. });
  2308. });
  2309. it('works with readConcern', function(done) {
  2310. const m = mquery(col).find({ name: 'exec' });
  2311. m.readConcern('l');
  2312. m.exec(function(err, docs) {
  2313. assert.ifError(err);
  2314. assert.equal(2, docs.length);
  2315. done();
  2316. });
  2317. });
  2318. it('works with collation', function(done) {
  2319. const m = mquery(col).find({ name: 'EXEC' });
  2320. m.collation({ locale: 'en_US', strength: 1 });
  2321. m.exec(function(err, docs) {
  2322. assert.ifError(err);
  2323. assert.equal(2, docs.length);
  2324. done();
  2325. });
  2326. });
  2327. });
  2328. it('findOne', function(done) {
  2329. const m = mquery(col).findOne({ age: 2 });
  2330. m.exec(function(err, doc) {
  2331. assert.ifError(err);
  2332. assert.equal(2, doc.age);
  2333. done();
  2334. });
  2335. });
  2336. it('count', function(done) {
  2337. const m = mquery(col).count({ name: 'exec' });
  2338. m.exec(function(err, count) {
  2339. assert.ifError(err);
  2340. assert.equal(2, count);
  2341. done();
  2342. });
  2343. });
  2344. it('distinct', function(done) {
  2345. const m = mquery({ name: 'exec' });
  2346. m.collection(col);
  2347. m.distinct('age');
  2348. m.exec(function(err, array) {
  2349. assert.ifError(err);
  2350. assert.ok(Array.isArray(array));
  2351. assert.equal(2, array.length);
  2352. assert(~array.indexOf(1));
  2353. assert(~array.indexOf(2));
  2354. done();
  2355. });
  2356. });
  2357. describe('update', function() {
  2358. describe('updateMany', function() {
  2359. it('works', function(done) {
  2360. mquery(col).updateMany({ 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, 2);
  2366. done();
  2367. });
  2368. });
  2369. });
  2370. it('works with write concern', function(done) {
  2371. mquery(col).updateMany({ name: 'exec' }, { name: 'test' })
  2372. .w(1).j(true).wtimeout(1000)
  2373. .exec(function(error) {
  2374. assert.ifError(error);
  2375. mquery(col).count({ name: 'test' }).exec(function(error, res) {
  2376. assert.ifError(error);
  2377. assert.equal(res, 2);
  2378. done();
  2379. });
  2380. });
  2381. });
  2382. });
  2383. describe('updateOne', function() {
  2384. it('works', function(done) {
  2385. mquery(col).updateOne({ name: 'exec' }, { name: 'test' }).
  2386. exec(function(error) {
  2387. assert.ifError(error);
  2388. mquery(col).count({ name: 'test' }).exec(function(error, res) {
  2389. assert.ifError(error);
  2390. assert.equal(res, 1);
  2391. done();
  2392. });
  2393. });
  2394. });
  2395. });
  2396. describe('replaceOne', function() {
  2397. it('works', function(done) {
  2398. mquery(col).replaceOne({ name: 'exec' }, { name: 'test' }).
  2399. exec(function(error) {
  2400. assert.ifError(error);
  2401. mquery(col).findOne({ name: 'test' }).exec(function(error, res) {
  2402. assert.ifError(error);
  2403. assert.equal(res.name, 'test');
  2404. assert.ok(res.age == null);
  2405. done();
  2406. });
  2407. });
  2408. });
  2409. });
  2410. });
  2411. describe('remove', function() {
  2412. it('with a callback', function(done) {
  2413. const m = mquery(col).where({ age: 2 }).remove();
  2414. m.exec(function(err, res) {
  2415. assert.ifError(err);
  2416. assert.equal(1, res.deletedCount);
  2417. done();
  2418. });
  2419. });
  2420. it('without a callback', function(done) {
  2421. const m = mquery(col).where({ age: 1 }).remove();
  2422. m.exec();
  2423. setTimeout(function() {
  2424. mquery(col).where('name', 'exec').count(function(err, num) {
  2425. assert.equal(1, num);
  2426. done();
  2427. });
  2428. }, 200);
  2429. });
  2430. });
  2431. describe('deleteOne', function() {
  2432. it('with a callback', function(done) {
  2433. const m = mquery(col).where({ age: { $gte: 0 } }).deleteOne();
  2434. m.exec(function(err, res) {
  2435. assert.ifError(err);
  2436. assert.equal(res.deletedCount, 1);
  2437. done();
  2438. });
  2439. });
  2440. it('with justOne set', function(done) {
  2441. const m = mquery(col).where({ age: { $gte: 0 } }).
  2442. // Should ignore `justOne`
  2443. setOptions({ justOne: false }).
  2444. deleteOne();
  2445. m.exec(function(err, res) {
  2446. assert.ifError(err);
  2447. assert.equal(res.deletedCount, 1);
  2448. done();
  2449. });
  2450. });
  2451. });
  2452. describe('deleteMany', function() {
  2453. it('with a callback', function(done) {
  2454. const m = mquery(col).where({ age: { $gte: 0 } }).deleteMany();
  2455. m.exec(function(err, res) {
  2456. assert.ifError(err);
  2457. assert.equal(res.deletedCount, 2);
  2458. done();
  2459. });
  2460. });
  2461. });
  2462. describe('findOneAndUpdate', function() {
  2463. it('with a callback', function(done) {
  2464. const m = mquery(col);
  2465. m.findOneAndUpdate({ name: 'exec', age: 1 }, { $set: { name: 'findOneAndUpdate' } }, { returnDocument: 'after' });
  2466. m.exec(function(err, res) {
  2467. assert.ifError(err);
  2468. assert.equal('findOneAndUpdate', res.value.name);
  2469. done();
  2470. });
  2471. });
  2472. });
  2473. describe('findOneAndRemove', function() {
  2474. it('with a callback', function(done) {
  2475. const m = mquery(col);
  2476. m.findOneAndRemove({ name: 'exec', age: 2 });
  2477. m.exec(function(err, res) {
  2478. assert.ifError(err);
  2479. assert.equal('exec', res.value.name);
  2480. assert.equal(2, res.value.age);
  2481. mquery(col).count({ name: 'exec' }, function(err, num) {
  2482. assert.ifError(err);
  2483. assert.equal(1, num);
  2484. done();
  2485. });
  2486. });
  2487. });
  2488. });
  2489. });
  2490. describe('setTraceFunction', function() {
  2491. beforeEach(function(done) {
  2492. col.insertMany([{ name: 'trace', age: 93 }], done);
  2493. });
  2494. it('calls trace function when executing query', function(done) {
  2495. const m = mquery(col);
  2496. let resultTraceCalled;
  2497. m.setTraceFunction(function(method, queryInfo) {
  2498. try {
  2499. assert.equal('findOne', method);
  2500. assert.equal('trace', queryInfo.conditions.name);
  2501. } catch (e) {
  2502. done(e);
  2503. }
  2504. return function(err, result, millis) {
  2505. try {
  2506. assert.equal(93, result.age);
  2507. assert.ok(typeof millis === 'number');
  2508. } catch (e) {
  2509. done(e);
  2510. }
  2511. resultTraceCalled = true;
  2512. };
  2513. });
  2514. m.findOne({ name: 'trace' }, function(err, doc) {
  2515. assert.ifError(err);
  2516. assert.equal(resultTraceCalled, true);
  2517. assert.equal(93, doc.age);
  2518. done();
  2519. });
  2520. });
  2521. it('inherits trace function when calling toConstructor', function(done) {
  2522. function traceFunction() { return function() {}; }
  2523. const tracedQuery = mquery().setTraceFunction(traceFunction).toConstructor();
  2524. const query = tracedQuery();
  2525. assert.equal(traceFunction, query._traceFunction);
  2526. done();
  2527. });
  2528. });
  2529. describe('thunk', function() {
  2530. it('returns a function', function(done) {
  2531. assert.equal('function', typeof mquery().thunk());
  2532. done();
  2533. });
  2534. it('passes the fn arg to `exec`', function(done) {
  2535. function cb() {}
  2536. const m = mquery();
  2537. m.exec = function testing(fn) {
  2538. assert.equal(this, m);
  2539. assert.equal(cb, fn);
  2540. done();
  2541. };
  2542. m.thunk()(cb);
  2543. });
  2544. });
  2545. describe('then', function() {
  2546. before(function(done) {
  2547. col.insertMany([{ name: 'then', age: 1 }, { name: 'then', age: 2 }], done);
  2548. });
  2549. after(function(done) {
  2550. mquery(col).remove({ name: 'then' }).exec(done);
  2551. });
  2552. it('returns a promise A+ compat object', function(done) {
  2553. const m = mquery(col).find();
  2554. assert.equal('function', typeof m.then);
  2555. done();
  2556. });
  2557. it('creates a promise that is resolved on success', function(done) {
  2558. const promise = mquery(col).count({ name: 'then' }).then();
  2559. promise.then(function(count) {
  2560. assert.equal(2, count);
  2561. done();
  2562. }, done);
  2563. });
  2564. it('supports exec() cb being called synchronously #66', function(done) {
  2565. const query = mquery(col).count({ name: 'then' });
  2566. query.exec = function(cb) {
  2567. cb(null, 66);
  2568. };
  2569. query.then(success, done);
  2570. function success(count) {
  2571. assert.equal(66, count);
  2572. done();
  2573. }
  2574. });
  2575. });
  2576. describe('stream', function() {
  2577. before(function(done) {
  2578. col.insertMany([{ name: 'stream', age: 1 }, { name: 'stream', age: 2 }], done);
  2579. });
  2580. after(function(done) {
  2581. mquery(col).remove({ name: 'stream' }).exec(done);
  2582. });
  2583. describe('throws', function() {
  2584. describe('if used with non-find operations', function() {
  2585. const ops = ['update', 'findOneAndUpdate', 'remove', 'count', 'distinct'];
  2586. ops.forEach(function(op) {
  2587. assert.throws(function() {
  2588. mquery(col)[op]().stream();
  2589. });
  2590. });
  2591. });
  2592. });
  2593. it('returns a stream', function(done) {
  2594. const stream = mquery(col).find({ name: 'stream' }).cursor().stream();
  2595. let count = 0;
  2596. let err;
  2597. stream.on('data', function(doc) {
  2598. assert.equal('stream', doc.name);
  2599. ++count;
  2600. });
  2601. stream.on('error', function(er) {
  2602. err = er;
  2603. });
  2604. stream.on('end', function() {
  2605. if (err) return done(err);
  2606. assert.equal(2, count);
  2607. done();
  2608. });
  2609. });
  2610. });
  2611. function noDistinct(type) {
  2612. it('cannot be used with distinct()', function(done) {
  2613. assert.throws(function() {
  2614. mquery().distinct('name')[type](4);
  2615. }, new RegExp(type + ' cannot be used with distinct'));
  2616. done();
  2617. });
  2618. }
  2619. function no(method, type) {
  2620. it('cannot be used with ' + method + '()', function(done) {
  2621. assert.throws(function() {
  2622. mquery()[method]()[type](4);
  2623. }, new RegExp(type + ' cannot be used with ' + method));
  2624. done();
  2625. });
  2626. }
  2627. // query internal
  2628. describe('_updateForExec', function() {
  2629. it('returns a clone of the update object with same key order #19', function(done) {
  2630. const update = {};
  2631. update.$push = { n: { $each: [{ x: 10 }], $slice: -1, $sort: { x: 1 } } };
  2632. const q = mquery().updateOne({ x: 1 }, update);
  2633. // capture original key order
  2634. const order = [];
  2635. let key;
  2636. for (key in q._update.$push.n) {
  2637. order.push(key);
  2638. }
  2639. // compare output
  2640. const doc = q._updateForExec();
  2641. let i = 0;
  2642. for (key in doc.$push.n) {
  2643. assert.equal(key, order[i]);
  2644. i++;
  2645. }
  2646. done();
  2647. });
  2648. });
  2649. });