ObjectExpression_test.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. "use strict";
  2. var assert = require("assert"),
  3. ObjectExpression = require("../../../../lib/pipeline/expressions/ObjectExpression"),
  4. Expression = require("../../../../lib/pipeline/expressions/Expression"),
  5. ConstantExpression = require("../../../../lib/pipeline/expressions/ConstantExpression"),
  6. FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression"),
  7. AndExpression = require("../../../../lib/pipeline/expressions/AndExpression"),
  8. Variables = require("../../../../lib/pipeline/expressions/Variables"),
  9. DepsTracker = require("../../../../lib/pipeline/DepsTracker"),
  10. utils = require("./utils");
  11. // Mocha one-liner to make these tests self-hosted
  12. if(!module.parent)return(require.cache[__filename]=null,(new(require("mocha"))({ui:"exports",reporter:"spec",grep:process.env.TEST_GREP})).addFile(__filename).run(process.exit));
  13. var constify = utils.constify;
  14. //SKIPPED: assertBinaryEqual
  15. //SKIPPED: toJson
  16. function expressionToJson(expr) {
  17. return expr.serialize(false);
  18. }
  19. //SKIPPED: fromJson
  20. //SKIPEPD: valueFromBson
  21. function assertDependencies(expectedDependencies, expression, includePath) {
  22. if (includePath === undefined) includePath = true;
  23. var path = [],
  24. dependencies = new DepsTracker();
  25. expression.addDependencies(dependencies, includePath ? path : undefined);
  26. var bab = Object.keys(dependencies.fields);
  27. assert.deepEqual(bab.sort(), expectedDependencies.sort());
  28. assert.strictEqual(dependencies.needWholeDocument, false);
  29. assert.strictEqual(dependencies.needTextScore, false);
  30. }
  31. /// An assertion for `ObjectExpression` instances based on Mongo's `ExpectedResultBase` class
  32. function assertExpectedResult(args) {
  33. {// check for required args
  34. if (args === undefined) throw new TypeError("missing arg: `args` is required");
  35. if (!("expected" in args)) throw new Error("missing arg: `args.expected` is required");
  36. if (!("expectedDependencies" in args)) throw new Error("missing arg: `args.expectedDependencies` is required");
  37. if (!("expectedJsonRepresentation" in args)) throw new Error("missing arg: `args.expectedJsonRepresentation` is required");
  38. }
  39. {// base args if none provided
  40. if (args.source === undefined) args.source = {_id:0, a:1, b:2};
  41. if (args.expectedIsSimple === undefined) args.expectedIsSimple = true;
  42. if (args.expression === undefined) args.expression = ObjectExpression.createRoot(); //NOTE: replaces prepareExpression + _expression assignment
  43. }
  44. // run implementation
  45. var doc = args.source,
  46. result = {},
  47. vars = new Variables(0, doc);
  48. args.expression.addToDocument(result, doc, vars);
  49. assert.deepEqual(result, args.expected);
  50. assertDependencies(args.expectedDependencies, args.expression);
  51. assert.deepEqual(expressionToJson(args.expression), args.expectedJsonRepresentation);
  52. assert.deepEqual(args.expression.isSimple(), args.expectedIsSimple);
  53. }
  54. exports.ObjectExpression = {
  55. "constructor()": {
  56. "should return instance if given arg": function() {
  57. assert(new ObjectExpression(false) instanceof Expression);
  58. assert(new ObjectExpression(true) instanceof Expression);
  59. },
  60. "should throw Error when constructing without args": function() {
  61. assert.throws(function() {
  62. new ObjectExpression();
  63. });
  64. },
  65. },
  66. "#addDependencies": {
  67. "should be able to get dependencies for non-inclusion expressions": function testNonInclusionDependencies() {
  68. /** Dependencies for non inclusion expressions. */
  69. var expr = ObjectExpression.createRoot();
  70. expr.addField("a", ConstantExpression.create(5));
  71. assertDependencies(["_id"], expr, true);
  72. assertDependencies([], expr, false);
  73. expr.addField("b", FieldPathExpression.create("c.d"));
  74. assertDependencies(["_id", "c.d"], expr, true);
  75. assertDependencies(["c.d"], expr, false);
  76. },
  77. "should be able to get dependencies for inclusion expressions": function testInclusionDependencies() {
  78. /** Dependencies for inclusion expressions. */
  79. var expr = ObjectExpression.createRoot();
  80. expr.includePath("a");
  81. assertDependencies(["_id", "a"], expr, true);
  82. var unused = new DepsTracker();
  83. assert.throws(function() {
  84. expr.addDependencies(unused);
  85. }, Error);
  86. },
  87. },
  88. "#serialize": {
  89. "should be able to convert to JSON representation and have constants represented by expressions": function testJson() {
  90. /** Serialize to a BSONObj, with constants represented by expressions. */
  91. var expr = ObjectExpression.createRoot();
  92. expr.addField("foo.a", ConstantExpression.create(5));
  93. assert.deepEqual({foo:{a:{$const:5}}}, expr.serialize());
  94. },
  95. },
  96. "#optimize": {
  97. "should be able to optimize expression and sub-expressions": function testOptimize() {
  98. /** Optimizing an object expression optimizes its sub expressions. */
  99. var expr = ObjectExpression.createRoot();
  100. // Add inclusion.
  101. expr.includePath("a");
  102. // Add non inclusion.
  103. var andExpr = new AndExpression();
  104. expr.addField("b", andExpr);
  105. expr.optimize();
  106. // Optimizing 'expression' optimizes its non inclusion sub expressions, while
  107. // inclusion sub expressions are passed through.
  108. assert.deepEqual({a:true, b:{$const:true}}, expressionToJson(expr));
  109. },
  110. },
  111. "#evaluate()": {
  112. "should be able to provide an empty object": function testEmpty() {
  113. /** Empty object spec. */
  114. var expr = ObjectExpression.createRoot();
  115. assertExpectedResult({
  116. expression: expr,
  117. expected: {_id:0},
  118. expectedDependencies: ["_id"],
  119. expectedJsonRepresentation: {}
  120. });
  121. },
  122. "should be able to include 'a' field only": function testInclude() {
  123. /** Include 'a' field only. */
  124. var expr = ObjectExpression.createRoot();
  125. expr.includePath("a");
  126. assertExpectedResult({
  127. expression: expr,
  128. expected: {_id:0, a:1},
  129. expectedDependencies: ["_id", "a"],
  130. expectedJsonRepresentation: {a:true}
  131. });
  132. },
  133. "should NOT be able to include missing 'a' field": function testMissingInclude() {
  134. /** Cannot include missing 'a' field. */
  135. var expr = ObjectExpression.createRoot();
  136. expr.includePath("a");
  137. assertExpectedResult({
  138. source: {_id:0, b:2},
  139. expression: expr,
  140. expected: {_id:0},
  141. expectedDependencies: ["_id", "a"],
  142. expectedJsonRepresentation: {a:true}
  143. });
  144. },
  145. "should be able to include '_id' field only": function testIncludeId() {
  146. /** Include '_id' field only. */
  147. var expr = ObjectExpression.createRoot();
  148. expr.includePath("_id");
  149. assertExpectedResult({
  150. expression: expr,
  151. expected: {_id:0},
  152. expectedDependencies: ["_id"],
  153. expectedJsonRepresentation: {_id:true}
  154. });
  155. },
  156. "should be able to exclude '_id' field": function testExcludeId() {
  157. /** Exclude '_id' field. */
  158. var expr = ObjectExpression.createRoot();
  159. expr.includePath("b");
  160. expr.excludeId = true;
  161. assertExpectedResult({
  162. expression: expr,
  163. expected: {b:2},
  164. expectedDependencies: ["b"],
  165. expectedJsonRepresentation: {_id:false, b:true}
  166. });
  167. },
  168. "should be able to include fields in source document order regardless of inclusion order": function testSourceOrder() {
  169. /** Result order based on source document field order, not inclusion spec field order. */
  170. var expr = ObjectExpression.createRoot();
  171. expr.includePath("b");
  172. expr.includePath("a");
  173. assertExpectedResult({
  174. expression: expr,
  175. get expected() { return this.source; },
  176. expectedDependencies: ["_id", "a", "b"],
  177. expectedJsonRepresentation: {b:true, a:true}
  178. });
  179. },
  180. "should be able to include a nested field": function testIncludeNested() {
  181. /** Include a nested field. */
  182. var expr = ObjectExpression.createRoot();
  183. expr.includePath("a.b");
  184. assertExpectedResult({
  185. expression: expr,
  186. expected: {_id:0, a:{b:5}},
  187. source: {_id:0, a:{b:5, c:6}, z:2},
  188. expectedDependencies: ["_id", "a.b"],
  189. expectedJsonRepresentation: {a:{b:true}}
  190. });
  191. },
  192. "should be able to include two nested fields": function testIncludeTwoNested() {
  193. /** Include two nested fields. */
  194. var expr = ObjectExpression.createRoot();
  195. expr.includePath("a.b");
  196. expr.includePath("a.c");
  197. assertExpectedResult({
  198. expression: expr,
  199. expected: {_id:0, a:{b:5, c:6}},
  200. source: {_id:0, a:{b:5,c:6}, z:2},
  201. expectedDependencies: ["_id", "a.b", "a.c"],
  202. expectedJsonRepresentation: {a:{b:true, c:true}}
  203. });
  204. },
  205. "should be able to include two fields nested within different parents": function testIncludeTwoParentNested() {
  206. /** Include two fields nested within different parents. */
  207. var expr = ObjectExpression.createRoot();
  208. expr.includePath("a.b");
  209. expr.includePath("c.d");
  210. assertExpectedResult({
  211. expression: expr,
  212. expected: {_id:0, a:{b:5}, c:{d:6}},
  213. source: {_id:0, a:{b:5}, c:{d:6}, z:2},
  214. expectedDependencies: ["_id", "a.b", "c.d"],
  215. expectedJsonRepresentation: {a:{b:true}, c:{d:true}}
  216. });
  217. },
  218. "should be able to attempt to include a missing nested field": function testIncludeMissingNested() {
  219. /** Attempt to include a missing nested field. */
  220. var expr = ObjectExpression.createRoot();
  221. expr.includePath("a.b");
  222. assertExpectedResult({
  223. expression: expr,
  224. expected: {_id:0, a:{}},
  225. source: {_id:0, a:{c:6}, z:2},
  226. expectedDependencies: ["_id", "a.b"],
  227. expectedJsonRepresentation: {a:{b:true}}
  228. });
  229. },
  230. "should be able to attempt to include a nested field within a non object": function testIncludeNestedWithinNonObject() {
  231. /** Attempt to include a nested field within a non object. */
  232. var expr = ObjectExpression.createRoot();
  233. expr.includePath("a.b");
  234. assertExpectedResult({
  235. expression: expr,
  236. expected: {_id:0},
  237. source: {_id:0, a:2, z:2},
  238. expectedDependencies: ["_id", "a.b"],
  239. expectedJsonRepresentation: {a:{b:true}}
  240. });
  241. },
  242. "should be able to include a nested field within an array": function testIncludeArrayNested() {
  243. /** Include a nested field within an array. */
  244. var expr = ObjectExpression.createRoot();
  245. expr.includePath("a.b");
  246. assertExpectedResult({
  247. expression: expr,
  248. expected: {_id:0,a:[{b:5},{b:2},{}]},
  249. source: {_id:0,a:[{b:5,c:6},{b:2,c:9},{c:7},[],2],z:1},
  250. expectedDependencies: ["_id", "a.b"],
  251. expectedJsonRepresentation: {a:{b:true}}
  252. });
  253. },
  254. "should NOT include non-root '_id' field implicitly": function testExcludeNonRootId() {
  255. /** Don't include not root '_id' field implicitly. */
  256. var expr = ObjectExpression.createRoot();
  257. expr.includePath("a.b");
  258. assertExpectedResult({
  259. expression: expr,
  260. source: {_id:0, a:{_id:1, b:1}},
  261. expected: {_id:0, a:{b:1}},
  262. expectedDependencies: ["_id", "a.b"],
  263. expectedJsonRepresentation: {a:{b:true}}
  264. });
  265. },
  266. "should be able to project a computed expression": function testComputed() {
  267. /** Project a computed expression. */
  268. var expr = ObjectExpression.createRoot();
  269. expr.addField("a", ConstantExpression.create(5));
  270. assertExpectedResult({
  271. expression: expr,
  272. source: {_id:0},
  273. expected: {_id:0, a:5},
  274. expectedDependencies: ["_id"],
  275. expectedJsonRepresentation: {a:{$const:5}},
  276. expectedIsSimple: false
  277. });
  278. },
  279. "should be able to project a computed expression replacing an existing field": function testComputedReplacement() {
  280. /** Project a computed expression replacing an existing field. */
  281. var expr = ObjectExpression.createRoot();
  282. expr.addField("a", ConstantExpression.create(5));
  283. assertExpectedResult({
  284. expression: expr,
  285. source: {_id:0, a:99},
  286. expected: {_id:0, a:5},
  287. expectedDependencies: ["_id"],
  288. expectedJsonRepresentation: {a:{$const:5}},
  289. expectedIsSimple: false
  290. });
  291. },
  292. "should NOT be able to project an undefined value": function testComputedUndefined() {
  293. /** An undefined value is passed through */
  294. var expr = ObjectExpression.createRoot();
  295. expr.addField("a", ConstantExpression.create(undefined));
  296. assertExpectedResult({
  297. expression: expr,
  298. source: {_id:0},
  299. expected: {_id:0, a:undefined},
  300. expectedDependencies: ["_id"],
  301. expectedJsonRepresentation: {a:{$const:undefined}},
  302. expectedIsSimple: false
  303. });
  304. },
  305. "should be able to project a computed expression replacing an existing field with Undefined": function testComputedUndefinedReplacement() {
  306. /** Project a computed expression replacing an existing field with Undefined. */
  307. var expr = ObjectExpression.createRoot();
  308. expr.addField("a", ConstantExpression.create(undefined));
  309. assertExpectedResult({
  310. expression: expr,
  311. source: {_id:0, a:99},
  312. expected: {_id:0, a:undefined},
  313. expectedDependencies: ["_id"],
  314. expectedJsonRepresentation: {a:{$const:undefined}},
  315. expectedIsSimple: false
  316. });
  317. },
  318. "should be able to project a null value": function testComputedNull() {
  319. /** A null value is projected. */
  320. var expr = ObjectExpression.createRoot();
  321. expr.addField("a", ConstantExpression.create(null));
  322. assertExpectedResult({
  323. expression: expr,
  324. source: {_id:0},
  325. expected: {_id:0, a:null},
  326. expectedDependencies: ["_id"],
  327. expectedJsonRepresentation: {a:{$const:null}},
  328. expectedIsSimple: false
  329. });
  330. },
  331. "should be able to project a nested value": function testComputedNested() {
  332. /** A nested value is projected. */
  333. var expr = ObjectExpression.createRoot();
  334. expr.addField("a.b", ConstantExpression.create(5));
  335. assertExpectedResult({
  336. expression: expr,
  337. source: {_id:0},
  338. expected: {_id:0, a:{b:5}},
  339. expectedDependencies: ["_id"],
  340. expectedJsonRepresentation: {a:{b:{$const:5}}},
  341. expectedIsSimple: false
  342. });
  343. },
  344. "should be able to project a field path": function testComputedFieldPath() {
  345. /** A field path is projected. */
  346. var expr = ObjectExpression.createRoot();
  347. expr.addField("a", FieldPathExpression.create("x"));
  348. assertExpectedResult({
  349. expression: expr,
  350. source: {_id:0, x:4},
  351. expected: {_id:0, a:4},
  352. expectedDependencies: ["_id", "x"],
  353. expectedJsonRepresentation: {a:"$x"},
  354. expectedIsSimple: false
  355. });
  356. },
  357. "should be able to project a nested field path": function testComputedNestedFieldPath() {
  358. /** A nested field path is projected. */
  359. var expr = ObjectExpression.createRoot();
  360. expr.addField("a.b", FieldPathExpression.create("x.y"));
  361. assertExpectedResult({
  362. expression: expr,
  363. source: {_id:0, x:{y:4}},
  364. expected: {_id:0, a:{b:4}},
  365. expectedDependencies: ["_id", "x.y"],
  366. expectedJsonRepresentation: {a:{b:"$x.y"}},
  367. expectedIsSimple: false
  368. });
  369. },
  370. "should NOT project an empty subobject expression for a missing field": function testEmptyNewSubobject() {
  371. /** An empty subobject expression for a missing field is not projected. */
  372. var expr = ObjectExpression.createRoot();
  373. // Create a sub expression returning an empty object.
  374. var subExpr = ObjectExpression.create();
  375. subExpr.addField("b", FieldPathExpression.create("a.b"));
  376. expr.addField("a", subExpr);
  377. assertExpectedResult({
  378. expression: expr,
  379. source: {_id:0},
  380. expected: {_id:0},
  381. expectedDependencies: ["_id", "a.b"],
  382. expectedJsonRepresentation: {a:{b:"$a.b"}},
  383. expectedIsSimple: false
  384. });
  385. },
  386. "should be able to project a non-empty new subobject": function testNonEmptyNewSubobject() {
  387. /** A non empty subobject expression for a missing field is projected. */
  388. var expr = ObjectExpression.createRoot();
  389. // Create a sub expression returning an empty object.
  390. var subExpr = ObjectExpression.create();
  391. subExpr.addField("b", ConstantExpression.create(6));
  392. expr.addField("a", subExpr);
  393. assertExpectedResult({
  394. expression: expr,
  395. source: {_id:0},
  396. expected: {_id:0, a:{b:6}},
  397. expectedDependencies: ["_id"],
  398. expectedJsonRepresentation: {a:{b:{$const:6}}},
  399. expectedIsSimple: false
  400. });
  401. },
  402. "should be able to project two computed fields within a common parent": function testAdjacentDottedComputedFields() {
  403. /** Two computed fields within a common parent. */
  404. var expr = ObjectExpression.createRoot();
  405. expr.addField("a.b", ConstantExpression.create(6));
  406. expr.addField("a.c", ConstantExpression.create(7));
  407. assertExpectedResult({
  408. expression: expr,
  409. source: {_id:0},
  410. expected: {_id:0, a:{b:6, c:7}},
  411. expectedDependencies: ["_id"],
  412. expectedJsonRepresentation: {a:{b:{$const:6},c:{$const:7}}},
  413. expectedIsSimple: false
  414. });
  415. },
  416. "should be able to project two computed fields within a common parent (w/ one case dotted)": function testAdjacentDottedAndNestedComputedFields() {
  417. /** Two computed fields within a common parent, in one case dotted. */
  418. var expr = ObjectExpression.createRoot();
  419. expr.addField("a.b", ConstantExpression.create(6));
  420. var subExpr = ObjectExpression.create();
  421. subExpr.addField("c", ConstantExpression.create(7));
  422. expr.addField("a", subExpr);
  423. assertExpectedResult({
  424. expression: expr,
  425. source: {_id:0},
  426. expected: {_id:0, a:{b:6, c:7}},
  427. expectedDependencies: ["_id"],
  428. expectedJsonRepresentation: {a:{b:{$const:6},c:{$const:7}}},
  429. expectedIsSimple: false
  430. });
  431. },
  432. "should be able to project two computed fields within a common parent (in another case dotted)": function testAdjacentNestedAndDottedComputedFields() {
  433. /** Two computed fields within a common parent, in another case dotted. */
  434. var expr = ObjectExpression.createRoot();
  435. var subExpr = ObjectExpression.create();
  436. subExpr.addField("b", ConstantExpression.create(6));
  437. expr.addField("a", subExpr);
  438. expr.addField("a.c", ConstantExpression.create(7));
  439. assertExpectedResult({
  440. expression: expr,
  441. source: {_id:0},
  442. expected: {_id:0, a:{b:6, c:7}},
  443. expectedDependencies: ["_id"],
  444. expectedJsonRepresentation: {a:{b:{$const:6},c:{$const:7}}},
  445. expectedIsSimple: false
  446. });
  447. },
  448. "should be able to project two computed fields within a common parent (nested rather than dotted)": function testAdjacentNestedComputedFields() {
  449. /** Two computed fields within a common parent, nested rather than dotted. */
  450. var expr = ObjectExpression.createRoot();
  451. var subExpr1 = ObjectExpression.create();
  452. subExpr1.addField("b", ConstantExpression.create(6));
  453. expr.addField("a", subExpr1);
  454. var subExpr2 = ObjectExpression.create();
  455. subExpr2.addField("c", ConstantExpression.create(7));
  456. expr.addField("a", subExpr2);
  457. assertExpectedResult({
  458. expression: expr,
  459. source: {_id:0},
  460. expected: {_id:0, a:{b:6, c:7}},
  461. expectedDependencies: ["_id"],
  462. expectedJsonRepresentation: {a:{b:{$const:6},c:{$const:7}}},
  463. expectedIsSimple: false
  464. });
  465. },
  466. "should be able to project multiple nested fields out of order without affecting output order": function testAdjacentNestedOrdering() {
  467. /** Field ordering is preserved when nested fields are merged. */
  468. var expr = ObjectExpression.createRoot();
  469. expr.addField("a.b", ConstantExpression.create(6));
  470. var subExpr = ObjectExpression.create();
  471. // Add field 'd' then 'c'. Expect the same field ordering in the result doc.
  472. subExpr.addField("d", ConstantExpression.create(7));
  473. subExpr.addField("c", ConstantExpression.create(8));
  474. expr.addField("a", subExpr);
  475. assertExpectedResult({
  476. expression: expr,
  477. source: {_id:0},
  478. expected: {_id:0, a:{b:6, d:7, c:8}},
  479. expectedDependencies: ["_id"],
  480. expectedJsonRepresentation: {a:{b:{$const:6},d:{$const:7},c:{$const:8}}},
  481. expectedIsSimple: false
  482. });
  483. },
  484. "should be able to project adjacent fields two levels deep": function testMultipleNestedFields() {
  485. /** Adjacent fields two levels deep. */
  486. var expr = ObjectExpression.createRoot();
  487. expr.addField("a.b.c", ConstantExpression.create(6));
  488. var bSubExpression = ObjectExpression.create();
  489. bSubExpression.addField("d", ConstantExpression.create(7));
  490. var aSubExpression = ObjectExpression.create();
  491. aSubExpression.addField("b", bSubExpression);
  492. expr.addField("a", aSubExpression);
  493. assertExpectedResult({
  494. expression: expr,
  495. source: {_id:0},
  496. expected: {_id:0, a:{b:{c:6, d:7}}},
  497. expectedDependencies: ["_id"],
  498. expectedJsonRepresentation: {a:{b:{c:{$const:6},d:{$const:7}}}},
  499. expectedIsSimple: false
  500. });
  501. },
  502. "should throw an Error if two expressions generate the same field": function testConflictingExpressionFields() {
  503. /** Two expressions cannot generate the same field. */
  504. var expr = ObjectExpression.createRoot();
  505. expr.addField("a", ConstantExpression.create(5));
  506. assert.throws(function() {
  507. expr.addField("a", ConstantExpression.create(6)); // Duplicate field.
  508. }, Error);
  509. },
  510. "should throw an Error if an expression field conflicts with an inclusion field": function testConflictingInclusionExpressionFields() {
  511. /** An expression field conflicts with an inclusion field. */
  512. var expr = ObjectExpression.createRoot();
  513. expr.includePath("a");
  514. assert.throws(function() {
  515. expr.addField("a", ConstantExpression.create(6));
  516. }, Error);
  517. },
  518. "should throw an Error if an inclusion field conflicts with an expression field": function testConflictingExpressionInclusionFields() {
  519. /** An inclusion field conflicts with an expression field. */
  520. var expr = ObjectExpression.createRoot();
  521. expr.addField("a", ConstantExpression.create(5));
  522. assert.throws(function() {
  523. expr.includePath("a");
  524. }, Error);
  525. },
  526. "should throw an Error if an object expression conflicts with a constant expression": function testConflictingObjectConstantExpressionFields() {
  527. /** An object expression conflicts with a constant expression. */
  528. var expr = ObjectExpression.createRoot();
  529. var subExpr = ObjectExpression.create();
  530. subExpr.includePath("b");
  531. expr.addField("a", subExpr);
  532. assert.throws(function() {
  533. expr.addField("a.b", ConstantExpression.create(6));
  534. }, Error);
  535. },
  536. "should throw an Error if a constant expression conflicts with an object expression": function testConflictingConstantObjectExpressionFields() {
  537. /** A constant expression conflicts with an object expression. */
  538. var expr = ObjectExpression.createRoot();
  539. expr.addField("a.b", ConstantExpression.create(6));
  540. var subExpr = ObjectExpression.create();
  541. subExpr.includePath("b");
  542. assert.throws(function() {
  543. expr.addField("a", subExpr);
  544. }, Error);
  545. },
  546. "should throw an Error if two nested expressions cannot generate the same field": function testConflictingNestedFields() {
  547. /** Two nested expressions cannot generate the same field. */
  548. var expr = ObjectExpression.createRoot();
  549. expr.addField("a.b", ConstantExpression.create(5));
  550. assert.throws(function() {
  551. expr.addField("a.b", ConstantExpression.create(6)); // Duplicate field.
  552. }, Error);
  553. },
  554. "should throw an Error if an expression is created for a subfield of another expression": function testConflictingFieldAndSubfield() {
  555. /** An expression cannot be created for a subfield of another expression. */
  556. var expr = ObjectExpression.createRoot();
  557. expr.addField("a", ConstantExpression.create(5));
  558. assert.throws(function() {
  559. expr.addField("a.b", ConstantExpression.create(5));
  560. }, Error);
  561. },
  562. "should throw an Error if an expression is created for a nested field of another expression.": function testConflictingFieldAndNestedField() {
  563. /** An expression cannot be created for a nested field of another expression. */
  564. var expr = ObjectExpression.createRoot();
  565. expr.addField("a", ConstantExpression.create(5));
  566. var subExpr = ObjectExpression.create();
  567. subExpr.addField("b", ConstantExpression.create(5));
  568. assert.throws(function() {
  569. expr.addField("a", subExpr);
  570. }, Error);
  571. },
  572. "should throw an Error if an expression is created for a parent field of another expression": function testConflictingSubfieldAndField() {
  573. /** An expression cannot be created for a parent field of another expression. */
  574. var expr = ObjectExpression.createRoot();
  575. expr.addField("a.b", ConstantExpression.create(5));
  576. assert.throws(function() {
  577. expr.addField("a", ConstantExpression.create(5));
  578. }, Error);
  579. },
  580. "should throw an Error if an expression is created for a parent of a nested field": function testConflictingNestedFieldAndField() {
  581. /** An expression cannot be created for a parent of a nested field. */
  582. var expr = ObjectExpression.createRoot();
  583. var subExpr = ObjectExpression.create();
  584. subExpr.addField("b", ConstantExpression.create(5));
  585. expr.addField("a", subExpr);
  586. assert.throws(function() {
  587. expr.addField("a", ConstantExpression.create(5));
  588. }, Error);
  589. },
  590. "should be able to evaluate expressions in general": function testEvaluate() {
  591. /**
  592. * evaluate() does not supply an inclusion document.
  593. * Inclusion spec'd fields are not included.
  594. * (Inclusion specs are not generally expected/allowed in cases where evaluate is called instead of addToDocument.)
  595. */
  596. var expr = ObjectExpression.createRoot();
  597. expr.includePath("a");
  598. expr.addField("b", ConstantExpression.create(5));
  599. expr.addField("c", FieldPathExpression.create("a"));
  600. var res = expr.evaluateInternal(new Variables(1, {_id:0, a:1}));
  601. assert.deepEqual({"b":5, "c":1}, res);
  602. },
  603. },
  604. };