UNPKG

4.26 kBJavaScriptView Raw
1
2var extractor = require('../extractor/fields');
3var fixtures = require('./fixtures/_index');
4
5module.exports.extractor = {};
6
7module.exports.extractor.invalidFields = function(test, common) {
8 test('constructor: invalidFields', function(t) {
9 try {
10 var proxy = extractor( null, function(){} );
11 }
12 catch (e){
13 t.equal(e.message, 'invalid fields supplied', 'exception thrown');
14 t.end();
15 }
16 });
17 test('constructor: emptyFields', function(t) {
18 try {
19 var proxy = extractor( [], function(){} );
20 }
21 catch (e){
22 t.equal(e.message, 'invalid fields supplied', 'exception thrown');
23 t.end();
24 }
25 });
26 test('constructor: invalidCallback', function(t) {
27 var proxy = extractor( ['foo'] );
28 t.equal(typeof proxy, 'function', 'function returned');
29 try { proxy(); }
30 catch (e){
31 t.equal(e.message, 'undefined is not a function', 'exception thrown');
32 t.end();
33 }
34 });
35}
36
37module.exports.extractor.respEmitsError = function(test, common) {
38 test('resp: emitsError', function(t) {
39 var proxy = extractor( ['foo','bar'], function( err, resp ){
40 t.equal(typeof err, 'string', 'error emitted');
41 t.equal(err, 'an upstream error', err);
42 t.end();
43 });
44 t.equal(typeof proxy, 'function', 'function returned');
45 proxy( 'an upstream error' ); // upstream error
46 });
47}
48
49module.exports.extractor.respNoHits = function(test, common) {
50 test('resp: respNoHits', function(t) {
51 var proxy = extractor( ['foo'], function( err, resp ){
52 t.equal(err, undefined, 'no error emitted');
53 t.equal(resp, undefined, 'no result returned');
54 t.end();
55 });
56 t.equal(typeof proxy, 'function', 'function returned');
57 proxy(); // no hits
58 });
59}
60
61// Failed to find any fields matching 'foo'
62module.exports.extractor.respGotHits = function(test, common) {
63 test('resp: respGotHits', function(t) {
64 var proxy = extractor( ['foo'], function( err, resp ){
65 t.equal(err, undefined, 'no error emitted');
66 t.equal(Array.isArray(resp), true, 'array returned');
67 t.equal(resp.length, 0, 'array contains 0 records');
68 t.end();
69 });
70 t.equal(typeof proxy, 'function', 'function returned');
71 proxy( null, fixtures.fields );
72 });
73}
74
75// Matched all fields
76module.exports.extractor.respGotHits = function(test, common) {
77 test('resp: respGotHits', function(t) {
78 var proxy = extractor( ['admin0','admin1','admin2'], function( err, resp ){
79 t.equal(err, undefined, 'no error emitted');
80 t.equal(Array.isArray(resp), true, 'array returned');
81 t.equal(resp.length, 1, 'array contains 1 record');
82 t.equal(resp[0]._id, '6289144', 'field returned');
83 t.equal(resp[0]._index, 'pelias', 'field returned');
84 t.equal(resp[0]._type, 'geoname', 'field returned');
85 t.equal(resp[0].admin0, 'United Kingdom', 'field returned');
86 t.equal(resp[0].admin1, 'England', 'field returned');
87 t.equal(resp[0].admin2, 'Greater London', 'field returned');
88 t.end();
89 });
90 t.equal(typeof proxy, 'function', 'function returned');
91 proxy( null, fixtures.fields );
92 });
93}
94
95module.exports.extractor.respFailedHits = function(test, common) {
96 test('resp: respFailedHits', function(t) {
97 var proxy = extractor( ['admin0','admin1','admin2'], function( err, resp ){
98 t.equal(err, 'invalid fields returned', 'error emitted');
99 t.equal(resp, undefined, 'no results returned');
100 t.end();
101 });
102 t.equal(typeof proxy, 'function', 'function returned');
103 proxy( null, fixtures.failedFields );
104 });
105}
106
107module.exports.extractor.genericFailure = function(test, common) {
108 test('resp: genericFailure', function(t) {
109 var proxy = extractor( ['foo'], function( err, resp ){
110 t.equal(err, fixtures.genericfail.error, 'error emitted');
111 t.equal(resp, undefined, 'no results returned');
112 t.end();
113 });
114 t.equal(typeof proxy, 'function', 'function returned');
115 proxy( null, fixtures.genericfail );
116 });
117}
118
119module.exports.all = function (tape, common) {
120
121 function test(name, testFunction) {
122 return tape('fields extractor: ' + name, testFunction)
123 }
124
125 for( var testCase in module.exports.extractor ){
126 module.exports.extractor[testCase](test, common);
127 }
128}
\No newline at end of file