mirror of https://github.com/pelias/api.git
Browse Source
Conflicts: controller/suggest.js test/unit/controller/suggest.js test/unit/mock/backend.jspull/42/head
Harish Krishna
10 years ago
24 changed files with 1024 additions and 119 deletions
@ -0,0 +1,72 @@
|
||||
|
||||
var service = { |
||||
suggest: require('../service/suggest'), |
||||
mget: require('../service/mget') |
||||
}; |
||||
var geojsonify = require('../helper/geojsonify').search; |
||||
|
||||
function setup( backend, query ){ |
||||
|
||||
// allow overriding of dependencies
|
||||
backend = backend || require('../src/backend'); |
||||
query = query || require('../query/suggest'); |
||||
|
||||
function controller( req, res, next ){ |
||||
|
||||
// backend command
|
||||
var cmd = { |
||||
index: 'pelias', |
||||
body: query( req.clean ) |
||||
}; |
||||
|
||||
// responder
|
||||
function reply( docs ){ |
||||
|
||||
// convert docs to geojson
|
||||
var geojson = geojsonify( docs ); |
||||
|
||||
// response envelope
|
||||
geojson.date = new Date().getTime(); |
||||
|
||||
// respond
|
||||
return res.status(200).json( geojson ); |
||||
} |
||||
|
||||
// query backend
|
||||
service.suggest( backend, cmd, function( err, suggested ){ |
||||
|
||||
// error handler
|
||||
if( err ){ return next( err ); } |
||||
|
||||
// no documents suggested, return empty array to avoid ActionRequestValidationException
|
||||
if( !Array.isArray( suggested ) || !suggested.length ){ |
||||
return reply([]); |
||||
} |
||||
|
||||
// map suggester output to mget query
|
||||
var query = suggested.map( function( doc ) { |
||||
var idParts = doc.text.split(':'); |
||||
return { |
||||
_index: 'pelias', |
||||
_type: idParts[0], |
||||
_id: idParts[1] |
||||
}; |
||||
}); |
||||
|
||||
service.mget( backend, query, function( err, docs ){ |
||||
|
||||
// error handler
|
||||
if( err ){ return next( err ); } |
||||
|
||||
// reply
|
||||
return reply( docs ); |
||||
|
||||
}); |
||||
}); |
||||
|
||||
} |
||||
|
||||
return controller; |
||||
} |
||||
|
||||
module.exports = setup; |
@ -0,0 +1,223 @@
|
||||
# valid suggest query |
||||
|
||||
*Generated: Thu Nov 06 2014 11:44:20 GMT-0500 (EST)* |
||||
## Request |
||||
```javascript |
||||
{ |
||||
"protocol": "http:", |
||||
"host": "localhost", |
||||
"method": "GET", |
||||
"port": 3100, |
||||
"path": "/suggest/nearby?input=a&lat=29.49136&lon=-82.50622" |
||||
} |
||||
``` |
||||
|
||||
## Response |
||||
```javascript |
||||
Status: 200 |
||||
{ |
||||
"x-powered-by": "mapzen", |
||||
"charset": "utf8", |
||||
"cache-control": "public,max-age=60", |
||||
"server": "Pelias/0.0.0", |
||||
"access-control-allow-origin": "*", |
||||
"access-control-allow-methods": "GET", |
||||
"access-control-allow-headers": "X-Requested-With,content-type", |
||||
"access-control-allow-credentials": "true", |
||||
"content-type": "application/json; charset=utf-8", |
||||
"content-length": "2034", |
||||
"etag": "W/\"Do9VJ5hCbynTxDjtm5fNlg==\"", |
||||
"date": "Thu, 06 Nov 2014 16:44:19 GMT", |
||||
"connection": "close" |
||||
} |
||||
``` |
||||
```javascript |
||||
{ |
||||
"type": "FeatureCollection", |
||||
"features": [ |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.05231, |
||||
29.17998 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"text": "Abiding Hope E V Lutheran Church, Marion County, Florida", |
||||
"score": 1, |
||||
"type": "geoname", |
||||
"id": "4145572" |
||||
} |
||||
}, |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.10231, |
||||
29.21942 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"text": "Abundant Harvest Ministries, Marion County, Florida", |
||||
"score": 1, |
||||
"type": "geoname", |
||||
"id": "4145578" |
||||
} |
||||
}, |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.50622, |
||||
29.49136 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"text": "Adam, Alachua County, Florida", |
||||
"score": 1, |
||||
"type": "geoname", |
||||
"id": "4145612" |
||||
} |
||||
}, |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.75374, |
||||
35.17789 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"text": "Adams Branch, Transylvania County, North Carolina", |
||||
"score": 1, |
||||
"type": "geoname", |
||||
"id": "4452189" |
||||
} |
||||
}, |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.83012, |
||||
29.4783 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"text": "Adamsville Cemetery, Levy County, Florida", |
||||
"score": 1, |
||||
"type": "geoname", |
||||
"id": "4145634" |
||||
} |
||||
}, |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.01511, |
||||
35.17289 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"text": "Africa School (historical), Spartanburg County, South Carolina", |
||||
"score": 1, |
||||
"type": "geoname", |
||||
"id": "4569065" |
||||
} |
||||
}, |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.20426, |
||||
29.25192 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"text": "Agape Baptist Church, Marion County, Florida", |
||||
"score": 1, |
||||
"type": "geoname", |
||||
"id": "4145673" |
||||
} |
||||
}, |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.14954, |
||||
29.19248 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"text": "Agnew Cemetery, Marion County, Florida", |
||||
"score": 1, |
||||
"type": "geoname", |
||||
"id": "4145677" |
||||
} |
||||
}, |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.75429, |
||||
35.16928 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"text": "Aiken Mountain, Transylvania County, North Carolina", |
||||
"score": 1, |
||||
"type": "geoname", |
||||
"id": "4452268" |
||||
} |
||||
}, |
||||
{ |
||||
"type": "Feature", |
||||
"geometry": { |
||||
"type": "Point", |
||||
"coordinates": [ |
||||
-82.15912, |
||||
29.47877 |
||||
] |
||||
}, |
||||
"properties": { |
||||
"text": "Alachua County Fire Rescue Station 31, Alachua County, Florida", |
||||
"score": 1, |
||||
"type": "geoname", |
||||
"id": "4152402" |
||||
} |
||||
} |
||||
], |
||||
"date": 1415292259785 |
||||
} |
||||
``` |
||||
|
||||
## Tests |
||||
|
||||
### âś“ 200 ok |
||||
```javascript |
||||
response.statusCode.should.equal 200 |
||||
``` |
||||
|
||||
### âś“ valid response |
||||
```javascript |
||||
now = new Date().getTime() |
||||
should.exist json |
||||
should.not.exist json.error |
||||
json.date.should.be.within now-5000, now+5000 |
||||
``` |
||||
|
||||
### âś“ valid geojson |
||||
```javascript |
||||
json.type.should.equal 'FeatureCollection' |
||||
json.features.should.be.instanceof Array |
||||
``` |
||||
|
@ -0,0 +1,16 @@
|
||||
|
||||
#> valid suggest query |
||||
path: '/suggest/nearby?input=a&lat=29.49136&lon=-82.50622' |
||||
|
||||
#? 200 ok |
||||
response.statusCode.should.equal 200 |
||||
|
||||
#? valid response |
||||
now = new Date().getTime() |
||||
should.exist json |
||||
should.not.exist json.error |
||||
json.date.should.be.within now-5000, now+5000 |
||||
|
||||
#? valid geojson |
||||
json.type.should.equal 'FeatureCollection' |
||||
json.features.should.be.instanceof Array |
@ -0,0 +1,100 @@
|
||||
|
||||
var setup = require('../../../controller/doc'), |
||||
mockBackend = require('../mock/backend'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('valid interface', function(t) { |
||||
t.equal(typeof setup, 'function', 'setup is a function'); |
||||
t.equal(typeof setup(), 'function', 'setup returns a controller'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// functionally test controller (backend success)
|
||||
module.exports.tests.functional_success = function(test, common) { |
||||
|
||||
// expected geojson features for 'client/doc/ok/1' fixture
|
||||
var expected = [{ |
||||
type: 'Feature', |
||||
geometry: { |
||||
type: 'Point', |
||||
coordinates: [ -50.5, 100.1 ] |
||||
}, |
||||
properties: { |
||||
id: 'myid1', |
||||
type: 'mytype1', |
||||
layer: 'mytype1', |
||||
name: 'test name1', |
||||
admin0: 'country1', |
||||
admin1: 'state1', |
||||
admin2: 'city1', |
||||
text: 'test name1, city1, state1' |
||||
} |
||||
}, { |
||||
type: 'Feature', |
||||
geometry: { |
||||
type: 'Point', |
||||
coordinates: [ -51.5, 100.2 ] |
||||
}, |
||||
properties: { |
||||
id: 'myid2', |
||||
type: 'mytype2', |
||||
layer: 'mytype2', |
||||
name: 'test name2', |
||||
admin0: 'country2', |
||||
admin1: 'state2', |
||||
admin2: 'city2', |
||||
text: 'test name2, city2, state2' |
||||
} |
||||
}]; |
||||
|
||||
test('functional success', function(t) { |
||||
var backend = mockBackend( 'client/mget/ok/1', function( cmd ){ |
||||
t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'a' } ] } }, 'correct backend command'); |
||||
}); |
||||
var controller = setup( backend ); |
||||
var res = { |
||||
status: function( code ){ |
||||
t.equal(code, 200, 'status set'); |
||||
return res; |
||||
}, |
||||
json: function( json ){ |
||||
t.equal(typeof json, 'object', 'returns json'); |
||||
t.equal(typeof json.date, 'number', 'date set'); |
||||
t.equal(json.type, 'FeatureCollection', 'valid geojson'); |
||||
t.true(Array.isArray(json.features), 'features is array'); |
||||
t.deepEqual(json.features, expected, 'values correctly mapped'); |
||||
t.end(); |
||||
} |
||||
}; |
||||
controller( { clean: { ids: [ {'id' : 123, 'type': 'a' } ] } }, res ); |
||||
}); |
||||
}; |
||||
|
||||
// functionally test controller (backend failure)
|
||||
module.exports.tests.functional_failure = function(test, common) { |
||||
test('functional failure', function(t) { |
||||
var backend = mockBackend( 'client/mget/fail/1', function( cmd ){ |
||||
t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'b' } ] } }, 'correct backend command'); |
||||
}); |
||||
var controller = setup( backend ); |
||||
var next = function( message ){ |
||||
t.equal(message,'a backend error occurred','error passed to errorHandler'); |
||||
t.end(); |
||||
}; |
||||
controller( { clean: { ids: [ {'id' : 123, 'type': 'b' } ] } }, undefined, next ); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('GET /doc ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -0,0 +1,107 @@
|
||||
|
||||
var setup = require('../../../controller/suggest'), |
||||
mockBackend = require('../mock/backend'), |
||||
mockQuery = require('../mock/query'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('valid interface', function(t) { |
||||
t.equal(typeof setup, 'function', 'setup is a function'); |
||||
t.equal(typeof setup(), 'function', 'setup returns a controller'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// functionally test controller (backend success)
|
||||
module.exports.tests.functional_success = function(test, common) { |
||||
|
||||
// expected geojson features for 'client/mget/ok/1' fixture
|
||||
var expected = [{ |
||||
type: 'Feature', |
||||
geometry: { |
||||
type: 'Point', |
||||
coordinates: [ -50.5, 100.1 ] |
||||
}, |
||||
properties: { |
||||
id: 'myid1', |
||||
type: 'mytype1', |
||||
layer: 'mytype1', |
||||
name: 'test name1', |
||||
admin0: 'country1', |
||||
admin1: 'state1', |
||||
admin2: 'city1', |
||||
text: 'test name1, city1, state1' |
||||
} |
||||
}, { |
||||
type: 'Feature', |
||||
geometry: { |
||||
type: 'Point', |
||||
coordinates: [ -51.5, 100.2 ] |
||||
}, |
||||
properties: { |
||||
id: 'myid2', |
||||
type: 'mytype2', |
||||
layer: 'mytype2', |
||||
name: 'test name2', |
||||
admin0: 'country2', |
||||
admin1: 'state2', |
||||
admin2: 'city2', |
||||
text: 'test name2, city2, state2' |
||||
} |
||||
}]; |
||||
|
||||
test('functional success', function(t) { |
||||
var i = 0; |
||||
var backend = mockBackend( 'client/suggest/ok/1', function( cmd ){ |
||||
// the backend executes 2 commands, so we check them both
|
||||
if( ++i === 1 ){ |
||||
t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias' }, 'correct suggest command'); |
||||
} else { |
||||
t.deepEqual(cmd, { body: { docs: [ { _id: 'mockid1', _index: 'pelias', _type: 'mocktype' }, { _id: 'mockid2', _index: 'pelias', _type: 'mocktype' } ] } }, 'correct mget command'); |
||||
} |
||||
}); |
||||
var controller = setup( backend, mockQuery() ); |
||||
var res = { |
||||
status: function( code ){ |
||||
t.equal(code, 200, 'status set'); |
||||
return res; |
||||
}, |
||||
json: function( json ){ |
||||
t.equal(typeof json, 'object', 'returns json'); |
||||
t.equal(typeof json.date, 'number', 'date set'); |
||||
t.equal(json.type, 'FeatureCollection', 'valid geojson'); |
||||
t.true(Array.isArray(json.features), 'features is array'); |
||||
t.deepEqual(json.features, expected, 'values correctly mapped'); |
||||
t.end(); |
||||
} |
||||
}; |
||||
controller( { clean: { a: 'b' } }, res ); |
||||
}); |
||||
}; |
||||
|
||||
// functionally test controller (backend failure)
|
||||
module.exports.tests.functional_failure = function(test, common) { |
||||
test('functional failure', function(t) { |
||||
var backend = mockBackend( 'client/suggest/fail/1', function( cmd ){ |
||||
t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias' }, 'correct backend command'); |
||||
}); |
||||
var controller = setup( backend, mockQuery() ); |
||||
var next = function( message ){ |
||||
t.equal(message,'a backend error occurred','error passed to errorHandler'); |
||||
t.end(); |
||||
}; |
||||
controller( { clean: { a: 'b' } }, undefined, next ); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('GET /suggest/nearby ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -0,0 +1,84 @@
|
||||
|
||||
var setup = require('../../../service/mget'), |
||||
mockBackend = require('../mock/backend'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('valid interface', function(t) { |
||||
t.equal(typeof setup, 'function', 'setup is a function'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// functionally test service
|
||||
module.exports.tests.functional_success = function(test, common) { |
||||
|
||||
var expected = [ |
||||
{ |
||||
_id: 'myid1', _type: 'mytype1', |
||||
value: 1, |
||||
center_point: { lat: 100.1, lon: -50.5 }, |
||||
name: { default: 'test name1' }, |
||||
admin0: 'country1', admin1: 'state1', admin2: 'city1' |
||||
},
|
||||
{ |
||||
_id: 'myid2', _type: 'mytype2', |
||||
value: 2, |
||||
center_point: { lat: 100.2, lon: -51.5 }, |
||||
name: { default: 'test name2' }, |
||||
admin0: 'country2', admin1: 'state2', admin2: 'city2' |
||||
} |
||||
]; |
||||
|
||||
test('valid query', function(t) { |
||||
var backend = mockBackend( 'client/mget/ok/1', function( cmd ){ |
||||
t.deepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'a' } ] } }, 'correct backend command'); |
||||
}); |
||||
setup( backend, [ { _id: 123, _index: 'pelias', _type: 'a' } ], function(err, data) { |
||||
t.true(Array.isArray(data), 'returns an array'); |
||||
data.forEach(function(d) { |
||||
t.true(typeof d === 'object', 'valid object'); |
||||
}); |
||||
t.deepEqual(data, expected, 'values correctly mapped') |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
// functionally test service
|
||||
module.exports.tests.functional_failure = function(test, common) { |
||||
|
||||
test('invalid query', function(t) { |
||||
var invalid_queries = [ |
||||
{ _id: 123, _index: 'pelias' }, |
||||
{ _id: 123, _type: 'a' }, |
||||
{ _index: 'pelias', _type: 'a' }, |
||||
{ } |
||||
]; |
||||
|
||||
var backend = mockBackend( 'client/mget/fail/1', function( cmd ){ |
||||
t.notDeepEqual(cmd, { body: { docs: [ { _id: 123, _index: 'pelias', _type: 'a' } ] } }, 'incorrect backend command'); |
||||
}); |
||||
invalid_queries.forEach(function(query) { |
||||
setup( backend, [ query ], function(err, data) { |
||||
t.equal(err, 'a backend error occurred','error passed to errorHandler'); |
||||
t.equal(data, undefined, 'data is undefined'); |
||||
}); |
||||
}); |
||||
t.end(); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('SERVICE /mget ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -0,0 +1,84 @@
|
||||
|
||||
var setup = require('../../../service/search'), |
||||
mockBackend = require('../mock/backend'); |
||||
|
||||
var example_valid_es_query = { body: { a: 'b' }, index: 'pelias' }; |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('valid interface', function(t) { |
||||
t.equal(typeof setup, 'function', 'setup is a function'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// functionally test service
|
||||
module.exports.tests.functional_success = function(test, common) { |
||||
|
||||
var expected = [ |
||||
{ |
||||
_id: 'myid1', _type: 'mytype1', |
||||
value: 1, |
||||
center_point: { lat: 100.1, lon: -50.5 }, |
||||
name: { default: 'test name1' }, |
||||
admin0: 'country1', admin1: 'state1', admin2: 'city1' |
||||
},
|
||||
{ |
||||
_id: 'myid2', _type: 'mytype2', |
||||
value: 2, |
||||
center_point: { lat: 100.2, lon: -51.5 }, |
||||
name: { default: 'test name2' }, |
||||
admin0: 'country2', admin1: 'state2', admin2: 'city2' |
||||
} |
||||
]; |
||||
|
||||
test('valid ES query', function(t) { |
||||
var backend = mockBackend( 'client/search/ok/1', function( cmd ){ |
||||
t.deepEqual(cmd, example_valid_es_query, 'no change to the command'); |
||||
}); |
||||
setup( backend, example_valid_es_query, function(err, data) { |
||||
t.true(Array.isArray(data), 'returns an array'); |
||||
data.forEach(function(d) { |
||||
t.true(typeof d === 'object', 'valid object'); |
||||
}); |
||||
t.deepEqual(data, expected, 'values correctly mapped') |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
// functionally test service
|
||||
module.exports.tests.functional_failure = function(test, common) { |
||||
|
||||
test('invalid ES query', function(t) { |
||||
var invalid_queries = [ |
||||
{ }, |
||||
{ foo: 'bar' } |
||||
]; |
||||
|
||||
var backend = mockBackend( 'client/search/fail/1', function( cmd ){ |
||||
t.notDeepEqual(cmd, example_valid_es_query, 'incorrect backend command'); |
||||
}); |
||||
invalid_queries.forEach(function(query) { |
||||
setup( backend, [ query ], function(err, data) { |
||||
t.equal(err, 'a backend error occurred','error passed to errorHandler'); |
||||
t.equal(data, undefined, 'data is undefined'); |
||||
}); |
||||
}); |
||||
t.end(); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('SERVICE /search ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -0,0 +1,72 @@
|
||||
|
||||
var setup = require('../../../service/suggest'), |
||||
mockBackend = require('../mock/backend'); |
||||
|
||||
var example_valid_es_query = { body: { a: 'b' }, index: 'pelias' }; |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('valid interface', function(t) { |
||||
t.equal(typeof setup, 'function', 'setup is a function'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// functionally test service
|
||||
module.exports.tests.functional_success = function(test, common) { |
||||
|
||||
var expected = [
|
||||
{ score: 1, text: 'mocktype:mockid1' },
|
||||
{ score: 2, text: 'mocktype:mockid2' }
|
||||
]; |
||||
|
||||
test('valid ES query', function(t) { |
||||
var backend = mockBackend( 'client/suggest/ok/1', function( cmd ){ |
||||
t.deepEqual(cmd, example_valid_es_query, 'no change to the command'); |
||||
}); |
||||
setup( backend, example_valid_es_query, function(err, data) { |
||||
t.true(Array.isArray(data), 'returns an array'); |
||||
data.forEach(function(d) { |
||||
t.true(typeof d === 'object', 'valid object'); |
||||
}); |
||||
t.deepEqual(data, expected, 'values correctly mapped') |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
// functionally test service
|
||||
module.exports.tests.functional_failure = function(test, common) { |
||||
|
||||
test('invalid ES query', function(t) { |
||||
var invalid_queries = [ |
||||
{ }, |
||||
{ foo: 'bar' } |
||||
]; |
||||
|
||||
var backend = mockBackend( 'client/suggest/fail/1', function( cmd ){ |
||||
t.notDeepEqual(cmd, example_valid_es_query, 'incorrect backend command'); |
||||
}); |
||||
invalid_queries.forEach(function(query) { |
||||
setup( backend, [ query ], function(err, data) { |
||||
t.equal(err, 'a backend error occurred','error passed to errorHandler'); |
||||
t.equal(data, undefined, 'data is undefined'); |
||||
}); |
||||
}); |
||||
t.end(); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('SERVICE /suggest ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
Loading…
Reference in new issue