var trimByGranularity = require('../../../middleware/trimByGranularity')(); module.exports.tests = {}; module.exports.tests.trimByGranularity = function(test, common) { test('empty res and req should not throw exception', function(t) { function testIt() { trimByGranularity({}, {}, function() {}); } t.doesNotThrow(testIt, 'an exception should not have been thrown'); t.end(); }); test('all records with fallback.* matched_queries name should retain only venues when they are most granular', function(t) { var req = { clean: {} }; var res = { data: [ { name: 'venue 1', _matched_queries: ['fallback.venue'] }, { name: 'venue 2', _matched_queries: ['fallback.venue'] }, { name: 'address 1', _matched_queries: ['fallback.address'] }, { name: 'neighbourhood 1', _matched_queries: ['fallback.neighbourhood'] }, { name: 'locality 1', _matched_queries: ['fallback.locality'] }, { name: 'county 1', _matched_queries: ['fallback.county'] }, { name: 'region 1', _matched_queries: ['fallback.region'] }, { name: 'country 1', _matched_queries: ['fallback.country'] }, { name: 'unknown', _matched_queries: ['fallback.unknown'] } ] }; var expected_data = [ { name: 'venue 1', _matched_queries: ['fallback.venue'] }, { name: 'venue 2', _matched_queries: ['fallback.venue'] }, ]; function testIt() { trimByGranularity(req, res, function() { t.deepEquals(res.data, expected_data, 'only venue records should be here'); t.end(); }); } testIt(); }); test('all records with fallback.* matched_queries name should retain only addresses when they are most granular', function(t) { var req = { clean: {} }; var res = { data: [ { name: 'address 1', _matched_queries: ['fallback.address'] }, { name: 'address 2', _matched_queries: ['fallback.address'] }, { name: 'neighbourhood 1', _matched_queries: ['fallback.neighbourhood'] }, { name: 'locality 1', _matched_queries: ['fallback.locality'] }, { name: 'county 1', _matched_queries: ['fallback.county'] }, { name: 'region 1', _matched_queries: ['fallback.region'] }, { name: 'country 1', _matched_queries: ['fallback.country'] }, { name: 'unknown', _matched_queries: ['fallback.unknown'] } ] }; var expected_data = [ { name: 'address 1', _matched_queries: ['fallback.address'] }, { name: 'address 2', _matched_queries: ['fallback.address'] }, ]; function testIt() { trimByGranularity(req, res, function() { t.deepEquals(res.data, expected_data, 'only address records should be here'); t.end(); }); } testIt(); }); test('all records with fallback.* matched_queries name should retain only neighbourhoods when they are most granular', function(t) { var req = { clean: {} }; var res = { data: [ { name: 'neighbourhood 1', _matched_queries: ['fallback.neighbourhood'] }, { name: 'neighbourhood 2', _matched_queries: ['fallback.neighbourhood'] }, { name: 'locality 1', _matched_queries: ['fallback.locality'] }, { name: 'county 1', _matched_queries: ['fallback.county'] }, { name: 'region 1', _matched_queries: ['fallback.region'] }, { name: 'country 1', _matched_queries: ['fallback.country'] }, { name: 'unknown', _matched_queries: ['fallback.unknown'] } ] }; var expected_data = [ { name: 'neighbourhood 1', _matched_queries: ['fallback.neighbourhood'] }, { name: 'neighbourhood 2', _matched_queries: ['fallback.neighbourhood'] }, ]; function testIt() { trimByGranularity(req, res, function() { t.deepEquals(res.data, expected_data, 'only neighbourhood records should be here'); t.end(); }); } testIt(); }); test('all records with fallback.* matched_queries name should retain only localities when they are most granular', function(t) { var req = { clean: {} }; var res = { data: [ { name: 'locality 1', _matched_queries: ['fallback.locality'] }, { name: 'locality 2', _matched_queries: ['fallback.locality'] }, { name: 'county 1', _matched_queries: ['fallback.county'] }, { name: 'region 1', _matched_queries: ['fallback.region'] }, { name: 'country 1', _matched_queries: ['fallback.country'] }, { name: 'unknown', _matched_queries: ['fallback.unknown'] } ] }; var expected_data = [ { name: 'locality 1', _matched_queries: ['fallback.locality'] }, { name: 'locality 2', _matched_queries: ['fallback.locality'] }, ]; function testIt() { trimByGranularity(req, res, function() { t.deepEquals(res.data, expected_data, 'only locality records should be here'); t.end(); }); } testIt(); }); test('all records with fallback.* matched_queries name should retain only counties when they are most granular', function(t) { var req = { clean: {} }; var res = { data: [ { name: 'county 1', _matched_queries: ['fallback.county'] }, { name: 'county 2', _matched_queries: ['fallback.county'] }, { name: 'region 1', _matched_queries: ['fallback.region'] }, { name: 'country 1', _matched_queries: ['fallback.country'] }, { name: 'unknown', _matched_queries: ['fallback.unknown'] } ] }; var expected_data = [ { name: 'county 1', _matched_queries: ['fallback.county'] }, { name: 'county 2', _matched_queries: ['fallback.county'] }, ]; function testIt() { trimByGranularity(req, res, function() { t.deepEquals(res.data, expected_data, 'only county records should be here'); t.end(); }); } testIt(); }); test('all records with fallback.* matched_queries name should retain only regions when they are most granular', function(t) { var req = { clean: {} }; var res = { data: [ { name: 'region 1', _matched_queries: ['fallback.region'] }, { name: 'region 2', _matched_queries: ['fallback.region'] }, { name: 'country 1', _matched_queries: ['fallback.country'] }, { name: 'unknown', _matched_queries: ['fallback.unknown'] } ] }; var expected_data = [ { name: 'region 1', _matched_queries: ['fallback.region'] }, { name: 'region 2', _matched_queries: ['fallback.region'] }, ]; function testIt() { trimByGranularity(req, res, function() { t.deepEquals(res.data, expected_data, 'only region records should be here'); t.end(); }); } testIt(); }); test('all records with fallback.* matched_queries name should retain only countries when they are most granular', function(t) { var req = { clean: {} }; var res = { data: [ { name: 'country 1', _matched_queries: ['fallback.country'] }, { name: 'country 2', _matched_queries: ['fallback.country'] }, { name: 'unknown', _matched_queries: ['fallback.unknown'] } ] }; var expected_data = [ { name: 'country 1', _matched_queries: ['fallback.country'] }, { name: 'country 2', _matched_queries: ['fallback.country'] }, ]; function testIt() { trimByGranularity(req, res, function() { t.deepEquals(res.data, expected_data, 'only country records should be here'); t.end(); }); } testIt(); }); test('presence of any non-fallback.* named queries should not trim', function(t) { var req = { clean: {} }; var res = { data: [ { name: 'region', _matched_queries: ['fallback.region'] }, { name: 'country', _matched_queries: ['fallback.country'] }, { name: 'result with non-named query' } ] }; var expected_data = [ { name: 'region', _matched_queries: ['fallback.region'] }, { name: 'country', _matched_queries: ['fallback.country'] }, { name: 'result with non-named query' } ]; function testIt() { trimByGranularity(req, res, function() { t.deepEquals(res.data, expected_data, 'all should results should have been retained'); t.end(); }); } testIt(); }); }; module.exports.all = function (tape, common) { function test(name, testFunction) { return tape('[middleware] trimByGranularity: ' + name, testFunction); } for( var testCase in module.exports.tests ){ module.exports.tests[testCase](test, common); } };