Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Unit tests for the query parsers and functions

* UT-1234 more update

* update with more query instance

* remove the comments between two quiry statment
  • Loading branch information
fenglianxu authored Jul 4, 2017
1 parent 5c21868 commit edfed42
Show file tree
Hide file tree
Showing 12 changed files with 674 additions and 15 deletions.
1 change: 0 additions & 1 deletion packages/composer-common/lib/query/limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class Limit {

this.ast = ast;
this.select = select;
this.process();
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/composer-common/lib/query/orderby.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class OrderBy {
for(let n=0; n < this.ast.sort.length; n++) {
this.sortCriteria.push( new Sort(this, this.ast.sort[n]));
}
} else {
throw new IllegalModelException('Invalid AST');
}
}

Expand Down
2 changes: 0 additions & 2 deletions packages/composer-common/lib/query/skip.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ class Skip {
if(!select || !ast) {
throw new IllegalModelException('Invalid Select or AST');
}

this.ast = ast;
this.select = select;
this.process();
}

/**
Expand Down
11 changes: 7 additions & 4 deletions packages/composer-common/lib/query/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,17 @@ class Sort {
* @private
*/
process() {
this.propertyPath = this.ast.fieldName.name;

this.direction = 'ASC';
if(!this.ast.fieldName) {
throw new IllegalModelException('Invalid AST');
}else{
this.propertyPath = this.ast.fieldName.name;
}

if(this.ast.direction) {
this.direction = this.ast.direction;
} else {
this.direction = 'ASC';
}
this.direction = this.ast.direction;
}

/**
Expand Down
1 change: 0 additions & 1 deletion packages/composer-common/lib/query/where.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class Where {

this.ast = ast;
this.select = select;
this.process();
}

/**
Expand Down
94 changes: 94 additions & 0 deletions packages/composer-common/test/query/limit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const Limit = require('../../lib/query/limit');
const parser = require('../../lib/query/parser');

const Select = require('../../lib/query/select');

require('chai').should();
const sinon = require('sinon');

describe('Limit', () => {

// let modelManager;
let sandbox;
let mockSelect;

const selectWhereLimit = parser.parse('SELECT org.acme.Driver WHERE (prop = "value") LIMIT 10', { startRule: 'SelectStatement' });

beforeEach(() => {
sandbox = sinon.sandbox.create();
mockSelect = sinon.createStubInstance(Select);
});

afterEach(() => {
sandbox.restore();
});

describe('#constructor', () => {

it('should throw when null select provided', () => {
(() => {
new Limit(null, null);
}).should.throw(/Invalid Select or AST/);
});

it('should throw when null ast provided', () => {
(() => {
new Limit(mockSelect, null);
}).should.throw(/Invalid Select or AST/);
});

});

describe('#accept', () => {

it('should call the visitor', () => {
let l = new Limit(mockSelect, selectWhereLimit.limit);
let visitor = {
visit: sinon.stub()
};
l.accept(visitor, ['some', 'args']);
sinon.assert.calledOnce(visitor.visit);
sinon.assert.calledWith(visitor.visit, l, ['some', 'args']);
});
});

describe('#getSelect', () => {

it('should return the select', () => {
const l = new Limit(mockSelect, selectWhereLimit.limit);
l.getSelect().should.equal(mockSelect);
});

});

describe('#validate', () => {

// TODO no validation method implemented yet

});

describe('#getAST', () => {

it('should return the limit clause', () => {
const l = new Limit(mockSelect, selectWhereLimit.limit);
l.getAST().type.should.equal('Literal');
l.getAST().value.should.equal(10);
});
});
});
95 changes: 95 additions & 0 deletions packages/composer-common/test/query/orderby.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const OrderBy = require('../../lib/query/orderby');
const parser = require('../../lib/query/parser');
const Select = require('../../lib/query/select');
const sinon = require('sinon');

describe('OrderBy', () => {

let sandbox;
let mockSelect;

const selectWhereOrderBy = parser.parse('SELECT org.acme.Driver WHERE (prop = "value") ORDER BY [id ASC]', { startRule: 'SelectStatement' });

beforeEach(() => {
sandbox = sinon.sandbox.create();
mockSelect = sinon.createStubInstance(Select);
});

afterEach(() => {
sandbox.restore();
});

describe('#constructor', () => {

it('should throw when null select provided', () => {
(() => {
new OrderBy(null, null);
}).should.throw(/Invalid Select or AST/);
});

it('should throw when null ast provided', () => {
(() => {
new OrderBy(mockSelect, null);
}).should.throw(/Invalid Select or AST/);
});

it('should throw when invalid ast provided', () => {
(() => {
new OrderBy(mockSelect, 'force failure');
}).should.throw(/Invalid AST/);
});
});

describe('#accept', () => {

it('should call the visitor', () => {
let o = new OrderBy(mockSelect, selectWhereOrderBy.orderBy);
let visitor = {
visit: sinon.stub()
};
o.accept(visitor, ['some', 'args']);
sinon.assert.calledOnce(visitor.visit);
sinon.assert.calledWith(visitor.visit, o, ['some', 'args']);
});
});

describe('#getSelect', () => {

it('should return the select', () => {
const o = new OrderBy(mockSelect, selectWhereOrderBy.orderBy);
o.getSelect().should.equal(mockSelect);
});

});

describe('#validate', () => {

// TODO no validation method implemented yet

});

describe('#getSortCriteria', () => {

it('should return the sort critera', () => {
const o = new OrderBy(mockSelect, selectWhereOrderBy.orderBy);
o.should.be.an.instanceOf(OrderBy);
o.getSortCriteria().should.have.lengthOf(1);
});
});
});
Loading

0 comments on commit edfed42

Please sign in to comment.