UNPKG

4.38 kBJavaScriptView Raw
1'use strict';
2
3module.exports = function (dbType, context) {
4
5 describe(dbType + ': deep fields', function () {
6
7 it('deep fields', function () {
8 var db = context.db;
9 var index = {
10 "index": {
11 "fields": [
12 "foo.bar"
13 ]
14 },
15 "name": "foo-index",
16 "type": "json"
17 };
18 return db.createIndex(index).then(function () {
19 return db.bulkDocs([
20 {_id: 'doc', foo: {bar: 'a'}},
21 ]);
22 }).then(function () {
23 return db.find({
24 selector: {'foo.bar': 'a'},
25 fields: ['_id']
26 });
27 }).then(function (res) {
28 res.should.deep.equal({
29 "docs": [
30 {
31 "_id": "doc"
32 }
33 ]
34 });
35 });
36 });
37
38 it('deeper fields', function () {
39 var db = context.db;
40 var index = {
41 "index": {
42 "fields": [
43 "foo.bar.baz"
44 ]
45 },
46 "name": "foo-index",
47 "type": "json"
48 };
49 return db.createIndex(index).then(function () {
50 return db.bulkDocs([
51 {_id: 'doc', foo: {bar: {baz: 'a'}}},
52 ]);
53 }).then(function () {
54 return db.find({
55 selector: {'foo.bar.baz': 'a'},
56 fields: ['_id']
57 });
58 }).then(function (res) {
59 res.should.deep.equal({
60 "docs": [
61 {
62 "_id": "doc"
63 }
64 ]
65 });
66 });
67 });
68
69 it('deep fields escaped', function () {
70 var db = context.db;
71 var index = {
72 "index": {
73 "fields": [
74 "foo\\.bar"
75 ]
76 },
77 "name": "foo-index",
78 "type": "json"
79 };
80 return db.createIndex(index).then(function () {
81 return db.bulkDocs([
82 {_id: 'doc1', foo: {bar: 'a'}},
83 {_id: 'doc2', 'foo.bar': 'a'}
84 ]);
85 }).then(function () {
86 return db.find({
87 selector: {'foo\\.bar': 'a'},
88 fields: ['_id']
89 });
90 }).then(function (res) {
91 res.should.deep.equal({
92 "docs": [{ "_id": "doc2"}]
93 });
94 });
95 });
96
97 it('should create a deep multi mapper', function () {
98 var db = context.db;
99 var index = {
100 "index": {
101 "fields": [
102 "foo.bar", "bar.baz"
103 ]
104 }
105 };
106 return db.createIndex(index).then(function () {
107 return db.bulkDocs([
108 {_id: 'a', foo: {bar: 'yo'}, bar: {baz: 'hey'}},
109 {_id: 'b', foo: {bar: 'sup'}, bar: {baz: 'dawg'}}
110 ]);
111 }).then(function () {
112 return db.find({
113 selector: {"foo.bar": 'yo', "bar.baz": 'hey'},
114 fields: ['_id']
115 });
116 }).then(function (res) {
117 res.docs.should.deep.equal([{_id: 'a'}]);
118 return db.find({
119 selector: {"foo.bar": 'yo', "bar.baz": 'sup'},
120 fields: ['_id']
121 });
122 }).then(function (res) {
123 res.docs.should.have.length(0);
124 return db.find({
125 selector: {"foo.bar": 'bruh', "bar.baz": 'nah'},
126 fields: ['_id']
127 });
128 }).then(function (res) {
129 res.docs.should.have.length(0);
130 });
131 });
132
133 it('should create a deep multi mapper, tricky docs', function () {
134 var db = context.db;
135 var index = {
136 "index": {
137 "fields": [
138 "foo.bar", "bar.baz"
139 ]
140 }
141 };
142 return db.createIndex(index).then(function () {
143 return db.bulkDocs([
144 {_id: 'a', foo: {bar: 'yo'}, bar: {baz: 'hey'}},
145 {_id: 'b', foo: {bar: 'sup'}, bar: {baz: 'dawg'}},
146 {_id: 'c', foo: true, bar: "yo"},
147 {_id: 'd', foo: null, bar: []}
148 ]);
149 }).then(function () {
150 return db.find({
151 selector: {"foo.bar": 'yo', "bar.baz": 'hey'},
152 fields: ['_id']
153 });
154 }).then(function (res) {
155 res.docs.should.deep.equal([{_id: 'a'}]);
156 return db.find({
157 selector: {"foo.bar": 'yo', "bar.baz": 'sup'},
158 fields: ['_id']
159 });
160 }).then(function (res) {
161 res.docs.should.have.length(0);
162 return db.find({
163 selector: {"foo.bar": 'bruh', "bar.baz": 'nah'},
164 fields: ['_id']
165 });
166 }).then(function (res) {
167 res.docs.should.have.length(0);
168 });
169 });
170 });
171};