| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- "use strict";
- var assert = require("assert"),
- Runner = require("../../../lib/query/Runner"),
- CursorDocumentSource = require("../../../lib/pipeline/documentSources/CursorDocumentSource"),
- LimitDocumentSource = require("../../../lib/pipeline/documentSources/LimitDocumentSource"),
- MatchDocumentSource = require("../../../lib/pipeline/documentSources/MatchDocumentSource"),
- ArrayRunner = require("../../../lib/query/ArrayRunner"),
- DocumentSourceRunner = require("../../../lib/query/DocumentSourceRunner");
- module.exports = {
- "ArrayRunner": {
- "#constructor": {
- "should accept an array of data": function(){
- var cds = new CursorDocumentSource(null, new ArrayRunner([]), null),
- pipeline = [];
- assert.doesNotThrow(function(){
- var ar = new DocumentSourceRunner(cds, pipeline);
- });
- },
- "should fail if not given a document source or pipeline": function(){
- var cds = new CursorDocumentSource(null, new ArrayRunner([]), null);
-
- assert.throws(function(){
- var ar = new DocumentSourceRunner();
- });
- assert.throws(function(){
- var ar = new DocumentSourceRunner(123);
- });
- assert.throws(function(){
- var ar = new DocumentSourceRunner(cds, 123);
- });
- },
- "should coalesce the pipeline into the given documentsource": function(){
- var cds = new CursorDocumentSource(null, new ArrayRunner([]), null),
- pipeline = [new LimitDocumentSource(3), new MatchDocumentSource({"a":true})],
- expected = [{$match:{a:true}}];
-
- var ds = new DocumentSourceRunner(cds, pipeline);
- var actual = pipeline.map(function(d){return d.serialize();});
-
- assert.deepEqual(expected, actual);
- }
- },
- "#getNext": {
- "should return the next item in the given documentsource": function(done){
- var cds = new CursorDocumentSource(null, new ArrayRunner([1,2,3]), null),
- pipeline = [new LimitDocumentSource(3)];
-
- var ds = new DocumentSourceRunner(cds, pipeline);
-
- ds.getNext(function(err, out, state){
- assert.strictEqual(state, Runner.RunnerState.RUNNER_ADVANCED);
- assert.strictEqual(out, 1);
- ds.getNext(function(err, out, state){
- assert.strictEqual(state, Runner.RunnerState.RUNNER_ADVANCED);
- assert.strictEqual(out, 2);
- ds.getNext(function(err, out, state){
- assert.strictEqual(state, Runner.RunnerState.RUNNER_ADVANCED);
- assert.strictEqual(out, 3);
- done();
- });
- });
- });
- },
- "should return EOF if there is nothing left in the given documentsource": function(done){
- var cds = new CursorDocumentSource(null, new ArrayRunner([1,2,3]), null),
- pipeline = [new LimitDocumentSource({}, 1)];
-
- var ds = new DocumentSourceRunner(cds, pipeline);
-
- ds.getNext(function(err, out, state){
- assert.strictEqual(state, Runner.RunnerState.RUNNER_ADVANCED);
- assert.strictEqual(out, 1);
- ds.getNext(function(err, out, state){
- assert.strictEqual(state, Runner.RunnerState.RUNNER_EOF);
- assert.strictEqual(out, null);
- done();
- });
- });
- }
- },
- "#getInfo": {
- "should return nothing if explain flag is not set": function(){
- var cds = new CursorDocumentSource(null, new ArrayRunner([1,2,3]), null),
- pipeline = [new LimitDocumentSource({}, 1)];
-
- var ds = new DocumentSourceRunner(cds, pipeline);
- assert.strictEqual(ds.getInfo(), undefined);
- },
- "should return information about the runner if explain flag is set": function(){
- var cds = new CursorDocumentSource(null, new ArrayRunner([1,2,3]), null),
- pipeline = [new LimitDocumentSource({}, 1)];
- var ds = new DocumentSourceRunner(cds, pipeline);
-
- assert.deepEqual(ds.getInfo(true), {
- "type": "DocumentSourceRunner",
- "docSrc": {
- "$cursor": {
- "query": undefined,
- "sort": null,
- "limit": 1,
- "fields": null,
- "plan": {
- "type": "ArrayRunner",
- "nDocs": 3,
- "position": 0,
- "state": "RUNNER_ADVANCED"
- }
- }
- },
- "state": "RUNNER_ADVANCED"
- });
- }
- },
- "#reset": {
- "should dispose of the documentSource": function(){
- var cds = new CursorDocumentSource(null, new ArrayRunner([1,2,3]), null),
- pipeline = [new LimitDocumentSource({}, 1)];
- var ds = new DocumentSourceRunner(cds, pipeline);
-
- ds.reset();
- assert.deepEqual(ds.getInfo(true), {
- "type": "DocumentSourceRunner",
- "docSrc": {
- "$cursor": {
- "query": undefined,
- "sort": null,
- "limit": 1,
- "fields": null,
- "plan": {
- "type": "ArrayRunner",
- "nDocs": 0,
- "position": 0,
- "state": "RUNNER_DEAD"
- }
- }
- },
- "state": "RUNNER_DEAD"
- });
- }
- }
- }
- };
- if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run();
|