@seedalpha/ticker
Version:
ticker parser of seedalpha
307 lines (205 loc) • 8.69 kB
JavaScript
var should = require('chai').should();
var ticker = require('../lib/ticker/index');
describe('seed-ticker', function() {
describe('ticker recognition & parsing', function() {
it('should return object w/ properties code & exchange', function() {
var result = ticker.parse('0005.hk');
result.should.be.an('object');
result.should.have.property('code');
result.should.have.property('exchange');
});
it('should remove leading zero(s) for numeric ticker codes', function() {
var result = ticker.parse('0005.hk');
result.should.be.an('object');
result.should.have.property('code', '5');
result.should.have.property('exchange', 'HK');
});
it('should allow whitespace(s) / dot / colon between ticker code and exchange code', function() {
['0005.hk', '0005 hk', '0005:hk'].forEach(function(str) {
var result = ticker.parse(str);
result.should.be.an('object');
result.should.have.property('code', '5');
result.should.have.property('exchange', 'HK');
});
});
it('should allow mulitple whitespace(s) / whitespace(s) around the dot / colon', function() {
['0005 . hk', '0005 hk', '0005 : hk'].forEach(function(str) {
var result = ticker.parse(str);
result.should.be.an('object');
result.should.have.property('code', '5');
result.should.have.property('exchange', 'HK');
});
});
it('should be case insensitive', function() {
var result = ticker.parse('5 Hk');
result.should.be.an('object');
result.should.have.property('code', '5');
result.should.have.property('exchange', 'HK');
result = ticker.parse('Aapl N');
result.should.be.an('object');
result.should.have.property('code', 'AAPL');
result.should.have.property('exchange', 'N');
result = ticker.parse('aapl us');
result.should.be.an('object');
result.should.have.property('code', 'AAPL');
result.should.have.property('exchange', 'US');
});
it('should support 6 digit pattern for china/korea stocks', function() {
var result = ticker.parse('600004 SS');
result.should.be.an('object');
result.should.have.property('code', '600004');
result.should.have.property('exchange', 'SS');
result = ticker.parse('028150 KS');
result.should.be.an('object');
result.should.have.property('code', '28150');
result.should.have.property('exchange', 'KS');
});
it('should support CapIQ style pattern', function() {
var result = ticker.parse('SEHK:5');
result.should.be.an('object');
result.should.have.property('code', '5');
result.should.have.property('exchange', 'SEHK');
});
it('should return null for unrecognized patterns', function() {
var result = ticker.parse('0005..hk');
should.equal(result, null);
result = ticker.parse('0005. .hk');
should.equal(result, null);
result = ticker.parse('random string');
should.equal(result, null);
result = ticker.parse('0005hk');
should.equal(result, null);
result = ticker.parse('5');
should.equal(result, null);
result = ticker.parse('1234567 hk');
should.equal(result, null);
result = ticker.parse('ABCDEF SS');
should.equal(result, null);
});
});
describe('stock lookup', function() {
it('should be able to directly use the result from parsing function', function(done) {
var result = ticker.parse('0005.hk');
ticker.lookup(result, function(err, result) {
should.equal(err, null);
should.exist(result);
result.should.have.property('stock');
result.should.have.property('exchange');
result.stock.should.have.property('code');
result.stock.should.have.property('name');
result.exchange.should.have.property('codes');
result.exchange.should.have.property('key');
done();
});
});
it('should be able to directly use the result from parsing function with CapIQ pattern', function(done) {
var result = ticker.parse('KOSDAQ:A093190');
ticker.lookup(result, function(err, result) {
should.equal(err, null);
should.exist(result);
result.should.have.property('stock');
result.should.have.property('exchange');
result.stock.should.have.property('code');
result.stock.should.have.property('name');
result.exchange.should.have.property('codes');
result.exchange.should.have.property('key');
done();
});
});
it('should also allow object with same format as parsing function\'s output', function(done) {
var obj = {
code: 'aapl',
exchange: 'us'
}
ticker.lookup(obj, function(err, result) {
should.equal(err, null);
should.exist(result);
result.should.have.property('stock');
result.should.have.property('exchange');
result.stock.should.have.property('code');
result.stock.should.have.property('name');
result.exchange.should.have.property('codes');
result.exchange.should.have.property('key');
done();
});
});
it('should support json dataset smaller than 2mb', function(done) {
var obj = {
code: 'lse',
exchange: 'l'
}
ticker.lookup(obj, function(err, result) {
should.equal(err, null);
should.exist(result);
result.should.have.property('stock');
result.should.have.property('exchange');
result.stock.should.have.property('code');
result.stock.should.have.property('name');
result.exchange.should.have.property('codes');
result.exchange.should.have.property('key');
done();
});
});
it('should callback with err when stock not found in dataset', function(done) {
var obj = {
code: 'gggg',
exchange: 'us'
}
ticker.lookup(obj, function(err, stock) {
should.exist(err);
should.not.exist(stock);
err.should.equal('stock not found');
done();
});
});
});
describe('get terms (keywords) for a stock', function() {
it('should callback with an array of strings', function(done) {
var obj = {
code: 'aapl',
exchange: 'us'
}
ticker.lookup(obj, function(err, result) {
ticker.getTerms(result, function(err, terms) {
should.equal(err, null);
should.exist(terms);
terms.should.be.an('array');
terms.forEach(function(term) {
term.should.be.a('string');
});
done();
});
});
});
it('should support argument for including non-padded numeric codes', function(done) {
var obj = ticker.parse('0005 hk');
ticker.lookup(obj, function(err, result) {
ticker.getTerms(result, function(err, terms) {
ticker.getTerms(result, true, function(err, moreTerms) {
terms.should.be.an('array');
moreTerms.should.be.an('array');
moreTerms.should.have.length.above(terms.length);
done();
});
});
});
});
it.skip('should support argument for including more related keywords as well', function(done) {
var obj = ticker.parse('0005 hk');
ticker.lookup(obj, function(err, result) {
ticker.getTerms(result, function(err, terms) {
ticker.getTerms(result, true, function(err, moreTerms) {
ticker.getTerms(result, true, true, function(err, evenMoreTerms) {
terms.should.be.an('array');
moreTerms.should.be.an('array');
evenMoreTerms.should.be.an('array');
moreTerms.should.have.length.above(terms.length);
evenMoreTerms.should.have.length.above(moreTerms.length);
done();
});
});
});
});
});
});
});