UNPKG

3.75 kBJavaScriptView Raw
1var should= require('chai').should(),
2 assert= require('chai').assert,
3 AWS = require('aws-sdk'),
4 _= require('underscore'),
5 fs= require('fs'),
6 dyngo= require('../index.js');
7
8const _noerr= function (done)
9 {
10 return function (err)
11 {
12 if (err) console.log(err,err.stack);
13 should.not.exist(err);
14 done();
15 };
16 },
17 accept= function (code,done)
18 {
19 return function (err)
20 {
21 if (err.code==code)
22 done();
23 else
24 done(err);
25 };
26 },
27 profiles= function ()
28 {
29 return JSON.parse(fs.readFileSync('test/sample.small.json','utf8'));
30 };
31
32
33describe('text',function ()
34{
35 var db;
36
37 before(function (done)
38 {
39 dyngo({ dynamo: { endpoint: new AWS.Endpoint('http://localhost:8000') }, hints: false },
40 function (err,_db)
41 {
42 db= _db;
43
44 db.test.remove().success(function ()
45 {
46 var ensure= function ()
47 {
48 return db.test.ensureIndex
49 ({
50 name: 'S',
51 $text: function (item)
52 {
53 return _.pick(item,['name','company','about']);
54 }
55 })
56 };
57
58 ensure().success(function ()
59 {
60 db.test.indexes[0].drop().success(function ()
61 {
62 ensure().success(function ()
63 {
64 var p= profiles();
65
66 db.test.save(p)
67 .success(done)
68 .error(done);
69 }).error(done);
70 }).error(done);
71 }).error(_noerr(done));
72 })
73 .error(_noerr(done));
74
75 });
76 });
77
78 it('Can do bloodhound like full text searches', function (done)
79 {
80 db.test.findOne({ $text: 'interdum adi' }) // inspired by bloodhound search with whitespace tokenizer
81 .result(function (obj)
82 {
83 obj.name.should.equal('Duncan Wall');
84 done();
85 })
86 .error(_noerr(done));
87 });
88
89 it('Can chain full text searches with normal fields', function (done)
90 {
91 db.test.findOne({ name: 'Duncan Wall', $text: 'interdum adi' })
92 .result(function (obj)
93 {
94 obj.name.should.equal('Duncan Wall');
95
96 db.test.findOne({ name: 'Hedley Booth', $text: 'interdum adi' })
97 .result(function (obj)
98 {
99 should.not.exist(obj);
100 done();
101 })
102 .error(accept('notfound',done));
103 })
104 .error(_noerr(done));
105 });
106
107 it('Should not find any result', function (done)
108 {
109 db.test.findOne({ $text: 'Xinterdum adi' }) // inspired by bloodhound search with whitespace tokenizer
110 .result(function (obj)
111 {
112 should.not.exist(obj);
113 done();
114 })
115 .error(accept('notfound',done));
116 });
117});